Added the events SDL_EVENT_JOYSTICK_UPDATE_COMPLETE and SDL_EVENT_GAMEPAD_UPDATE_COMPLETE

This allows the application to tell when a joystick polling cycle is complete and can process state changes as a single atomic update. It is disabled by default, at least for now.
This commit is contained in:
Sam Lantinga
2023-06-21 13:59:53 -07:00
parent 808d83dd67
commit 4c9fb3e169
5 changed files with 67 additions and 2 deletions

View File

@@ -1753,6 +1753,7 @@ int SDL_SendJoystickAxis(Uint64 timestamp, SDL_Joystick *joystick, Uint8 axis, S
/* Update internal joystick state */
info->value = value;
joystick->update_complete = timestamp;
/* Post the event, if desired */
posted = 0;
@@ -1795,6 +1796,7 @@ int SDL_SendJoystickHat(Uint64 timestamp, SDL_Joystick *joystick, Uint8 hat, Uin
/* Update internal joystick state */
joystick->hats[hat] = value;
joystick->update_complete = timestamp;
/* Post the event, if desired */
posted = 0;
@@ -1853,6 +1855,7 @@ int SDL_SendJoystickButton(Uint64 timestamp, SDL_Joystick *joystick, Uint8 butto
/* Update internal joystick state */
joystick->buttons[button] = state;
joystick->update_complete = timestamp;
/* Post the event, if desired */
posted = 0;
@@ -1913,6 +1916,21 @@ void SDL_UpdateJoysticks(void)
}
}
if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE)) {
for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
if (joystick->update_complete) {
SDL_Event event;
event.type = SDL_EVENT_JOYSTICK_UPDATE_COMPLETE;
event.common.timestamp = joystick->update_complete;
event.gdevice.which = joystick->instance_id;
SDL_PushEvent(&event);
joystick->update_complete = 0;
}
}
}
/* this needs to happen AFTER walking the joystick list above, so that any
dangling hardware data from removed devices can be free'd
*/
@@ -3178,6 +3196,7 @@ int SDL_SendJoystickTouchpad(Uint64 timestamp, SDL_Joystick *joystick, int touch
finger_info->x = x;
finger_info->y = y;
finger_info->pressure = pressure;
joystick->update_complete = timestamp;
/* Post the event, if desired */
posted = 0;
@@ -3219,6 +3238,7 @@ int SDL_SendJoystickSensor(Uint64 timestamp, SDL_Joystick *joystick, SDL_SensorT
/* Update internal sensor state */
SDL_memcpy(sensor->data, data, num_values * sizeof(*data));
joystick->update_complete = timestamp;
/* Post the event, if desired */
#ifndef SDL_EVENTS_DISABLED