home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / cmanual / devices / gameportdevice / example4.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  17KB  |  497 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Gameport Device             Tulevagen 22       */
  8. /* File:    Example4.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-27                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This program demonstrates how you can use the */
  23. /* Gameport Device to read mouse events. The     */
  24. /* program will report any mouse movements or if */
  25. /* any buttons was pressed/released. If the user */
  26. /* has not used the mouse for one minnute, the   */
  27. /* program will terminate.                       */
  28. /*                                               */
  29. /* This example is "nice" to the system and does */
  30. /* everything according to the "rules".          */
  31. /*                                               */
  32. /* This example does not wait for something to   */
  33. /* happen, but is instead constantly trying to   */
  34. /* collect messages and can therefore continue   */
  35. /* do do something while the user is not using   */
  36. /* the mouse.                                    */
  37. /*                                               */
  38. /* NOTE! Since Intuition is already using port 1 */
  39. /* (the "mouse port") we have to use port 2. So  */
  40. /* remember to connect the mouse to port 2,      */
  41. /* before you run the program.                   */
  42.  
  43.  
  44.  
  45. #include <exec/types.h>         /* UWORD, STRPTR */
  46. #include <devices/gameport.h>   /* GPCT_MOUSE */
  47. #include <devices/inputevent.h> /* struct InputEvent */
  48.  
  49.  
  50.  
  51. #define RIGHTGAMEPORT 1 /* The "Joystick" port. */
  52. #define LEFTGAMEPORT  0 /* The "Mouse" port.    */
  53. #define MINX          5 /* The mouse must be moved at least 5 stepps */
  54. #define MINY          5 /* before a mouse event should occure.       */
  55.  
  56.  
  57.  
  58. /* Pointer to the Graphics library: */
  59. struct GfxBase *GfxBase;
  60.  
  61.  
  62.  
  63. struct IOStdReq *game_io_msg;    /* Pointer to our IOStdReq str.    */
  64. struct MsgPort   *game_msg_port; /* Pointer to our message port.    */
  65. BOOL deviceerror;                /* Have we opened the device, OK?  */
  66. int counter;
  67.  
  68.  
  69.  
  70. /* Declare some external functions:        */
  71. /* (We must otherwise do so much casting.) */
  72. extern struct MsgPort *CreatePort();
  73. extern struct IOStdReq *CreateStdIO();    
  74.  
  75.  
  76.  
  77. /* Declare all functions in this module: */
  78. void main();
  79. void PrintMouseData();       /* Print some information about the mouse.   */
  80. BYTE SetControllerType();    /* Set type of controller.                   */
  81. BYTE SetControllerTrigger(); /* Set trigger.                              */
  82. void SetControllerRead();    /* Prepare it so we can read.                */
  83. BYTE GetControllerType();    /* Get type of controller already coneccted. */
  84. void clean_up();             /* Cleanup, and quit.                        */
  85.  
  86.  
  87.  
  88. void main()
  89. {
  90.   /* Put all data in this structure: */
  91.   struct InputEvent gamedata;
  92.  
  93.   BYTE type;
  94.  
  95.  
  96.   /* Open the Graphics library: */
  97.   GfxBase = (struct GfxBase *)
  98.     OpenLibrary( "graphics.library", 0 );
  99.   if( !GfxBase )
  100.     clean_up( "ERROR! Could not open the Graphics library!" );
  101.  
  102.  
  103.   /* 1. Create a message port so the system can communicate with us: */
  104.   game_msg_port = CreatePort( 0, 0 );
  105.   if( !game_msg_port )
  106.     clean_up( "ERROR! Could not create message port!" );
  107.  
  108.  
  109.   /* 2. Allocate and initialize a new I/O request block.  */
  110.   /* It should use our new message port as reply port:    */
  111.   game_io_msg = CreateStdIO( game_msg_port );     
  112.   if( !game_io_msg )
  113.     clean_up( "ERROR! Could not allocate new I/O request block!" );
  114.  
  115.  
  116.   /* 3. Open the Game Port Device, use the right port: */
  117.   /*    (Intuition is already using the left port!)    */
  118.   deviceerror = OpenDevice( "gameport.device", RIGHTGAMEPORT, game_io_msg, 0xFFFF );
  119.   if( deviceerror )
  120.     clean_up( "ERROR! Could not open the Game Port Device!" );
  121.  
  122.  
  123.   /* 4. Check if some other task is already using the gameport: */
  124.   if( type = GetControllerType() )
  125.   {
  126.     switch( type )
  127.     {
  128.       case GPCT_MOUSE:
  129.         printf( "A mouse is connected to the port!\n" );
  130.         break;
  131.  
  132.       case GPCT_RELJOYSTICK:
  133.         printf( "A proportional joystick is connected to the port!\n" );
  134.         break;
  135.  
  136.       case GPCT_ABSJOYSTICK:
  137.         printf( "A normal joystick is connected to the port!\n" );
  138.         break;
  139.     }
  140.     
  141.     /* Do not close the device! If we do it, the other task will */
  142.     /* then not be able to use the gameport device either!       */
  143.     deviceerror = TRUE;
  144.     clean_up( "ERROR! Some other task is already using the Gameport!" );
  145.   }
  146.  
  147.  
  148.   /* 5. Set device type (mouse): */
  149.   if( SetControllerType( GPCT_MOUSE ) )
  150.     clean_up( "ERROR! Could not set device type!" );
  151.  
  152.  
  153.   /* 6. Set device trigger: */
  154.   if( SetControllerTrigger
  155.       (                            /* Report following events:    */
  156.         GPTF_DOWNKEYS|GPTF_UPKEYS, /* buttonpressed/released,     */ 
  157.         600,                       /* every 10th second,          */
  158.         MINX,                      /* X movements more than MINX, */
  159.         MINY                       /* Y movements more than MINY. */
  160.       )
  161.     )
  162.     clean_up( "ERROR! Could not set device trigger!" );
  163.  
  164.  
  165.   /* 7. Prepare the device to read: */
  166.   SetControllerRead( &gamedata );
  167.  
  168.  
  169.   /* 8. Do our request, and return without delay: */
  170.   SendIO( game_io_msg );
  171.  
  172.  
  173.   printf( "Gameport Device - Mouse Example - Waiting\n" );
  174.   /* Stay in the while loop until the user has not moved the */
  175.   /* mouse during the last minute: (6 times 10 seconds)      */
  176.   while( counter < 6 )
  177.   {
  178.     printf("Working...\n");
  179.  
  180.  
  181.     /* Try to collect a message: */
  182.     if( GetMsg( game_msg_port ) )
  183.     {
  184.       /* Print some information abut the mouse: */
  185.       PrintMouseData( &gamedata );
  186.  
  187.       /* We have now collected a mouse event, so prepare the */
  188.       /* gameport device again:                              */
  189.       SendIO( game_io_msg );
  190.     }
  191.   }
  192.  
  193.  
  194.   /* 9. NOTE! In this example we will have done one more SendIO()  */
  195.   /*    than GetMsg(), so before we quit we must try to cancel our */
  196.   /*    last command:                                              */
  197.   AbortIO( game_io_msg );
  198.  
  199.   /*    We must also make sure that there are no messages waiting at */
  200.   /*    our message port, so we try to collect as many messages as   */
  201.   /*    possible, until we can not collect any more:                 */
  202.   while( GetMsg( game_msg_port ) )
  203.     printf( "Collecting remaining messages...\n" );
  204.  
  205.  
  206.  
  207.   /* 10. Clean up nicely, and quit: */
  208.   clean_up( "The End!" );
  209. }    
  210.  
  211.  
  212.  
  213. /****************************************************************/
  214. /*                                                              */
  215. /* PrintMouseData() prints how many x/y counts the mouse has    */
  216. /* been moved, and if any buttons has been pressed or released. */
  217. /*                                                              */
  218. /* Synopsis: PrintMouseData( data );                            */
  219. /*                                                              */
  220. /* data:     (struct InputEvent *) Pointer to an InputEvent     */
  221. /*           structure which has previously been initialized.   */
  222. /*                                                              */
  223. /****************************************************************/
  224.  
  225. void PrintMouseData( data )
  226. struct InputEvent *data;
  227. {
  228.   WORD x;
  229.   WORD y;
  230.   UWORD code;
  231.  
  232.  
  233.   /* Collect data: */
  234.   x = data->ie_X;
  235.   y = data->ie_Y;
  236.   code = data->ie_Code;
  237.  
  238.  
  239.   /* Print: */
  240.   switc