src/haptic/darwin/SDL_syshaptic.c
changeset 8986 1c81316ac642
parent 8925 8469a1c3e484
child 9070 8973a237f360
equal deleted inserted replaced
8985:a89acd7254f2 8986:1c81316ac642
   255     result = IORegistryEntryCreateCFProperties(device,
   255     result = IORegistryEntryCreateCFProperties(device,
   256                                                &hidProperties,
   256                                                &hidProperties,
   257                                                kCFAllocatorDefault,
   257                                                kCFAllocatorDefault,
   258                                                kNilOptions);
   258                                                kNilOptions);
   259     if ((result == KERN_SUCCESS) && hidProperties) {
   259     if ((result == KERN_SUCCESS) && hidProperties) {
   260         refCF =
   260         refCF = CFDictionaryGetValue(hidProperties,
   261         CFDictionaryGetValue(hidProperties,
   261                                      CFSTR(kIOHIDPrimaryUsagePageKey));
   262                              CFSTR(kIOHIDPrimaryUsagePageKey));
       
   263         if (refCF) {
   262         if (refCF) {
   264             if (!CFNumberGetValue(refCF, kCFNumberLongType,
   263             if (!CFNumberGetValue(refCF, kCFNumberLongType, &item->usagePage)) {
   265                                   &item->usagePage))
   264                 SDL_SetError("Haptic: Recieving device's usage page.");
   266                 SDL_SetError
   265             }
   267                 ("Haptic: Recieving device's usage page.");
   266             refCF = CFDictionaryGetValue(hidProperties,
   268             refCF =
   267                                          CFSTR(kIOHIDPrimaryUsageKey));
   269             CFDictionaryGetValue(hidProperties,
       
   270                                  CFSTR(kIOHIDPrimaryUsageKey));
       
   271             if (refCF) {
   268             if (refCF) {
   272                 if (!CFNumberGetValue(refCF, kCFNumberLongType,
   269                 if (!CFNumberGetValue(refCF, kCFNumberLongType, &item->usage)) {
   273                                       &item->usage))
       
   274                     SDL_SetError("Haptic: Recieving device's usage.");
   270                     SDL_SetError("Haptic: Recieving device's usage.");
       
   271                 }
   275             }
   272             }
   276         }
   273         }
   277         CFRelease(hidProperties);
   274         CFRelease(hidProperties);
   278     }
   275     }
   279 
   276 
   375              * try hid dictionary first, if fail then go to USB dictionary
   372              * try hid dictionary first, if fail then go to USB dictionary
   376              */
   373              */
   377 
   374 
   378 
   375 
   379             /* Get product name */
   376             /* Get product name */
   380             refCF =
   377             refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
   381                 CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
   378             if (!refCF) {
   382             if (!refCF)
   379                 refCF = CFDictionaryGetValue(usbProperties,
   383                 refCF =
   380                                              CFSTR("USB Product Name"));
   384                     CFDictionaryGetValue(usbProperties,
   381             }
   385                                          CFSTR("USB Product Name"));
       
   386             if (refCF) {
   382             if (refCF) {
   387                 if (!CFStringGetCString(refCF, name, 256,
   383                 if (!CFStringGetCString(refCF, name, 256,
   388                                         CFStringGetSystemEncoding())) {
   384                                         CFStringGetSystemEncoding())) {
   389                     return SDL_SetError
   385                     return SDL_SetError("Haptic: CFStringGetCString error retrieving pDevice->product.");
   390                         ("Haptic: CFStringGetCString error retrieving pDevice->product.");
       
   391                 }
   386                 }
   392             }
   387             }
   393 
   388 
   394             CFRelease(usbProperties);
   389             CFRelease(usbProperties);
   395         } else {
   390         } else {
   396             return SDL_SetError
   391             return SDL_SetError("Haptic: IORegistryEntryCreateCFProperties failed to create usbProperties.");
   397                 ("Haptic: IORegistryEntryCreateCFProperties failed to create usbProperties.");
       
   398         }
   392         }
   399 
   393 
   400         /* Release stuff. */
   394         /* Release stuff. */
   401         if (kIOReturnSuccess != IOObjectRelease(parent2)) {
   395         if (kIOReturnSuccess != IOObjectRelease(parent2)) {
   402             SDL_SetError("Haptic: IOObjectRelease error with parent2.");
   396             SDL_SetError("Haptic: IOObjectRelease error with parent2.");
   455     FF_TEST(FFCAP_ET_CUSTOMFORCE, SDL_HAPTIC_CUSTOM);
   449     FF_TEST(FFCAP_ET_CUSTOMFORCE, SDL_HAPTIC_CUSTOM);
   456 
   450 
   457     /* Check if supports gain. */
   451     /* Check if supports gain. */
   458     ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_FFGAIN,
   452     ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_FFGAIN,
   459                                            &val, sizeof(val));
   453                                            &val, sizeof(val));
   460     if (ret == FF_OK)
   454     if (ret == FF_OK) {
   461         supported |= SDL_HAPTIC_GAIN;
   455         supported |= SDL_HAPTIC_GAIN;
   462     else if (ret != FFERR_UNSUPPORTED) {
   456     } else if (ret != FFERR_UNSUPPORTED) {
   463         return SDL_SetError("Haptic: Unable to get if device supports gain: %s.",
   457         return SDL_SetError("Haptic: Unable to get if device supports gain: %s.",
   464                             FFStrError(ret));
   458                             FFStrError(ret));
   465     }
   459     }
   466 
   460 
   467     /* Checks if supports autocenter. */
   461     /* Checks if supports autocenter. */
   468     ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_AUTOCENTER,
   462     ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_AUTOCENTER,
   469                                            &val, sizeof(val));
   463                                            &val, sizeof(val));
   470     if (ret == FF_OK)
   464     if (ret == FF_OK) {
   471         supported |= SDL_HAPTIC_AUTOCENTER;
   465         supported |= SDL_HAPTIC_AUTOCENTER;
   472     else if (ret != FFERR_UNSUPPORTED) {
   466     } else if (ret != FFERR_UNSUPPORTED) {
   473         return SDL_SetError
   467         return SDL_SetError
   474             ("Haptic: Unable to get if device supports autocenter: %s.",
   468             ("Haptic: Unable to get if device supports autocenter: %s.",
   475              FFStrError(ret));
   469              FFStrError(ret));
   476     }
   470     }
   477 
   471 
   602  * Checks to see if a joystick has haptic features.
   596  * Checks to see if a joystick has haptic features.
   603  */
   597  */
   604 int
   598 int
   605 SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
   599 SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
   606 {
   600 {
   607     if (joystick->hwdata->ffservice != 0)
   601     if (joystick->hwdata->ffservice != 0) {
   608         return SDL_TRUE;
   602         return SDL_TRUE;
       
   603     }
   609     return SDL_FALSE;
   604     return SDL_FALSE;
   610 }
   605 }
   611 
   606 
   612 
   607 
   613 /*
   608 /*
   615  */
   610  */
   616 int
   611 int
   617 SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
   612 SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
   618 {
   613 {
   619     if (IOObjectIsEqualTo((io_object_t) ((size_t)haptic->hwdata->device),
   614     if (IOObjectIsEqualTo((io_object_t) ((size_t)haptic->hwdata->device),
   620                           joystick->hwdata->ffservice))
   615                           joystick->hwdata->ffservice)) {
   621         return 1;
   616         return 1;
       
   617     }
   622     return 0;
   618     return 0;
   623 }
   619 }
   624 
   620 
   625 
   621 
   626 /*
   622 /*
   737         rglDir[0] = dir->dir[0];
   733         rglDir[0] = dir->dir[0];
   738         return 0;
   734         return 0;
   739     case SDL_HAPTIC_CARTESIAN:
   735     case SDL_HAPTIC_CARTESIAN:
   740         effect->dwFlags |= FFEFF_CARTESIAN;
   736         effect->dwFlags |= FFEFF_CARTESIAN;
   741         rglDir[0] = dir->dir[0];
   737         rglDir[0] = dir->dir[0];
   742         if (naxes > 1)
   738         if (naxes > 1) {
   743             rglDir[1] = dir->dir[1];
   739             rglDir[1] = dir->dir[1];
   744         if (naxes > 2)
   740         }
       
   741         if (naxes > 2) {
   745             rglDir[2] = dir->dir[2];
   742             rglDir[2] = dir->dir[2];
       
   743         }
   746         return 0;
   744         return 0;
   747     case SDL_HAPTIC_SPHERICAL:
   745     case SDL_HAPTIC_SPHERICAL:
   748         effect->dwFlags |= FFEFF_SPHERICAL;
   746         effect->dwFlags |= FFEFF_SPHERICAL;
   749         rglDir[0] = dir->dir[0];
   747         rglDir[0] = dir->dir[0];
   750         if (naxes > 1)
   748         if (naxes > 1) {
   751             rglDir[1] = dir->dir[1];
   749             rglDir[1] = dir->dir[1];
   752         if (naxes > 2)
   750         }
       
   751         if (naxes > 2) {
   753             rglDir[2] = dir->dir[2];
   752             rglDir[2] = dir->dir[2];
       
   753         }
   754         return 0;
   754         return 0;
   755 
   755 
   756     default:
   756     default:
   757         return SDL_SetError("Haptic: Unknown direction type.");
   757         return SDL_SetError("Haptic: Unknown direction type.");
   758     }
   758     }
   765 #define CONVERT(x)    (((x)*10000) / 0x7FFF)
   765 #define CONVERT(x)    (((x)*10000) / 0x7FFF)
   766 /*
   766 /*
   767  * Creates the FFEFFECT from a SDL_HapticEffect.
   767  * Creates the FFEFFECT from a SDL_HapticEffect.
   768  */
   768  */
   769 static int
   769 static int
   770 SDL_SYS_ToFFEFFECT(SDL_Haptic * haptic, FFEFFECT * dest,
   770 SDL_SYS_ToFFEFFECT(SDL_Haptic * haptic, FFEFFECT * dest, SDL_HapticEffect * src)
   771                    SDL_HapticEffect * src)
       
   772 {
   771 {
   773     int i;
   772     int i;
   774     FFCONSTANTFORCE *constant;
   773     FFCONSTANTFORCE *constant;
   775     FFPERIODIC *periodic;
   774     FFPERIODIC *periodic;
   776     FFCONDITION *condition;     /* Actually an array of conditions - one per axis. */
   775     FFCONDITION *condition;     /* Actually an array of conditions - one per axis. */
  1267 void
  1266 void
  1268 SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  1267 SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  1269 {
  1268 {
  1270     HRESULT ret;
  1269     HRESULT ret;
  1271 
  1270 
  1272     ret =
  1271     ret = FFDeviceReleaseEffect(haptic->hwdata->device, effect->hweffect->ref);
  1273         FFDeviceReleaseEffect(haptic->hwdata->device, effect->hweffect->ref);
       
  1274     if (ret != FF_OK) {
  1272     if (ret != FF_OK) {
  1275         SDL_SetError("Haptic: Error removing the effect from the device: %s.",
  1273         SDL_SetError("Haptic: Error removing the effect from the device: %s.",
  1276                      FFStrError(ret));
  1274                      FFStrError(ret));
  1277     }
  1275     }
  1278     SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect,
  1276     SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect,
  1297         SDL_SetError("Haptic: Unable to get effect status: %s.",
  1295         SDL_SetError("Haptic: Unable to get effect status: %s.",
  1298                      FFStrError(ret));
  1296                      FFStrError(ret));
  1299         return -1;
  1297         return -1;
  1300     }
  1298     }
  1301 
  1299 
  1302     if (status == 0)
  1300     if (status == 0) {
  1303         return SDL_FALSE;
  1301         return SDL_FALSE;
       
  1302     }
  1304     return SDL_TRUE;            /* Assume it's playing or emulated. */
  1303     return SDL_TRUE;            /* Assume it's playing or emulated. */
  1305 }
  1304 }
  1306 
  1305 
  1307 
  1306 
  1308 /*
  1307 /*
  1313 {
  1312 {
  1314     HRESULT ret;
  1313     HRESULT ret;
  1315     Uint32 val;
  1314     Uint32 val;
  1316 
  1315 
  1317     val = gain * 100;           /* Mac OS X uses 0 to 10,000 */
  1316     val = gain * 100;           /* Mac OS X uses 0 to 10,000 */
  1318     ret =
  1317     ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
  1319         FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
  1318                                            FFPROP_FFGAIN, &val);
  1320                                          FFPROP_FFGAIN, &val);
       
  1321     if (ret != FF_OK) {
  1319     if (ret != FF_OK) {
  1322         return SDL_SetError("Haptic: Error setting gain: %s.", FFStrError(ret));
  1320         return SDL_SetError("Haptic: Error setting gain: %s.", FFStrError(ret));
  1323     }
  1321     }
  1324 
  1322 
  1325     return 0;
  1323     return 0;
  1334 {
  1332 {
  1335     HRESULT ret;
  1333     HRESULT ret;
  1336     Uint32 val;
  1334     Uint32 val;
  1337 
  1335 
  1338     /* Mac OS X only has 0 (off) and 1 (on) */
  1336     /* Mac OS X only has 0 (off) and 1 (on) */
  1339     if (autocenter == 0)
  1337     if (autocenter == 0) {
  1340         val = 0;
  1338         val = 0;
  1341     else
  1339     } else {
  1342         val = 1;
  1340         val = 1;
       
  1341     }
  1343 
  1342 
  1344     ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
  1343     ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
  1345                                            FFPROP_AUTOCENTER, &val);
  1344                                            FFPROP_AUTOCENTER, &val);
  1346     if (ret != FF_OK) {
  1345     if (ret != FF_OK) {
  1347         return SDL_SetError("Haptic: Error setting autocenter: %s.",
  1346         return SDL_SetError("Haptic: Error setting autocenter: %s.",