Debian Codegraph @

src/macosx/hidman.m

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * MacOS X HID Manager access routines.
12 *
13 * By Angelo Mottola.
14 *
15 * See readme.txt for copyright information.
16 */
17
18
19 #include "allegro5/allegro.h"
20 #include "allegro5/internal/aintern.h"
21 #include "allegro5/internal/aintern_events.h"
22 #include "allegro5/internal/aintern_keyboard.h"
23 #include "allegro5/platform/aintosx.h"
24
25 #ifndef ALLEGRO_MACOSX
26 #error Something is wrong with the makefile
27 #endif
28
29 #define USAGE(p, u) (((p)<<16)+(u))
30
31 static void hid_store_element_data(CFTypeRef element, int type, HID_DEVICE *device, int app, int collection, int index);
32 static void hid_scan_physical_collection(CFTypeRef properties, HID_DEVICE *device, int app, int collection);
33 static void hid_scan_application_collection(CFMutableDictionaryRef properties, HID_DEVICE *device);
34
35 static HID_DEVICE* add_device(HID_DEVICE_COLLECTION*);
36
37 /* add_element:
38 * Add a new, blank element to a device.
39 * This is later specialized into a button
40 * axis or whatever
41 */
42 static HID_ELEMENT* add_element(HID_DEVICE* d) {
43 HID_ELEMENT* e;
44 if (d->element==NULL) {
45 d->capacity=8;
46 d->element=al_malloc(d->capacity*sizeof(HID_ELEMENT));
47 }
48 if (d->num_elements>=d->capacity) {
49 d->capacity*=2;
50 d->element=al_realloc(d->element, d->capacity*sizeof(HID_ELEMENT));
51 }
52 e=&d->element[d->num_elements++];
53 memset(e, 0, sizeof(HID_ELEMENT));
54 return e;
55 }
56
57 /* i_val:
58 * Helper method - get an integer value from a dictionary
59 * Defaults to 0 if the value is not found; in practice this
60 * should not occur.
61 */
62 static int i_val(CFTypeRef d, CFStringRef key) {
63 int ans;
64 CFTypeRef num = CFDictionaryGetValue(d, key);
65 if (num)
66 CFNumberGetValue(num, kCFNumberIntType, &ans);
67 else
68 ans = 0;
69 return ans;
70 }
71
72 /* hid_store_element_data:
73 * Parse this HID element, break it down and store the
74 * relevant data in the device structure
75 */
76 static void hid_store_element_data(CFTypeRef element, int type, HID_DEVICE *device, int app, int col, int idx)
77 {
78 HID_ELEMENT *hid_element;
79 CFTypeRef type_ref;
80 const char *name;
81
82 hid_element = add_element(device);
83 hid_element->type = type;
84 hid_element->index = idx;
85 hid_element->col = col;
86 hid_element->app = app;
87 hid_element->cookie = (IOHIDElementCookie) i_val(element, CFSTR(kIOHIDElementCookieKey));
88 hid_element->min = i_val(element, CFSTR(kIOHIDElementMinKey));
89 hid_element->max = i_val(element, CFSTR(kIOHIDElementMaxKey));
90 type_ref = CFDictionaryGetValue(element, CFSTR(kIOHIDElementNameKey));
91 if ((type_ref) && (name = CFStringGetCStringPtr(type_ref, CFStringGetSystemEncoding())))
92 hid_element->name = strdup(name);
93 else
94 hid_element->name = NULL;
95 }
96
97 /* hid_scan_application:
98 * scan the elements that make up one 'application'
99 * i.e. one unit like a joystick.
100 */
101 static void hid_scan_application(CFTypeRef properties, HID_DEVICE *device, int app)
102 {
103 CFTypeRef array_ref, element;
104 int type, usage_page, usage;
105 int i;
106 int axis=0;
107 int stick=0;
108 array_ref = CFDictionaryGetValue(properties, CFSTR(kIOHIDElementKey));
109 if ((array_ref) && (CFGetTypeID(array_ref) == CFArrayGetTypeID())) {
110 for (i = 0; i < CFArrayGetCount(array_ref); i++) {
111 element = CFArrayGetValueAtIndex(array_ref, i);
112 if (CFGetTypeID(element) == CFDictionaryGetTypeID()) {
113 type = i_val(element, CFSTR(kIOHIDElementTypeKey));
114 usage = i_val(element, CFSTR(kIOHIDElementUsageKey));
115 usage_page = i_val(element, CFSTR(kIOHIDElementUsagePageKey));
116 if (type == kIOHIDElementTypeCollection)
117 {
118 /* It is a collection; recurse into it, if it is part of the
119 * generic desktop (sometimes the whole joystick is wrapped
120 * up inside a collection like this.
121 */
122 if (usage_page == kHIDPage_GenericDesktop)
123 hid_scan_application(element, device, app);
124 }
125 else {
126 switch (usage_page) {
127 case kHIDPage_GenericDesktop:
128 switch (usage) {
129 case kHIDUsage_GD_Pointer:
130 if (axis!=0) {
131 /* already have some elements in this stick */
132 ++stick;
133 axis=0;
134 }
135 hid_scan_physical_collection(element, device, app, stick);
136 ++stick;
137 break;
138 case kHIDUsage_GD_X:
139 hid_store_element_data(element, HID_ELEMENT_AXIS_PRIMARY_X, device, app, stick, axis++);
140 break;
141
142 case kHIDUsage_GD_Y:
143 hid_store_element_data(element, HID_ELEMENT_AXIS_PRIMARY_Y, device, app, stick, axis++);
144 break;
145
146 case kHIDUsage_GD_Z:
147 case kHIDUsage_GD_Rx:
148 case kHIDUsage_GD_Ry:
149 case kHIDUsage_GD_Rz:
150 hid_store_element_data(element, HID_ELEMENT_AXIS, device,app, stick, axis++);
151 break;
152
153 case kHIDUsage_GD_Slider:
154 case kHIDUsage_GD_Dial:
155 case kHIDUsage_GD_Wheel:
156 /* If we've already seen some axes on this stick, move to the next one */
157 if (axis > 0)
158 {
159 ++stick;
160 axis=0;
161 }
162 hid_store_element_data(element, HID_ELEMENT_STANDALONE_AXIS, device, app, stick++, 0);
163 break;
164
165 case kHIDUsage_GD_Hatswitch:
166 /* If we've already seen some axes on this stick, move to the next one */
167 if (axis > 0)
168 {
169 ++stick;
170 axis=0;
171 }
172 hid_store_element_data(element, HID_ELEMENT_HAT, device, app, stick++, 0);
173 break;
174 }
175 break;
176 case kHIDPage_Button:
177 hid_store_element_data(element, HID_ELEMENT_BUTTON, device, app, 0, usage-1);
178 break;
179 }
180 }
181 if (axis>=2) {
182 ++stick;
183 axis=0;
184 }
185 }
186 }
187 }
188 }
189
190 /* hid_scan_physical_collection:
191 * scan the elements that make up one 'stick'
192 */
193 static void hid_scan_physical_collection(CFTypeRef properties, HID_DEVICE *device, int app, int stick)
194 {
195 CFTypeRef array_ref, element;
196 int type, usage_page, usage;
197 int i;
198 int axis=0;
199 array_ref = CFDictionaryGetValue(properties, CFSTR(kIOHIDElementKey));
200 if ((array_ref) && (CFGetTypeID(array_ref) == CFArrayGetTypeID())) {
201 for (i = 0; i < CFArrayGetCount(array_ref); i++) {
202 element = CFArrayGetValueAtIndex(array_ref, i);
203 if (CFGetTypeID(element) == CFDictionaryGetTypeID()) {
204 type = i_val(element, CFSTR(kIOHIDElementTypeKey));
205 usage = i_val(element, CFSTR(kIOHIDElementUsageKey));
206 usage_page = i_val(element, CFSTR(kIOHIDElementUsagePageKey));
207 switch (usage_page) {
208 case kHIDPage_GenericDesktop:
209 switch (usage) {
210 case kHIDUsage_GD_X:
211 hid_store_element_data(element, HID_ELEMENT_AXIS_PRIMARY_X, device, app, stick, axis++);
212 break;
213
214 case kHIDUsage_GD_Y:
215 hid_store_element_data(element, HID_ELEMENT_AXIS_PRIMARY_Y, device, app, stick, axis++);
216 break;
217
218 case kHIDUsage_GD_Z:
219 case kHIDUsage_GD_Rx:
220 case kHIDUsage_GD_Ry:
221 case kHIDUsage_GD_Rz:
222 hid_store_element_data(element, HID_ELEMENT_AXIS, device,app, stick, axis++);
223 break;
224 }
225 break;
226 case kHIDPage_Button:
227 hid_store_element_data(element, HID_ELEMENT_BUTTON, device, app, 0, usage-1);
228 break;
229 }
230 }
231 }
232 }
233 }
234
235 /* hid_scan_application_collection:
236 * Scan the elements array; each element will be an 'application'
237 * i.e. one joystick, gamepad or mouse
238 *
239 */
240 static void hid_scan_application_collection(CFMutableDictionaryRef properties, HID_DEVICE *device)
241 {
242 CFTypeRef array_ref, element;
243
244 int usage, usage_page;
245 int i;
246
247 array_ref = CFDictionaryGetValue(properties, CFSTR(kIOHIDElementKey));
248 if ((array_ref) && (CFGetTypeID(array_ref) == CFArrayGetTypeID())) {
249 for (i = 0; i < CFArrayGetCount(array_ref); i++) {
250 element = CFArrayGetValueAtIndex(array_ref, i);
251 if (CFGetTypeID(element) == CFDictionaryGetTypeID()) {
252 usage=i_val(element, CFSTR(kIOHIDElementUsageKey));
253 usage_page=i_val(element, CFSTR(kIOHIDElementUsagePageKey));
254 switch (USAGE(usage_page, usage)) {
255 case USAGE(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick):
256 device->type=HID_JOYSTICK;
257 hid_scan_application(element, device, device->cur_app);
258 device->cur_app++;
259 break;
260 case USAGE(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad):
261 device->type=HID_GAMEPAD;
262 hid_scan_application(element, device, device->cur_app);
263 device->cur_app++;
264 break;
265 case USAGE(kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse):
266 device->type=HID_MOUSE;
267 hid_scan_application(element, device, device->cur_app);
268 device->cur_app++;
269 break;
270 }
271 }
272 }
273 }
274 }
275
276 /* get_usb_properties:
277 * Get a property dictionary from the USB plane.
278 */
279 static CFMutableDictionaryRef get_usb_properties(io_object_t device)
280 {
281 io_registry_entry_t parent, grandparent;
282 CFMutableDictionaryRef usb_properties = NULL;
283 if (IORegistryEntryGetParentEntry(device, kIOServicePlane, &parent) == KERN_SUCCESS) {
284 if (IORegistryEntryGetParentEntry(parent, kIOServicePlane, &grandparent) == KERN_SUCCESS) {
285 IORegistryEntryCreateCFProperties (grandparent, &usb_properties,
286 kCFAllocatorDefault, kNilOptions);
287 IOObjectRelease(grandparent);
288 }
289 IOObjectRelease(parent);
290 }
291 return usb_properties;
292 }
293
294 #if OSX_HID_PSEUDO_SCAN
295 /*
296 * Pseudo scan - for development purposes, if someone has hardware
297 * that isn't parsed by this code, you can ask them to dump _their_ scan
298 * as a plist, then reload it here in order to debug it.
299 */
300 HID_DEVICE_COLLECTION *_al_osx_hid_scan(int type, HID_DEVICE_COLLECTION* col)
301 {
302 HID_DEVICE* this_device;
303 NSDictionary* properties = nil;
304 NSString* filename;
305 switch (type) {
306 case HID_GAMEPAD:
307 filename = @"gamepad.plist";
308 break;
309 case HID_JOYSTICK:
310 filename = @"joystick.plist";
311 break;
312 case HID_MOUSE:
313 filename = @"mouse.plist";
314 break;
315 default:
316 filename = nil;
317 break;
318 }
319 if (filename != nil)
320 properties = [NSDictionary dictionaryWithContentsOfFile:filename];
321 if (properties)
322 {
323 this_device = add_device(col);
324 this_device->manufacturer = strdup([((NSString*) [properties objectForKey: @kIOHIDManufacturerKey]) lossyCString]);
325 this_device->product = strdup([((NSString*) [properties objectForKey: @kIOHIDProductKey]) lossyCString]);
326 hid_scan_application_collection((CFDictionaryRef) properties, this_device);
327 [properties release];
328 }
329 return col;
330 }
331 #else
332 /* _get_matching_services:
333 * get iterator for corresponding services
334 */
335 static IOReturn _get_matching_services(mach_port_t master_port, int usage_page, int usage, io_iterator_t *hid_object_iterator)
336 {
337 CFMutableDictionaryRef class_dictionary = NULL;
338 CFNumberRef usage_ref = NULL;
339 CFNumberRef usage_page_ref = NULL;
340
341 class_dictionary = IOServiceMatching(kIOHIDDeviceKey);
342 if (class_dictionary) {
343 /* Add key for device type to refine the matching dictionary. */
344 usage_ref = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
345 usage_page_ref = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage_page);
346 CFDictionarySetValue(class_dictionary, CFSTR(kIOHIDDeviceUsageKey), usage_ref);
347 CFDictionarySetValue(class_dictionary, CFSTR(kIOHIDDeviceUsagePageKey), usage_page_ref);
348 }
349 IOReturn result = IOServiceGetMatchingServices(master_port, class_dictionary, hid_object_iterator);
350 if (usage_ref) CFRelease(usage_ref);
351 if (usage_page_ref) CFRelease(usage_page_ref);
352 return result;
353 }
354
355 /* _al_osx_hid_scan:
356 * Scan the hid manager for devices of type 'type',
357 * and append to the collection col
358 */
359 HID_DEVICE_COLLECTION *_al_osx_hid_scan(int type, HID_DEVICE_COLLECTION* col)
360 {
361 ASSERT(col);
362 HID_DEVICE *this_device;
363 mach_port_t master_port = 0;
364 io_iterator_t hid_object_iterator = 0;
365 io_object_t hid_device = 0;
366 int usage, usage_page;
367 CFTypeRef type_ref;
368 CFMutableDictionaryRef properties = NULL, usb_properties = NULL;
369 IOCFPlugInInterface **plugin_interface = NULL;
370 IOReturn result;
371 const char *string;
372 SInt32 score = 0;
373 int error;
374
375 usage_page = kHIDPage_GenericDesktop;
376 switch (type) {
377 case HID_MOUSE:
378 usage = kHIDUsage_GD_Mouse;
379 break;
380 case HID_JOYSTICK:
381 usage = kHIDUsage_GD_Joystick;
382 break;
383 case HID_GAMEPAD:
384 usage=kHIDUsage_GD_GamePad;
385 break;
386 }
387
388 result = IOMasterPort(bootstrap_port, &master_port);
389 if (result == kIOReturnSuccess) {
390 result = _get_matching_services(master_port, usage_page, usage, &hid_object_iterator);
391 if ((type == HID_MOUSE) && (hid_object_iterator == IO_OBJECT_NULL)) {
392 /* in case of a mouse, GD_Mouse must not be true but can also be a pointing device */
393 result = _get_matching_services(master_port, usage_page, kHIDUsage_GD_Pointer, &hid_object_iterator);
394 }
395 if ((result == kIOReturnSuccess) && (hid_object_iterator)) {
396 /* Ok, we have a list of attached HID devices; scan them. */
397 while ((hid_device = IOIteratorNext(hid_object_iterator))!=0) {
398 if ((IORegistryEntryCreateCFProperties(hid_device, &properties, kCFAllocatorDefault, kNilOptions) == KERN_SUCCESS) && (properties != NULL)) {
399 error = false;
400 this_device = add_device(col);
401 this_device->type = type;
402 if (col->count==0) {
403 this_device->cur_app=0;
404 }
405 else {
406 this_device->cur_app=col->devices[col->count-1].cur_app;
407 }
408 /*
409 * Mac OS X currently is not mirroring all USB properties
410 * to HID page so need to look at USB device page also.
411 */
412 usb_properties = get_usb_properties(hid_device);
413 type_ref = CFDictionaryGetValue(properties, CFSTR(kIOHIDManufacturerKey));
414 if (!type_ref)
415 type_ref = CFDictionaryGetValue(usb_properties, CFSTR("USB Vendor Name"));
416 if ((type_ref) && (string = CFStringGetCStringPtr(type_ref, CFStringGetSystemEncoding())))
417 this_device->manufacturer = strdup(string);
418 else
419 this_device->manufacturer = NULL;
420 type_ref = CFDictionaryGetValue(properties, CFSTR(kIOHIDProductKey));
421 if (!type_ref)
422 type_ref = CFDictionaryGetValue(usb_properties, CFSTR("USB Product Name"));
423 if ((type_ref) && (string = CFStringGetCStringPtr(type_ref, CFStringGetSystemEncoding())))
424 this_device->product = strdup(string);
425 else
426 this_device->product = NULL;
427
428 type_ref = CFDictionaryGetValue(usb_properties, CFSTR("USB Address"));
429 if ((type == HID_MOUSE) && (!type_ref)) {
430 /* Not an USB mouse. Must be integrated trackpad: we report it as a single button mouse */
431 add_element(this_device)->type = HID_ELEMENT_BUTTON;
432 }
433 else {
434 /* Scan for device elements */
435 this_device->num_elements = 0;
436 hid_scan_application_collection(properties, this_device);
437 }
438
439 this_device->interface = NULL;
440 if ((type == HID_JOYSTICK) || (type == HID_GAMEPAD)) {
441 /* Joystick or gamepad device: create HID interface */
442 if (IOCreatePlugInInterfaceForService(hid_device, kIOHIDDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin_interface, &score) != kIOReturnSuccess)
443 error = true;
444 else {
445 if ((*plugin_interface)->QueryInterface(plugin_interface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (void *)&(this_device->interface)) != S_OK)
446 error = true;
447 (*plugin_interface)->Release(plugin_interface);
448 if ((*(this_device->interface))->open(this_device->interface, 0) != KERN_SUCCESS)
449 error = true;
450 }
451 }
452 if (error) {
453 if (this_device->manufacturer)
454 al_free(this_device->manufacturer);
455 if (this_device->product)
456 al_free(this_device->product);
457 if (this_device->interface)
458 (*(this_device->interface))->Release(this_device->interface);
459 this_device->interface=NULL;
460 --col->count;
461 }
462
463 CFRelease(properties);
464 CFRelease(usb_properties);
465 }
466 }
467 IOObjectRelease(hid_object_iterator);
468 }
469 mach_port_deallocate(mach_task_self(), master_port);
470 }
471
472 return col;
473 }
474 #endif
475 /* add_device:
476 * add a new device to a collection
477 */
478 static HID_DEVICE* add_device(HID_DEVICE_COLLECTION* o) {
479 HID_DEVICE* d;
480 if (o->devices==NULL) {
481 o->count=0;
482 o->capacity=1;
483 o->devices=al_malloc(o->capacity*sizeof(HID_DEVICE));
484 }
485 if (o->count>=o->capacity) {
486 o->capacity*=2;
487 o->devices=al_realloc(o->devices, o->capacity*sizeof(HID_DEVICE));
488 }
489 d=&o->devices[o->count];
490 memset(d, 0, sizeof(HID_DEVICE));
491 /* Chain onto the preceding app count */
492 if (o->count>0)
493 d->cur_app = o->devices[o->count-1].cur_app;
494 ++o->count;
495 return d;
496 }
497
498 /* _al_osx_hid_free:
499 * Release the memory taken up by a collection
500 */
501 void _al_osx_hid_free(HID_DEVICE_COLLECTION *col)
502 {
503 HID_DEVICE *device;
504 HID_ELEMENT *element;
505 int i, j;
506
507 for (i = 0; i < col->count; i++) {
508 device = &col->devices[i];
509 if (device->manufacturer)
510 al_free(device->manufacturer);
511 if (device->product)
512 al_free(device->product);
513 for (j = 0; j < device->num_elements; j++) {
514 element = &device->element[j];
515 if (element->name)
516 al_free(element->name);
517 }
518 al_free(device->element);
519 if (device->interface) {
520 (*(device->interface))->close(device->interface);
521 (*(device->interface))->Release(device->interface);
522 }
523 }
524 al_free(col->devices);
525 }
526
527
528 /* Local variables: */
529 /* c-basic-offset: 3 */
530 /* indent-tabs-mode: nil */
531 /* End: */