Alternative Games (game porting company) has found a rather interesting
bug in the haptic code. I WOULD like to see it fixed for the 2.0.2
release, but the fix may be somewhat complicated. So I would like
suggestions as to how I should fix it…
Here is the bug. (which can be reproduced with the testhotplug.c I
- Plug in a rumble-supported controller
- run the testhotplug test app in a terminal (terminal only app)
- It detects the controller and the haptic interface…
- Moving the axis,or pressing buttons triggers rumble (I had a little
- disconnect the controller & the app sees the controller went away.
- reconnect the controller & the app sees the controller again and
re-establishes the haptics
Now here’s one of the broken cases. press Ctrl-C in the terminal to
exit the test app.
- disconnect the controller
- start the testhotplug app
- note that it detects 0 controllers and 0 haptics.
- plug in your controller
- it detects the controller and sees that it supports haptic
(JoystickIsHaptic) but fails to open it with an error that 0 haptic
devices are available.
So, in the platform generic haptic code there is an array SDL_haptics
and an in SDL_numhaptics that get sized and initialized on haptic
subsystem init. So in the second case it gets initialized to 0 haptic
devices. Later when a joystick is hotplugged that array is resized, nor
the # of devices updated thus the haptic open fails because there is no
place to store the data.
This is a fully cross-platform bug too!
This would also happen if you had one controller plugged and later
plugged a second controller… only one controller would be able to open
a haptic interface.
So, ideas I’ve had on fixing it.
- have the haptic generic bits resize the array / update the #. ( BAD
as it could cause threading issues)
- pre-allocate a number of free slots. ( would limit the # of
hotpluggable devices… Mixed feelings on this.)
- convert it to a linked list (less threading issues and it’s what is
done in Joystick and GameController subsystems)… This just requires
I tried # 2 by doing a dirty fix of simply adding + 4 to the result of
SDL_SYS_HapticInit… this failed in horrible ways as the haptic system
tried opening non-existent devices… And #3 sounds the best approach to
solving the issue but requires a slight overhaul of the innards of the