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

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

unix.superglobalmegacorp.com

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