home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.1 / Takeover / joymouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  5.0 KB  |  219 lines

  1. /*
  2.   Copyright (c) 1988 Commodore-Amiga, Inc.
  3.  
  4.   Executables based on this information may be used in software
  5.   for Commodore Amiga computers.  All other rights reserved.
  6.  
  7.   This information is provided "as is"; no warranties are made.
  8.   All use is at your own risk, and no liability or responsibility is assumed.
  9. */
  10.  
  11. #define    DEBUG
  12. #include    "exec/types.h"
  13. #include    "exec/nodes.h"
  14. #include    "exec/lists.h"
  15. #include    "exec/ports.h"
  16. #include    "exec/interrupts.h"
  17. #include    "devices/gameport.h"
  18. #include    "devices/input.h"
  19. #include    "devices/inputevent.h"
  20. #include    "libraries/dos.h"
  21.  
  22. struct Task *FindTask();
  23.  
  24. struct MsgPort iorp = {
  25.     {0, 0, NT_MSGPORT, 0, 0}, 0, SIGF_SINGLE, 0, {0, 0, 0, 0, 0}
  26. };
  27.  
  28. struct IOStdReq ior = {
  29.     {{0, 0, NT_MESSAGE, 0, 0}, &iorp, 0},
  30.     0, 0, 0, 0, 0, 0, 0, 0, 0
  31. };
  32.  
  33. struct InputEvent nullEvent = {
  34.     NULL, IECLASS_NULL
  35. };
  36.  
  37. struct GamePortTrigger joyTrigger = {
  38.     GPTF_DOWNKEYS|GPTF_UPKEYS, 1, 1, 1
  39. };
  40.  
  41. struct GamePortTrigger mouseTrigger = {
  42.     GPTF_DOWNKEYS|GPTF_UPKEYS, 0, 1, 1
  43. };
  44.  
  45. extern VOID eventAFunction();
  46.  
  47. struct Interrupt inputHandler = {
  48.     {0, 0, NT_INTERRUPT, 100, 0}, 0, eventAFunction
  49. };
  50.  
  51. #define    MAXDELTA    255
  52. #define    DELTASHIFT    4
  53.  
  54. int deltaX = 0;
  55. int deltaY = 0;
  56.  
  57. struct InputEvent *
  58. eventFunction(event, data)
  59. struct InputEvent *event;
  60. {
  61.     struct InputEvent *loopevent;
  62.  
  63. #ifdef    DEBUG
  64. kprintf(".");
  65. #endif
  66.  
  67.     loopevent = event;
  68.     do {
  69.     if (loopevent->ie_Class == IECLASS_RAWMOUSE) {
  70.         /* handle X, then Y.  slow down faster than speed up */
  71.         /* this could use another iteration to get it to "feel" right */
  72.         if (loopevent->ie_X < 0) {
  73. #ifdef    DEBUG
  74. kprintf("<");
  75. #endif
  76.         if (deltaX <= 0) {
  77.             if (deltaX > -MAXDELTA) deltaX--;
  78.         }
  79.         else deltaX = deltaX/2;
  80.         }
  81.         else if (loopevent->ie_X > 0) {
  82. #ifdef    DEBUG
  83. kprintf(">");
  84. #endif
  85.         if (deltaX >= 0) {
  86.             if (deltaX < MAXDELTA) deltaX++;
  87.         }
  88.         else deltaX = deltaX/2;
  89.         }
  90.         else if (loopevent->ie_Y != 0) {
  91.         if (deltaX != 0) deltaX += (deltaX<0)?1:-1;
  92.         }
  93.         else deltaX = deltaX/2;
  94.         if (deltaX < 0)
  95.         loopevent->ie_X = (deltaX-((1<<DELTASHIFT)+1))>>DELTASHIFT;
  96.         else
  97.         loopevent->ie_X = (deltaX+((1<<DELTASHIFT)-1))>>DELTASHIFT;
  98.  
  99.         if (loopevent->ie_Y < 0) {
  100. #ifdef    DEBUG
  101. kprintf("v");
  102. #endif
  103.         if (deltaY <= 0) {
  104.             if (deltaY > -MAXDELTA) deltaY--;
  105.         }
  106.         else deltaY = deltaY/2;
  107.         }
  108.         else if (loopevent->ie_Y > 0) {
  109. #ifdef    DEBUG
  110. kprintf("^");
  111. #endif
  112.         if (deltaY >= 0) {
  113.             if (deltaY < MAXDELTA) deltaY++;
  114.         }
  115.         else deltaY = deltaY/2;
  116.         }
  117.         else if (loopevent->ie_X != 0) {
  118.         if (deltaY != 0) deltaY += (deltaY<0)?1:-1;
  119.         }
  120.         else deltaY = deltaY/2;
  121.         if (deltaY < 0)
  122.         loopevent->ie_Y = (deltaY-((1<<DELTASHIFT)+1))>>DELTASHIFT;
  123.         else
  124.         loopevent->ie_Y = (deltaY+((1<<DELTASHIFT)-1))>>DELTASHIFT;
  125.     }
  126.     }
  127.     while ((loopevent = loopevent->ie_NextEvent) != 0);
  128.     return(event);
  129. }
  130.  
  131.  
  132. main(argc, argv)
  133. int argc;
  134. char *argv[];
  135. {
  136.     char c;
  137.     int result;
  138.  
  139.     NewList(&iorp.mp_MsgList);
  140.     iorp.mp_SigTask = (struct Task *) FindTask((char *) NULL);
  141.  
  142.     if (OpenDevice("input.device", 0, &ior, 0)) exit(20);
  143.  
  144.     /* free this controller */
  145.     c = GPCT_NOCONTROLLER;
  146.     ior.io_Command = IND_SETMTYPE;
  147.     ior.io_Length = 1;
  148.     ior.io_Data = (APTR) &c;
  149.     DoIO(&ior);
  150.  
  151.     /* wait for the SETM to take effect with a side effect */
  152.     ior.io_Command = IND_WRITEEVENT;
  153.     ior.io_Length = (LONG) sizeof(nullEvent);
  154.     ior.io_Data = (APTR) &nullEvent;
  155.     DoIO(&ior);
  156.  
  157.     /* switch to the right game port */
  158.     c = 1;
  159.     ior.io_Command = IND_SETMPORT;
  160.     ior.io_Length = 1;
  161.     ior.io_Data = (APTR) &c;
  162.     DoIO(&ior);
  163.  
  164.     /* set this controller to a joystick */
  165.     c = GPCT_ABSJOYSTICK;
  166.     ior.io_Command = IND_SETMTYPE;
  167.     ior.io_Length = 1;
  168.     ior.io_Data = (APTR) &c;
  169.     DoIO(&ior);
  170.  
  171.     /* set the trigger for the joystick */
  172.     ior.io_Command = IND_SETMTRIG;
  173.     ior.io_Length = (LONG) sizeof(joyTrigger);
  174.     ior.io_Data = (APTR) &joyTrigger;
  175.     DoIO(&ior);
  176.  
  177.     /* insert the accellerator handler into the food chain */
  178.     ior.io_Command = IND_ADDHANDLER;
  179.     ior.io_Data = (APTR) &inputHandler;
  180.     ior.io_Length = (LONG) sizeof(struct Interrupt);
  181.     DoIO(&ior);
  182.  
  183.     /* wait for this program to terminate */
  184.     Wait(SIGBREAKF_CTRL_C);
  185.  
  186.     /* free this controller */
  187.     c = GPCT_NOCONTROLLER;
  188.     ior.io_Command = IND_SETMTYPE;
  189.     ior.io_Length = 1;
  190.     ior.io_Data = (APTR) &c;
  191.     DoIO(&ior);
  192.  
  193.     /* remove the accellerator handler from the food chain */
  194.     ior.io_Command = IND_REMHANDLER;
  195.     ior.io_Data = (APTR) &inputHandler;
  196.     ior.io_Length = (LONG) sizeof(struct Interrupt);
  197.     DoIO(&ior);
  198.  
  199.     /* switch to the left game port */
  200.     c = 0;
  201.     ior.io_Command = IND_SETMPORT;
  202.     ior.io_Length = 1;
  203.     ior.io_Data = (APTR) &c;
  204.     result = DoIO(&ior);
  205.  
  206.     /* set the trigger for a mouse */
  207.     ior.io_Command = IND_SETMTRIG;
  208.     ior.io_Length = sizeof(mouseTrigger);
  209.     ior.io_Data = (APTR) &mouseTrigger;
  210.     DoIO(&ior);
  211.  
  212.     /* set this controller to a mouse */
  213.     c = GPCT_MOUSE;
  214.     ior.io_Command = IND_SETMTYPE;
  215.     ior.io_Length = 1;
  216.     ior.io_Data = (APTR) &c;
  217.     result = DoIO(&ior);
  218. }
  219.