Only call the joystick event callback if the state actually changed.

Only calculate the joystick ID when the event callback is actually called.
This commit is contained in:
Beoran 2020-03-15 20:53:20 +01:00
parent 1ed4c69550
commit ef269c0ae9

View File

@ -387,44 +387,45 @@ void _glfwInputGamepad(_GLFWjoystick* js)
// //
void _glfwInputJoystick(_GLFWjoystick* js, int event) void _glfwInputJoystick(_GLFWjoystick* js, int event)
{ {
if (_glfw.callbacks.joystick) {
const int jid = (int) (js - _glfw.joysticks); const int jid = (int) (js - _glfw.joysticks);
if (_glfw.callbacks.joystick)
_glfw.callbacks.joystick(jid, event); _glfw.callbacks.joystick(jid, event);
} }
}
// Notifies shared code of the new value of a joystick axis // Notifies shared code of the new value of a joystick axis
// //
void _glfwInputJoystickAxis(_GLFWjoystick* js, int axis, float value) void _glfwInputJoystickAxis(_GLFWjoystick* js, int axis, float value)
{ {
if (js->axes[axis] != value) {
const int jid = (int) (js - _glfw.joysticks); const int jid = (int) (js - _glfw.joysticks);
if ((_glfw.callbacks.joystick_axis) && (js->axes[axis] != value))
_glfw.callbacks.joystick_axis(jid, axis, value);
js->axes[axis] = value; js->axes[axis] = value;
if (_glfw.callbacks.joystick_axis)
_glfw.callbacks.joystick_axis(jid, axis, value);
_glfwInputGamepad(js); _glfwInputGamepad(js);
} }
}
// Notifies shared code of the new value of a joystick button // Notifies shared code of the new value of a joystick button
// //
void _glfwInputJoystickButton(_GLFWjoystick* js, int button, char value) void _glfwInputJoystickButton(_GLFWjoystick* js, int button, char value)
{ {
if (js->buttons[button] != value) {
const int jid = (int) (js - _glfw.joysticks); const int jid = (int) (js - _glfw.joysticks);
js->buttons[button] = value; js->buttons[button] = value;
if(_glfw.callbacks.joystick_button) if(_glfw.callbacks.joystick_button)
_glfw.callbacks.joystick_button(jid, button, value); _glfw.callbacks.joystick_button(jid, button, value);
_glfwInputGamepad(js); _glfwInputGamepad(js);
} }
}
// Notifies shared code of the new value of a joystick hat // Notifies shared code of the new value of a joystick hat
// //
void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value) void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value)
{ {
const int jid = (int) (js - _glfw.joysticks);
const int base = js->buttonCount + hat * 4; const int base = js->buttonCount + hat * 4;
js->buttons[base + 0] = (value & 0x01) ? GLFW_PRESS : GLFW_RELEASE; js->buttons[base + 0] = (value & 0x01) ? GLFW_PRESS : GLFW_RELEASE;
@ -432,12 +433,15 @@ void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value)
js->buttons[base + 2] = (value & 0x04) ? GLFW_PRESS : GLFW_RELEASE; js->buttons[base + 2] = (value & 0x04) ? GLFW_PRESS : GLFW_RELEASE;
js->buttons[base + 3] = (value & 0x08) ? GLFW_PRESS : GLFW_RELEASE; js->buttons[base + 3] = (value & 0x08) ? GLFW_PRESS : GLFW_RELEASE;
js->hats[hat] = value; if (js->hats[hat] != value) {
const int jid = (int) (js - _glfw.joysticks);
js->hats[hat] = value;
if(_glfw.callbacks.joystick_hat) if(_glfw.callbacks.joystick_hat)
_glfw.callbacks.joystick_hat(jid, hat, value); _glfw.callbacks.joystick_hat(jid, hat, value);
_glfwInputGamepad(js); _glfwInputGamepad(js);
} }
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////