Fix SDL Gamepad Hotplugging

Reporter: RobertBColton  |  Status: open  |  Last Modified: January 09, 2021, 04:05:26 PM

This fixes #2091 in support of user. The problem is basically that the cdevice->which member is the device index (what GM uses) for device add events, but is actually the joystick instance id for button/axis/remove events. So clearly all we needed was a map of the joystick instance ids to the device indexes.

To clarify, you plug controller in, it has device index 0, and joystick instance id 0. You unplug it and plug it back in again, it still has device index 0, but then it has joystick instance id 1, and so on. It keeps getting incremented by one, because it's basically trying to track the player index, which we don't need because GM uses device index. Little test here you can do to make sure input still works after hot plugging.

draw_text(0,20,string(gamepad_button_check(0, gp_padl)));

I really hated having to program it this way, and was stuck thinking of alternatives. The only other way I see to do this without the map is to disable the events and manually poll the gamepads/joysticks in our main loop like I do for XInput extension. I would probably rather do it that way anyway. We could generalize the gamepad structure from my XInput extension and the SDL system so they are both using the same structure, and then generalize all of the gamepad joystick functions for every platform.

@time-killer-games you want to give it a test?

Codecov Report

Merging #2175 (5ebd8c6) into master (e273f89) will decrease coverage by 0.01%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2175      +/-   ##
- Coverage   32.89%   32.88%   -0.02%     
  Files         197      197              
  Lines       19062    19068       +6     
  Hits         6270     6270              
- Misses      12792    12798       +6     
Impacted Files Coverage Δ
ENIGMAsystem/SHELL/Platforms/SDL/Event.cpp 17.61% <0.00%> (-0.63%) ⬇️
ENIGMAsystem/SHELL/Platforms/SDL/Gamepad.cpp 5.64% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e273f89...5ebd8c6. Read the comment docs.

Please sign in to post comments, or you can view this issue on GitHub.