Cleanups in the joystick code.

Removed some redundant state and other confusions.

Fixes Bugzilla #2738.
This commit is contained in:
Ryan C. Gordon 2015-03-24 13:52:01 -04:00
parent 4a071b311b
commit 162ef5eae9
15 changed files with 24 additions and 59 deletions

View File

@ -206,10 +206,6 @@ SDL_PrivateJoystickValid(SDL_Joystick * joystick)
valid = 1; valid = 1;
} }
if (joystick && joystick->closed) {
valid = 0;
}
return valid; return valid;
} }
@ -412,6 +408,7 @@ SDL_JoystickClose(SDL_Joystick * joystick)
} }
SDL_SYS_JoystickClose(joystick); SDL_SYS_JoystickClose(joystick);
joystick->hwdata = NULL;
joysticklist = SDL_joysticks; joysticklist = SDL_joysticks;
joysticklistprev = NULL; joysticklistprev = NULL;
@ -668,7 +665,7 @@ SDL_JoystickUpdate(void)
SDL_SYS_JoystickUpdate(joystick); SDL_SYS_JoystickUpdate(joystick);
if (joystick->closed && joystick->uncentered) { if (joystick->force_recentering) {
int i; int i;
/* Tell the app that everything is centered/unpressed... */ /* Tell the app that everything is centered/unpressed... */
@ -681,7 +678,7 @@ SDL_JoystickUpdate(void)
for (i = 0; i < joystick->nhats; i++) for (i = 0; i < joystick->nhats; i++)
SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED); SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
joystick->uncentered = SDL_FALSE; joystick->force_recentering = SDL_FALSE;
} }
SDL_updating_joystick = NULL; SDL_updating_joystick = NULL;

View File

@ -53,8 +53,7 @@ struct _SDL_Joystick
int ref_count; /* Reference count for multiple opens */ int ref_count; /* Reference count for multiple opens */
SDL_bool closed; /* SDL_TRUE if this device is no longer valid */ SDL_bool force_recentering; /* SDL_TRUE if this device needs to have its state reset to 0 */
SDL_bool uncentered; /* SDL_TRUE if this device needs to have its state reset to 0 */
struct _SDL_Joystick *next; /* pointer to next joystick we have allocated */ struct _SDL_Joystick *next; /* pointer to next joystick we have allocated */
}; };

View File

@ -498,7 +498,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
/* Function to determine is this joystick is attached to the system right now */ /* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick) SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{ {
return !joystick->closed && (joystick->hwdata != NULL); return joystick->hwdata != NULL;
} }
void void
@ -529,11 +529,6 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void void
SDL_SYS_JoystickClose(SDL_Joystick * joystick) SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{ {
if (joystick->hwdata) {
((SDL_joylist_item*)joystick->hwdata)->joystick = NULL;
joystick->hwdata = NULL;
}
joystick->closed = 1;
} }
/* Function to perform any system-specific joystick related cleanup */ /* Function to perform any system-specific joystick related cleanup */

View File

@ -558,8 +558,6 @@ SDL_SYS_JoystickClose(SDL_Joystick * joy)
close(joy->hwdata->fd); close(joy->hwdata->fd);
SDL_free(joy->hwdata->path); SDL_free(joy->hwdata->path);
SDL_free(joy->hwdata); SDL_free(joy->hwdata);
return;
} }
void void

View File

@ -138,7 +138,7 @@ static void
JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender) JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
{ {
recDevice *device = (recDevice *) ctx; recDevice *device = (recDevice *) ctx;
device->removed = 1; device->removed = SDL_TRUE;
device->deviceRef = NULL; // deviceRef was invalidated due to the remove device->deviceRef = NULL; // deviceRef was invalidated due to the remove
#if SDL_HAPTIC_IOKIT #if SDL_HAPTIC_IOKIT
MacHaptic_MaybeRemoveDevice(device->ffservice); MacHaptic_MaybeRemoveDevice(device->ffservice);
@ -677,16 +677,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
SDL_bool SDL_bool
SDL_SYS_JoystickAttached(SDL_Joystick * joystick) SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
{ {
recDevice *device = gpDeviceList; return joystick->hwdata != NULL;
while (device) {
if (joystick->instance_id == device->instance_id) {
return SDL_TRUE;
}
device = device->pNext;
}
return SDL_FALSE;
} }
/* Function to update the state of a joystick - called as a device poll. /* Function to update the state of a joystick - called as a device poll.
@ -707,9 +698,10 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
} }
if (device->removed) { /* device was unplugged; ignore it. */ if (device->removed) { /* device was unplugged; ignore it. */
joystick->closed = 1; if (joystick->hwdata) {
joystick->uncentered = 1; joystick->force_recentering = SDL_TRUE;
joystick->hwdata = NULL; joystick->hwdata = NULL;
}
return; return;
} }
@ -797,7 +789,6 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void void
SDL_SYS_JoystickClose(SDL_Joystick * joystick) SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{ {
joystick->closed = 1;
} }
/* Function to perform any system-specific joystick related cleanup */ /* Function to perform any system-specific joystick related cleanup */

View File

@ -58,8 +58,7 @@ struct joystick_hwdata
recElement *firstButton; recElement *firstButton;
recElement *firstHat; recElement *firstHat;
int removed; SDL_bool removed;
int uncentered;
int instance_id; int instance_id;
SDL_JoystickGUID guid; SDL_JoystickGUID guid;

View File

@ -34,7 +34,7 @@
int int
SDL_SYS_JoystickInit(void) SDL_SYS_JoystickInit(void)
{ {
return (0); return 0;
} }
int SDL_SYS_NumJoysticks() int SDL_SYS_NumJoysticks()
@ -85,21 +85,18 @@ SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
void void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{ {
return;
} }
/* Function to close a joystick after use */ /* Function to close a joystick after use */
void void
SDL_SYS_JoystickClose(SDL_Joystick * joystick) SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{ {
return;
} }
/* Function to perform any system-specific joystick related cleanup */ /* Function to perform any system-specific joystick related cleanup */
void void
SDL_SYS_JoystickQuit(void) SDL_SYS_JoystickQuit(void)
{ {
return;
} }
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index ) SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )

View File

@ -326,7 +326,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
/* Function to determine is this joystick is attached to the system right now */ /* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick) SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{ {
return !joystick->closed && (joystick->hwdata != NULL); return joystick->hwdata != NULL;
} }
/* Function to update the state of a joystick - called as a device poll. /* Function to update the state of a joystick - called as a device poll.
@ -378,11 +378,6 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void void
SDL_SYS_JoystickClose(SDL_Joystick * joystick) SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{ {
if (joystick->hwdata) {
((SDL_joylist_item*)joystick->hwdata)->joystick = NULL;
joystick->hwdata = NULL;
}
joystick->closed = 1;
} }
/* Function to perform any system-specific joystick related cleanup */ /* Function to perform any system-specific joystick related cleanup */

View File

@ -228,7 +228,6 @@ extern "C"
SDL_free(joystick->hwdata->new_hats); SDL_free(joystick->hwdata->new_hats);
SDL_free(joystick->hwdata->new_axes); SDL_free(joystick->hwdata->new_axes);
SDL_free(joystick->hwdata); SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
} }
} }

View File

@ -153,7 +153,6 @@ void
SDL_SYS_JoystickClose(SDL_Joystick * joystick) SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{ {
[motionManager stopAccelerometerUpdates]; [motionManager stopAccelerometerUpdates];
joystick->closed = 1;
} }
/* Function to perform any system-specific joystick related cleanup */ /* Function to perform any system-specific joystick related cleanup */

View File

@ -624,7 +624,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
/* Function to determine is this joystick is attached to the system right now */ /* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick) SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{ {
return !joystick->closed && (joystick->hwdata->item != NULL); return joystick->hwdata->item != NULL;
} }
static SDL_INLINE void static SDL_INLINE void
@ -841,9 +841,7 @@ SDL_SYS_JoystickClose(SDL_Joystick * joystick)
SDL_free(joystick->hwdata->balls); SDL_free(joystick->hwdata->balls);
SDL_free(joystick->hwdata->fname); SDL_free(joystick->hwdata->fname);
SDL_free(joystick->hwdata); SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
} }
joystick->closed = 1;
} }
/* Function to perform any system-specific joystick related cleanup */ /* Function to perform any system-specific joystick related cleanup */

View File

@ -233,7 +233,6 @@ void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
/* Function to close a joystick after use */ /* Function to close a joystick after use */
void SDL_SYS_JoystickClose(SDL_Joystick *joystick) void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
{ {
/* Do nothing. */
} }
/* Function to perform any system-specific joystick related cleanup */ /* Function to perform any system-specific joystick related cleanup */

View File

@ -383,9 +383,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void void
SDL_SYS_JoystickClose(SDL_Joystick * joystick) SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{ {
/* free system specific hardware data */
SDL_free(joystick->hwdata); SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
} }
/* Function to perform any system-specific joystick related cleanup */ /* Function to perform any system-specific joystick related cleanup */

View File

@ -460,7 +460,6 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
/* allocate memory for system specific hardware data */ /* allocate memory for system specific hardware data */
joystick->instance_id = joystickdevice->nInstanceID; joystick->instance_id = joystickdevice->nInstanceID;
joystick->closed = SDL_FALSE;
joystick->hwdata = joystick->hwdata =
(struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata)); (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
if (joystick->hwdata == NULL) { if (joystick->hwdata == NULL) {
@ -480,13 +479,13 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
SDL_bool SDL_bool
SDL_SYS_JoystickAttached(SDL_Joystick * joystick) SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
{ {
return !joystick->closed && !joystick->hwdata->removed; return joystick->hwdata && !joystick->hwdata->removed;
} }
void void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{ {
if (joystick->closed || !joystick->hwdata) { if (!joystick->hwdata || joystick->hwdata->removed) {
return; return;
} }
@ -497,8 +496,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
} }
if (joystick->hwdata->removed) { if (joystick->hwdata->removed) {
joystick->closed = SDL_TRUE; joystick->force_recentering = SDL_TRUE;
joystick->uncentered = SDL_TRUE;
} }
} }
@ -512,10 +510,7 @@ SDL_SYS_JoystickClose(SDL_Joystick * joystick)
SDL_DINPUT_JoystickClose(joystick); SDL_DINPUT_JoystickClose(joystick);
} }
/* free system specific hardware data */
SDL_free(joystick->hwdata); SDL_free(joystick->hwdata);
joystick->closed = SDL_TRUE;
} }
/* Function to perform any system-specific joystick related cleanup */ /* Function to perform any system-specific joystick related cleanup */

View File

@ -56,6 +56,12 @@ loop(void *arg)
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_JOYDEVICEREMOVED:
SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
SDL_Log("Our instance ID is %d\n", (int) SDL_JoystickInstanceID(joystick));
break;
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
SDL_Log("Joystick %d axis %d value: %d\n", SDL_Log("Joystick %d axis %d value: %d\n",
event.jaxis.which, event.jaxis.which,