2 // File: HID_Utilities_External.h
4 // Abstract: External interface for HID Utilities, can be used with either library or source
5 // Check notes below for usage. Some type casting is required so library is framework and carbon free
7 // Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. ("Apple")
8 // in consideration of your agreement to the following terms, and your use,
9 // installation, modification or redistribution of this Apple software
10 // constitutes acceptance of these terms. If you do not agree with these
11 // terms, please do not use, install, modify or redistribute this Apple
14 // In consideration of your agreement to abide by the following terms, and
15 // subject to these terms, Apple grants you a personal, non - exclusive
16 // license, under Apple's copyrights in this original Apple software ( the
17 // "Apple Software" ), to use, reproduce, modify and redistribute the Apple
18 // Software, with or without modifications, in source and / or binary forms;
19 // provided that if you redistribute the Apple Software in its entirety and
20 // without modifications, you must retain this notice and the following text
21 // and disclaimers in all such redistributions of the Apple Software. Neither
22 // the name, trademarks, service marks or logos of Apple Inc. may be used to
23 // endorse or promote products derived from the Apple Software without specific
24 // prior written permission from Apple. Except as expressly stated in this
25 // notice, no other rights or licenses, express or implied, are granted by
26 // Apple herein, including but not limited to any patent rights that may be
27 // infringed by your derivative works or by other works in which the Apple
28 // Software may be incorporated.
30 // The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
31 // WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
32 // WARRANTIES OF NON - INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
33 // PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION
34 // ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
36 // IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
37 // CONSEQUENTIAL DAMAGES ( INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 // INTERRUPTION ) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
40 // AND / OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER
41 // UNDER THEORY OF CONTRACT, TORT ( INCLUDING NEGLIGENCE ), STRICT LIABILITY OR
42 // OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 // Copyright © 2001-2009 Apple Inc. All Rights Reserved.
47 #ifndef _HID_Utilities_External_h_
48 #define _HID_Utilities_External_h_
50 // ==================================
56 // ==================================
61 #include "IOHIDLib_.h"
63 // ==================================
65 #ifndef _IOKIT_HID_IOHIDKEYS_H_
67 @typedef IOHIDElementCookie
68 @abstract Abstract data type used as a unique identifier for an element.
71 typedef uint32_t IOHIDElementCookie
;
73 typedef void * IOHIDElementCookie
;
77 // Device and Element Interfaces
79 enum HIDElementTypeMask
81 kHIDElementTypeInput
= 1 << 1,
82 kHIDElementTypeOutput
= 1 << 2,
83 kHIDElementTypeFeature
= 1 << 3,
84 kHIDElementTypeCollection
= 1 << 4,
85 kHIDElementTypeIO
= kHIDElementTypeInput
| kHIDElementTypeOutput
| kHIDElementTypeFeature
,
86 kHIDElementTypeAll
= kHIDElementTypeIO
| kHIDElementTypeCollection
88 typedef enum HIDElementTypeMask HIDElementTypeMask
;
90 // ==================================
92 //*****************************************************
93 #pragma mark - exported globals
94 //-----------------------------------------------------
96 extern IOHIDManagerRef gIOHIDManagerRef
;
97 extern CFMutableArrayRef gDeviceCFArrayRef
;
98 extern CFArrayRef gElementCFArrayRef
;
100 //*************************************************************************
102 // HIDBuildMultiDeviceList( inUsagePages, inUsages, inNumDeviceTypes )
104 // Purpose: builds list of devices with elements (allocates memory and captures devices) in which
105 // the devices could be of different types/usages list is allocated internally within HID
106 // Utilites and can be accessed via accessor functions structures within list are considered
107 // flat and user accessable, but not user modifiable can be called again to rebuild list to
108 // account for new devices (will do the right thing in case of disposing existing list)
109 // usagePage, usage are each a numDeviceTypes sized array of matching usage and usage pages
110 // returns true if succesful
112 // Inputs: inUsagePages - inNumDeviceTypes sized array of matching usage pages
113 // inUsages - inNumDeviceTypes sized array of matching usages
114 // inNumDeviceTypes - number of usage pages & usages
116 // Returns: Boolean - if successful
118 extern Boolean
HIDBuildMultiDeviceList( const UInt32
*inUsagePages
, const UInt32
*inUsages
, int inNumDeviceTypes
);
120 // same as above but this uses a single usagePage and usage
121 extern Boolean
HIDBuildDeviceList (UInt32 usagePage
, UInt32 usage
);
123 // updates the current device list for any new/removed devices
124 // if this is called before HIDBuildDeviceList the it functions like HIDBuildMultiDeviceList
125 // usagePage, usage are each a numDeviceTypes sized array of matching usage and usage pages
126 // returns true if successful which means if any device were added or removed (the device config changed)
127 extern Boolean
HIDUpdateDeviceList( const UInt32
*inUsagePages
, const UInt32
*inUsages
, int inNumDeviceTypes
);
130 // release list built by above function
131 // MUST be called prior to application exit to properly release devices
132 // if not called (or app crashes) devices can be recovered by pluging into different location in USB chain
133 extern void HIDReleaseDeviceList (void);
135 //*************************************************************************
137 // HIDRebuildDevices( )
139 // Purpose: rebuilds the (internal) list of devices
146 extern void HIDRebuildDevices( void );
148 // does a device list exist
149 extern unsigned char HIDHaveDeviceList (void);
151 // how many HID devices have been found
152 // returns 0 if no device list exist
153 extern UInt32
HIDCountDevices (void);
155 // how many elements does a specific device have
156 // returns 0 if device is invalid or NULL
157 // uses mask of HIDElementTypeMask to restrict element found
158 // use kHIDElementTypeIO to get non-collection elements
159 extern UInt32
HIDCountDeviceElements ( IOHIDDeviceRef inIOHIDDeviceRef
, HIDElementTypeMask typeMask
);
161 // get the first device in the device list
162 // returns NULL if no list exists
163 extern IOHIDDeviceRef
HIDGetFirstDevice (void);
165 // get next device in list given current device as parameter
166 // returns NULL if end of list
167 extern IOHIDDeviceRef
HIDGetNextDevice (IOHIDDeviceRef inIOHIDDeviceRef
);
169 // get the first element of device passed in as parameter
170 // returns NULL if no list exists or device does not exists or is NULL
171 // uses mask of HIDElementTypeMask to restrict element found
172 // use kHIDElementTypeIO to get previous HIDGetFirstDeviceElement functionality
173 extern IOHIDElementRef
HIDGetFirstDeviceElement (IOHIDDeviceRef inIOHIDDeviceRef
, HIDElementTypeMask typeMask
);
175 // get next element of given device in list given current element as parameter
176 // will walk down each collection then to next element or collection (depthwise traverse)
177 // returns NULL if end of list
178 // uses mask of HIDElementTypeMask to restrict element found
179 // use kHIDElementTypeIO to get previous HIDGetNextDeviceElement functionality
180 extern IOHIDElementRef
HIDGetNextDeviceElement ( IOHIDElementRef inIOHidElementRef
, HIDElementTypeMask typeMask
);
182 // get previous element of given device in list given current element as parameter
183 // this walks directly up the tree to the top element and does not search at each level
184 // returns NULL if beginning of list
185 // uses mask of HIDElementTypeMask to restrict element found
186 // use kHIDElementTypeIO to get non-collection elements
187 extern IOHIDElementRef
HIDGetPreviousDeviceElement (IOHIDElementRef inIOHidElementRef
, HIDElementTypeMask typeMask
);
189 // returns C string type name given a type enumeration passed in as parameter( see IOHIDKeys.h )
190 // returns empty string for invalid types
191 extern void HIDGetTypeName( IOHIDElementType inIOHIDElementType
, char * outCStrName
);
192 extern void HIDGetUsageName (const long valueUsagePage
, const long valueUsage
, char * cstrName
);
193 extern CFStringRef
HIDCopyUsageName( long inUsagePage
, long inUsage
);
195 // ==================================
197 // Element Event Queue and Value Interfaces
201 kDefaultUserMin
= 0, // default user min and max used for scaling
202 kDefaultUserMax
= 255
207 kDeviceQueueSize
= 50 // this is wired kernel memory so should be set to as small as possible
208 // but should account for the maximum possible events in the queue
209 // USB updates will likely occur at 100 Hz so one must account for this rate of
210 // if states change quickly (updates are only posted on state changes)
213 // ==================================
215 // queues specific element, performing any device queue set up required
216 extern int HIDQueueElement (IOHIDDeviceRef inIOHIDDeviceRef
, IOHIDElementRef inIOHidElementRef
);
218 // adds all elements to queue, performing any device queue set up required
219 extern int HIDQueueDevice (IOHIDDeviceRef inIOHIDDeviceRef
);
221 // removes element for queue, if last element in queue will release queue and device
222 extern int HIDDequeueElement (IOHIDDeviceRef inIOHIDDeviceRef
, IOHIDElementRef inIOHidElementRef
);
224 // completely removes all elements from queue and releases queue and device
225 extern int HIDDequeueDevice (IOHIDDeviceRef inIOHIDDeviceRef
);
227 // releases all device queues for quit or rebuild (must be called)
228 extern int HIDReleaseAllDeviceQueues (void);
230 // returns true if an event is avialable for the element and fills out *pHIDEvent structure, returns false otherwise
231 // pHIDEvent is a poiner to a IOHIDEventStruct, using void here for compatibility, users can cast a required
232 extern unsigned char HIDGetEvent( IOHIDDeviceRef inIOHIDDeviceRef
, IOHIDValueRef
* pIOHIDValueRef
);
234 // ==================================
236 // Conguration and Save Interfaces
240 kPercentMove
= 10 // precent of overall range a element must move to register
247 // need to add serial number when I have a test case
261 IOHIDElementCookie cookie
; // always 32 bits
264 typedef struct recSaveHID recSaveHID
;
265 typedef recSaveHID
* pRecSaveHID
;
267 // get vendor name from vendor ID
268 extern Boolean
HIDGetVendorNameFromVendorID( long inVendorID
, char * outCStrName
);
270 // get product name from vendor/product ID
271 extern Boolean
HIDGetProductNameFromVendorProductID( long inVendorID
, long inProductID
, char * outCStrName
);
273 // get element name from vendor id/product id look up ( using element cookie )
274 extern Boolean
HIDGetElementNameFromVendorProductCookie (int inVendorID
, int inProductID
, IOHIDElementCookie inCookie
, char * outCStrName
);
276 // get element name from vendor id/product id look up ( using element usage page & usage )
277 extern Boolean
HIDGetElementNameFromVendorProductUsage( long inVendorID
, long inProductID
, long inUsagePage
, long inUsage
, char * inCStrName
);
279 // utility routines to dump device or element info
280 extern void HIDDumpDeviceInfo( IOHIDDeviceRef inIOHIDDeviceRef
);
281 extern void HIDDumpElementInfo( IOHIDElementRef inIOHIDElementRef
);
282 extern void HIDDumpElementCalibrationInfo( IOHIDElementRef inIOHIDElementRef
);
284 // polls single device's elements for a change greater than kPercentMove. Times out after given time
285 // returns 1 and pointer to element if found
286 // returns 0 and NULL for both parameters if not found
287 extern unsigned char HIDConfigureSingleDeviceAction (IOHIDDeviceRef inIOHIDDeviceRef
, IOHIDElementRef
*outIOHIDElementRef
, float timeout
);
289 //*************************************************************************
291 // HIDConfigureAction( outDeviceRef, outElementRef, inTimeout )
293 // Purpose: polls all devices and elements for a change greater than kPercentMove.
294 // Times out after given time returns 1 and pointer to device and element
295 // if found; returns 0 and NULL for both parameters if not found
297 // Inputs: outDeviceRef - address where to store the device
298 // outElementRef - address where to store the element
299 // inTimeout - the timeout
300 // Returns: Boolean - TRUE if successful
301 // outDeviceRef - the device
302 // outElementRef - the element
305 extern Boolean
HIDConfigureAction( IOHIDDeviceRef
* outDeviceRef
, IOHIDElementRef
*outElementRef
, float inTimeout
);
307 //*************************************************************************
309 // HIDSaveElementPref( inKeyCFStringRef, inAppCFStringRef, inDeviceRef, inElementRef )
311 // Purpose: Save the device & element values into the specified key in the specified applications preferences
313 // Inputs: inKeyCFStringRef - the preference key
314 // inAppCFStringRef - the application identifier
315 // inDeviceRef - the device
316 // inElementRef - the element
317 // Returns: Boolean - if successful
320 extern Boolean
HIDSaveElementPref( const CFStringRef inKeyCFStringRef
,
321 CFStringRef inAppCFStringRef
,
322 IOHIDDeviceRef inDeviceRef
,
323 IOHIDElementRef inElementRef
);
325 //*************************************************************************
327 // HIDRestoreElementPref( inKeyCFStringRef, inAppCFStringRef, outDeviceRef, outElementRef )
329 // Purpose: Find the specified preference in the specified application
331 // Inputs: inKeyCFStringRef - the preference key
332 // inAppCFStringRef - the application identifier
333 // outDeviceRef - address where to restore the device
334 // outElementRef - address where to restore the element
335 // Returns: Boolean - if successful
336 // outDeviceRef - the device
337 // outElementRef - the element
340 extern Boolean
HIDRestoreElementPref( CFStringRef inKeyCFStringRef
,
341 CFStringRef inAppCFStringRef
,
342 IOHIDDeviceRef
*outDeviceRef
,
343 IOHIDElementRef
*outElementRef
);
345 //*************************************************************************
347 // HIDFindDeviceAndElement( inSearchInfo, outFoundDevice, outFoundElement )
349 // Purpose: find the closest matching device and element for this action
351 // Notes: matches device: serial, vendorID, productID, location, inUsagePage, usage
352 // matches element: cookie, inUsagePage, usage,
354 // Inputs: inSearchInfo - the device & element info we searching for
355 // outFoundDevice - the address of the best matching device
356 // outFoundElement - the address of the best matching element
358 // Returns: Boolean - TRUE if we find a match
359 // outFoundDevice - the best matching device
360 // outFoundElement - the best matching element
363 extern Boolean
HIDFindDeviceAndElement( const recSaveHID
* inSearchInfo
,
364 IOHIDDeviceRef
* outFoundDevice
,
365 IOHIDElementRef
*outFoundElement
);
367 // -- These are routines to use if the applcationwants HID Utilities to do the file handling --
368 // Note: the FILE * is a MachO posix FILE and will not likely work directly with MW MSL FILE * type.
370 // take input records, save required info
371 // assume file is open and at correct position.
372 void HIDSaveElementConfig (FILE * fileRef
, IOHIDDeviceRef inIOHIDDeviceRef
, IOHIDElementRef inIOHidElementRef
, int actionCookie
);
374 // takes a file, reads one record (assume file position is correct and file is open)
375 // search for matching device
376 // return tIOHIDDeviceRef, tIOHIDElementRef and cookie for action
377 int HIDRestoreElementConfig (FILE * fileRef
, IOHIDDeviceRef
*outIOHIDDeviceRef
, IOHIDElementRef
*outIOHIDElementRef
);
379 // -- These are routines to use if the client wants to use their own file handling --
381 // Set up a config record for saving
382 // takes an input records, returns record user can save as they want
383 // Note: the save rec must be pre-allocated by the calling app and will be filled out
384 void HIDSetElementConfig (pRecSaveHID pConfigRec
, IOHIDDeviceRef inIOHIDDeviceRef
, IOHIDElementRef inIOHidElementRef
, int actionCookie
);
386 // Get matching element from config record
387 // takes a pre-allocated and filled out config record
388 // search for matching device
389 // return tIOHIDDeviceRef, tIOHIDElementRef and cookie for action
390 int HIDGetElementConfig (pRecSaveHID pConfigRec
, IOHIDDeviceRef
*outIOHIDDeviceRef
, IOHIDElementRef
*outIOHIDElementRef
);
392 // ==================================
394 // Error reporter, can be set to report however the application desires
395 extern void HIDReportError (char * strError
);
397 // Error with numeric code reporter, can be set to report however the application desires
398 extern void HIDReportErrorNum (char * strError
, int numError
);
405 #endif // _HID_Utilities_External_h_