
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/runbar.h>
#include <hardware/cia.h>

struct Library *RunBarBase; /* Library Base*/

/* Filter ON Image */
UWORD chip fonData[39] =
{
	/* Plane 0 */
	0x0000,0x2F90,0x1000,0x6920,0x4020,0xD6E8,0x44E0,0x64F0,
	0x47E0,0x6FE0,0x1F80,0x1240,0x0000,
	/* Plane 1 */
	0x0000,0x2490,0x0040,0x4920,0x0710,0x9A58,0x0810,0x2C90,
	0x0010,0x4920,0x0040,0x1FC0,0x0000,
	/* Plane 2 */
	0x0000,0x2490,0x0F80,0x5F20,0x3820,0xB768,0x2760,0x27F0,
	0x00E0,0x4FE0,0x0F80,0x1240,0x0000
};

struct Image fon =
{
	0, 0,		/* LeftEdge, TopEdge */
	13, 13, 3,	/* Width, Height, Depth */
	fonData,	/* ImageData */
	0x0007, 0x0000,	/* PlanePick, PlaneOnOff */
	NULL		/* NextImage */
};

/* Filter OFF Image */
UWORD chip foffData[39] =
{
	/* Plane 0 */
	0x0000,0x0F80,0x1000,0x2000,0x4020,0x46E0,0x44E0,0x40E0,
	0x47E0,0x27C0,0x1F80,0x0000,0x0000,
	/* Plane 1 */
	0x0000,0x0000,0x0040,0x0020,0x0710,0x0810,0x0810,0x0810,
	0x0010,0x0020,0x0040,0x0F80,0x0000,
	/* Plane 2 */
	0x0000,0x0000,0x0F80,0x1F00,0x3820,0x3760,0x2760,0x0760,
	0x00E0,0x07C0,0x0F80,0x0000,0x0000
};
struct Image foff =
{
	0, 0,		/* LeftEdge, TopEdge */
	13, 13, 3,	/* Width, Height, Depth */
	foffData,	/* ImageData */
	0x0007, 0x0000,	/* PlanePick, PlaneOnOff */
	NULL		/* NextImage */
};

/* Menu Structure */
struct SBItem menu[]=
{
  {0,"Filter OFF"}, /* 0 - Normal, 1 - Disabled */ 
  {2,NULL},         /* 2 - Bar   ,15 - Menu End */
  {0,"Exit"},
  {15,NULL}     
};

/* Information about Menu */
struct RBInfo info =
{
  RB_VERSION,     /* Always set it to RB_VERSION*/
  "FilterControl",/* Menu name.Please use specific name, because */
                  /* this name will use for unique check. */
  RBF_UNIQUE      /* Will not allow duplicates. */
};
static  char   ver[]="$VER: Filter Control by Sergej Kravcenko 1.0 (24.11.96)";
/*--------------------Main Program-------------------------*/
void main()
{
    struct CIA *cia = (struct CIA *) 0xBFE001; 
    struct Task *prtask;
    BOOL my_close;
    struct MsgPort *port;
    struct RBMessage *my_message;
    ULONG  class,code,filtermode;
    
/*---------------Open Library------------------------------*/    
    RunBarBase = (struct Library *)
    OpenLibrary( "RunBar.library",1);
  
    if( RunBarBase == NULL) goto end;
    
/*------------------Get Task Pointer-----------------------*/    
    prtask = FindTask(NULL);
    
/*------------------Create RunBar Menu---------------------*/    
    if (RB_AddTask(prtask,menu,&foff,&info,&port) != 0) goto end;
    
/*------------------Turn filter off------------------------*/    
    cia->ciapra |= 0x02;
    filtermode = 0;
    
/*------------------Wait Port------------------------------*/    
    my_close = FALSE;
    while( my_close == FALSE )
    {
     WaitPort(port);
     my_message = (struct RBMessage *) GetMsg(port);
     if (my_message)
     {
        class    = my_message->rb_class;
        code     = my_message->rb_code;
        ReplyMsg((struct Message *) my_message );
        
        if(class == IDCMP_RUNBARCMD)
         {
          if (code == 2) my_close = TRUE;  
          if (code == 0)
          {
           if (filtermode == 0)
           {
        /*----------Turn Filter On-----------*/    
            filtermode = 1;
            cia->ciapra &= 0xFD;
            menu[0].sbi_Name = "Filter ON";
            RB_Edit(prtask,menu,&fon);
           }
           else 
           {
        /*----------Turn Filter Off------------*/    
            filtermode = 0;
            cia->ciapra |= 0x02;
            menu[0].sbi_Name = "Filter OFF";
            RB_Edit(prtask,menu,&foff);
           }
          }   
         } 
        if (class == IDCMP_RUNBARMSG)
           {
       /*------------Exit--------------------*/     
             if (code == RB_REMOVE) my_close = TRUE;      
           }                          
       
     }
    }
/*------------Remove RunBar Menu---------------------*/
/* If you get non NULL result, you must wait some time, and try */
/* again. */
    
    while (RB_RemoveTask(prtask)) Delay(10);
    
/*-----------Close Library---------------------------*/    
end:if ( RunBarBase ) CloseLibrary ((struct Library *) RunBarBase);   
}
