25 #include <sys/errno.h>
27 #include <IOKit/hid/IOHIDLib.h>
176 CFArrayRef elemAry=IOHIDDeviceCopyMatchingElements(hidDev,NULL,0);
177 int nElem=(int)CFArrayGetCount(elemAry);
178 int isMouse=0,isJoystick=0,isKeyboard=0,isGamePad=0;
183 for(j=0; j<nElem; j++)
185 IOHIDElementRef elem=(IOHIDElementRef)CFArrayGetValueAtIndex(elemAry,j);
187 unsigned int usage=IOHIDElementGetUsage(elem);
188 unsigned int usagePage=IOHIDElementGetUsagePage(elem);
218 if(kHIDPage_GenericDesktop==usagePage)
222 case kHIDUsage_GD_Mouse:
226 case kHIDUsage_GD_Keyboard:
230 case kHIDUsage_GD_Joystick:
234 case kHIDUsage_GD_GamePad:
248 for(j=0; j<nElem; j++)
250 IOHIDElementRef elem=(IOHIDElementRef)CFArrayGetValueAtIndex(elemAry,j);
251 IOHIDElementType elemType=IOHIDElementGetType(elem);
252 unsigned int usage=IOHIDElementGetUsage(elem);
253 unsigned int usagePage=IOHIDElementGetUsagePage(elem);
257 int min=IOHIDElementGetLogicalMin(elem);
258 int max=IOHIDElementGetLogicalMax(elem);
262 if(elemType==kIOHIDElementTypeInput_Misc ||
263 elemType==kIOHIDElementTypeInput_Button ||
264 elemType==kIOHIDElementTypeInput_Axis ||
265 elemType==kIOHIDElementTypeInput_ScanCodes)
269 case kHIDPage_GenericDesktop:
272 case kHIDUsage_GD_Mouse:
274 case kHIDUsage_GD_Keyboard:
276 case kHIDUsage_GD_Joystick:
278 case kHIDUsage_GD_GamePad:
282 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
286 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
290 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
292 case kHIDUsage_GD_Rx:
294 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
296 case kHIDUsage_GD_Ry:
298 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
300 case kHIDUsage_GD_Rz:
302 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
304 case kHIDUsage_GD_Slider:
306 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
308 case kHIDUsage_GD_Wheel:
311 case kHIDUsage_GD_Hatswitch:
323 case kHIDPage_Button:
351 IOHIDValueRef valueRef;
356 IOHIDDeviceGetValue(
hidDev,
axis[i].elem,&valueRef);
357 axis[i].
value=IOHIDValueGetIntegerValue(valueRef);
365 button[i].
value=IOHIDValueGetIntegerValue(valueRef);
374 double scaled=IOHIDValueGetScaledValue(valueRef,kIOHIDValueScaleTypePhysical);
375 if(scaled<-0.001 || 359.999<scaled)
430 CFArrayAppendValue((CFMutableArrayRef)context,value);
439 hidManager=IOHIDManagerCreate(kCFAllocatorDefault,kIOHIDOptionsTypeNone);
444 IOHIDManagerSetDeviceMatching(
hidManager,NULL);
445 IOHIDManagerOpen(
hidManager,kIOHIDOptionsTypeNone);
447 CFSetRef copyOfDevices=IOHIDManagerCopyDevices(
hidManager);
453 devArray=CFArrayCreateMutable(kCFAllocatorDefault,0,&kCFTypeArrayCallBacks);
456 CFIndex nDev=CFArrayGetCount(
devArray);
458 qDebug(
"%d HID devices found. Looking for joysticks...", (
int) nDev);
460 CFRelease(copyOfDevices);
465 for(i=0; i<nDev && nJoystick<maxNumJoystick; i++)
467 IOHIDDeviceRef
hidDev=(IOHIDDeviceRef)CFArrayGetValueAtIndex(
devArray,i);
477 qDebug(
"%d joystick(s) found.", nJoystick);
485 fprintf(fp,
"BGNJOY %d\n",
joyId);
490 fprintf(fp,
"AXSINF %d %d %d %d\n",i,
axis[i].calibCenter,
axis[i].calibMin,
axis[i].calibMax);
493 #ifdef YSJOYREADER_USE_HAT_CALIBRATION
498 fprintf(fp,
"HATINF %d %d %d %d %d %d\n",
508 fprintf(fp,
"ENDJOY\n");
515 while(fgets(str,255,fp)!=NULL)
517 if(strncmp(str,
"AXSINF",6)==0)
519 int axisId,cen,min,max;
520 sscanf(str,
"%*s %d %d %d %d",&axisId,&cen,&min,&max);
528 #ifdef YSJOYREADER_USE_HAT_CALIBRATION
529 else if(strncmp(str,
"HATINF",6)==0)
532 int valueNeutral=0,value0Deg=1,value90Deg=3,value180Deg=5,value270Deg=7;
533 sscanf(str,
"%*s %d %d %d %d %d %d",&hatId,&valueNeutral,&value0Deg,&value90Deg,&value180Deg,&value270Deg);
544 else if(strncmp(str,
"ENDJOY",6)==0)
573 for(i=0; i<nJoystick; i++)
592 while(fgets(str,255,fp)!=NULL)
594 if(strncmp(str,
"BGNJOY",6)==0)
597 sscanf(str,
"%*s %d",&joyId);
598 if(0<=joyId && joyId<nJoystick)