Commit b352698d99b08f6bd240fecd06f9e5b4a999d2e6

Edward Rudd 2014-02-04T18:17:16

Implement new backend methods for haptic and hot plugging on OS X

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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
index fa8e493..0222d83 100755
--- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj
+++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
@@ -1083,6 +1083,7 @@
 		D55A1B7F179F262300625D7C /* SDL_cocoamousetap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_cocoamousetap.h; sourceTree = "<group>"; };
 		D55A1B80179F262300625D7C /* SDL_cocoamousetap.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_cocoamousetap.m; sourceTree = "<group>"; };
 		DB31407717554B71006C0E22 /* libSDL2.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libSDL2.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
+		DB89958518A1A5C50092407C /* SDL_syshaptic_c.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDL_syshaptic_c.h; sourceTree = "<group>"; };
 		F59C70FF00D5CB5801000001 /* ReadMe.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = ReadMe.txt; sourceTree = "<group>"; };
 		F59C710000D5CB5801000001 /* Welcome.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = Welcome.txt; sourceTree = "<group>"; };
 		F59C710300D5CB5801000001 /* ReadMe.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = ReadMe.txt; sourceTree = "<group>"; };
@@ -1401,6 +1402,7 @@
 			isa = PBXGroup;
 			children = (
 				04BDFDF312E6671700899322 /* SDL_syshaptic.c */,
+				DB89958518A1A5C50092407C /* SDL_syshaptic_c.h */,
 			);
 			path = darwin;
 			sourceTree = "<group>";
diff --git a/src/haptic/darwin/SDL_syshaptic.c b/src/haptic/darwin/SDL_syshaptic.c
index b939802..6df950b 100644
--- a/src/haptic/darwin/SDL_syshaptic.c
+++ b/src/haptic/darwin/SDL_syshaptic.c
@@ -22,11 +22,13 @@
 
 #ifdef SDL_HAPTIC_IOKIT
 
+#include "SDL_assert.h"
 #include "SDL_haptic.h"
 #include "../SDL_syshaptic.h"
 #include "SDL_joystick.h"
 #include "../../joystick/SDL_sysjoystick.h"     /* For the real SDL_Joystick */
 #include "../../joystick/darwin/SDL_sysjoystick_c.h"    /* For joystick hwdata */
+#include "SDL_syshaptic_c.h"
 
 #include <IOKit/IOKitLib.h>
 #include <IOKit/hid/IOHIDKeys.h>
@@ -38,13 +40,10 @@
 #define IO_OBJECT_NULL  ((io_service_t)0)
 #endif
 
-#define MAX_HAPTICS  32
-
-
 /*
  * List of available haptic devices.
  */
-static struct
+typedef struct SDL_hapticlist_item
 {
     char name[256];             /* Name of the device. */
 
@@ -54,7 +53,9 @@ static struct
     /* Usage pages for determining if it's a mouse or not. */
     long usage;
     long usagePage;
-} SDL_hapticlist[MAX_HAPTICS];
+
+    struct SDL_hapticlist_item *next;
+} SDL_hapticlist_item;
 
 
 /*
@@ -82,6 +83,9 @@ struct haptic_hweffect
 static void SDL_SYS_HapticFreeFFEFFECT(FFEFFECT * effect, int type);
 static int HIDGetDeviceProduct(io_service_t dev, char *name);
 
+static SDL_hapticlist_item *SDL_hapticlist = NULL;
+static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
+static int numhaptics = 0;
 
 /*
  * Like strerror but for force feedback errors.
@@ -146,16 +150,10 @@ FFStrError(HRESULT err)
 int
 SDL_SYS_HapticInit(void)
 {
-    int numhaptics;
     IOReturn result;
     io_iterator_t iter;
     CFDictionaryRef match;
     io_service_t device;
-    CFMutableDictionaryRef hidProperties;
-    CFTypeRef refCF;
-
-    /* Clear all the memory. */
-    SDL_memset(SDL_hapticlist, 0, sizeof(SDL_hapticlist));
 
     /* Get HID devices. */
     match = IOServiceMatching(kIOHIDDeviceKey);
@@ -174,62 +172,150 @@ SDL_SYS_HapticInit(void)
         return 0;
     }
 
-    numhaptics = 0;
     while ((device = IOIteratorNext(iter)) != IO_OBJECT_NULL) {
+        PRIVATE_MaybeAddDevice(device);
+        /* always release as the AddDevice will retain IF it's a forcefeedback device */
+        IOObjectRelease(device);
+    }
+    IOObjectRelease(iter);
 
-        /* Check for force feedback. */
-        if (FFIsForceFeedback(device) == FF_OK) {
-
-            /* Set basic device data. */
-            HIDGetDeviceProduct(device, SDL_hapticlist[numhaptics].name);
-            SDL_hapticlist[numhaptics].dev = device;
-            SDL_hapticlist[numhaptics].haptic = NULL;
-
-            /* Set usage pages. */
-            hidProperties = 0;
-            refCF = 0;
-            result = IORegistryEntryCreateCFProperties(device,
-                                                       &hidProperties,
-                                                       kCFAllocatorDefault,
-                                                       kNilOptions);
-            if ((result == KERN_SUCCESS) && hidProperties) {
-                refCF =
-                    CFDictionaryGetValue(hidProperties,
-                                         CFSTR(kIOHIDPrimaryUsagePageKey));
-                if (refCF) {
-                    if (!CFNumberGetValue(refCF, kCFNumberLongType,
-                                          &SDL_hapticlist[numhaptics].
-                                          usagePage))
-                        SDL_SetError
-                            ("Haptic: Recieving device's usage page.");
-                    refCF =
-                        CFDictionaryGetValue(hidProperties,
-                                             CFSTR(kIOHIDPrimaryUsageKey));
-                    if (refCF) {
-                        if (!CFNumberGetValue(refCF, kCFNumberLongType,
-                                              &SDL_hapticlist[numhaptics].
-                                              usage))
-                            SDL_SetError("Haptic: Recieving device's usage.");
-                    }
-                }
-                CFRelease(hidProperties);
-            }
+    return numhaptics;
+}
+
+int
+SDL_SYS_NumHaptics()
+{
+    return numhaptics;
+}
+
+static SDL_hapticlist_item *
+HapticByDevIndex(int device_index)
+{
+    SDL_hapticlist_item *item = SDL_hapticlist;
+    
+    if ((device_index < 0) || (device_index >= numhaptics)) {
+        return NULL;
+    }
+    
+    while (device_index > 0) {
+        SDL_assert(item != NULL);
+        device_index--;
+        item = item->next;
+    }
+    
+    return item;
+}
+
+int
+PRIVATE_MaybeAddDevice( io_object_t device )
+{
+    IOReturn result;
+    CFMutableDictionaryRef hidProperties;
+    CFTypeRef refCF;
+    SDL_hapticlist_item *item;
 
-            /* Device has been added. */
-            numhaptics++;
-        } else {                /* Free the unused device. */
-            IOObjectRelease(device);
+    /* Check for force feedback. */
+    if (FFIsForceFeedback(device) != FF_OK) {
+        return -1;
+    }
+
+    /* Make sure we don't already have it */
+    for (item = SDL_hapticlist; item ; item = item->next)
+    {
+        if (IOObjectIsEqualTo((io_object_t) item->dev, device)) {
+            /* Already added */
+            return -1;
         }
+    }
 
-        /* Reached haptic limit. */
-        if (numhaptics >= MAX_HAPTICS)
-            break;
+    item = (SDL_hapticlist_item *)SDL_malloc( sizeof(SDL_hapticlist_item));
+    if (item == NULL) {
+        return SDL_SetError("Could not allocate haptic storage");
     }
-    IOObjectRelease(iter);
+    
+    /* retain it as we are going to keep it around a while */
+    IOObjectRetain(device);
+
+    /* Set basic device data. */
+    HIDGetDeviceProduct(device, item->name);
+    item->dev = device;
+    item->haptic = NULL;
+    
+    /* Set usage pages. */
+    hidProperties = 0;
+    refCF = 0;
+    result = IORegistryEntryCreateCFProperties(device,
+                                               &hidProperties,
+                                               kCFAllocatorDefault,
+                                               kNilOptions);
+    if ((result == KERN_SUCCESS) && hidProperties) {
+        refCF =
+        CFDictionaryGetValue(hidProperties,
+                             CFSTR(kIOHIDPrimaryUsagePageKey));
+        if (refCF) {
+            if (!CFNumberGetValue(refCF, kCFNumberLongType,
+                                  &item->usagePage))
+                SDL_SetError
+                ("Haptic: Recieving device's usage page.");
+            refCF =
+            CFDictionaryGetValue(hidProperties,
+                                 CFSTR(kIOHIDPrimaryUsageKey));
+            if (refCF) {
+                if (!CFNumberGetValue(refCF, kCFNumberLongType,
+                                      &item->usage))
+                    SDL_SetError("Haptic: Recieving device's usage.");
+            }
+        }
+        CFRelease(hidProperties);
+    }
+    
+    if (SDL_hapticlist_tail == NULL) {
+        SDL_hapticlist = SDL_hapticlist_tail = item;
+    } else {
+        SDL_hapticlist_tail->next = item;
+        SDL_hapticlist_tail = item;
+    }
+    
+    /* Device has been added. */
+    ++numhaptics;
 
     return numhaptics;
 }
 
+int
+PRIVATE_MaybeRemoveDevice( io_object_t device )
+{
+    SDL_hapticlist_item *item;
+    SDL_hapticlist_item *prev = NULL;
+    
+    for (item = SDL_hapticlist; item != NULL; item = item->next) {
+        /* found it, remove it. */
+        if (IOObjectIsEqualTo((io_object_t) item->dev, device)) {
+            const int retval = item->haptic ? item->haptic->index : -1;
+
+            if (prev != NULL) {
+                prev->next = item->next;
+            } else {
+                SDL_assert(SDL_hapticlist == item);
+                SDL_hapticlist = item->next;
+            }
+            if (item == SDL_hapticlist_tail) {
+                SDL_hapticlist_tail = prev;
+            }
+
+            /* Need to decrement the haptic count */
+            --numhaptics;
+            /* !!! TODO: Send a haptic remove event? */
+            
+            IOObjectRelease(item->dev);
+            SDL_free(item);
+            return retval;
+        }
+        prev = item;
+    }
+    
+    return -1;
+}
 
 /*
  * Return the name of a haptic device, does not need to be opened.
@@ -237,7 +323,9 @@ SDL_SYS_HapticInit(void)
 const char *
 SDL_SYS_HapticName(int index)
 {
-    return SDL_hapticlist[index].name;
+    SDL_hapticlist_item *item;
+    item = HapticByDevIndex(index);
+    return item->name;
 }
 
 /*
@@ -470,8 +558,10 @@ SDL_SYS_HapticOpenFromService(SDL_Haptic * haptic, io_service_t service)
 int
 SDL_SYS_HapticOpen(SDL_Haptic * haptic)
 {
-    return SDL_SYS_HapticOpenFromService(haptic,
-                                         SDL_hapticlist[haptic->index].dev);
+    SDL_hapticlist_item *item;
+    item = HapticByDevIndex(haptic->index);
+
+    return SDL_SYS_HapticOpenFromService(haptic, item->dev);
 }
 
 
@@ -481,12 +571,15 @@ SDL_SYS_HapticOpen(SDL_Haptic * haptic)
 int
 SDL_SYS_HapticMouse(void)
 {
-    int i;
+    int device_index = 0;
+    SDL_hapticlist_item *item;
 
-    for (i = 0; i < SDL_numhaptics; i++) {
-        if ((SDL_hapticlist[i].usagePage == kHIDPage_GenericDesktop) &&
-            (SDL_hapticlist[i].usage == kHIDUsage_GD_Mouse))
-            return i;
+    for (item = SDL_hapticlist; item; item = item->next) {
+        if ((item->usagePage == kHIDPage_GenericDesktop) &&
+            (item->usage == kHIDUsage_GD_Mouse))
+            return device_index;
+
+        ++device_index;
     }
 
     return -1;
@@ -524,16 +617,16 @@ SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
 int
 SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
 {
-    int i;
-    for (i=0; i<SDL_numhaptics; i++) {
-       if (IOObjectIsEqualTo((io_object_t) SDL_hapticlist[i].dev,
+    int device_index = 0;
+    SDL_hapticlist_item *item;
+
+    for (item = SDL_hapticlist; item; item = item->next) {
+        if (IOObjectIsEqualTo((io_object_t) item->dev,
                              joystick->hwdata->ffservice)) {
-           haptic->index = i;
+           haptic->index = device_index;
            break;
-       }
-    }
-    if (i >= SDL_numhaptics) {
-       return -1;
+        }
+        ++device_index;
     }
 
     return SDL_SYS_HapticOpenFromService(haptic, joystick->hwdata->ffservice);
@@ -569,15 +662,18 @@ SDL_SYS_HapticClose(SDL_Haptic * haptic)
 void
 SDL_SYS_HapticQuit(void)
 {
-    int i;
+    SDL_hapticlist_item *item;
+    SDL_hapticlist_item *next = NULL;
 
-    for (i = 0; i < SDL_numhaptics; i++) {
+    for (item = SDL_hapticlist; item; item = next) {
+        next = item->next;
         /* Opened and not closed haptics are leaked, this is on purpose.
          * Close your haptic devices after usage. */
 
         /* Free the io_service_t */
-        IOObjectRelease(SDL_hapticlist[i].dev);
+        IOObjectRelease(item->dev);
     }
+    numhaptics = 0;
 }
 
 
diff --git a/src/haptic/darwin/SDL_syshaptic_c.h b/src/haptic/darwin/SDL_syshaptic_c.h
new file mode 100644
index 0000000..0e86253
--- /dev/null
+++ b/src/haptic/darwin/SDL_syshaptic_c.h
@@ -0,0 +1,26 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
+ 
+ This software is provided 'as-is', without any express or implied
+ warranty.  In no event will the authors be held liable for any damages
+ arising from the use of this software.
+ 
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+ 
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+ */
+
+int
+PRIVATE_MaybeAddDevice( io_object_t device );
+
+int
+PRIVATE_MaybeRemoveDevice( io_object_t device );
diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c
index 6961f0f..f57bcf8 100644
--- a/src/joystick/darwin/SDL_sysjoystick.c
+++ b/src/joystick/darwin/SDL_sysjoystick.c
@@ -48,6 +48,7 @@
 #include "../SDL_joystick_c.h"
 #include "SDL_sysjoystick_c.h"
 #include "SDL_events.h"
+#include "../../haptic/darwin/SDL_syshaptic_c.h"    /* For haptic hot plugging */
 #if !SDL_EVENTS_DISABLED
 #include "../../events/SDL_events_c.h"
 #endif
@@ -122,6 +123,9 @@ HIDRemovalCallback(void *target, IOReturn result, void *refcon, void *sender)
 {
     recDevice *device = (recDevice *) refcon;
     device->removed = 1;
+#if SDL_HAPTIC_IOKIT
+    PRIVATE_MaybeRemoveDevice(device->ffservice);
+#endif
     s_bDeviceRemoved = SDL_TRUE;
 }
 
@@ -134,6 +138,9 @@ void JoystickDeviceWasRemovedCallback( void * refcon, io_service_t service, natu
     {
         recDevice *device = (recDevice *) refcon;
         device->removed = 1;
+#if SDL_HAPTIC_IOKIT
+        PRIVATE_MaybeRemoveDevice(device->ffservice);
+#endif
         s_bDeviceRemoved = SDL_TRUE;
     }
 }
@@ -679,6 +686,9 @@ AddDeviceHelper( io_object_t ioHIDDeviceObject )
      * SDL_HapticOpenFromJoystick */
     if (FFIsForceFeedback(ioHIDDeviceObject) == FF_OK) {
         device->ffservice = ioHIDDeviceObject;
+#if SDL_HAPTIC_IOKIT
+        PRIVATE_MaybeAddDevice(ioHIDDeviceObject);
+#endif
     } else {
         device->ffservice = 0;
     }