common: prevent buffer overflow
[supercollider.git] / lang / LangPrimSource / HID_Utilities / IOHIDDevice_.c
blob6b73cf08065a4977faebe5ad569254098133465f
1 //
2 // File: IOHIDDevice_.c of HID Utilities
3 //
4 // Created: 9/21/06
5 //
6 // Contains: convieance functions for IOHIDDeviceGetProperty
7 //
8 // Copyright © 2007-2009 Apple Inc., All Rights Reserved
9 //
10 // Disclaimer: IMPORTANT: This Apple software is supplied to you by
11 // Apple Inc. ("Apple") in consideration of your agreement to the
12 // following terms, and your use, installation, modification or
13 // redistribution of this Apple software constitutes acceptance of these
14 // terms. If you do not agree with these terms, please do not use,
15 // install, modify or redistribute this Apple software.
17 // In consideration of your agreement to abide by the following terms, and
18 // subject to these terms, Apple grants you a personal, non-exclusive
19 // license, under Apple's copyrights in this original Apple software (the
20 // "Apple Software"), to use, reproduce, modify and redistribute the Apple
21 // Software, with or without modifications, in source and/or binary forms;
22 // provided that if you redistribute the Apple Software in its entirety and
23 // without modifications, you must retain this notice and the following
24 // text and disclaimers in all such redistributions of the Apple Software.
25 // Neither the name, trademarks, service marks or logos of Apple Inc.
26 // may be used to endorse or promote products derived from the Apple
27 // Software without specific prior written permission from Apple. Except
28 // as expressly stated in this notice, no other rights or licenses, express
29 // or implied, are granted by Apple herein, including but not limited to
30 // any patent rights that may be infringed by your derivative works or by
31 // other works in which the Apple Software may be incorporated.
33 // The Apple Software is provided by Apple on an "AS IS" basis. APPLE
34 // MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
35 // THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
36 // FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
37 // OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
39 // IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
40 // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 // INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
43 // MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
44 // AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
45 // STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
46 // POSSIBILITY OF SUCH DAMAGE.
49 //*****************************************************
50 #pragma mark - includes & imports
51 //-----------------------------------------------------
53 #include "IOHIDDevice_.h"
55 //*****************************************************
56 #pragma mark - typedef's, struct's, enums, defines, etc.
57 //-----------------------------------------------------
59 #define kIOHIDDevice_TransactionKey "DeviceTransactionRef"
60 #define kIOHIDDevice_QueueKey "DeviceQueueRef"
62 //*****************************************************
63 #pragma mark - local (static) function prototypes
64 //-----------------------------------------------------
66 static Boolean IOHIDDevice_GetLongProperty( IOHIDDeviceRef inIOHIDDeviceRef, CFStringRef inKey, long * outValue );
67 static void IOHIDDevice_SetLongProperty( IOHIDDeviceRef inIOHIDDeviceRef, CFStringRef inKey, long inValue );
69 //*****************************************************
70 #pragma mark - exported globals
71 //-----------------------------------------------------
73 //*****************************************************
74 #pragma mark - local (static) globals
75 //-----------------------------------------------------
77 //*****************************************************
78 #pragma mark - exported function implementations
79 //-----------------------------------------------------
81 //*************************************************************************
83 // HIDIsValidDevice( inIOHIDDeviceRef )
85 // Purpose: validate this device
87 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
89 // Returns: Boolean - TRUE if we find the device in our( internal ) device list
92 Boolean HIDIsValidDevice( IOHIDDeviceRef inIOHIDDeviceRef )
94 Boolean result = FALSE; // assume failure (pessimist!)
96 if ( inIOHIDDeviceRef ) {
97 if ( CFGetTypeID( inIOHIDDeviceRef ) == IOHIDDeviceGetTypeID() ) {
98 result = TRUE;
101 return result;
104 //*************************************************************************
106 // IOHIDDevice_GetTransport( inIOHIDDeviceRef )
108 // Purpose: get the Transport CFString for this device
110 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
112 // Returns: CFStringRef - the Transport for this device
115 CFStringRef IOHIDDevice_GetTransport( IOHIDDeviceRef inIOHIDDeviceRef )
117 assert( IOHIDDeviceGetTypeID() == CFGetTypeID( inIOHIDDeviceRef ) );
118 return IOHIDDeviceGetProperty( inIOHIDDeviceRef, CFSTR( kIOHIDTransportKey ) );
121 //*************************************************************************
123 // IOHIDDevice_GetVendorID( inIOHIDDeviceRef )
125 // Purpose: get the vendor ID for this device
127 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
129 // Returns: long - the vendor ID for this device
132 long IOHIDDevice_GetVendorID( IOHIDDeviceRef inIOHIDDeviceRef )
134 long result = 0;
135 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDVendorIDKey ), &result );
136 return result;
137 } // IOHIDDevice_GetVendorID
139 //*************************************************************************
141 // IOHIDDevice_GetVendorIDSource( inIOHIDDeviceRef )
143 // Purpose: get the VendorIDSource for this device
145 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
147 // Returns: long - the VendorIDSource for this device
150 long IOHIDDevice_GetVendorIDSource( IOHIDDeviceRef inIOHIDDeviceRef )
152 long result = 0;
153 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDVendorIDSourceKey ), &result );
154 return result;
157 //*************************************************************************
159 // IOHIDDevice_GetProductID( inIOHIDDeviceRef )
161 // Purpose: get the product ID for this device
163 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
165 // Returns: long - the product ID for this device
168 long IOHIDDevice_GetProductID( IOHIDDeviceRef inIOHIDDeviceRef )
170 long result = 0;
171 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDProductIDKey ), &result );
172 return result;
173 } // IOHIDDevice_GetProductID
175 //*************************************************************************
177 // IOHIDDevice_GetVersionNumber( inIOHIDDeviceRef )
179 // Purpose: get the VersionNumber CFString for this device
181 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
183 // Returns: CFStringRef - the VersionNumber for this device
186 long IOHIDDevice_GetVersionNumber( IOHIDDeviceRef inIOHIDDeviceRef )
188 long result = 0;
189 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDVersionNumberKey ), &result );
190 return result;
193 //*************************************************************************
195 // IOHIDDevice_GetManufacturer( inIOHIDDeviceRef )
197 // Purpose: get the Manufacturer CFString for this device
199 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
201 // Returns: CFStringRef - the Manufacturer for this device
204 CFStringRef IOHIDDevice_GetManufacturer( IOHIDDeviceRef inIOHIDDeviceRef )
206 assert( IOHIDDeviceGetTypeID() == CFGetTypeID( inIOHIDDeviceRef ) );
207 return IOHIDDeviceGetProperty( inIOHIDDeviceRef, CFSTR( kIOHIDManufacturerKey ) );
208 } // IOHIDDevice_GetManufacturer
210 //*************************************************************************
212 // IOHIDDevice_GetProduct( inIOHIDDeviceRef )
214 // Purpose: get the Product CFString for this device
216 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
218 // Returns: CFStringRef - the Product for this device
221 CFStringRef IOHIDDevice_GetProduct( IOHIDDeviceRef inIOHIDDeviceRef )
223 assert( IOHIDDeviceGetTypeID() == CFGetTypeID( inIOHIDDeviceRef ) );
224 return IOHIDDeviceGetProperty( inIOHIDDeviceRef, CFSTR( kIOHIDProductKey ) );
225 } // IOHIDDevice_GetProduct
227 //*************************************************************************
229 // IOHIDDevice_GetSerialNumber( inIOHIDDeviceRef )
231 // Purpose: get the SerialNumber CFString for this device
233 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
235 // Returns: CFStringRef - the SerialNumber for this device
238 CFStringRef IOHIDDevice_GetSerialNumber( IOHIDDeviceRef inIOHIDDeviceRef )
240 assert( IOHIDDeviceGetTypeID() == CFGetTypeID( inIOHIDDeviceRef ) );
241 return IOHIDDeviceGetProperty( inIOHIDDeviceRef, CFSTR( kIOHIDSerialNumberKey ) );
244 //*************************************************************************
246 // IOHIDDevice_GetCountryCode( inIOHIDDeviceRef )
248 // Purpose: get the CountryCode CFString for this device
250 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
252 // Returns: CFStringRef - the CountryCode for this device
255 long IOHIDDevice_GetCountryCode( IOHIDDeviceRef inIOHIDDeviceRef )
257 long result = 0;
258 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDCountryCodeKey ), &result );
259 return result;
262 //*************************************************************************
264 // IOHIDDevice_GetLocationID( inIOHIDDeviceRef )
266 // Purpose: get the location ID for this device
268 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
270 // Returns: long - the location ID for this device
273 long IOHIDDevice_GetLocationID( IOHIDDeviceRef inIOHIDDeviceRef )
275 long result = 0;
276 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDLocationIDKey ), &result );
277 return result;
278 } // IOHIDDevice_GetLocationID
280 //*************************************************************************
282 // IOHIDDevice_GetUsage( inIOHIDDeviceRef )
284 // Purpose: get the usage for this device
286 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
288 // Returns: uint32_t - the usage for this device
291 uint32_t IOHIDDevice_GetUsage( IOHIDDeviceRef inIOHIDDeviceRef )
293 uint32_t result = 0;
294 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDDeviceUsageKey ), ( long * ) &result );
295 return result;
296 } // IOHIDDevice_GetUsage
298 //*************************************************************************
300 // IOHIDDevice_GetUsagePage( inIOHIDDeviceRef )
302 // Purpose: get the usage page for this device
304 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
306 // Returns: uint32_t - the usage page for this device
309 uint32_t IOHIDDevice_GetUsagePage( IOHIDDeviceRef inIOHIDDeviceRef )
311 long result = 0;
312 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDDeviceUsagePageKey ), &result );
313 return result;
314 } // IOHIDDevice_GetUsagePage
316 //*************************************************************************
318 // IOHIDDevice_GetUsagePairs( inIOHIDDeviceRef )
320 // Purpose: get the UsagePairs CFString for this device
322 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
324 // Returns: CFArrayRef - the UsagePairs for this device
327 CFArrayRef IOHIDDevice_GetUsagePairs( IOHIDDeviceRef inIOHIDDeviceRef )
329 assert( IOHIDDeviceGetTypeID() == CFGetTypeID( inIOHIDDeviceRef ) );
330 return IOHIDDeviceGetProperty( inIOHIDDeviceRef, CFSTR( kIOHIDDeviceUsagePairsKey ) );
333 //*************************************************************************
335 // IOHIDDevice_GetPrimaryUsage( inIOHIDDeviceRef )
337 // Purpose: get the PrimaryUsage CFString for this device
339 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
341 // Returns: CFStringRef - the PrimaryUsage for this device
344 uint32_t IOHIDDevice_GetPrimaryUsage( IOHIDDeviceRef inIOHIDDeviceRef )
346 long result = 0;
347 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDPrimaryUsageKey ), &result );
348 return result;
351 //*************************************************************************
353 // IOHIDDevice_GetPrimaryUsagePage( inIOHIDDeviceRef )
355 // Purpose: get the PrimaryUsagePage CFString for this device
357 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
359 // Returns: CFStringRef - the PrimaryUsagePage for this device
362 uint32_t IOHIDDevice_GetPrimaryUsagePage( IOHIDDeviceRef inIOHIDDeviceRef )
364 long result = 0;
365 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDPrimaryUsagePageKey ), &result );
366 return result;
369 //*************************************************************************
371 // IOHIDDevice_GetMaxInputReportSize( inIOHIDDeviceRef )
373 // Purpose: get the MaxInputReportSize CFString for this device
375 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
377 // Returns: long - the MaxInputReportSize for this device
380 long IOHIDDevice_GetMaxInputReportSize( IOHIDDeviceRef inIOHIDDeviceRef )
382 long result = 0;
383 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDMaxInputReportSizeKey ), &result );
384 return result;
387 //*************************************************************************
389 // IOHIDDevice_GetMaxOutputReportSize( inIOHIDDeviceRef )
391 // Purpose: get the MaxOutputReportSize for this device
393 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
395 // Returns: long - the MaxOutput for this device
398 long IOHIDDevice_GetMaxOutputReportSize( IOHIDDeviceRef inIOHIDDeviceRef )
400 long result = 0;
401 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDMaxOutputReportSizeKey ), &result );
402 return result;
405 //*************************************************************************
407 // IOHIDDevice_GetMaxFeatureReportSize( inIOHIDDeviceRef )
409 // Purpose: get the MaxFeatureReportSize for this device
411 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
413 // Returns: long - the MaxFeatureReportSize for this device
416 long IOHIDDevice_GetMaxFeatureReportSize( IOHIDDeviceRef inIOHIDDeviceRef )
418 long result = 0;
419 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDMaxFeatureReportSizeKey ), &result );
420 return result;
423 //*************************************************************************
425 // IOHIDDevice_GetReportInterval( inIOHIDDeviceRef )
427 // Purpose: get the ReportInterval for this device
429 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
431 // Returns: long - the ReportInterval for this device
433 #ifndef kIOHIDReportIntervalKey
434 #define kIOHIDReportIntervalKey "ReportInterval"
435 #endif
436 long IOHIDDevice_GetReportInterval( IOHIDDeviceRef inIOHIDDeviceRef )
438 long result = 0;
439 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDReportIntervalKey ), &result );
440 return result;
443 //*************************************************************************
445 // IOHIDDevice_GetQueue( inIOHIDDeviceRef )
447 // Purpose: get the Queue for this device
449 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
451 // Returns: IOHIDQueueRef - the Queue for this device
454 IOHIDQueueRef IOHIDDevice_GetQueue( IOHIDDeviceRef inIOHIDDeviceRef )
456 IOHIDQueueRef result = 0;
457 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDDevice_QueueKey ), ( long * ) &result );
459 if ( result ) {
460 assert( IOHIDQueueGetTypeID() == CFGetTypeID( result ) );
462 return result;
465 //*************************************************************************
467 // IOHIDDevice_SetQueue( inIOHIDDeviceRef, inQueueRef )
469 // Purpose: Set the Queue for this device
471 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
472 // inQueueRef - the Queue reference
474 // Returns: nothing
477 void IOHIDDevice_SetQueue( IOHIDDeviceRef inIOHIDDeviceRef, IOHIDQueueRef inQueueRef )
479 IOHIDDevice_SetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDDevice_QueueKey ), ( long ) inQueueRef );
482 //*************************************************************************
484 // IOHIDDevice_GetTransaction( inIOHIDDeviceRef )
486 // Purpose: get the Transaction for this device
488 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
490 // Returns: IOHIDTransactionRef - the Transaction for this device
493 IOHIDTransactionRef IOHIDDevice_GetTransaction( IOHIDDeviceRef inIOHIDDeviceRef )
495 IOHIDTransactionRef result = 0;
496 ( void ) IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDDevice_TransactionKey ), ( long * ) &result );
497 return result;
500 //*************************************************************************
502 // IOHIDDevice_SetTransaction( inIOHIDDeviceRef, inTransactionRef )
504 // Purpose: Set the Transaction for this device
506 // Inputs: inIOHIDDeviceRef - the IDHIDDeviceRef for this device
507 // inTransactionRef - the Transaction reference
509 // Returns: nothing
512 void IOHIDDevice_SetTransaction( IOHIDDeviceRef inIOHIDDeviceRef, IOHIDTransactionRef inTransactionRef )
514 IOHIDDevice_SetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDDevice_TransactionKey ), ( long ) inTransactionRef );
517 //*****************************************************
518 #pragma mark - local (static) function implementations
519 //-----------------------------------------------------
521 //*************************************************************************
523 // IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, inKey, outValue )
525 // Purpose: convieance function to return a long property of a device
527 // Inputs: inIOHIDDeviceRef - the device
528 // inKey - CFString for the
529 // outValue - address where to restore the element
530 // Returns: the action cookie
531 // outValue - the device
534 static Boolean IOHIDDevice_GetLongProperty( IOHIDDeviceRef inIOHIDDeviceRef, CFStringRef inKey, long * outValue )
536 Boolean result = FALSE;
538 if ( inIOHIDDeviceRef ) {
539 assert( IOHIDDeviceGetTypeID() == CFGetTypeID( inIOHIDDeviceRef ) );
541 CFTypeRef tCFTypeRef = IOHIDDeviceGetProperty( inIOHIDDeviceRef, inKey );
543 if ( tCFTypeRef ) {
544 // if this is a number
545 if ( CFNumberGetTypeID() == CFGetTypeID( tCFTypeRef ) ) {
546 // get it's value
547 result = CFNumberGetValue( ( CFNumberRef ) tCFTypeRef, kCFNumberSInt32Type, outValue );
551 return result;
552 } // IOHIDDevice_GetLongProperty
554 //*************************************************************************
556 // IOHIDDevice_SetLongProperty( inIOHIDDeviceRef, inKey, inValue )
558 // Purpose: convieance function to set a long property of an Device
560 // Inputs: inIOHIDDeviceRef - the Device
561 // inKey - CFString for the key
562 // inValue - the value to set it to
563 // Returns: nothing
566 static void IOHIDDevice_SetLongProperty( IOHIDDeviceRef inIOHIDDeviceRef, CFStringRef inKey, long inValue )
568 CFNumberRef tCFNumberRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberSInt32Type, &inValue );
570 if ( tCFNumberRef ) {
571 IOHIDDeviceSetProperty( inIOHIDDeviceRef, inKey, tCFNumberRef );
572 CFRelease( tCFNumberRef );
574 } // IOHIDDevice_SetLongProperty
576 //*****************************************************