-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing3.html
executable file
·1128 lines (980 loc) · 45.4 KB
/
listing3.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>HID Manager Basics - /HID_Manager_Test.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/Games/index.html">Games</a> > <a href="../../samplecode/Games/idxHumanInterfaceDeviceForceFeedback-date.html">Human Interface Device & Force Feedback</a> > <A HREF="javascript:location.replace('index.html');">HID Manager Basics</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">HID Manager Basics</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_Manager_Test.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">/Carbon_Include.h</option>
<option value="listing2.html">/Carbon_Main.c</option>
<option value="listing3.html">/HID_Manager_Test.c</option>
<option value="listing4.html">/HID_Manager_Test.h</option></select>
</p>
</form>
<p><strong><a href="HID_Manager_Basics.zip">Download Sample</a></strong> (“HID_Manager_Basics.zip”, 23.9K)<BR>
<strong><a href="HID_Manager_Basics.dmg">Download Sample</a></strong> (“HID_Manager_Basics.dmg”, 81.5K)</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">/*
File: HID Manager Test.c
Contains: Basic HID Manager Test Code
DRI: George Warner
Copyright: Copyright © 2002 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.
*/
// system includes ----------------------------------------------------------
#ifndef __APPLE_CC__
#include "Carbon_Include.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/errno.h>
#include <sysexits.h>
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDUsageTables.h>
#include <IOKit/usb/USB.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h>
// project includes ---------------------------------------------------------
#include "HID_Manager_Test.h"
// prototypes ---------------------------------------------------------------
void PrintErrMsgIfIOErr (long expr, char * msg);
void PrintErrMsgIfErr (long expr, char * msg);
static CFMutableDictionaryRef MySetUpHIDMatchingDictionary (UInt32 usagePage, UInt32 usage);
static io_iterator_t MyFindHIDDevices (const mach_port_t masterPort, UInt32 usagePage, UInt32 usage);
static void MyIncrementElementIndex(CFIndex increment);
static void MyIndent ();
static void MyStartBlock (Boolean printIndex);
static void MyEndBlock ();
static CFIndex MyGetElementIndex ();
static void MySetElementIndex (CFIndex newIndex);
static void MyCFArrayShow(CFArrayRef object);
static void MyCFArrayShowApplier (const void * value, void * parameter);
static void MyCFBooleanShow(CFBooleanRef object);
static void MyStoreImportantCookies (CFDictionaryRef element);
static void MyShowTypeElement (CFDictionaryRef dictionary);
static void MyShowUsageAndPageElement (CFDictionaryRef dictionary);
static void MyCFDictionaryShow(CFDictionaryRef object);
static void MyCFNumberShow (CFNumberRef object);
static void MyCFStringShow(CFStringRef object);
static void MyCFObjectShow(CFTypeRef object);
static void MyShowProperty(const void * key, const void * value);
static Boolean MyShowDictionaryElement (CFDictionaryRef dictionary, CFStringRef key);
static void MyShowHIDProperties (io_registry_entry_t hidDevice);
static IOHIDDeviceInterface ** MyCreateHIDDeviceInterface (io_object_t hidDevice);
static void MyTestEventInterface (IOHIDDeviceInterface **hidDeviceInterface);
static void MyTestPollingInterface (IOHIDDeviceInterface ** hidDeviceInterface);
// statics/globals (internal only) ------------------------------------------
static CFIndex gNestingLevel = 0;
static CFIndex gElementIndex = 0;
static IOHIDElementCookie gXAxisCookie = 0;
static IOHIDElementCookie gButton1Cookie = 0;
static IOHIDElementCookie gButton2Cookie = 0;
static IOHIDElementCookie gButton3Cookie = 0;
// functions (internal/private) ---------------------------------------------
void PrintErrMsgIfIOErr (long expr, char * msg)
{
IOReturn err = (expr);
if (err != kIOReturnSuccess)
{
fprintf (stderr, "%s - %s(%x,%d)\n",
msg, mach_error_string (err), err, err & 0xffffff);
fflush(stderr);
DebugStr ("\pExecution Halted");
}
}
// --------------------------------------------------------------------------
void PrintErrMsg (char * msg)
{
fprintf (stderr, "%s\n", msg);
fflush (stderr);
DebugStr ("\pExecution Halted");
}
// --------------------------------------------------------------------------
static CFMutableDictionaryRef MySetUpHIDMatchingDictionary (UInt32 usagePage, UInt32 usage)
{
CFNumberRef refUsage = NULL, refUsagePage = NULL;
CFMutableDictionaryRef refHIDMatchDictionary = NULL;
// Set up a matching dictionary to search I/O Registry by class name for all HID class devices.
refHIDMatchDictionary = IOServiceMatching (kIOHIDDeviceKey);
if (refHIDMatchDictionary != NULL)
{
// Add key for device type (joystick, in this case) to refine the matching dictionary.
refUsagePage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usagePage);
refUsage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usage);
CFDictionarySetValue (refHIDMatchDictionary, CFSTR (kIOHIDPrimaryUsagePageKey), refUsagePage);
CFDictionarySetValue (refHIDMatchDictionary, CFSTR (kIOHIDPrimaryUsageKey), refUsage);
}
else
PrintErrMsg ("Failed to get HID CFMutableDictionaryRef via IOServiceMatching.");
return refHIDMatchDictionary;
}
// --------------------------------------------------------------------------
static io_iterator_t MyFindHIDDevices (const mach_port_t masterPort, UInt32 usagePage, UInt32 usage)
{
CFMutableDictionaryRef hidMatchDictionary = NULL;
IOReturn ioReturnValue = kIOReturnSuccess;
io_iterator_t hidObjectIterator;
// Set up matching dictionary to search the I/O Registry for HID devices we are interested in. Dictionary reference is NULL if error.
hidMatchDictionary = MySetUpHIDMatchingDictionary (usagePage, usage);
if (NULL == hidMatchDictionary)
PrintErrMsg ("Couldn't create a matching dictionary.");
// Now search I/O Registry for matching devices.
ioReturnValue = IOServiceGetMatchingServices (masterPort, hidMatchDictionary, &hidObjectIterator);
// If error, print message and hang (for debugging purposes).
if ((ioReturnValue != kIOReturnSuccess) | (hidObjectIterator == NULL))
PrintErrMsg ("Couldn't create a HID object iterator.");
// IOServiceGetMatchingServices consumes a reference to the dictionary, so we don't need to release the dictionary ref.
hidMatchDictionary = NULL;
return hidObjectIterator;
}
// --------------------------------------------------------------------------
static void MyIncrementElementIndex(CFIndex increment)
{
gElementIndex += increment;
}
// --------------------------------------------------------------------------
static void MyIndent ()
{
int i;
printf("\n");
for (i = 0; i < gNestingLevel; i++)
printf (" ");
}
// --------------------------------------------------------------------------
static void MyStartBlock (Boolean printIndex)
{
if (printIndex)
{
MyIndent ();
printf ("[%ld]", MyGetElementIndex());
}
gNestingLevel++;
}
// --------------------------------------------------------------------------
static void MyEndBlock ()
{
gNestingLevel--;
}
// --------------------------------------------------------------------------
static CFIndex MyGetElementIndex ()
{
return gElementIndex;
}
// --------------------------------------------------------------------------
static void MySetElementIndex (CFIndex newIndex)
{
gElementIndex = newIndex;
}
// --------------------------------------------------------------------------
static void MyCFArrayShow(CFArrayRef object)
{
CFRange range = {0, CFArrayGetCount (object)};
CFIndex savedIndex = MyGetElementIndex ();
//Show an element array containing one or more element dictionaries
MySetElementIndex (0); //Reset index to zero
MyStartBlock (FALSE);
CFArrayApplyFunction (object, range, MyCFArrayShowApplier, 0);
MyEndBlock ();
MySetElementIndex (savedIndex); //Restore original index
}
// --------------------------------------------------------------------------
static void MyCFArrayShowApplier (const void * value, void * parameter)
{
if (CFGetTypeID (value) != CFDictionaryGetTypeID ())
return;
MyCFObjectShow(value);
}
// --------------------------------------------------------------------------
static void MyCFBooleanShow(CFBooleanRef object)
{
printf(CFBooleanGetValue(object) ? "true" : "false");
}
// --------------------------------------------------------------------------
static void MyStoreImportantCookies (CFDictionaryRef element)
{
CFTypeRef object;
long number;
IOHIDElementCookie cookie;
long usage;
long usagePage;
//Get cookie
object = CFDictionaryGetValue (element, CFSTR(kIOHIDElementCookieKey));
if (object == 0 || CFGetTypeID (object) != CFNumberGetTypeID ())
return;
if(!CFNumberGetValue ((CFNumberRef) object, kCFNumberLongType, &number))
return;
cookie = (IOHIDElementCookie) number;
//Get usage
object = CFDictionaryGetValue (element, CFSTR(kIOHIDElementUsageKey));
if (object == 0 || CFGetTypeID (object) != CFNumberGetTypeID ())
return;
if (!CFNumberGetValue ((CFNumberRef) object, kCFNumberLongType, &number))
return;
usage = number;
//Get usage page
object = CFDictionaryGetValue (element,CFSTR(kIOHIDElementUsagePageKey));
if (object == 0 || CFGetTypeID (object) != CFNumberGetTypeID ())
return;
if (!CFNumberGetValue ((CFNumberRef) object, kCFNumberLongType, &number))
return;
usagePage = number;
//Check for x axis
if (usage == kHIDUsage_GD_X && usagePage == kHIDPage_GenericDesktop)
gXAxisCookie = cookie;
//Check for buttons
else if (usage == kHIDUsage_Button_1 && usagePage == kHIDPage_Button)
gButton1Cookie = cookie;
else if (usage == kHIDUsage_Button_2 && usagePage == kHIDPage_Button)
gButton2Cookie = cookie;
else if (usage == kHIDUsage_Button_3 && usagePage == kHIDPage_Button)
gButton3Cookie = cookie;
}
// --------------------------------------------------------------------------
static void MyShowTypeElement (CFDictionaryRef dictionary)
{
CFStringRef keyType = CFSTR(kIOHIDElementTypeKey);
CFTypeRef objectType = CFDictionaryGetValue (dictionary, keyType);
long valueType = 0;
if (objectType)
{
if (CFNumberGetValue (objectType, kCFNumberLongType, &valueType))
{
MyIndent ();
switch (valueType)
{
case kIOHIDElementTypeInput_Misc:
printf("Miscellaneous Input Type (%ld), ", valueType);
break;
case kIOHIDElementTypeInput_Button:
printf("Button Input Type (%ld), ", valueType);
break;
case kIOHIDElementTypeInput_Axis:
printf("Axis Input Type (%ld), ", valueType);
break;
case kIOHIDElementTypeInput_ScanCodes:
printf("Scan Code Input Type (%ld), ", valueType);
break;
case kIOHIDElementTypeOutput:
printf("Output Type (%ld), ", valueType);
break;
case kIOHIDElementTypeFeature:
printf("Feature Type (%ld), ", valueType);
break;
case kIOHIDElementTypeCollection:
printf("Collection Type (%ld), ", valueType);
break;
}
}
}
}
// --------------------------------------------------------------------------
static void MyShowUsageAndPageElement (CFDictionaryRef dictionary)
{
CFStringRef keyUsagePage = CFSTR(kIOHIDElementUsagePageKey);
CFStringRef keyUsage = CFSTR(kIOHIDElementUsageKey);
CFTypeRef objectUsagePage = CFDictionaryGetValue (dictionary, keyUsagePage);
CFTypeRef objectUsage = CFDictionaryGetValue (dictionary, keyUsage);
long valueUsage = 0, valueUsagePage = 0;
if (objectUsagePage && objectUsage)
{
if (CFNumberGetValue (objectUsage, kCFNumberLongType, &valueUsage) && CFNumberGetValue (objectUsagePage, kCFNumberLongType, &valueUsagePage))
{
MyIndent ();
printf ("Usage: ");
switch (valueUsagePage)
{
case kHIDPage_Undefined:
printf("Undefined Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default:
printf("Undefined Usage (0x%lx)", valueUsage);
break;
}
break;
case kHIDPage_GenericDesktop:
printf("Generic Desktop (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
case kHIDUsage_GD_Pointer: printf("Pointer (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Mouse: printf("Mouse (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Joystick: printf("Joystick (0x%lx)", valueUsage); break;
case kHIDUsage_GD_GamePad: printf("GamePad (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Keyboard: printf("Keyboard (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Keypad: printf("Keypad (0x%lx)", valueUsage); break;
case kHIDUsage_GD_MultiAxisController: printf("Multi-Axis Controller (0x%lx)", valueUsage); break;
case kHIDUsage_GD_X: printf("X-Axis (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Y: printf("Y-Axis (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Z: printf("Z-Axis (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Rx: printf("X-Rotation (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Ry: printf("Y-Rotation (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Rz: printf("Z-Rotation (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Slider: printf("Slider (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Dial: printf("Dial (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Wheel: printf("Wheel (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Hatswitch: printf("Hatswitch (0x%lx)", valueUsage); break;
case kHIDUsage_GD_CountedBuffer: printf("Counted Buffer (0x%lx)", valueUsage); break;
case kHIDUsage_GD_ByteCount: printf("Byte Count (0x%lx)", valueUsage); break;
case kHIDUsage_GD_MotionWakeup: printf("Motion Wakeup (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Start: printf("Start (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Select: printf("Select (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Vx: printf("X-Velocity (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Vy: printf("Y-Velocity (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Vz: printf("Z-Velocity (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Vbrx: printf("X-Rotation Velocity (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Vbry: printf("Y-Rotation Velocity (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Vbrz: printf("Z-Rotation Velocity (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Vno: printf("Vno (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemControl: printf("System Control (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemPowerDown: printf("System Power Down (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemSleep: printf("System Sleep (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemWakeUp: printf("System Wake Up (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemContextMenu: printf("System Context Menu (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemMainMenu: printf("System Main Menu (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemAppMenu: printf("System App Menu (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemMenuHelp: printf("System Menu Help (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemMenuExit: printf("System Menu Exit (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemMenu: printf("System Menu (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemMenuRight: printf("System Menu Right (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemMenuLeft: printf("System Menu Left (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemMenuUp: printf("System Menu Up (0x%lx)", valueUsage); break;
case kHIDUsage_GD_SystemMenuDown: printf("System Menu Down (0x%lx)", valueUsage); break;
case kHIDUsage_GD_DPadUp: printf("DPad Up (0x%lx)", valueUsage); break;
case kHIDUsage_GD_DPadDown: printf("DPad Down (0x%lx)", valueUsage); break;
case kHIDUsage_GD_DPadRight: printf("DPad Right (0x%lx)", valueUsage); break;
case kHIDUsage_GD_DPadLeft: printf("DPad Left (0x%lx)", valueUsage); break;
case kHIDUsage_GD_Reserved: printf("Reserved (0x%lx)", valueUsage); break;
default: printf("Undefined Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Simulation:
printf("Simulation Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_VR:
printf("VR Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Sport:
printf("Sport Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Game:
printf("Game Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_KeyboardOrKeypad:
printf("Keyboard or Keypad Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Button:
printf("Button Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Button #%ld (0x%lx)", valueUsage, valueUsage); break;
}
break;
case kHIDPage_Ordinal:
printf("Ordinal Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Telephony:
printf("Telephony Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Consumer:
printf("Consumer Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Digitizer:
printf("Digitizer Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_PID:
printf("PID Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Unicode:
printf("Unicode Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_AlphanumericDisplay:
printf("Alphanumeric Display Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_BarCodeScanner:
printf("Bar Code Scanner Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Scale:
printf("Scale Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_CameraControl:
printf("Camera Control Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
case kHIDPage_Arcade:
printf("Arcade Page (0x%lx), ", valueUsagePage);
switch (valueUsage)
{
default: printf("Usage (0x%lx)", valueUsage); break;
}
break;
default:
printf("Unknown Page (0x%lx), ", valueUsagePage);
printf("Usage (0x%lx)", valueUsage);
break;
}
}
}
}
// --------------------------------------------------------------------------
static void MyCFDictionaryShow(CFDictionaryRef object)
{
MyStartBlock(TRUE);
MyShowDictionaryElement (object, CFSTR(kIOHIDElementCookieKey));
MyShowTypeElement (object);
MyShowDictionaryElement (object, CFSTR(kIOHIDElementCollectionTypeKey));
MyShowUsageAndPageElement (object);
MyShowDictionaryElement (object, CFSTR(kIOHIDElementMinKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementMaxKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementScaledMinKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementScaledMaxKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementSizeKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementIsRelativeKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementIsWrappingKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementIsNonLinearKey));
#ifdef kIOHIDElementHasPreferredStateKey
MyShowDictionaryElement (object, CFSTR(kIOHIDElementHasPreferredStateKey));
#else
MyShowDictionaryElement (object, CFSTR(kIOHIDElementHasPreferedStateKey));
#endif
MyShowDictionaryElement (object, CFSTR(kIOHIDElementHasNullStateKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementVendorSpecificKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementUnitKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementUnitExponentKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementNameKey));
MyShowDictionaryElement (object, CFSTR(kIOHIDElementKey));
fflush (stdout);
MyStoreImportantCookies (object);
//Store specific elements in global variables for use in queue handling functions.
MyEndBlock ();
MyIncrementElementIndex (1);
}
// --------------------------------------------------------------------------
static void MyCFNumberShow (CFNumberRef object)
{
long number;
if (CFNumberGetValue (object, kCFNumberLongType, &number))
printf("0x%lx (%ld)", number, number);
}
// --------------------------------------------------------------------------
static void MyCFStringShow(CFStringRef object)
{
const char * c = CFStringGetCStringPtr (object, CFStringGetSystemEncoding ());
if (c)
printf ("%s", c);
else
{
CFIndex bufferSize = CFStringGetLength (object) + 1;
char * buffer = (char *)malloc (bufferSize);
if (buffer)
{
if (CFStringGetCString (object, buffer, bufferSize, CFStringGetSystemEncoding ()))
printf ("%s", buffer);
free(buffer);
}
}
}
// --------------------------------------------------------------------------
static void MyCFObjectShow (CFTypeRef object)
{
CFTypeID type = CFGetTypeID(object);
if (type == CFArrayGetTypeID())
MyCFArrayShow(object);
else if (type == CFBooleanGetTypeID())
MyCFBooleanShow(object);
else if (type == CFDictionaryGetTypeID())
MyCFDictionaryShow(object);
else if (type == CFNumberGetTypeID())
MyCFNumberShow(object);
else if (type == CFStringGetTypeID())
MyCFStringShow(object);
else
printf("<unknown object>");
}
// --------------------------------------------------------------------------
static void MyShowProperty(const void * key, const void * value)
{
//Both parameters are references to CF objects
MyIndent ();
MyCFStringShow (key);
printf (" = ");
MyCFObjectShow (value);
}
// --------------------------------------------------------------------------
static Boolean MyShowDictionaryElement (CFDictionaryRef dictionary, CFStringRef key)
{
CFTypeRef object = CFDictionaryGetValue (dictionary, key);
if (object)
MyShowProperty (key,object);
return (object != NULL);
}
// --------------------------------------------------------------------------
static void MyShowHIDProperties (io_registry_entry_t hidDevice)
{
kern_return_t result;
CFMutableDictionaryRef properties = 0;
char path[512];
result = IORegistryEntryGetPath (hidDevice, kIOServicePlane, path);
if (result == KERN_SUCCESS)
printf("IO Registry Path: [ %s ]\n", path);
//Create a CF dictionary representation of the I/O Registry entry's properties
result = IORegistryEntryCreateCFProperties (hidDevice, &properties, kCFAllocatorDefault, kNilOptions);
if ((result == KERN_SUCCESS) && properties)
{
printf ("- Device Properties -\n");
MyShowDictionaryElement (properties, CFSTR(kIOHIDTransportKey));
MyShowDictionaryElement (properties, CFSTR(kIOHIDVendorIDKey));
MyShowDictionaryElement (properties, CFSTR(kIOHIDProductIDKey));
MyShowDictionaryElement (properties, CFSTR(kIOHIDVersionNumberKey));
MyShowDictionaryElement (properties, CFSTR(kIOHIDManufacturerKey));
MyShowDictionaryElement (properties, CFSTR(kIOHIDProductKey));
MyShowDictionaryElement (properties, CFSTR(kIOHIDSerialNumberKey));
if (!MyShowDictionaryElement (properties, CFSTR(kIOHIDLocationIDKey)))
MyShowDictionaryElement (properties, CFSTR(kUSBDevicePropertyLocationID));
MyShowDictionaryElement (properties, CFSTR(kIOHIDPrimaryUsageKey));
MyShowDictionaryElement (properties, CFSTR(kIOHIDPrimaryUsagePageKey));
MyShowDictionaryElement (properties, CFSTR("idVendor"));
MyShowDictionaryElement (properties, CFSTR("USB Product Name"));
MyShowDictionaryElement (properties, CFSTR("idProduct"));
fflush (stdout);
printf ("- Device Element Properties -\n");
MyShowDictionaryElement (properties, CFSTR(kIOHIDElementKey));
//Release the properties dictionary
CFRelease (properties);
}
else
PrintErrMsg ("Failed to create properties via IORegistryEntryCreateCFProperties.");
printf ("\n\n");
}
// --------------------------------------------------------------------------
static IOHIDDeviceInterface ** MyCreateHIDDeviceInterface (io_object_t hidDevice)
{
io_name_t className;
IOCFPlugInInterface **plugInInterface = NULL;
HRESULT plugInResult = S_OK;
SInt32 score = 0;
IOReturn ioReturnValue = kIOReturnSuccess;
IOHIDDeviceInterface ** pphidDeviceInterface = NULL;
ioReturnValue = IOObjectGetClass (hidDevice, className);
PrintErrMsgIfIOErr (ioReturnValue, "Failed to get class name.");
printf ("Creating interface for device of class %s\n\n", className);
ioReturnValue = IOCreatePlugInInterfaceForService (hidDevice, kIOHIDDeviceUserClientTypeID,
kIOCFPlugInInterfaceID, &plugInInterface, &score);
if (ioReturnValue == kIOReturnSuccess)
{
// Call a method of the intermediate plug-in to create the device interface
plugInResult = (*plugInInterface)->QueryInterface (plugInInterface,
CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID), (void *) &pphidDeviceInterface);
if (plugInResult != S_OK)
PrintErrMsg ("Couldn't query HID class device interface from plugInInterface");
(*plugInInterface)->Release (plugInInterface);
}
else
PrintErrMsg ("Failed to create **plugInInterface via IOCreatePlugInInterfaceForService.");
return pphidDeviceInterface;
}
// --------------------------------------------------------------------------
#define TEST_EVENT_CALLBACK TRUE
#if TEST_EVENT_CALLBACK
static UInt32 gCallback_count = 0;
static void MyIOHIDCallbackFunction(void * target, IOReturn result, void * refcon, void * sender)
{
gCallback_count++;
//if (0 == (gCallback_count % 32))
{
printf("MyIOHIDCallbackFunction - count = %ld.\n",gCallback_count);
fflush(stdout);
}
result = kIOReturnSuccess;
}
#endif TEST_EVENT_CALLBACK
// --------------------------------------------------------------------------
static void MyTestEventInterface (IOHIDDeviceInterface **hidDeviceInterface)
{
HRESULT result;
IOHIDQueueInterface ** queue;
Boolean hasElement;
IOHIDEventStruct event;
queue = (*hidDeviceInterface)->allocQueue (hidDeviceInterface);
printf("Queue allocated: %lx\n", (long) queue);
if (queue)
{
//create the queue
result = (*queue)->create (queue, 0, /* flag?? */ 8);/* depth: maximum number of elements
in queue before oldest elements in
queue begin to be lost*/
printf ("Queue create result: %lx\n", result);
//add elements to the queue
result = (*queue)->addElement (queue, gXAxisCookie, 0);
printf ("Queue added x axis result: %lx\n", result);
result = (*queue)->addElement (queue, gButton1Cookie, 0);
printf ("Queue added button 1 result: %lx\n", result);
result = (*queue)->addElement (queue, gButton2Cookie, 0);
printf ("Queue added button 2 result: %lx\n", result);
result = (*queue)->addElement (queue, gButton3Cookie, 0);
printf ("Queue added button 3 result: %lx\n", result);
//check to see if button 3 is in queue
hasElement = (*queue)->hasElement (queue,gButton3Cookie);
printf ("queue has button 3 result: %s\n", hasElement ? "true" : "false");
//remove button 3 from queue
result = (*queue)->removeElement (queue, gButton3Cookie);
printf ("queue remove button 3 result: %lx\n",result);
#if TEST_EVENT_CALLBACK
{
CFRunLoopSourceRef tCFRunLoopSourceRef = NULL;
result = (*queue)->createAsyncEventSource(queue, &tCFRunLoopSourceRef);
if (kIOReturnSuccess != result)
printf ("Failed to createAsyncEventSource, error: %ld.\n", result);
// if we have one now\xC9
if (NULL != tCFRunLoopSourceRef)
{
CFRunLoopRef tCFRunLoopRef = (CFRunLoopRef) GetCFRunLoopFromEventLoop(GetMainEventLoop());
// and it's not already attached to our runloop\xC9
if (!CFRunLoopContainsSource(tCFRunLoopRef, tCFRunLoopSourceRef, kCFRunLoopDefaultMode))
// then attach it now.
CFRunLoopAddSource(tCFRunLoopRef, tCFRunLoopSourceRef, kCFRunLoopDefaultMode);
}
// now install our callback
result = (*queue)->setEventCallout(queue, MyIOHIDCallbackFunction, queue, queue);
if (kIOReturnSuccess != result)
printf ("Failed to setEventCallout, error: %ld.\n", result);
#endif TEST_EVENT_CALLBACK
//start data delivery to queue
result = (*queue)->start (queue);
printf ("queue start result: %lx\n", result);
//check queue a few times to see accumulated events
printf ("Testing Event Interface (Mouse button to continue)...\n");
fflush (stdout);
while (Button()) {}
while (!Button())
{
AbsoluteTime zeroTime = {0,0};
result = (*queue)->getNextEvent (queue, &event, zeroTime, 0);
if (result == kIOReturnUnderrun)
{
// print nothing queue empty
}
else if (result != kIOReturnSuccess)
printf ("queue getNextEvent result error: %lx\n", result);
else
printf ("queue: event:[%lx] %ld\n", (unsigned long) event.elementCookie, event.value);
fflush (stdout);
#if TEST_EVENT_CALLBACK
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.25f, true);
#endif TEST_EVENT_CALLBACK
}
//stop data delivery to queue
result = (*queue)->stop (queue);
printf ("queue stop result: %lx\n", result);
#if TEST_EVENT_CALLBACK
// see if we have an Async event source for this queue\xC9
tCFRunLoopSourceRef = (*queue)->getAsyncEventSource(queue);
if (NULL != tCFRunLoopSourceRef) // if so then\xC9
{
CFRunLoopRef tCFRunLoopRef = (CFRunLoopRef) GetCFRunLoopFromEventLoop(GetMainEventLoop());
// if it's attached to our runloop\xC9
if (CFRunLoopContainsSource(tCFRunLoopRef, tCFRunLoopSourceRef, kCFRunLoopDefaultMode))
// then remove it
CFRunLoopRemoveSource(tCFRunLoopRef, tCFRunLoopSourceRef, kCFRunLoopDefaultMode);
// now release it.
CFRelease(tCFRunLoopSourceRef);
}
}
#endif TEST_EVENT_CALLBACK
//dispose of queue
result = (*queue)->dispose (queue);
//release the queue we allocated
result = (*queue)->Release (queue);
printf ("queue release result: %lx\n", result);
fflush (stdout);
}
}
// --------------------------------------------------------------------------
static void MyTestPollingInterface (IOHIDDeviceInterface ** hidDeviceInterface)
{
HRESULT result;
IOHIDEventStruct hidEvent;
printf ("Testing Polling Interface (Mouse button to continue)...\n");
printf("X Axis (%lx), Button 1 (%lx), Button 2 (%lx), Button 3 (%lx)\n",
(long) gXAxisCookie, (long) gButton1Cookie, (long) gButton2Cookie, (long) gButton3Cookie);
while (Button()) {}
while (!Button())
{
long xAxis, button1, button2, button3;
//Get x axis
result = (*hidDeviceInterface)->getElementValue(hidDeviceInterface, gXAxisCookie, &hidEvent);
if (result)