Fix joystick user pointer NULL during disconnect

The joystick code did not distinguish between the allocation status of
the GLFW joystick object and whether it is connection to an OS level
joystick object.

These are now tracked separately.

Fixes #2092

This is adapted to 3.3-stable from
2c204ab52e and
fd7e737216.
This commit is contained in:
Camilla Löwy
2022-05-05 22:23:12 +02:00
parent 6de6446aca
commit 416f7828ff
7 changed files with 41 additions and 32 deletions

View File

@@ -360,6 +360,11 @@ void _glfwInputJoystick(_GLFWjoystick* js, int event)
{
const int jid = (int) (js - _glfw.joysticks);
if (event == GLFW_CONNECTED)
js->connected = GLFW_TRUE;
else if (event == GLFW_DISCONNECTED)
js->connected = GLFW_FALSE;
if (_glfw.callbacks.joystick)
_glfw.callbacks.joystick(jid, event);
}
@@ -415,7 +420,7 @@ void _glfwInitGamepadMappings(void)
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
{
_GLFWjoystick* js = _glfw.joysticks + jid;
if (js->present)
if (js->connected)
js->mapping = findValidMapping(js);
}
}
@@ -433,7 +438,7 @@ _GLFWjoystick* _glfwAllocJoystick(const char* name,
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
{
if (!_glfw.joysticks[jid].present)
if (!_glfw.joysticks[jid].allocated)
break;
}
@@ -441,7 +446,7 @@ _GLFWjoystick* _glfwAllocJoystick(const char* name,
return NULL;
js = _glfw.joysticks + jid;
js->present = GLFW_TRUE;
js->allocated = GLFW_TRUE;
js->axes = calloc(axisCount, sizeof(float));
js->buttons = calloc(buttonCount + (size_t) hatCount * 4, 1);
js->hats = calloc(hatCount, 1);
@@ -944,7 +949,7 @@ GLFWAPI int glfwJoystickPresent(int jid)
}
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->connected)
return GLFW_FALSE;
return _glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE);
@@ -969,7 +974,7 @@ GLFWAPI const float* glfwGetJoystickAxes(int jid, int* count)
}
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->connected)
return NULL;
if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_AXES))
@@ -998,7 +1003,7 @@ GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count)
}
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->connected)
return NULL;
if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_BUTTONS))
@@ -1031,7 +1036,7 @@ GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count)
}
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->connected)
return NULL;
if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_BUTTONS))
@@ -1057,7 +1062,7 @@ GLFWAPI const char* glfwGetJoystickName(int jid)
}
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->connected)
return NULL;
if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
@@ -1082,7 +1087,7 @@ GLFWAPI const char* glfwGetJoystickGUID(int jid)
}
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->connected)
return NULL;
if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
@@ -1101,7 +1106,7 @@ GLFWAPI void glfwSetJoystickUserPointer(int jid, void* pointer)
_GLFW_REQUIRE_INIT();
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->allocated)
return;
js->userPointer = pointer;
@@ -1117,7 +1122,7 @@ GLFWAPI void* glfwGetJoystickUserPointer(int jid)
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->allocated)
return NULL;
return js->userPointer;
@@ -1183,7 +1188,7 @@ GLFWAPI int glfwUpdateGamepadMappings(const char* string)
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
{
_GLFWjoystick* js = _glfw.joysticks + jid;
if (js->present)
if (js->connected)
js->mapping = findValidMapping(js);
}
@@ -1206,7 +1211,7 @@ GLFWAPI int glfwJoystickIsGamepad(int jid)
}
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->connected)
return GLFW_FALSE;
if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
@@ -1231,7 +1236,7 @@ GLFWAPI const char* glfwGetGamepadName(int jid)
}
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->connected)
return NULL;
if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
@@ -1263,7 +1268,7 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state)
}
js = _glfw.joysticks + jid;
if (!js->present)
if (!js->connected)
return GLFW_FALSE;
if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_ALL))