-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing13.html
executable file
·2018 lines (1825 loc) · 87.9 KB
/
listing13.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>GLCarbon1ContextPbuffer - /HID Support/HID_Utilities.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/GraphicsImaging/index.html">Graphics & Imaging</a> > <a href="../../samplecode/GraphicsImaging/idxOpenGL-date.html">OpenGL</a> > <A HREF="javascript:location.replace('index.html');">GLCarbon1ContextPbuffer</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">GLCarbon1ContextPbuffer</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/HID Support/HID_Utilities.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/camera.h</option>
<option value="listing2.html">/drawInfo.h</option>
<option value="listing3.html">/glCheck.c</option>
<option value="listing4.html">/glCheck.h</option>
<option value="listing5.html">/HID Support/HID_Config_Utilities.c</option>
<option value="listing6.html">/HID Support/HID_Config_Utilities.h</option>
<option value="listing7.html">/HID Support/HID_Error_Handler.c</option>
<option value="listing8.html">/HID Support/HID_Error_Handler.h</option>
<option value="listing9.html">/HID Support/HID_Name_Lookup.c</option>
<option value="listing10.html">/HID Support/HID_Name_Lookup.h</option>
<option value="listing11.html">/HID Support/HID_Queue_Utilities.c</option>
<option value="listing12.html">/HID Support/HID_Queue_Utilities.h</option>
<option value="listing13.html">/HID Support/HID_Utilities.c</option>
<option value="listing14.html">/HID Support/HID_Utilities.h</option>
<option value="listing15.html">/HID Support/HID_Utilities_External.h</option>
<option value="listing16.html">/HID Support/HID_Utilities_Internal.h</option>
<option value="listing17.html">/HID Support/HIDSupport.c</option>
<option value="listing18.html">/HID Support/HIDSupport.h</option>
<option value="listing19.html">/main.c</option>
<option value="listing20.html">/main.h</option>
<option value="listing21.html">/pbuffer.c</option>
<option value="listing22.html">/pbuffer.h</option>
<option value="listing23.html">/SurfaceGeometry.c</option>
<option value="listing24.html">/SurfaceGeometry.h</option>
<option value="listing25.html">/trackball.c</option>
<option value="listing26.html">/trackball.h</option></select>
</p>
</form>
<p><strong><a href="GLCarbon1ContextPbuffer.zip">Download Sample</a></strong> (“GLCarbon1ContextPbuffer.zip”, 220.4K)<BR>
<strong><a href="GLCarbon1ContextPbuffer.dmg">Download Sample</a></strong> (“GLCarbon1ContextPbuffer.dmg”, 270.0K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
* HID Utilities.c
* HID Explorer
*
* Created by ggs on Fri Apr 20 2001.
Copyright: Copyright © 2001 Apple Computer, Inc., All Rights Reserved
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "HID_Utilities_Internal.h"
#include "HID_Utilities_External.h"
// ==================================
// private functions
// path to follow to next element
static char MapChar (char c);
static void CleanString (char * targetString);
static void HIDGetElementInfo (CFTypeRef refElement, pRecElement pElement);
static void HIDAddElement (CFTypeRef refElement, pRecElement * ppElementCurrent);
static void HIDGetElementsCFArrayHandler (const void * value, void * parameter);
static void HIDGetElements (CFTypeRef refElementCurrent, pRecElement * ppElementCurrent);
static void HIDGetCollectionElements (CFMutableDictionaryRef deviceProperties, pRecElement * ppElementCurrent);
static void HIDGetDeviceInfo (io_object_t hidDevice, CFMutableDictionaryRef hidProperties, pRecDevice pDevice);
static void HIDAddDevice (pRecDevice *ppListDeviceHead, pRecDevice pNewDevice);
static pRecDevice HIDMoveDevice (pRecDevice *ppListDeviceHead, pRecDevice pNewDevice, pRecDevice *ppOldListDeviceHead);
static pRecDevice HIDBuildDevice (io_object_t hidDevice);
static pRecDevice HIDCreateSingleTypeDeviceList (io_iterator_t hidObjectIterator);
static pRecDevice HIDCreateMultiTypeDeviceList (UInt32 *usagePage, UInt32 *usage, UInt32 numDeviceTypes);
static Boolean HIDFindDeviceInList (pRecDevice pDeviceList, pRecDevice pFindDevice);
static Boolean HIDCompareUpdateDeviceList (pRecDevice *ppListDeviceHead, pRecDevice *ppNewDeviceList);
static void HIDMergeDeviceList (pRecDevice *ppNewDeviceList, pRecDevice *ppDeviceList);
static io_iterator_t HIDGetIterator (const mach_port_t masterPort, UInt32 usagePage, UInt32 usage);
static CFMutableDictionaryRef HIDSetUpMatchingDictionary (UInt32 usagePage, UInt32 usage);
static void HIDDisposeDeviceElements (pRecElement pElement);
static pRecDevice HIDDisposeDevice (pRecDevice *ppDevice);
static UInt32 HIDCountCurrentDevices (pRecDevice pDeviceList);
static Boolean HIDMatchElementTypeMask (IOHIDElementType type, HIDElementTypeMask typeMask);
static pRecElement HIDGetDeviceElement (pRecElement pElement, HIDElementTypeMask typeMask);
// ==================================
// globals
// for element retrieval
pRecDevice gCurrentGetDevice = NULL;
Boolean gfAddAsChild = true;
pRecDevice gpDeviceList = NULL;
unsigned long gNumDevices = 0;
// ==================================
// private functions
// Maps bad chars to good chars for html/printing ASCII
static char MapChar (char c)
{
unsigned char uc = (unsigned char) c;
switch (uc)
{
case 0x01: return ' ';
case 0x02: return ' ';
case 0x03: return ' ';
case 0x04: return ' ';
case 0x05: return ' ';
case 0x06: return ' ';
case 0x07: return ' ';
case 0x08: return ' ';
case 0x09: return ' ';
case 0x0A: return ' ';
case 0x0B: return ' ';
case 0x0C: return ' ';
case 0x0D: return ' ';
case 0x0E: return ' ';
case 0x0F: return ' ';
case 0x10: return ' ';
case 0x11: return ' ';
case 0x12: return ' ';
case 0x13: return ' ';
case 0x14: return ' ';
case 0x15: return ' ';
case 0x16: return ' ';
case 0x17: return ' ';
case 0x18: return ' ';
case 0x19: return ' ';
case 0x1A: return ' ';
case 0x1B: return ' ';
case 0x1C: return ' ';
case 0x1D: return ' ';
case 0x1E: return ' ';
case 0x1F: return ' ';
case '/': return '-'; // use dash instead of slash
case 0x7F: return ' ';
case 0x80: return 'A';
case 0x81: return 'A';
case 0x82: return 'C';
case 0x83: return 'E';
case 0x84: return 'N';
case 0x85: return 'O';
case 0x86: return 'U';
case 0x87: return 'a';
case 0x88: return 'a';
case 0x89: return 'a';
case 0x8A: return 'a';
case 0x8B: return 'a';
case 0x8C: return 'a';
case 0x8D: return 'c';
case 0x8E: return 'e';
case 0x8F: return 'e';
case 0x90: return ' ';
case 0x91: return ' '; // ? '
case 0x92: return ' '; // ? '
case 0x93: return ' '; // ? "
case 0x94: return ' '; // ? "
case 0x95: return ' ';
case 0x96: return ' ';
case 0x97: return ' ';
case 0x98: return ' ';
case 0x99: return ' ';
case 0x9A: return ' ';
case 0x9B: return 0x27;
case 0x9C: return 0x22;
case 0x9D: return ' ';
case 0x9E: return ' ';
case 0x9F: return ' ';
case 0xA0: return ' ';
case 0xA1: return ' ';
case 0xA2: return ' ';
case 0xA3: return ' ';
case 0xA4: return ' ';
case 0xA5: return ' ';
case 0xA6: return ' ';
case 0xA7: return ' ';
case 0xA8: return ' ';
case 0xA9: return ' ';
case 0xAA: return ' ';
case 0xAB: return ' ';
case 0xAC: return ' ';
case 0xAD: return ' ';
case 0xAE: return ' ';
case 0xAF: return ' ';
case 0xB0: return ' ';
case 0xB1: return ' ';
case 0xB2: return ' ';
case 0xB3: return ' ';
case 0xB4: return ' ';
case 0xB5: return ' ';
case 0xB6: return ' ';
case 0xB7: return ' ';
case 0xB8: return ' ';
case 0xB9: return ' ';
case 0xBA: return ' ';
case 0xBB: return ' ';
case 0xBC: return ' ';
case 0xBD: return ' ';
case 0xBE: return ' ';
case 0xBF: return ' ';
case 0xC0: return ' ';
case 0xC1: return ' ';
case 0xC2: return ' ';
case 0xC3: return ' ';
case 0xC4: return ' ';
case 0xC5: return ' ';
case 0xC6: return ' ';
case 0xC7: return ' ';
case 0xC8: return ' ';
case 0xC9: return ' ';
case 0xCA: return ' ';
case 0xCB: return 'A';
case 0xCC: return 'A';
case 0xCD: return 'O';
case 0xCE: return ' ';
case 0xCF: return ' ';
case 0xD0: return '-';
case 0xD1: return '-';
case 0xD2: return 0x22;
case 0xD3: return 0x22;
case 0xD4: return 0x27;
case 0xD5: return 0x27;
case 0xD6: return '-'; // use dash instead of slash
case 0xD7: return ' ';
case 0xD8: return 'y';
case 0xD9: return 'Y';
case 0xDA: return '-'; // use dash instead of slash
case 0xDB: return ' ';
case 0xDC: return '<';
case 0xDD: return '>';
case 0xDE: return ' ';
case 0xDF: return ' ';
case 0xE0: return ' ';
case 0xE1: return ' ';
case 0xE2: return ',';
case 0xE3: return ',';
case 0xE4: return ' ';
case 0xE5: return 'A';
case 0xE6: return 'E';
case 0xE7: return 'A';
case 0xE8: return 'E';
case 0xE9: return 'E';
case 0xEA: return 'I';
case 0xEB: return 'I';
case 0xEC: return 'I';
case 0xED: return 'I';
case 0xEE: return 'O';
case 0xEF: return 'O';
case 0xF0: return ' ';
case 0xF1: return 'O';
case 0xF2: return 'U';
case 0xF3: return 'U';
case 0xF4: return 'U';
case 0xF5: return '|';
case 0xF6: return ' ';
case 0xF7: return ' ';
case 0xF8: return ' ';
case 0xF9: return ' ';
case 0xFA: return '.';
case 0xFB: return ' ';
case 0xFC: return ' ';
case 0xFD: return 0x22;
case 0xFE: return ' ';
case 0xFF: return ' ';
}
return c;
}
// ---------------------------------
// ensures the string only contains printable ASCII characters
// using varition of my html conversion source
// input is null terminated string, change is made in place
static void CleanString (char * targetString)
{
char * charIt = targetString;
while (*charIt) {
*charIt = MapChar (*charIt);
charIt++;
}
}
// ---------------------------------
// extracts actual specific element information from each element CF dictionary entry
static void HIDGetElementInfo (CFTypeRef refElement, pRecElement pElement)
{
long number;
CFTypeRef refType;
// type, usagePage, usage already stored
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementCookieKey));
if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
pElement->cookie = (IOHIDElementCookie) number;
else
pElement->cookie = (IOHIDElementCookie) 0;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementMinKey));
if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
pElement->min = number;
else
pElement->min = 0;
pElement->maxReport = pElement->min;
pElement->userMin = kDefaultUserMin;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementMaxKey));
if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
pElement->max = number;
else
pElement->max = 0;
pElement->minReport = pElement->max;
pElement->userMax = kDefaultUserMax;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMinKey));
if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
pElement->scaledMin = number;
else
pElement->scaledMin = 0;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMaxKey));
if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
pElement->scaledMax = number;
else
pElement->scaledMax = 0;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementSizeKey));
if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
pElement->size = number;
else
pElement->size = 0;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsRelativeKey));
if (refType)
pElement->relative = CFBooleanGetValue (refType);
else
pElement->relative = 0;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsWrappingKey));
if (refType)
pElement->wrapping = CFBooleanGetValue (refType);
else
pElement->wrapping = false;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsNonLinearKey));
if (refType)
pElement->nonLinear = CFBooleanGetValue (refType);
else
pElement->nonLinear = false;
#ifdef kIOHIDElementHasPreferredStateKey
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasPreferredStateKey));
#else // Mac OS X 10.0 has spelling error
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasPreferedStateKey));
#endif
if (refType)
pElement->preferredState = CFBooleanGetValue (refType);
else
pElement->preferredState = false;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasNullStateKey));
if (refType)
pElement->nullState = CFBooleanGetValue (refType);
else
pElement->nullState = false;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUnitKey));
if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
pElement->units = number;
else
pElement->units = 0;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUnitExponentKey));
if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
pElement->unitExp = number;
else
pElement->unitExp = 0;
refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementNameKey));
if (refType)
if (!CFStringGetCString (refType, pElement->name, 256, CFStringGetSystemEncoding ()))
HIDReportError ("CFStringGetCString error retrieving pElement->name.");
CleanString (pElement->name);
if (!*pElement->name) { // set name from vendor id/product id look up
GetElementNameFromVendorProduct (gCurrentGetDevice->vendorID, gCurrentGetDevice->productID, (long) pElement->cookie, pElement->name);
if (!*pElement->name) { // if no name
HIDGetUsageName (pElement->usagePage, pElement->usage, pElement->name);
if (!*pElement->name) // if not usage
sprintf (pElement->name, "Element");
}
}
}
// ---------------------------------
// examines CF dictionary value in device element hierarchy to determine if it is element of interest or a collection of more elements
// if element of interest allocate storage, add to list and retrieve element specific info
// if collection then pass on to deconstruction collection into additional individual elements
static void HIDAddElement (CFTypeRef refElement, pRecElement * ppElementCurrent)
{
pRecDevice pDevice = gCurrentGetDevice;
pRecElement pElement = NULL;
long elementType, usagePage, usage;
CFTypeRef refElementType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementTypeKey));
CFTypeRef refUsagePage = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUsagePageKey));
CFTypeRef refUsage = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUsageKey));
if (refElementType)
CFNumberGetValue (refElementType, kCFNumberLongType, &elementType);
if (refUsagePage)
CFNumberGetValue (refUsagePage, kCFNumberLongType, &usagePage);
if (refUsage)
CFNumberGetValue (refUsage, kCFNumberLongType, &usage);
if (NULL == pDevice)
return;
if (elementType)
{
// look at types of interest
if (elementType != kIOHIDElementTypeCollection)
{
switch (usagePage) // only interested in kHIDPage_GenericDesktop and kHIDPage_Button
{
case kHIDPage_GenericDesktop:
{
switch (usage) // look at usage to determine function
{
case kHIDUsage_GD_X:
case kHIDUsage_GD_Y:
case kHIDUsage_GD_Z:
case kHIDUsage_GD_Rx:
case kHIDUsage_GD_Ry:
case kHIDUsage_GD_Rz:
pElement = (pRecElement) malloc (sizeof (recElement));
if (pElement)
pDevice->axis++;
break;
case kHIDUsage_GD_Slider:
pElement = (pRecElement) malloc (sizeof (recElement));
if (pElement)
pDevice->sliders++;
break;
case kHIDUsage_GD_Dial:
pElement = (pRecElement) malloc (sizeof (recElement));
if (pElement)
pDevice->dials++;
break;
case kHIDUsage_GD_Wheel:
pElement = (pRecElement) malloc (sizeof (recElement));
if (pElement)
pDevice->wheels++;
break;
case kHIDUsage_GD_Hatswitch:
pElement = (pRecElement) malloc (sizeof (recElement));
if (pElement)
pDevice->hats++;
break;
}
}
break;
case kHIDPage_Button:
pElement = (pRecElement) malloc (sizeof (recElement));
if (pElement)
pDevice->buttons++;
break;
default:
// just add a generic element
pElement = (pRecElement) malloc (sizeof (recElement));
break;
}
}
else // collection
pElement = (pRecElement) malloc (sizeof (recElement));
}
else
HIDReportError ("CFNumberGetValue error when getting value for refElementType.");
if (pElement) // add to list
{
// this code builds a binary tree based on the collection hierarchy of inherent in the device element layout
// it preserves the structure of the lements as collections have children and lements are siblings to each other
// clear record
unsigned long i;
char * temp = (char *) pElement;
for (i = 0; i < sizeof (recElement); i++)
*temp++ = 0x00;
// get element info
pElement->type = elementType;
pElement->usagePage = usagePage;
pElement->usage = usage;
HIDGetElementInfo (refElement, pElement);
// count elements
pDevice->totalElements++;
switch (pElement->type)
{
case kIOHIDElementTypeInput_Misc:
case kIOHIDElementTypeInput_Button:
case kIOHIDElementTypeInput_Axis:
case kIOHIDElementTypeInput_ScanCodes:
pDevice->inputs++;
break;
case kIOHIDElementTypeOutput:
pDevice->outputs++;
break;
case kIOHIDElementTypeFeature:
pDevice->features++;
break;
case kIOHIDElementTypeCollection:
pDevice->collections++;
break;
default:
HIDReportErrorNum ("Unknown element type : ", pElement->type);
}
// if a type that is normally an axis and has a preferred state and is not relative
pElement->hasCenter = false;
if ((pElement->preferredState) && (!pElement->relative) && (pElement->usagePage == kHIDPage_GenericDesktop))
switch (pElement->usage) {
case kHIDUsage_GD_X:
case kHIDUsage_GD_Y:
case kHIDUsage_GD_Z:
case kHIDUsage_GD_Rx:
case kHIDUsage_GD_Ry:
case kHIDUsage_GD_Rz:
// case kHIDUsage_GD_Slider: // should not have center
// case kHIDUsage_GD_Dial: // should not have center
case kHIDUsage_GD_Wheel:
case kHIDUsage_GD_Hatswitch:
pElement->hasCenter = true; // respect center
pElement->initialCenter = HIDGetElementValue (pDevice, pElement);
}
if (NULL == *ppElementCurrent) // if at list head
{
pDevice->pListElements = pElement; // add current element
*ppElementCurrent = pElement; // set current element to element we just added
if (elementType == kIOHIDElementTypeCollection) // if this element is a collection of other elements
{
gfAddAsChild = true; // the next element is a child of this element
HIDGetCollectionElements ((CFMutableDictionaryRef) refElement, &pElement); // recursively process the collection
}
gfAddAsChild = false; // when returning from the recursive processing or the element is not a collection, add the next as a sibling
}
else // have existing structure
{
if (gfAddAsChild) // if the previous element was a collection, let's add this as a child of the previous
{
// this iteration should not be needed but there maybe some untested degenerate case which this code will ensure works
while ((*ppElementCurrent)->pChild) // step down tree until free child node found
*ppElementCurrent = (*ppElementCurrent)->pChild;
(*ppElementCurrent)->pChild = pElement; // insert there
}
else // add as sibling
{
// this iteration should not be needed but there maybe some untested degenerate case which this code will ensure works
while ((*ppElementCurrent)->pSibling) // step down tree until free sibling node found
*ppElementCurrent = (*ppElementCurrent)->pSibling;
(*ppElementCurrent)->pSibling = pElement; // insert there
}
pElement->pPrevious = *ppElementCurrent; // point to previous
*ppElementCurrent = pElement; // set current to our collection
if (elementType == kIOHIDElementTypeCollection) // if this element is a collection of other elements
{
gfAddAsChild = true; // add next set as children to this element
HIDGetCollectionElements ((CFMutableDictionaryRef) refElement, &pElement); // recursively process the collection
}
gfAddAsChild = false; // add next as this elements sibling (when return from a collection or with non-collections)
}
}
}
// ---------------------------------
// collects information from each array member in device element list (each array memeber = element)
static void HIDGetElementsCFArrayHandler (const void * value, void * parameter)
{
if (CFGetTypeID (value) == CFDictionaryGetTypeID ())
HIDAddElement ((CFTypeRef) value, (pRecElement *) parameter);
}
// ---------------------------------
// handles retrieval of element information from arrays of elements in device IO registry information
static void HIDGetElements (CFTypeRef refElementCurrent, pRecElement *ppCurrentElement)
{
CFTypeID type = CFGetTypeID (refElementCurrent);
if (type == CFArrayGetTypeID()) // if element is an array
{
CFRange range = {0, CFArrayGetCount (refElementCurrent)};
// CountElementsCFArrayHandler called for each array member
CFArrayApplyFunction (refElementCurrent, range, HIDGetElementsCFArrayHandler, ppCurrentElement);
}
}
// ---------------------------------
// handles extracting element information from element collection CF types
// used from top level element decoding and hierarchy deconstruction to flatten device element list
static void HIDGetCollectionElements (CFMutableDictionaryRef deviceProperties, pRecElement *ppCurrentCollection)
{
CFTypeRef refElementTop = CFDictionaryGetValue (deviceProperties, CFSTR(kIOHIDElementKey));
if (refElementTop)
HIDGetElements (refElementTop, ppCurrentCollection);
else
HIDReportError ("CFDictionaryGetValue error when creating CFTypeRef for kIOHIDElementKey.");
}
// ---------------------------------
// use top level element usage page and usage to discern device usage page and usage setting appropriate values in device record
static void HIDTopLevelElementHandler (const void * value, void * parameter)
{
CFTypeRef refCF = 0;
if ((NULL == value) || (NULL == parameter))
return; // (paramErr)
if (CFGetTypeID (value) != CFDictionaryGetTypeID ())
return;
refCF = CFDictionaryGetValue (value, CFSTR(kIOHIDElementUsagePageKey));
if (!CFNumberGetValue (refCF, kCFNumberLongType, &((pRecDevice) parameter)->usagePage))
HIDReportError ("CFNumberGetValue error retrieving pDevice->usagePage.");
refCF = CFDictionaryGetValue (value, CFSTR(kIOHIDElementUsageKey));
if (!CFNumberGetValue (refCF, kCFNumberLongType, &((pRecDevice) parameter)->usage))
HIDReportError ("CFNumberGetValue error retrieving pDevice->usage.");
}
// ---------------------------------
// extracts device info from CF dictionary records in IO registry
static void HIDGetDeviceInfo (io_object_t hidDevice, CFMutableDictionaryRef hidProperties, pRecDevice pDevice)
{
CFMutableDictionaryRef usbProperties = 0;
io_registry_entry_t parent1, parent2;
// Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
// get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
if ((KERN_SUCCESS == IORegistryEntryGetParentEntry (hidDevice, kIOServicePlane, &parent1)) &&
(KERN_SUCCESS == IORegistryEntryGetParentEntry (parent1, kIOServicePlane, &parent2)) &&
(KERN_SUCCESS == IORegistryEntryCreateCFProperties (parent2, &usbProperties, kCFAllocatorDefault, kNilOptions)))
{
if (usbProperties)
{
CFTypeRef refCF = 0;
// get device info
// try hid dictionary first, if fail then go to usb dictionary
// get transport
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDTransportKey));
if (refCF)
{
if (!CFStringGetCString (refCF, pDevice->transport, 256, CFStringGetSystemEncoding ()))
HIDReportError ("CFStringGetCString error retrieving pDevice->transport.");
CleanString (pDevice->transport);
}
// get vendorID
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDVendorIDKey));
if (!refCF)
refCF = CFDictionaryGetValue (usbProperties, CFSTR("idVendor"));
if (refCF)
{
if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->vendorID))
HIDReportError ("CFNumberGetValue error retrieving pDevice->vendorID.");
}
// get product ID
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductIDKey));
if (!refCF)
refCF = CFDictionaryGetValue (usbProperties, CFSTR("idProduct"));
if (refCF)
{
if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->productID))
HIDReportError ("CFNumberGetValue error retrieving pDevice->productID.");
}
// get product version
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDVersionNumberKey));
if (refCF)
{
if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->version))
HIDReportError ("CFNumberGetValue error retrieving pDevice->version.");
}
// get manufacturer name
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDManufacturerKey));
if (!refCF)
refCF = CFDictionaryGetValue (usbProperties, CFSTR("USB Vendor Name"));
if (refCF)
{
if (!CFStringGetCString (refCF, pDevice->manufacturer, 256, CFStringGetSystemEncoding ()))
HIDReportError ("CFStringGetCString error retrieving pDevice->manufacturer.");
CleanString (pDevice->manufacturer);
}
// get product name
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductKey));
if (!refCF)
refCF = CFDictionaryGetValue (usbProperties, CFSTR("USB Product Name"));
if (refCF)
{
if (!CFStringGetCString (refCF, pDevice->product, 256, CFStringGetSystemEncoding ()))
HIDReportError ("CFStringGetCString error retrieving pDevice->product.");
CleanString (pDevice->product);
}
// get serial
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDSerialNumberKey));
if (refCF)
{
if (!CFStringGetCString (refCF, pDevice->serial, 256, CFStringGetSystemEncoding ()))
HIDReportError ("CFStringGetCString error retrieving pDevice->serial.");
CleanString (pDevice->serial);
}
// get location ID
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDLocationIDKey));
if (!refCF)
refCF = CFDictionaryGetValue (usbProperties, CFSTR("locationID"));
if (refCF)
{
if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->locID))
HIDReportError ("CFNumberGetValue error retrieving pDevice->locID.");
}
// get usage page and usage
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
if (refCF)
{
if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usagePage))
HIDReportError ("CFNumberGetValue error retrieving pDevice->usagePage.");
refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
if (refCF)
if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usage))
HIDReportError ("CFNumberGetValue error retrieving pDevice->usage.");
}
if (NULL == refCF) // get top level element HID usage page or usage
{
// use top level element instead
CFTypeRef refCFTopElement = 0;
refCFTopElement = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
{
// refCFTopElement points to an array of element dictionaries
CFRange range = {0, CFArrayGetCount (refCFTopElement)};
CFArrayApplyFunction (refCFTopElement, range, HIDTopLevelElementHandler, NULL);
}
}
}
else
HIDReportError ("IORegistryEntryCreateCFProperties failed to create usbProperties.");
CFRelease (usbProperties);
if (kIOReturnSuccess != IOObjectRelease (parent2))
HIDReportError ("IOObjectRelease error with parent2.");
if (kIOReturnSuccess != IOObjectRelease (parent1))
HIDReportError ("IOObjectRelease error with parent1.");
}
}
// ---------------------------------
// adds device to linked list of devices passed in (handles NULL lists properly)
static void HIDAddDevice (pRecDevice *ppListDeviceHead, pRecDevice pNewDevice)
{
if (NULL == *ppListDeviceHead)
*ppListDeviceHead = pNewDevice;
else
{
pRecDevice pDevicePrevious = NULL, pDevice = *ppListDeviceHead;
while (pDevice)
{
pDevicePrevious = pDevice;
pDevice = pDevicePrevious->pNext;
}
pDevicePrevious->pNext = pNewDevice;
}
pNewDevice->pNext = NULL;
}
// ---------------------------------
// adds device to linked list of devices passed in (handles NULL lists properly)
// returns next device is old list (for properly iterating)
static pRecDevice HIDMoveDevice (pRecDevice *ppListDeviceHead, pRecDevice pNewDevice, pRecDevice *ppOldListDeviceHead)
{
pRecDevice pDeviceNext = NULL;
if (!pNewDevice || !ppOldListDeviceHead || !ppListDeviceHead) { // handle NULL pointers
HIDReportError ("HIDMoveDevice, NULL input error.");
return pDeviceNext;
}
// remove from old
if (pNewDevice == *ppOldListDeviceHead) { // replacing head
*ppOldListDeviceHead = pNewDevice->pNext;
pDeviceNext = *ppOldListDeviceHead;
} else {
pRecDevice pDevicePrevious = NULL, pDevice = *ppOldListDeviceHead;
while (pDevice && (pDevice != pNewDevice)) { // step through list until match or end
pDevicePrevious = pDevice;
pDevice = pDevicePrevious->pNext;
}
if (pDevice == pNewDevice) { // if there was a match
pDevicePrevious->pNext = pDevice->pNext; // skip this device
pDeviceNext = pDevice->pNext;
} else
HIDReportError ("HIDMoveDevice device not found when moving.");
}
// add to new list
HIDAddDevice (ppListDeviceHead, pNewDevice);
return pDeviceNext; // return next device
}
// ---------------------------------
// given a IO device object build a flat device record including device info and elements
static pRecDevice HIDBuildDevice (io_object_t hidDevice)
{
pRecDevice pDevice = (pRecDevice) malloc (sizeof (recDevice));
if (pDevice)
{
unsigned long i;
// get dictionary for HID properties
CFMutableDictionaryRef hidProperties = 0;
kern_return_t result = IORegistryEntryCreateCFProperties (hidDevice, &hidProperties, kCFAllocatorDefault, kNilOptions);
// clear record
for (i = 0; i < sizeof (recDevice); i++) // clear array
*((char *) pDevice + i) = 0x00;
if ((result == KERN_SUCCESS) && hidProperties)
{
pRecElement pCurrentElement = NULL;
// create device interface
result = HIDCreateOpenDeviceInterface (hidDevice, pDevice);
if (kIOReturnSuccess != result)
HIDReportErrorNum ("HIDCreateOpenDeviceInterface failed.", result);
HIDGetDeviceInfo (hidDevice, hidProperties, pDevice); // hidDevice used to find parents in registry tree
// set current device for use in getting elements
gCurrentGetDevice = pDevice;
HIDGetCollectionElements (hidProperties, &pCurrentElement);
gCurrentGetDevice = NULL;
CFRelease (hidProperties);
}
else
HIDReportErrorNum ("IORegistryEntryCreateCFProperties error when creating deviceProperties.", result);
}
else
HIDReportError ("malloc error when allocating pRecDevice.");
return pDevice;
}
// ---------------------------------
// build flat linked list of devices from device iterator
static pRecDevice HIDCreateSingleTypeDeviceList (io_iterator_t hidObjectIterator)
{
IOReturn result = kIOReturnSuccess;
pRecDevice pListDeviceHead = NULL;
pRecDevice pNewDevice = NULL;
io_object_t ioHIDDeviceObject = NULL;
while ((ioHIDDeviceObject = IOIteratorNext (hidObjectIterator)))
{
pNewDevice = HIDBuildDevice (ioHIDDeviceObject);
// dump device object, it is no longer needed
result = IOObjectRelease (ioHIDDeviceObject);
if (KERN_SUCCESS != result)
HIDReportErrorNum ("IOObjectRelease error with ioHIDDeviceObject.", result);
HIDAddDevice (&pListDeviceHead, pNewDevice);
}
result = IOObjectRelease (hidObjectIterator); // release the iterator
if (kIOReturnSuccess != result)
HIDReportErrorNum ("IOObjectRelease error with hidObjectIterator.", result);
return pListDeviceHead;
}
// ---------------------------------