Added locking for Android joystick events
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 65 66 67 68 69 70
diff --git a/src/joystick/android/SDL_sysjoystick.c b/src/joystick/android/SDL_sysjoystick.c
index 6af06b6..558d0d7 100644
--- a/src/joystick/android/SDL_sysjoystick.c
+++ b/src/joystick/android/SDL_sysjoystick.c
@@ -208,12 +208,14 @@ Android_OnPadDown(int device_id, int keycode)
SDL_joylist_item *item;
int button = keycode_to_SDL(keycode);
if (button >= 0) {
+ SDL_LockJoysticks();
item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickButton(item->joystick, button, SDL_PRESSED);
} else {
SDL_SendKeyboardKey(SDL_PRESSED, button_to_scancode(button));
}
+ SDL_UnlockJoysticks();
return 0;
}
@@ -226,12 +228,14 @@ Android_OnPadUp(int device_id, int keycode)
SDL_joylist_item *item;
int button = keycode_to_SDL(keycode);
if (button >= 0) {
+ SDL_LockJoysticks();
item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
} else {
SDL_SendKeyboardKey(SDL_RELEASED, button_to_scancode(button));
}
+ SDL_UnlockJoysticks();
return 0;
}
@@ -242,10 +246,14 @@ int
Android_OnJoy(int device_id, int axis, float value)
{
/* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
- SDL_joylist_item *item = JoystickByDeviceId(device_id);
+ SDL_joylist_item *item;
+
+ SDL_LockJoysticks();
+ item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value));
}
+ SDL_UnlockJoysticks();
return 0;
}
@@ -259,7 +267,10 @@ Android_OnHat(int device_id, int hat_id, int x, int y)
const int DPAD_RIGHT_MASK = (1 << SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
if (x >= -1 && x <= 1 && y >= -1 && y <= 1) {
- SDL_joylist_item *item = JoystickByDeviceId(device_id);
+ SDL_joylist_item *item;
+
+ SDL_LockJoysticks();
+ item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
int dpad_state = 0;
int dpad_delta;
@@ -291,6 +302,7 @@ Android_OnHat(int device_id, int hat_id, int x, int y)
item->dpad_state = dpad_state;
}
}
+ SDL_UnlockJoysticks();
return 0;
}