diff --git a/src/drivers/Qt/GamePadConf.cpp b/src/drivers/Qt/GamePadConf.cpp index 24cfb88c..eafc6de9 100644 --- a/src/drivers/Qt/GamePadConf.cpp +++ b/src/drivers/Qt/GamePadConf.cpp @@ -2,6 +2,7 @@ // #include "Qt/GamePadConf.h" #include "Qt/main.h" +#include "Qt/dface.h" #include "Qt/input.h" #include "Qt/config.h" #include "Qt/keyscan.h" @@ -20,6 +21,8 @@ GamePadConfDialog_t::GamePadConfDialog_t(QWidget *parent) QPushButton *closebutton; QPushButton *clearButton[GAMEPAD_NUM_BUTTONS]; + InitJoysticks(); + portNum = 0; configNo = 0; buttonConfigStatus = 1; diff --git a/src/drivers/Qt/dface.h b/src/drivers/Qt/dface.h index 2f3890ec..12c515ee 100644 --- a/src/drivers/Qt/dface.h +++ b/src/drivers/Qt/dface.h @@ -18,6 +18,8 @@ void SilenceSound(int s); /* DOS and SDL */ int InitJoysticks(void); int KillJoysticks(void); +int AddJoystick( int which ); +int RemoveJoystick( int which ); uint32 *GetJSOr(void); int InitVideo(FCEUGI *gi); diff --git a/src/drivers/Qt/input.cpp b/src/drivers/Qt/input.cpp index 552f7ad8..640be3a8 100644 --- a/src/drivers/Qt/input.cpp +++ b/src/drivers/Qt/input.cpp @@ -956,6 +956,12 @@ UpdatePhysicalInput () g_keyState[ event.key.keysym.scancode ] = (event.type == SDL_KEYDOWN) ? 1 : 0; //checkKeyBoardState( event.key.keysym.scancode ); break; + case SDL_JOYDEVICEADDED: + AddJoystick( event.jdevice.which ); + break; + case SDL_JOYDEVICEREMOVED: + RemoveJoystick( event.jdevice.which ); + break; default: break; } @@ -964,8 +970,6 @@ UpdatePhysicalInput () } -static int bcpv=0, bcpj=0; - /** * Begin configuring the buttons by placing the video and joystick * subsystems into a well-known state. Button configuration really @@ -973,13 +977,7 @@ static int bcpv=0, bcpj=0; */ int ButtonConfigBegin () { - // shut down the joystick subsystems - //SDL_Surface *screen; - - bcpj = KillJoysticks (); - - // XXX soules - why did we shut this down? - // initialize the joystick subsystem + // initialize the joystick subsystem (if not already inited) InitJoysticks (); buttonConfigInProgress = 1; @@ -995,18 +993,6 @@ int ButtonConfigBegin () void ButtonConfigEnd () { - // shutdown the joystick and video subsystems - KillJoysticks (); - //SDL_QuitSubSystem(SDL_INIT_VIDEO); - - // re-initialize joystick and video subsystems if they were active before - /*if(!bcpv) { - InitVideo(GameInfo); - } */ - if (!bcpj) - { - InitJoysticks (); - } buttonConfigInProgress = 0; } diff --git a/src/drivers/Qt/sdl-joystick.cpp b/src/drivers/Qt/sdl-joystick.cpp index 71148b2e..0434e9f3 100644 --- a/src/drivers/Qt/sdl-joystick.cpp +++ b/src/drivers/Qt/sdl-joystick.cpp @@ -30,7 +30,86 @@ #include #define MAX_JOYSTICKS 32 -static SDL_Joystick *s_Joysticks[MAX_JOYSTICKS] = {NULL}; +struct jsDev_t +{ + SDL_Joystick *js; + SDL_GameController *gc; + + jsDev_t(void) + { + js = NULL; + gc = NULL; + }; + + //~jsDev_t(void) + //{ + // if ( js ) + // { + // SDL_JoystickClose( js ); js = NULL; + // } + // if ( gc ) + // { + // SDL_GameControllerClose( gc ); gc = NULL; + // } + //} + + int close(void) + { + if ( js ) + { + SDL_JoystickClose( js ); js = NULL; + } + if ( gc ) + { + SDL_GameControllerClose( gc ); gc = NULL; + } + return 0; + } + + SDL_Joystick *getJS(void) + { + if ( gc != NULL ) + { + return SDL_GameControllerGetJoystick( gc ); + } + return js; + } + + bool isGameController(void) + { + return ( gc != NULL ); + } + + bool inUse(void) + { + return ( (js != NULL) || (gc != NULL) ); + } + + void print(void) + { + char guidStr[64]; + + SDL_JoystickGUID guid = SDL_JoystickGetGUID( getJS() ); + + SDL_JoystickGetGUIDString( guid, guidStr, sizeof(guidStr) ); + + printf("JoyStickID: %i: '%s' \n", + SDL_JoystickInstanceID( getJS() ), SDL_JoystickName( getJS() ) ); + printf("GUID: %s \n", guidStr ); + printf("NumAxes: %i \n", SDL_JoystickNumAxes(getJS()) ); + printf("NumButtons: %i \n", SDL_JoystickNumButtons(getJS()) ); + printf("NumHats: %i \n", SDL_JoystickNumHats(getJS()) ); + + if ( gc ) + { + printf("GameController Name: '%s'\n", SDL_GameControllerName(gc) ); + printf("GameController Mapping: %s\n", SDL_GameControllerMapping(gc) ); + } + } +}; + +static jsDev_t jsDev[ MAX_JOYSTICKS ]; +//static SDL_Joystick *s_Joysticks[MAX_JOYSTICKS] = {NULL}; static int s_jinited = 0; @@ -42,6 +121,7 @@ int DTestButtonJoy(ButtConfig *bc) { int x; + SDL_Joystick *js; for(x = 0; x < bc->NumC; x++) { @@ -49,10 +129,12 @@ DTestButtonJoy(ButtConfig *bc) { continue; } + js = jsDev[bc->DeviceNum[x]].getJS(); + if (bc->ButtonNum[x] & 0x2000) { /* Hat "button" */ - if(SDL_JoystickGetHat(s_Joysticks[bc->DeviceNum[x]], + if(SDL_JoystickGetHat( js, ((bc->ButtonNum[x] >> 8) & 0x1F)) & (bc->ButtonNum[x]&0xFF)) return 1; @@ -61,7 +143,7 @@ DTestButtonJoy(ButtConfig *bc) { /* Axis "button" */ int pos; - pos = SDL_JoystickGetAxis(s_Joysticks[bc->DeviceNum[x]], + pos = SDL_JoystickGetAxis( js, bc->ButtonNum[x] & 16383); if ((bc->ButtonNum[x] & 0x4000) && pos <= -16383) { return 1; @@ -69,57 +151,162 @@ DTestButtonJoy(ButtConfig *bc) return 1; } } - else if(SDL_JoystickGetButton(s_Joysticks[bc->DeviceNum[x]], + else if(SDL_JoystickGetButton( js, bc->ButtonNum[x])) return 1; } return 0; } + +//static void printJoystick( SDL_Joystick *js ) +//{ +// char guidStr[64]; +// SDL_Joystick *js; +// +// js = jsDev[i].getJS(); +// +// SDL_JoystickGUID guid = SDL_JoystickGetGUID( js ); +// +// SDL_JoystickGetGUIDString( guid, guidStr, sizeof(guidStr) ); +// +// printf("JoyStickID: %i: %s \n", +// SDL_JoystickInstanceID( js ), SDL_JoystickName( js ) ); +// printf("GUID: %s \n", guidStr ); +// printf("NumAxes: %i \n", SDL_JoystickNumAxes(js) ); +// printf("NumButtons: %i \n", SDL_JoystickNumButtons(js) ); +// printf("NumHats: %i \n", SDL_JoystickNumHats(js) ); +// +//} + /** * Shutdown the SDL joystick subsystem. */ int -KillJoysticks() +KillJoysticks(void) { int n; /* joystick index */ - if(!s_jinited) { + if (!s_jinited) { return -1; } - for(n = 0; n < MAX_JOYSTICKS; n++) { - if (s_Joysticks[n] != 0) { - SDL_JoystickClose(s_Joysticks[n]); - } - s_Joysticks[n]=0; + for (n = 0; n < MAX_JOYSTICKS; n++) + { + jsDev[n].close(); } SDL_QuitSubSystem(SDL_INIT_JOYSTICK); return 0; } +int AddJoystick( int which ) +{ + if ( jsDev[ which ].inUse() ) + { + //printf("Error: Joystick already exists at device index %i \n", which ); + return -1; + } + else + { + if ( SDL_IsGameController(which) ) + { + jsDev[which].gc = SDL_GameControllerOpen(which); + + if ( jsDev[which].gc == NULL ) + { + printf("Could not open game controller %d: %s.\n", + which, SDL_GetError()); + } + else + { + printf("Added Joystick: %i \n", which ); + jsDev[which].print(); + //printJoystick( s_Joysticks[which] ); + } + } + else + { + jsDev[which].js = SDL_JoystickOpen(which); + + if ( jsDev[which].js == NULL ) + { + printf("Could not open joystick %d: %s.\n", + which, SDL_GetError()); + } + else + { + printf("Added Joystick: %i \n", which ); + jsDev[which].print(); + //printJoystick( s_Joysticks[which] ); + } + } + } + return 0; +} + +int RemoveJoystick( int which ) +{ + //printf("Remove Joystick: %i \n", which ); + + for (int i=0; iMAX_JOYSTICKS) { + if (total > MAX_JOYSTICKS) + { total = MAX_JOYSTICKS; } - for(n = 0; n < total; n++) { + for (n = 0; n < total; n++) + { /* Open the joystick under SDL. */ - s_Joysticks[n] = SDL_JoystickOpen(n); - //printf("Could not open joystick %d: %s.\n", - //joy[n] - 1, SDL_GetError()); - continue; + AddJoystick(n); + //if ( SDL_IsGameController(n) ) + //{ + // SDL_GameController *gc = SDL_GameControllerOpen(n); + // printf("Is Game Controller: %i \n", n); + + // printf("Mapping: %s \n", SDL_GameControllerMapping(gc) ); + //} + // s_Joysticks[n] = SDL_JoystickOpen(n); + + //if ( s_Joysticks[n] == NULL ) + //{ + // printf("Could not open joystick %d: %s.\n", + // n, SDL_GetError()); + //} + //else + //{ + // printf("Opened JS %i: \n", SDL_JoystickInstanceID( s_Joysticks[n] ) ); + // jsDev[which].print(); + // //printJoystick( s_Joysticks[n] ); + //} } s_jinited = 1;