Advertisement
Guest User

mouse.c

a guest
Sep 18th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. /* Mouse()                                                               */
  2. /* Mouse() is a handy, easy and fast but naughty function that hits the  */
  3. /* hardware of the Amiga. It looks at either port 1 or port 2, and       */
  4. /* returns the (x and y) delta movement of the mouse, as well as a       */
  5. /* bitfield containing the present state of the three buttons. (A normal */
  6. /* Amiga mouse has only two buttons (left and right), but it is possible */
  7. /* to connect a mouse with three buttons, so why shouldn't we support    */
  8. /* it?)                                                                  */
  9. /*                                                                       */
  10. /* Synopsis: buttons = Mouse( port, dx, dy );                            */
  11. /* buttons:  (UBYTE) If the left mouse button was pressed, bit one is    */
  12. /*           set. If the middle mouse button was pressed the second      */
  13. /*           bit is set, and if the right mouse button was pressed the   */
  14. /*           third bit is set.                                           */
  15. /* port:     (UBYTE) Set the flag PORT1 if you want to check the first   */
  16. /*           (mouse) port, or set the flag PORT2 if you want to check    */
  17. /*           the second (joystick) port.                                 */
  18. /* dx:       (BYTE *) Pointer to a variable that will be initialized     */
  19. /*           with the horizontal delta movement of the mouse.            */
  20. /* dy:       (BYTE *) Pointer to a variable that will be initialized     */
  21. /*           with the vertical delta movement of the mouse.              */
  22.  
  23.  
  24. #include <exec/types.h>
  25. #include <hardware/custom.h>
  26. #include <hardware/cia.h>
  27.  
  28.  
  29. #define CIAAPRA 0xBFE001
  30.  
  31. #define LEFT_BUTTON   1
  32. #define MIDDLE_BUTTON 2
  33. #define RIGHT_BUTTON  4
  34.  
  35. #define PORT1 1
  36. #define PORT2 2
  37.  
  38.  
  39. extern struct Custom far custom;
  40. struct CIA *cia = (struct CIA *) CIAAPRA;
  41.  
  42.  
  43. void main();
  44. UBYTE Mouse();
  45.  
  46.  
  47. void main()
  48. {
  49.   int timer = 0;
  50.   UBYTE value = 0, old_value = 0;
  51.   BYTE dx=0, dy=0;
  52.  
  53.   while( timer < 30 )
  54.   {
  55.     old_value = value;
  56.  
  57.     value = Mouse( PORT1, &dx, &dy );
  58.  
  59.     printf( "(%4d :%4d)  ", dx, dy );
  60.    
  61.     if( value != old_value )
  62.     {
  63.       timer++;
  64.  
  65.       if( value & LEFT_BUTTON )
  66.         printf("LEFT ");
  67.       if( value & MIDDLE_BUTTON )
  68.         printf("MIDDLE ");
  69.       if( value & RIGHT_BUTTON )
  70.         printf("RIGHT ");
  71.     }
  72.     printf("\n");
  73.   }
  74. }
  75.  
  76.  
  77.  
  78. UBYTE Mouse( port, delta_x, delta_y )
  79. UBYTE port;
  80. BYTE *delta_x, *delta_y;
  81. {
  82.   UBYTE data = 0;
  83.   UWORD joy, pot;
  84.   static BYTE x=0, y=0, old_x=0, old_y=0;
  85.  
  86.  
  87.   custom.potgo = 0xFF00;
  88.   pot = custom.potinp;
  89.  
  90.   if( port == PORT1 )
  91.   {
  92.     /* PORT 1 ("MOUSE PORT") */
  93.     joy = custom.joy0dat;
  94.     data += !( cia->ciapra & 0x0040 ) ? LEFT_BUTTON : 0;
  95.     data += !( pot & 0x0100 ) ? MIDDLE_BUTTON : 0;
  96.     data += !( pot & 0x0400 ) ? RIGHT_BUTTON : 0;
  97.   }
  98.   else
  99.   {
  100.     /* PORT 2 ("JOYSTICK PORT") */
  101.     joy = custom.joy1dat;
  102.     data += !( cia->ciapra & 0x0080 ) ? LEFT_BUTTON : 0;
  103.     data += !( pot & 0x1000 ) ? MIDDLE_BUTTON : 0;
  104.     data += !( pot & 0x4000 ) ? RIGHT_BUTTON : 0;
  105.   }
  106.  
  107.   old_x = x;
  108.   x = joy & 0x00FF;
  109.   *delta_x = x - old_x;
  110.  
  111.   old_y = y;
  112.   y = joy >> 8;
  113.   *delta_y = y - old_y;
  114.  
  115.   return( data );
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement