Fix space mouse detection with SDL_hidapi - #7665
Conversation
notimaginative
left a comment
There was a problem hiding this comment.
SDL_hid_enumerate() will call SDL_hid_init() if it has not already been initialized. The code previously did that, as addressed by the stale SDL_hid_exit() call mentioned in the PR. It was removed since it shouldn't be necessary.
However if SDL_hid_init() really needs to be called then we really need to do it before SDL is initialized (i.e., put it in os_init()) and a companion SDL_hid_exit() should be added as well (os_deinit()).
Since scp-fs2open#7646 switched from the bundled hidapi to SDL_hidapi, no space mouse is found. SDL_hid_enumerate() nominally auto-inits HIDAPI, but in practice it only reliably reports an already-connected device when SDL_hid_init() has been called up front. Call SDL_hid_init() once in os_init() (before SDL_Init), balanced by SDL_hid_exit() in os_deinit(). io::spacemouse (qtFRED's only consumer) then enumerates the device correctly.
8997e16 to
b7b0915
Compare
Without SDL_hid_init() spacemouse did not work in QtFRED. So I guess this is needed. I've changed it as requested and tested. This version does still fix the issue. |
Seems okay to me, but I'll leave the authoritative approve to taylor.
Since #7646 switched from the bundled hidapi to SDL_hidapi, no space mouse is ever found: the standalone hidapi's hid_enumerate() implicitly initialized the library, but SDL's SDL_hid_enumerate() does not reliably do so, so it returns an empty device list and getSharedSpaceMouse() caches nullptr for the session.
Call SDL_hid_init() before enumerating, matching SDL's documented usage.