Fixed bug 2869 - Controllers connected on launch are reported twice. Since all device detection/removal happens on the main thread now, post events inline with when the status changes occur. Also fixed rare cases when joystick API functions could return data about removed joysticks when called with a device index.
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
diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c
index 29fd95f..f46035d 100644
--- a/src/joystick/darwin/SDL_sysjoystick.c
+++ b/src/joystick/darwin/SDL_sysjoystick.c
@@ -46,13 +46,23 @@ static IOHIDManagerRef hidman = NULL;
/* Linked list of all available devices */
static recDevice *gpDeviceList = NULL;
-/* if SDL_TRUE then a device was added since the last update call */
-static SDL_bool s_bDeviceAdded = SDL_FALSE;
-static SDL_bool s_bDeviceRemoved = SDL_FALSE;
-
/* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */
static int s_joystick_instance_id = -1;
+static recDevice *GetDeviceForIndex(int device_index)
+{
+ recDevice *device = gpDeviceList;
+ while (device) {
+ if (!device->removed) {
+ if (device_index == 0)
+ break;
+
+ --device_index;
+ }
+ device = device->pNext;
+ }
+ return device;
+}
static void
FreeElementList(recElement *pElement)
@@ -143,7 +153,22 @@ JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
#if SDL_HAPTIC_IOKIT
MacHaptic_MaybeRemoveDevice(device->ffservice);
#endif
- s_bDeviceRemoved = SDL_TRUE;
+
+/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
+#if !SDL_EVENTS_DISABLED
+ {
+ SDL_Event event;
+ event.type = SDL_JOYDEVICEREMOVED;
+
+ if (SDL_GetEventState(event.type) == SDL_ENABLE) {
+ event.jdevice.which = device->instance_id;
+ if ((SDL_EventOK == NULL)
+ || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
+ SDL_PushEvent(&event);
+ }
+ }
+ }
+#endif /* !SDL_EVENTS_DISABLED */
}
@@ -381,6 +406,7 @@ static void
JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
{
recDevice *device;
+ int device_index = 0;
if (res != kIOReturnSuccess) {
return;
@@ -420,9 +446,6 @@ JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDevic
#endif
}
- device->send_open_event = 1;
- s_bDeviceAdded = SDL_TRUE;
-
/* Add device to the end of the list */
if ( !gpDeviceList ) {
gpDeviceList = device;
@@ -431,10 +454,27 @@ JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDevic
curdevice = gpDeviceList;
while ( curdevice->pNext ) {
+ ++device_index;
curdevice = curdevice->pNext;
}
curdevice->pNext = device;
}
+
+/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
+#if !SDL_EVENTS_DISABLED
+ {
+ SDL_Event event;
+ event.type = SDL_JOYDEVICEADDED;
+
+ if (SDL_GetEventState(event.type) == SDL_ENABLE) {
+ event.jdevice.which = device_index;
+ if ((SDL_EventOK == NULL)
+ || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
+ SDL_PushEvent(&event);
+ }
+ }
+ }
+#endif /* !SDL_EVENTS_DISABLED */
}
static SDL_bool
@@ -560,53 +600,12 @@ SDL_SYS_NumJoysticks()
void
SDL_SYS_JoystickDetect()
{
- if (s_bDeviceAdded || s_bDeviceRemoved) {
- recDevice *device = gpDeviceList;
- s_bDeviceAdded = SDL_FALSE;
- s_bDeviceRemoved = SDL_FALSE;
- int device_index = 0;
- /* send notifications */
- while (device) {
- if (device->send_open_event) {
- device->send_open_event = 0;
-/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
-#if !SDL_EVENTS_DISABLED
- SDL_Event event;
- event.type = SDL_JOYDEVICEADDED;
-
- if (SDL_GetEventState(event.type) == SDL_ENABLE) {
- event.jdevice.which = device_index;
- if ((SDL_EventOK == NULL)
- || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
- SDL_PushEvent(&event);
- }
- }
-#endif /* !SDL_EVENTS_DISABLED */
-
- }
-
- if (device->removed) {
- const int instance_id = device->instance_id;
- device = FreeDevice(device);
-
-/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
-#if !SDL_EVENTS_DISABLED
- SDL_Event event;
- event.type = SDL_JOYDEVICEREMOVED;
-
- if (SDL_GetEventState(event.type) == SDL_ENABLE) {
- event.jdevice.which = instance_id;
- if ((SDL_EventOK == NULL)
- || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
- SDL_PushEvent(&event);
- }
- }
-#endif /* !SDL_EVENTS_DISABLED */
-
- } else {
- device = device->pNext;
- device_index++;
- }
+ recDevice *device = gpDeviceList;
+ while (device) {
+ if (device->removed) {
+ device = FreeDevice(device);
+ } else {
+ device = device->pNext;
}
}
@@ -621,13 +620,8 @@ SDL_SYS_JoystickDetect()
const char *
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{
- recDevice *device = gpDeviceList;
-
- while (device_index-- > 0) {
- device = device->pNext;
- }
-
- return device->product;
+ recDevice *device = GetDeviceForIndex(device_index);
+ return device ? device->product : "UNKNOWN";
}
/* Function to return the instance id of the joystick at device_index
@@ -635,14 +629,8 @@ SDL_SYS_JoystickNameForDeviceIndex(int device_index)
SDL_JoystickID
SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
- recDevice *device = gpDeviceList;
- int index;
-
- for (index = device_index; index > 0; index--) {
- device = device->pNext;
- }
-
- return device->instance_id;
+ recDevice *device = GetDeviceForIndex(device_index);
+ return device ? device->instance_id : 0;
}
/* Function to open a joystick for use.
@@ -653,12 +641,7 @@ SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{
- recDevice *device = gpDeviceList;
- int index;
-
- for (index = device_index; index > 0; index--) {
- device = device->pNext;
- }
+ recDevice *device = GetDeviceForIndex(device_index);
joystick->instance_id = device->instance_id;
joystick->hwdata = device;
@@ -805,21 +788,19 @@ SDL_SYS_JoystickQuit(void)
CFRelease(hidman);
hidman = NULL;
}
-
- s_bDeviceAdded = s_bDeviceRemoved = SDL_FALSE;
}
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{
- recDevice *device = gpDeviceList;
- int index;
-
- for (index = device_index; index > 0; index--) {
- device = device->pNext;
+ recDevice *device = GetDeviceForIndex(device_index);
+ SDL_JoystickGUID guid;
+ if (device) {
+ guid = device->guid;
+ } else {
+ SDL_zero(guid);
}
-
- return device->guid;
+ return guid;
}
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
diff --git a/src/joystick/darwin/SDL_sysjoystick_c.h b/src/joystick/darwin/SDL_sysjoystick_c.h
index 976bca7..7bc20d0 100644
--- a/src/joystick/darwin/SDL_sysjoystick_c.h
+++ b/src/joystick/darwin/SDL_sysjoystick_c.h
@@ -62,7 +62,6 @@ struct joystick_hwdata
int instance_id;
SDL_JoystickGUID guid;
- Uint8 send_open_event; /* 1 if we need to send an Added event for this device */
struct joystick_hwdata *pNext; /* next device */
};