7 #include <IOKit/hid/IOHIDLib.h> 37 if(calibCenter<
value && calibMax!=calibCenter)
39 calib=(double)(
value-calibCenter)/(double)(calibMax-calibCenter);
41 else if(
value<calibCenter && calibMin!=calibCenter)
43 calib=(double)(
value-calibCenter)/(double)(calibCenter-calibMin);
69 calibMin=calibCenter+1000;
70 calibMax=calibCenter-1000;
87 calibCenter=(calibMin+calibMax)/2;
105 if(
value==valueNeutral)
109 else if(
value==value0Deg)
113 else if(
value==value90Deg)
117 else if(
value==value180Deg)
121 else if(value270Deg==
value)
129 else if(value90Deg<
value &&
value<value180Deg)
133 else if(value180Deg<
value &&
value<value270Deg)
137 else if(value270Deg<
value)
159 CFArrayRef elemAry=IOHIDDeviceCopyMatchingElements(hidDev,NULL,0);
160 int nElem=(
int)CFArrayGetCount(elemAry);
161 int isMouse=0,isJoystick=0,isKeyboard=0,isGamePad=0;
163 printf(
"This HID Device has %d elements.\n",nElem);
166 for(j=0; j<nElem; j++)
168 IOHIDElementRef
elem=(IOHIDElementRef)CFArrayGetValueAtIndex(elemAry,j);
169 IOHIDElementType elemType=IOHIDElementGetType(elem);
170 unsigned int usage=IOHIDElementGetUsage(elem);
171 unsigned int usagePage=IOHIDElementGetUsagePage(elem);
173 printf(
"Element %3d",j);
176 case kIOHIDElementTypeInput_ScanCodes:
177 printf(
" ScanCode ");
179 case kIOHIDElementTypeInput_Misc:
182 case kIOHIDElementTypeInput_Button:
185 case kIOHIDElementTypeInput_Axis:
188 case kIOHIDElementTypeOutput:
191 case kIOHIDElementTypeFeature:
194 case kIOHIDElementTypeCollection:
195 printf(
" Collection");
199 printf(
" Usage %3d UsagePage %3d\n",usage,usagePage);
201 if(kHIDPage_GenericDesktop==usagePage)
205 case kHIDUsage_GD_Mouse:
206 printf(
" Can function as mouse\n");
209 case kHIDUsage_GD_Keyboard:
210 printf(
" Can function as Keyboard\n");
213 case kHIDUsage_GD_Joystick:
214 printf(
" Can function as Joystick\n");
217 case kHIDUsage_GD_GamePad:
218 printf(
" Can function as GamePad\n");
231 for(j=0; j<nElem; j++)
233 IOHIDElementRef
elem=(IOHIDElementRef)CFArrayGetValueAtIndex(elemAry,j);
234 IOHIDElementType elemType=IOHIDElementGetType(elem);
235 unsigned int usage=IOHIDElementGetUsage(elem);
236 unsigned int usagePage=IOHIDElementGetUsagePage(elem);
240 int min=IOHIDElementGetLogicalMin(elem);
241 int max=IOHIDElementGetLogicalMax(elem);
245 if(elemType==kIOHIDElementTypeInput_Misc ||
246 elemType==kIOHIDElementTypeInput_Button ||
247 elemType==kIOHIDElementTypeInput_Axis ||
248 elemType==kIOHIDElementTypeInput_ScanCodes)
252 case kHIDPage_GenericDesktop:
255 case kHIDUsage_GD_Mouse:
257 case kHIDUsage_GD_Keyboard:
259 case kHIDUsage_GD_Joystick:
261 case kHIDUsage_GD_GamePad:
264 printf(
" This element is for X-Axis (%d->%d) Scaled(%d->%d)\n",min,max,scaledMin,scaledMax);
265 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
268 printf(
" This element is for Y-Axis (%d->%d) Scaled(%d->%d)\n",min,max,scaledMin,scaledMax);
269 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
272 printf(
" This element is for Z-Axis (%d->%d) Scaled(%d->%d)\n",min,max,scaledMin,scaledMax);
273 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
275 case kHIDUsage_GD_Rx:
276 printf(
" This element is for Rx-Axis (%d->%d) Scaled(%d->%d)\n",min,max,scaledMin,scaledMax);
277 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
279 case kHIDUsage_GD_Ry:
280 printf(
" This element is for Ry-Axis (%d->%d) Scaled(%d->%d)\n",min,max,scaledMin,scaledMax);
281 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
283 case kHIDUsage_GD_Rz:
284 printf(
" This element is for Rz-Axis (%d->%d) Scaled(%d->%d)\n",min,max,scaledMin,scaledMax);
285 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
287 case kHIDUsage_GD_Slider:
288 printf(
" This element is for Slider (%d->%d) Scaled(%d->%d)\n",min,max,scaledMin,scaledMax);
289 AddAxis(nAxis++,elem,min,max,scaledMin,scaledMax);
291 case kHIDUsage_GD_Wheel:
292 printf(
" This element is for Wheel (%d->%d) Scaled(%d->%d)\n",min,max,scaledMin,scaledMax);
294 case kHIDUsage_GD_Hatswitch:
295 printf(
" This element is for Hatswitch (%d->%d) Scaled(%d->%d)\n",min,max,scaledMin,scaledMax);
298 hatSwitch[nHat].exist=1;
299 hatSwitch[nHat].elem=
elem;
306 case kHIDPage_Button:
307 printf(
" This element is for Button %d\n",usage-1);
311 button[
usage].exist=1;
333 IOHIDValueRef valueRef;
338 IOHIDDeviceGetValue(hidDev,axis[i].
elem,&valueRef);
339 axis[i].value=IOHIDValueGetIntegerValue(valueRef);
344 if(button[i].
exist!=0)
346 IOHIDDeviceGetValue(hidDev,button[i].
elem,&valueRef);
347 button[i].value=IOHIDValueGetIntegerValue(valueRef);
352 if(hatSwitch[i].
exist!=0)
354 IOHIDDeviceGetValue(hidDev,hatSwitch[i].
elem,&valueRef);
356 double scaled=IOHIDValueGetScaledValue(valueRef,kIOHIDValueScaleTypePhysical);
357 if(scaled<-0.001 || 359.999<scaled)
359 hatSwitch[i].value=0;
363 hatSwitch[i].value=1+(
int)((scaled+22.5)/45.0);
395 axis[axisId].exist=1;
396 axis[axisId].elem=
elem;
397 axis[axisId].min=
min;
398 axis[axisId].max=
max;
399 axis[axisId].scaledMin=scaledMin;
400 axis[axisId].scaledMax=scaledMax;
402 axis[axisId].calibCenter=(min+
max)/2;
403 axis[axisId].calibMin=
min;
404 axis[axisId].calibMax=
max;
412 CFArrayAppendValue((CFMutableArrayRef)context,value);
421 hidManager=IOHIDManagerCreate(kCFAllocatorDefault,kIOHIDOptionsTypeNone);
426 IOHIDManagerSetDeviceMatching(hidManager,NULL);
427 IOHIDManagerOpen(hidManager,kIOHIDOptionsTypeNone);
429 CFSetRef copyOfDevices=IOHIDManagerCopyDevices(hidManager);
435 devArray=CFArrayCreateMutable(kCFAllocatorDefault,0,&kCFTypeArrayCallBacks);
438 CFIndex nDev=CFArrayGetCount(devArray);
440 printf(
"%d devices found\n",(
int)nDev);
442 CFRelease(copyOfDevices);
447 for(i=0; i<nDev && nJoystick<maxNumJoystick; i++)
449 IOHIDDeviceRef hidDev=(IOHIDDeviceRef)CFArrayGetValueAtIndex(devArray,i);
450 if(joystick[nJoystick].SetUpInterface(nJoystick,hidDev)!=0)
464 fprintf(fp,
"BGNJOY %d\n",joyId);
469 fprintf(fp,
"AXSINF %d %d %d %d\n",i,axis[i].calibCenter,axis[i].calibMin,axis[i].calibMax);
472 #ifdef YSJOYREADER_USE_HAT_CALIBRATION 475 if(0!=hatSwitch[i].
exist)
477 fprintf(fp,
"HATINF %d %d %d %d %d %d\n",
479 hatSwitch[i].valueNeutral,
480 hatSwitch[i].value0Deg,
481 hatSwitch[i].value90Deg,
482 hatSwitch[i].value180Deg,
483 hatSwitch[i].value270Deg);
487 fprintf(fp,
"ENDJOY\n");
494 while(fgets(str,255,fp)!=NULL)
496 if(strncmp(str,
"AXSINF",6)==0)
499 sscanf(str,
"%*s %d %d %d %d",&axisId,&cen,&min,&max);
502 axis[axisId].calibCenter=cen;
503 axis[axisId].calibMin=
min;
504 axis[axisId].calibMax=
max;
507 #ifdef YSJOYREADER_USE_HAT_CALIBRATION 508 else if(strncmp(str,
"HATINF",6)==0)
511 int valueNeutral=0,value0Deg=1,value90Deg=3,value180Deg=5,value270Deg=7;
512 sscanf(str,
"%*s %d %d %d %d %d %d",&hatId,&valueNeutral,&value0Deg,&value90Deg,&value180Deg,&value270Deg);
515 hatSwitch[hatId].valueNeutral=valueNeutral;
516 hatSwitch[hatId].value0Deg=value0Deg;
517 hatSwitch[hatId].value90Deg=value90Deg;
518 hatSwitch[hatId].value180Deg=value180Deg;
519 hatSwitch[hatId].value270Deg=value270Deg;
523 else if(strncmp(str,
"ENDJOY",6)==0)
552 for(i=0; i<nJoystick; i++)
571 while(fgets(str,255,fp)!=NULL)
573 if(strncmp(str,
"BGNJOY",6)==0)
576 sscanf(str,
"%*s %d",&joyId);
577 if(0<=joyId && joyId<nJoystick)
int YsJoyReaderLoadJoystickCalibrationInfo(int nJoystick, YsJoyReader joystick[])
static CFMutableArrayRef devArray
static IOHIDManagerRef hidManager
int GetDiscreteValue(void) const
static int SetUpJoystick(int &nJoystick, YsJoyReader joystick[], int maxNumJoystick)
FILE * YsJoyReaderOpenJoystickCalibrationFile(const char mode[])
const int YsJoyReaderMaxNumHatSwitch
int ReadCalibInfoFile(FILE *fp)
FILE * YsJoyReaderOpenJoystickCalibrationFileC(const char mode[])
void ReleaseInterface(void)
void CFSetCopyCallBack(const void *value, void *context)
const int YsJoyReaderMaxNumAxis
const int YsJoyReaderMaxNumButton
def j(str, encoding="cp932")
int YsJoyReaderSaveJoystickCalibrationInfo(int nJoystick, YsJoyReader joystick[])
void AddAxis(int axisId, IOHIDElementRef elem, int min, int max, int scaledMin, int scaledMax)
int SetUpInterface(int joyId, IOHIDDeviceRef hidDev)
void CenterFromMinMax(void)
void BeginCaptureMinMax(void)
int YsJoyReaderSetUpJoystick(int &nJoystick, YsJoyReader joystick[], int maxNumJoystick)
int WriteCalibInfoFile(FILE *fp) const
double GetCalibratedValue(void) const