Annotation of hatari/src/joy.c, revision 1.1.1.1

1.1       root        1: /*
                      2:   Hatari
                      3: 
                      4:   Joystick routines
                      5: 
                      6:   NOTE Also the ST uses the joystick port 1 as the default controller
                      7:          - so we allocate our joysticks  with index 1 and then 0 so these match.
                      8: */
                      9: 
                     10: #include <SDL.h>
                     11: 
                     12: #include "main.h"
                     13: #include "configuration.h"
                     14: #include "debug.h"
                     15: #include "dialog.h"
                     16: #include "errlog.h"
                     17: #include "joy.h"
                     18: #include "screen.h"
                     19: 
                     20: #define REVERSE_ID(id)  ((id)^1)             /* ST joystick 1(default) is first PC joystick found */
                     21: 
                     22: BOOL bJoystickWorking[2] = { FALSE,FALSE };  /* Is joystick plugged in and working? */
                     23: int WindowsJoystickID[2];                    /* Windows Joystick ID of above joysticks */
                     24: int JoystickSpaceBar = FALSE;                /* State of space-bar on joystick button 2 */
                     25: int cursorJoyEmu;                            /* set in view.c */
                     26: 
                     27: 
                     28: /*-----------------------------------------------------------------------*/
                     29: /*
                     30:   Initialise joysticks, try to use DirectInput or, if this fails, standard Windows calls
                     31: */
                     32: void Joy_Init(void)
                     33: {
                     34:   /* First, try for DirectInput(this sets bAllowDirectInput)*/
                     35: //  DJoy_Init();
                     36:   /* Also try standard Windows joystick functions */
                     37:   Joy_InitWindows();
                     38: 
                     39:   /* OK, do we have any working joysticks? */
                     40:   if (!bJoystickWorking[1]) {
                     41:     /* No, so if first time install need to set cursor emulation */
                     42: //FIXME    if (bFirstTimeInstall)
                     43: //FIXME      ConfigureParams.Joysticks.Joy[1].bCursorEmulation = TRUE;
                     44:   }
                     45:   /* Make sure we only have one in cursor emulation mode */
                     46:   Joy_PreventBothUsingCursorEmulation();
                     47: 
                     48:   JoystickSpaceBar = FALSE;
                     49: }
                     50: 
                     51: //-----------------------------------------------------------------------
                     52: /*
                     53:   Initialise connected joysticks using Windows library calls
                     54: */
                     55: void Joy_InitWindows(void)
                     56: {
                     57: //FIXME
                     58: /*
                     59:    JOYINFO JoyInfo;
                     60:   int i,nPadsConnected,JoyID=1;            // Store in ST joystick slot 1 then 0
                     61: 
                     62:   // Scan joystick connection array for first two working joysticks
                     63:   nPadsConnected = joyGetNumDevs();
                     64:   for(i=0; (i<nPadsConnected) && (JoyID>=0); i++) {
                     65:     // Is pad ok?
                     66:     if (joyGetPos(i,&JoyInfo)==JOYERR_NOERROR) {
                     67:       // Set as working(NOTE we assign ST joysticks 1 first), and store Windows ID
                     68:       ErrLog_File("Joystick: %d found\n",JoyID);
                     69:       bJoystickWorking[JoyID] = TRUE;
                     70:       WindowsJoystickID[JoyID] = i;
                     71:       JoyID--;
                     72:     }
                     73:   }
                     74: */
                     75: }
                     76: 
                     77: //-----------------------------------------------------------------------
                     78: /*
                     79:   Make sure only one joystick is assigned as cursor emulation mode
                     80: */
                     81: void Joy_PreventBothUsingCursorEmulation(void)
                     82: {
                     83:   /* Make sure we cannot have both joysticks assigned as cursor emulation */
                     84:   if (ConfigureParams.Joysticks.Joy[0].bCursorEmulation && ConfigureParams.Joysticks.Joy[0].bCursorEmulation) {
                     85:     ConfigureParams.Joysticks.Joy[0].bCursorEmulation = FALSE;
                     86:     ConfigureParams.Joysticks.Joy[1].bCursorEmulation = TRUE;
                     87:   }
                     88: }
                     89: 
                     90: //-----------------------------------------------------------------------
                     91: /*
                     92:   Read details from joystick using Windows calls
                     93:   NOTE ID is that of ST(ie 1 is default)
                     94: */
                     95: BOOL Joy_ReadJoystick(int JoystickID,JOYREADING *pJoyReading)
                     96: {
                     97: //FIXME
                     98: /*
                     99:   JOYINFO JoyInfo;
                    100: 
                    101:   // Joystick is OK, read position
                    102:   if (joyGetPos(WindowsJoystickID[JoystickID],&JoyInfo)==JOYERR_NOERROR) {
                    103:     // Copy details
                    104:     pJoyReading->XPos = JoyInfo.wXpos;
                    105:     pJoyReading->YPos = JoyInfo.wYpos;
                    106:     pJoyReading->Buttons = JoyInfo.wButtons;
                    107: 
                    108:     return(TRUE);
                    109:   }
                    110: */
                    111:   return(FALSE);
                    112: }
                    113: 
                    114: //-----------------------------------------------------------------------
                    115: /*
                    116:   Read PC joystick and return ST format byte, ie lower 4 bits direction and top bit fire
                    117:   NOTE : ID 0 is Joystick 0/Mouse and ID 1 is Joystick 1(default)
                    118: */
                    119: unsigned char Joy_GetStickData(unsigned int JoystickID)
                    120: {
                    121:   unsigned char Data = 0;
                    122: 
                    123: //FIXME   JOYREADING JoyReading;
                    124: 
                    125:   /* Are we emulating the joystick via the cursor key? */
                    126:   if (ConfigureParams.Joysticks.Joy[JoystickID].bCursorEmulation) {
                    127:     /* If holding 'SHIFT' we actually want cursor key movement, so ignore any of this */
                    128:     if ( !(SDL_GetModState()&(KMOD_LSHIFT|KMOD_RSHIFT)) )
                    129:                   {
                    130:                    Data = cursorJoyEmu;    /* cursorJoyEmu is set in view.c */
                    131:       }
                    132:   }
                    133: /*
                    134:   else if (bJoystickWorking[JoystickID]) {
                    135:     // Read joystick information from DirectInput or Windows calls into standard format
                    136:     if (!DJoy_ReadJoystick(JoystickID,&JoyReading)) {
                    137:       if (!Joy_ReadJoystick(JoystickID,&JoyReading)) {
                    138:         // Something is wrong, we cannot read the joystick
                    139:         bJoystickWorking[JoystickID] = FALSE;
                    140:       }
                    141:     }
                    142: 
                    143:     // So, did read joysyick OK?
                    144:     if (bJoystickWorking[JoystickID]) {
                    145:       // Directions
                    146:       if (JoyReading.YPos<=JOYRANGE_UP_VALUE)
                    147:         Data |= 0x01;
                    148:       if (JoyReading.YPos>=JOYRANGE_DOWN_VALUE)
                    149:         Data |= 0x02;
                    150:       if (JoyReading.XPos<=JOYRANGE_LEFT_VALUE)
                    151:         Data |= 0x04;
                    152:       if (JoyReading.XPos>=JOYRANGE_RIGHT_VALUE)
                    153:         Data |= 0x08;
                    154: 
                    155:       // Buttons - I've made fire button 2 to simulate the pressing of the space bar(for Xenon II etc...)
                    156: #ifdef USE_FIREBUTTON_2_AS_SPACE
                    157:       // PC Joystick button 1 is set as ST joystick button and PC button 2 is the space bar
                    158:       if (JoyReading.Buttons&JOY_BUTTON1)
                    159:         Data |= 0x80;
                    160:       if (JoyReading.Buttons&JOY_BUTTON2) {
                    161:         // Only press 'space bar' if not in NULL state
                    162:         if (!JoystickSpaceBar) {
                    163:           // Press, ikbd will send packets and de-press
                    164:           JoystickSpaceBar = JOYSTICK_SPACE_DOWN;
                    165:         }
                    166:       }
                    167: #else  //USE_FIREBUTTON_2_AS_SPACE
                    168:       // PC Joystick buttons 1+2 are set as ST joystick button
                    169:       if ( (JoyReading.Buttons&JOY_BUTTON1) || (JoyReading.Buttons&JOY_BUTTON2) )
                    170:         Data |= 0x80;
                    171: #endif  //USE_FIREBUTTON_2_AS_SPACE
                    172:     }
                    173:   }
                    174: 
                    175:   // Ignore fire button every 8 frames if enabled autofire(for both cursor emulation and joystick)
                    176:   if (ConfigureParams.Joysticks.Joy[JoystickID].bEnableAutoFire) {
                    177:     if ((VBLCounter&0x7)<4)
                    178:       Data &= 0x7f;  // Remove top bit!
                    179:   }
                    180: */
                    181:   return(Data);
                    182: }
                    183: 
                    184: //-----------------------------------------------------------------------
                    185: /*
                    186:   Toggle cursor emulation
                    187: */
                    188: void Joy_ToggleCursorEmulation(void)
                    189: {
                    190:   // Toggle joystick 1 cursor emulation
                    191:   ConfigureParams.Joysticks.Joy[1].bCursorEmulation ^= TRUE;
                    192:   // Prevent both having emulation
                    193:   Joy_PreventBothUsingCursorEmulation();
                    194: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.