| Summary: | SDL_JoystickInstanceID returns -1 while handling Joystick disconnect event | ||
|---|---|---|---|
| Product: | SDL | Reporter: | JKaniarz <john> |
| Component: | joystick | Assignee: | Sam Lantinga <slouken> |
| Status: | RESOLVED FIXED | QA Contact: | Sam Lantinga <slouken> |
| Severity: | normal | ||
| Priority: | P2 | CC: | philipp.wiesemann |
| Version: | 2.0.3 | ||
| Hardware: | All | ||
| OS: | All | ||
All SDL_PrivateJoystickValid() does is check to see if the joystick is NULL. Can you provide a test case that we can use to see what you mean, and a patch that you've tested that fixes the problem? Thanks! The commit "2015-03-24 Cleanups in the joystick code." removed these lines from SDL_PrivateJoystickValid():
if ( joystick && joystick->closed )
{
valid = 0;
}
I believe they were causing the issue I was having where the pointer was still good but I was unable to use SDL_JoystickInstanceID() during a SDL_JOYDEVICEREMOVED event. I no longer have the code that was breaking so I'm unable to test but I suspect it was inadvertently fixed.
Oh, okay, thanks! |
When handling the SDL_JoyDeviceEvent for SDL_JOYDEVICEREMOVED there is no way to match the "which" field to a SDL_Joystick * because SDL_JostickInstanceID() return -1. This requires maintaining a map of SDL_JoystickID to SDL_Joystick* on the user side. At a glance, modifying SDL_JoystickInstanceID() to not call SDL_PrivateJoystickValid() would allow the ID to perpetuate after the joystick is closed. As IDs are never reused this shouldn't cause any trouble. Here is an implementation of my proposal. SDL_JoystickID SDL_JoystickInstanceID(SDL_Joystick * joystick) { if (!joystick) { return (-1); } return (joystick->instance_id); }