home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 148.lha / MouseZoom / MouseZoom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-11-15  |  6.4 KB  |  250 lines

  1. #define TASKNAME    "Mouse Zoom"
  2. #define TERMINATE    0xff
  3.  
  4. #define BANNER "\x9B0;33mMouseZoom\x9B0m by John Meissen now installed \nTo remove \x9B0;33mMouseZoom\x9B0m, run it again.\n"
  5. #define KILLMSG "Removing \x9B0;33mMouseZoom\x9B0m\n"
  6.  
  7. #include <exec/types.h>
  8. #include <exec/lists.h>
  9. #include <exec/memory.h>
  10. #include <exec/interrupts.h>
  11. #include <exec/libraries.h>
  12. #include <exec/io.h>
  13. #include <exec/tasks.h>
  14. #include <exec/devices.h>
  15. #include <devices/input.h>
  16. #include <devices/inputevent.h>
  17. #include <intuition/intuition.h>
  18. #include <libraries/dos.h>
  19. #include <graphics/gfxmacros.h>
  20.  
  21. #include <proto/dos.h>
  22. #include <proto/exec.h>
  23. #include <proto/intuition.h>
  24. #include <proto/graphics.h>
  25. #include <string.h>
  26.  
  27. #define abs(x) ((x) < 0 ? -(x) : (x))
  28.  
  29. struct MsgPort *FindPort(), *CreatePort();
  30. void DeletePort();
  31.  
  32. struct OURMSG {
  33.  struct Message msgpart;
  34.  short  Command;
  35.  short  Misc;
  36.  };
  37.  
  38. /* Declarations for CBACK */
  39. extern BPTR _Backstdout;         /* standard output when run in background */
  40. long _BackGroundIO = 1;          /* Flag to tell it we want to do I/O      */
  41. long _stack = 4000;              /* Amount of stack space our task needs   */
  42. char *_procname = "Mouse Zoom";  /* The name of the task to create         */
  43. long _priority = 0;              /* The priority to run us at              */
  44.  
  45. /************************************************************************/
  46. /* the handler subroutine - called through the handler stub             */
  47. /************************************************************************/
  48. struct InputEvent *myhandler(ev, gptr)
  49. struct InputEvent *ev;            /* and a pointer to a list of events */
  50. APTR   gptr;                      /* Everything we need to know about */
  51. {
  52. register struct InputEvent *ep;
  53. register short i;
  54.  
  55.    /* run down the list of events to see if they are mouse-move */
  56.  
  57. for (ep = ev; ep != NULL; ep = ep->ie_NextEvent)
  58.     {
  59.     if ((ep->ie_Class == IECLASS_RAWMOUSE)    &&
  60.         (ep->ie_Qualifier & IEQUALIFIER_RELATIVEMOUSE))
  61.         {
  62.     i = abs(ep->ie_X) >> 3;        /* adjust x value */
  63.     if (i != 0) ep->ie_X *= i;
  64.     i = abs(ep->ie_Y) >> 3;        /* adjust y value */
  65.     if (i != 0) ep->ie_Y *= i;
  66.         }
  67.      }
  68.    /* pass on the pointer to the event */
  69. return(ev);
  70. }
  71.  
  72. /* * * * * * * * * * * EXTERNAL ROUTINES * * * * * * * * * */
  73.  
  74. extern struct MsgPort  *CreatePort();
  75. struct IOStdReq  *CreateIOReq(struct MsgPort *, int);
  76. void              DeleteIOReq(struct IOStdReq *);
  77. extern void       HandlerInterface();
  78.  
  79. /************************************************************************/
  80. /* the main program .                                                   */
  81. /************************************************************************/
  82. void _main(cmd)
  83. char *cmd;
  84. {
  85. struct OURMSG *msg;
  86. short Command;
  87. short Misc;
  88. struct MsgPort     *port;
  89. struct MsgPort     *inputDevPort;
  90. struct IOStdReq    *inputRequestBlock;
  91. struct Interrupt      handlerStuff;
  92.  
  93. port                = NULL;
  94. inputDevPort        = NULL;
  95. inputRequestBlock   = NULL;
  96.  
  97.     /* see if we already exist, and if so, wake up and die */
  98.  
  99. if ((port = FindPort(TASKNAME)) != NULL)
  100.     {
  101.     if ((msg = (struct OURMSG *)
  102.               AllocMem(sizeof(struct OURMSG), MEMF_CLEAR|MEMF_PUBLIC)) == NULL)
  103.     goto abort;
  104.     msg->Command = TERMINATE;
  105.     msg->msgpart.mn_Node.ln_Type = NT_MESSAGE;
  106.     msg->msgpart.mn_Node.ln_Pri  = 0;
  107.     msg->msgpart.mn_Length = sizeof(struct OURMSG);
  108.     PutMsg(port,msg);
  109.     port = NULL;
  110.         if (_Backstdout) 
  111.         {
  112.         Write(_Backstdout, KILLMSG, sizeof(KILLMSG));
  113.             Close(_Backstdout);
  114.         }
  115.     goto abort;
  116.     }        
  117.  
  118. if (_Backstdout)
  119.     {
  120.         Write(_Backstdout, BANNER, sizeof(BANNER));
  121.         Close(_Backstdout);
  122.     }    
  123.  
  124. _Backstdout = 0;
  125.    
  126. if ((port  = CreatePort(TASKNAME,0)) == NULL) goto abort;
  127.  
  128. if (  ((inputDevPort = CreatePort(0,0)) == NULL)                        ||
  129.  
  130.       ((inputRequestBlock =
  131.           CreateIOReq(inputDevPort, sizeof(struct IOStdReq))) == NULL)  ||
  132.  
  133.       OpenDevice("input.device",0,(struct IORequest *)inputRequestBlock,0))
  134.  
  135.       goto abort;
  136.  
  137. handlerStuff.is_Data = NULL;
  138. handlerStuff.is_Code = HandlerInterface;
  139. handlerStuff.is_Node.ln_Pri = 51;
  140.  
  141.  
  142. inputRequestBlock->io_Command = IND_ADDHANDLER;
  143. inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  144.  
  145. DoIO((struct IORequest *)inputRequestBlock);
  146.  
  147. for(;;)         /* FOREVER */
  148.       {
  149.       WaitPort( port );
  150.  
  151.       while ((msg = (struct OURMSG *)GetMsg(port)) != NULL)
  152.         {
  153.     Command = msg->Command;
  154.     Misc    = msg->Misc;
  155.     FreeMem(msg, sizeof(struct OURMSG));
  156.     switch(Command)
  157.         {
  158.         case TERMINATE:
  159.             goto abort;
  160.             break;
  161.         
  162.         default:
  163.             break;
  164.         }            
  165.     }
  166.       }
  167.  
  168. abort:
  169.  
  170. if (inputRequestBlock != NULL)
  171.       {
  172.       if (inputRequestBlock->io_Device != NULL)
  173.          {
  174.          inputRequestBlock->io_Command = IND_REMHANDLER;
  175.          inputRequestBlock->io_Data = (APTR)&handlerStuff;
  176.          DoIO((struct IORequest *)inputRequestBlock);
  177.      
  178.          CloseDevice((struct IORequest *)inputRequestBlock);
  179.          }
  180.       DeleteIOReq(inputRequestBlock);
  181.       }
  182. if (inputDevPort != NULL)       DeletePort(inputDevPort);
  183. if (port != NULL)               DeletePort(port);
  184. }
  185.  
  186. void MemCleanup(){}
  187.  
  188. struct MsgPort *CreatePort(name, pri)
  189. char *name;
  190. int pri;
  191. {
  192.    UBYTE sigbit;
  193.    register struct MsgPort *port;
  194.  
  195.    if ((sigbit = AllocSignal(-1)) == -1)
  196.       return((struct MsgPort *)0);
  197.  
  198.    if ((port = (struct MsgPort *)AllocMem(sizeof(struct MsgPort),
  199.                         MEMF_CLEAR|MEMF_PUBLIC)) == 0)
  200.       {
  201.       FreeSignal(sigbit);
  202.       return((struct MsgPort *) (0));
  203.       }
  204.    port->mp_Node.ln_Name = name;
  205.    port->mp_Node.ln_Pri = pri;
  206.    port->mp_Node.ln_Type = NT_MSGPORT;
  207.    port->mp_Flags = PA_SIGNAL;
  208.    port->mp_SigBit = sigbit;
  209.    port->mp_SigTask = (struct Task *)FindTask(0);
  210.    AddPort(port);
  211.    return(port);
  212. }
  213.  
  214. void DeletePort(port)
  215. struct MsgPort *port;
  216. {
  217. RemPort(port);
  218. FreeSignal(port->mp_SigBit);
  219. FreeMem((char *)port,sizeof(struct MsgPort));
  220. }
  221.  
  222. struct IOStdReq *
  223. CreateIOReq(port, size)
  224. struct MsgPort *port;
  225. int size;
  226. {
  227.    struct IOStdReq *ioReq;
  228.  
  229.    if ((ioReq = (struct IOStdReq *)
  230.                 AllocMem(size, MEMF_CLEAR | MEMF_PUBLIC)) != NULL)
  231.       {
  232.       ioReq->io_Message.mn_Node.ln_Type = NT_MESSAGE;
  233.       ioReq->io_Message.mn_Node.ln_Pri  = 0;
  234.       ioReq->io_Message.mn_Length       = size;
  235.       ioReq->io_Message.mn_ReplyPort    = port;
  236.       }
  237.    return(ioReq);
  238. }
  239.  
  240. void DeleteIOReq(ioReq)
  241. struct IOStdReq *ioReq;
  242. {
  243. ioReq->io_Message.mn_Node.ln_Type = 0xff;
  244. ioReq->io_Device = (struct Device *) -1;
  245. ioReq->io_Unit = (struct Unit *) -1;
  246.  
  247. FreeMem( (char *)ioReq, ioReq->io_Message.mn_Length);
  248. }
  249.  
  250.