2 File: HID_Utilities_External.h
4 Contains: Definition of the HID Utilities exported functions
8 Copyright: Copyright © 2002 Apple Computer, Inc., All Rights Reserved
10 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
11 ("Apple") in consideration of your agreement to the following terms, and your
12 use, installation, modification or redistribution of this Apple software
13 constitutes acceptance of these terms. If you do not agree with these terms,
14 please do not use, install, modify or redistribute this Apple software.
16 In consideration of your agreement to abide by the following terms, and subject
17 to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
18 copyrights in this original Apple software (the "Apple Software"), to use,
19 reproduce, modify and redistribute the Apple Software, with or without
20 modifications, in source and/or binary forms; provided that if you redistribute
21 the Apple Software in its entirety and without modifications, you must retain
22 this notice and the following text and disclaimers in all such redistributions of
23 the Apple Software. Neither the name, trademarks, service marks or logos of
24 Apple Computer, Inc. may be used to endorse or promote products derived from the
25 Apple Software without specific prior written permission from Apple. Except as
26 expressly stated in this notice, no other rights or licenses, express or implied,
27 are granted by Apple herein, including but not limited to any patent rights that
28 may be infringed by your derivative works or by other works in which the Apple
29 Software may be incorporated.
31 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
32 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
33 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
35 COMBINATION WITH YOUR PRODUCTS.
37 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
38 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
39 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
41 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
42 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
43 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 #ifndef _HID_Utilities_External_h_
47 #define _HID_Utilities_External_h_
49 // ==================================
53 typedef void (*IOHIDCallbackFunction
)(void * target
, unsigned long result
, void* refcon
, void * sender
);
55 #include <IOKit/hid/IOHIDLib.h>
56 #endif TARGET_RT_MAC_CFM
61 #include <IOKit/hid/IOHIDUsageTables.h>
63 //#include "PID.h" // NOTE: These are now in <IOHIDUsageTables.h>
64 //#include "IOHIDPowerUsage.h" // NOTE: These are now in <IOHIDUsageTables.h>
67 // ==================================
73 // ==================================
74 // Device and Element Interfaces
76 typedef enum HIDElementTypeMask
78 kHIDElementTypeInput
= 1 << 1,
79 kHIDElementTypeOutput
= 1 << 2,
80 kHIDElementTypeFeature
= 1 << 3,
81 kHIDElementTypeCollection
= 1 << 4,
82 kHIDElementTypeIO
= kHIDElementTypeInput
| kHIDElementTypeOutput
| kHIDElementTypeFeature
,
83 kHIDElementTypeAll
= kHIDElementTypeIO
| kHIDElementTypeCollection
88 unsigned long type
; // the type defined by IOHIDElementType in IOHIDKeys.h
89 long usagePage
; // usage page from IOUSBHIDParser.h which defines general usage
90 long usage
; // usage within above page from IOUSBHIDParser.h which defines specific usage
91 void * cookie
; // unique value (within device of specific vendorID and productID) which identifies element, will NOT change
92 long min
; // reported min value possible
93 long max
; // reported max value possible
94 long scaledMin
; // reported scaled min value possible
95 long scaledMax
; // reported scaled max value possible
96 long size
; // size in bits of data return from element
97 unsigned char relative
; // are reports relative to last report (deltas)
98 unsigned char wrapping
; // does element wrap around (one value higher than max is min)
99 unsigned char nonLinear
; // are the values reported non-linear relative to element movement
100 unsigned char preferredState
; // does element have a preferred state (such as a button)
101 unsigned char nullState
; // does element have null state
102 long units
; // units value is reported in (not used very often)
103 long unitExp
; // exponent for units (also not used very often)
104 char name
[256]; // name of element (c string)
107 long calMin
; // min returned value
108 long calMax
; // max returned value (calibrate call)
109 long userMin
; // user set value to scale to (scale call)
112 struct recElement
* pPrevious
; // previous element (NULL at list head)
113 struct recElement
* pChild
; // next child (only of collections)
114 struct recElement
* pSibling
; // next sibling (for elements and collections)
118 typedef struct recElement recElement
;
119 typedef recElement
* pRecElement
;
123 void * interface
; // interface to device, NULL = no interface
124 void * queue
; // device queue, NULL = no queue
125 void * queueRunLoopSource
; // device queue run loop source, NULL == no source
126 void * transaction
; // output transaction interface, NULL == no interface
127 void * notification
; // notifications
128 char transport
[256]; // device transport (c string)
129 long vendorID
; // id for device vendor, unique across all devices
130 long productID
; // id for particular product, unique across all of a vendors devices
131 long version
; // version of product
132 char manufacturer
[256]; // name of manufacturer
133 char product
[256]; // name of product
134 char serial
[256]; // serial number of specific product, can be assumed unique across specific product or specific vendor (not used often)
135 long locID
; // long representing location in USB (or other I/O) chain which device is pluged into, can identify specific device on machine
136 long usage
; // usage page from IOUSBHID Parser.h which defines general usage
137 long usagePage
; // usage within above page from IOUSBHID Parser.h which defines specific usage
138 long totalElements
; // number of total elements (should be total of all elements on device including collections) (calculated, not reported by device)
139 long features
; // number of elements of type kIOHIDElementTypeFeature
140 long inputs
; // number of elements of type kIOHIDElementTypeInput_Misc or kIOHIDElementTypeInput_Button or kIOHIDElementTypeInput_Axis or kIOHIDElementTypeInput_ScanCodes
141 long outputs
; // number of elements of type kIOHIDElementTypeOutput
142 long collections
; // number of elements of type kIOHIDElementTypeCollection
143 long axis
; // number of axis (calculated, not reported by device)
144 long buttons
; // number of buttons (calculated, not reported by device)
145 long hats
; // number of hat switches (calculated, not reported by device)
146 long sliders
; // number of sliders (calculated, not reported by device)
147 long dials
; // number of dials (calculated, not reported by device)
148 long wheels
; // number of wheels (calculated, not reported by device)
149 recElement
* pListElements
; // head of linked list of elements
150 struct recDevice
* pNext
; // next device
152 typedef struct recDevice recDevice
;
153 typedef recDevice
* pRecDevice
;
155 // ==================================
156 // HID Utilities interface
157 // ==================================
158 // Create and open an interface to device, required prior to extracting values or building queues
159 // Note: appliction now owns the device and must close and release it prior to exiting
160 extern unsigned long HIDCreateOpenDeviceInterface (UInt32 hidDevice
, pRecDevice pDevice
);
162 // builds list of device with elements (allocates memory and captures devices)
163 // list is allcoated internally within HID Utilites and can be accessed via accessor functions
164 // structures within list are considered flat and user accessable, butnot user modifiable
165 // can be called again to rebuild list to account for new devices (will do the right thing in case of disposing existing list)
166 extern Boolean
HIDBuildDeviceList (UInt32 usagePage
, UInt32 usage
);
168 // release list built by above function
169 // MUST be called prior to application exit to properly release devices
170 // if not called (or app crashes) devices can be recovered by pluging into different location in USB chain
171 extern void HIDReleaseDeviceList (void);
173 // does a device list exist
174 extern Boolean
HIDHaveDeviceList (void);
176 // how many HID devices have been found
177 // returns 0 if no device list exist
178 extern UInt32
HIDCountDevices (void);
180 // how many elements does a specific device have
181 // returns 0 if device is invalid or NULL
182 // uses mask of HIDElementTypeMask to restrict element found
183 // use kHIDElementTypeIO to get non-collection elements
184 extern UInt32
HIDCountDeviceElements (pRecDevice pDevice
, HIDElementTypeMask typeMask
);
186 // get the first device in the device list
187 // returns NULL if no list exists
188 extern pRecDevice
HIDGetFirstDevice (void);
190 // get next device in list given current device as parameter
191 // returns NULL if end of list
192 extern pRecDevice
HIDGetNextDevice (pRecDevice pDevice
);
194 // get the first element of device passed in as parameter
195 // returns NULL if no list exists or device does not exists or is NULL
196 // uses mask of HIDElementTypeMask to restrict element found
197 // use kHIDElementTypeIO to get previous HIDGetFirstDeviceElement functionality
198 extern pRecElement
HIDGetFirstDeviceElement (pRecDevice pDevice
, HIDElementTypeMask typeMask
);
200 // get next element of given device in list given current element as parameter
201 // will walk down each collection then to next element or collection (depthwise traverse)
202 // returns NULL if end of list
203 // uses mask of HIDElementTypeMask to restrict element found
204 // use kHIDElementTypeIO to get previous HIDGetNextDeviceElement functionality
205 extern pRecElement
HIDGetNextDeviceElement (pRecElement pElement
, HIDElementTypeMask typeMask
);
207 // get previous element of given device in list given current element as parameter
208 // this wlaks directly up the tree to the top element and does not search at each level
209 // returns NULL if beginning of list
210 // uses mask of HIDElementTypeMask to restrict element found
211 // use kHIDElementTypeIO to get non-collection elements
212 extern pRecElement
HIDGetPreviousDeviceElement (pRecElement pElement
, HIDElementTypeMask typeMask
);
214 // returns C string type name given a type enumeration passed in as parameter (see IOHIDKeys.h)
215 // returns empty string for invlid types
216 extern void HIDGetTypeName (IOHIDElementType theType
, char * cstrName
);
218 // set name from vendor id/product id look up (using cookies)
219 extern Boolean
HIDGetElementNameFromVendorProductCookie (const long vendorID
, const long productID
, const long cookie
, char * pName
);
221 // set name from vendor id/product id look up (using usage page & usage)
222 extern Boolean
HIDGetElementNameFromVendorProductUsage (const long vendorID
, const long productID
, const long pUsagePage
, const long pUsage
, char * pName
);
224 // returns C string usage given usage page and usage passed in as parameters (see IOUSBHIDParser.h)
225 // returns usage page and usage values in string form for unknown values
226 extern void HIDGetUsageName (long valueUsagePage
, long valueUsage
, char * cstrName
);
228 // returns calibrated value given raw value passed in
229 // calibrated value is equal to min and max values returned by HIDGetElementValue since device list built scaled to element reported min and max values
230 extern SInt32
HIDCalibrateValue (SInt32 value
, pRecElement pElement
);
232 // returns scaled value given raw value passed in
233 // scaled value is equal to current value assumed to be in the range of element reported min and max values scaled to user min and max scaled values
234 extern SInt32
HIDScaleValue (SInt32 value
, pRecElement pElement
);
236 // ---------------------------------
237 // convert an element type to a mask
238 extern HIDElementTypeMask
HIDConvertElementTypeToMask (const long type
);
241 extern Boolean
HIDFindDevice(const pRecDevice pSearchDevice
, pRecDevice
*ppFoundDevice
);
243 // find the device and element for this action
244 // Device: serial, vendorID, productID, location, usagePage, usage
245 // Element: cookie, usagePage, usage,
246 extern Boolean
HIDFindActionDeviceAndElement(const pRecDevice pSearchDevice
, const pRecElement pSearchElement
,
247 pRecDevice
*ppFoundDevice
, pRecElement
*ppFoundElement
);
249 // find the device and element for this action
250 // Device: serial, vendorID, productID, location, usagePage, usage
251 // Element: cookie, usagePage, usage,
253 extern Boolean
HIDFindSubElement(const pRecElement pStartElement
, const pRecElement pSearchElement
, pRecElement
*ppFoundElement
);
255 // print out all of an elements information
256 extern int HIDPrintElement(const pRecElement pElement
);
258 // return true if this is a valid device pointer
259 extern Boolean
HIDIsValidDevice(const pRecDevice pSearchDevice
);
261 // return true if this is a valid element pointer for this device
262 extern Boolean
HIDIsValidElement(const pRecDevice pSearchDevice
, const pRecElement pSearchElement
);
264 // ==================================
265 // Element Event Queue and Value Interfaces
269 kDefaultUserMin
= 0, // default user min and max used for scaling
270 kDefaultUserMax
= 255
275 kDeviceQueueSize
= 50 // this is wired kernel memory so should be set to as small as possible
276 // but should account for the maximum possible events in the queue
277 // USB updates will likely occur at 100 Hz so one must account for this rate of
278 // if states change quickly (updates are only posted on state changes)
281 // ==================================
282 // queues specific element, performing any device queue set up required
283 extern unsigned long HIDQueueElement (pRecDevice pDevice
, pRecElement pElement
);
285 // adds all elements to queue, performing any device queue set up required
286 extern unsigned long HIDQueueDevice (pRecDevice pDevice
);
288 // removes element for queue, if last element in queue will release queue and device
289 extern unsigned long HIDDequeueElement (pRecDevice pDevice
, pRecElement pElement
);
291 // completely removes all elements from queue and releases queue and device
292 extern unsigned long HIDDequeueDevice (pRecDevice pDevice
);
294 // releases all device queues for quit or rebuild (must be called)
295 extern unsigned long HIDReleaseAllDeviceQueues (void);
297 // releases interface to device, should be done prior to exiting application (called from HIDReleaseDeviceList)
298 extern unsigned long HIDCloseReleaseInterface (pRecDevice pDevice
);
300 // returns true if an event is avialable for the element and fills out *pHIDEvent structure, returns false otherwise
301 // pHIDEvent is a poiner to a IOHIDEventStruct, using void here for compatibility, users can cast a required
302 extern unsigned char HIDGetEvent (pRecDevice pDevice
, void * pHIDEvent
);
304 // returns current value for element, creating device interface as required, polling element
305 extern long HIDGetElementValue (pRecDevice pDevice
, pRecElement pElement
);
307 // Set an elements value
308 // NOTE: This should only be used when a single element report needs to be sent.
309 // If multiple elements reports are to be send then transactions should be used.
310 // pIOHIDEvent is a poiner to a IOHIDEventStruct, using void here for compatibility, users can cast a required
311 extern long HIDSetElementValue (pRecDevice pDevice
, pRecElement pElement
,void* pIOHIDEvent
);
313 // Set a callback to be called when a queue goes from empty to non-empty
314 extern long HIDSetQueueCallback (pRecDevice pDevice
, IOHIDCallbackFunction callback
);
317 // Get a report from a device
318 extern long HIDGetReport (pRecDevice pDevice
,const IOHIDReportType reportType
, const unsigned long reportID
, void* reportBuffer
, unsigned long* reportBufferSize
);
320 // Send a report to a device
321 extern long HIDSetReport (pRecDevice pDevice
,const IOHIDReportType reportType
, const unsigned long reportID
, void* reportBuffer
, const unsigned long reportBufferSize
);
324 // ==================================
325 // HUD utilities interfaces
327 // returns calibrated value given raw value passed in
328 // calibrated value is equal to min and max values returned by HIDGetElementValue since device list built scaled to element reported min and max values
329 extern long HIDCalibrateValue (long value
, pRecElement pElement
);
331 // returns scaled value given raw value passed in
332 // scaled value is equal to current value assumed to be in the range of element reported min and max values scaled to user min and max scaled values
333 extern long HIDScaleValue (long value
, pRecElement pElement
);
335 // convert an element type to a mask
336 extern HIDElementTypeMask
HIDConvertElementTypeToMask (const long type
);
339 extern Boolean
HIDFindDevice(const pRecDevice pSearchDevice
, pRecDevice
*ppFoundDevice
);
341 // find the device and element for this action
342 // Device: serial, vendorID, productID, location, usagePage, usage
343 // Element: cookie, usagePage, usage,
344 extern Boolean
HIDFindActionDeviceAndElement(const pRecDevice pSearchDevice
, const pRecElement pSearchElement
,
345 pRecDevice
*ppFoundDevice
, pRecElement
*ppFoundElement
);
346 // find the device and element for this action
347 // Device: serial, vendorID, productID, location, usagePage, usage
348 // Element: cookie, usagePage, usage,
350 extern Boolean
HIDFindSubElement(const pRecElement pStartElement
, const pRecElement pSearchElement
, pRecElement
*ppFoundElement
);
352 // print out all of an elements information
353 extern int HIDPrintElement(const pRecElement pElement
);
355 // return true if this is a valid device pointer
356 extern Boolean
HIDIsValidDevice(const pRecDevice pSearchDevice
);
358 // return true if this is a valid element pointer for this device
359 extern Boolean
HIDIsValidElement(const pRecDevice pSearchDevice
, const pRecElement pSearchElement
);
361 // ==================================
362 // Name Lookup Interfaces
364 // set name from vendor id/product id look up (using cookies)
365 extern Boolean
HIDGetElementNameFromVendorProductCookie (const long vendorID
, const long productID
, const long cookie
, char * pName
);
367 // set name from vendor id/product id look up (using usage page & usage)
368 extern Boolean
HIDGetElementNameFromVendorProductUsage (const long vendorID
, const long productID
, const long pUsagePage
, const long pUsage
, char * pName
);
370 // ==================================
371 // Conguration and Save Interfaces
375 kPercentMove
= 10 // precent of overall range a element must move to register
377 /* this is not in the current version... jan.t
378 typedef struct recSaveHID
382 // need to add serial number when I have a test case
392 }recSaveHID,*pRecSaveHID;
394 // polls all devices and elements for a change greater than kPercentMove. Times out after given time
395 // returns true and pointer to device and element if found
396 // returns false and NULL for both parameters if not found
397 extern unsigned char HIDConfigureAction (pRecDevice * ppDevice, pRecElement * ppElement, float timeout);
399 // take input records, save required info
400 // assume file is open and at correct position.
401 extern void HIDSaveElementConfig (FILE * fileRef, pRecDevice pDevice, pRecElement pElement, long actionCookie);
403 // take file, read one record (assume file position is correct and file is open)
404 // search for matching device
405 // return pDevice, pElement and cookie for action
406 extern long HIDRestoreElementConfig (FILE * fileRef, pRecDevice * ppDevice, pRecElement * ppElement);
408 // Save the device & element values into the specified key in the specified applications preferences
409 extern Boolean HIDSaveElementPref (CFStringRef keyCFStringRef, CFStringRef appCFStringRef, pRecDevice pDevice, pRecElement pElement);
411 // Find the specified preference in the specified application
412 // search for matching device and element
413 // return pDevice, pElement that matches
415 extern Boolean HIDRestoreElementPref (CFStringRef keyCFStringRef, CFStringRef appCFStringRef, pRecDevice * ppDevice, pRecElement * ppElement);
417 // ==================================
418 // Output Transaction interface
420 // Create and open an transaction interface to device, required prior to extracting values or building Transactions
421 extern unsigned long HIDTransactionAddElement(pRecDevice pDevice
, pRecElement pElement
);
423 // removes an element from a Transaction
424 extern unsigned long HIDTransactionRemoveElement(pRecDevice pDevice
, pRecElement pElement
);
426 // return true if this transaction contains this element
427 extern Boolean
HIDTransactionHasElement(pRecDevice pDevice
, pRecElement pElement
);
429 /* This changes the default value of an element, when the values of the */
430 /* elements are cleared, on clear or commit, they are reset to the */
432 /* This call can be made on elements that are not in the transaction, but */
433 /* has undefined behavior if made on elements not in the transaction */
434 /* which are later added to the transaction. */
435 /* In other words, an element should be added before its default is */
436 /* set, for well defined behavior. */
437 // pHIDEvent is a poiner to a IOHIDEventStruct, using void here for compatibility, users can cast a required
438 extern unsigned long HIDTransactionSetElementDefault(pRecDevice pDevice
, pRecElement pElement
,IOHIDEventStruct
* pValueEvent
);
440 /* Get the current setting of an element's default value */
441 // pHIDEvent is a poiner to a IOHIDEventStruct, using void here for compatibility, users can cast a required
442 extern unsigned long HIDTransactionGetElementDefault(pRecDevice pDevice
, pRecElement pElement
,IOHIDEventStruct
* pValueEvent
);
444 /* Add a change to the transaction, by setting an element value */
445 /* The change is not actually made until it is commited */
446 /* The element must be part of the transaction or this call will fail */
447 // pHIDEvent is a poiner to a IOHIDEventStruct, using void here for compatibility, users can cast a required
448 extern unsigned long HIDTransactionSetElementValue(pRecDevice pDevice
, pRecElement pElement
,IOHIDEventStruct
* pValueEvent
);
450 /* Get the current setting of an element value */
451 // pHIDEvent is a poiner to a IOHIDEventStruct, using void here for compatibility, users can cast a required
452 extern unsigned long HIDTransactionGetElementValue(pRecDevice pDevice
, pRecElement pElement
,IOHIDEventStruct
* pValueEvent
);
454 /* Commit the transaction, or clear all the changes and start over */
455 /* timoutMS is the timeout in milliseconds, a zero timeout will cause */
456 /* this call to be non-blocking (returning queue empty) if there */
457 /* is a NULL callback, and blocking forever until the queue is */
458 /* non-empty if their is a valid callback */
459 /* callback, if non-NULL is a callback to be called when data is */
460 /* inserted to the queue */
461 /* callbackTarget and callbackRefcon are passed to the callback */
462 extern unsigned long HIDTransactionCommit(pRecDevice pDevice
);
464 /* Clear all the changes and start over */
465 extern unsigned long HIDTransactionClear(pRecDevice pDevice
);
467 // ==================================
473 #endif // _HID_Utilities_External_h_