SDL: let through a SetLED command every 5sec to deall with situations where the controller loses power when a computer is suspended CR: SamL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 7d7a12a..8cf47c7 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -416,6 +416,7 @@ SDL_JoystickOpen(int device_index)
joystick->instance_id = instance_id;
joystick->attached = SDL_TRUE;
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
+ joystick->led_expiration = SDL_GetTicks();
if (driver->Open(joystick, device_index) < 0) {
SDL_free(joystick);
@@ -954,6 +955,7 @@ int
SDL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
{
int result;
+ SDL_bool isfreshvalue;
if (!SDL_PrivateJoystickValid(joystick)) {
return -1;
@@ -961,13 +963,17 @@ SDL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
SDL_LockJoysticks();
- if (red == joystick->led_red &&
- green == joystick->led_green &&
- blue == joystick->led_blue) {
+ isfreshvalue = red != joystick->led_red ||
+ green != joystick->led_green ||
+ blue != joystick->led_blue;
+
+ if ( isfreshvalue || SDL_TICKS_PASSED( SDL_GetTicks(), joystick->led_expiration ) ) {
+ result = joystick->driver->SetLED(joystick, red, green, blue);
+ joystick->led_expiration = SDL_GetTicks() + SDL_LED_MIN_REPEAT_MS;
+ }
+ else {
/* Avoid spamming the driver */
result = 0;
- } else {
- result = joystick->driver->SetLED(joystick, red, green, blue);
}
/* Save the LED value regardless of success, so we don't spam the driver */
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index b8af118..749c6ef 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -99,6 +99,7 @@ struct _SDL_Joystick
Uint8 led_red;
Uint8 led_green;
Uint8 led_blue;
+ Uint32 led_expiration;
SDL_bool attached;
SDL_bool is_game_controller;
@@ -189,6 +190,8 @@ typedef struct _SDL_JoystickDriver
/* Windows and Mac OSX has a limit of MAX_DWORD / 1000, Linux kernel has a limit of 0xFFFF */
#define SDL_MAX_RUMBLE_DURATION_MS 0xFFFF
+#define SDL_LED_MIN_REPEAT_MS 5000
+
/* The available joystick drivers */
extern SDL_JoystickDriver SDL_ANDROID_JoystickDriver;
extern SDL_JoystickDriver SDL_BSD_JoystickDriver;