src/haptic/darwin/SDL_syshaptic.c
author Edgar Simo <bobbens@gmail.com>
Fri, 18 Jul 2008 19:16:03 +0000
branchgsoc2008_force_feedback
changeset 2532 688cad3f6090
parent 2531 7f2a4c387292
child 2533 0c8cea99c4b8
permissions -rw-r--r--
More comments.
Disabled envelope on condition effects.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 2008 Edgar Simo
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #ifdef SDL_HAPTIC_IOKIT
    25 
    26 #include "SDL_haptic.h"
    27 #include "../SDL_syshaptic.h"
    28 #include "SDL_joystick.h"
    29 #include "../../joystick/SDL_sysjoystick.h" /* For the real SDL_Joystick */
    30 /*#include "../../joystick/dawrin/SDL_sysjoystick_c.h"*/ /* For joystick hwdata */ 
    31 
    32 #include <IOKit/IOKitLib.h>
    33 #include <IOKit/hid/IOHIDKeys.h>
    34 #include <ForceFeedback/ForceFeedback.h>
    35 #include <ForceFeedback/ForceFeedbackConstants.h>
    36 
    37 
    38 #define MAX_HAPTICS  32
    39 
    40 
    41 /*
    42  * List of available haptic devices.
    43  */
    44 static struct
    45 {
    46    io_service_t dev;
    47    SDL_Haptic *haptic;
    48 } SDL_hapticlist[MAX_HAPTICS];
    49 
    50 
    51 /*
    52  * Haptic system hardware data.
    53  */
    54 struct haptic_hwdata
    55 {
    56    FFDeviceObjectReference device; /* Hardware device. */
    57 };
    58 
    59 
    60 /*
    61  * Haptic system effect data.
    62  */
    63 struct haptic_hweffect
    64 {
    65    FFEffectObjectReference ref; /* Reference. */
    66    struct FFEFFECT effect; /* Hardware effect. */
    67 };
    68 
    69 /*
    70  * Prototypes.
    71  */
    72 static void SDL_SYS_HapticFreeFFEFFECT( FFEFFECT * effect );
    73 
    74 
    75 /*
    76  * Initializes the haptic subsystem.
    77  */
    78 int
    79 SDL_SYS_HapticInit(void)
    80 {
    81    int numhaptics;
    82    IOReturn result;
    83    io_iterator_t iter;
    84    CFDictionaryRef match;
    85    io_service_t device;
    86 
    87    /* Get HID devices. */
    88    match = IOServiceMatching(kIOHIDDeviceKey);
    89    if (match == NULL) {
    90       SDL_SetError("Haptic: Failed to get IOServiceMatching.");
    91       return -1;
    92    }
    93 
    94    /* Now search I/O Registry for matching devices. */
    95    result = IOServiceGetMatchingServices(kIOMasterPortDefault, match, &iter);
    96    if (result != kIOReturnSuccess) {
    97       SDL_SetError("Haptic: Couldn't create a HID object iterator.");
    98       return -1;
    99    }
   100    /* IOServiceGetMatchingServices consumes dictionary. */
   101 
   102    numhaptics = 0;
   103    while ((device = IOIteratorNext(iter)) != IO_OBJECT_NULL) {
   104 
   105       /* Check for force feedback. */
   106       if (FFIsForceFeedback(device) == FF_OK) {
   107          SDL_hapticlist[numhaptics].dev = device;
   108          SDL_hapticlist[numhaptics].haptic = NULL;
   109          numhaptics++;
   110       }
   111 
   112       /* Reached haptic limit. */
   113       if (numhaptics >= MAX_HAPTICS)
   114          break;
   115    }
   116    IOObjectRelease(iter);
   117 
   118    return numhaptics;
   119 }
   120 
   121 
   122 /*
   123  * Return the name of a haptic device, does not need to be opened.
   124  */
   125 const char *
   126 SDL_SYS_HapticName(int index)
   127 {
   128    return NULL;
   129 }
   130 
   131 
   132 #define FF_TEST(ff, s) \
   133 if (features.supportedEffects & ff) supported |= s
   134 /*
   135  * Gets supported features.
   136  */
   137 static unsigned int
   138 GetSupportedFeatures(FFDeviceObjectReference device,
   139                      int *neffects, int *nplaying, int *naxes)
   140 {
   141    HRESULT ret;
   142    FFCAPABILITIES features;
   143    unsigned int supported;
   144    Uint32 val;
   145 
   146    ret = FFDeviceGetForceFeedbackCapabilities(device, &features);
   147    if (ret != FF_OK) {
   148       SDL_SetError("Haptic: Unable to get device's supported features.");
   149       return 0;
   150    }
   151 
   152    supported = 0;
   153 
   154    /* Get maximum effects. */
   155    *neffects = features.storageCapacity;
   156    *nplaying = features.playbackCapacity;
   157 
   158    /* Test for effects. */
   159    FF_TEST(FFCAP_ET_CONSTANTFORCE, SDL_HAPTIC_CONSTANT);
   160    FF_TEST(FFCAP_ET_RAMPFORCE,     SDL_HAPTIC_RAMP);
   161    FF_TEST(FFCAP_ET_SQUARE,        SDL_HAPTIC_SQUARE);
   162    FF_TEST(FFCAP_ET_SINE,          SDL_HAPTIC_SINE);
   163    FF_TEST(FFCAP_ET_TRIANGLE,      SDL_HAPTIC_TRIANGLE);
   164    FF_TEST(FFCAP_ET_SAWTOOTHUP,    SDL_HAPTIC_SAWTOOTHUP);
   165    FF_TEST(FFCAP_ET_SAWTOOTHDOWN,  SDL_HAPTIC_SAWTOOTHDOWN);
   166    FF_TEST(FFCAP_ET_SPRING,        SDL_HAPTIC_SPRING);
   167    FF_TEST(FFCAP_ET_DAMPER,        SDL_HAPTIC_DAMPER);
   168    FF_TEST(FFCAP_ET_INERTIA,       SDL_HAPTIC_INERTIA);
   169    FF_TEST(FFCAP_ET_FRICTION,      SDL_HAPTIC_FRICTION);
   170    FF_TEST(FFCAP_ET_CUSTOMFORCE,   SDL_HAPTIC_CUSTOM);
   171 
   172    /* Check if supports gain. */
   173    ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_FFGAIN,
   174                                           &val, sizeof(val));
   175    if (ret == FF_OK) supported |= SDL_HAPTIC_GAIN;
   176    else if (ret != FFERR_UNSUPPORTED) {
   177       SDL_SetError("Haptic: Unable to get if device supports gain.");
   178       return 0;
   179    }
   180 
   181    /* Checks if supports autocenter. */
   182    ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_AUTOCENTER,
   183                                           &val, sizeof(val));
   184    if (ret == FF_OK) supported |= SDL_HAPTIC_AUTOCENTER;
   185    else if (ret != FFERR_UNSUPPORTED) {
   186       SDL_SetError("Haptic: Unable to get if device supports autocenter.");
   187       return 0;
   188    }
   189 
   190    /* Check for axes, we have an artificial limit on axes */
   191    *naxes = ((features.numFfAxes) > 3) ?
   192          3 : features.numFfAxes;
   193 
   194    /* Always supported features. */
   195    supported |= SDL_HAPTIC_STATUS;
   196    return supported;
   197 }
   198 
   199 
   200 /*
   201  * Opens the haptic device from the file descriptor.
   202  */
   203 static int
   204 SDL_SYS_HapticOpenFromService(SDL_Haptic * haptic, io_service_t service)
   205 {
   206    /* Allocate the hwdata */
   207    haptic->hwdata = (struct haptic_hwdata *)
   208          SDL_malloc(sizeof(*haptic->hwdata));
   209    if (haptic->hwdata == NULL) {
   210       SDL_OutOfMemory();
   211       goto creat_err;
   212    }
   213    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
   214   
   215    /* Open the device */
   216    if (FFCreateDevice( service, &haptic->hwdata->device ) != FF_OK) {
   217       SDL_SetError("Haptic: Unable to create device from service.");
   218       goto creat_err;
   219    }
   220 
   221    /* Get supported features. */
   222    haptic->supported = GetSupportedFeatures(haptic->hwdata->device,
   223                                             &haptic->neffects, &haptic->nplaying,
   224                                             &haptic->naxes);
   225    if (haptic->supported == 0) { /* Error since device supports nothing. */
   226       goto open_err;
   227    }
   228    haptic->effects = (struct haptic_effect *)
   229          SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
   230    if (haptic->effects == NULL) {
   231       SDL_OutOfMemory();
   232       goto open_err;
   233    }
   234    /* Clear the memory */
   235    SDL_memset(haptic->effects, 0,
   236          sizeof(struct haptic_effect) * haptic->neffects);
   237    
   238    return 0;
   239    
   240    /* Error handling */
   241 open_err:
   242    FFReleaseDevice(haptic->hwdata->device);
   243 creat_err:
   244    if (haptic->hwdata != NULL) {
   245       free(haptic->hwdata);
   246       haptic->hwdata = NULL;                                              
   247    }
   248    return -1;
   249 
   250 }
   251 
   252 
   253 /*
   254  * Opens a haptic device for usage.
   255  */
   256 int
   257 SDL_SYS_HapticOpen(SDL_Haptic * haptic)
   258 {
   259    return SDL_SYS_HapticOpenFromService(haptic,
   260                 SDL_hapticlist[haptic->index].dev);
   261 }
   262 
   263 
   264 /*
   265  * Opens a haptic device from first mouse it finds for usage.
   266  */
   267 int
   268 SDL_SYS_HapticMouse(void)
   269 {
   270    return -1;
   271 }
   272 
   273 
   274 /*
   275  * Checks to see if a joystick has haptic features.
   276  */
   277 int
   278 SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
   279 {
   280    return SDL_FALSE;
   281 }
   282 
   283 
   284 /*
   285  * Checks to see if the haptic device and joystick and in reality the same.
   286  */
   287 int
   288 SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
   289 {
   290    return 0;
   291 }
   292 
   293 
   294 /*
   295  * Opens a SDL_Haptic from a SDL_Joystick.
   296  */
   297 int
   298 SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
   299 {
   300    return -1;
   301 }
   302 
   303 
   304 /*
   305  * Closes the haptic device.
   306  */
   307 void
   308 SDL_SYS_HapticClose(SDL_Haptic * haptic)
   309 {
   310    int i;
   311 
   312    if (haptic->hwdata) {
   313 
   314       /* Free the effects. */
   315       for (i=0; i<haptic->neffects; i++) {        
   316          if (haptic->effects[i].hweffect != NULL) {
   317             SDL_SYS_HapticFreeFFEFFECT(&haptic->effects[i].hweffect->effect);
   318             SDL_free(haptic->effects[i].hweffect);
   319          } 
   320       }    
   321       SDL_free(haptic->effects);
   322       haptic->neffects = 0;
   323 
   324       /* Clean up */
   325       FFReleaseDevice(haptic->hwdata->device);
   326 
   327       /* Free */
   328       SDL_free(haptic->hwdata);
   329       haptic->hwdata = NULL;
   330    }
   331 }
   332 
   333 
   334 /* 
   335  * Clean up after system specific haptic stuff
   336  */
   337 void
   338 SDL_SYS_HapticQuit(void)
   339 {
   340    int i;
   341 
   342    for (i=0; i < SDL_numhaptics; i++) {
   343       /* Opened and not closed haptics are leaked, this is on purpose.
   344        * Close your haptic devices after usage. */
   345 
   346       /* Free the io_service_t */
   347       IOObjectRelease(SDL_hapticlist[i].dev);
   348    }
   349 }
   350 
   351 
   352 /*
   353  * Sets the direction.
   354  */
   355 static int
   356 SDL_SYS_SetDirection( FFEFFECT * effect, SDL_HapticDirection *dir, int naxes )
   357 {
   358    LONG *rglDir;
   359 
   360    /* Handle no axes a part. */
   361    if (naxes == 0) {
   362       effect->rglDirection = NULL;
   363       return 0;
   364    }
   365 
   366    /* Has axes. */
   367    rglDir = SDL_malloc( sizeof(LONG) * naxes );
   368    if (rglDir == NULL) {
   369       SDL_OutOfMemory();
   370       return -1;
   371    }
   372    SDL_memset( rglDir, 0, sizeof(LONG) * naxes );
   373    effect->rglDirection = rglDir;
   374 
   375    switch (dir->type) {
   376       case SDL_HAPTIC_POLAR:
   377          effect->dwFlags |= FFEFF_POLAR;
   378          rglDir[0] = dir->dir[0];
   379          return 0;
   380       case SDL_HAPTIC_CARTESIAN:
   381          effect->dwFlags |= FFEFF_CARTESIAN;
   382          rglDir[0] = dir->dir[0];
   383          rglDir[1] = dir->dir[1];
   384          rglDir[2] = dir->dir[2];
   385          return 0;
   386       case SDL_HAPTIC_SPHERICAL:
   387          effect->dwFlags |= FFEFF_SPHERICAL;
   388          rglDir[0] = dir->dir[0];
   389          rglDir[1] = dir->dir[1];
   390          rglDir[2] = dir->dir[2];
   391          return 0;
   392 
   393       default:
   394          SDL_SetError("Haptic: Unknown direction type.");
   395          return -1;
   396    }
   397 }
   398 
   399 #define CONVERT(x)   (((x)*10000) / 0xFFFF )
   400 /*
   401  * Creates the FFEFFECT from a SDL_HapticEffect.
   402  */
   403 static int
   404 SDL_SYS_ToFFEFFECT( SDL_Haptic * haptic, FFEFFECT * dest, SDL_HapticEffect * src )
   405 {
   406    int i;
   407    FFCONSTANTFORCE *constant;
   408    FFPERIODIC *periodic;
   409    FFCONDITION *condition; /* Actually an array of conditions - one per axis. */
   410    FFRAMPFORCE *ramp;
   411    FFCUSTOMFORCE *custom;
   412    FFENVELOPE *envelope;
   413    SDL_HapticConstant *hap_constant;
   414    SDL_HapticPeriodic *hap_periodic;
   415    SDL_HapticCondition *hap_condition;
   416    SDL_HapticRamp *hap_ramp;
   417    DWORD *axes;
   418 
   419    /* Set global stuff. */
   420    SDL_memset(dest, 0, sizeof(FFEFFECT));
   421    dest->dwSize = sizeof(FFEFFECT); /* Set the structure size. */
   422    dest->dwSamplePeriod = 0; /* Not used by us. */
   423    dest->dwGain = 10000; /* Gain is set globally, not locally. */
   424 
   425    /* Envelope. */
   426    envelope = SDL_malloc( sizeof(FFENVELOPE) );
   427    if (envelope == NULL) {
   428       SDL_OutOfMemory();
   429       return -1;
   430    }
   431    SDL_memset(envelope, 0, sizeof(FFENVELOPE));
   432    dest->lpEnvelope = envelope;
   433    envelope->dwSize = sizeof(FFENVELOPE); /* Always should be this. */
   434 
   435    /* Axes. */
   436    dest->cAxes = haptic->naxes;
   437    if (dest->cAxes > 0) {
   438       axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
   439       if (axes == NULL) {
   440          SDL_OutOfMemory();
   441          return -1;
   442       }
   443       axes[0] = FFJOFS_X; /* Always at least one axis. */
   444       if (dest->cAxes > 1) {
   445          axes[1] = FFJOFS_Y;
   446       }
   447       if (dest->cAxes > 2) {
   448          axes[2] = FFJOFS_Z;
   449       }
   450       dest->rgdwAxes = axes;
   451    }
   452 
   453 
   454    /* The big type handling switch, even bigger then linux's version. */
   455    switch (src->type) {
   456       case SDL_HAPTIC_CONSTANT:
   457          hap_constant = &src->constant;
   458          constant = SDL_malloc( sizeof(FFCONSTANTFORCE) );
   459          if (constant == NULL) {
   460             SDL_OutOfMemory();
   461             return -1;
   462          }
   463 
   464          /* Specifics */
   465          constant->lMagnitude = CONVERT(hap_constant->level);
   466          dest->cbTypeSpecificParams = sizeof(FFCONSTANTFORCE); 
   467          dest->lpvTypeSpecificParams = constant;
   468 
   469          /* Generics */
   470          dest->dwDuration = hap_constant->length * 1000; /* In microseconds. */
   471          dest->dwTriggerButton = FFJOFS_BUTTON(hap_constant->button);
   472          dest->dwTriggerRepeatInterval = hap_constant->interval;
   473          dest->dwStartDelay = hap_constant->delay * 1000; /* In microseconds. */
   474 
   475          /* Direction. */
   476          if (SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes) < 0) {
   477             return -1;
   478          }
   479          
   480          /* Envelope */
   481          envelope->dwAttackLevel = CONVERT(hap_constant->attack_level);
   482          envelope->dwAttackTime = hap_constant->attack_length * 1000;
   483          envelope->dwFadeLevel = CONVERT(hap_constant->fade_level);
   484          envelope->dwFadeTime = hap_constant->fade_length * 1000;
   485 
   486          break;
   487 
   488       case SDL_HAPTIC_SINE:
   489       case SDL_HAPTIC_SQUARE:
   490       case SDL_HAPTIC_TRIANGLE:
   491       case SDL_HAPTIC_SAWTOOTHUP:
   492       case SDL_HAPTIC_SAWTOOTHDOWN:
   493          hap_periodic = &src->periodic;
   494          periodic = SDL_malloc(sizeof(FFPERIODIC));
   495          if (periodic == NULL) {
   496             SDL_OutOfMemory();
   497             return -1;
   498          }
   499 
   500          /* Specifics */
   501          periodic->dwMagnitude = CONVERT(hap_periodic->magnitude);
   502          periodic->lOffset = CONVERT(hap_periodic->offset);
   503          periodic->dwPhase = hap_periodic->phase;
   504          periodic->dwPeriod = hap_periodic->period * 1000;
   505          dest->cbTypeSpecificParams = sizeof(FFPERIODIC);
   506          dest->lpvTypeSpecificParams = periodic;
   507 
   508          /* Generics */
   509          dest->dwDuration = hap_periodic->length * 1000; /* In microseconds. */
   510          dest->dwTriggerButton = FFJOFS_BUTTON(hap_periodic->button);
   511          dest->dwTriggerRepeatInterval = hap_periodic->interval;
   512          dest->dwStartDelay = hap_periodic->delay * 1000; /* In microseconds. */
   513          
   514          /* Direction. */
   515          if (SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes) < 0) {
   516             return -1;
   517          }
   518          
   519          /* Envelope */
   520          envelope->dwAttackLevel = CONVERT(hap_periodic->attack_level);
   521          envelope->dwAttackTime = hap_periodic->attack_length * 1000;
   522          envelope->dwFadeLevel = CONVERT(hap_periodic->fade_level);
   523          envelope->dwFadeTime = hap_periodic->fade_length * 1000;
   524 
   525          break;
   526 
   527       case SDL_HAPTIC_SPRING:
   528       case SDL_HAPTIC_DAMPER:
   529       case SDL_HAPTIC_INERTIA:
   530       case SDL_HAPTIC_FRICTION:
   531          hap_condition = &src->condition;
   532          condition = SDL_malloc(sizeof(FFCONDITION) * dest->cAxes);
   533          if (condition == NULL) {
   534             SDL_OutOfMemory();
   535             return -1;
   536          }
   537 
   538          /* Specifics */
   539          for (i=0; i<dest->cAxes; i++) {
   540             condition[i].lOffset = CONVERT(hap_condition->center[i]);
   541             condition[i].lPositiveCoefficient = CONVERT(hap_condition->right_coeff[i]);
   542             condition[i].lNegativeCoefficient = CONVERT(hap_condition->left_coeff[i]);
   543             condition[i].dwPositiveSaturation = CONVERT(hap_condition->right_sat[i]);
   544             condition[i].dwNegativeSaturation = CONVERT(hap_condition->left_sat[i]);
   545             condition[i].lDeadBand = CONVERT(hap_condition->deadband[i]);
   546          }
   547          dest->cbTypeSpecificParams = sizeof(FFCONDITION) * dest->cAxes;
   548          dest->lpvTypeSpecificParams = condition;
   549 
   550          /* Generics */
   551          dest->dwDuration = hap_condition->length * 1000; /* In microseconds. */
   552          dest->dwTriggerButton = FFJOFS_BUTTON(hap_condition->button);
   553          dest->dwTriggerRepeatInterval = hap_condition->interval;
   554          dest->dwStartDelay = hap_condition->delay * 1000; /* In microseconds. */
   555 
   556          /* Direction. */
   557          if (SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes) < 0) {
   558             return -1;                
   559          }                            
   560                                       
   561          /* Envelope */
   562 /* TODO Check is envelope actually used.
   563          envelope->dwAttackLevel = CONVERT(hap_condition->attack_level);
   564          envelope->dwAttackTime = hap_condition->attack_length * 1000;
   565          envelope->dwFadeLevel = CONVERT(hap_condition->fade_level);
   566          envelope->dwFadeTime = hap_condition->fade_length * 1000;
   567 */
   568 
   569          break;
   570 
   571       case SDL_HAPTIC_RAMP:
   572          hap_ramp = &src->ramp;
   573 
   574          break;
   575 
   576 
   577       default:
   578          SDL_SetError("Haptic: Unknown effect type.");
   579          return -1;
   580    }
   581 
   582    return 0;
   583 }
   584 
   585 
   586 /*
   587  * Frees an FFEFFECT allocated by SDL_SYS_ToFFEFFECT.
   588  */
   589 static void
   590 SDL_SYS_HapticFreeFFEFFECT( FFEFFECT * effect )
   591 {
   592    if (effect->lpEnvelope != NULL) {
   593       SDL_free(effect->lpEnvelope);
   594       effect->lpEnvelope = NULL;
   595    }
   596    if (effect->rgdwAxes != NULL) {
   597       SDL_free(effect->rgdwAxes);
   598       effect->rgdwAxes = NULL;
   599    }
   600    if (effect->lpvTypeSpecificParams != NULL) {
   601       SDL_free(effect->lpvTypeSpecificParams);
   602       effect->lpvTypeSpecificParams = NULL;
   603    }
   604    if (effect->rglDirection != NULL) {
   605       SDL_free(effect->rglDirection);
   606       effect->rglDirection = NULL;
   607    }
   608 }
   609 
   610 
   611 /*
   612  * Gets the effect type from the generic SDL haptic effect wrapper.
   613  */
   614 CFUUIDRef
   615 SDL_SYS_HapticEffectType(struct haptic_effect * effect)
   616 {
   617    switch (effect->effect.type) {
   618       case SDL_HAPTIC_CONSTANT:
   619          return kFFEffectType_ConstantForce_ID;
   620 
   621       case SDL_HAPTIC_RAMP:
   622          return kFFEffectType_RampForce_ID;
   623 
   624       case SDL_HAPTIC_SQUARE:
   625          return kFFEffectType_Square_ID;
   626 
   627       case SDL_HAPTIC_SINE:
   628          return kFFEffectType_Sine_ID;
   629 
   630       case SDL_HAPTIC_TRIANGLE:
   631          return kFFEffectType_Triangle_ID;
   632 
   633       case SDL_HAPTIC_SAWTOOTHUP:
   634          return kFFEffectType_SawtoothUp_ID;
   635 
   636       case SDL_HAPTIC_SAWTOOTHDOWN:
   637          return kFFEffectType_SawtoothDown_ID;
   638 
   639       case SDL_HAPTIC_SPRING:
   640          return kFFEffectType_Spring_ID;
   641 
   642       case SDL_HAPTIC_DAMPER:
   643          return kFFEffectType_Damper_ID;
   644 
   645       case SDL_HAPTIC_INERTIA:
   646          return kFFEffectType_Inertia_ID;
   647 
   648       case SDL_HAPTIC_FRICTION:
   649          return kFFEffectType_Friction_ID;
   650 
   651       case SDL_HAPTIC_CUSTOM:
   652          return kFFEffectType_CustomForce_ID;
   653 
   654       default:
   655          SDL_SetError("Haptic: Unknown effect type.");
   656          return NULL;
   657    }
   658 }
   659 
   660 
   661 /*
   662  * Creates a new haptic effect.
   663  */
   664 int
   665 SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
   666       SDL_HapticEffect * base)
   667 {
   668    HRESULT ret;
   669    CFUUIDRef type;
   670 
   671    /* Alloc the effect. */
   672    effect->hweffect = (struct haptic_hweffect *)
   673          SDL_malloc(sizeof(struct haptic_hweffect));
   674    if (effect->hweffect == NULL) {
   675       SDL_OutOfMemory();
   676       goto err_hweffect;
   677    }
   678 
   679    /* Get the type. */
   680    type = SDL_SYS_HapticEffectType(effect);
   681    if (type == NULL) {
   682       goto err_hweffect;
   683    }
   684 
   685    /* Get the effect. */
   686    if (SDL_SYS_ToFFEFFECT(haptic, &effect->hweffect->effect, &effect->effect ) < 0) {
   687       goto err_effectdone;
   688    }
   689 
   690    /* Create the actual effect. */
   691    ret = FFDeviceCreateEffect( haptic->hwdata->device, type,
   692          &effect->hweffect->effect, &effect->hweffect->ref );
   693 
   694    if (ret != FF_OK) {
   695       SDL_SetError("Haptic: Unable to create effect.");
   696       goto err_effectdone;
   697    }
   698 
   699    return 0;
   700 
   701 err_effectdone:
   702    SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect);
   703 err_hweffect:
   704    if (effect->hweffect != NULL) {
   705       SDL_free(effect->hweffect);
   706       effect->hweffect = NULL;
   707    }
   708    return -1;
   709 }
   710 
   711 
   712 /*
   713  * Updates an effect.
   714  */
   715 int
   716 SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
   717       struct haptic_effect * effect, SDL_HapticEffect * data)
   718 {
   719    /* TODO */
   720    return -1;
   721 }
   722 
   723 
   724 /*
   725  * Runs an effect.
   726  */
   727 int
   728 SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
   729                         Uint32 iterations)
   730 {
   731    HRESULT ret;
   732    Uint32 iter;
   733 
   734    /* Check if it's infinite. */
   735    if (iterations == SDL_HAPTIC_INFINITY) {
   736       iter = FF_INFINITE;
   737    }
   738    else
   739       iter = iterations;
   740 
   741    /* Run the effect. */
   742    ret = FFEffectStart(effect->hweffect->ref, iter, 0);
   743    if (ret != FF_OK) {
   744       SDL_SetError("Haptic: Unable to run the effect.");
   745       return -1;
   746    }
   747 
   748    return 0;
   749 }
   750 
   751 
   752 /*
   753  * Stops an effect.
   754  */
   755 int
   756 SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
   757 {
   758    HRESULT ret;
   759 
   760    ret = FFEffectStop(effect->hweffect->ref);
   761    if (ret != FF_OK) {
   762       SDL_SetError("Haptic: Unable to stop the effect.");
   763       return -1;
   764    }
   765 
   766    return 0;
   767 }
   768 
   769 
   770 /*
   771  * Frees the effect.
   772  */
   773 void
   774 SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
   775 {
   776    HRESULT ret;
   777 
   778    ret = FFDeviceReleaseEffect(haptic->hwdata->device, effect->hweffect->ref);
   779    if (ret != FF_OK) {
   780       SDL_SetError("Haptic: Error removing the effect from the device.");
   781    }
   782    SDL_free(effect->hweffect->effect.lpvTypeSpecificParams);
   783    effect->hweffect->effect.lpvTypeSpecificParams = NULL;
   784    SDL_free(effect->hweffect);
   785    effect->hweffect = NULL;
   786 }
   787 
   788 
   789 /*
   790  * Gets the status of a haptic effect.
   791  */
   792 int
   793 SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect * effect)
   794 {
   795    HRESULT ret;
   796    FFEffectStatusFlag status;
   797 
   798    ret = FFEffectGetEffectStatus(effect->hweffect->ref, &status);
   799    if (ret != FF_OK) {
   800       SDL_SetError("Haptic: Unable to get effect status.");
   801       return -1;
   802    }
   803 
   804    if (status == 0) return SDL_FALSE;
   805    return SDL_TRUE; /* Assume it's playing or emulated. */
   806 }
   807 
   808 
   809 /*
   810  * Sets the gain.
   811  */
   812 int
   813 SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
   814 {
   815    HRESULT ret;
   816    Uint32 val;
   817 
   818    val = gain * 100; /* Mac OS X uses 0 to 10,000 */
   819    ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device, FFPROP_FFGAIN, &val);
   820    if (ret != FF_OK) {
   821       SDL_SetError("Haptic: Error setting gain.");
   822       return -1;
   823    }
   824 
   825    return 0;
   826 }
   827 
   828 
   829 /*
   830  * Sets the autocentering.
   831  */
   832 int
   833 SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
   834 {
   835    HRESULT ret;
   836    Uint32 val;
   837 
   838    /* Mac OS X only has 0 (off) and 1 (on) */
   839    if (autocenter == 0) val = 0;
   840    else val = 1;
   841 
   842    ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
   843                FFPROP_AUTOCENTER, &val);
   844    if (ret != FF_OK) {
   845       SDL_SetError("Haptic: Error setting autocenter.");
   846       return -1;
   847    }
   848   
   849    return 0;
   850 
   851 }
   852 
   853 
   854 #endif /* SDL_HAPTIC_LINUX */