From f439e08844e76232cbb9b78b7db08db66cb09294 Mon Sep 17 00:00:00 2001 From: thesource Date: Tue, 29 Oct 2024 10:44:31 +0300 Subject: [PATCH 1/6] Linux/GTK3: add gamepad hotplug support --- desmume/src/frontend/posix/gtk/main.cpp | 17 ++- .../src/frontend/posix/shared/ctrlssdl.cpp | 142 +++++++++++++----- desmume/src/frontend/posix/shared/ctrlssdl.h | 6 + 3 files changed, 127 insertions(+), 38 deletions(-) diff --git a/desmume/src/frontend/posix/gtk/main.cpp b/desmume/src/frontend/posix/gtk/main.cpp index 5c6fb0bff..59c39427b 100644 --- a/desmume/src/frontend/posix/gtk/main.cpp +++ b/desmume/src/frontend/posix/gtk/main.cpp @@ -2088,13 +2088,13 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data) switch(event.type) { 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; break; case SDL_JOYAXISMOTION: 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) key |= 1; done = TRUE; @@ -2102,7 +2102,7 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data) break; case SDL_JOYHATMOTION: 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) key |= JOY_HAT_UP; else if ((event.jhat.value & SDL_HAT_RIGHT) != 0) @@ -2114,6 +2114,9 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data) done = TRUE; } break; + default: + do_process_joystick_device_events(&event); + break; } } @@ -3320,6 +3323,13 @@ static gboolean timeout_exit_cb(gpointer data) return FALSE; } +static gboolean OutOfLoopJoyDeviceCheckTimerFunc(gpointer data) +{ + if(!regMainLoop && !in_joy_config_mode) + process_joystick_device_events(); + return !regMainLoop; +} + static void 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); RedrawScreen(); + g_timeout_add(200, OutOfLoopJoyDeviceCheckTimerFunc, 0); } static void Teardown() { diff --git a/desmume/src/frontend/posix/shared/ctrlssdl.cpp b/desmume/src/frontend/posix/shared/ctrlssdl.cpp index 30a4477b4..3b4768ae5 100644 --- a/desmume/src/frontend/posix/shared/ctrlssdl.cpp +++ b/desmume/src/frontend/posix/shared/ctrlssdl.cpp @@ -23,6 +23,8 @@ #include "NDSSystem.h" #include "frontend/modules/osd/agg/agg_osd.h" #include "driver.h" +#include +#include #ifdef FAKE_MIC #include "mic.h" @@ -39,7 +41,8 @@ u16 nbr_joy; mouse_status mouse; static int fullscreen; -static SDL_Joystick **open_joysticks = NULL; +//List of currently connected joysticks (SDL_Joystick structure, joystick instance ID, joystick number) +static std::list > open_joysticks; /* Keypad key names */ const char *key_names[NB_KEYS] = @@ -120,22 +123,21 @@ BOOL init_joy( void) { if ( nbr_joy > 0) { 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++) - { - SDL_Joystick * joy = SDL_JoystickOpen(i); - printf("Joystick %d %s\n", i, SDL_JoystickNameForIndex(i)); - 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)); - } - } - else { - joy_init_good = FALSE; + + for (i = 0; i < nbr_joy; i++) { + SDL_Joystick * joy = SDL_JoystickOpen(i); + if(joy) { + printf("Joystick %d %s\n", i, SDL_JoystickNameForIndex(i)); + 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.push_back(std::make_tuple(joy, SDL_JoystickInstanceID(joy), i)); + } + else { + fprintf(stderr, "Failed to open joystick %d: %s\n", i, SDL_GetError()); + joy_init_good = FALSE; + } } } @@ -145,17 +147,13 @@ BOOL init_joy( void) { /* Unload joysticks */ void uninit_joy( void) { - int i; - - if ( open_joysticks != NULL) { - for (i = 0; i < SDL_NumJoysticks(); i++) { - SDL_JoystickClose( open_joysticks[i]); - } - - free( open_joysticks); + for (auto & it: open_joysticks) { + if(std::get<0>(it)) + SDL_JoystickClose(std::get<0>(it)); } - - open_joysticks = NULL; + + open_joysticks.clear(); + nbr_joy = 0; SDL_QuitSubSystem(SDL_INIT_JOYSTICK); } @@ -203,14 +201,14 @@ u16 get_joy_key(int index) { { case SDL_JOYBUTTONDOWN: 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; break; case SDL_JOYAXISMOTION: /* Dead zone of 50% */ 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) { printf( "Device: %d; Axis: %d (+)\n", event.jaxis.which, event.jaxis.axis ); key |= 1; @@ -224,7 +222,7 @@ u16 get_joy_key(int index) { /* 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 */ 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. */ if ((event.jhat.value & SDL_HAT_UP) != 0) { key |= JOY_HAT_UP; @@ -379,7 +377,7 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) { /* Joystick axis motion Note: button constants have a 1bit offset. */ 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 (event->jaxis.value > 0) @@ -406,7 +404,7 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) { case SDL_JOYHATMOTION: /* 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 */ - 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_r = lookup_joy_key( key_code | JOY_HAT_RIGHT ); key_d = lookup_joy_key( key_code | JOY_HAT_DOWN ); @@ -432,7 +430,7 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) { /* Joystick button pressed */ /* FIXME: Add support for BOOST */ 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 ); if (key != 0) ADD_KEY( *keypad, key ); @@ -440,7 +438,7 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) { /* Joystick button released */ 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 ); if (key != 0) RM_KEY( *keypad, key ); @@ -454,6 +452,55 @@ do_process_joystick_events( u16 *keypad, SDL_Event *event) { 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; + switch(event->type) { + /* Joystick disconnected */ + case SDL_JOYDEVICEREMOVED: + { + auto it = std::find_if(open_joysticks.begin(), open_joysticks.end(), + [event] (const decltype(open_joysticks)::value_type & a) {return std::get<1>(a)==event->jdevice.which;}); + if(it != open_joysticks.end()) { + printf("Joystick with instance %d disconnected\n", event->jdevice.which); + SDL_JoystickClose(std::get<0>(*it)); + open_joysticks.erase(it); + } + nbr_joy = open_joysticks.size(); + } + break; + + /* Joystick connected */ + case SDL_JOYDEVICEADDED: + { + if(get_joystick_number_by_id(SDL_JoystickGetDeviceInstanceID(event->jdevice.which))==-1) { + 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.push_back(std::make_tuple(joy, SDL_JoystickInstanceID(joy), event->jdevice.which)); + nbr_joy = open_joysticks.size(); + } + else + fprintf(stderr, "Failed to open joystick %d: %s\n", event->jdevice.which, SDL_GetError()); + } + } + break; + + default: + processed = 0; + break; + } + return processed; +} + /* * Process only the joystick events */ @@ -468,10 +515,35 @@ process_joystick_events( u16 *keypad) { /* There's an event waiting to be processed? */ 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) +{ + auto it = std::find_if(open_joysticks.cbegin(), open_joysticks.cend(), + [id] (const decltype(open_joysticks)::value_type & a) {return std::get<1>(a)==id;}); + if(it != open_joysticks.cend()) + return std::get<2>(*it); + return -1; +} + static u16 shift_pressed; void diff --git a/desmume/src/frontend/posix/shared/ctrlssdl.h b/desmume/src/frontend/posix/shared/ctrlssdl.h index a56c42b93..7610e75fc 100644 --- a/desmume/src/frontend/posix/shared/ctrlssdl.h +++ b/desmume/src/frontend/posix/shared/ctrlssdl.h @@ -111,5 +111,11 @@ process_ctrls_event( SDL_Event& event, void 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); #endif /* CTRLSSDL_H */ From 6a65fcd7aef9a29622b68c950c42ffa8841f25d9 Mon Sep 17 00:00:00 2001 From: thesource Date: Tue, 29 Oct 2024 13:23:32 +0300 Subject: [PATCH 2/6] Use unordered_map for open_joysticks since we need instance lookup often --- .../src/frontend/posix/shared/ctrlssdl.cpp | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/desmume/src/frontend/posix/shared/ctrlssdl.cpp b/desmume/src/frontend/posix/shared/ctrlssdl.cpp index 3b4768ae5..acccc13fb 100644 --- a/desmume/src/frontend/posix/shared/ctrlssdl.cpp +++ b/desmume/src/frontend/posix/shared/ctrlssdl.cpp @@ -24,7 +24,7 @@ #include "frontend/modules/osd/agg/agg_osd.h" #include "driver.h" #include -#include +#include #ifdef FAKE_MIC #include "mic.h" @@ -41,8 +41,8 @@ u16 nbr_joy; mouse_status mouse; static int fullscreen; -//List of currently connected joysticks (SDL_Joystick structure, joystick instance ID, joystick number) -static std::list > open_joysticks; +//List of currently connected joysticks (key - instance id, value - SDL_Joystick structure, joystick number) +static std::unordered_map > open_joysticks; /* Keypad key names */ const char *key_names[NB_KEYS] = @@ -132,13 +132,14 @@ BOOL init_joy( void) { printf("Buttons: %d\n", SDL_JoystickNumButtons(joy)); printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy)); printf("Hats: %d\n\n", SDL_JoystickNumHats(joy)); - open_joysticks.push_back(std::make_tuple(joy, SDL_JoystickInstanceID(joy), i)); + open_joysticks.insert(std::make_pair(SDL_JoystickInstanceID(joy), std::make_pair(joy, i))); } else { fprintf(stderr, "Failed to open joystick %d: %s\n", i, SDL_GetError()); joy_init_good = FALSE; } } + nbr_joy = open_joysticks.size(); } return joy_init_good; @@ -148,8 +149,8 @@ BOOL init_joy( void) { void uninit_joy( void) { for (auto & it: open_joysticks) { - if(std::get<0>(it)) - SDL_JoystickClose(std::get<0>(it)); + if(it.second.first) + SDL_JoystickClose(it.second.first); } open_joysticks.clear(); @@ -463,11 +464,10 @@ do_process_joystick_device_events(SDL_Event* event) { /* Joystick disconnected */ case SDL_JOYDEVICEREMOVED: { - auto it = std::find_if(open_joysticks.begin(), open_joysticks.end(), - [event] (const decltype(open_joysticks)::value_type & a) {return std::get<1>(a)==event->jdevice.which;}); - if(it != open_joysticks.end()) { + auto it = open_joysticks.find(event->jdevice.which); + if(it != open_joysticks.cend()) { printf("Joystick with instance %d disconnected\n", event->jdevice.which); - SDL_JoystickClose(std::get<0>(*it)); + SDL_JoystickClose(it->second.first); open_joysticks.erase(it); } nbr_joy = open_joysticks.size(); @@ -485,7 +485,7 @@ do_process_joystick_device_events(SDL_Event* event) { printf("Buttons: %d\n", SDL_JoystickNumButtons(joy)); printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy)); printf("Hats: %d\n\n", SDL_JoystickNumHats(joy)); - open_joysticks.push_back(std::make_tuple(joy, SDL_JoystickInstanceID(joy), event->jdevice.which)); + open_joysticks.insert(std::make_pair(SDL_JoystickInstanceID(joy), std::make_pair(joy, event->jdevice.which))); nbr_joy = open_joysticks.size(); } else @@ -537,10 +537,9 @@ void process_joystick_device_events() { int get_joystick_number_by_id(SDL_JoystickID id) { - auto it = std::find_if(open_joysticks.cbegin(), open_joysticks.cend(), - [id] (const decltype(open_joysticks)::value_type & a) {return std::get<1>(a)==id;}); + auto it = open_joysticks.find(id); if(it != open_joysticks.cend()) - return std::get<2>(*it); + return it->second.second; return -1; } From 7e9436306064e84dcb9f6e6af994ef9f02c2fce4 Mon Sep 17 00:00:00 2001 From: thesource Date: Tue, 29 Oct 2024 18:29:45 +0300 Subject: [PATCH 3/6] Make open_joysticks a static array --- .../src/frontend/posix/shared/ctrlssdl.cpp | 56 ++++++++++--------- desmume/src/frontend/posix/shared/ctrlssdl.h | 2 + 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/desmume/src/frontend/posix/shared/ctrlssdl.cpp b/desmume/src/frontend/posix/shared/ctrlssdl.cpp index acccc13fb..416f4650b 100644 --- a/desmume/src/frontend/posix/shared/ctrlssdl.cpp +++ b/desmume/src/frontend/posix/shared/ctrlssdl.cpp @@ -23,8 +23,6 @@ #include "NDSSystem.h" #include "frontend/modules/osd/agg/agg_osd.h" #include "driver.h" -#include -#include #ifdef FAKE_MIC #include "mic.h" @@ -42,7 +40,7 @@ mouse_status mouse; static int fullscreen; //List of currently connected joysticks (key - instance id, value - SDL_Joystick structure, joystick number) -static std::unordered_map > open_joysticks; +static std::pair open_joysticks[MAX_JOYSTICKS]; /* Keypad key names */ const char *key_names[NB_KEYS] = @@ -118,8 +116,11 @@ BOOL init_joy( void) { SDL_GetError()); return FALSE; } + + for(i=0; i 0) { printf("Found %d joysticks\n", nbr_joy); @@ -132,14 +133,13 @@ BOOL init_joy( void) { printf("Buttons: %d\n", SDL_JoystickNumButtons(joy)); printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy)); printf("Hats: %d\n\n", SDL_JoystickNumHats(joy)); - open_joysticks.insert(std::make_pair(SDL_JoystickInstanceID(joy), std::make_pair(joy, i))); + open_joysticks[i]=std::make_pair(joy, SDL_JoystickInstanceID(joy)); } else { fprintf(stderr, "Failed to open joystick %d: %s\n", i, SDL_GetError()); joy_init_good = FALSE; } } - nbr_joy = open_joysticks.size(); } return joy_init_good; @@ -148,12 +148,14 @@ BOOL init_joy( void) { /* Unload joysticks */ void uninit_joy( void) { - for (auto & it: open_joysticks) { - if(it.second.first) - SDL_JoystickClose(it.second.first); + int i; + for (i=0; itype) { /* Joystick disconnected */ case SDL_JOYDEVICEREMOVED: - { - auto it = open_joysticks.find(event->jdevice.which); - if(it != open_joysticks.cend()) { - printf("Joystick with instance %d disconnected\n", event->jdevice.which); - SDL_JoystickClose(it->second.first); - open_joysticks.erase(it); - } - nbr_joy = open_joysticks.size(); + 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(get_joystick_number_by_id(SDL_JoystickGetDeviceInstanceID(event->jdevice.which))==-1) { + if(event->jdevice.whichjdevice.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)); @@ -485,13 +485,15 @@ do_process_joystick_device_events(SDL_Event* event) { printf("Buttons: %d\n", SDL_JoystickNumButtons(joy)); printf("Trackballs: %d\n", SDL_JoystickNumBalls(joy)); printf("Hats: %d\n\n", SDL_JoystickNumHats(joy)); - open_joysticks.insert(std::make_pair(SDL_JoystickInstanceID(joy), std::make_pair(joy, event->jdevice.which))); - nbr_joy = open_joysticks.size(); + 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: @@ -537,9 +539,11 @@ void process_joystick_device_events() { int get_joystick_number_by_id(SDL_JoystickID id) { - auto it = open_joysticks.find(id); - if(it != open_joysticks.cend()) - return it->second.second; + int i; + for(i=0; i Date: Wed, 30 Oct 2024 08:43:50 +0300 Subject: [PATCH 4/6] Add joystick count function --- desmume/src/frontend/posix/shared/ctrlssdl.cpp | 9 +++++++++ desmume/src/frontend/posix/shared/ctrlssdl.h | 1 + 2 files changed, 10 insertions(+) diff --git a/desmume/src/frontend/posix/shared/ctrlssdl.cpp b/desmume/src/frontend/posix/shared/ctrlssdl.cpp index 416f4650b..bbf8d2ab8 100644 --- a/desmume/src/frontend/posix/shared/ctrlssdl.cpp +++ b/desmume/src/frontend/posix/shared/ctrlssdl.cpp @@ -547,6 +547,15 @@ int get_joystick_number_by_id(SDL_JoystickID id) return -1; } +int get_number_of_joysticks() +{ + int i, n=0; + for(i=0; i Date: Wed, 30 Oct 2024 18:52:38 +0300 Subject: [PATCH 5/6] Get rid of nbr_joy --- desmume/src/frontend/interface/interface.cpp | 2 +- desmume/src/frontend/posix/shared/ctrlssdl.cpp | 4 +--- desmume/src/frontend/posix/shared/ctrlssdl.h | 2 -- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/desmume/src/frontend/interface/interface.cpp b/desmume/src/frontend/interface/interface.cpp index bb731db2c..7a1c003cf 100644 --- a/desmume/src/frontend/interface/interface.cpp +++ b/desmume/src/frontend/interface/interface.cpp @@ -546,7 +546,7 @@ EXPORTED void desmume_input_joy_uninit(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) diff --git a/desmume/src/frontend/posix/shared/ctrlssdl.cpp b/desmume/src/frontend/posix/shared/ctrlssdl.cpp index bbf8d2ab8..ff8edde54 100644 --- a/desmume/src/frontend/posix/shared/ctrlssdl.cpp +++ b/desmume/src/frontend/posix/shared/ctrlssdl.cpp @@ -35,7 +35,6 @@ u32 joypad_cfg[NB_KEYS]; static_assert(sizeof(keyboard_cfg) == sizeof(joypad_cfg), ""); -u16 nbr_joy; mouse_status mouse; static int fullscreen; @@ -120,7 +119,7 @@ BOOL init_joy( void) { for(i=0; i 0) { printf("Found %d joysticks\n", nbr_joy); @@ -156,7 +155,6 @@ void uninit_joy( void) } } - nbr_joy = 0; SDL_QuitSubSystem(SDL_INIT_JOYSTICK); } diff --git a/desmume/src/frontend/posix/shared/ctrlssdl.h b/desmume/src/frontend/posix/shared/ctrlssdl.h index b4ab623eb..a7a2980b9 100644 --- a/desmume/src/frontend/posix/shared/ctrlssdl.h +++ b/desmume/src/frontend/posix/shared/ctrlssdl.h @@ -69,8 +69,6 @@ extern const char *key_names[NB_KEYS]; extern u32 keyboard_cfg[NB_KEYS]; /* Current joypad configuration */ extern u32 joypad_cfg[NB_KEYS]; -/* Number of detected joypads */ -extern u16 nbr_joy; #ifndef GTK_UI struct mouse_status From a744a3a33ad0bf64eadd1a562756b955f0dd8634 Mon Sep 17 00:00:00 2001 From: thesource Date: Thu, 31 Oct 2024 09:34:01 +0300 Subject: [PATCH 6/6] Port these changes to GTK2 too. --- desmume/src/frontend/posix/gtk2/main.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/desmume/src/frontend/posix/gtk2/main.cpp b/desmume/src/frontend/posix/gtk2/main.cpp index d04a47d8c..3e09644e0 100644 --- a/desmume/src/frontend/posix/gtk2/main.cpp +++ b/desmume/src/frontend/posix/gtk2/main.cpp @@ -2245,13 +2245,13 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data) switch(event.type) { 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; break; case SDL_JOYAXISMOTION: 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) key |= 1; done = TRUE; @@ -2259,7 +2259,7 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data) break; case SDL_JOYHATMOTION: 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) key |= JOY_HAT_UP; else if ((event.jhat.value & SDL_HAT_RIGHT) != 0) @@ -2271,6 +2271,9 @@ static gboolean JoyKeyAcceptTimerFunc(gpointer data) done = TRUE; } break; + default: + do_process_joystick_device_events(&event); + break; } } @@ -3367,6 +3370,13 @@ static gboolean timeout_exit_cb(gpointer data) return FALSE; } +static gboolean OutOfLoopJoyDeviceCheckTimerFunc(gpointer data) +{ + if(!regMainLoop && !in_joy_config_mode) + process_joystick_device_events(); + return !regMainLoop; +} + static int common_gtk_main( class configured_features *my_config) { @@ -3918,6 +3928,7 @@ common_gtk_main( class configured_features *my_config) video->SetFilterParameteri(VF_PARAM_SCANLINE_D, _scanline_filter_d); RedrawScreen(); + g_timeout_add(200, OutOfLoopJoyDeviceCheckTimerFunc, 0); /* Main loop */ gtk_main();