71
71
// ----------------------------------------------------
72
72
// function to create a matching dictionary for usage page & usage
73
73
static CFMutableDictionaryRef hu_CreateMatchingDictionaryUsagePageUsage ( Boolean isDeviceNotElement ,
74
- UInt32 inUsagePage ,
75
- UInt32 inUsage )
74
+ UInt32 inUsagePage ,
75
+ UInt32 inUsage )
76
76
{
77
- // create a dictionary to add usage page / usages to
78
- CFMutableDictionaryRef result = CFDictionaryCreateMutable ( kCFAllocatorDefault ,
79
- 0 ,
80
- & kCFTypeDictionaryKeyCallBacks ,
81
- & kCFTypeDictionaryValueCallBacks );
82
-
83
- if ( result ) {
84
- if ( inUsagePage ) {
85
- // Add key for device type to refine the matching dictionary.
86
- CFNumberRef pageCFNumberRef = CFNumberCreate ( kCFAllocatorDefault , kCFNumberIntType , & inUsagePage );
87
-
88
- if ( pageCFNumberRef ) {
89
- if ( isDeviceNotElement ) {
90
- CFDictionarySetValue ( result , CFSTR ( kIOHIDDeviceUsagePageKey ), pageCFNumberRef );
91
- } else {
92
- CFDictionarySetValue ( result , CFSTR ( kIOHIDElementUsagePageKey ), pageCFNumberRef );
93
- }
94
- CFRelease ( pageCFNumberRef );
95
-
96
- // note: the usage is only valid if the usage page is also defined
97
- if ( inUsage ) {
98
- CFNumberRef usageCFNumberRef = CFNumberCreate ( kCFAllocatorDefault , kCFNumberIntType , & inUsage );
99
-
100
- if ( usageCFNumberRef ) {
101
- if ( isDeviceNotElement ) {
102
- CFDictionarySetValue ( result , CFSTR ( kIOHIDDeviceUsageKey ), usageCFNumberRef );
103
- } else {
104
- CFDictionarySetValue ( result , CFSTR ( kIOHIDElementUsageKey ), usageCFNumberRef );
105
- }
106
- CFRelease ( usageCFNumberRef );
107
- } else {
108
- fprintf ( stderr , "%s: CFNumberCreate( usage ) failed." , __PRETTY_FUNCTION__ );
109
- }
110
- }
111
- } else {
112
- fprintf ( stderr , "%s: CFNumberCreate( usage page ) failed." , __PRETTY_FUNCTION__ );
113
- }
114
- }
115
- } else {
116
- fprintf ( stderr , "%s: CFDictionaryCreateMutable failed." , __PRETTY_FUNCTION__ );
117
- }
118
- return result ;
119
- } // hu_CreateMatchingDictionaryUsagePageUsage
77
+ // create a dictionary to add usage page / usages to
78
+ CFMutableDictionaryRef result = CFDictionaryCreateMutable ( kCFAllocatorDefault ,
79
+ 0 ,
80
+ & kCFTypeDictionaryKeyCallBacks ,
81
+ & kCFTypeDictionaryValueCallBacks );
82
+
83
+ if ( result ) {
84
+ if ( inUsagePage ) {
85
+ // Add key for device type to refine the matching dictionary.
86
+ CFNumberRef pageCFNumberRef = CFNumberCreate ( kCFAllocatorDefault , kCFNumberIntType , & inUsagePage );
87
+
88
+ if ( pageCFNumberRef ) {
89
+ if ( isDeviceNotElement ) {
90
+ CFDictionarySetValue ( result , CFSTR ( kIOHIDDeviceUsagePageKey ), pageCFNumberRef );
91
+ } else {
92
+ CFDictionarySetValue ( result , CFSTR ( kIOHIDElementUsagePageKey ), pageCFNumberRef );
93
+ }
94
+ CFRelease ( pageCFNumberRef );
95
+
96
+ // note: the usage is only valid if the usage page is also defined
97
+ if ( inUsage ) {
98
+ CFNumberRef usageCFNumberRef = CFNumberCreate ( kCFAllocatorDefault , kCFNumberIntType , & inUsage );
99
+
100
+ if ( usageCFNumberRef ) {
101
+ if ( isDeviceNotElement ) {
102
+ CFDictionarySetValue ( result , CFSTR ( kIOHIDDeviceUsageKey ), usageCFNumberRef );
103
+ } else {
104
+ CFDictionarySetValue ( result , CFSTR ( kIOHIDElementUsageKey ), usageCFNumberRef );
105
+ }
106
+ CFRelease ( usageCFNumberRef );
107
+ } else {
108
+ fprintf ( stderr , "%s: CFNumberCreate( usage ) failed." , __PRETTY_FUNCTION__ );
109
+ }
110
+ }
111
+ } else {
112
+ fprintf ( stderr , "%s: CFNumberCreate( usage page ) failed." , __PRETTY_FUNCTION__ );
113
+ }
114
+ }
115
+ } else {
116
+ fprintf ( stderr , "%s: CFDictionaryCreateMutable failed." , __PRETTY_FUNCTION__ );
117
+ }
118
+ return result ;
119
+ } // hu_CreateMatchingDictionaryUsagePageUsage
120
120
121
121
int main ( int argc , const char * argv [] )
122
122
{
123
- #pragma unused ( argc, argv )
124
- IOHIDDeviceRef * tIOHIDDeviceRefs = nil ;
123
+ #pragma unused ( argv )
124
+ IOHIDDeviceRef * tIOHIDDeviceRefs = nil ;
125
125
126
- // create a IO HID Manager reference
127
- IOHIDManagerRef tIOHIDManagerRef = IOHIDManagerCreate ( kCFAllocatorDefault , kIOHIDOptionsTypeNone );
128
- require ( tIOHIDManagerRef , Oops );
129
-
130
- // Create a device matching dictionary
131
- CFDictionaryRef matchingCFDictRef = hu_CreateMatchingDictionaryUsagePageUsage ( TRUE,
132
- kHIDPage_GenericDesktop ,
133
- kHIDUsage_GD_Keyboard );
134
- require ( matchingCFDictRef , Oops );
135
-
136
- // set the HID device matching dictionary
137
- IOHIDManagerSetDeviceMatching ( tIOHIDManagerRef , matchingCFDictRef );
138
-
139
- if ( matchingCFDictRef ) {
140
- CFRelease ( matchingCFDictRef );
141
- }
142
-
143
- // Now open the IO HID Manager reference
144
- IOReturn tIOReturn = IOHIDManagerOpen ( tIOHIDManagerRef , kIOHIDOptionsTypeNone );
145
- require_noerr ( tIOReturn , Oops );
146
-
147
- // and copy out its devices
148
- CFSetRef deviceCFSetRef = IOHIDManagerCopyDevices ( tIOHIDManagerRef );
149
- require ( deviceCFSetRef , Oops );
150
-
151
- // how many devices in the set?
152
- CFIndex deviceIndex , deviceCount = CFSetGetCount ( deviceCFSetRef );
153
-
154
- // allocate a block of memory to extact the device ref's from the set into
155
- tIOHIDDeviceRefs = malloc ( sizeof ( IOHIDDeviceRef ) * deviceCount );
156
- if (!tIOHIDDeviceRefs ) {
157
- CFRelease (deviceCFSetRef );
158
- deviceCFSetRef = NULL ;
159
- goto Oops ;
160
- }
161
-
162
- // now extract the device ref's from the set
163
- CFSetGetValues ( deviceCFSetRef , (const void * * ) tIOHIDDeviceRefs );
164
- CFRelease (deviceCFSetRef );
165
- deviceCFSetRef = NULL ;
166
-
167
- // before we get into the device loop we'll setup our element matching dictionary
168
- matchingCFDictRef = hu_CreateMatchingDictionaryUsagePageUsage ( FALSE, kHIDPage_LEDs , 0 );
169
- require ( matchingCFDictRef , Oops );
170
-
171
- int led_mask = 5 ; // no arguments turn on scrlk and numlk
126
+ // create a IO HID Manager reference
127
+ IOHIDManagerRef tIOHIDManagerRef = IOHIDManagerCreate ( kCFAllocatorDefault , kIOHIDOptionsTypeNone );
128
+ require ( tIOHIDManagerRef , Oops );
129
+
130
+ // Create a device matching dictionary
131
+ CFDictionaryRef matchingCFDictRef = hu_CreateMatchingDictionaryUsagePageUsage ( TRUE,
132
+ kHIDPage_GenericDesktop ,
133
+ kHIDUsage_GD_Keyboard );
134
+ require ( matchingCFDictRef , Oops );
135
+
136
+ // set the HID device matching dictionary
137
+ IOHIDManagerSetDeviceMatching ( tIOHIDManagerRef , matchingCFDictRef );
138
+
139
+ if ( matchingCFDictRef ) {
140
+ CFRelease ( matchingCFDictRef );
141
+ }
142
+
143
+ // Now open the IO HID Manager reference
144
+ IOReturn tIOReturn = IOHIDManagerOpen ( tIOHIDManagerRef , kIOHIDOptionsTypeNone );
145
+ require_noerr ( tIOReturn , Oops );
146
+
147
+ // and copy out its devices
148
+ CFSetRef deviceCFSetRef = IOHIDManagerCopyDevices ( tIOHIDManagerRef );
149
+ require ( deviceCFSetRef , Oops );
150
+
151
+ // how many devices in the set?
152
+ CFIndex deviceIndex , deviceCount = CFSetGetCount ( deviceCFSetRef );
153
+
154
+ // allocate a block of memory to extact the device ref's from the set into
155
+ tIOHIDDeviceRefs = malloc ( sizeof ( IOHIDDeviceRef ) * deviceCount );
156
+ if (!tIOHIDDeviceRefs ) {
157
+ CFRelease (deviceCFSetRef );
158
+ deviceCFSetRef = NULL ;
159
+ goto Oops ;
160
+ }
161
+
162
+ // now extract the device ref's from the set
163
+ CFSetGetValues ( deviceCFSetRef , (const void * * ) tIOHIDDeviceRefs );
164
+ CFRelease (deviceCFSetRef );
165
+ deviceCFSetRef = NULL ;
166
+
167
+ // before we get into the device loop we'll setup our element matching dictionary
168
+ matchingCFDictRef = hu_CreateMatchingDictionaryUsagePageUsage ( FALSE, kHIDPage_LEDs , 0 );
169
+ require ( matchingCFDictRef , Oops );
170
+
171
+ int led_mask = 5 ; // no arguments turn on scrlk and numlk
172
172
if (argc > 1 ) {
173
173
led_mask = 1 ; // any argument tun on only numlk (and the rest off)
174
174
printf ("Turning off all but numlk\n" );
175
175
} else {
176
176
printf ("Turning on numlk and scrlk\n" );
177
177
}
178
178
179
- Boolean delayFlag = FALSE; // if we find an LED element we'll set this to TRUE
179
+ Boolean delayFlag = FALSE; // if we find an LED element we'll set this to TRUE
180
180
181
181
// printf( "pass = %d.\n", pass );
182
182
for ( deviceIndex = 0 ; deviceIndex < deviceCount ; deviceIndex ++ ) {
183
183
184
184
// if this isn't a keyboard device...
185
185
if ( !IOHIDDeviceConformsTo ( tIOHIDDeviceRefs [deviceIndex ], kHIDPage_GenericDesktop , kHIDUsage_GD_Keyboard ) ) {
186
- continue ; // ...skip it
186
+ continue ; // ...skip it
187
187
}
188
188
189
- // printf( " device = %p.\n", tIOHIDDeviceRefs[deviceIndex] );
189
+ // printf( " device = %p.\n", tIOHIDDeviceRefs[deviceIndex] );
190
190
191
191
// copy all the elements
192
192
CFArrayRef elementCFArrayRef = IOHIDDeviceCopyMatchingElements ( tIOHIDDeviceRefs [deviceIndex ],
@@ -209,14 +209,14 @@ int main( int argc, const char * argv[] )
209
209
210
210
// if this isn't an LED element...
211
211
if ( kHIDPage_LEDs != usagePage ) {
212
- continue ; // ...skip it
212
+ continue ; // ...skip it
213
213
}
214
214
215
215
uint32_t usage = IOHIDElementGetUsage ( tIOHIDElementRef );
216
216
IOHIDElementType tIOHIDElementType = IOHIDElementGetType ( tIOHIDElementRef );
217
217
218
218
/*
219
- printf( " element = %p (page: %d, usage: %d, type: %d ).\n",
219
+ printf( " element = %p (page: %d, usage: %d, type: %d ).\n",
220
220
tIOHIDElementRef, usagePage, usage, tIOHIDElementType );
221
221
*/
222
222
@@ -232,7 +232,7 @@ int main( int argc, const char * argv[] )
232
232
device_value /= modCFIndex ;
233
233
234
234
/*
235
- printf( " value = 0x%08lX.\n", tCFIndex );
235
+ printf( " value = 0x%08lX.\n", tCFIndex );
236
236
*/
237
237
238
238
uint64_t timestamp = 0 ; // create the IO HID Value to be sent to this LED element
@@ -242,27 +242,27 @@ int main( int argc, const char * argv[] )
242
242
tIOReturn = IOHIDDeviceSetValue ( tIOHIDDeviceRefs [deviceIndex ], tIOHIDElementRef , tIOHIDValueRef );
243
243
CFRelease ( tIOHIDValueRef );
244
244
require_noerr ( tIOReturn , next_element );
245
- delayFlag = TRUE; // set this TRUE so we'll delay before changing our LED values again
245
+ delayFlag = TRUE; // set this TRUE so we'll delay before changing our LED values again
246
246
}
247
- next_element : ;
247
+ next_element : ;
248
248
continue ;
249
249
}
250
250
CFRelease ( elementCFArrayRef );
251
251
next_device : ;
252
252
continue ;
253
253
}
254
-
255
-
256
- if ( matchingCFDictRef ) {
257
- CFRelease ( matchingCFDictRef );
258
- }
259
- Oops : ;
260
- if ( tIOHIDDeviceRefs ) {
261
- free (tIOHIDDeviceRefs );
262
- }
263
-
264
- if ( tIOHIDManagerRef ) {
265
- CFRelease ( tIOHIDManagerRef );
266
- }
267
- return 0 ;
254
+
255
+
256
+ if ( matchingCFDictRef ) {
257
+ CFRelease ( matchingCFDictRef );
258
+ }
259
+ Oops : ;
260
+ if ( tIOHIDDeviceRefs ) {
261
+ free (tIOHIDDeviceRefs );
262
+ }
263
+
264
+ if ( tIOHIDManagerRef ) {
265
+ CFRelease ( tIOHIDManagerRef );
266
+ }
267
+ return 0 ;
268
268
} /* main */
0 commit comments