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

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

unix.superglobalmegacorp.com

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