-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing2.html
executable file
·1148 lines (1004 loc) · 51.6 KB
/
listing2.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 Explorer - /HID Utilities/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/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 Explorer</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 Explorer</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 Utilities/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">/HID Explorer-Read Me.txt</option>
<option value="listing2.html">/HID Utilities/HID_Utilities.c</option>
<option value="listing3.html">/HID Utilities/HID_Utilities.h</option>
<option value="listing4.html">/HID Utilities/ImmrHIDUtilAddOn.c</option>
<option value="listing5.html">/HID Utilities/ImmrHIDUtilAddOn.h</option>
<option value="listing6.html">/HID Utilities/IOHIDDevice_.c</option>
<option value="listing7.html">/HID Utilities/IOHIDDevice_.h</option>
<option value="listing8.html">/HID Utilities/IOHIDElement_.c</option>
<option value="listing9.html">/HID Utilities/IOHIDElement_.h</option>
<option value="listing10.html">/HID Utilities/IOHIDLib_.h</option>
<option value="listing11.html">/main.c</option></select>
</p>
</form>
<p><strong><a href="HID_Explorer.zip">Download Sample</a></strong> (“HID_Explorer.zip”, 106.9K)<BR>
<strong><a href="HID_Explorer.dmg">Download Sample</a></strong> (“HID_Explorer.dmg”, 164.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_Utilities.c
//
// Contains: Implementation of the HID configuration utilities
//
// Copyright: Copyright (c) 2007 Apple Inc., All Rights Reserved
//
// Disclaimer: IMPORTANT: This Apple software is supplied to you by
// Apple 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 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.
//
//***************************************************
#pragma mark - includes & imports
//-----------------------------------------------------
//#include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>
#include <stdlib.h> // malloc
#include <time.h> // clock
#include <AssertMacros.h>
#include "HID_Utilities.h"
//***************************************************
#pragma mark - typedefs, enums, defines, etc.
//-----------------------------------------------------
#define FAKE_MISSING_NAMES 1 // set this to true while debuging to get more explicit element names; false for the generic ones
#define kPercentMove 10 // precent of overall range a element must move to register
#define kNameKeyCFStringRef CFSTR( "Name" ) // dictionary key
//***************************************************
#pragma mark - local ( static ) function prototypes
//-----------------------------------------------------
static void CFSetApplierFunctionCopyToCFArray(const void *value, void *context);
static CFComparisonResult CFDeviceArrayComparatorFunction(const void *val1, const void *val2, void *context);
static CFMutableDictionaryRef hu_SetUpMatchingDictionary( UInt32 inUsagePage, UInt32 inUsage );
static CFPropertyListRef hu_XMLLoad( CFStringRef inResourceName, CFStringRef inResourceExtension );
static CFPropertyListRef hu_LoadFromXMLFile( CFURLRef inCFURLRef );
//***************************************************
#pragma mark - exported globals
//-----------------------------------------------------
IOHIDManagerRef gIOHIDManagerRef = NULL;
CFMutableArrayRef gDeviceCFArrayRef = NULL;
CFArrayRef gElementCFArrayRef = NULL;
//***************************************************
#pragma mark - local ( static ) globals
//-----------------------------------------------------
//***************************************************
#pragma mark - exported function implementations
//-----------------------------------------------------
//*************************************************************************
//
// HIDBuildMultiDeviceList( inUsagePages, inUsages, inNumDeviceTypes )
//
// Purpose: builds list of devices with elements
//
// Inputs: inUsagePages - inNumDeviceTypes sized array of matching usage pages
// inUsages - inNumDeviceTypes sized array of matching usages
// inNumDeviceTypes - number of usage pages & usages
//
// Returns: Boolean - if successful
//
Boolean HIDBuildMultiDeviceList( UInt32 inUsagePages[], UInt32 inUsages[], UInt32 inNumDeviceTypes )
{
Boolean result = FALSE; // assume failure ( pessimist! )
Boolean first = (!gIOHIDManagerRef); // not yet created?
if ( first ) {
// create the manager
gIOHIDManagerRef = IOHIDManagerCreate( kCFAllocatorDefault, kIOHIDOptionsTypeNone );
}
if ( gIOHIDManagerRef ) {
CFMutableArrayRef hidMatchingCFMutableArrayRef = NULL;
if ( inUsages && inUsagePages && inNumDeviceTypes ) {
hidMatchingCFMutableArrayRef = CFArrayCreateMutable( kCFAllocatorDefault, 0, & kCFTypeArrayCallBacks );
if ( hidMatchingCFMutableArrayRef ) {
int idx;
for ( idx = 0; idx < inNumDeviceTypes; idx++ ) { // for all usage and usage page types
// Set up matching dictionary. returns NULL on error.
CFMutableDictionaryRef hidMatchingCFDictRef = hu_SetUpMatchingDictionary( inUsagePages[idx], inUsages[idx] );
if ( hidMatchingCFDictRef ) {
CFArrayAppendValue( hidMatchingCFMutableArrayRef, (void*) hidMatchingCFDictRef );
CFRelease( hidMatchingCFDictRef );
} else {
fprintf( stderr, "%s: Couldn't create a matching dictionary.", __PRETTY_FUNCTION__ );
}
}
} else {
fprintf( stderr, "%s: Couldn't create a matching array.", __PRETTY_FUNCTION__ );
}
}
// set it for IOHIDManager to use to match against
IOHIDManagerSetDeviceMatchingMultiple( gIOHIDManagerRef, hidMatchingCFMutableArrayRef );
if ( hidMatchingCFMutableArrayRef ) {
CFRelease( hidMatchingCFMutableArrayRef );
}
if ( first ) {
// open it
IOReturn tIOReturn = IOHIDManagerOpen( gIOHIDManagerRef, kIOHIDOptionsTypeNone );
if ( kIOReturnSuccess != tIOReturn ) {
fprintf( stderr, "%s: Couldn't open IOHIDManager.", __PRETTY_FUNCTION__ );
goto Oops;
}
}
HIDRebuildDevices( );
result = TRUE;
} else {
fprintf( stderr, "%s: Couldn't create a IOHIDManager.", __PRETTY_FUNCTION__ );
}
Oops: ;
return result;
} // HIDBuildMultiDeviceList
//*************************************************************************
//
// HIDRebuildDevices( )
//
// Purpose: rebuilds the (internal) list of IOHIDDevices
//
// Inputs: none
//
// Returns: none
//
void HIDRebuildDevices( void ) {
// get the set of devices from the IOHID manager
CFSetRef devCFSetRef = IOHIDManagerCopyDevices( gIOHIDManagerRef );
if ( devCFSetRef ) {
// if the existing array isn't empty...
if ( gDeviceCFArrayRef ) {
// release it
CFRelease( gDeviceCFArrayRef );
}
// create an empty array
gDeviceCFArrayRef = CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks );
// now copy the set to the array
CFSetApplyFunction( devCFSetRef, CFSetApplierFunctionCopyToCFArray, ( void * ) gDeviceCFArrayRef );
// now sort the array by location ID's
CFIndex cnt = CFArrayGetCount( gDeviceCFArrayRef );
CFArraySortValues( gDeviceCFArrayRef, CFRangeMake( 0, cnt ), CFDeviceArrayComparatorFunction, NULL );
// and release the set we copied from the IOHID manager
CFRelease( devCFSetRef );
}
} // HIDRebuildDevices
//*************************************************************************
//
// HIDConfigureAction( outIOHIDDeviceRef, outIOHIDElementRef, inTimeout )
//
// Purpose: polls all devices and elements for a change greater than kPercentMove.
// Times out after given time returns 1 and pointer to device and element
// if found; returns 0 and NULL for both parameters if not found
//
// Inputs: outIOHIDDeviceRef - address where to store the device
// outIOHIDElementRef - address where to store the element
// inTimeout - the timeout
// Returns: Boolean - if successful
// outIOHIDDeviceRef - the device
// outIOHIDElementRef - the element
//
Boolean HIDConfigureAction( IOHIDDeviceRef* outIOHIDDeviceRef, IOHIDElementRef *outIOHIDElementRef, float inTimeout )
{
if ( !outIOHIDDeviceRef || !outIOHIDElementRef ) {
return FALSE;
}
if ( !gDeviceCFArrayRef ) { // if we do not have a device list
// and we can't build another list
if ( !HIDBuildMultiDeviceList( nil, nil, 0 ) || ! gDeviceCFArrayRef ) {
return FALSE;
}
}
// duration timers
clock_t start, stop;
// determine the maximum number of elements
CFIndex maxElements = 0;
CFIndex devIndex, devCount = CFArrayGetCount( gDeviceCFArrayRef );
for ( devIndex = 0; devIndex < devCount; devIndex++ ) {
IOHIDDeviceRef tIOHIDDeviceRef = ( IOHIDDeviceRef ) CFArrayGetValueAtIndex( gDeviceCFArrayRef, devIndex );
if ( !tIOHIDDeviceRef) continue;
gElementCFArrayRef = IOHIDDeviceCopyMatchingElements( tIOHIDDeviceRef, NULL, 0 );
if ( gElementCFArrayRef ) {
CFIndex numElements = CFArrayGetCount( gElementCFArrayRef );
if ( numElements > maxElements ) {
maxElements = numElements;
}
CFRelease( gElementCFArrayRef );
gElementCFArrayRef = NULL;
}
}
// allocate an array of doubles to store devCount * maxElements values
double* saveValueArray = ( double * ) calloc( sizeof( double ), devCount * maxElements ); // clear 2D array to save values
// on the first pass store the initial values
Boolean found = FALSE, done = FALSE, first = TRUE;
// poll all devices and elements, compare current value to the initial values
while ( !found && !done ) {
for ( devIndex = 0; devIndex < devCount; devIndex++ ) {
IOHIDDeviceRef tIOHIDDeviceRef = ( IOHIDDeviceRef ) CFArrayGetValueAtIndex( gDeviceCFArrayRef, devIndex );
if ( !tIOHIDDeviceRef) continue;
gElementCFArrayRef = IOHIDDeviceCopyMatchingElements( tIOHIDDeviceRef, NULL, 0 );
if ( gElementCFArrayRef ) {
CFIndex eleIndex, eleCount = CFArrayGetCount( gElementCFArrayRef );
for ( eleIndex = 0; eleIndex < eleCount; eleIndex++ ) {
IOHIDElementRef tIOHIDElementRef = ( IOHIDElementRef ) CFArrayGetValueAtIndex( gElementCFArrayRef, eleIndex );
IOHIDElementType tIOHIDElementType = IOHIDElementGetType( tIOHIDElementRef );
// only care about inputs (no outputs or features)
if ( tIOHIDElementType <= kIOHIDElementTypeInput_ScanCodes ) {
uint32_t usagePage = IOHIDElementGetUsagePage( tIOHIDElementRef );
uint32_t usage = IOHIDElementGetUsage( tIOHIDElementRef );
// ignore PID elements and arrays
if ( ( kHIDPage_PID != usagePage ) && ( -1 != usage ) ) {
IOHIDValueRef tIOHIDValueRef;
IOHIDDeviceGetValue( tIOHIDDeviceRef, tIOHIDElementRef, &tIOHIDValueRef );
double value = IOHIDValueGetScaledValue( tIOHIDValueRef, kIOHIDValueScaleTypePhysical );
if ( first ) {
saveValueArray[( devIndex * maxElements ) + eleIndex] = value;
} else {
double initialValue = saveValueArray[( devIndex * maxElements ) + eleIndex];
CFIndex valueMin = IOHIDElementGetPhysicalMin( tIOHIDElementRef );
CFIndex valueMax = IOHIDElementGetPhysicalMax( tIOHIDElementRef );
double delta = fabs( ( valueMax - valueMin ) * kPercentMove * 0.01f );
if ( fabs( initialValue - value ) > delta ) {
found = TRUE;
*outIOHIDDeviceRef = tIOHIDDeviceRef;
*outIOHIDElementRef = tIOHIDElementRef;
break;
}
} // if first
} // if usage
} // if type
} // for elements...
CFRelease( gElementCFArrayRef );
gElementCFArrayRef = NULL;
} // for devices
} // while !( found || done)
if ( first ) {
start = clock( );
first = FALSE;
} else {
// are we done?
stop = clock( );
double secs;
secs = ( double ) ( stop - start ) / CLOCKS_PER_SEC;
if ( secs > inTimeout ) {
done = TRUE;
break;
}
}
} // while ( !found && !done );
// return device and element moved
if ( !found ) {
*outIOHIDDeviceRef = NULL;
*outIOHIDElementRef = NULL;
}
return found;
} // HIDConfigureAction
//*************************************************************************
//
// HIDSaveElementPref( inKeyCFStringRef, inAppCFStringRef, inIOHIDDeviceRef, inIOHIDElementRef )
//
// Purpose: Save the device & element values into the specified key in the specified applications preferences
//
// Inputs: inKeyCFStringRef - the preference key
// inAppCFStringRef - the application identifier
// inIOHIDDeviceRef - the device
// inIOHIDElementRef - the element
// Returns: Boolean - if successful
//
Boolean HIDSaveElementPref( const CFStringRef inKeyCFStringRef, CFStringRef inAppCFStringRef, IOHIDDeviceRef inIOHIDDeviceRef, IOHIDElementRef inIOHIDElementRef )
{
Boolean success = FALSE;
if ( inKeyCFStringRef && inAppCFStringRef && inIOHIDDeviceRef && inIOHIDElementRef ) {
long vendorID = IOHIDDevice_GetVendorID( inIOHIDDeviceRef );
require( vendorID, Oops);
long productID = IOHIDDevice_GetProductID( inIOHIDDeviceRef );
require( productID, Oops);
long locID = IOHIDDevice_GetLocationID( inIOHIDDeviceRef );
require( locID, Oops);
uint32_t devUsagePage = IOHIDDevice_GetUsagePage( inIOHIDDeviceRef );
uint32_t devUsage = IOHIDDevice_GetUsage( inIOHIDDeviceRef );
if ( !devUsagePage || !devUsage ) {
devUsagePage = IOHIDDevice_GetPrimaryUsagePage( inIOHIDDeviceRef );
devUsage = IOHIDDevice_GetPrimaryUsage( inIOHIDDeviceRef );
}
require( devUsagePage && devUsage, Oops);
IOHIDElementType eleType = IOHIDElementGetType( inIOHIDElementRef );
uint32_t eleUsagePage = IOHIDElementGetUsagePage( inIOHIDElementRef );
uint32_t eleUsage = IOHIDElementGetUsage( inIOHIDElementRef );
IOHIDElementCookie eleCookie = IOHIDElementGetCookie( inIOHIDElementRef );
CFStringRef prefCFStringRef = CFStringCreateWithFormat( kCFAllocatorDefault, NULL,
CFSTR( "d:{v:%ld, p:%ld, l:%ld, p:%ld, u:%ld}, e:{t:%ld, p:%ld, u:%ld, c:%ld}" ),
vendorID,
productID,
locID,
devUsagePage,
devUsage,
eleType,
eleUsagePage,
eleUsage,
eleCookie );
if ( prefCFStringRef ) {
CFPreferencesSetAppValue( inKeyCFStringRef, prefCFStringRef, inAppCFStringRef );
CFRelease( prefCFStringRef );
success = TRUE;
}
}
Oops: ;
return success;
} // HIDSaveElementPref
//*************************************************************************
//
// HIDRestoreElementPref( inKeyCFStringRef, inAppCFStringRef, outIOHIDDeviceRef, outIOHIDElementRef )
//
// Purpose: Find the specified preference in the specified application
//
// Inputs: inKeyCFStringRef - the preference key
// inAppCFStringRef - the application identifier
// outIOHIDDeviceRef - address where to restore the device
// outIOHIDElementRef - address where to restore the element
// Returns: Boolean - if successful
// outIOHIDDeviceRef - the device
// outIOHIDElementRef - the element
//
Boolean HIDRestoreElementPref( CFStringRef inKeyCFStringRef, CFStringRef inAppCFStringRef, IOHIDDeviceRef* outIOHIDDeviceRef, IOHIDElementRef *outIOHIDElementRef )
{
Boolean found = FALSE;
if ( inKeyCFStringRef && inAppCFStringRef && outIOHIDDeviceRef && outIOHIDElementRef ) {
CFPropertyListRef prefCFPropertyListRef = CFPreferencesCopyAppValue( inKeyCFStringRef, inAppCFStringRef );
if ( prefCFPropertyListRef ) {
if ( CFStringGetTypeID( ) == CFGetTypeID( prefCFPropertyListRef ) ) {
char buffer[256];
if ( CFStringGetCString( ( CFStringRef ) prefCFPropertyListRef, buffer, sizeof( buffer ), kCFStringEncodingUTF8 ) ) {
hu_InfoRec_t searchInfo;
int count = sscanf( buffer, "d:{v:%ld, p:%ld, l:%ld, p:%d, u:%d}, e:{t:%ld, p:%d, u:%d, c:%ld}",
&searchInfo.vendorID, &searchInfo.productID, &searchInfo.locID, &searchInfo.devUsagePage, &searchInfo.devUsage,
&searchInfo.type, &searchInfo.eleUsagePage, &searchInfo.eleUsage,( long* ) &searchInfo.cookie );
if ( 9 == count ) { // if we found all nine parameters\xC9
// and can find a device & element that matches these\xC9
if ( HIDFindDeviceAndElement( &searchInfo, outIOHIDDeviceRef, outIOHIDElementRef ) ) {
found = TRUE;
}
}
}
} else {
// We found the entry with this key but it's the wrong type; delete it.
CFPreferencesSetAppValue( inKeyCFStringRef, NULL, inAppCFStringRef );
( void ) CFPreferencesAppSynchronize( inAppCFStringRef );
}
CFRelease( prefCFPropertyListRef );
}
}
return found;
} // HIDRestoreElementPref
//*************************************************************************
//
// HIDFindDeviceAndElement( inSearchInfo, outFoundDevice, outFoundElement )
//
// Purpose: find the closest matching device and element for this action
//
// Notes: matches device: serial, vendorID, productID, location, inUsagePage, usage
// matches element: cookie, inUsagePage, usage,
//
// Inputs: inSearchInfo - the device & element info we searching for
// outFoundDevice - the address of the best matching device
// outFoundElement - the address of the best matching element
//
// Returns: Boolean - TRUE if we find a match
// outFoundDevice - the best matching device
// outFoundElement - the best matching element
//
Boolean HIDFindDeviceAndElement( const hu_InfoRec_t* inSearchInfo, IOHIDDeviceRef* outFoundDevice, IOHIDElementRef *outFoundElement )
{
Boolean result = FALSE;
IOHIDDeviceRef bestIOHIDDeviceRef = NULL;
IOHIDElementRef bestIOHIDElementRef = NULL;
long bestScore = 0;
CFIndex devIndex, devCount = CFArrayGetCount( gDeviceCFArrayRef );
for ( devIndex = 0; devIndex < devCount; devIndex++ ) {
long deviceScore = 1;
IOHIDDeviceRef tIOHIDDeviceRef = ( IOHIDDeviceRef ) CFArrayGetValueAtIndex( gDeviceCFArrayRef, devIndex );
if ( !tIOHIDDeviceRef) continue;
// match vendorID, productID (+10, +8)
if ( inSearchInfo->vendorID ) {
long vendorID = IOHIDDevice_GetVendorID( tIOHIDDeviceRef);
if ( vendorID ) {
if ( inSearchInfo->vendorID == vendorID ) {
deviceScore += 10;
if ( inSearchInfo->productID ) {
long productID = IOHIDDevice_GetProductID( tIOHIDDeviceRef);
if ( productID ) {
if ( inSearchInfo->productID == productID ) {
deviceScore += 8;
} // if ( inSearchInfo->productID == productID )
} // if ( productID )
} // if ( inSearchInfo->productID )
} // if (inSearchInfo->vendorID == vendorID)
} // if vendorID
} // if search->vendorID
// match usagePage & usage (+9)
if ( inSearchInfo->devUsagePage && inSearchInfo->devUsage ) {
uint32_t usagePage = IOHIDDevice_GetUsagePage( tIOHIDDeviceRef ) ;
uint32_t usage = IOHIDDevice_GetUsage( tIOHIDDeviceRef );
if ( !usagePage || ! usage ) {
usagePage = IOHIDDevice_GetPrimaryUsagePage( tIOHIDDeviceRef );
usage = IOHIDDevice_GetPrimaryUsage( tIOHIDDeviceRef );
}
if ( usagePage ) {
if ( inSearchInfo->devUsagePage == usagePage ) {
if ( usage ) {
if ( inSearchInfo->devUsage == usage ) {
deviceScore += 9;
} // if ( inSearchInfo->usage == usage )
} // if ( usage )
} // if ( inSearchInfo->usagePage == usagePage )
} // if ( usagePage )
} // if ( inSearchInfo->usagePage && inSearchInfo->usage )
// match location ID (+5)
if ( inSearchInfo->locID ) {
long locID = IOHIDDevice_GetLocationID( tIOHIDDeviceRef );
if ( locID ) {
if ( inSearchInfo->locID == locID ) {
deviceScore += 5;
}
}
}
// iterate over all elements of this device
gElementCFArrayRef = IOHIDDeviceCopyMatchingElements( tIOHIDDeviceRef, NULL, 0 );
if ( gElementCFArrayRef ) {
CFIndex eleIndex, eleCount = CFArrayGetCount( gElementCFArrayRef );
for ( eleIndex = 0; eleIndex < eleCount; eleIndex++ ) {
IOHIDElementRef tIOHIDElementRef = ( IOHIDElementRef ) CFArrayGetValueAtIndex( gElementCFArrayRef, eleIndex );
if ( !tIOHIDElementRef ) continue;
long score = deviceScore;
// match type, usage page, usage & cookie
if ( inSearchInfo->type == IOHIDElementGetType( tIOHIDElementRef ) ) {
if ( inSearchInfo->eleUsagePage && inSearchInfo->eleUsage ) {
uint32_t usagePage = IOHIDElementGetUsagePage( tIOHIDElementRef );
if ( inSearchInfo->eleUsagePage == usagePage ) {
uint32_t usage = IOHIDElementGetUsage( tIOHIDElementRef );
if ( inSearchInfo->eleUsage == usage ) {
score += 5;
IOHIDElementCookie cookie = IOHIDElementGetCookie( tIOHIDElementRef );
if ( inSearchInfo->cookie == cookie ) {
score += 4;
} // cookies match
} else {
score = 0;
} // usages match
} else {
score = 0;
} // usage pages match
} // if ( search usage page & usage )
} // if type
#if LOG_SCORING
if ( kHIDPage_KeyboardOrKeypad != tElementRef->usagePage ) { // skip keyboards here
printf( "%s: ( %ld:%ld )-I-Debug, score: %ld\t", __PRETTY_FUNCTION__, inSearchInfo->eleUsagePage, inSearchInfo->eleUsage, score );
HIDPrintElement( tIOHIDElementRef );
}
#endif LOG_SCORING
if ( score > bestScore ) {
bestIOHIDDeviceRef = tIOHIDDeviceRef;
bestIOHIDElementRef = tIOHIDElementRef;
bestScore = score;
#if LOG_SCORING
printf( "%s: ( %ld:%ld )-I-Debug, better score: %ld\t", __PRETTY_FUNCTION__, inSearchInfo->eleUsagePage, inSearchInfo->eleUsage, score );
HIDPrintElement( bestIOHIDElementRef );
#endif LOG_SCORING
}
} // for elements...
CFRelease( gElementCFArrayRef );
gElementCFArrayRef = NULL;
} // if ( gElementCFArrayRef )
} // for ( devIndex = 0; devIndex < devCount; devIndex++ )
if ( bestIOHIDDeviceRef || bestIOHIDElementRef ) {
*outFoundDevice = bestIOHIDDeviceRef;
*outFoundElement = bestIOHIDElementRef;
#if LOG_SCORING
printf( "%s: ( %ld:%ld )-I-Debug, best score: %ld\t", __PRETTY_FUNCTION__, inSearchInfo->eleUsagePage, inSearchInfo->eleUsage, bestScore );
HIDPrintElement( bestIOHIDElementRef );
#endif LOG_SCORING
result = TRUE;
}
return result;
} // HIDFindDeviceAndElement
//*************************************************************************
//
// HIDGetUsageName( inUsagePage, inUsage )
//
// Purpose: return a CFStringRef string for a given usage page & usage( see IOUSBHIDParser.h )
//
// Notes: returns usage page and usage values in CFString form for unknown values
//
// Inputs: inUsagePage - the usage page
// inUsage - the usage
//
// Returns: CFStringRef - the resultant string
//
CFStringRef HIDCopyUsageName( long inUsagePage, long inUsage )
{
static CFPropertyListRef tCFPropertyListRef = NULL;
CFStringRef result = NULL;
if ( !tCFPropertyListRef )
tCFPropertyListRef = hu_XMLLoad( CFSTR( "HID_usage_strings" ), CFSTR( "plist" ) );
if ( tCFPropertyListRef ) {
if ( CFDictionaryGetTypeID( ) == CFGetTypeID( tCFPropertyListRef ) ) {
CFStringRef pageKeyCFStringRef = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR( "0x%4.4lX" ), inUsagePage );
if ( pageKeyCFStringRef ) {
CFDictionaryRef pageCFDictionaryRef;
if ( CFDictionaryGetValueIfPresent( tCFPropertyListRef, pageKeyCFStringRef, ( const void** ) &pageCFDictionaryRef ) ) {
CFStringRef pageCFStringRef;
if ( CFDictionaryGetValueIfPresent( pageCFDictionaryRef, kNameKeyCFStringRef, ( const void** ) &pageCFStringRef ) ) {
CFStringRef usageKeyCFStringRef = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR( "0x%4.4lX" ), inUsage );
if ( usageKeyCFStringRef ) {
CFStringRef usageCFStringRef;
if ( CFDictionaryGetValueIfPresent( pageCFDictionaryRef, usageKeyCFStringRef, ( const void** ) &usageCFStringRef ) ) {
result = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR( "%@ %@" ), pageCFStringRef, usageCFStringRef );
}
#if FAKE_MISSING_NAMES
else {
result = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR( "%@ #%@" ), pageCFStringRef, usageKeyCFStringRef );
}
#endif
CFRelease( usageKeyCFStringRef );
}
} else {
// no name data for this page
}
} else {
// no data for this page
}
CFRelease( pageKeyCFStringRef );
}
}
// CFRelease( tCFPropertyListRef ); // Leak this!
// tCFPropertyListRef = NULL;
}
return result;
} // HIDCopyUsageName
// utility routine to dump device info
void HIDDumpDeviceInfo( IOHIDDeviceRef inIOHIDDeviceRef )
{
printf( "Device: %p = { ", inIOHIDDeviceRef );
char manufacturer[256] = ""; // name of manufacturer
CFStringRef tCFStringRef = IOHIDDevice_GetManufacturer( inIOHIDDeviceRef );
if ( tCFStringRef ) {
verify( CFStringGetCString( tCFStringRef, manufacturer, sizeof( manufacturer ), kCFStringEncodingUTF8 ) );
}
char product[256] = ""; // name of product
tCFStringRef = IOHIDDevice_GetProduct( inIOHIDDeviceRef );
if ( tCFStringRef ) {
verify( CFStringGetCString( tCFStringRef, product, sizeof( product ), kCFStringEncodingUTF8 ) );
}
printf( "%s - %s, ", manufacturer, product );
long vendorID = IOHIDDevice_GetVendorID( inIOHIDDeviceRef );
if ( vendorID ) {
#if 1
printf( " vendorID: 0x%04lX, ", vendorID );
#else
char string[256];
if ( HIDGetVendorNameFromVendorID( vendorID, string ) ) {
printf( " vendorID: 0x%04lX (\"%s\"), ", vendorID, string );
} else {
printf( " vendorID: 0x%04lX, ", vendorID );
}
#endif
}
long productID = IOHIDDevice_GetProductID( inIOHIDDeviceRef );
if ( productID ) {
#if 1
printf( " productID: 0x%04lX, ", productID );
#else
if ( HIDGetProductNameFromVendorProductID( vendorID, productID, string ) ) {
printf( " productID: 0x%04lX (\"%s\"), ", productID, string );
} else {
printf( " productID: 0x%04lX, ", productID );
}
#endif
}
uint32_t devUsagePage = IOHIDDevice_GetUsagePage( inIOHIDDeviceRef );
uint32_t devUsage = IOHIDDevice_GetUsage( inIOHIDDeviceRef );
if ( !devUsagePage || !devUsage ) {
devUsagePage = IOHIDDevice_GetPrimaryUsagePage( inIOHIDDeviceRef );
devUsage = IOHIDDevice_GetPrimaryUsage( inIOHIDDeviceRef );
}
printf( "usage: 0x%04lX:0x%04lX, ", (long unsigned int) devUsagePage, (long unsigned int) devUsage );
#if 1
tCFStringRef = HIDCopyUsageName( devUsagePage, devUsage );
if ( tCFStringRef ) {
char usageString[256] = "";
verify( CFStringGetCString( tCFStringRef, usageString, sizeof( usageString ), kCFStringEncodingUTF8 ) );
printf( "\"%s\"", usageString );
CFRelease( tCFStringRef );
}
#endif
printf( "\n" );
fflush( stdout );
} // HIDDumpDeviceInfo
// utility routine to dump element info
void HIDDumpElementInfo( IOHIDElementRef inIOHIDElementRef )
{
if ( inIOHIDElementRef ) {
printf( "Element: %p = { ", inIOHIDElementRef );
IOHIDDeviceRef tIOHIDDeviceRef = IOHIDElementGetDevice( inIOHIDElementRef );
printf( "Device: %p, ", tIOHIDDeviceRef );
IOHIDElementRef parentIOHIDElementRef = IOHIDElementGetParent( inIOHIDElementRef );
printf( "parent: %p, ", parentIOHIDElementRef );
#if 0
CFArrayRef childrenCFArrayRef = IOHIDElementGetChildren( inIOHIDElementRef );
printf( "children: %p: { ", childrenCFArrayRef ); fflush( stdout );
CFShow( childrenCFArrayRef ); fflush( stdout );
printf( " }, " );
#endif
IOHIDElementCookie tIOHIDElementCookie = IOHIDElementGetCookie( inIOHIDElementRef );
printf( "cookie: %p, ", tIOHIDElementCookie );
IOHIDElementType tIOHIDElementType = IOHIDElementGetType( inIOHIDElementRef );
switch ( tIOHIDElementType ) {
case kIOHIDElementTypeInput_Misc: {
printf( "type: Misc, " );
break;
}
case kIOHIDElementTypeInput_Button: {
printf( "type: Button, " );
break;
}
case kIOHIDElementTypeInput_Axis: {
printf( "type: Axis, " );
break;
}
case kIOHIDElementTypeInput_ScanCodes: {
printf( "type: ScanCodes, " );
break;
}
case kIOHIDElementTypeOutput: {
printf( "type: Output, " );
break;
}
case kIOHIDElementTypeFeature: {
printf( "type: Feature, " );
break;
}
case kIOHIDElementTypeCollection: {
printf( "type: Collection, " );
break;
}
default: {
printf( "type: %p, ", (void*) tIOHIDElementType );
break;
}
}
IOHIDElementCollectionType tIOHIDElementCollectionType = IOHIDElementGetCollectionType( inIOHIDElementRef );
printf( "collection: %p, ", (void*) tIOHIDElementCollectionType );
uint32_t usagePage = IOHIDElementGetUsagePage( inIOHIDElementRef );
uint32_t usage = IOHIDElementGetUsage( inIOHIDElementRef );
printf( "usage: 0x%04lX:0x%04lX, ", (long unsigned int) usagePage, (long unsigned int) usage );
#if 1
CFStringRef tCFStringRef = HIDCopyUsageName( usagePage, usage );
if ( tCFStringRef ) {
char usageString[256] = "";
verify( CFStringGetCString( tCFStringRef, usageString, sizeof( usageString ), kCFStringEncodingUTF8 ) );
printf( "\"%s\", ", usageString );
CFRelease( tCFStringRef );
}
#endif
CFStringRef nameCFStringRef = IOHIDElementGetName( inIOHIDElementRef );
char buffer[256];
if ( nameCFStringRef && CFStringGetCString( nameCFStringRef, buffer, sizeof( buffer ), kCFStringEncodingUTF8 ) ) {
printf( "name: %s, ", buffer );
}
uint32_t reportID = IOHIDElementGetReportID( inIOHIDElementRef );
printf( "report: { ID: %08lX, ", (long unsigned int) reportID );
uint32_t reportSize = IOHIDElementGetReportSize( inIOHIDElementRef );
printf( "Size: %lu, ", (long unsigned int) reportSize );
uint32_t reportCount = IOHIDElementGetReportCount( inIOHIDElementRef );
printf( "Count: %lu }, ", (long unsigned int) reportCount );
uint32_t unit = IOHIDElementGetUnit( inIOHIDElementRef );
printf( "unit: %lu, ", (long unsigned int) unit );
uint32_t unitExp = IOHIDElementGetUnitExponent( inIOHIDElementRef );
printf( "unitExp: %lu, ", (long unsigned int) unitExp );
CFIndex logicalMin = IOHIDElementGetLogicalMin( inIOHIDElementRef );
printf( "logicalMin: %ld, ", logicalMin );
CFIndex logicalMax = IOHIDElementGetLogicalMax( inIOHIDElementRef );
printf( "logicalMax: %ld, ", logicalMax );
CFIndex physicalMin = IOHIDElementGetPhysicalMin( inIOHIDElementRef );
printf( "physicalMin: %ld, ", physicalMin );
CFIndex physicalMax = IOHIDElementGetPhysicalMax( inIOHIDElementRef );
printf( "physicalMax: %ld, ", physicalMax );
#if 1
Boolean isVirtual = IOHIDElementIsVirtual( inIOHIDElementRef );
if ( isVirtual ) printf( "isVirtual, " );
Boolean isRelative = IOHIDElementIsRelative( inIOHIDElementRef );
if ( isRelative ) printf( "isRelative, " );
Boolean isWrapping = IOHIDElementIsWrapping( inIOHIDElementRef );
if ( isWrapping ) printf( "isWrapping, " );
Boolean isArray = IOHIDElementIsArray( inIOHIDElementRef );
if ( isArray ) printf( "isArray, " );
Boolean isNonLinear = IOHIDElementIsNonLinear( inIOHIDElementRef );
if ( isNonLinear ) printf( "isNonLinear, " );
Boolean hasPreferredState = IOHIDElementHasPreferredState( inIOHIDElementRef );
if ( hasPreferredState ) printf( "hasPreferredState, " );
Boolean hasNullState = IOHIDElementHasNullState( inIOHIDElementRef );
if ( hasNullState ) printf( "hasNullState, " );
#else
Boolean isVirtual = IOHIDElementIsVirtual( inIOHIDElementRef );
printf( "isVirtual: %s, ", isVirtual ? "YES" : "NO" );
Boolean isRelative = IOHIDElementIsRelative( inIOHIDElementRef );
printf( "isRelative: %s, ", isRelative ? "YES" : "NO" );
Boolean isWrapping = IOHIDElementIsWrapping( inIOHIDElementRef );
printf( "isWrapping: %s, ", isWrapping ? "YES" : "NO" );
Boolean isArray = IOHIDElementIsArray( inIOHIDElementRef );
printf( "isArray: %s, ", isArray ? "YES" : "NO" );
Boolean isNonLinear = IOHIDElementIsNonLinear( inIOHIDElementRef );
printf( "isNonLinear: %s, ", isNonLinear ? "YES" : "NO" );
Boolean hasPreferredState = IOHIDElementHasPreferredState( inIOHIDElementRef );
printf( "hasPreferredState: %s, ", hasPreferredState ? "YES" : "NO" );
Boolean hasNullState = IOHIDElementHasNullState( inIOHIDElementRef );
printf( "hasNullState: %s, ", hasNullState ? "YES" : "NO" );
#endif
printf( " }\n" );
}
} // HIDDumpElementInfo
//***************************************************
#pragma mark - local ( static ) function implementations
//-----------------------------------------------------
//*************************************************************************
//
// CFSetApplierFunctionCopyToCFArray( value, context )
//
// Purpose: CFSetApplierFunction to copy the CFSet to a CFArray
//
// Notes: called one time for each item in the CFSet
//
// Inputs: value - the current element of the CFSet
// context - the CFMutableArrayRef we're adding the CFSet elements to
//
// Returns: nothing
//
static void CFSetApplierFunctionCopyToCFArray(const void *value, void *context)
{
// printf( "%s: 0x%08lX\n", __PRETTY_FUNCTION__, (long unsigned int) value );
CFArrayAppendValue( ( CFMutableArrayRef ) context, value );
} // CFSetApplierFunctionCopyToCFArray
// ---------------------------------
// used to sort the CFDevice array after copying it from the (unordered) (CF)set.
// we compare based on the location ID's since they're consistant (across boots & launches).
//
static CFComparisonResult CFDeviceArrayComparatorFunction(const void *val1, const void *val2, void *context)
{
#pragma unused( context )
CFComparisonResult result = kCFCompareEqualTo;
long loc1 = IOHIDDevice_GetLocationID( ( IOHIDDeviceRef ) val1 );
long loc2 = IOHIDDevice_GetLocationID( ( IOHIDDeviceRef ) val2 );
if ( loc1 < loc2 ) {