home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / varie / twitchymouse / source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-11  |  6.6 KB  |  333 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <exec/libraries.h>
  4. #include <libraries/commodities.h>
  5. #include <dos/dos.h>
  6. #include <devices/inputevent.h>
  7.  
  8. #include <intuition/intuitionbase.h>
  9.  
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12. #include <proto/intuition.h>
  13. #include <proto/utility.h>
  14. #include <proto/commodities.h>
  15. #include <proto/graphics.h>
  16.  
  17. #include <clib/alib_protos.h>
  18. #include <clib/extras_protos.h>
  19. #include <debug.h>
  20.  
  21. void OpenUI(void);
  22. void CloseUI(void);
  23.  
  24. int CXCRBK(void) {return(0);}
  25. int chkabort(void) {return(0);}
  26.  
  27. void ProcessMsg(void);
  28. void __asm __saveds CxFunction(register __a0 CxMsg *cxm, register __a1 CxObj *co);
  29.  
  30. /* CX stuff */
  31. struct MsgPort *Broker_mp;
  32. CxObj *Broker, *cocustom, *cosig;
  33.  
  34. struct Screen *MyScreen;
  35. struct Window *Window;
  36.  
  37. #define NAME "Twitchy Mouse"
  38.  
  39. char vstring[]="$VER: Twitchy_Mouse 1.0 "__AMIGADATE__;//                                              (1.0.1)
  40.  
  41. struct NewBroker newBroker=
  42. {
  43.   NB_VERSION,
  44.   NAME,
  45.   NAME,
  46.   "Fixes Wobbling Pointers",
  47.   NBU_UNIQUE | NBU_NOTIFY,
  48.   COF_SHOW_HIDE,
  49.   0,0,0
  50. };
  51.  
  52. struct InputEvent nev;
  53.  
  54. BOOL mine=FALSE;
  55.  
  56. struct Task *task;
  57. ULONG cxsigflag, sig, cxobjsig;
  58.  
  59. struct Library *CxBase, *IconBase, *GTLayoutBase;
  60.  
  61. struct Libs MyLibs[]=
  62. {
  63.   (APTR)&IconBase,      "icon.library",               39, 0,
  64.   (APTR)&IntuitionBase, "intuition.library",          39, 0,
  65.   (APTR)&CxBase,        "commodities.library",        39, 0,
  66.   (APTR)&GfxBase,       "graphics.library",           39, 0,
  67.   0,0,0
  68. };
  69.  
  70. struct Task *Me;
  71. BYTE   TSig;
  72. ULONG  TSignal;
  73.  
  74. void main (int argc, char **argv)
  75. {
  76.   UBYTE **ttypes;
  77.   CxMsg *msg;
  78.  
  79.   Me=FindTask(0);
  80.  
  81.   TSig=AllocSignal(-1);
  82.   TSignal=1<<TSig;
  83.  
  84.  
  85.   nev.ie_NextEvent=0;
  86.   nev.ie_Class=IECLASS_POINTERPOS;
  87.   
  88.   if(ex_OpenLibs(argc, (STRPTR)NAME , 0,0,0,MyLibs))  // from extras.lib
  89.   {
  90.     if(Broker_mp=CreateMsgPort())
  91.     {
  92.       newBroker.nb_Port=Broker_mp;
  93.       cxsigflag=1<<Broker_mp->mp_SigBit;
  94.       
  95.       ttypes=ArgArrayInit(argc,argv);
  96.       
  97.       newBroker.nb_Pri=(BYTE)ArgInt(ttypes,(STRPTR)"CX_PRIORITY",10);
  98.       if(Broker=CxBroker(&newBroker,NULL))
  99.       {
  100.         if(cocustom=CxCustom(CxFunction,0))
  101.         {
  102.           AttachCxObj(Broker,cocustom);
  103.           
  104.           if((sig=(ULONG)AllocSignal(-1)) != -1)
  105.           {
  106.             cxobjsig=1<<sig;
  107.             cxsigflag|=cxobjsig;
  108.             
  109.             task=FindTask(NULL);
  110.             if(cosig=CxSignal(task,sig))
  111.             {
  112.               AttachCxObj(cocustom,cosig);
  113.               ActivateCxObj(Broker,1);
  114.               ProcessMsg();
  115.               ActivateCxObj(Broker,0);
  116.             }
  117.             FreeSignal(sig);
  118.           }
  119.         }
  120.         DeleteCxObjAll(Broker);
  121.         
  122.         while(msg=(CxMsg *)GetMsg(Broker_mp))
  123.           ReplyMsg((struct Message *)msg);
  124.       }
  125.       DeletePort(Broker_mp);
  126.     }
  127.     ArgArrayDone();
  128.     ex_CloseLibs(MyLibs);
  129.   }              
  130. }
  131.  
  132. UBYTE Color=0;
  133.  
  134. void ProcessMsg(void)
  135. {
  136.   CxMsg *msg;
  137.   ULONG sigrcvd,msgid,waitsigs;
  138.   LONG returnvalue=1;
  139.  
  140.   nev.ie_Class=255;
  141.   
  142.   waitsigs=SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | cxsigflag | TSignal;
  143.   
  144.   while(returnvalue)
  145.   {
  146.     sigrcvd=Wait(waitsigs);
  147.  
  148.     if(TSignal & sigrcvd)
  149.     {
  150.       if(Window)
  151.       {
  152.         SetAPen(Window->RPort, Color);
  153.         Color++;
  154.         RectFill(Window->RPort, Window->BorderLeft, Window->BorderTop, Window->Width-Window->BorderRight-1, Window->Height-Window->BorderBottom-1);
  155.         Delay(1);
  156.       }
  157.     }
  158.  
  159.     while(msg=(CxMsg *)GetMsg(Broker_mp))
  160.     {
  161.       msgid=CxMsgID(msg);
  162.       ReplyMsg((struct Message *)msg);
  163.       
  164.       switch(msgid)
  165.       {
  166.         case CXCMD_DISABLE:
  167.           ActivateCxObj(Broker,0);
  168.           break;
  169.         case CXCMD_ENABLE:
  170.           ActivateCxObj(Broker,1);
  171.           break;
  172.         case CXCMD_APPEAR:
  173.           OpenUI();
  174.           break;
  175.         case CXCMD_DISAPPEAR:
  176.           CloseUI();
  177.           break;
  178.         case CXCMD_UNIQUE:
  179.         case CXCMD_KILL:
  180.           returnvalue=0;
  181.           break;
  182.       }
  183.     }
  184.     if(sigrcvd & SIGBREAKF_CTRL_C) returnvalue=0;
  185.   }
  186.   CloseUI();
  187. }
  188.  
  189. void OpenUI(void)
  190. {
  191.   if(Window) return;
  192.   
  193.   if(MyScreen=LockPubScreen(0))
  194.   {
  195.     if(Window=OpenWindowTags(0,
  196.                       WA_DragBar,        1,
  197.                       WA_Title,          "Twitch-o-Sensor",
  198.                       WA_InnerWidth,     100,
  199.                       WA_InnerHeight,    10,
  200.                       WA_DepthGadget,    1,
  201.                       WA_SizeGadget,     1,
  202.                       WA_MaxWidth,       16000,
  203.                       WA_MaxHeight,       16000,
  204.                       WA_CloseGadget,    1,
  205.                       TAG_DONE))
  206.     {
  207.       return;      
  208.     }
  209.     else
  210.     {
  211.       UnlockPubScreen(0,MyScreen);
  212.       MyScreen=0;
  213.     }
  214.   }
  215. }
  216.  
  217. void CloseUI(void)
  218. {
  219.   if(MyScreen)
  220.   {
  221.     if(Window)
  222.     {
  223.       CloseWindow(Window);
  224.       Window=0;
  225.     }
  226.     UnlockPubScreen(0,MyScreen);
  227.     MyScreen=0;
  228.   }
  229. }
  230. /*
  231. void __asm __saveds CxFunction(register __a0 CxMsg *cxm, register __a1 CxObj *co)
  232. {
  233.   struct InputEvent *ie;
  234.   static LONG ox=0,oy=0;
  235.   LONG x,y;
  236.   float fx,fy;
  237.   static float px,py; 
  238.       
  239.   ie=(struct InputEvent*)CxMsgData(cxm);
  240.   
  241.   if(ie->ie_Class==255)
  242.   {
  243.     ie->ie_Class=IECLASS_RAWMOUSE;
  244.   }
  245.   else
  246.   {
  247.     if(ie->ie_Class==IECLASS_RAWMOUSE)
  248.     {
  249.       x=ie->ie_X;
  250.       y=ie->ie_Y;
  251.           
  252.       fx=(x+ox)/2+px;
  253.       fy=(y+oy)/2+py;
  254.  
  255.       ox=x;
  256.       oy=y;
  257.  
  258.       x=fx;
  259.       y=fy;
  260.       
  261.       px=fx-x;
  262.       py=fy-y;
  263.  
  264.       ie->ie_X=x;
  265.       ie->ie_Y=y;
  266.  
  267.       if((x==0 && y==0) && (ie->ie_Qualifier & 0x7fff)==0)
  268.       {  
  269.         if(Window && ox!=x && oy!=y)
  270.         {
  271.           Signal(Me, TSignal);
  272.         }
  273.             
  274.         DisposeCxMsg(cxm);
  275.       }
  276.     }
  277.   }
  278. }
  279.  
  280. */
  281.  
  282. void __asm __saveds CxFunction(register __a0 CxMsg *cxm, register __a1 CxObj *co)
  283. {
  284.   struct InputEvent *ie;
  285.   static LONG ox=0,oy=0,oqual=0;
  286.   LONG x,y,xx,yy,nqual;
  287.       
  288.   ie=(struct InputEvent*)CxMsgData(cxm);
  289.   
  290.   if(ie->ie_Class==255)
  291.   {
  292.     ie->ie_Class=IECLASS_RAWMOUSE;
  293.   }
  294.   else
  295.   {
  296.     if(ie->ie_Class==IECLASS_RAWMOUSE)
  297.     {
  298.       nqual=ie->ie_Qualifier & 0x7fff; // get rid of RawMouse bit
  299.       xx=x=ie->ie_X;
  300.       yy=y=ie->ie_Y;
  301.  
  302.       if(x==-ox) x=0;
  303.       if(y==-oy) y=0;
  304.  
  305. //if(Window)
  306. {
  307. //      kprintf("ie.x=%ld  ie.Y=%ld  ox=%ld  oy=%ld  new ie.x=%ld  ie.y=%ld  Qual=%lx\n", xx, yy, ox, oy, x, y, ie->ie_Qualifier); 
  308. }     
  309.       // if new values aren't = 0 then set to new value
  310.       if(xx!=0)  ox=xx;
  311.       if(yy!=0)  oy=yy;
  312.       
  313.       ie->ie_X=x;
  314.       ie->ie_Y=y;
  315.       
  316.       if((x==0 && y==0) && nqual==oqual)
  317.       {
  318.         if(Window)
  319.         {
  320. //          kprintf("  ie deleted\n");
  321.           
  322.           Signal(Me, TSignal);
  323.         }
  324.         DisposeCxMsg(cxm);
  325.       }
  326.       oqual=nqual;
  327.     }
  328.   }
  329. }
  330.  
  331.  
  332.  
  333.