scide: implement selectionLength for openDocument
[supercollider.git] / lang / LangPrimSource / HID_Utilities / IOHIDElement_.c
blob88385dae8cf2314f4bce073bc98578470505cbac
1 //
2 // File: IOHIDElement_.c of HID Utilities
3 //
4 // Created: 9/21/06
5 //
6 // Contains: Convieance functions for IOHIDElementGetProperty
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 "IOHIDElement_.h"
55 //*****************************************************
56 #pragma mark - typedef's, struct's, enums, defines, etc.
57 //-----------------------------------------------------
59 //*****************************************************
60 #pragma mark - local (static) function prototypes
61 //-----------------------------------------------------
63 // static Boolean IOHIDElement_GetLongProperty( IOHIDElementRef inElementRef, CFStringRef inKey, long * outValue );
64 // static void IOHIDElement_SetLongProperty( IOHIDElementRef inElementRef, CFStringRef inKey, long inValue );
66 //*****************************************************
67 #pragma mark - exported globals
68 //-----------------------------------------------------
70 //*****************************************************
71 #pragma mark - local (static) globals
72 //-----------------------------------------------------
74 //*****************************************************
75 #pragma mark - exported function implementations
76 //-----------------------------------------------------
78 //*************************************************************************
80 // HIDIsValidElement( inIOHIDElementRef )
82 // Purpose: validate this element
84 // Inputs: inIOHIDElementRef - the element
86 // Returns: Boolean - TRUE if this is a valid element ref
88 Boolean HIDIsValidElement( IOHIDElementRef inIOHIDElementRef )
90 Boolean result = FALSE; // assume failure (pessimist!)
91 if ( inIOHIDElementRef ) {
92 if ( CFGetTypeID( inIOHIDElementRef ) == IOHIDElementGetTypeID() ) {
93 result = TRUE;
96 return result;
99 //*************************************************************************
101 // IOHIDElement_GetValue( inElementRef, inIOHIDValueScaleType )
103 // Purpose: returns the current value for an element( polling )
105 // Notes: will return 0 on error conditions which should be accounted for by application
107 // Inputs: inElementRef - the element
108 // inIOHIDValueScaleType - scale type ( calibrated or physical )
110 // Returns: double - current value for element
112 double IOHIDElement_GetValue( IOHIDElementRef inElementRef, IOHIDValueScaleType inIOHIDValueScaleType )
114 long result = 0;
115 IOHIDValueRef tIOHIDValueRef;
117 if ( kIOReturnSuccess == IOHIDDeviceGetValue( IOHIDElementGetDevice( inElementRef ), inElementRef, &tIOHIDValueRef ) ) {
118 result = IOHIDValueGetScaledValue( tIOHIDValueRef, inIOHIDValueScaleType );
120 return result;
121 } // IOHIDElement_GetValue
123 //*************************************************************************
125 // IOHIDElement_GetCalibrationMin( inElementRef )
127 // Purpose: get the minimum bounds for a calibrated value for this element
129 // Inputs: inElementRef - the IOHIDElementRef for this element
131 // Returns: CFIndex - the minimum Calibration value for this element
134 CFIndex IOHIDElement_GetCalibrationMin( IOHIDElementRef inElementRef )
136 CFIndex result;
138 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationMinKey ), &result ) ) {
139 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementMaxKey ), &result ) ) {
140 result = 0x7FFFFFFF;
142 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationMinKey ), result );
144 return result;
145 } // IOHIDElement_GetCalibrationMin
147 //*************************************************************************
149 // IOHIDElement_SetCalibrationMin( inElementRef, inValue )
151 // Purpose: set the minimum bounds for a calibrated value for this element
153 // Inputs: inElementRef - the IOHIDElementRef for this element
154 // inValue - the minimum bounds for a calibrated value for this element
156 // Returns: nothing
159 void IOHIDElement_SetCalibrationMin( IOHIDElementRef inElementRef, CFIndex inValue )
161 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationMinKey ), inValue );
162 } // IOHIDElement_SetCalibrationMin
164 //*************************************************************************
166 // IOHIDElement_GetCalibrationMax( inElementRef )
168 // Purpose: get the maximum bounds for a calibrated value for this element
170 // Inputs: inElementRef - the IOHIDElementRef for this element
172 // Returns: CFIndex - the maximum Calibration value for this element
175 CFIndex IOHIDElement_GetCalibrationMax( IOHIDElementRef inElementRef )
177 CFIndex result;
179 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationMaxKey ), &result ) ) {
180 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementMinKey ), &result ) ) {
181 result = -0x7FFFFFFF;
183 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationMaxKey ), result );
185 return result;
186 } // IOHIDElement_GetCalibrationMax
188 //*************************************************************************
190 // IOHIDElement_SetCalibrationMax( inElementRef, inValue )
192 // Purpose: set the maximum bounds for a calibrated value for this element
194 // Inputs: inElementRef - the IOHIDElementRef for this element
195 // inValue - the maximum Calibration value for this element
197 // Returns: nothing
200 void IOHIDElement_SetCalibrationMax( IOHIDElementRef inElementRef, CFIndex inValue )
202 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationMaxKey ), inValue );
203 } // IOHIDElement_SetCalibrationMax
205 //*************************************************************************
207 // IOHIDElement_GetCalibrationSaturationMin( inElementRef )
209 // Purpose: get the mininum tolerance to be used when calibrating a logical element value
211 // Inputs: inElementRef - the IOHIDElementRef for this element
213 // Returns: CFIndex - the maximum Calibration value for this element
216 CFIndex IOHIDElement_GetCalibrationSaturationMin( IOHIDElementRef inElementRef )
218 CFIndex result;
220 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationSaturationMinKey ), &result ) ) {
221 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementMinKey ), &result ) ) {
222 result = -0x7FFFFFFF;
224 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationSaturationMinKey ), result );
226 return result;
227 } // IOHIDElement_GetCalibrationSaturationMin
229 //*************************************************************************
231 // IOHIDElement_SetCalibrationSaturationMin( inElementRef, inValue )
233 // Purpose: set the mininum tolerance to be used when calibrating a logical element value
235 // Inputs: inElementRef - the IOHIDElementRef for this element
236 // inValue - the maximum Calibration value for this element
238 // Returns: nothing
241 void IOHIDElement_SetCalibrationSaturationMin( IOHIDElementRef inElementRef, CFIndex inValue )
243 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationSaturationMinKey ), inValue );
244 } // IOHIDElement_SetCalibrationSaturationMin
246 //*************************************************************************
248 // IOHIDElement_GetCalibrationSaturationMax( inElementRef )
250 // Purpose: get the maximum tolerance to be used when calibrating a logical element value
252 // Inputs: inElementRef - the IOHIDElementRef for this element
254 // Returns: CFIndex - the maximum Calibration value for this element
257 CFIndex IOHIDElement_GetCalibrationSaturationMax( IOHIDElementRef inElementRef )
259 CFIndex result;
261 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationSaturationMaxKey ), &result ) ) {
262 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementMinKey ), &result ) ) {
263 result = -0x7FFFFFFF;
265 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationSaturationMaxKey ), result );
267 return result;
268 } // IOHIDElement_GetCalibrationSaturationMax
270 //*************************************************************************
272 // IOHIDElement_SetCalibrationSaturationMax( inElementRef, inValue )
274 // Purpose: set the maximum tolerance to be used when calibrating a logical element value
276 // Inputs: inElementRef - the IOHIDElementRef for this element
277 // inValue - the maximum Calibration value for this element
279 // Returns: nothing
282 void IOHIDElement_SetCalibrationSaturationMax( IOHIDElementRef inElementRef, CFIndex inValue )
284 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationSaturationMaxKey ), inValue );
285 } // IOHIDElement_SetCalibrationSaturationMax
287 //*************************************************************************
289 // IOHIDElement_GetCalibrationDeadZoneMin( inElementRef )
291 // Purpose: get the minimum bounds near the midpoint of a logical value in which the value is ignored
293 // Inputs: inElementRef - the IOHIDElementRef for this element
295 // Returns: CFIndex - the maximum Calibration value for this element
298 CFIndex IOHIDElement_GetCalibrationDeadZoneMin( IOHIDElementRef inElementRef )
300 CFIndex result;
302 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationDeadZoneMinKey ), &result ) ) {
303 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementMinKey ), &result ) ) {
304 result = -0x7FFFFFFF;
306 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationDeadZoneMinKey ), result );
308 return result;
309 } // IOHIDElement_GetCalibrationDeadZoneMin
311 //*************************************************************************
313 // IOHIDElement_SetCalibrationDeadZoneMin( inElementRef, inValue )
315 // Purpose: set the minimum bounds near the midpoint of a logical value in which the value is ignored
317 // Inputs: inElementRef - the IOHIDElementRef for this element
318 // inValue - the maximum Calibration value for this element
320 // Returns: nothing
323 void IOHIDElement_SetCalibrationDeadZoneMin( IOHIDElementRef inElementRef, CFIndex inValue )
325 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationDeadZoneMinKey ), inValue );
326 } // IOHIDElement_SetCalibrationDeadZoneMin
328 //*************************************************************************
330 // IOHIDElement_GetCalibrationDeadZoneMax( inElementRef )
332 // Purpose: get the maximum bounds near the midpoint of a logical value in which the value is ignored
334 // Inputs: inElementRef - the IOHIDElementRef for this element
336 // Returns: CFIndex - the maximum Calibration value for this element
339 CFIndex IOHIDElement_GetCalibrationDeadZoneMax( IOHIDElementRef inElementRef )
341 CFIndex result;
343 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationDeadZoneMaxKey ), &result ) ) {
344 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementMinKey ), &result ) ) {
345 result = -0x7FFFFFFF;
347 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationDeadZoneMaxKey ), result );
349 return result;
350 } // IOHIDElement_GetCalibrationDeadZoneMax
352 //*************************************************************************
354 // IOHIDElement_SetCalibrationDeadZoneMax( inElementRef, inValue )
356 // Purpose: set the maximum bounds near the midpoint of a logical value in which the value is ignored
358 // Inputs: inElementRef - the IOHIDElementRef for this element
359 // inValue - the maximum Calibration value for this element
361 // Returns: nothing
364 void IOHIDElement_SetCalibrationDeadZoneMax( IOHIDElementRef inElementRef, CFIndex inValue )
366 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationDeadZoneMaxKey ), inValue );
367 } // IOHIDElement_SetCalibrationDeadZoneMax
369 //*************************************************************************
371 // IOHIDElement_GetCalibrationGranularity( inElementRef )
373 // Purpose: get the level of detail returned for a calibrated element value
375 // Inputs: inElementRef - the IOHIDElementRef for this element
377 // Returns: double_t - the maximum Calibration value for this element
380 double_t IOHIDElement_GetCalibrationGranularity( IOHIDElementRef inElementRef )
382 CFIndex result;
384 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationGranularityKey ), &result ) ) {
385 if ( !IOHIDElement_GetLongProperty( inElementRef, CFSTR( kIOHIDElementMinKey ), &result ) ) {
386 result = -0x7FFFFFFF;
388 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationGranularityKey ), result );
390 return result;
391 } // IOHIDElement_GetCalibrationGranularity
393 //*************************************************************************
395 // IOHIDElement_SetCalibrationGranularity( inElementRef, inValue )
397 // Purpose: set the level of detail returned for a calibrated element value
399 // Inputs: inElementRef - the IOHIDElementRef for this element
400 // inValue - the the level of detail for this element
402 // Returns: nothing
405 void IOHIDElement_SetCalibrationGranularity( IOHIDElementRef inElementRef, double_t inValue )
407 IOHIDElement_SetLongProperty( inElementRef, CFSTR( kIOHIDElementCalibrationGranularityKey ), inValue );
408 } // IOHIDElement_SetCalibrationGranularity
410 //*************************************************************************
412 // IOHIDElement_SetupCalibration( inElementRef )
414 // Purpose: set default values for the element calibration parameters
416 // Inputs: inElementRef - the IOHIDElementRef for this element
418 // Returns: nothing
420 void IOHIDElement_SetupCalibration( IOHIDElementRef inIOHIDElementRef )
422 // these are the min/max values returned by IOHIDValueGetScaledValue( v, kIOHIDValueScaleTypeCalibrated );
423 IOHIDElement_SetCalibrationMin( inIOHIDElementRef, IOHIDElementGetLogicalMin( inIOHIDElementRef ) );
424 IOHIDElement_SetCalibrationMax( inIOHIDElementRef, IOHIDElementGetLogicalMax( inIOHIDElementRef ) );
426 // this is the granularity of the values returned by IOHIDValueGetScaledValue( v, kIOHIDValueScaleTypeCalibrated );
427 // for example if set to 0.1 the values returned will be multiples of 0.1 ( 0.1, 0.2, 0.3, etc. )
428 IOHIDElement_SetCalibrationGranularity( inIOHIDElementRef, 0. );
430 // these define the dead zone (like in the middel of joystick axis)
431 IOHIDElement_SetCalibrationDeadZoneMin( inIOHIDElementRef, 0 );
432 IOHIDElement_SetCalibrationDeadZoneMax( inIOHIDElementRef, 0 );
433 #if 1
434 // get the current value of this element
435 double value = IOHIDElement_GetValue( inIOHIDElementRef, kIOHIDValueScaleTypePhysical );
436 // use it as our min/mas saturation
437 IOHIDElement_SetCalibrationSaturationMin( inIOHIDElementRef, value );
438 IOHIDElement_SetCalibrationSaturationMax( inIOHIDElementRef, value );
439 #else
440 // calculate the middle physical value we would expect from this element
441 CFIndex valueMin = IOHIDElementGetPhysicalMin( inIOHIDElementRef );
442 CFIndex valueMax = IOHIDElementGetPhysicalMax( inIOHIDElementRef );
443 CFIndex valueMid = ( valueMin + valueMax ) / 2;
445 // use it as our min/mas saturation
446 // this value determines the min/max values that have been recieved from the device element
447 IOHIDElement_SetCalibrationSaturationMin( inIOHIDElementRef, valueMid );
448 IOHIDElement_SetCalibrationSaturationMax( inIOHIDElementRef, valueMid );
450 // get the current value of this element
451 double value = IOHIDElement_GetValue( inIOHIDElementRef, kIOHIDValueScaleTypePhysical );
453 // and use it to adjust the current saturation values if it's outside their range
454 if ( value < IOHIDElement_GetCalibrationSaturationMin( inIOHIDElementRef ) ) {
455 IOHIDElement_SetCalibrationSaturationMin( inIOHIDElementRef, value );
458 if ( value > IOHIDElement_GetCalibrationSaturationMax( inIOHIDElementRef ) ) {
459 IOHIDElement_SetCalibrationSaturationMax( inIOHIDElementRef, value );
461 #endif
462 } // IOHIDElement_SetupCalibration
463 //*****************************************************
464 #pragma mark - local (static) function implementations
465 //-----------------------------------------------------
467 //*************************************************************************
469 // IOHIDElement_GetLongProperty( inElementRef, inKey, outValue )
471 // Purpose: convieance function to return a long property of an element
473 // Inputs: inElementRef - the element
474 // inKey - CFString for the key
475 // outValue - address where to store the value
476 // Returns: Boolean - TRUE if successful
477 // outValue - the long property's value
480 Boolean IOHIDElement_GetLongProperty( IOHIDElementRef inElementRef, CFStringRef inKey, long * outValue )
482 Boolean result = FALSE;
484 CFTypeRef tCFTypeRef = IOHIDElementGetProperty( inElementRef, inKey );
486 if ( tCFTypeRef ) {
487 // if this is a number
488 if ( CFNumberGetTypeID() == CFGetTypeID( tCFTypeRef ) ) {
489 // get it's value
490 result = CFNumberGetValue( ( CFNumberRef ) tCFTypeRef, kCFNumberSInt32Type, outValue );
493 return result;
494 } /* IOHIDElement_GetLongProperty */
496 //*************************************************************************
498 // IOHIDElement_SetLongProperty( inElementRef, inKey, inValue )
500 // Purpose: convieance function to set a long property of an element
502 // Inputs: inElementRef - the element
503 // inKey - CFString for the key
504 // inValue - the value to set it to
506 // Returns: nothing
509 void IOHIDElement_SetLongProperty( IOHIDElementRef inElementRef, CFStringRef inKey, long inValue )
511 CFNumberRef tCFNumberRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberSInt32Type, &inValue );
513 if ( tCFNumberRef ) {
514 IOHIDElementSetProperty( inElementRef, inKey, tCFNumberRef );
515 CFRelease( tCFNumberRef );
519 //*****************************************************