GTK*: add gamepad hotplug support (#855)

* add gamepad hotplug support
* Get rid of nbr_joy
This commit is contained in:
Max Fedotov 2024-11-04 21:32:07 +03:00 committed by GitHub
parent f51e19b19b
commit 3b1989a9d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 155 additions and 44 deletions

View File

@ -546,7 +546,7 @@ EXPORTED void desmume_input_joy_uninit(void)
EXPORTED u16 desmume_input_joy_number_connected(void) EXPORTED u16 desmume_input_joy_number_connected(void)
{ {
return nbr_joy; return get_number_of_joysticks();
} }
EXPORTED u16 desmume_input_joy_get_key(int index) EXPORTED u16 desmume_input_joy_get_key(int index)

View File

@ -2088,13 +2088,13 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data)
switch(event.type) switch(event.type)
{ {
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
key = ((event.jbutton.which & 15) << 12) | JOY_BUTTON << 8 | (event.jbutton.button & 255); key = ((get_joystick_number_by_id(event.jbutton.which) & 15) << 12) | JOY_BUTTON << 8 | (event.jbutton.button & 255);
done = TRUE; done = TRUE;
break; break;
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
if( ((u32)abs(event.jaxis.value) >> 14) != 0 ) if( ((u32)abs(event.jaxis.value) >> 14) != 0 )
{ {
key = ((event.jaxis.which & 15) << 12) | JOY_AXIS << 8 | ((event.jaxis.axis & 127) << 1); key = ((get_joystick_number_by_id(event.jaxis.which) & 15) << 12) | JOY_AXIS << 8 | ((event.jaxis.axis & 127) << 1);
if (event.jaxis.value > 0) if (event.jaxis.value > 0)
key |= 1; key |= 1;
done = TRUE; done = TRUE;
@ -2102,7 +2102,7 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data)
break; break;
case SDL_JOYHATMOTION: case SDL_JOYHATMOTION:
if (event.jhat.value != SDL_HAT_CENTERED) { if (event.jhat.value != SDL_HAT_CENTERED) {
key = ((event.jhat.which & 15) << 12) | JOY_HAT << 8 | ((event.jhat.hat & 63) << 2); key = ((get_joystick_number_by_id(event.jhat.which) & 15) << 12) | JOY_HAT << 8 | ((event.jhat.hat & 63) << 2);
if ((event.jhat.value & SDL_HAT_UP) != 0) if ((event.jhat.value & SDL_HAT_UP) != 0)
key |= JOY_HAT_UP; key |= JOY_HAT_UP;
else if ((event.jhat.value & SDL_HAT_RIGHT) != 0) else if ((event.jhat.value & SDL_HAT_RIGHT) != 0)
@ -2114,6 +2114,9 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data)
done = TRUE; done = TRUE;
} }
break; break;
default:
do_process_joystick_device_events(&event);
break;
} }
} }
@ -3320,6 +3323,13 @@ static gboolean timeout_exit_cb(gpointer data)
return FALSE; return FALSE;
} }
static gboolean OutOfLoopJoyDeviceCheckTimerFunc(gpointer data)
{
if(!regMainLoop && !in_joy_config_mode)
process_joystick_device_events();
return !regMainLoop;
}
static void static void
common_gtk_main(GApplication *app, gpointer user_data) common_gtk_main(GApplication *app, gpointer user_data)
{ {
@ -4118,6 +4128,7 @@ common_gtk_main(GApplication *app, gpointer user_data)
video->SetFilterParameteri(VF_PARAM_SCANLINE_D, _scanline_filter_d); video->SetFilterParameteri(VF_PARAM_SCANLINE_D, _scanline_filter_d);
RedrawScreen(); RedrawScreen();
g_timeout_add(200, OutOfLoopJoyDeviceCheckTimerFunc, 0);
} }
static void Teardown() { static void Teardown() {

View File

@ -2277,13 +2277,13 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data)
switch(event.type) switch(event.type)
{ {
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
key = ((event.jbutton.which & 15) << 12) | JOY_BUTTON << 8 | (event.jbutton.button & 255); key = ((get_joystick_number_by_id(event.jbutton.which) & 15) << 12) | JOY_BUTTON << 8 | (event.jbutton.button & 255);
done = TRUE; done = TRUE;
break; break;
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
if( ((u32)abs(event.jaxis.value) >> 14) != 0 ) if( ((u32)abs(event.jaxis.value) >> 14) != 0 )
{ {
key = ((event.jaxis.which & 15) << 12) | JOY_AXIS << 8 | ((event.jaxis.axis & 127) << 1); key = ((get_joystick_number_by_id(event.jaxis.which) & 15) << 12) | JOY_AXIS << 8 | ((event.jaxis.axis & 127) << 1);
if (event.jaxis.value > 0) if (event.jaxis.value > 0)
key |= 1; key |= 1;
done = TRUE; done = TRUE;
@ -2291,7 +2291,7 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data)
break; break;
case SDL_JOYHATMOTION: case SDL_JOYHATMOTION:
if (event.jhat.value != SDL_HAT_CENTERED) { if (event.jhat.value != SDL_HAT_CENTERED) {
key = ((event.jhat.which & 15) << 12) | JOY_HAT << 8 | ((event.jhat.hat & 63) << 2); key = ((get_joystick_number_by_id(event.jhat.which) & 15) << 12) | JOY_HAT << 8 | ((event.jhat.hat & 63) << 2);
if ((event.jhat.value & SDL_HAT_UP) != 0) if ((event.jhat.value & SDL_HAT_UP) != 0)
key |= JOY_HAT_UP; key |= JOY_HAT_UP;
else if ((event.jhat.value & SDL_HAT_RIGHT) != 0) else if ((event.jhat.value & SDL_HAT_RIGHT) != 0)
@ -2303,6 +2303,9 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data)
done = TRUE; done = TRUE;
} }
break; break;
default:
do_process_joystick_device_events(&event);
break;
} }
} }
@ -3446,6 +3449,13 @@ static gboolean timeout_exit_cb(gpointer data)
return FALSE; return FALSE;
} }
static gboolean OutOfLoopJoyDeviceCheckTimerFunc(gpointer data)
{
if(!regMainLoop && !in_joy_config_mode)
process_joystick_device_events();
return !regMainLoop;
}
static int static int
common_gtk_main( class configured_features *my_config) common_gtk_main( class configured_features *my_config)
{ {
@ -4007,6 +4017,7 @@ common_gtk_main( class configured_features *my_config)
video->SetFilterParameteri(VF_PARAM_SCANLINE_D, _scanline_filter_d); video->SetFilterParameteri(VF_PARAM_SCANLINE_D, _scanline_filter_d);
RedrawScreen(); RedrawScreen();
g_timeout_add(200, OutOfLoopJoyDeviceCheckTimerFunc, 0);
/* Main loop */ /* Main loop */
gtk_main(); gtk_main();

View File

@ -35,11 +35,11 @@ u32 joypad_cfg[NB_KEYS];
static_assert(sizeof(keyboard_cfg) == sizeof(joypad_cfg), ""); static_assert(sizeof(keyboard_cfg) == sizeof(joypad_cfg), "");
u16 nbr_joy;
mouse_status mouse; mouse_status mouse;
static int fullscreen; static int fullscreen;
static SDL_Joystick **open_joysticks = NULL; //List of currently connected joysticks (key - instance id, value - SDL_Joystick structure, joystick number)
static std::pair<SDL_Joystick *, SDL_JoystickID> open_joysticks[MAX_JOYSTICKS];
/* Keypad key names */ /* Keypad key names */
const char *key_names[NB_KEYS] = const char *key_names[NB_KEYS] =
@ -116,28 +116,30 @@ BOOL init_joy( void) {
return FALSE; return FALSE;
} }
nbr_joy = SDL_NumJoysticks(); for(i=0; i<MAX_JOYSTICKS; ++i)
open_joysticks[i]=std::make_pair((SDL_Joystick*)NULL, 0);
int nbr_joy = std::min(SDL_NumJoysticks(), MAX_JOYSTICKS);
if ( nbr_joy > 0) { if ( nbr_joy > 0) {
printf("Found %d joysticks\n", nbr_joy); printf("Found %d joysticks\n", nbr_joy);
open_joysticks =
(SDL_Joystick**)calloc( sizeof ( SDL_Joystick *), nbr_joy);
if ( open_joysticks != NULL) { for (i = 0; i < nbr_joy; i++) {
for (i = 0; i < nbr_joy; i++)
{
SDL_Joystick * joy = SDL_JoystickOpen(i); SDL_Joystick * joy = SDL_JoystickOpen(i);
if(joy) {
printf("Joystick %d %s\n", i, SDL_JoystickNameForIndex(i)); printf("Joystick %d %s\n", i, SDL_JoystickNameForIndex(i));
printf("Axes: %d\n", SDL_JoystickNumAxes(joy)); printf("Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Buttons: %d\n", SDL_JoystickNumButtons(joy)); printf("Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy)); printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy));
printf("Hats: %d\n\n", SDL_JoystickNumHats(joy)); printf("Hats: %d\n\n", SDL_JoystickNumHats(joy));
} open_joysticks[i]=std::make_pair(joy, SDL_JoystickInstanceID(joy));
} }
else { else {
fprintf(stderr, "Failed to open joystick %d: %s\n", i, SDL_GetError());
joy_init_good = FALSE; joy_init_good = FALSE;
} }
} }
}
return joy_init_good; return joy_init_good;
} }
@ -146,16 +148,13 @@ BOOL init_joy( void) {
void uninit_joy( void) void uninit_joy( void)
{ {
int i; int i;
for (i=0; i<MAX_JOYSTICKS; ++i) {
if ( open_joysticks != NULL) { if(open_joysticks[i].first) {
for (i = 0; i < SDL_NumJoysticks(); i++) { SDL_JoystickClose(open_joysticks[i].first);
SDL_JoystickClose( open_joysticks[i]); open_joysticks[i]=std::make_pair((SDL_Joystick*)NULL, 0);
}
} }
free( open_joysticks);
}
open_joysticks = NULL;
SDL_QuitSubSystem(SDL_INIT_JOYSTICK); SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
} }
@ -203,14 +202,14 @@ u16 get_joy_key(int index) {
{ {
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
printf( "Device: %d; Button: %d\n", event.jbutton.which, event.jbutton.button ); printf( "Device: %d; Button: %d\n", event.jbutton.which, event.jbutton.button );
key = ((event.jbutton.which & 15) << 12) | JOY_BUTTON << 8 | (event.jbutton.button & 255); key = ((get_joystick_number_by_id(event.jbutton.which) & 15) << 12) | JOY_BUTTON << 8 | (event.jbutton.button & 255);
done = TRUE; done = TRUE;
break; break;
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
/* Dead zone of 50% */ /* Dead zone of 50% */
if( ((u32)abs(event.jaxis.value) >> 14) != 0 ) if( ((u32)abs(event.jaxis.value) >> 14) != 0 )
{ {
key = ((event.jaxis.which & 15) << 12) | JOY_AXIS << 8 | ((event.jaxis.axis & 127) << 1); key = ((get_joystick_number_by_id(event.jaxis.which) & 15) << 12) | JOY_AXIS << 8 | ((event.jaxis.axis & 127) << 1);
if (event.jaxis.value > 0) { if (event.jaxis.value > 0) {
printf( "Device: %d; Axis: %d (+)\n", event.jaxis.which, event.jaxis.axis ); printf( "Device: %d; Axis: %d (+)\n", event.jaxis.which, event.jaxis.axis );
key |= 1; key |= 1;
@ -224,7 +223,7 @@ u16 get_joy_key(int index) {
/* Diagonal positions will be treated as two separate keys being activated, rather than a single diagonal key. */ /* Diagonal positions will be treated as two separate keys being activated, rather than a single diagonal key. */
/* JOY_HAT_* are sequential integers, rather than a bitmask */ /* JOY_HAT_* are sequential integers, rather than a bitmask */
if (event.jhat.value != SDL_HAT_CENTERED) { if (event.jhat.value != SDL_HAT_CENTERED) {
key = ((event.jhat.which & 15) << 12) | JOY_HAT << 8 | ((event.jhat.hat & 63) << 2); key = ((get_joystick_number_by_id(event.jhat.which) & 15) << 12) | JOY_HAT << 8 | ((event.jhat.hat & 63) << 2);
/* Can't just use a switch here because SDL_HAT_* make up a bitmask. We only want one of these when assigning keys. */ /* Can't just use a switch here because SDL_HAT_* make up a bitmask. We only want one of these when assigning keys. */
if ((event.jhat.value & SDL_HAT_UP) != 0) { if ((event.jhat.value & SDL_HAT_UP) != 0) {
key |= JOY_HAT_UP; key |= JOY_HAT_UP;
@ -379,7 +378,7 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) {
/* Joystick axis motion /* Joystick axis motion
Note: button constants have a 1bit offset. */ Note: button constants have a 1bit offset. */
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
key_code = ((event->jaxis.which & 15) << 12) | JOY_AXIS << 8 | ((event->jaxis.axis & 127) << 1); key_code = ((get_joystick_number_by_id(event->jaxis.which) & 15) << 12) | JOY_AXIS << 8 | ((event->jaxis.axis & 127) << 1);
if( ((u32)abs(event->jaxis.value) >> 14) != 0 ) if( ((u32)abs(event->jaxis.value) >> 14) != 0 )
{ {
if (event->jaxis.value > 0) if (event->jaxis.value > 0)
@ -406,7 +405,7 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) {
case SDL_JOYHATMOTION: case SDL_JOYHATMOTION:
/* Diagonal positions will be treated as two separate keys being activated, rather than a single diagonal key. */ /* Diagonal positions will be treated as two separate keys being activated, rather than a single diagonal key. */
/* JOY_HAT_* are sequential integers, rather than a bitmask */ /* JOY_HAT_* are sequential integers, rather than a bitmask */
key_code = ((event->jhat.which & 15) << 12) | JOY_HAT << 8 | ((event->jhat.hat & 63) << 2); key_code = ((get_joystick_number_by_id(event->jhat.which) & 15) << 12) | JOY_HAT << 8 | ((event->jhat.hat & 63) << 2);
key_u = lookup_joy_key( key_code | JOY_HAT_UP ); key_u = lookup_joy_key( key_code | JOY_HAT_UP );
key_r = lookup_joy_key( key_code | JOY_HAT_RIGHT ); key_r = lookup_joy_key( key_code | JOY_HAT_RIGHT );
key_d = lookup_joy_key( key_code | JOY_HAT_DOWN ); key_d = lookup_joy_key( key_code | JOY_HAT_DOWN );
@ -431,7 +430,7 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) {
/* Joystick button pressed */ /* Joystick button pressed */
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
key_code = ((event->jbutton.which & 15) << 12) | JOY_BUTTON << 8 | (event->jbutton.button & 255); key_code = ((get_joystick_number_by_id(event->jbutton.which) & 15) << 12) | JOY_BUTTON << 8 | (event->jbutton.button & 255);
key = lookup_joy_key( key_code ); key = lookup_joy_key( key_code );
if (key != 0) if (key != 0)
ADD_KEY( *keypad, key ); ADD_KEY( *keypad, key );
@ -439,7 +438,7 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) {
/* Joystick button released */ /* Joystick button released */
case SDL_JOYBUTTONUP: case SDL_JOYBUTTONUP:
key_code = ((event->jbutton.which & 15) << 12) | JOY_BUTTON << 8 | (event->jbutton.button & 255); key_code = ((get_joystick_number_by_id(event->jbutton.which) & 15) << 12) | JOY_BUTTON << 8 | (event->jbutton.button & 255);
key = lookup_joy_key( key_code ); key = lookup_joy_key( key_code );
if (key != 0) if (key != 0)
RM_KEY( *keypad, key ); RM_KEY( *keypad, key );
@ -453,6 +452,54 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) {
return processed; return processed;
} }
/*
* The real joystick connect/disconnect processing function
* Not static because we need it in some frontends during gamepad button configuration
*/
int
do_process_joystick_device_events(SDL_Event* event) {
int processed = 1, n;
switch(event->type) {
/* Joystick disconnected */
case SDL_JOYDEVICEREMOVED:
n=get_joystick_number_by_id(event->jdevice.which);
if(n != -1) {
printf("Joystick %d disconnected\n", n);
SDL_JoystickClose(open_joysticks[n].first);
open_joysticks[n]=std::make_pair((SDL_Joystick*)NULL, 0);
}
break;
/* Joystick connected */
case SDL_JOYDEVICEADDED:
if(event->jdevice.which<MAX_JOYSTICKS) {
//Filter connect events for joysticks already initialized in init_joy
if(!open_joysticks[event->jdevice.which].first) {
SDL_Joystick* joy = SDL_JoystickOpen(event->jdevice.which);
if(joy) {
printf("Joystick %d %s\n", event->jdevice.which, SDL_JoystickNameForIndex(event->jdevice.which));
printf("Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy));
printf("Hats: %d\n\n", SDL_JoystickNumHats(joy));
open_joysticks[event->jdevice.which]=std::make_pair(joy, SDL_JoystickInstanceID(joy));
}
else
fprintf(stderr, "Failed to open joystick %d: %s\n", event->jdevice.which, SDL_GetError());
}
}
else
printf("Joystick %d connected to the system, but maximum supported joystick index is %d, ignoring\n",
event->jdevice.which, MAX_JOYSTICKS-1);
break;
default:
processed = 0;
break;
}
return processed;
}
/* /*
* Process only the joystick events * Process only the joystick events
*/ */
@ -467,10 +514,45 @@ process_joystick_events( u16 *keypad) {
/* There's an event waiting to be processed? */ /* There's an event waiting to be processed? */
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event))
{ {
do_process_joystick_events( keypad, &event); if(!do_process_joystick_events( keypad, &event))
do_process_joystick_device_events(&event);
} }
} }
/*
* Process only joystick connect/disconnect events
*/
void process_joystick_device_events() {
SDL_Event event;
/* IMPORTANT: Reenable joystick events if needed. */
if(SDL_JoystickEventState(SDL_QUERY) == SDL_IGNORE)
SDL_JoystickEventState(SDL_ENABLE);
/* There's an event waiting to be processed? */
while (SDL_PollEvent(&event))
do_process_joystick_device_events(&event);
}
int get_joystick_number_by_id(SDL_JoystickID id)
{
int i;
for(i=0; i<MAX_JOYSTICKS; ++i)
if(open_joysticks[i].first)
if(open_joysticks[i].second==id)
return i;
return -1;
}
int get_number_of_joysticks()
{
int i, n=0;
for(i=0; i<MAX_JOYSTICKS; ++i)
if(open_joysticks[i].first)
++n;
return n;
}
static u16 shift_pressed; static u16 shift_pressed;
void void

View File

@ -30,6 +30,8 @@
#include "types.h" #include "types.h"
#define MAX_JOYSTICKS 16
#define ADD_KEY(keypad,key) ( (keypad) |= (key) ) #define ADD_KEY(keypad,key) ( (keypad) |= (key) )
#define RM_KEY(keypad,key) ( (keypad) &= ~(key) ) #define RM_KEY(keypad,key) ( (keypad) &= ~(key) )
#define KEYMASK_(k) (1 << (k)) #define KEYMASK_(k) (1 << (k))
@ -67,8 +69,6 @@ extern const char *key_names[NB_KEYS];
extern u32 keyboard_cfg[NB_KEYS]; extern u32 keyboard_cfg[NB_KEYS];
/* Current joypad configuration */ /* Current joypad configuration */
extern u32 joypad_cfg[NB_KEYS]; extern u32 joypad_cfg[NB_KEYS];
/* Number of detected joypads */
extern u16 nbr_joy;
#ifndef GTK_UI #ifndef GTK_UI
struct mouse_status struct mouse_status
@ -110,5 +110,12 @@ process_ctrls_event( SDL_Event& event,
void void
process_joystick_events( u16 *keypad); process_joystick_events( u16 *keypad);
int
do_process_joystick_device_events(SDL_Event* event);
void
process_joystick_device_events();
int get_joystick_number_by_id(SDL_JoystickID id);
int get_number_of_joysticks();
#endif /* CTRLSSDL_H */ #endif /* CTRLSSDL_H */