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