src/haptic/darwin/SDL_syshaptic.c
author Edgar Simo <bobbens@gmail.com>
Fri, 18 Jul 2008 19:22:56 +0000
branchgsoc2008_force_feedback
changeset 2534 e597de8dccd5
parent 2533 0c8cea99c4b8
child 2535 f0ed8471497d
permissions -rw-r--r--
Was a typo in the mac os x documentation.
     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          ramp = SDL_malloc(sizeof(FFRAMPFORCE));
   574          if (ramp == NULL) {
   575             SDL_OutOfMemory();
   576             return -1;
   577          }
   578 
   579          /* Specifics */
   580          ramp->lStart = CONVERT(hap_ramp->start);
   581          ramp->lEnd = CONVERT(hap_ramp->end);
   582 
   583          /* Generics */
   584          dest->dwDuration = hap_ramp->length * 1000; /* In microseconds. */
   585          dest->dwTriggerButton = FFJOFS_BUTTON(hap_ramp->button);
   586          dest->dwTriggerRepeatInterval = hap_ramp->interval;
   587          dest->dwStartDelay = hap_ramp->delay * 1000; /* In microseconds. */
   588 
   589          /* Direction. */
   590          if (SDL_SYS_SetDirection(dest, &hap_ramp->direction, dest->cAxes) < 0) {
   591             return -1;
   592          }
   593 
   594          /* Envelope */
   595          envelope->dwAttackLevel = CONVERT(hap_ramp->attack_level);
   596          envelope->dwAttackTime = hap_ramp->attack_length * 1000;
   597          envelope->dwFadeLevel = CONVERT(hap_ramp->fade_level);
   598          envelope->dwFadeTime = hap_ramp->fade_length * 1000;
   599 
   600          break;
   601 
   602 
   603       default:
   604          SDL_SetError("Haptic: Unknown effect type.");
   605          return -1;
   606    }
   607 
   608    return 0;
   609 }
   610 
   611 
   612 /*
   613  * Frees an FFEFFECT allocated by SDL_SYS_ToFFEFFECT.
   614  */
   615 static void
   616 SDL_SYS_HapticFreeFFEFFECT( FFEFFECT * effect )
   617 {
   618    if (effect->lpEnvelope != NULL) {
   619       SDL_free(effect->lpEnvelope);
   620       effect->lpEnvelope = NULL;
   621    }
   622    if (effect->rgdwAxes != NULL) {
   623       SDL_free(effect->rgdwAxes);
   624       effect->rgdwAxes = NULL;
   625    }
   626    if (effect->lpvTypeSpecificParams != NULL) {
   627       SDL_free(effect->lpvTypeSpecificParams);
   628       effect->lpvTypeSpecificParams = NULL;
   629    }
   630    if (effect->rglDirection != NULL) {
   631       SDL_free(effect->rglDirection);
   632       effect->rglDirection = NULL;
   633    }
   634 }
   635 
   636 
   637 /*
   638  * Gets the effect type from the generic SDL haptic effect wrapper.
   639  */
   640 CFUUIDRef
   641 SDL_SYS_HapticEffectType(struct haptic_effect * effect)
   642 {
   643    switch (effect->effect.type) {
   644       case SDL_HAPTIC_CONSTANT:
   645          return kFFEffectType_ConstantForce_ID;
   646 
   647       case SDL_HAPTIC_RAMP:
   648          return kFFEffectType_RampForce_ID;
   649 
   650       case SDL_HAPTIC_SQUARE:
   651          return kFFEffectType_Square_ID;
   652 
   653       case SDL_HAPTIC_SINE:
   654          return kFFEffectType_Sine_ID;
   655 
   656       case SDL_HAPTIC_TRIANGLE:
   657          return kFFEffectType_Triangle_ID;
   658 
   659       case SDL_HAPTIC_SAWTOOTHUP:
   660          return kFFEffectType_SawtoothUp_ID;
   661 
   662       case SDL_HAPTIC_SAWTOOTHDOWN:
   663          return kFFEffectType_SawtoothDown_ID;
   664 
   665       case SDL_HAPTIC_SPRING:
   666          return kFFEffectType_Spring_ID;
   667 
   668       case SDL_HAPTIC_DAMPER:
   669          return kFFEffectType_Damper_ID;
   670 
   671       case SDL_HAPTIC_INERTIA:
   672          return kFFEffectType_Inertia_ID;
   673 
   674       case SDL_HAPTIC_FRICTION:
   675          return kFFEffectType_Friction_ID;
   676 
   677       case SDL_HAPTIC_CUSTOM:
   678          return kFFEffectType_CustomForce_ID;
   679 
   680       default:
   681          SDL_SetError("Haptic: Unknown effect type.");
   682          return NULL;
   683    }
   684 }
   685 
   686 
   687 /*
   688  * Creates a new haptic effect.
   689  */
   690 int
   691 SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
   692       SDL_HapticEffect * base)
   693 {
   694    HRESULT ret;
   695    CFUUIDRef type;
   696 
   697    /* Alloc the effect. */
   698    effect->hweffect = (struct haptic_hweffect *)
   699          SDL_malloc(sizeof(struct haptic_hweffect));
   700    if (effect->hweffect == NULL) {
   701       SDL_OutOfMemory();
   702       goto err_hweffect;
   703    }
   704 
   705    /* Get the type. */
   706    type = SDL_SYS_HapticEffectType(effect);
   707    if (type == NULL) {
   708       goto err_hweffect;
   709    }
   710 
   711    /* Get the effect. */
   712    if (SDL_SYS_ToFFEFFECT(haptic, &effect->hweffect->effect, &effect->effect ) < 0) {
   713       goto err_effectdone;
   714    }
   715 
   716    /* Create the actual effect. */
   717    ret = FFDeviceCreateEffect( haptic->hwdata->device, type,
   718          &effect->hweffect->effect, &effect->hweffect->ref );
   719 
   720    if (ret != FF_OK) {
   721       SDL_SetError("Haptic: Unable to create effect.");
   722       goto err_effectdone;
   723    }
   724 
   725    return 0;
   726 
   727 err_effectdone:
   728    SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect);
   729 err_hweffect:
   730    if (effect->hweffect != NULL) {
   731       SDL_free(effect->hweffect);
   732       effect->hweffect = NULL;
   733    }
   734    return -1;
   735 }
   736 
   737 
   738 /*
   739  * Updates an effect.
   740  */
   741 int
   742 SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
   743       struct haptic_effect * effect, SDL_HapticEffect * data)
   744 {
   745    /* TODO */
   746    return -1;
   747 }
   748 
   749 
   750 /*
   751  * Runs an effect.
   752  */
   753 int
   754 SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
   755                         Uint32 iterations)
   756 {
   757    HRESULT ret;
   758    Uint32 iter;
   759 
   760    /* Check if it's infinite. */
   761    if (iterations == SDL_HAPTIC_INFINITY) {
   762       iter = FF_INFINITE;
   763    }
   764    else
   765       iter = iterations;
   766 
   767    /* Run the effect. */
   768    ret = FFEffectStart(effect->hweffect->ref, iter, 0);
   769    if (ret != FF_OK) {
   770       SDL_SetError("Haptic: Unable to run the effect.");
   771       return -1;
   772    }
   773 
   774    return 0;
   775 }
   776 
   777 
   778 /*
   779  * Stops an effect.
   780  */
   781 int
   782 SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
   783 {
   784    HRESULT ret;
   785 
   786    ret = FFEffectStop(effect->hweffect->ref);
   787    if (ret != FF_OK) {
   788       SDL_SetError("Haptic: Unable to stop the effect.");
   789       return -1;
   790    }
   791 
   792    return 0;
   793 }
   794 
   795 
   796 /*
   797  * Frees the effect.
   798  */
   799 void
   800 SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
   801 {
   802    HRESULT ret;
   803 
   804    ret = FFDeviceReleaseEffect(haptic->hwdata->device, effect->hweffect->ref);
   805    if (ret != FF_OK) {
   806       SDL_SetError("Haptic: Error removing the effect from the device.");
   807    }
   808    SDL_free(effect->hweffect->effect.lpvTypeSpecificParams);
   809    effect->hweffect->effect.lpvTypeSpecificParams = NULL;
   810    SDL_free(effect->hweffect);
   811    effect->hweffect = NULL;
   812 }
   813 
   814 
   815 /*
   816  * Gets the status of a haptic effect.
   817  */
   818 int
   819 SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect * effect)
   820 {
   821    HRESULT ret;
   822    FFEffectStatusFlag status;
   823 
   824    ret = FFEffectGetEffectStatus(effect->hweffect->ref, &status);
   825    if (ret != FF_OK) {
   826       SDL_SetError("Haptic: Unable to get effect status.");
   827       return -1;
   828    }
   829 
   830    if (status == 0) return SDL_FALSE;
   831    return SDL_TRUE; /* Assume it's playing or emulated. */
   832 }
   833 
   834 
   835 /*
   836  * Sets the gain.
   837  */
   838 int
   839 SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
   840 {
   841    HRESULT ret;
   842    Uint32 val;
   843 
   844    val = gain * 100; /* Mac OS X uses 0 to 10,000 */
   845    ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device, FFPROP_FFGAIN, &val);
   846    if (ret != FF_OK) {
   847       SDL_SetError("Haptic: Error setting gain.");
   848       return -1;
   849    }
   850 
   851    return 0;
   852 }
   853 
   854 
   855 /*
   856  * Sets the autocentering.
   857  */
   858 int
   859 SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
   860 {
   861    HRESULT ret;
   862    Uint32 val;
   863 
   864    /* Mac OS X only has 0 (off) and 1 (on) */
   865    if (autocenter == 0) val = 0;
   866    else val = 1;
   867 
   868    ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
   869                FFPROP_AUTOCENTER, &val);
   870    if (ret != FF_OK) {
   871       SDL_SetError("Haptic: Error setting autocenter.");
   872       return -1;
   873    }
   874   
   875    return 0;
   876 
   877 }
   878 
   879 
   880 #endif /* SDL_HAPTIC_LINUX */