Skip to content

Commit

Permalink
bugfix: store joystick GUID, use it to check before re-importing joys…
Browse files Browse the repository at this point in the history
…ticks (#1355)
  • Loading branch information
midwan committed Jun 19, 2024
1 parent 89df389 commit 8bf3de7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
19 changes: 12 additions & 7 deletions src/osdep/amiberry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,13 +1344,17 @@ void handle_clipboard_update_event()
}
}

void handle_joy_device_event()
void handle_joy_device_event(const int which, const bool removed)
{
write_log("SDL2 Controller/Joystick added or removed, re-running import joysticks...\n");
if (inputdevice_devicechange(&currprefs))
const didata* existing_did = &di_joystick[which];
if (existing_did->guid == "" || removed)
{
import_joysticks();
joystick_refresh_needed = true;
write_log("SDL2 Controller/Joystick added or removed, re-running import joysticks...\n");
if (inputdevice_devicechange(&currprefs))
{
import_joysticks();
joystick_refresh_needed = true;
}
}
}

Expand Down Expand Up @@ -1625,9 +1629,10 @@ void process_event(const SDL_Event& event)
break;

case SDL_JOYDEVICEADDED:
handle_joy_device_event(event.jdevice.which, false);
break;
case SDL_JOYDEVICEREMOVED:
// Disable this for now, as it forces a re-import of joysticks which will reset the controller mappings
//handle_joy_device_event();
handle_joy_device_event(0, true);
break;

case SDL_CONTROLLERBUTTONDOWN:
Expand Down
2 changes: 2 additions & 0 deletions src/osdep/amiberry_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ void open_as_game_controller(struct didata* did, const int i)
did->joystick = SDL_GameControllerGetJoystick(did->controller);
did->joystick_id = SDL_JoystickInstanceID(did->joystick);
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(did->joystick), guid_str, 33);
did->guid = std::string(guid_str);

if (SDL_GameControllerNameForIndex(i) != nullptr)
did->controller_name.assign(SDL_GameControllerNameForIndex(i));
Expand Down Expand Up @@ -865,6 +866,7 @@ void open_as_joystick(struct didata* did, const int i)
did->is_controller = false;
did->joystick_id = SDL_JoystickInstanceID(did->joystick);
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(did->joystick), guid_str, 33);
did->guid = std::string(guid_str);

if (SDL_JoystickNameForIndex(i) != nullptr)
did->joystick_name.assign(SDL_JoystickNameForIndex(i));
Expand Down
1 change: 1 addition & 0 deletions src/osdep/amiberry_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct didata {
std::string controller_name{};
std::string joystick_name{};

std::string guid{};
bool is_controller{};
SDL_GameController* controller{};
SDL_Joystick* joystick{};
Expand Down
15 changes: 12 additions & 3 deletions src/osdep/gui/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ void check_input()

auto got_event = 0;
didata* did = &di_joystick[0];
didata* existing_did = nullptr;

while (SDL_PollEvent(&gui_event))
{
Expand All @@ -437,10 +438,18 @@ void check_input()
break;

case SDL_JOYDEVICEADDED:
//case SDL_CONTROLLERDEVICEADDED:
// Check if we need to re-import joysticks
existing_did = &di_joystick[gui_event.jdevice.which];
if (existing_did->guid == "")
{
write_log("GUI: SDL2 Controller/Joystick added, re-running import joysticks...\n");
import_joysticks();
joystick_refresh_needed = true;
RefreshPanelInput();
}
return;
case SDL_JOYDEVICEREMOVED:
//case SDL_CONTROLLERDEVICEREMOVED:
write_log("GUI: SDL2 Controller/Joystick added or removed, re-running import joysticks...\n");
write_log("GUI: SDL2 Controller/Joystick removed, re-running import joysticks...\n");
if (inputdevice_devicechange(&currprefs))
{
import_joysticks();
Expand Down

0 comments on commit 8bf3de7

Please sign in to comment.