src/haptic/darwin/SDL_syshaptic.c
author Edgar Simo <bobbens@gmail.com>
Thu, 31 Jul 2008 10:25:49 +0000
branchgsoc2008_force_feedback
changeset 2563 be2ea885d70b
parent 2562 c284ce0cd8e0
child 2564 c9348710a98a
permissions -rw-r--r--
More verbose about envelopes.
     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    char name[256];
    47    io_service_t dev;
    48    SDL_Haptic *haptic;
    49 } SDL_hapticlist[MAX_HAPTICS];
    50 
    51 
    52 /*
    53  * Haptic system hardware data.
    54  */
    55 struct haptic_hwdata
    56 {
    57    FFDeviceObjectReference device; /* Hardware device. */
    58 };
    59 
    60 
    61 /*
    62  * Haptic system effect data.
    63  */
    64 struct haptic_hweffect
    65 {
    66    FFEffectObjectReference ref; /* Reference. */
    67    struct FFEFFECT effect; /* Hardware effect. */
    68 };
    69 
    70 /*
    71  * Prototypes.
    72  */
    73 static void SDL_SYS_HapticFreeFFEFFECT(FFEFFECT * effect, int type);
    74 static int HIDGetDeviceProduct(io_service_t dev, char * name);
    75 
    76 
    77 /* 
    78  * Like strerror but for force feedback errors.
    79  */
    80 static const char *
    81 FFStrError(HRESULT err)
    82 {
    83    switch (err) {
    84       case FFERR_DEVICEFULL:
    85          return "device full";
    86       /*case FFERR_DEVICENOTREG:
    87          return "device not registered";*/
    88       case FFERR_DEVICEPAUSED:
    89          return "device paused";
    90       case FFERR_DEVICERELEASED:
    91          return "device released";
    92       case FFERR_EFFECTPLAYING:
    93          return "effect playing";
    94       case FFERR_EFFECTTYPEMISMATCH:
    95          return "effect type mismatch";
    96       case FFERR_EFFECTTYPENOTSUPPORTED:
    97          return "effect type not supported";
    98       case FFERR_GENERIC:
    99          return "undetermined error";
   100       case FFERR_HASEFFECTS:
   101          return "device has effects";
   102       case FFERR_INCOMPLETEEFFECT:
   103          return "incomplete effect";
   104       case FFERR_INTERNAL:
   105          return "internal fault";
   106       case FFERR_INVALIDDOWNLOADID:
   107          return "invalid download id";
   108       case FFERR_INVALIDPARAM:
   109          return "invalid parameter";
   110       case FFERR_MOREDATA:
   111          return "more data";
   112       case FFERR_NOINTERFACE:
   113          return "interface not supported";
   114       case FFERR_NOTDOWNLOADED:
   115          return "effect is not downloaded";
   116       case FFERR_NOTINITIALIZED:
   117          return "object has not been initialized";
   118       case FFERR_OUTOFMEMORY:
   119          return "out of memory";
   120       case FFERR_UNPLUGGED:
   121          return "device is unplugged";
   122       case FFERR_UNSUPPORTED:
   123          return "function call unsupported";
   124       case FFERR_UNSUPPORTEDAXIS:
   125          return "axis unsupported";
   126 
   127       default:
   128          return "unknown error";
   129    }
   130 }
   131 
   132 
   133 /*
   134  * Initializes the haptic subsystem.
   135  */
   136 int
   137 SDL_SYS_HapticInit(void)
   138 {
   139    int numhaptics;
   140    IOReturn result;
   141    io_iterator_t iter;
   142    CFDictionaryRef match;
   143    io_service_t device;
   144 
   145    /* Clear all the memory. */
   146    SDL_memset(SDL_hapticlist, 0, sizeof(SDL_hapticlist));
   147 
   148    /* Get HID devices. */
   149    match = IOServiceMatching(kIOHIDDeviceKey);
   150    if (match == NULL) {
   151       SDL_SetError("Haptic: Failed to get IOServiceMatching.");
   152       return -1;
   153    }
   154 
   155    /* Now search I/O Registry for matching devices. */
   156    result = IOServiceGetMatchingServices(kIOMasterPortDefault, match, &iter);
   157    if (result != kIOReturnSuccess) {
   158       SDL_SetError("Haptic: Couldn't create a HID object iterator.");
   159       return -1;
   160    }
   161    /* IOServiceGetMatchingServices consumes dictionary. */
   162 
   163    numhaptics = 0;
   164    while ((device = IOIteratorNext(iter)) != IO_OBJECT_NULL) {
   165 
   166       /* Check for force feedback. */
   167       if (FFIsForceFeedback(device) == FF_OK) {
   168          HIDGetDeviceProduct(device, SDL_hapticlist[numhaptics].name);
   169          SDL_hapticlist[numhaptics].dev = device;
   170          SDL_hapticlist[numhaptics].haptic = NULL;
   171          numhaptics++;
   172       }
   173       else { /* Free the unused device. */
   174          IOObjectRelease(device);
   175       }
   176 
   177       /* Reached haptic limit. */
   178       if (numhaptics >= MAX_HAPTICS)
   179          break;
   180    }
   181    IOObjectRelease(iter);
   182 
   183    return numhaptics;
   184 }
   185 
   186 
   187 /*
   188  * Return the name of a haptic device, does not need to be opened.
   189  */
   190 const char *
   191 SDL_SYS_HapticName(int index)
   192 {
   193    return SDL_hapticlist[index].name;
   194 }
   195 
   196 /*
   197  * Gets the device's product name.
   198  */
   199 static int
   200 HIDGetDeviceProduct(io_service_t dev, char *name)
   201 {
   202    CFMutableDictionaryRef hidProperties, usbProperties;
   203    io_registry_entry_t parent1, parent2;
   204    kern_return_t ret;
   205 
   206    hidProperties = usbProperties = 0;
   207 
   208    ret = IORegistryEntryCreateCFProperties(dev, &hidProperties,
   209                                            kCFAllocatorDefault,
   210                                            kNilOptions);
   211    if ((ret != KERN_SUCCESS) || !hidProperties) {
   212       SDL_SetError("Haptic: Unable to create CFProperties.");
   213       return -1;
   214    }
   215 
   216    /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
   217     * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
   218     */
   219    if ((KERN_SUCCESS ==
   220             IORegistryEntryGetParentEntry(dev, kIOServicePlane, &parent1))
   221          && (KERN_SUCCESS ==
   222             IORegistryEntryGetParentEntry(parent1, kIOServicePlane, &parent2))
   223          && (KERN_SUCCESS ==
   224             IORegistryEntryCreateCFProperties(parent2, &usbProperties,
   225                                               kCFAllocatorDefault,
   226                                               kNilOptions))) {
   227       if (usbProperties) {
   228          CFTypeRef refCF = 0;
   229          /* get device info
   230           * try hid dictionary first, if fail then go to usb dictionary
   231           */
   232 
   233 
   234          /* Get product name */
   235          refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
   236          if (!refCF)
   237             refCF =
   238                CFDictionaryGetValue(usbProperties, CFSTR("USB Product Name"));
   239          if (refCF) {
   240             if (!CFStringGetCString(refCF, name, 256,
   241                                     CFStringGetSystemEncoding())) {
   242                SDL_SetError("CFStringGetCString error retrieving pDevice->product.");
   243                return -1;
   244             }
   245          }
   246 
   247          CFRelease(usbProperties);
   248       }
   249       else {
   250          SDL_SetError("IORegistryEntryCreateCFProperties failed to create usbProperties.");
   251          return -1;
   252       }
   253 
   254       /* Release stuff. */
   255       if (kIOReturnSuccess != IOObjectRelease(parent2)) {
   256          SDL_SetError("IOObjectRelease error with parent2.");
   257       }
   258       if (kIOReturnSuccess != IOObjectRelease(parent1))  {
   259          SDL_SetError("IOObjectRelease error with parent1.");
   260       }
   261    }
   262    else {
   263       SDL_SetError("Haptic: Error getting registry entries.");
   264       return -1;
   265    }
   266 
   267    return 0;
   268 }
   269 
   270 
   271 #define FF_TEST(ff, s) \
   272 if (features.supportedEffects & ff) supported |= s
   273 /*
   274  * Gets supported features.
   275  */
   276 static unsigned int
   277 GetSupportedFeatures(FFDeviceObjectReference device,
   278                      int *neffects, int *nplaying, int *naxes)
   279 {
   280    HRESULT ret;
   281    FFCAPABILITIES features;
   282    unsigned int supported;
   283    Uint32 val;
   284 
   285    ret = FFDeviceGetForceFeedbackCapabilities(device, &features);
   286    if (ret != FF_OK) {
   287       SDL_SetError("Haptic: Unable to get device's supported features.");
   288       return 0;
   289    }
   290 
   291    supported = 0;
   292 
   293    /* Get maximum effects. */
   294    *neffects = features.storageCapacity;
   295    *nplaying = features.playbackCapacity;
   296 
   297    /* Test for effects. */
   298    FF_TEST(FFCAP_ET_CONSTANTFORCE, SDL_HAPTIC_CONSTANT);
   299    FF_TEST(FFCAP_ET_RAMPFORCE,     SDL_HAPTIC_RAMP);
   300    FF_TEST(FFCAP_ET_SQUARE,        SDL_HAPTIC_SQUARE);
   301    FF_TEST(FFCAP_ET_SINE,          SDL_HAPTIC_SINE);
   302    FF_TEST(FFCAP_ET_TRIANGLE,      SDL_HAPTIC_TRIANGLE);
   303    FF_TEST(FFCAP_ET_SAWTOOTHUP,    SDL_HAPTIC_SAWTOOTHUP);
   304    FF_TEST(FFCAP_ET_SAWTOOTHDOWN,  SDL_HAPTIC_SAWTOOTHDOWN);
   305    FF_TEST(FFCAP_ET_SPRING,        SDL_HAPTIC_SPRING);
   306    FF_TEST(FFCAP_ET_DAMPER,        SDL_HAPTIC_DAMPER);
   307    FF_TEST(FFCAP_ET_INERTIA,       SDL_HAPTIC_INERTIA);
   308    FF_TEST(FFCAP_ET_FRICTION,      SDL_HAPTIC_FRICTION);
   309    FF_TEST(FFCAP_ET_CUSTOMFORCE,   SDL_HAPTIC_CUSTOM);
   310 
   311    /* Check if supports gain. */
   312    ret = FFDeviceGetForceFeedbackProperty( device, FFPROP_FFGAIN,
   313                                            &val, sizeof(val));
   314    if (ret == FF_OK) supported |= SDL_HAPTIC_GAIN;
   315    else if (ret != FFERR_UNSUPPORTED) {
   316       SDL_SetError("Haptic: Unable to get if device supports gain: %s.",
   317                    FFStrError(ret));
   318       return 0;
   319    }
   320 
   321    /* Checks if supports autocenter. */
   322    ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_AUTOCENTER,
   323                                           &val, sizeof(val));
   324    if (ret == FF_OK) supported |= SDL_HAPTIC_AUTOCENTER;
   325    else if (ret != FFERR_UNSUPPORTED) {
   326       SDL_SetError("Haptic: Unable to get if device supports autocenter: %s.",
   327                    FFStrError(ret));
   328       return 0;
   329    }
   330 
   331    /* Check for axes, we have an artificial limit on axes */
   332    *naxes = ((features.numFfAxes) > 3) ?
   333          3 : features.numFfAxes;
   334 
   335    /* Always supported features. */
   336    supported |= SDL_HAPTIC_STATUS;
   337    return supported;
   338 }
   339 
   340 
   341 /*
   342  * Opens the haptic device from the file descriptor.
   343  */
   344 static int
   345 SDL_SYS_HapticOpenFromService(SDL_Haptic * haptic, io_service_t service)
   346 {
   347    HRESULT ret;
   348 
   349    /* Allocate the hwdata */
   350    haptic->hwdata = (struct haptic_hwdata *)
   351          SDL_malloc(sizeof(*haptic->hwdata));
   352    if (haptic->hwdata == NULL) {
   353       SDL_OutOfMemory();
   354       goto creat_err;
   355    }
   356    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
   357   
   358    /* Open the device */
   359    ret = FFCreateDevice( service, &haptic->hwdata->device);
   360    if (ret != FF_OK) {
   361       SDL_SetError("Haptic: Unable to create device from service: %s.",
   362                    FFStrError(ret));
   363       goto creat_err;
   364    }
   365 
   366    /* Get supported features. */
   367    haptic->supported = GetSupportedFeatures( haptic->hwdata->device,
   368                                              &haptic->neffects, &haptic->nplaying,
   369                                              &haptic->naxes );
   370    if (haptic->supported == 0) { /* Error since device supports nothing. */
   371       goto open_err;
   372    }
   373 
   374 
   375    /* Reset and then enable actuators. */
   376    ret = FFDeviceSendForceFeedbackCommand( haptic->hwdata->device,
   377                                            FFSFFC_RESET );
   378    if (ret != FF_OK) {
   379       SDL_SetError("Haptic: Unable to reset device: %s.", FFStrError(ret));
   380       goto open_err;
   381    }
   382    ret = FFDeviceSendForceFeedbackCommand( haptic->hwdata->device,
   383                                            FFSFFC_SETACTUATORSON  );
   384    if (ret != FF_OK) {
   385       SDL_SetError("Haptic: Unable to enable actuators: %s.", FFStrError(ret));
   386       goto open_err;
   387    }
   388 
   389 
   390    /* Allocate effects memory. */
   391    haptic->effects = (struct haptic_effect *)
   392          SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
   393    if (haptic->effects == NULL) {
   394       SDL_OutOfMemory();
   395       goto open_err;
   396    }
   397    /* Clear the memory */
   398    SDL_memset(haptic->effects, 0,
   399          sizeof(struct haptic_effect) * haptic->neffects);
   400    
   401    return 0;
   402    
   403    /* Error handling */
   404 open_err:
   405    FFReleaseDevice(haptic->hwdata->device);
   406 creat_err:
   407    if (haptic->hwdata != NULL) {
   408       free(haptic->hwdata);
   409       haptic->hwdata = NULL;                                              
   410    }
   411    return -1;
   412 
   413 }
   414 
   415 
   416 /*
   417  * Opens a haptic device for usage.
   418  */
   419 int
   420 SDL_SYS_HapticOpen(SDL_Haptic * haptic)
   421 {
   422    return SDL_SYS_HapticOpenFromService(haptic,
   423                 SDL_hapticlist[haptic->index].dev);
   424 }
   425 
   426 
   427 /*
   428  * Opens a haptic device from first mouse it finds for usage.
   429  */
   430 int
   431 SDL_SYS_HapticMouse(void)
   432 {
   433    return -1;
   434 }
   435 
   436 
   437 /*
   438  * Checks to see if a joystick has haptic features.
   439  */
   440 int
   441 SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
   442 {
   443    return SDL_FALSE;
   444 }
   445 
   446 
   447 /*
   448  * Checks to see if the haptic device and joystick and in reality the same.
   449  */
   450 int
   451 SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
   452 {
   453    return 0;
   454 }
   455 
   456 
   457 /*
   458  * Opens a SDL_Haptic from a SDL_Joystick.
   459  */
   460 int
   461 SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
   462 {
   463    return -1;
   464 }
   465 
   466 
   467 /*
   468  * Closes the haptic device.
   469  */
   470 void
   471 SDL_SYS_HapticClose(SDL_Haptic * haptic)
   472 {
   473    if (haptic->hwdata) {
   474 
   475       /* Free Effects. */
   476       SDL_free(haptic->effects);
   477       haptic->effects = NULL;
   478       haptic->neffects = 0;
   479 
   480       /* Clean up */
   481       FFReleaseDevice(haptic->hwdata->device);
   482 
   483       /* Free */
   484       SDL_free(haptic->hwdata);
   485       haptic->hwdata = NULL;
   486    }
   487 }
   488 
   489 
   490 /* 
   491  * Clean up after system specific haptic stuff
   492  */
   493 void
   494 SDL_SYS_HapticQuit(void)
   495 {
   496    int i;
   497 
   498    for (i=0; i < SDL_numhaptics; i++) {
   499       /* Opened and not closed haptics are leaked, this is on purpose.
   500        * Close your haptic devices after usage. */
   501 
   502       /* Free the io_service_t */
   503       IOObjectRelease(SDL_hapticlist[i].dev);
   504    }
   505 }
   506 
   507 
   508 /*
   509  * Converts an SDL trigger button to an FFEFFECT trigger button.
   510  */
   511 static DWORD
   512 FFGetTriggerButton( Uint16 button )
   513 {
   514    DWORD dwTriggerButton;
   515 
   516    dwTriggerButton = FFEB_NOTRIGGER;
   517 
   518    if (button != 0) {
   519       dwTriggerButton = FFJOFS_BUTTON(button - 1);
   520    }
   521 
   522    return dwTriggerButton;
   523 }
   524 
   525 
   526 /*
   527  * Sets the direction.
   528  */
   529 static int
   530 SDL_SYS_SetDirection( FFEFFECT * effect, SDL_HapticDirection *dir, int naxes )
   531 {
   532    LONG *rglDir;
   533 
   534    /* Handle no axes a part. */
   535    if (naxes == 0) {
   536       effect->dwFlags |= FFEFF_SPHERICAL; /* Set as default. */
   537       effect->rglDirection = NULL;
   538       return 0;
   539    }
   540 
   541    /* Has axes. */
   542    rglDir = SDL_malloc( sizeof(LONG) * naxes );
   543    if (rglDir == NULL) {
   544       SDL_OutOfMemory();
   545       return -1;
   546    }
   547    SDL_memset( rglDir, 0, sizeof(LONG) * naxes );
   548    effect->rglDirection = rglDir;
   549 
   550    switch (dir->type) {
   551       case SDL_HAPTIC_POLAR:
   552          effect->dwFlags |= FFEFF_POLAR;
   553          rglDir[0] = dir->dir[0];
   554          return 0;
   555       case SDL_HAPTIC_CARTESIAN:
   556          effect->dwFlags |= FFEFF_CARTESIAN;
   557          rglDir[0] = dir->dir[0];
   558          rglDir[1] = dir->dir[1];
   559          rglDir[2] = dir->dir[2];
   560          return 0;
   561       case SDL_HAPTIC_SPHERICAL:
   562          effect->dwFlags |= FFEFF_SPHERICAL;
   563          rglDir[0] = dir->dir[0];
   564          rglDir[1] = dir->dir[1];
   565          rglDir[2] = dir->dir[2];
   566          return 0;
   567 
   568       default:
   569          SDL_SetError("Haptic: Unknown direction type.");
   570          return -1;
   571    }
   572 }
   573 
   574 #define CONVERT(x)   (((x)*10000) / 0xFFFF )
   575 /*
   576  * Creates the FFEFFECT from a SDL_HapticEffect.
   577  */
   578 static int
   579 SDL_SYS_ToFFEFFECT( SDL_Haptic * haptic, FFEFFECT * dest, SDL_HapticEffect * src )
   580 {
   581    int i;
   582    FFCONSTANTFORCE *constant;
   583    FFPERIODIC *periodic;
   584    FFCONDITION *condition; /* Actually an array of conditions - one per axis. */
   585    FFRAMPFORCE *ramp;
   586    FFCUSTOMFORCE *custom;
   587    FFENVELOPE *envelope;
   588    SDL_HapticConstant *hap_constant;
   589    SDL_HapticPeriodic *hap_periodic;
   590    SDL_HapticCondition *hap_condition;
   591    SDL_HapticRamp *hap_ramp;
   592    SDL_HapticCustom *hap_custom;
   593    DWORD *axes;
   594 
   595    /* Set global stuff. */
   596    SDL_memset(dest, 0, sizeof(FFEFFECT));
   597    dest->dwSize = sizeof(FFEFFECT); /* Set the structure size. */
   598    dest->dwSamplePeriod = 0; /* Not used by us. */
   599    dest->dwGain = 10000; /* Gain is set globally, not locally. */
   600 
   601    /* Envelope. */
   602    envelope = SDL_malloc( sizeof(FFENVELOPE) );
   603    if (envelope == NULL) {
   604       SDL_OutOfMemory();
   605       return -1;
   606    }
   607    SDL_memset(envelope, 0, sizeof(FFENVELOPE));
   608    dest->lpEnvelope = envelope;
   609    envelope->dwSize = sizeof(FFENVELOPE); /* Always should be this. */
   610 
   611    /* Axes. */
   612    dest->cAxes = haptic->naxes;
   613    if (dest->cAxes > 0) {
   614       axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
   615       if (axes == NULL) {
   616          SDL_OutOfMemory();
   617          return -1;
   618       }
   619       axes[0] = FFJOFS_X; /* Always at least one axis. */
   620       if (dest->cAxes > 1) {
   621          axes[1] = FFJOFS_Y;
   622       }
   623       if (dest->cAxes > 2) {
   624          axes[2] = FFJOFS_Z;
   625       }
   626       dest->rgdwAxes = axes;
   627    }
   628 
   629 
   630    /* The big type handling switch, even bigger then linux's version. */
   631    switch (src->type) {
   632       case SDL_HAPTIC_CONSTANT:
   633          hap_constant = &src->constant;
   634          constant = SDL_malloc( sizeof(FFCONSTANTFORCE) );
   635          if (constant == NULL) {
   636             SDL_OutOfMemory();
   637             return -1;
   638          }
   639          SDL_memset(constant, 0, sizeof(FFCONSTANTFORCE));
   640 
   641          /* Specifics */
   642          constant->lMagnitude = CONVERT(hap_constant->level);
   643          dest->cbTypeSpecificParams = sizeof(FFCONSTANTFORCE); 
   644          dest->lpvTypeSpecificParams = constant;
   645 
   646          /* Generics */
   647          dest->dwDuration = hap_constant->length * 1000; /* In microseconds. */
   648          dest->dwTriggerButton = FFGetTriggerButton(hap_constant->button);
   649          dest->dwTriggerRepeatInterval = hap_constant->interval;
   650          dest->dwStartDelay = hap_constant->delay * 1000; /* In microseconds. */
   651 
   652          /* Direction. */
   653          if (SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes) < 0) {
   654             return -1;
   655          }
   656          
   657          /* Envelope */
   658          if ((envelope->dwAttackTime == 0) && (envelope->dwFadeTime == 0)) {
   659             SDL_free(envelope);
   660             dest->lpEnvelope = NULL;
   661          }
   662          else {
   663             envelope->dwAttackLevel = CONVERT(hap_constant->attack_level);
   664             envelope->dwAttackTime = hap_constant->attack_length * 1000;
   665             envelope->dwFadeLevel = CONVERT(hap_constant->fade_level);
   666             envelope->dwFadeTime = hap_constant->fade_length * 1000;
   667          }
   668 
   669          break;
   670 
   671       case SDL_HAPTIC_SINE:
   672       case SDL_HAPTIC_SQUARE:
   673       case SDL_HAPTIC_TRIANGLE:
   674       case SDL_HAPTIC_SAWTOOTHUP:
   675       case SDL_HAPTIC_SAWTOOTHDOWN:
   676          hap_periodic = &src->periodic;
   677          periodic = SDL_malloc(sizeof(FFPERIODIC));
   678          if (periodic == NULL) {
   679             SDL_OutOfMemory();
   680             return -1;
   681          }
   682          SDL_memset(periodic, 0, sizeof(FFPERIODIC));
   683 
   684          /* Specifics */
   685          periodic->dwMagnitude = CONVERT(hap_periodic->magnitude);
   686          periodic->lOffset = CONVERT(hap_periodic->offset);
   687          periodic->dwPhase = hap_periodic->phase;
   688          periodic->dwPeriod = hap_periodic->period * 1000;
   689          dest->cbTypeSpecificParams = sizeof(FFPERIODIC);
   690          dest->lpvTypeSpecificParams = periodic;
   691 
   692          /* Generics */
   693          dest->dwDuration = hap_periodic->length * 1000; /* In microseconds. */
   694          dest->dwTriggerButton = FFGetTriggerButton(hap_periodic->button);
   695          dest->dwTriggerRepeatInterval = hap_periodic->interval;
   696          dest->dwStartDelay = hap_periodic->delay * 1000; /* In microseconds. */
   697          
   698          /* Direction. */
   699          if (SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes) < 0) {
   700             return -1;
   701          }
   702          
   703          /* Envelope */
   704          if ((envelope->dwAttackTime == 0) && (envelope->dwFadeTime == 0)) {
   705             SDL_free(envelope);
   706             dest->lpEnvelope = NULL;
   707          }
   708          else {
   709             envelope->dwAttackLevel = CONVERT(hap_periodic->attack_level);
   710             envelope->dwAttackTime = hap_periodic->attack_length * 1000;
   711             envelope->dwFadeLevel = CONVERT(hap_periodic->fade_level);
   712             envelope->dwFadeTime = hap_periodic->fade_length * 1000;
   713          }
   714 
   715          break;
   716 
   717       case SDL_HAPTIC_SPRING:
   718       case SDL_HAPTIC_DAMPER:
   719       case SDL_HAPTIC_INERTIA:
   720       case SDL_HAPTIC_FRICTION:
   721          hap_condition = &src->condition;
   722          condition = SDL_malloc(sizeof(FFCONDITION) * dest->cAxes);
   723          if (condition == NULL) {
   724             SDL_OutOfMemory();
   725             return -1;
   726          }
   727          SDL_memset(condition, 0, sizeof(FFCONDITION));
   728 
   729          /* Specifics */
   730          for (i=0; i<dest->cAxes; i++) {
   731             condition[i].lOffset = CONVERT(hap_condition->center[i]);
   732             condition[i].lPositiveCoefficient = CONVERT(hap_condition->right_coeff[i]);
   733             condition[i].lNegativeCoefficient = CONVERT(hap_condition->left_coeff[i]);
   734             condition[i].dwPositiveSaturation = CONVERT(hap_condition->right_sat[i]);
   735             condition[i].dwNegativeSaturation = CONVERT(hap_condition->left_sat[i]);
   736             condition[i].lDeadBand = CONVERT(hap_condition->deadband[i]);
   737          }
   738          dest->cbTypeSpecificParams = sizeof(FFCONDITION) * dest->cAxes;
   739          dest->lpvTypeSpecificParams = condition;
   740 
   741          /* Generics */
   742          dest->dwDuration = hap_condition->length * 1000; /* In microseconds. */
   743          dest->dwTriggerButton = FFGetTriggerButton(hap_condition->button);
   744          dest->dwTriggerRepeatInterval = hap_condition->interval;
   745          dest->dwStartDelay = hap_condition->delay * 1000; /* In microseconds. */
   746 
   747          /* Direction. */
   748          if (SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes) < 0) {
   749             return -1;                
   750          }                            
   751                                       
   752          /* Envelope */
   753          SDL_free(dest->lpEnvelope);
   754          dest->lpEnvelope = NULL;
   755 /* TODO Check is envelope actually used.
   756          envelope->dwAttackLevel = CONVERT(hap_condition->attack_level);
   757          envelope->dwAttackTime = hap_condition->attack_length * 1000;
   758          envelope->dwFadeLevel = CONVERT(hap_condition->fade_level);
   759          envelope->dwFadeTime = hap_condition->fade_length * 1000;
   760 */
   761 
   762          break;
   763 
   764       case SDL_HAPTIC_RAMP:
   765          hap_ramp = &src->ramp;
   766          ramp = SDL_malloc(sizeof(FFRAMPFORCE));
   767          if (ramp == NULL) {
   768             SDL_OutOfMemory();
   769             return -1;
   770          }
   771          SDL_memset(ramp, 0, sizeof(FFRAMPFORCE));
   772 
   773          /* Specifics */
   774          ramp->lStart = CONVERT(hap_ramp->start);
   775          ramp->lEnd = CONVERT(hap_ramp->end);
   776          dest->cbTypeSpecificParams = sizeof(FFRAMPFORCE);
   777          dest->lpvTypeSpecificParams = ramp;
   778 
   779          /* Generics */
   780          dest->dwDuration = hap_ramp->length * 1000; /* In microseconds. */
   781          dest->dwTriggerButton = FFGetTriggerButton(hap_ramp->button);
   782          dest->dwTriggerRepeatInterval = hap_ramp->interval;
   783          dest->dwStartDelay = hap_ramp->delay * 1000; /* In microseconds. */
   784 
   785          /* Direction. */
   786          if (SDL_SYS_SetDirection(dest, &hap_ramp->direction, dest->cAxes) < 0) {
   787             return -1;
   788          }
   789 
   790          /* Envelope */
   791          if ((envelope->dwAttackTime == 0) && (envelope->dwFadeTime == 0)) {
   792             SDL_free(envelope);
   793             dest->lpEnvelope = NULL;
   794          else {
   795             envelope->dwAttackLevel = CONVERT(hap_ramp->attack_level);
   796             envelope->dwAttackTime = hap_ramp->attack_length * 1000;
   797             envelope->dwFadeLevel = CONVERT(hap_ramp->fade_level);
   798             envelope->dwFadeTime = hap_ramp->fade_length * 1000;
   799          }
   800 
   801          break;
   802 
   803       case SDL_HAPTIC_CUSTOM:
   804          hap_custom = &src->custom;
   805          custom = SDL_malloc(sizeof(FFCUSTOMFORCE));
   806          if (custom == NULL) {
   807             SDL_OutOfMemory();
   808             return -1;
   809          }
   810          SDL_memset(custom, 0, sizeof(FFCUSTOMFORCE));
   811 
   812          /* Specifics */
   813          custom->cChannels = hap_custom->channels;
   814          custom->dwSamplePeriod = hap_custom->period * 1000;
   815          custom->cSamples = hap_custom->samples;
   816          custom->rglForceData = SDL_malloc(sizeof(LONG)*custom->cSamples*custom->cChannels);
   817          for (i=0; i<hap_custom->samples*hap_custom->channels; i++) { /* Copy data. */
   818             custom->rglForceData[i] = CONVERT(hap_custom->data[i]);
   819          }
   820          dest->cbTypeSpecificParams = sizeof(FFCUSTOMFORCE);
   821          dest->lpvTypeSpecificParams = custom;
   822 
   823          /* Generics */
   824          dest->dwDuration = hap_custom->length * 1000; /* In microseconds. */
   825          dest->dwTriggerButton = FFGetTriggerButton(hap_custom->button);
   826          dest->dwTriggerRepeatInterval = hap_custom->interval;
   827          dest->dwStartDelay = hap_custom->delay * 1000; /* In microseconds. */
   828 
   829          /* Direction. */
   830          if (SDL_SYS_SetDirection(dest, &hap_custom->direction, dest->cAxes) < 0) {
   831             return -1;
   832          }
   833          
   834          /* Envelope */
   835          if ((envelope->dwAttackTime == 0) && (envelope->dwFadeTime == 0)) {
   836             SDL_free(envelope);
   837             dest->lpEnvelope = NULL;
   838          }
   839          else {
   840             envelope->dwAttackLevel = CONVERT(hap_custom->attack_level);
   841             envelope->dwAttackTime = hap_custom->attack_length * 1000;
   842             envelope->dwFadeLevel = CONVERT(hap_custom->fade_level);
   843             envelope->dwFadeTime = hap_custom->fade_length * 1000;
   844          }
   845 
   846          break;
   847 
   848 
   849       default:
   850          SDL_SetError("Haptic: Unknown effect type.");
   851          return -1;
   852    }
   853 
   854    return 0;
   855 }
   856 
   857 
   858 /*
   859  * Frees an FFEFFECT allocated by SDL_SYS_ToFFEFFECT.
   860  */
   861 static void
   862 SDL_SYS_HapticFreeFFEFFECT( FFEFFECT * effect, int type )
   863 {
   864    FFCUSTOMFORCE *custom;
   865 
   866    if (effect->lpEnvelope != NULL) {
   867       SDL_free(effect->lpEnvelope);
   868       effect->lpEnvelope = NULL;
   869    }
   870    if (effect->rgdwAxes != NULL) {
   871       SDL_free(effect->rgdwAxes);
   872       effect->rgdwAxes = NULL;
   873    }
   874    if (effect->lpvTypeSpecificParams != NULL) {
   875       if (type == SDL_HAPTIC_CUSTOM) { /* Must free the custom data. */
   876          custom = (FFCUSTOMFORCE*) effect->lpvTypeSpecificParams;
   877          SDL_free(custom->rglForceData);
   878          custom->rglForceData = NULL;
   879       }
   880       SDL_free(effect->lpvTypeSpecificParams);
   881       effect->lpvTypeSpecificParams = NULL;
   882    }
   883    if (effect->rglDirection != NULL) {
   884       SDL_free(effect->rglDirection);
   885       effect->rglDirection = NULL;
   886    }
   887 }
   888 
   889 
   890 /*
   891  * Gets the effect type from the generic SDL haptic effect wrapper.
   892  */
   893 CFUUIDRef
   894 SDL_SYS_HapticEffectType( Uint16 type )
   895 {
   896    switch (type) {
   897       case SDL_HAPTIC_CONSTANT:
   898          return kFFEffectType_ConstantForce_ID;
   899 
   900       case SDL_HAPTIC_RAMP:
   901          return kFFEffectType_RampForce_ID;
   902 
   903       case SDL_HAPTIC_SQUARE:
   904          return kFFEffectType_Square_ID;
   905 
   906       case SDL_HAPTIC_SINE:
   907          return kFFEffectType_Sine_ID;
   908 
   909       case SDL_HAPTIC_TRIANGLE:
   910          return kFFEffectType_Triangle_ID;
   911 
   912       case SDL_HAPTIC_SAWTOOTHUP:
   913          return kFFEffectType_SawtoothUp_ID;
   914 
   915       case SDL_HAPTIC_SAWTOOTHDOWN:
   916          return kFFEffectType_SawtoothDown_ID;
   917 
   918       case SDL_HAPTIC_SPRING:
   919          return kFFEffectType_Spring_ID;
   920 
   921       case SDL_HAPTIC_DAMPER:
   922          return kFFEffectType_Damper_ID;
   923 
   924       case SDL_HAPTIC_INERTIA:
   925          return kFFEffectType_Inertia_ID;
   926 
   927       case SDL_HAPTIC_FRICTION:
   928          return kFFEffectType_Friction_ID;
   929 
   930       case SDL_HAPTIC_CUSTOM:
   931          return kFFEffectType_CustomForce_ID;
   932 
   933       default:
   934          SDL_SetError("Haptic: Unknown effect type.");
   935          return NULL;
   936    }
   937 }
   938 
   939 
   940 /*
   941  * Creates a new haptic effect.
   942  */
   943 int
   944 SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
   945       SDL_HapticEffect * base)
   946 {
   947    HRESULT ret;
   948    CFUUIDRef type;
   949 
   950    /* Alloc the effect. */
   951    effect->hweffect = (struct haptic_hweffect *)
   952          SDL_malloc(sizeof(struct haptic_hweffect));
   953    if (effect->hweffect == NULL) {
   954       SDL_OutOfMemory();
   955       goto err_hweffect;
   956    }
   957 
   958    /* Get the type. */
   959    type = SDL_SYS_HapticEffectType(base->type);
   960    if (type == NULL) {
   961       goto err_hweffect;
   962    }
   963 
   964    /* Get the effect. */
   965    if (SDL_SYS_ToFFEFFECT(haptic, &effect->hweffect->effect, base) < 0) {
   966       goto err_effectdone;
   967    }
   968 
   969    /* Create the actual effect. */
   970    ret = FFDeviceCreateEffect(haptic->hwdata->device, type,
   971          &effect->hweffect->effect, &effect->hweffect->ref);
   972    if (ret != FF_OK) {
   973       SDL_SetError("Haptic: Unable to create effect: %s.", FFStrError(ret));
   974       goto err_effectdone;
   975    }
   976 
   977    return 0;
   978 
   979 err_effectdone:
   980    SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, base->type);
   981 err_hweffect:
   982    if (effect->hweffect != NULL) {
   983       SDL_free(effect->hweffect);
   984       effect->hweffect = NULL;
   985    }
   986    return -1;
   987 }
   988 
   989 
   990 /*
   991  * Updates an effect.
   992  */
   993 int
   994 SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
   995       struct haptic_effect * effect, SDL_HapticEffect * data)
   996 {
   997    HRESULT ret;
   998    FFEffectParameterFlag flags;
   999    FFEFFECT temp;
  1000 
  1001    /* Get the effect. */
  1002    SDL_memset(&temp, 0, sizeof(FFEFFECT));
  1003    if (SDL_SYS_ToFFEFFECT(haptic, &temp, data) < 0) {
  1004       goto err_update;
  1005    }
  1006 
  1007    /* Set the flags.  Might be worthwhile to diff temp with loaded effect and
  1008     *  only change those parameters. */
  1009    flags = FFEP_ALLPARAMS;
  1010 
  1011    /* Create the actual effect. */
  1012    ret = FFEffectSetParameters(effect->hweffect->ref, &temp, flags);
  1013    if (ret != FF_OK) {
  1014       SDL_SetError("Haptic: Unable to update effect: %s.", FFStrError(ret));
  1015       goto err_update;
  1016    }
  1017 
  1018    /* Copy it over. */
  1019    SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, data->type);
  1020    SDL_memcpy(&effect->hweffect->effect, &temp, sizeof(FFEFFECT));
  1021 
  1022    return 0;
  1023 
  1024 err_update:
  1025    SDL_SYS_HapticFreeFFEFFECT(&temp, data->type);
  1026    return -1;
  1027 }
  1028 
  1029 
  1030 /*
  1031  * Runs an effect.
  1032  */
  1033 int
  1034 SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
  1035                         Uint32 iterations)
  1036 {
  1037    HRESULT ret;
  1038    Uint32 iter;
  1039 
  1040    /* Check if it's infinite. */
  1041    if (iterations == SDL_HAPTIC_INFINITY) {
  1042       iter = FF_INFINITE;
  1043    }
  1044    else
  1045       iter = iterations;
  1046 
  1047    /* Run the effect. */
  1048    ret = FFEffectStart(effect->hweffect->ref, iter, 0);
  1049    if (ret != FF_OK) {
  1050       SDL_SetError("Haptic: Unable to run the effect: %s.", FFStrError(ret));
  1051       return -1;
  1052    }
  1053 
  1054    return 0;
  1055 }
  1056 
  1057 
  1058 /*
  1059  * Stops an effect.
  1060  */
  1061 int
  1062 SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
  1063 {
  1064    HRESULT ret;
  1065 
  1066    ret = FFEffectStop(effect->hweffect->ref);
  1067    if (ret != FF_OK) {
  1068       SDL_SetError("Haptic: Unable to stop the effect: %s.", FFStrError(ret));
  1069       return -1;
  1070    }
  1071 
  1072    return 0;
  1073 }
  1074 
  1075 
  1076 /*
  1077  * Frees the effect.
  1078  */
  1079 void
  1080 SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
  1081 {
  1082    HRESULT ret;
  1083 
  1084    ret = FFDeviceReleaseEffect(haptic->hwdata->device, effect->hweffect->ref);
  1085    if (ret != FF_OK) {
  1086       SDL_SetError("Haptic: Error removing the effect from the device: %s.",
  1087                    FFStrError(ret));
  1088    }
  1089    SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, effect->effect.type);
  1090    SDL_free(effect->hweffect);
  1091    effect->hweffect = NULL;
  1092 }
  1093 
  1094 
  1095 /*
  1096  * Gets the status of a haptic effect.
  1097  */
  1098 int
  1099 SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect * effect)
  1100 {
  1101    HRESULT ret;
  1102    FFEffectStatusFlag status;
  1103 
  1104    ret = FFEffectGetEffectStatus(effect->hweffect->ref, &status);
  1105    if (ret != FF_OK) {
  1106       SDL_SetError("Haptic: Unable to get effect status: %s.", FFStrError(ret));
  1107       return -1;
  1108    }
  1109 
  1110    if (status == 0) return SDL_FALSE;
  1111    return SDL_TRUE; /* Assume it's playing or emulated. */
  1112 }
  1113 
  1114 
  1115 /*
  1116  * Sets the gain.
  1117  */
  1118 int
  1119 SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
  1120 {
  1121    HRESULT ret;
  1122    Uint32 val;
  1123 
  1124    val = gain * 100; /* Mac OS X uses 0 to 10,000 */
  1125    ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device, FFPROP_FFGAIN, &val);
  1126    if (ret != FF_OK) {
  1127       SDL_SetError("Haptic: Error setting gain: %s.", FFStrError(ret));
  1128       return -1;
  1129    }
  1130 
  1131    return 0;
  1132 }
  1133 
  1134 
  1135 /*
  1136  * Sets the autocentering.
  1137  */
  1138 int
  1139 SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
  1140 {
  1141    HRESULT ret;
  1142    Uint32 val;
  1143 
  1144    /* Mac OS X only has 0 (off) and 1 (on) */
  1145    if (autocenter == 0) val = 0;
  1146    else val = 1;
  1147 
  1148    ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
  1149                FFPROP_AUTOCENTER, &val);
  1150    if (ret != FF_OK) {
  1151       SDL_SetError("Haptic: Error setting autocenter: %s.", FFStrError(ret));
  1152       return -1;
  1153    }
  1154   
  1155    return 0;
  1156 
  1157 }
  1158 
  1159 
  1160 #endif /* SDL_HAPTIC_IOKIT */