[Android] Fixes #2264, handle joystick open/closed state properly
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 71 72 73 74 75 76
diff --git a/src/joystick/android/SDL_sysjoystick.c b/src/joystick/android/SDL_sysjoystick.c
index 3289a28..34d4584 100644
--- a/src/joystick/android/SDL_sysjoystick.c
+++ b/src/joystick/android/SDL_sysjoystick.c
@@ -161,20 +161,18 @@ SDL_SYS_JoystickInit(void)
if (SYS_accelAsJoy) {
SYS_numjoysticks++;
}
- SYS_Joysticks = (SDL_Joystick **)SDL_malloc(SYS_numjoysticks*sizeof(SDL_Joystick *));
+ SYS_Joysticks = (SDL_Joystick **)SDL_calloc(1, SYS_numjoysticks*sizeof(SDL_Joystick *));
if (SYS_Joysticks == NULL)
{
return SDL_OutOfMemory();
}
- SYS_JoystickNames = (char **)SDL_malloc(SYS_numjoysticks*sizeof(char *));
+ SYS_JoystickNames = (char **)SDL_calloc(1, SYS_numjoysticks*sizeof(char *));
if (SYS_JoystickNames == NULL)
{
SDL_free(SYS_Joysticks);
SYS_Joysticks = NULL;
return SDL_OutOfMemory();
}
- SDL_memset(SYS_JoystickNames, 0, (SYS_numjoysticks*sizeof(char *)));
- SDL_memset(SYS_Joysticks, 0, (SYS_numjoysticks*sizeof(SDL_Joystick *)));
for (i = 0; i < SYS_numjoysticks; i++)
{
@@ -274,6 +272,15 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
+ int device_index;
+
+ for (device_index = 0; device_index < SYS_numjoysticks; device_index++) {
+ if ( SYS_Joysticks[device_index] == joystick ) {
+ SYS_Joysticks[device_index] = NULL;
+ }
+ }
+
+ joystick->closed = 1;
}
/* Function to perform any system-specific joystick related cleanup */
@@ -311,7 +318,9 @@ Android_OnPadDown(int padId, int keycode)
{
int button = keycode_to_SDL(keycode);
if (button >= 0) {
- SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
+ if (SYS_Joysticks[padId]) {
+ SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
+ }
return 0;
}
@@ -323,7 +332,9 @@ Android_OnPadUp(int padId, int keycode)
{
int button = keycode_to_SDL(keycode);
if (button >= 0) {
- SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
+ if (SYS_Joysticks[padId]) {
+ SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
+ }
return 0;
}
@@ -335,7 +346,9 @@ Android_OnJoy(int joyId, int axis, float value)
{
/* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
/* TODO: Are the reported values right? */
- SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
+ if (SYS_Joysticks[joyId]) {
+ SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
+ }
return 0;
}