/
iohidmanager_hid.c
1036 lines (885 loc) · 31.2 KB
/
iohidmanager_hid.c
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* RetroArch - A frontend for libretro.
* Copyright (C) 2013-2014 - Jason Fetters
* Copyright (C) 2011-2017 - Daniel De Matteis
* Courtesy Contributor - Olivier Parra
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string/stdstring.h>
#include <IOKit/hid/IOHIDManager.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <retro_miscellaneous.h>
#include "../input_defines.h"
#include "../input_driver.h"
#include "../connect/joypad_connection.h"
#include "../../tasks/tasks_internal.h"
#include "../../verbosity.h"
typedef struct apple_input_rec
{
IOHIDElementCookie cookie;
uint32_t id;
struct apple_input_rec *next;
} apple_input_rec_t;
typedef struct apple_hid
{
IOHIDManagerRef ptr;
joypad_connection_t *slots;
uint32_t buttons[MAX_USERS];
int16_t axes[MAX_USERS][6];
int8_t hats[MAX_USERS][2]; /* MacOS only supports 1 hat AFAICT */
} iohidmanager_hid_t;
struct iohidmanager_hid_adapter
{
uint32_t slot;
IOHIDDeviceRef handle;
char name[PATH_MAX_LENGTH];
apple_input_rec_t *axes;
apple_input_rec_t *hats;
apple_input_rec_t *buttons;
uint8_t data[2048];
uint32_t uniqueId;
};
CFComparisonResult iohidmanager_sort_elements(const void *val1, const void *val2, void *context)
{
uint32_t page1 = (uint32_t)IOHIDElementGetUsagePage((IOHIDElementRef)val1);
uint32_t page2 = (uint32_t)IOHIDElementGetUsagePage((IOHIDElementRef)val2);
uint32_t use1 = (uint32_t)IOHIDElementGetUsage((IOHIDElementRef)val1);
uint32_t use2 = (uint32_t)IOHIDElementGetUsage((IOHIDElementRef)val2);
uint32_t cookie1 = (uint32_t)IOHIDElementGetCookie((IOHIDElementRef)val1);
uint32_t cookie2 = (uint32_t)IOHIDElementGetCookie((IOHIDElementRef)val2);
if (page1 != page2)
return (CFComparisonResult)(page1 > page2);
if (use1 != use2)
return (CFComparisonResult)(use1 > use2);
return (CFComparisonResult)(cookie1 > cookie2);
}
static bool iohidmanager_check_for_id(apple_input_rec_t *rec, uint32_t id)
{
while (rec)
{
if (rec->id == id)
return true;
rec = rec->next;
}
return false;
}
static void iohidmanager_append_record(apple_input_rec_t *rec, apple_input_rec_t *b)
{
apple_input_rec_t *tmp = rec;
while (tmp->next)
tmp = tmp->next;
tmp->next = b;
}
/* Insert a new detected button into a button ordered list.
* Button list example with Nimbus Controller:
* +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
* "id" list member | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 144 | 145 | 146 | 147 | 547 |
* Final Button ID | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
* +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
* Ranges |< X/Y/A/B/L1/L2/R1/R2 buttons >|< D-PAD >|MENU |
* In that way, HID button IDs allocation:
* - becomes robust and determinist
* - remains compatible with previous algorithm (i.e. btn->id = (uint32_t)(use - 1)) and so
* compatible with previous autoconfig files.
*/
static void iohidmanager_append_record_ordered(apple_input_rec_t **p_rec, apple_input_rec_t *b)
{
apple_input_rec_t *tmp = *p_rec;
while (tmp && (tmp->id <= b->id))
{
p_rec = &tmp->next;
tmp = tmp->next;
}
b->next = tmp;
*p_rec = b;
}
static bool iohidmanager_hid_joypad_query(void *data, unsigned pad)
{
return pad < MAX_USERS;
}
static const char *iohidmanager_hid_joypad_name(void *data, unsigned pad)
{
/* TODO/FIXME - implement properly */
if (pad >= MAX_USERS)
return NULL;
return NULL;
}
static void iohidmanager_hid_joypad_get_buttons(void *data,
unsigned port, input_bits_t *state)
{
iohidmanager_hid_t *hid = (iohidmanager_hid_t*)data;
if (hid)
return pad_connection_get_buttons(&hid->slots[port], port, state);
else
BIT256_CLEAR_ALL_PTR(state);
}
static bool iohidmanager_hid_joypad_button(void *data,
unsigned port, uint16_t joykey)
{
input_bits_t buttons;
iohidmanager_hid_t *hid = (iohidmanager_hid_t*)data;
unsigned hat_dir = GET_HAT_DIR(joykey);
iohidmanager_hid_joypad_get_buttons(data, port, &buttons);
/* Check hat. */
if (hat_dir)
{
unsigned h = GET_HAT(joykey);
if (h >= 1)
return false;
switch(hat_dir)
{
case HAT_LEFT_MASK:
return hid->hats[port][0] < 0;
case HAT_RIGHT_MASK:
return hid->hats[port][0] > 0;
case HAT_UP_MASK:
return hid->hats[port][1] < 0;
case HAT_DOWN_MASK:
return hid->hats[port][1] > 0;
}
return 0;
}
/* Check the button. */
if ((port < MAX_USERS) && (joykey < 32))
return (BIT256_GET(buttons, joykey) != 0)
|| ((hid->buttons[port] & (1 << joykey)) != 0);
return false;
}
static bool iohidmanager_hid_joypad_rumble(void *data, unsigned pad,
enum retro_rumble_effect effect, uint16_t strength)
{
iohidmanager_hid_t *hid = (iohidmanager_hid_t*)data;
if (!hid)
return false;
return pad_connection_rumble(&hid->slots[pad], pad, effect, strength);
}
static int16_t iohidmanager_hid_joypad_axis(void *data,
unsigned port, uint32_t joyaxis)
{
iohidmanager_hid_t *hid = (iohidmanager_hid_t*)data;
int16_t val = 0;
if (joyaxis == AXIS_NONE)
return 0;
if (AXIS_NEG_GET(joyaxis) < 6)
{
val += hid->axes[port][AXIS_NEG_GET(joyaxis)];
val += pad_connection_get_axis(&hid->slots[port],
port, AXIS_NEG_GET(joyaxis));
if (val >= 0)
val = 0;
}
else if (AXIS_POS_GET(joyaxis) < 6)
{
val += hid->axes[port][AXIS_POS_GET(joyaxis)];
val += pad_connection_get_axis(&hid->slots[port],
port, AXIS_POS_GET(joyaxis));
if (val <= 0)
val = 0;
}
return val;
}
static void iohidmanager_hid_device_send_control(void *data,
uint8_t* data_buf, size_t size)
{
struct iohidmanager_hid_adapter *adapter =
(struct iohidmanager_hid_adapter*)data;
if (adapter)
IOHIDDeviceSetReport(adapter->handle,
kIOHIDReportTypeOutput, 0x01, data_buf + 1, size - 1);
}
static void iohidmanager_hid_device_report(void *data,
IOReturn result, void *sender,
IOHIDReportType type, uint32_t reportID, uint8_t *report,
CFIndex reportLength)
{
struct iohidmanager_hid_adapter *adapter =
(struct iohidmanager_hid_adapter*)data;
iohidmanager_hid_t *hid = (iohidmanager_hid_t*)hid_driver_get_data();
if (hid && adapter)
pad_connection_packet(&hid->slots[adapter->slot], adapter->slot,
adapter->data, (uint32_t)(reportLength + 1));
}
/* NOTE: I pieced this together through trial and error,
* any corrections are welcome. */
static void iohidmanager_hid_device_input_callback(void *data, IOReturn result,
void* sender, IOHIDValueRef value)
{
iohidmanager_hid_t *hid = (iohidmanager_hid_t*)hid_driver_get_data();
struct iohidmanager_hid_adapter *adapter = (struct iohidmanager_hid_adapter*)data;
IOHIDElementRef element = IOHIDValueGetElement(value);
uint32_t type = (uint32_t)IOHIDElementGetType(element);
uint32_t page = (uint32_t)IOHIDElementGetUsagePage(element);
uint32_t use = (uint32_t)IOHIDElementGetUsage(element);
uint32_t cookie = (uint32_t)IOHIDElementGetCookie(element);
apple_input_rec_t *tmp = NULL;
if (type != kIOHIDElementTypeInput_Misc)
if (type != kIOHIDElementTypeInput_Button)
if (type != kIOHIDElementTypeInput_Axis)
return;
/* Joystick handler.
* TODO: Can GamePad work the same? */
int pushed_button = 0;
switch (page)
{
case kHIDPage_GenericDesktop:
switch (type)
{
case kIOHIDElementTypeInput_Misc:
switch (use)
{
case kHIDUsage_GD_Hatswitch:
{
tmp = adapter->hats;
while (tmp && tmp->cookie != (IOHIDElementCookie)cookie)
tmp = tmp->next;
if (tmp->cookie == (IOHIDElementCookie)cookie)
{
CFIndex range = IOHIDElementGetLogicalMax(element) - IOHIDElementGetLogicalMin(element);
CFIndex val = IOHIDValueGetIntegerValue(value);
if (range == 3)
val *= 2;
switch(val)
{
case 0:
/* pos = up */
hid->hats[adapter->slot][0] = 0;
hid->hats[adapter->slot][1] = -1;
break;
case 1:
/* pos = up+right */
hid->hats[adapter->slot][0] = 1;
hid->hats[adapter->slot][1] = -1;
break;
case 2:
/* pos = right */
hid->hats[adapter->slot][0] = 1;
hid->hats[adapter->slot][1] = 0;
break;
case 3:
/* pos = down+right */
hid->hats[adapter->slot][0] = 1;
hid->hats[adapter->slot][1] = 1;
break;
case 4:
/* pos = down */
hid->hats[adapter->slot][0] = 0;
hid->hats[adapter->slot][1] = 1;
break;
case 5:
/* pos = down+left */
hid->hats[adapter->slot][0] = -1;
hid->hats[adapter->slot][1] = 1;
break;
case 6:
/* pos = left */
hid->hats[adapter->slot][0] = -1;
hid->hats[adapter->slot][1] = 0;
break;
case 7:
/* pos = up_left */
hid->hats[adapter->slot][0] = -1;
hid->hats[adapter->slot][1] = -1;
break;
default:
/* pos = centered */
hid->hats[adapter->slot][0] = 0;
hid->hats[adapter->slot][1] = 0;
break;
}
}
}
break;
default:
tmp = adapter->axes;
while (tmp && tmp->cookie != (IOHIDElementCookie)cookie)
tmp = tmp->next;
if (tmp)
{
if (tmp->cookie == (IOHIDElementCookie)cookie)
{
CFIndex min = IOHIDElementGetPhysicalMin(element);
CFIndex state = IOHIDValueGetIntegerValue(value) - min;
CFIndex max = IOHIDElementGetPhysicalMax(element) - min;
float val = (float)state / (float)max;
hid->axes[adapter->slot][tmp->id] =
((val * 2.0f) - 1.0f) * 32767.0f;
}
}
else
pushed_button = 1;
break;
}
break;
}
break;
case kHIDPage_Consumer:
case kHIDPage_Button:
switch (type)
{
case kIOHIDElementTypeInput_Misc:
case kIOHIDElementTypeInput_Button:
pushed_button = 1;
break;
}
break;
}
if (pushed_button)
{
uint8_t bit = 0;
tmp = adapter->buttons;
while (tmp && tmp->cookie != (IOHIDElementCookie)cookie)
{
bit++;
tmp = tmp->next;
}
if (tmp && tmp->cookie == (IOHIDElementCookie)cookie)
{
CFIndex state = IOHIDValueGetIntegerValue(value);
if (state)
BIT64_SET(hid->buttons[adapter->slot], bit);
else
BIT64_CLEAR(hid->buttons[adapter->slot], bit);
}
}
}
static void iohidmanager_hid_device_remove(void *data,
IOReturn result, void* sender)
{
struct iohidmanager_hid_adapter *adapter =
(struct iohidmanager_hid_adapter*)data;
iohidmanager_hid_t *hid = (iohidmanager_hid_t*)
hid_driver_get_data();
if (hid && adapter && (adapter->slot < MAX_USERS))
{
input_autoconfigure_disconnect(adapter->slot, adapter->name);
hid->buttons[adapter->slot] = 0;
memset(hid->axes[adapter->slot], 0, sizeof(hid->axes));
pad_connection_pad_deinit(&hid->slots[adapter->slot], adapter->slot);
}
if (adapter)
{
apple_input_rec_t* tmp = NULL;
while (adapter->hats != NULL)
{
tmp = adapter->hats;
adapter->hats = adapter->hats->next;
free(tmp);
}
while (adapter->axes != NULL)
{
tmp = adapter->axes;
adapter->axes = adapter->axes->next;
free(tmp);
}
while (adapter->buttons != NULL)
{
tmp = adapter->buttons;
adapter->buttons = adapter->buttons->next;
free(tmp);
}
free(adapter);
}
}
static int32_t iohidmanager_hid_device_get_int_property(
IOHIDDeviceRef device, CFStringRef key)
{
CFNumberRef ref = (CFNumberRef)IOHIDDeviceGetProperty(device, key);
if (ref && (CFGetTypeID(ref) == CFNumberGetTypeID()))
{
int32_t value = 0;
CFNumberGetValue((CFNumberRef)ref, kCFNumberIntType, &value);
return value;
}
return 0;
}
static uint16_t iohidmanager_hid_device_get_vendor_id(IOHIDDeviceRef device)
{
return iohidmanager_hid_device_get_int_property(device,
CFSTR(kIOHIDVendorIDKey));
}
static uint16_t iohidmanager_hid_device_get_product_id(IOHIDDeviceRef device)
{
return iohidmanager_hid_device_get_int_property(device,
CFSTR(kIOHIDProductIDKey));
}
static uint32_t iohidmanager_hid_device_get_location_id(IOHIDDeviceRef device)
{
return iohidmanager_hid_device_get_int_property(device,
CFSTR(kIOHIDLocationIDKey));
}
static uint32_t iohidmanager_hid_device_get_unique_id(IOHIDDeviceRef device)
{
/* osx seems to assign an unique id to each device when they are plugged in
* the id change if device is unplugged/plugged, but it's unique amongst the
* other device plugged */
return iohidmanager_hid_device_get_int_property(device,CFSTR(kIOHIDUniqueIDKey));
}
static void iohidmanager_hid_device_get_product_string(
IOHIDDeviceRef device, char *buf, size_t len)
{
CFStringRef ref = (CFStringRef)
IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
if (ref)
CFStringGetCString(ref, buf, len, kCFStringEncodingUTF8);
}
static void iohidmanager_hid_device_add_autodetect(unsigned idx,
const char *device_name, const char *driver_name,
uint16_t dev_vid, uint16_t dev_pid)
{
if (!input_autoconfigure_connect(
device_name,
NULL,
driver_name,
idx,
dev_vid,
dev_pid
))
input_config_set_device_name(idx, device_name);
RARCH_LOG("Port %d: %s.\n", idx, device_name);
}
static void iohidmanager_hid_device_add_device(IOHIDDeviceRef device, iohidmanager_hid_t* hid)
{
int i;
/* get device unique id */
uint32_t deviceUniqueId = iohidmanager_hid_device_get_unique_id(device);
/* check if pad was already registered previously (by deterministic method)
* if so do not re-add the pad */
for (i=0; i<MAX_USERS; i++)
{
struct iohidmanager_hid_adapter *a = hid->slots[i].data;
if (a == NULL)
continue;
if (a->uniqueId == deviceUniqueId)
return;
}
IOReturn ret;
uint16_t dev_vid, dev_pid;
CFArrayRef elements_raw;
int count;
CFMutableArrayRef elements;
CFRange range;
bool found_axis[6] =
{ false, false, false, false, false, false };
apple_input_rec_t *tmp = NULL;
apple_input_rec_t *tmpButtons = NULL;
apple_input_rec_t *tmpAxes = NULL;
struct iohidmanager_hid_adapter *adapter = (struct iohidmanager_hid_adapter*)
calloc(1, sizeof(*adapter));
if (!adapter)
return;
if (!hid)
goto error;
adapter->handle = device;
ret = IOHIDDeviceOpen(device, kIOHIDOptionsTypeNone);
if (ret != kIOReturnSuccess)
goto error;
/* Move the device's run loop to this thread. */
IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetCurrent(),
kCFRunLoopCommonModes);
IOHIDDeviceRegisterRemovalCallback(device,
iohidmanager_hid_device_remove, adapter);
#ifndef IOS
iohidmanager_hid_device_get_product_string(device, adapter->name,
sizeof(adapter->name));
#endif
dev_vid = iohidmanager_hid_device_get_vendor_id (device);
dev_pid = iohidmanager_hid_device_get_product_id (device);
adapter->uniqueId = deviceUniqueId;
adapter->slot = pad_connection_pad_init(hid->slots,
adapter->name, dev_vid, dev_pid, adapter,
&iohidmanager_hid);
if (adapter->slot == -1)
goto error;
if (pad_connection_has_interface(hid->slots, adapter->slot))
IOHIDDeviceRegisterInputReportCallback(device,
adapter->data + 1, sizeof(adapter->data) - 1,
iohidmanager_hid_device_report, adapter);
else
IOHIDDeviceRegisterInputValueCallback(device,
iohidmanager_hid_device_input_callback, adapter);
if (string_is_empty(adapter->name))
goto error;
/* scan for buttons, axis, hats */
elements_raw = IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone);
count = (int)CFArrayGetCount(elements_raw);
elements = CFArrayCreateMutableCopy(
kCFAllocatorDefault,(CFIndex)count,elements_raw);
range = CFRangeMake(0,count);
CFArraySortValues(elements,
range, iohidmanager_sort_elements, NULL);
for (i = 0; i < count; i++)
{
IOHIDElementType type;
uint32_t page, use, cookie;
int detected_button = 0;
IOHIDElementRef element = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i);
if (!element)
continue;
type = IOHIDElementGetType(element);
page = (uint32_t)IOHIDElementGetUsagePage(element);
use = (uint32_t)IOHIDElementGetUsage(element);
cookie = (uint32_t)IOHIDElementGetCookie(element);
switch (page)
{
case kHIDPage_GenericDesktop:
switch (type)
{
case kIOHIDElementTypeCollection:
case kIOHIDElementTypeInput_ScanCodes:
case kIOHIDElementTypeFeature:
case kIOHIDElementTypeInput_Button:
case kIOHIDElementTypeOutput:
case kIOHIDElementTypeInput_Axis:
/* TODO/FIXME */
break;
case kIOHIDElementTypeInput_Misc:
switch (use)
{
case kHIDUsage_GD_Hatswitch:
{
/* as far as I can tell, OSX only reports one Hat */
apple_input_rec_t *hat = (apple_input_rec_t *)malloc(sizeof(apple_input_rec_t));
hat->id = 0;
hat->cookie = (IOHIDElementCookie)cookie;
hat->next = NULL;
adapter->hats = hat;
}
break;
default:
{
uint32_t i = 0;
static const uint32_t axis_use_ids[6] =
{ 48, 49, 51, 52, 50, 53 };
while (i < 6 && axis_use_ids[i] != use)
i++;
if (i < 6)
{
apple_input_rec_t *axis = (apple_input_rec_t *)malloc(sizeof(apple_input_rec_t));
axis->id = i;
axis->cookie = (IOHIDElementCookie)cookie;
axis->next = NULL;
if (iohidmanager_check_for_id(adapter->axes,i))
{
/* axis ID already exists, save to tmp for appending later */
if (tmpAxes)
iohidmanager_append_record(tmpAxes, axis);
else
tmpAxes = axis;
}
else
{
found_axis[axis->id] = true;
if (adapter->axes)
iohidmanager_append_record(adapter->axes, axis);
else
adapter->axes = axis;
}
}
else
detected_button = 1;
}
break;
}
break;
}
break;
case kHIDPage_Consumer:
case kHIDPage_Button:
switch (type)
{
case kIOHIDElementTypeCollection:
case kIOHIDElementTypeFeature:
case kIOHIDElementTypeInput_ScanCodes:
case kIOHIDElementTypeInput_Axis:
case kIOHIDElementTypeOutput:
/* TODO/FIXME */
break;
case kIOHIDElementTypeInput_Misc:
case kIOHIDElementTypeInput_Button:
detected_button = 1;
break;
}
break;
}
if (detected_button)
{
apple_input_rec_t *btn = (apple_input_rec_t *)malloc(sizeof(apple_input_rec_t));
btn->id = (uint32_t)use;
btn->cookie = (IOHIDElementCookie)cookie;
btn->next = NULL;
if (iohidmanager_check_for_id(adapter->buttons,btn->id))
{
if (tmpButtons)
iohidmanager_append_record_ordered(&tmpButtons, btn);
else
tmpButtons = btn;
}
else
{
if (adapter->buttons)
iohidmanager_append_record_ordered(&adapter->buttons, btn);
else
adapter->buttons = btn;
}
}
}
/* take care of buttons/axes with duplicate 'use' values */
for (i = 0; i < 6; i++)
{
if (found_axis[i] == false && tmpAxes)
{
apple_input_rec_t *next = tmpAxes->next;
tmpAxes->id = i;
tmpAxes->next = NULL;
iohidmanager_append_record(adapter->axes, tmpAxes);
tmpAxes = next;
}
}
tmp = adapter->buttons;
if (tmp)
{
while (tmp->next)
tmp = tmp->next;
}
while (tmpButtons)
{
apple_input_rec_t *next = tmpButtons->next;
tmpButtons->id = tmp->id;
tmpButtons->next = NULL;
tmp->next = tmpButtons;
tmp = tmp->next;
tmpButtons = next;
}
iohidmanager_hid_device_add_autodetect(adapter->slot,
adapter->name, iohidmanager_hid.ident, dev_vid, dev_pid);
return;
error:
{
apple_input_rec_t *tmp = NULL;
while (adapter->hats != NULL)
{
tmp = adapter->hats;
adapter->hats = adapter->hats->next;
free(tmp);
}
while (adapter->axes != NULL)
{
tmp = adapter->axes;
adapter->axes = adapter->axes->next;
free(tmp);
}
while (adapter->buttons != NULL)
{
tmp = adapter->buttons;
adapter->buttons = adapter->buttons->next;
free(tmp);
}
while (tmpAxes != NULL)
{
tmp = tmpAxes;
tmpAxes = tmpAxes->next;
free(tmp);
}
while (tmpButtons != NULL)
{
tmp = tmpButtons;
tmpButtons = tmpButtons->next;
free(tmp);
}
free(adapter);
}
}
static void iohidmanager_hid_device_add(void *data, IOReturn result,
void* sender, IOHIDDeviceRef device)
{
iohidmanager_hid_t *hid = (iohidmanager_hid_t*) hid_driver_get_data();
iohidmanager_hid_device_add_device(device, hid);
}
static void iohidmanager_hid_append_matching_dictionary(
CFMutableArrayRef array,
uint32_t page, uint32_t use)
{
CFMutableDictionaryRef matcher = CFDictionaryCreateMutable(
kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFNumberRef pagen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
CFNumberRef usen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &use);
CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsagePageKey), pagen);
CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsageKey), usen);
CFArrayAppendValue(array, matcher);
CFRelease(pagen);
CFRelease(usen);
CFRelease(matcher);
}
static int iohidmanager_hid_manager_init(iohidmanager_hid_t *hid)
{
if (!hid)
return -1;
if (hid->ptr) /* already initialized. */
return 0;
hid->ptr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (!hid->ptr)
return -1;
IOHIDManagerSetDeviceMatching(hid->ptr, NULL);
IOHIDManagerScheduleWithRunLoop(hid->ptr, CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
return 0;
}
static int iohidmanager_hid_manager_free(iohidmanager_hid_t *hid)
{
if (!hid || !hid->ptr)
return -1;
IOHIDManagerUnscheduleFromRunLoop(hid->ptr,
CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
IOHIDManagerClose(hid->ptr, kIOHIDOptionsTypeNone);
CFRelease(hid->ptr);
hid->ptr = NULL;
return 0;
}
static int iohidmanager_hid_manager_set_device_matching(
iohidmanager_hid_t *hid)
{
/* deterministically add all device currently plugged when lanching retroarch
* order by location id which seems to correspond to usb port number */
CFSetRef set = IOHIDManagerCopyDevices(hid->ptr);
CFIndex num_devices = CFSetGetCount(set);
IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef));
CFSetGetValues(set, (const void **) device_array);
/* re order device by location id */
typedef struct hid_list
{
IOHIDDeviceRef device;
uint32_t lid;
struct hid_list *next;
} hid_list_t;
hid_list_t* devList = NULL;
for (long i=0; i<num_devices;i++)
{
IOHIDDeviceRef dev = device_array[i];
/* filter gamepad/joystick devices */
if ( IOHIDDeviceConformsTo(dev, kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick)
|| IOHIDDeviceConformsTo(dev, kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad)
)
{
if ( devList == NULL )
{
devList = (hid_list_t *)malloc(sizeof(hid_list_t));
devList->device = dev;
devList->lid = iohidmanager_hid_device_get_location_id(dev);
devList->next = NULL;
}
else
{
hid_list_t * new = (hid_list_t *)malloc(sizeof(hid_list_t));
new->device = dev;
new->lid = iohidmanager_hid_device_get_location_id(dev);
new->next = NULL;
hid_list_t * ptr = devList;
if ( new->lid < ptr->lid )
{
new->next = ptr;
devList = new;
}
else
{
while ( ( ptr->lid < new->lid ) && (ptr->next != NULL) )
{
ptr = ptr->next;
}
new->next = ptr->next;
ptr->next = new;
}
}
}
}
/* register devices */
hid_list_t * ptr = devList;
while (ptr != NULL)
{
iohidmanager_hid_device_add_device(ptr->device, hid);
//printf("%d\n",ptr->lid);
ptr = ptr->next;
free(devList);
devList = ptr;
}
free(device_array);
/* register call back to dynamically add device plugged when retroarch is
* running
* those will be added after the one plugged when retroarch was launched,
* and by order they are plugged in (so not deterministic) */
CFMutableArrayRef matcher = CFArrayCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeArrayCallBacks);
if (!matcher)
return -1;
iohidmanager_hid_append_matching_dictionary(matcher,
kHIDPage_GenericDesktop,
kHIDUsage_GD_Joystick);
iohidmanager_hid_append_matching_dictionary(matcher,
kHIDPage_GenericDesktop,
kHIDUsage_GD_GamePad);
IOHIDManagerSetDeviceMatchingMultiple(hid->ptr, matcher);
IOHIDManagerRegisterDeviceMatchingCallback(hid->ptr,
iohidmanager_hid_device_add, 0);
CFRelease(matcher);
return 0;
}
static void *iohidmanager_hid_init(void)
{
iohidmanager_hid_t *hid_apple = (iohidmanager_hid_t*)
calloc(1, sizeof(*hid_apple));
if (!hid_apple)
return NULL;
hid_apple->slots = pad_connection_init(MAX_USERS);
if (!hid_apple->slots)
goto error;
if (iohidmanager_hid_manager_init(hid_apple) == -1)
goto error;
if (iohidmanager_hid_manager_set_device_matching(hid_apple) == -1)
goto error;
return hid_apple;
error:
if (hid_apple->slots)
free(hid_apple->slots);
hid_apple->slots = NULL;
if (hid_apple)