Skip to content

Commit

Permalink
Cleanups in the joystick code.
Browse files Browse the repository at this point in the history
Removed some redundant state and other confusions.

Fixes Bugzilla #2738.
  • Loading branch information
icculus committed Mar 24, 2015
1 parent 4a071b3 commit 162ef5e
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 59 deletions.
9 changes: 3 additions & 6 deletions src/joystick/SDL_joystick.c
Expand Up @@ -206,10 +206,6 @@ SDL_PrivateJoystickValid(SDL_Joystick * joystick)
valid = 1;
}

if (joystick && joystick->closed) {
valid = 0;
}

return valid;
}

Expand Down Expand Up @@ -412,6 +408,7 @@ SDL_JoystickClose(SDL_Joystick * joystick)
}

SDL_SYS_JoystickClose(joystick);
joystick->hwdata = NULL;

joysticklist = SDL_joysticks;
joysticklistprev = NULL;
Expand Down Expand Up @@ -668,7 +665,7 @@ SDL_JoystickUpdate(void)

SDL_SYS_JoystickUpdate(joystick);

if (joystick->closed && joystick->uncentered) {
if (joystick->force_recentering) {
int i;

/* Tell the app that everything is centered/unpressed... */
Expand All @@ -681,7 +678,7 @@ SDL_JoystickUpdate(void)
for (i = 0; i < joystick->nhats; i++)
SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);

joystick->uncentered = SDL_FALSE;
joystick->force_recentering = SDL_FALSE;
}

SDL_updating_joystick = NULL;
Expand Down
3 changes: 1 addition & 2 deletions src/joystick/SDL_sysjoystick.h
Expand Up @@ -53,8 +53,7 @@ struct _SDL_Joystick

int ref_count; /* Reference count for multiple opens */

SDL_bool closed; /* SDL_TRUE if this device is no longer valid */
SDL_bool uncentered; /* SDL_TRUE if this device needs to have its state reset to 0 */
SDL_bool force_recentering; /* SDL_TRUE if this device needs to have its state reset to 0 */
struct _SDL_Joystick *next; /* pointer to next joystick we have allocated */
};

Expand Down
7 changes: 1 addition & 6 deletions src/joystick/android/SDL_sysjoystick.c
Expand Up @@ -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 */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
return !joystick->closed && (joystick->hwdata != NULL);
return joystick->hwdata != NULL;
}

void
Expand Down Expand Up @@ -529,11 +529,6 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void
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 */
Expand Down
2 changes: 0 additions & 2 deletions src/joystick/bsd/SDL_sysjoystick.c
Expand Up @@ -558,8 +558,6 @@ SDL_SYS_JoystickClose(SDL_Joystick * joy)
close(joy->hwdata->fd);
SDL_free(joy->hwdata->path);
SDL_free(joy->hwdata);

return;
}

void
Expand Down
21 changes: 6 additions & 15 deletions src/joystick/darwin/SDL_sysjoystick.c
Expand Up @@ -138,7 +138,7 @@ static void
JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
{
recDevice *device = (recDevice *) ctx;
device->removed = 1;
device->removed = SDL_TRUE;
device->deviceRef = NULL; // deviceRef was invalidated due to the remove
#if SDL_HAPTIC_IOKIT
MacHaptic_MaybeRemoveDevice(device->ffservice);
Expand Down Expand Up @@ -677,16 +677,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
SDL_bool
SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
{
recDevice *device = gpDeviceList;

while (device) {
if (joystick->instance_id == device->instance_id) {
return SDL_TRUE;
}
device = device->pNext;
}

return SDL_FALSE;
return joystick->hwdata != NULL;
}

/* Function to update the state of a joystick - called as a device poll.
Expand All @@ -707,9 +698,10 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
}

if (device->removed) { /* device was unplugged; ignore it. */
joystick->closed = 1;
joystick->uncentered = 1;
joystick->hwdata = NULL;
if (joystick->hwdata) {
joystick->force_recentering = SDL_TRUE;
joystick->hwdata = NULL;
}
return;
}

Expand Down Expand Up @@ -797,7 +789,6 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
joystick->closed = 1;
}

/* Function to perform any system-specific joystick related cleanup */
Expand Down
3 changes: 1 addition & 2 deletions src/joystick/darwin/SDL_sysjoystick_c.h
Expand Up @@ -58,8 +58,7 @@ struct joystick_hwdata
recElement *firstButton;
recElement *firstHat;

int removed;
int uncentered;
SDL_bool removed;

int instance_id;
SDL_JoystickGUID guid;
Expand Down
5 changes: 1 addition & 4 deletions src/joystick/dummy/SDL_sysjoystick.c
Expand Up @@ -34,7 +34,7 @@
int
SDL_SYS_JoystickInit(void)
{
return (0);
return 0;
}

int SDL_SYS_NumJoysticks()
Expand Down Expand Up @@ -85,21 +85,18 @@ SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
return;
}

/* Function to close a joystick after use */
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
return;
}

/* Function to perform any system-specific joystick related cleanup */
void
SDL_SYS_JoystickQuit(void)
{
return;
}

SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
Expand Down
7 changes: 1 addition & 6 deletions src/joystick/emscripten/SDL_sysjoystick.c
Expand Up @@ -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 */
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.
Expand Down Expand Up @@ -378,11 +378,6 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void
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 */
Expand Down
1 change: 0 additions & 1 deletion src/joystick/haiku/SDL_haikujoystick.cc
Expand Up @@ -228,7 +228,6 @@ extern "C"
SDL_free(joystick->hwdata->new_hats);
SDL_free(joystick->hwdata->new_axes);
SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
}
}

Expand Down
1 change: 0 additions & 1 deletion src/joystick/iphoneos/SDL_sysjoystick.m
Expand Up @@ -153,7 +153,6 @@ static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
[motionManager stopAccelerometerUpdates];
joystick->closed = 1;
}

/* Function to perform any system-specific joystick related cleanup */
Expand Down
4 changes: 1 addition & 3 deletions src/joystick/linux/SDL_sysjoystick.c
Expand Up @@ -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 */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
return !joystick->closed && (joystick->hwdata->item != NULL);
return joystick->hwdata->item != NULL;
}

static SDL_INLINE void
Expand Down Expand Up @@ -841,9 +841,7 @@ SDL_SYS_JoystickClose(SDL_Joystick * joystick)
SDL_free(joystick->hwdata->balls);
SDL_free(joystick->hwdata->fname);
SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
}
joystick->closed = 1;
}

/* Function to perform any system-specific joystick related cleanup */
Expand Down
1 change: 0 additions & 1 deletion src/joystick/psp/SDL_sysjoystick.c
Expand Up @@ -233,7 +233,6 @@ void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
/* Function to close a joystick after use */
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
{
/* Do nothing. */
}

/* Function to perform any system-specific joystick related cleanup */
Expand Down
2 changes: 0 additions & 2 deletions src/joystick/windows/SDL_mmjoystick.c
Expand Up @@ -383,9 +383,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
/* free system specific hardware data */
SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
}

/* Function to perform any system-specific joystick related cleanup */
Expand Down
11 changes: 3 additions & 8 deletions src/joystick/windows/SDL_windowsjoystick.c
Expand Up @@ -460,7 +460,6 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)

/* allocate memory for system specific hardware data */
joystick->instance_id = joystickdevice->nInstanceID;
joystick->closed = SDL_FALSE;
joystick->hwdata =
(struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
if (joystick->hwdata == NULL) {
Expand All @@ -480,13 +479,13 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
SDL_bool
SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
{
return !joystick->closed && !joystick->hwdata->removed;
return joystick->hwdata && !joystick->hwdata->removed;
}

void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
if (joystick->closed || !joystick->hwdata) {
if (!joystick->hwdata || joystick->hwdata->removed) {
return;
}

Expand All @@ -497,8 +496,7 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
}

if (joystick->hwdata->removed) {
joystick->closed = SDL_TRUE;
joystick->uncentered = SDL_TRUE;
joystick->force_recentering = SDL_TRUE;
}
}

Expand All @@ -512,10 +510,7 @@ SDL_SYS_JoystickClose(SDL_Joystick * joystick)
SDL_DINPUT_JoystickClose(joystick);
}

/* free system specific hardware data */
SDL_free(joystick->hwdata);

joystick->closed = SDL_TRUE;
}

/* Function to perform any system-specific joystick related cleanup */
Expand Down
6 changes: 6 additions & 0 deletions test/testjoystick.c
Expand Up @@ -56,6 +56,12 @@ loop(void *arg)

while (SDL_PollEvent(&event)) {
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:
SDL_Log("Joystick %d axis %d value: %d\n",
event.jaxis.which,
Expand Down

0 comments on commit 162ef5e

Please sign in to comment.