22 #include <Carbon/Carbon.h> 23 #include <AvailabilityMacros.h> 24 #include <IOKit/hid/IOHIDLib.h> 25 #include "../ConnexionHID.h" 29 namespace connexion_plugin {
36 NULL, NULL, NULL, NULL};
42 CFArrayAppendValue((CFMutableArrayRef) context, value);
48 Boolean result = FALSE;
50 if(inIOHIDDeviceRef) {
51 CFTypeRef tCFTypeRef = IOHIDDeviceGetProperty(inIOHIDDeviceRef, inKey);
54 if(CFNumberGetTypeID() == CFGetTypeID(tCFTypeRef)) {
56 result = CFNumberGetValue((CFNumberRef) tCFTypeRef,
57 kCFNumberSInt32Type, outValue);
67 myIOHIDManagerRef = IOHIDManagerCreate(kCFAllocatorDefault,
68 kIOHIDOptionsTypeNone );
70 if(myIOHIDManagerRef) {
72 IOReturn tIOReturn = IOHIDManagerOpen(myIOHIDManagerRef,
73 kIOHIDOptionsTypeNone);
74 if(kIOReturnSuccess != tIOReturn) {
75 fprintf(stderr,
"%s: Couldn't open IOHIDManager.",
80 fprintf(stderr,
"%s: Couldn't create a IOHIDManager.",
86 IOHIDManagerSetDeviceMatchingMultiple(myIOHIDManagerRef,
89 CFSetRef devCFSetRef = IOHIDManagerCopyDevices(myIOHIDManagerRef);
92 myDeviceCFArrayRef = CFArrayCreateMutable( kCFAllocatorDefault, 0,
93 &kCFTypeArrayCallBacks );
96 (
void * ) myDeviceCFArrayRef );
98 CFRelease( devCFSetRef );
102 if(!myDeviceCFArrayRef)
return 0;
103 CFIndex idx, cnt = CFArrayGetCount( myDeviceCFArrayRef );
104 char product[255] =
"";
105 char target_product[255] =
"SpaceNavigator";
106 char target_product2[255] =
"Space Navigator";
108 bool found_device =
false;
109 for ( idx = 0; idx < cnt; idx++ ) {
110 myIOHIDDeviceRef = (IOHIDDeviceRef)CFArrayGetValueAtIndex(myDeviceCFArrayRef, idx);
113 CFStringRef
string = (CFStringRef)IOHIDDeviceGetProperty(myIOHIDDeviceRef,
114 CFSTR(kIOHIDProductKey));
116 verify(CFStringGetCString(
string, product,
sizeof(product),
117 kCFStringEncodingUTF8));
121 CFSTR(kIOHIDProductIDKey), &result);
123 if(!strcmp(target_product, product)) {
127 if(!strcmp(target_product2, product)) {
132 if(!found_device)
return 0;
133 myElementCFArrayRef = IOHIDDeviceCopyMatchingElements(myIOHIDDeviceRef,
135 if(myElementCFArrayRef) {
136 CFIndex eleIndex, eleCount = CFArrayGetCount(myElementCFArrayRef);
138 for(eleIndex=0; eleIndex<eleCount; eleIndex++) {
139 IOHIDElementRef tIOHIDElementRef = (IOHIDElementRef)CFArrayGetValueAtIndex(myElementCFArrayRef, eleIndex);
140 if ( !tIOHIDElementRef )
continue;
141 IOHIDElementType tIOHIDElementType = IOHIDElementGetType(tIOHIDElementRef);
142 if(tIOHIDElementType>kIOHIDElementTypeInput_ScanCodes)
continue;
143 long usagePage = IOHIDElementGetUsagePage(tIOHIDElementRef);
144 long usage = IOHIDElementGetUsage(tIOHIDElementRef);
145 if ( !usagePage || !usage )
continue;
146 if ( -1 == usage )
continue;
147 myElementRef[e_count++] = tIOHIDElementRef;
158 IOHIDValueRef value = NULL;
160 for(el = 0; el < 8; el++) {
161 if(myElementRef[el] != NULL) {
162 if(IOHIDDeviceGetValue(myIOHIDDeviceRef, myElementRef[el],
163 &value) == kIOReturnSuccess) {
164 values[el] = (double)IOHIDValueGetIntegerValue(value);
168 rawValues->
tx = values[0];
169 rawValues->
ty = values[1];
170 rawValues->
tz = values[2];
171 rawValues->
rx = values[3];
172 rawValues->
ry = values[4];
173 rawValues->
rz = values[5];
174 rawValues->
button1 = (int)values[6];
175 rawValues->
button2 = (int)values[7];
177 coordinates[0] = rawValues->
tx * fabs(rawValues->
tx * 0.01);
178 coordinates[2] = -rawValues->
ty * fabs(rawValues->
ty * 0.01);
179 coordinates[1] = -rawValues->
tz * fabs(rawValues->
tz * 0.01);
180 coordinates[3] = rawValues->
rx * fabs(rawValues->
rx * 0.01);
181 coordinates[5] = -rawValues->
ry * fabs(rawValues->
ry * 0.01);
182 coordinates[4] = -rawValues->
rz * fabs(rawValues->
rz * 0.01);
188 if(myIOHIDManagerRef) {
189 IOHIDManagerClose(myIOHIDManagerRef, 0);
191 CFRelease(myDeviceCFArrayRef);
192 CFRelease(myElementCFArrayRef);
static Boolean IOHIDDevice_GetLongProperty(IOHIDDeviceRef inIOHIDDeviceRef, CFStringRef inKey, long *outValue)
void getValue(interfaces::sReal *coordiantes, struct connexionValues *rawValues)
IOHIDManagerRef myIOHIDManagerRef
static void CFSetApplierFunctionCopyToCFArray(const void *value, void *context)
IOHIDDeviceRef myIOHIDDeviceRef
int initConnexionHID(void *windowID)
Copyright 2012, DFKI GmbH Robotics Innovation Center.
IOHIDElementRef myElementRef[8]
static const ControlID gScrolledControlID
CFMutableArrayRef myDeviceCFArrayRef
CFArrayRef myElementCFArrayRef