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