Joystick configuration changes and threading


#1

Are the callbacks registered with glfwSetJoystickCallback guaranteed to be called in the main thread? I would expect to receive them when calling glfwPollEvents, but I am getting some weird behavior…


#2

Note that GLFW requires that you perform window creation and glfwPollEvents from the main thread for compatibility reasons - see this answer to a previous question about GLFW.

If you’re following that advice, then all callbacks should occur on the main thread.


#3

Thanks for that info. I am following advice and the callback are indeed all coming from the main thread. Could it be that glfwGetJoystickAxes is triggering a callback?
My joystick state collection gets invalidated while looping over it (if the joystick gets unplugged) and there is nothing by glfwGetJoystickAxes and glfwGetJoystickButtons in the loop.


#4

glfwGetJoystickAxescan can trigger a callback if the joystick was removed. This callback will occur on the same thread as glfwGetJoystickAxeswas called, which should be the main thread as glfwGetJoystickAxes should only be called from that thread.

If the joystick you’re polling is disconnected you should also get NULL back from glfwGetJoystickAxes.


#5

I have just stumbled across this thread while trying to solve similar issue. I’m keeping the list of “valid” joysticks, removing or adding to the list through the callbacks. However, sometimes my list gets invalidated as if the callback has been launched in parallel to the code.