home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma24.dms / ma24.adf / WOLF3D / JoyStick.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  8KB  |  222 lines

  1. #include <exec/io.h>
  2. #include <devices/gameport.h>
  3. #include <devices/inputevent.h>
  4. #include <clib/exec_protos.h>
  5. #include <clib/alib_protos.h>
  6.  
  7. #include "JoyStick.h"
  8.  
  9. /* This file contains routines for allocating, reading, and deallocating
  10.    a joystick port under Intuition.
  11.  */
  12.  
  13. /* Note from the author: These routines are placed in the public domain. */
  14.  
  15. /*-------------------------------------------------------*
  16.  * NAME: AllocateJoystick                                *
  17.  *                                                       *
  18.  * AUTHOR: Terence Russell (August 9,1993)               *
  19.  *                                                       *
  20.  * DESC: Attempts to allocate a gameport device for use  *
  21.  *       as a joystick port. Will return 1 if successful *
  22.  *       and 0 if it failed. If a port is successfully   *
  23.  *       allocated, then you MUST call DeallocateJoystick*
  24.  *       once your program is finished. Do NOT call the  *
  25.  *       deallocation routine if the port was not        *
  26.  *       allocated.                                      *
  27.  *-------------------------------------------------------*/
  28.  
  29. short AllocateJoystick(void);
  30.  
  31. /*-------------------------------------------------------*
  32.  * NAME: ReadJoystick                                    *
  33.  *                                                       *
  34.  * AUTHOR: Terence Russell (August 9,1993)               *
  35.  *                                                       *
  36.  * DESC: This routine essentially reads the gameport     *
  37.  *       which was allocated with the AllocateJoystick() *
  38.  *       routine.                                        *
  39.  *       It returns a 16 bit word which indicates the    *
  40.  *       state of the joystick. Currently only the lower *
  41.  *       6 bits are used.                                *
  42.  *                                                       *
  43.  *       Joystick positions are given in -1,0 & 1 X,Y    *
  44.  *       coordinates.                                    *
  45.  *                                                       *
  46.  *       The first 2 bits represent the X values:        *
  47.  *           left = X = -1 -> gives a bit value of 01    *
  48.  *         center = X =  0 -> gives a bit value of 00    *
  49.  *          right = X =  1 -> gives a bit value of 10    *
  50.  *                                                       *
  51.  *       The next 2 bits represent the Y values:         *
  52.  *             up = Y = -1 -> gives a bit value of 01    *
  53.  *         center = Y =  0 -> gives a bit value of 00    *
  54.  *           down = Y =  1 -> gives a bit value of 10    *
  55.  *                                                       *
  56.  *       The 5th bit is the up/down state of the FIRE    *
  57.  *       button. 0 = down, 1 = up.                       *
  58.  *                                                       *
  59.  *       The 6th bit is the up/down state of a second    *
  60.  *       FIRE button. (For those of you with real two    *
  61.  *       button joysticks.                               *
  62.  *-------------------------------------------------------*/ 
  63.  
  64. short ReadJoystick(void);
  65.  
  66.  
  67. /*-------------------------------------------------------*
  68.  * NAME: DeallocateJoystick                              *
  69.  *                                                       *
  70.  * AUTHOR: Terence Russell (August 9,1993)               *
  71.  *                                                       *
  72.  * DESC: This routine deallocates a joystick gameport.   *
  73.  *       Do NOT call this routine if a gameport was not  *
  74.  *       previously allocated!                           *
  75.  *-------------------------------------------------------*/
  76.  
  77. void  DeallocateJoystick(void);
  78.  
  79.  
  80. struct MsgPort    *JoyStikMP;
  81. struct IOStdReq   *JoyStikIO;
  82.  
  83. struct InputEvent JoyStikEV;
  84. struct GamePortTrigger JoyStikTR;
  85.  
  86. short AllocateJoystick(void)
  87. {
  88.     char controllerType = 0;
  89.     
  90.     if( JoyStikMP = CreatePort(PORTNAME,0) )
  91.     {
  92.         if(JoyStikIO = (struct IOStdReq *)CreateExtIO(JoyStikMP,sizeof(*JoyStikIO)) )
  93.         {     
  94.             if(! OpenDevice("gameport.device",UNIT,(struct IORequest *)JoyStikIO,0) )
  95.             {
  96.                 /* Now check controller type and ask if we can use it. */
  97.  
  98.                 Forbid();
  99.                 
  100.                 JoyStikIO->io_Command = GPD_ASKCTYPE;
  101.                 JoyStikIO->io_Length  = 1;
  102.                 JoyStikIO->io_Flags   = IOF_QUICK;
  103.                 JoyStikIO->io_Data    = (APTR)&controllerType;
  104.                 DoIO((struct IORequest *)JoyStikIO);
  105.                 
  106.                 if(controllerType == GPCT_NOCONTROLLER)
  107.                 {
  108.                     controllerType = GPCT_ABSJOYSTICK;
  109.                     
  110.                     JoyStikIO->io_Command = GPD_SETCTYPE;
  111.                     JoyStikIO->io_Length  = 1;
  112.                     JoyStikIO->io_Data    = (APTR)&controllerType;
  113.                     DoIO((struct IORequest *)JoyStikIO);
  114.                     
  115.                     Permit();
  116.                     
  117.                     JoyStikIO->io_Command = CMD_CLEAR; // flush the buffer
  118.                     JoyStikIO->io_Length  = 0;
  119.                     JoyStikIO->io_Flags   = IOF_QUICK;
  120.                     JoyStikIO->io_Data    = NULL;
  121.                     DoIO((struct IORequest *)JoyStikIO);
  122.  
  123.                     JoyStikTR.gpt_Keys = GPTF_DOWNKEYS | GPTF_UPKEYS;
  124.                     JoyStikTR.gpt_XDelta = 1;
  125.                     JoyStikTR.gpt_YDelta = 1;
  126.                     JoyStikIO->io_Command = GPD_SETTRIGGER;
  127.                     JoyStikIO->io_Length  = sizeof(struct GamePortTrigger);
  128.                     JoyStikIO->io_Data    = (APTR)&JoyStikTR;
  129.                     DoIO((struct IORequest *)JoyStikIO);
  130.                     
  131.                     return (TRUE);
  132.                 };
  133.                 
  134.                 Permit();
  135.                 CloseDevice((struct IORequest *)JoyStikIO);// There should be no other IO requests pending.
  136.             };
  137.             
  138.             DeleteExtIO((struct IORequest *)JoyStikIO);
  139.         };
  140.  
  141.         DeletePort(JoyStikMP);
  142.     };
  143.     
  144.     return (FALSE);
  145. }
  146.  
  147. short ReadJoystick(void)
  148. {
  149.     static short RequestSwitch = FALSE, result = 0;
  150.     register short temp;
  151.     
  152.     if(! RequestSwitch) // No request has yet been sent.
  153.     {
  154.         JoyStikIO->io_Command = GPD_READEVENT;
  155.         JoyStikIO->io_Length  = sizeof( struct InputEvent);
  156.         JoyStikIO->io_Data    = (APTR)&JoyStikEV;
  157.         JoyStikIO->io_Flags   = 0;
  158.         SendIO((struct IORequest *)JoyStikIO);// Asynchronous
  159.         RequestSwitch = TRUE;
  160.     };
  161.     
  162.     if( GetMsg(JoyStikMP) )
  163.     {
  164.         RequestSwitch = FALSE;
  165.             
  166.         switch(JoyStikEV.ie_Code)
  167.         {
  168.             case IECODE_LBUTTON:
  169.                 result |= LBUTTON;
  170.                 break;
  171.                     
  172.             case (IECODE_LBUTTON | IECODE_UP_PREFIX):
  173.                 result &= (!LBUTTON);
  174.                 break;
  175.                 
  176.             case IECODE_RBUTTON:
  177.                 result |= RBUTTON;
  178.                 break;
  179.                     
  180.             case (IECODE_RBUTTON | IECODE_UP_PREFIX):
  181.                 result &= (!RBUTTON);
  182.                 break;
  183.         };
  184.  
  185.         result &= (LBUTTON+RBUTTON);
  186.         
  187.         temp = JoyStikEV.ie_X;
  188.         if(temp == -1) result |= XJOYLFT;
  189.         else if(temp) result |= XJOYRGT;
  190.         
  191.  
  192.         temp = JoyStikEV.ie_Y;
  193.         if(temp == -1) result |= YJOYUP;
  194.         else if(temp) result |= YJOYDWN;
  195.     };
  196.     
  197.     return(result);
  198. }
  199.  
  200. void DeallocateJoystick(void)
  201. {
  202.     char controllerType = GPCT_NOCONTROLLER;
  203.  
  204.     /* First toast any remaining asynchronous messages. */
  205.     if(! CheckIO((struct IORequest *)JoyStikIO) )
  206.     {
  207.         AbortIO((struct IORequest *)JoyStikIO);
  208.         WaitIO((struct IORequest *)JoyStikIO);
  209.     };
  210.  
  211.     /* Tell the device we are no longer using it. */
  212.     JoyStikIO->io_Command = GPD_SETCTYPE;
  213.     JoyStikIO->io_Length  = 1;
  214.     JoyStikIO->io_Flags   = IOF_QUICK;
  215.     JoyStikIO->io_Data    = (APTR)&controllerType;
  216.     DoIO((struct IORequest *)JoyStikIO);
  217.  
  218.     CloseDevice((struct IORequest *)JoyStikIO);
  219.     DeleteExtIO((struct IORequest *)JoyStikIO);
  220.     DeletePort(JoyStikMP);
  221. }
  222.