Cleanups in the joystick code. Removed some redundant state and other confusions. Fixes Bugzilla #2738.
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 2a60c00..44cc216 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -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;
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index 819a961..f126e75 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -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 */
};
diff --git a/src/joystick/android/SDL_sysjoystick.c b/src/joystick/android/SDL_sysjoystick.c
index 49fa584..3d82fcb 100644
--- a/src/joystick/android/SDL_sysjoystick.c
+++ b/src/joystick/android/SDL_sysjoystick.c
@@ -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 */
diff --git a/src/joystick/bsd/SDL_sysjoystick.c b/src/joystick/bsd/SDL_sysjoystick.c
index 65a32ed..efbd79c 100644
--- a/src/joystick/bsd/SDL_sysjoystick.c
+++ b/src/joystick/bsd/SDL_sysjoystick.c
@@ -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
diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c
index cc82286..fc7ae75 100644
--- a/src/joystick/darwin/SDL_sysjoystick.c
+++ b/src/joystick/darwin/SDL_sysjoystick.c
@@ -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 */
diff --git a/src/joystick/darwin/SDL_sysjoystick_c.h b/src/joystick/darwin/SDL_sysjoystick_c.h
index 0a15ba1..4c3300d 100644
--- a/src/joystick/darwin/SDL_sysjoystick_c.h
+++ b/src/joystick/darwin/SDL_sysjoystick_c.h
@@ -58,8 +58,7 @@ struct joystick_hwdata
recElement *firstButton;
recElement *firstHat;
- int removed;
- int uncentered;
+ SDL_bool removed;
int instance_id;
SDL_JoystickGUID guid;
diff --git a/src/joystick/dummy/SDL_sysjoystick.c b/src/joystick/dummy/SDL_sysjoystick.c
index 1d1c16a..a046e0e 100644
--- a/src/joystick/dummy/SDL_sysjoystick.c
+++ b/src/joystick/dummy/SDL_sysjoystick.c
@@ -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 )
diff --git a/src/joystick/emscripten/SDL_sysjoystick.c b/src/joystick/emscripten/SDL_sysjoystick.c
index 5b82677..a1e9c15 100644
--- a/src/joystick/emscripten/SDL_sysjoystick.c
+++ b/src/joystick/emscripten/SDL_sysjoystick.c
@@ -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 */
diff --git a/src/joystick/haiku/SDL_haikujoystick.cc b/src/joystick/haiku/SDL_haikujoystick.cc
index 394804e..c9b7e11 100644
--- a/src/joystick/haiku/SDL_haikujoystick.cc
+++ b/src/joystick/haiku/SDL_haikujoystick.cc
@@ -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;
}
}
diff --git a/src/joystick/iphoneos/SDL_sysjoystick.m b/src/joystick/iphoneos/SDL_sysjoystick.m
index dd626f2..56c3925 100644
--- a/src/joystick/iphoneos/SDL_sysjoystick.m
+++ b/src/joystick/iphoneos/SDL_sysjoystick.m
@@ -153,7 +153,6 @@ void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
[motionManager stopAccelerometerUpdates];
- joystick->closed = 1;
}
/* Function to perform any system-specific joystick related cleanup */
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index d4e42e4..7dd33ef 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -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 */
diff --git a/src/joystick/psp/SDL_sysjoystick.c b/src/joystick/psp/SDL_sysjoystick.c
index 9eae4e2..41f9358 100644
--- a/src/joystick/psp/SDL_sysjoystick.c
+++ b/src/joystick/psp/SDL_sysjoystick.c
@@ -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 */
diff --git a/src/joystick/windows/SDL_mmjoystick.c b/src/joystick/windows/SDL_mmjoystick.c
index 7fd555d..6c02597 100644
--- a/src/joystick/windows/SDL_mmjoystick.c
+++ b/src/joystick/windows/SDL_mmjoystick.c
@@ -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 */
diff --git a/src/joystick/windows/SDL_windowsjoystick.c b/src/joystick/windows/SDL_windowsjoystick.c
index 3404899..5682a22 100644
--- a/src/joystick/windows/SDL_windowsjoystick.c
+++ b/src/joystick/windows/SDL_windowsjoystick.c
@@ -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 */
diff --git a/test/testjoystick.c b/test/testjoystick.c
index 72f056c..adb376e 100644
--- a/test/testjoystick.c
+++ b/test/testjoystick.c
@@ -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,