Skip to content
Permalink
Browse files

Cleanups in the joystick code.

Removed some redundant state and other confusions.

Fixes Bugzilla #2738.
  • Loading branch information
icculus committed Mar 24, 2015
1 parent 4a071b3 commit 162ef5eae92f4684d5142af47f2fbdbb9292e3de
@@ -206,10 +206,6 @@ SDL_PrivateJoystickValid(SDL_Joystick * joystick)
valid = 1;
}

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

return valid;
}

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

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

joysticklist = SDL_joysticks;
joysticklistprev = NULL;
@@ -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... */
@@ -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;
@@ -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 */
};

@@ -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
@@ -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 */
@@ -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
@@ -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);
@@ -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.
@@ -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;
}

@@ -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 */
@@ -58,8 +58,7 @@ struct joystick_hwdata
recElement *firstButton;
recElement *firstHat;

int removed;
int uncentered;
SDL_bool removed;

int instance_id;
SDL_JoystickGUID guid;
@@ -34,7 +34,7 @@
int
SDL_SYS_JoystickInit(void)
{
return (0);
return 0;
}

int SDL_SYS_NumJoysticks()
@@ -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 )
@@ -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.
@@ -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 */
@@ -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;
}
}

@@ -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 */
@@ -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
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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) {
@@ -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;
}

@@ -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;
}
}

@@ -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 */
@@ -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,

0 comments on commit 162ef5e

Please sign in to comment.