Don't send the initial joystick axis event if the application is in the background

main
Sam Lantinga 2021-11-09 12:30:37 -08:00
parent d31f90d9e1
commit 7681929cb4
2 changed files with 6 additions and 3 deletions

View File

@ -1344,7 +1344,7 @@ SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
info->value = value; info->value = value;
info->zero = value; info->zero = value;
info->has_initial_value = SDL_TRUE; info->has_initial_value = SDL_TRUE;
} else if (value == info->value) { } else if (value == info->value && !info->sending_initial_value) {
return 0; return 0;
} else { } else {
info->has_second_value = SDL_TRUE; info->has_second_value = SDL_TRUE;
@ -1356,15 +1356,17 @@ SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
return 0; return 0;
} }
info->sent_initial_value = SDL_TRUE; info->sent_initial_value = SDL_TRUE;
info->value = ~value; /* Just so we pass the check above */ info->sending_initial_value = SDL_TRUE;
SDL_PrivateJoystickAxis(joystick, axis, info->initial_value); SDL_PrivateJoystickAxis(joystick, axis, info->initial_value);
info->sending_initial_value = SDL_FALSE;
} }
/* We ignore events if we don't have keyboard focus, except for centering /* We ignore events if we don't have keyboard focus, except for centering
* events. * events.
*/ */
if (SDL_PrivateJoystickShouldIgnoreEvent()) { if (SDL_PrivateJoystickShouldIgnoreEvent()) {
if ((value > info->zero && value >= info->value) || if (info->sending_initial_value ||
(value > info->zero && value >= info->value) ||
(value < info->zero && value <= info->value)) { (value < info->zero && value <= info->value)) {
return 0; return 0;
} }

View File

@ -36,6 +36,7 @@ typedef struct _SDL_JoystickAxisInfo
SDL_bool has_initial_value; /* Whether we've seen a value on the axis yet */ SDL_bool has_initial_value; /* Whether we've seen a value on the axis yet */
SDL_bool has_second_value; /* Whether we've seen a second value on the axis yet */ SDL_bool has_second_value; /* Whether we've seen a second value on the axis yet */
SDL_bool sent_initial_value; /* Whether we've sent the initial axis value */ SDL_bool sent_initial_value; /* Whether we've sent the initial axis value */
SDL_bool sending_initial_value; /* Whether we are sending the initial axis value */
} SDL_JoystickAxisInfo; } SDL_JoystickAxisInfo;
typedef struct _SDL_JoystickTouchpadFingerInfo typedef struct _SDL_JoystickTouchpadFingerInfo