src/haptic/darwin/SDL_syshaptic.c
author Edgar Simo <bobbens@gmail.com>
Sun, 20 Jul 2008 21:55:23 +0000
branchgsoc2008_force_feedback
changeset 2542 4e055fa7acb9
parent 2541 f2649eaa552e
child 2543 3544a632064c
permissions -rw-r--r--
Fixed some stupid mistakes.
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@2541
    86
      case FFERR_DEVICENOTREG:
bobbens@2541
    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@2537
   204
bobbens@2537
   205
   hidProperties = usbProperties = 0;
bobbens@2537
   206
bobbens@2537
   207
   /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
bobbens@2537
   208
    * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
bobbens@2537
   209
    */
bobbens@2537
   210
   if ((KERN_SUCCESS ==
bobbens@2537
   211
            IORegistryEntryGetParentEntry(dev, kIOServicePlane, &parent1))
bobbens@2537
   212
         && (KERN_SUCCESS ==
bobbens@2537
   213
            IORegistryEntryGetParentEntry(parent1, kIOServicePlane, &parent2))
bobbens@2537
   214
         && (KERN_SUCCESS ==
bobbens@2537
   215
            IORegistryEntryCreateCFProperties(parent2, &usbProperties,
bobbens@2537
   216
               kCFAllocatorDefault,
bobbens@2537
   217
               kNilOptions))) {
bobbens@2537
   218
      if (usbProperties) {
bobbens@2537
   219
         CFTypeRef refCF = 0;
bobbens@2537
   220
         /* get device info
bobbens@2537
   221
          * try hid dictionary first, if fail then go to usb dictionary
bobbens@2537
   222
          */
bobbens@2537
   223
bobbens@2537
   224
bobbens@2537
   225
         /* Get product name */
bobbens@2537
   226
         refCF =
bobbens@2537
   227
            CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
bobbens@2537
   228
         if (!refCF)
bobbens@2537
   229
            refCF =
bobbens@2537
   230
               CFDictionaryGetValue(usbProperties, CFSTR("USB Product Name"));
bobbens@2537
   231
         if (refCF) {
bobbens@2537
   232
            if (!CFStringGetCString(refCF, name, 256,
bobbens@2537
   233
                                    CFStringGetSystemEncoding())) {
bobbens@2537
   234
               SDL_SetError("CFStringGetCString error retrieving pDevice->product.");
bobbens@2537
   235
               return -1;
bobbens@2537
   236
            }
bobbens@2537
   237
         }
bobbens@2537
   238
bobbens@2537
   239
         CFRelease(usbProperties);
bobbens@2537
   240
      }
bobbens@2537
   241
      else {
bobbens@2537
   242
         SDL_SetError("IORegistryEntryCreateCFProperties failed to create usbProperties.");
bobbens@2537
   243
         return -1;
bobbens@2537
   244
      }
bobbens@2537
   245
bobbens@2537
   246
      /* Release stuff. */
bobbens@2537
   247
      if (kIOReturnSuccess != IOObjectRelease(parent2)) {
bobbens@2537
   248
         SDL_SetError("IOObjectRelease error with parent2.");
bobbens@2537
   249
      }
bobbens@2537
   250
      if (kIOReturnSuccess != IOObjectRelease(parent1))  {
bobbens@2537
   251
         SDL_SetError("IOObjectRelease error with parent1.");
bobbens@2537
   252
      }
bobbens@2537
   253
   }
bobbens@2537
   254
   return 0;
bobbens@2520
   255
}
bobbens@2520
   256
bobbens@2520
   257
bobbens@2520
   258
#define FF_TEST(ff, s) \
bobbens@2520
   259
if (features.supportedEffects & ff) supported |= s
bobbens@2520
   260
/*
bobbens@2520
   261
 * Gets supported features.
bobbens@2520
   262
 */
bobbens@2520
   263
static unsigned int
bobbens@2520
   264
GetSupportedFeatures(FFDeviceObjectReference device,
bobbens@2527
   265
                     int *neffects, int *nplaying, int *naxes)
bobbens@2520
   266
{
bobbens@2520
   267
   HRESULT ret;
bobbens@2520
   268
   FFCAPABILITIES features;
bobbens@2520
   269
   unsigned int supported;
bobbens@2520
   270
   Uint32 val;
bobbens@2520
   271
bobbens@2520
   272
   ret = FFDeviceGetForceFeedbackCapabilities(device, &features);
bobbens@2520
   273
   if (ret != FF_OK) {
bobbens@2520
   274
      SDL_SetError("Haptic: Unable to get device's supported features.");
bobbens@2520
   275
      return 0;
bobbens@2520
   276
   }
bobbens@2520
   277
bobbens@2520
   278
   supported = 0;
bobbens@2520
   279
bobbens@2520
   280
   /* Get maximum effects. */
bobbens@2520
   281
   *neffects = features.storageCapacity;
bobbens@2520
   282
   *nplaying = features.playbackCapacity;
bobbens@2520
   283
bobbens@2520
   284
   /* Test for effects. */
bobbens@2520
   285
   FF_TEST(FFCAP_ET_CONSTANTFORCE, SDL_HAPTIC_CONSTANT);
bobbens@2520
   286
   FF_TEST(FFCAP_ET_RAMPFORCE,     SDL_HAPTIC_RAMP);
bobbens@2520
   287
   FF_TEST(FFCAP_ET_SQUARE,        SDL_HAPTIC_SQUARE);
bobbens@2520
   288
   FF_TEST(FFCAP_ET_SINE,          SDL_HAPTIC_SINE);
bobbens@2520
   289
   FF_TEST(FFCAP_ET_TRIANGLE,      SDL_HAPTIC_TRIANGLE);
bobbens@2520
   290
   FF_TEST(FFCAP_ET_SAWTOOTHUP,    SDL_HAPTIC_SAWTOOTHUP);
bobbens@2520
   291
   FF_TEST(FFCAP_ET_SAWTOOTHDOWN,  SDL_HAPTIC_SAWTOOTHDOWN);
bobbens@2520
   292
   FF_TEST(FFCAP_ET_SPRING,        SDL_HAPTIC_SPRING);
bobbens@2520
   293
   FF_TEST(FFCAP_ET_DAMPER,        SDL_HAPTIC_DAMPER);
bobbens@2520
   294
   FF_TEST(FFCAP_ET_INERTIA,       SDL_HAPTIC_INERTIA);
bobbens@2520
   295
   FF_TEST(FFCAP_ET_FRICTION,      SDL_HAPTIC_FRICTION);
bobbens@2520
   296
   FF_TEST(FFCAP_ET_CUSTOMFORCE,   SDL_HAPTIC_CUSTOM);
bobbens@2520
   297
bobbens@2520
   298
   /* Check if supports gain. */
bobbens@2520
   299
   ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_FFGAIN,
bobbens@2522
   300
                                          &val, sizeof(val));
bobbens@2520
   301
   if (ret == FF_OK) supported |= SDL_HAPTIC_GAIN;
bobbens@2520
   302
   else if (ret != FFERR_UNSUPPORTED) {
bobbens@2541
   303
      SDL_SetError("Haptic: Unable to get if device supports gain: %s.",
bobbens@2542
   304
                   FFStrError(ret));
bobbens@2520
   305
      return 0;
bobbens@2520
   306
   }
bobbens@2520
   307
bobbens@2520
   308
   /* Checks if supports autocenter. */
bobbens@2521
   309
   ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_AUTOCENTER,
bobbens@2522
   310
                                          &val, sizeof(val));
bobbens@2520
   311
   if (ret == FF_OK) supported |= SDL_HAPTIC_AUTOCENTER;
bobbens@2520
   312
   else if (ret != FFERR_UNSUPPORTED) {
bobbens@2541
   313
      SDL_SetError("Haptic: Unable to get if device supports autocenter: %s.",
bobbens@2541
   314
                   FFStrError(ret));
bobbens@2520
   315
      return 0;
bobbens@2520
   316
   }
bobbens@2520
   317
bobbens@2527
   318
   /* Check for axes, we have an artificial limit on axes */
bobbens@2528
   319
   *naxes = ((features.numFfAxes) > 3) ?
bobbens@2527
   320
         3 : features.numFfAxes;
bobbens@2527
   321
bobbens@2520
   322
   /* Always supported features. */
bobbens@2520
   323
   supported |= SDL_HAPTIC_STATUS;
bobbens@2520
   324
   return supported;
bobbens@2520
   325
}
bobbens@2520
   326
bobbens@2520
   327
bobbens@2520
   328
/*
bobbens@2520
   329
 * Opens the haptic device from the file descriptor.
bobbens@2520
   330
 */
bobbens@2520
   331
static int
bobbens@2520
   332
SDL_SYS_HapticOpenFromService(SDL_Haptic * haptic, io_service_t service)
bobbens@2520
   333
{
bobbens@2541
   334
   HRESULT ret;
bobbens@2541
   335
bobbens@2520
   336
   /* Allocate the hwdata */
bobbens@2520
   337
   haptic->hwdata = (struct haptic_hwdata *)
bobbens@2520
   338
         SDL_malloc(sizeof(*haptic->hwdata));
bobbens@2520
   339
   if (haptic->hwdata == NULL) {
bobbens@2520
   340
      SDL_OutOfMemory();
bobbens@2520
   341
      goto creat_err;
bobbens@2520
   342
   }
bobbens@2520
   343
   SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
bobbens@2520
   344
  
bobbens@2520
   345
   /* Open the device */
bobbens@2541
   346
   ret = FFCreateDevice( service, &haptic->hwdata->device);
bobbens@2541
   347
   if (ret != FF_OK) {
bobbens@2541
   348
      SDL_SetError("Haptic: Unable to create device from service: %s.",
bobbens@2541
   349
                   FFStrError(ret));
bobbens@2520
   350
      goto creat_err;
bobbens@2520
   351
   }
bobbens@2520
   352
bobbens@2520
   353
   /* Get supported features. */
bobbens@2520
   354
   haptic->supported = GetSupportedFeatures(haptic->hwdata->device,
bobbens@2527
   355
                                            &haptic->neffects, &haptic->nplaying,
bobbens@2527
   356
                                            &haptic->naxes);
bobbens@2520
   357
   if (haptic->supported == 0) { /* Error since device supports nothing. */
bobbens@2520
   358
      goto open_err;
bobbens@2520
   359
   }
bobbens@2520
   360
   haptic->effects = (struct haptic_effect *)
bobbens@2520
   361
         SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
bobbens@2520
   362
   if (haptic->effects == NULL) {
bobbens@2520
   363
      SDL_OutOfMemory();
bobbens@2520
   364
      goto open_err;
bobbens@2520
   365
   }
bobbens@2520
   366
   /* Clear the memory */
bobbens@2520
   367
   SDL_memset(haptic->effects, 0,
bobbens@2520
   368
         sizeof(struct haptic_effect) * haptic->neffects);
bobbens@2520
   369
   
bobbens@2520
   370
   return 0;
bobbens@2520
   371
   
bobbens@2520
   372
   /* Error handling */
bobbens@2520
   373
open_err:
bobbens@2520
   374
   FFReleaseDevice(haptic->hwdata->device);
bobbens@2520
   375
creat_err:
bobbens@2520
   376
   if (haptic->hwdata != NULL) {
bobbens@2520
   377
      free(haptic->hwdata);
bobbens@2520
   378
      haptic->hwdata = NULL;                                              
bobbens@2520
   379
   }
bobbens@2520
   380
   return -1;
bobbens@2520
   381
bobbens@2520
   382
}
bobbens@2520
   383
bobbens@2520
   384
bobbens@2520
   385
/*
bobbens@2520
   386
 * Opens a haptic device for usage.
bobbens@2520
   387
 */
bobbens@2520
   388
int
bobbens@2520
   389
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
bobbens@2520
   390
{
bobbens@2520
   391
   return SDL_SYS_HapticOpenFromService(haptic,
bobbens@2522
   392
                SDL_hapticlist[haptic->index].dev);
bobbens@2520
   393
}
bobbens@2520
   394
bobbens@2520
   395
bobbens@2520
   396
/*
bobbens@2520
   397
 * Opens a haptic device from first mouse it finds for usage.
bobbens@2520
   398
 */
bobbens@2520
   399
int
bobbens@2520
   400
SDL_SYS_HapticMouse(void)
bobbens@2520
   401
{
bobbens@2520
   402
   return -1;
bobbens@2520
   403
}
bobbens@2520
   404
bobbens@2520
   405
bobbens@2520
   406
/*
bobbens@2520
   407
 * Checks to see if a joystick has haptic features.
bobbens@2520
   408
 */
bobbens@2520
   409
int
bobbens@2520
   410
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
bobbens@2520
   411
{
bobbens@2520
   412
   return SDL_FALSE;
bobbens@2520
   413
}
bobbens@2520
   414
bobbens@2520
   415
bobbens@2520
   416
/*
bobbens@2520
   417
 * Checks to see if the haptic device and joystick and in reality the same.
bobbens@2520
   418
 */
bobbens@2520
   419
int
bobbens@2520
   420
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
bobbens@2520
   421
{
bobbens@2520
   422
   return 0;
bobbens@2520
   423
}
bobbens@2520
   424
bobbens@2520
   425
bobbens@2520
   426
/*
bobbens@2520
   427
 * Opens a SDL_Haptic from a SDL_Joystick.
bobbens@2520
   428
 */
bobbens@2520
   429
int
bobbens@2520
   430
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
bobbens@2520
   431
{
bobbens@2520
   432
   return -1;
bobbens@2520
   433
}
bobbens@2520
   434
bobbens@2520
   435
bobbens@2520
   436
/*
bobbens@2520
   437
 * Closes the haptic device.
bobbens@2520
   438
 */
bobbens@2520
   439
void
bobbens@2520
   440
SDL_SYS_HapticClose(SDL_Haptic * haptic)
bobbens@2520
   441
{
bobbens@2520
   442
   int i;
bobbens@2520
   443
bobbens@2520
   444
   if (haptic->hwdata) {
bobbens@2520
   445
bobbens@2530
   446
      /* Free the effects. */
bobbens@2530
   447
      for (i=0; i<haptic->neffects; i++) {        
bobbens@2530
   448
         if (haptic->effects[i].hweffect != NULL) {
bobbens@2535
   449
            SDL_SYS_HapticFreeFFEFFECT(&haptic->effects[i].hweffect->effect,
bobbens@2535
   450
                                       haptic->effects[i].effect.type);
bobbens@2530
   451
            SDL_free(haptic->effects[i].hweffect);
bobbens@2530
   452
         } 
bobbens@2530
   453
      }    
bobbens@2530
   454
      SDL_free(haptic->effects);
bobbens@2530
   455
      haptic->neffects = 0;
bobbens@2530
   456
bobbens@2520
   457
      /* Clean up */
bobbens@2520
   458
      FFReleaseDevice(haptic->hwdata->device);
bobbens@2520
   459
bobbens@2520
   460
      /* Free */
bobbens@2520
   461
      SDL_free(haptic->hwdata);
bobbens@2520
   462
      haptic->hwdata = NULL;
bobbens@2520
   463
   }
bobbens@2520
   464
}
bobbens@2520
   465
bobbens@2520
   466
bobbens@2520
   467
/* 
bobbens@2520
   468
 * Clean up after system specific haptic stuff
bobbens@2520
   469
 */
bobbens@2520
   470
void
bobbens@2520
   471
SDL_SYS_HapticQuit(void)
bobbens@2520
   472
{
bobbens@2520
   473
   int i;
bobbens@2520
   474
bobbens@2520
   475
   for (i=0; i < SDL_numhaptics; i++) {
bobbens@2530
   476
      /* Opened and not closed haptics are leaked, this is on purpose.
bobbens@2530
   477
       * Close your haptic devices after usage. */
bobbens@2530
   478
bobbens@2530
   479
      /* Free the io_service_t */
bobbens@2520
   480
      IOObjectRelease(SDL_hapticlist[i].dev);
bobbens@2520
   481
   }
bobbens@2520
   482
}
bobbens@2520
   483
bobbens@2520
   484
bobbens@2520
   485
/*
bobbens@2520
   486
 * Sets the direction.
bobbens@2520
   487
 */
bobbens@2520
   488
static int
bobbens@2529
   489
SDL_SYS_SetDirection( FFEFFECT * effect, SDL_HapticDirection *dir, int naxes )
bobbens@2520
   490
{
bobbens@2521
   491
   LONG *rglDir;
bobbens@2527
   492
bobbens@2527
   493
   /* Handle no axes a part. */
bobbens@2528
   494
   if (naxes == 0) {
bobbens@2527
   495
      effect->rglDirection = NULL;
bobbens@2527
   496
      return 0;
bobbens@2527
   497
   }
bobbens@2527
   498
bobbens@2527
   499
   /* Has axes. */
bobbens@2529
   500
   rglDir = SDL_malloc( sizeof(LONG) * naxes );
bobbens@2527
   501
   if (rglDir == NULL) {
bobbens@2520
   502
      SDL_OutOfMemory();
bobbens@2520
   503
      return -1;
bobbens@2520
   504
   }
bobbens@2529
   505
   SDL_memset( rglDir, 0, sizeof(LONG) * naxes );
bobbens@2521
   506
   effect->rglDirection = rglDir;
bobbens@2520
   507
bobbens@2520
   508
   switch (dir->type) {
bobbens@2520
   509
      case SDL_HAPTIC_POLAR:
bobbens@2520
   510
         effect->dwFlags |= FFEFF_POLAR;
bobbens@2521
   511
         rglDir[0] = dir->dir[0];
bobbens@2520
   512
         return 0;
bobbens@2520
   513
      case SDL_HAPTIC_CARTESIAN:
bobbens@2522
   514
         effect->dwFlags |= FFEFF_CARTESIAN;
bobbens@2521
   515
         rglDir[0] = dir->dir[0];
bobbens@2521
   516
         rglDir[1] = dir->dir[1];
bobbens@2521
   517
         rglDir[2] = dir->dir[2];
bobbens@2520
   518
         return 0;
bobbens@2521
   519
      case SDL_HAPTIC_SPHERICAL:
bobbens@2522
   520
         effect->dwFlags |= FFEFF_SPHERICAL;
bobbens@2521
   521
         rglDir[0] = dir->dir[0];
bobbens@2521
   522
         rglDir[1] = dir->dir[1];
bobbens@2521
   523
         rglDir[2] = dir->dir[2];
bobbens@2520
   524
         return 0;
bobbens@2520
   525
bobbens@2520
   526
      default:
bobbens@2520
   527
         SDL_SetError("Haptic: Unknown direction type.");
bobbens@2520
   528
         return -1;
bobbens@2520
   529
   }
bobbens@2520
   530
}
bobbens@2520
   531
bobbens@2520
   532
#define CONVERT(x)   (((x)*10000) / 0xFFFF )
bobbens@2520
   533
/*
bobbens@2527
   534
 * Creates the FFEFFECT from a SDL_HapticEffect.
bobbens@2520
   535
 */
bobbens@2520
   536
static int
bobbens@2527
   537
SDL_SYS_ToFFEFFECT( SDL_Haptic * haptic, FFEFFECT * dest, SDL_HapticEffect * src )
bobbens@2520
   538
{
bobbens@2530
   539
   int i;
bobbens@2520
   540
   FFCONSTANTFORCE *constant;
bobbens@2520
   541
   FFPERIODIC *periodic;
bobbens@2530
   542
   FFCONDITION *condition; /* Actually an array of conditions - one per axis. */
bobbens@2520
   543
   FFRAMPFORCE *ramp;
bobbens@2520
   544
   FFCUSTOMFORCE *custom;
bobbens@2522
   545
   FFENVELOPE *envelope;
bobbens@2520
   546
   SDL_HapticConstant *hap_constant;
bobbens@2521
   547
   SDL_HapticPeriodic *hap_periodic;
bobbens@2520
   548
   SDL_HapticCondition *hap_condition;
bobbens@2520
   549
   SDL_HapticRamp *hap_ramp;
bobbens@2535
   550
   SDL_HapticCustom *hap_custom;
bobbens@2527
   551
   DWORD *axes;
bobbens@2520
   552
bobbens@2520
   553
   /* Set global stuff. */
bobbens@2520
   554
   SDL_memset(dest, 0, sizeof(FFEFFECT));
bobbens@2520
   555
   dest->dwSize = sizeof(FFEFFECT); /* Set the structure size. */
bobbens@2520
   556
   dest->dwSamplePeriod = 0; /* Not used by us. */
bobbens@2520
   557
   dest->dwGain = 10000; /* Gain is set globally, not locally. */
bobbens@2522
   558
bobbens@2522
   559
   /* Envelope. */
bobbens@2522
   560
   envelope = SDL_malloc( sizeof(FFENVELOPE) );
bobbens@2522
   561
   if (envelope == NULL) {
bobbens@2522
   562
      SDL_OutOfMemory();
bobbens@2522
   563
      return -1;
bobbens@2522
   564
   }
bobbens@2522
   565
   SDL_memset(envelope, 0, sizeof(FFENVELOPE));
bobbens@2522
   566
   dest->lpEnvelope = envelope;
bobbens@2522
   567
   envelope->dwSize = sizeof(FFENVELOPE); /* Always should be this. */
bobbens@2520
   568
bobbens@2527
   569
   /* Axes. */
bobbens@2527
   570
   dest->cAxes = haptic->naxes;
bobbens@2527
   571
   if (dest->cAxes > 0) {
bobbens@2527
   572
      axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
bobbens@2527
   573
      if (axes == NULL) {
bobbens@2527
   574
         SDL_OutOfMemory();
bobbens@2527
   575
         return -1;
bobbens@2527
   576
      }
bobbens@2527
   577
      axes[0] = FFJOFS_X; /* Always at least one axis. */
bobbens@2527
   578
      if (dest->cAxes > 1) {
bobbens@2527
   579
         axes[1] = FFJOFS_Y;
bobbens@2527
   580
      }
bobbens@2527
   581
      if (dest->cAxes > 2) {
bobbens@2527
   582
         axes[2] = FFJOFS_Z;
bobbens@2527
   583
      }
bobbens@2527
   584
      dest->rgdwAxes = axes;
bobbens@2527
   585
   }
bobbens@2527
   586
bobbens@2527
   587
bobbens@2530
   588
   /* The big type handling switch, even bigger then linux's version. */
bobbens@2520
   589
   switch (src->type) {
bobbens@2520
   590
      case SDL_HAPTIC_CONSTANT:
bobbens@2520
   591
         hap_constant = &src->constant;
bobbens@2520
   592
         constant = SDL_malloc( sizeof(FFCONSTANTFORCE) );
bobbens@2527
   593
         if (constant == NULL) {
bobbens@2527
   594
            SDL_OutOfMemory();
bobbens@2527
   595
            return -1;
bobbens@2527
   596
         }
bobbens@2535
   597
         SDL_memset(constant, 0, sizeof(FFCONSTANTFORCE));
bobbens@2520
   598
bobbens@2520
   599
         /* Specifics */
bobbens@2520
   600
         constant->lMagnitude = CONVERT(hap_constant->level);
bobbens@2520
   601
         dest->cbTypeSpecificParams = sizeof(FFCONSTANTFORCE); 
bobbens@2520
   602
         dest->lpvTypeSpecificParams = constant;
bobbens@2520
   603
bobbens@2520
   604
         /* Generics */
bobbens@2521
   605
         dest->dwDuration = hap_constant->length * 1000; /* In microseconds. */
bobbens@2520
   606
         dest->dwTriggerButton = FFJOFS_BUTTON(hap_constant->button);
bobbens@2520
   607
         dest->dwTriggerRepeatInterval = hap_constant->interval;
bobbens@2521
   608
         dest->dwStartDelay = hap_constant->delay * 1000; /* In microseconds. */
bobbens@2520
   609
bobbens@2520
   610
         /* Direction. */
bobbens@2522
   611
         if (SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes) < 0) {
bobbens@2520
   612
            return -1;
bobbens@2520
   613
         }
bobbens@2520
   614
         
bobbens@2520
   615
         /* Envelope */
bobbens@2522
   616
         envelope->dwAttackLevel = CONVERT(hap_constant->attack_level);
bobbens@2522
   617
         envelope->dwAttackTime = hap_constant->attack_length * 1000;
bobbens@2522
   618
         envelope->dwFadeLevel = CONVERT(hap_constant->fade_level);
bobbens@2522
   619
         envelope->dwFadeTime = hap_constant->fade_length * 1000;
bobbens@2520
   620
bobbens@2520
   621
         break;
bobbens@2520
   622
bobbens@2520
   623
      case SDL_HAPTIC_SINE:
bobbens@2520
   624
      case SDL_HAPTIC_SQUARE:
bobbens@2520
   625
      case SDL_HAPTIC_TRIANGLE:
bobbens@2520
   626
      case SDL_HAPTIC_SAWTOOTHUP:
bobbens@2520
   627
      case SDL_HAPTIC_SAWTOOTHDOWN:
bobbens@2522
   628
         hap_periodic = &src->periodic;
bobbens@2527
   629
         periodic = SDL_malloc(sizeof(FFPERIODIC));
bobbens@2527
   630
         if (periodic == NULL) {
bobbens@2527
   631
            SDL_OutOfMemory();
bobbens@2527
   632
            return -1;
bobbens@2527
   633
         }
bobbens@2535
   634
         SDL_memset(periodic, 0, sizeof(FFPERIODIC));
bobbens@2527
   635
bobbens@2527
   636
         /* Specifics */
bobbens@2527
   637
         periodic->dwMagnitude = CONVERT(hap_periodic->magnitude);
bobbens@2527
   638
         periodic->lOffset = CONVERT(hap_periodic->offset);
bobbens@2527
   639
         periodic->dwPhase = hap_periodic->phase;
bobbens@2527
   640
         periodic->dwPeriod = hap_periodic->period * 1000;
bobbens@2527
   641
         dest->cbTypeSpecificParams = sizeof(FFPERIODIC);
bobbens@2527
   642
         dest->lpvTypeSpecificParams = periodic;
bobbens@2527
   643
bobbens@2527
   644
         /* Generics */
bobbens@2527
   645
         dest->dwDuration = hap_periodic->length * 1000; /* In microseconds. */
bobbens@2527
   646
         dest->dwTriggerButton = FFJOFS_BUTTON(hap_periodic->button);
bobbens@2527
   647
         dest->dwTriggerRepeatInterval = hap_periodic->interval;
bobbens@2527
   648
         dest->dwStartDelay = hap_periodic->delay * 1000; /* In microseconds. */
bobbens@2527
   649
         
bobbens@2527
   650
         /* Direction. */
bobbens@2527
   651
         if (SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes) < 0) {
bobbens@2527
   652
            return -1;
bobbens@2527
   653
         }
bobbens@2527
   654
         
bobbens@2527
   655
         /* Envelope */
bobbens@2527
   656
         envelope->dwAttackLevel = CONVERT(hap_periodic->attack_level);
bobbens@2527
   657
         envelope->dwAttackTime = hap_periodic->attack_length * 1000;
bobbens@2527
   658
         envelope->dwFadeLevel = CONVERT(hap_periodic->fade_level);
bobbens@2527
   659
         envelope->dwFadeTime = hap_periodic->fade_length * 1000;
bobbens@2520
   660
bobbens@2520
   661
         break;
bobbens@2520
   662
bobbens@2520
   663
      case SDL_HAPTIC_SPRING:
bobbens@2520
   664
      case SDL_HAPTIC_DAMPER:
bobbens@2520
   665
      case SDL_HAPTIC_INERTIA:
bobbens@2520
   666
      case SDL_HAPTIC_FRICTION:
bobbens@2522
   667
         hap_condition = &src->condition;
bobbens@2530
   668
         condition = SDL_malloc(sizeof(FFCONDITION) * dest->cAxes);
bobbens@2530
   669
         if (condition == NULL) {
bobbens@2530
   670
            SDL_OutOfMemory();
bobbens@2530
   671
            return -1;
bobbens@2530
   672
         }
bobbens@2535
   673
         SDL_memset(condition, 0, sizeof(FFCONDITION));
bobbens@2530
   674
bobbens@2530
   675
         /* Specifics */
bobbens@2530
   676
         for (i=0; i<dest->cAxes; i++) {
bobbens@2531
   677
            condition[i].lOffset = CONVERT(hap_condition->center[i]);
bobbens@2531
   678
            condition[i].lPositiveCoefficient = CONVERT(hap_condition->right_coeff[i]);
bobbens@2531
   679
            condition[i].lNegativeCoefficient = CONVERT(hap_condition->left_coeff[i]);
bobbens@2531
   680
            condition[i].dwPositiveSaturation = CONVERT(hap_condition->right_sat[i]);
bobbens@2531
   681
            condition[i].dwNegativeSaturation = CONVERT(hap_condition->left_sat[i]);
bobbens@2531
   682
            condition[i].lDeadBand = CONVERT(hap_condition->deadband[i]);
bobbens@2530
   683
         }
bobbens@2530
   684
         dest->cbTypeSpecificParams = sizeof(FFCONDITION) * dest->cAxes;
bobbens@2530
   685
         dest->lpvTypeSpecificParams = condition;
bobbens@2530
   686
bobbens@2530
   687
         /* Generics */
bobbens@2531
   688
         dest->dwDuration = hap_condition->length * 1000; /* In microseconds. */
bobbens@2531
   689
         dest->dwTriggerButton = FFJOFS_BUTTON(hap_condition->button);
bobbens@2531
   690
         dest->dwTriggerRepeatInterval = hap_condition->interval;
bobbens@2531
   691
         dest->dwStartDelay = hap_condition->delay * 1000; /* In microseconds. */
bobbens@2530
   692
bobbens@2530
   693
         /* Direction. */
bobbens@2531
   694
         if (SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes) < 0) {
bobbens@2530
   695
            return -1;                
bobbens@2530
   696
         }                            
bobbens@2530
   697
                                      
bobbens@2532
   698
         /* Envelope */
bobbens@2532
   699
/* TODO Check is envelope actually used.
bobbens@2531
   700
         envelope->dwAttackLevel = CONVERT(hap_condition->attack_level);
bobbens@2531
   701
         envelope->dwAttackTime = hap_condition->attack_length * 1000;
bobbens@2531
   702
         envelope->dwFadeLevel = CONVERT(hap_condition->fade_level);
bobbens@2531
   703
         envelope->dwFadeTime = hap_condition->fade_length * 1000;
bobbens@2532
   704
*/
bobbens@2520
   705
bobbens@2520
   706
         break;
bobbens@2520
   707
bobbens@2520
   708
      case SDL_HAPTIC_RAMP:
bobbens@2522
   709
         hap_ramp = &src->ramp;
bobbens@2533
   710
         ramp = SDL_malloc(sizeof(FFRAMPFORCE));
bobbens@2533
   711
         if (ramp == NULL) {
bobbens@2533
   712
            SDL_OutOfMemory();
bobbens@2533
   713
            return -1;
bobbens@2533
   714
         }
bobbens@2535
   715
         SDL_memset(ramp, 0, sizeof(FFRAMPFORCE));
bobbens@2533
   716
bobbens@2533
   717
         /* Specifics */
bobbens@2534
   718
         ramp->lStart = CONVERT(hap_ramp->start);
bobbens@2533
   719
         ramp->lEnd = CONVERT(hap_ramp->end);
bobbens@2535
   720
         dest->cbTypeSpecificParams = sizeof(FFRAMPFORCE);
bobbens@2535
   721
         dest->lpvTypeSpecificParams = ramp;
bobbens@2533
   722
bobbens@2533
   723
         /* Generics */
bobbens@2533
   724
         dest->dwDuration = hap_ramp->length * 1000; /* In microseconds. */
bobbens@2533
   725
         dest->dwTriggerButton = FFJOFS_BUTTON(hap_ramp->button);
bobbens@2533
   726
         dest->dwTriggerRepeatInterval = hap_ramp->interval;
bobbens@2533
   727
         dest->dwStartDelay = hap_ramp->delay * 1000; /* In microseconds. */
bobbens@2533
   728
bobbens@2533
   729
         /* Direction. */
bobbens@2533
   730
         if (SDL_SYS_SetDirection(dest, &hap_ramp->direction, dest->cAxes) < 0) {
bobbens@2533
   731
            return -1;
bobbens@2533
   732
         }
bobbens@2533
   733
bobbens@2533
   734
         /* Envelope */
bobbens@2533
   735
         envelope->dwAttackLevel = CONVERT(hap_ramp->attack_level);
bobbens@2533
   736
         envelope->dwAttackTime = hap_ramp->attack_length * 1000;
bobbens@2533
   737
         envelope->dwFadeLevel = CONVERT(hap_ramp->fade_level);
bobbens@2533
   738
         envelope->dwFadeTime = hap_ramp->fade_length * 1000;
bobbens@2520
   739
bobbens@2520
   740
         break;
bobbens@2520
   741
bobbens@2535
   742
      case SDL_HAPTIC_CUSTOM:
bobbens@2535
   743
         hap_custom = &src->custom;
bobbens@2535
   744
         custom = SDL_malloc(sizeof(FFCUSTOMFORCE));
bobbens@2535
   745
         if (custom == NULL) {
bobbens@2535
   746
            SDL_OutOfMemory();
bobbens@2535
   747
            return -1;
bobbens@2535
   748
         }
bobbens@2535
   749
         SDL_memset(custom, 0, sizeof(FFCUSTOMFORCE));
bobbens@2535
   750
bobbens@2535
   751
         /* Specifics */
bobbens@2535
   752
         custom->cChannels = hap_custom->channels;
bobbens@2535
   753
         custom->dwSamplePeriod = hap_custom->period * 1000;
bobbens@2535
   754
         custom->cSamples = hap_custom->samples;
bobbens@2535
   755
         custom->rglForceData = SDL_malloc(sizeof(LONG)*custom->cSamples*custom->cChannels);
bobbens@2535
   756
         for (i=0; i<hap_custom->samples*hap_custom->channels; i++) { /* Copy data. */
bobbens@2535
   757
            custom->rglForceData[i] = CONVERT(hap_custom->data[i]);
bobbens@2535
   758
         }
bobbens@2535
   759
         dest->cbTypeSpecificParams = sizeof(FFCUSTOMFORCE);
bobbens@2535
   760
         dest->lpvTypeSpecificParams = custom;
bobbens@2535
   761
bobbens@2535
   762
         /* Generics */
bobbens@2535
   763
         dest->dwDuration = hap_custom->length * 1000; /* In microseconds. */
bobbens@2535
   764
         dest->dwTriggerButton = FFJOFS_BUTTON(hap_custom->button);
bobbens@2535
   765
         dest->dwTriggerRepeatInterval = hap_custom->interval;
bobbens@2535
   766
         dest->dwStartDelay = hap_custom->delay * 1000; /* In microseconds. */
bobbens@2535
   767
bobbens@2535
   768
         /* Direction. */
bobbens@2535
   769
         if (SDL_SYS_SetDirection(dest, &hap_custom->direction, dest->cAxes) < 0) {
bobbens@2535
   770
            return -1;
bobbens@2535
   771
         }
bobbens@2535
   772
         
bobbens@2535
   773
         /* Envelope */
bobbens@2535
   774
         envelope->dwAttackLevel = CONVERT(hap_custom->attack_level);
bobbens@2535
   775
         envelope->dwAttackTime = hap_custom->attack_length * 1000;
bobbens@2535
   776
         envelope->dwFadeLevel = CONVERT(hap_custom->fade_level);
bobbens@2535
   777
         envelope->dwFadeTime = hap_custom->fade_length * 1000;
bobbens@2535
   778
bobbens@2535
   779
         break;
bobbens@2535
   780
bobbens@2520
   781
bobbens@2520
   782
      default:
bobbens@2520
   783
         SDL_SetError("Haptic: Unknown effect type.");
bobbens@2520
   784
         return -1;
bobbens@2520
   785
   }
bobbens@2520
   786
bobbens@2520
   787
   return 0;
bobbens@2520
   788
}
bobbens@2520
   789
bobbens@2520
   790
bobbens@2520
   791
/*
bobbens@2527
   792
 * Frees an FFEFFECT allocated by SDL_SYS_ToFFEFFECT.
bobbens@2527
   793
 */
bobbens@2531
   794
static void
bobbens@2535
   795
SDL_SYS_HapticFreeFFEFFECT( FFEFFECT * effect, int type )
bobbens@2527
   796
{
bobbens@2535
   797
   FFCUSTOMFORCE *custom;
bobbens@2535
   798
bobbens@2527
   799
   if (effect->lpEnvelope != NULL) {
bobbens@2527
   800
      SDL_free(effect->lpEnvelope);
bobbens@2527
   801
      effect->lpEnvelope = NULL;
bobbens@2527
   802
   }
bobbens@2527
   803
   if (effect->rgdwAxes != NULL) {
bobbens@2527
   804
      SDL_free(effect->rgdwAxes);
bobbens@2527
   805
      effect->rgdwAxes = NULL;
bobbens@2527
   806
   }
bobbens@2527
   807
   if (effect->lpvTypeSpecificParams != NULL) {
bobbens@2535
   808
      if (type == SDL_HAPTIC_CUSTOM) { /* Must free the custom data. */
bobbens@2535
   809
         custom = (FFCUSTOMFORCE*) effect->lpvTypeSpecificParams;
bobbens@2535
   810
         SDL_free(custom->rglForceData);
bobbens@2535
   811
         custom->rglForceData = NULL;
bobbens@2535
   812
      }
bobbens@2527
   813
      SDL_free(effect->lpvTypeSpecificParams);
bobbens@2527
   814
      effect->lpvTypeSpecificParams = NULL;
bobbens@2527
   815
   }
bobbens@2527
   816
   if (effect->rglDirection != NULL) {
bobbens@2527
   817
      SDL_free(effect->rglDirection);
bobbens@2527
   818
      effect->rglDirection = NULL;
bobbens@2527
   819
   }
bobbens@2527
   820
}
bobbens@2527
   821
bobbens@2527
   822
bobbens@2527
   823
/*
bobbens@2520
   824
 * Gets the effect type from the generic SDL haptic effect wrapper.
bobbens@2520
   825
 */
bobbens@2531
   826
CFUUIDRef
bobbens@2531
   827
SDL_SYS_HapticEffectType(struct haptic_effect * effect)
bobbens@2520
   828
{
bobbens@2521
   829
   switch (effect->effect.type) {
bobbens@2520
   830
      case SDL_HAPTIC_CONSTANT:
bobbens@2520
   831
         return kFFEffectType_ConstantForce_ID;
bobbens@2520
   832
bobbens@2520
   833
      case SDL_HAPTIC_RAMP:
bobbens@2520
   834
         return kFFEffectType_RampForce_ID;
bobbens@2520
   835
bobbens@2520
   836
      case SDL_HAPTIC_SQUARE:
bobbens@2520
   837
         return kFFEffectType_Square_ID;
bobbens@2520
   838
bobbens@2520
   839
      case SDL_HAPTIC_SINE:
bobbens@2520
   840
         return kFFEffectType_Sine_ID;
bobbens@2520
   841
bobbens@2521
   842
      case SDL_HAPTIC_TRIANGLE:
bobbens@2520
   843
         return kFFEffectType_Triangle_ID;
bobbens@2520
   844
bobbens@2520
   845
      case SDL_HAPTIC_SAWTOOTHUP:
bobbens@2520
   846
         return kFFEffectType_SawtoothUp_ID;
bobbens@2520
   847
bobbens@2520
   848
      case SDL_HAPTIC_SAWTOOTHDOWN:
bobbens@2520
   849
         return kFFEffectType_SawtoothDown_ID;
bobbens@2520
   850
bobbens@2520
   851
      case SDL_HAPTIC_SPRING:
bobbens@2520
   852
         return kFFEffectType_Spring_ID;
bobbens@2520
   853
bobbens@2520
   854
      case SDL_HAPTIC_DAMPER:
bobbens@2520
   855
         return kFFEffectType_Damper_ID;
bobbens@2520
   856
bobbens@2520
   857
      case SDL_HAPTIC_INERTIA:
bobbens@2520
   858
         return kFFEffectType_Inertia_ID;
bobbens@2520
   859
bobbens@2520
   860
      case SDL_HAPTIC_FRICTION:
bobbens@2520
   861
         return kFFEffectType_Friction_ID;
bobbens@2520
   862
bobbens@2520
   863
      case SDL_HAPTIC_CUSTOM:
bobbens@2520
   864
         return kFFEffectType_CustomForce_ID;
bobbens@2520
   865
bobbens@2520
   866
      default:
bobbens@2520
   867
         SDL_SetError("Haptic: Unknown effect type.");
bobbens@2520
   868
         return NULL;
bobbens@2520
   869
   }
bobbens@2520
   870
}
bobbens@2520
   871
bobbens@2520
   872
bobbens@2520
   873
/*
bobbens@2520
   874
 * Creates a new haptic effect.
bobbens@2520
   875
 */
bobbens@2520
   876
int
bobbens@2520
   877
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
bobbens@2520
   878
      SDL_HapticEffect * base)
bobbens@2520
   879
{
bobbens@2520
   880
   HRESULT ret;
bobbens@2520
   881
   CFUUIDRef type;
bobbens@2520
   882
bobbens@2520
   883
   /* Alloc the effect. */
bobbens@2520
   884
   effect->hweffect = (struct haptic_hweffect *)
bobbens@2520
   885
         SDL_malloc(sizeof(struct haptic_hweffect));
bobbens@2520
   886
   if (effect->hweffect == NULL) {
bobbens@2520
   887
      SDL_OutOfMemory();
bobbens@2527
   888
      goto err_hweffect;
bobbens@2520
   889
   }
bobbens@2520
   890
bobbens@2520
   891
   /* Get the type. */
bobbens@2520
   892
   type = SDL_SYS_HapticEffectType(effect);
bobbens@2520
   893
   if (type == NULL) {
bobbens@2527
   894
      goto err_hweffect;
bobbens@2520
   895
   }
bobbens@2520
   896
bobbens@2520
   897
   /* Get the effect. */
bobbens@2538
   898
   if (SDL_SYS_ToFFEFFECT(haptic, &effect->hweffect->effect, base) < 0) {
bobbens@2527
   899
      goto err_effectdone;
bobbens@2520
   900
   }
bobbens@2520
   901
bobbens@2527
   902
   /* Create the actual effect. */
bobbens@2538
   903
   ret = FFDeviceCreateEffect(haptic->hwdata->device, type,
bobbens@2538
   904
         &effect->hweffect->effect, &effect->hweffect->ref);
bobbens@2527
   905
   if (ret != FF_OK) {
bobbens@2541
   906
      SDL_SetError("Haptic: Unable to create effect: %s.", FFStrError(ret));
bobbens@2527
   907
      goto err_effectdone;
bobbens@2527
   908
   }
bobbens@2527
   909
bobbens@2527
   910
   return 0;
bobbens@2527
   911
bobbens@2527
   912
err_effectdone:
bobbens@2538
   913
   SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, base->type);
bobbens@2527
   914
err_hweffect:
bobbens@2527
   915
   if (effect->hweffect != NULL) {
bobbens@2527
   916
      SDL_free(effect->hweffect);
bobbens@2527
   917
      effect->hweffect = NULL;
bobbens@2527
   918
   }
bobbens@2527
   919
   return -1;
bobbens@2520
   920
}
bobbens@2520
   921
bobbens@2520
   922
bobbens@2520
   923
/*
bobbens@2520
   924
 * Updates an effect.
bobbens@2520
   925
 */
bobbens@2531
   926
int
bobbens@2531
   927
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
bobbens@2520
   928
      struct haptic_effect * effect, SDL_HapticEffect * data)
bobbens@2520
   929
{
bobbens@2538
   930
   HRESULT ret;
bobbens@2538
   931
   FFEffectParameterFlag flags;
bobbens@2538
   932
   FFEFFECT temp;
bobbens@2538
   933
bobbens@2538
   934
   /* Get the effect. */
bobbens@2538
   935
   SDL_memset(&temp, 0, sizeof(FFEFFECT));
bobbens@2538
   936
   if (SDL_SYS_ToFFEFFECT(haptic, &temp, data) < 0) {
bobbens@2538
   937
      goto err_update;
bobbens@2538
   938
   }
bobbens@2538
   939
bobbens@2538
   940
   /* Set the flags.  Might be worthwhile to diff temp with loaded effect and
bobbens@2538
   941
    *  only change those parameters. */
bobbens@2538
   942
   flags = FFEP_ALLPARAMS;
bobbens@2538
   943
bobbens@2538
   944
   /* Create the actual effect. */
bobbens@2542
   945
   ret = FFEffectSetParameters(effect->hweffect->ref, &temp, flags);
bobbens@2538
   946
   if (ret != FF_OK) {
bobbens@2541
   947
      SDL_SetError("Haptic: Unable to update effect: %s.", FFStrError(ret));
bobbens@2538
   948
      goto err_update;
bobbens@2538
   949
   }
bobbens@2538
   950
bobbens@2538
   951
   /* Copy it over. */
bobbens@2538
   952
   SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, data->type);
bobbens@2538
   953
   SDL_memcpy(&effect->hweffect->effect, &temp, sizeof(FFEFFECT));
bobbens@2538
   954
bobbens@2538
   955
   return 0;
bobbens@2538
   956
bobbens@2538
   957
err_update:
bobbens@2538
   958
   SDL_SYS_HapticFreeFFEFFECT(&temp, data->type);
bobbens@2520
   959
   return -1;
bobbens@2520
   960
}
bobbens@2520
   961
bobbens@2520
   962
bobbens@2520
   963
/*
bobbens@2520
   964
 * Runs an effect.
bobbens@2520
   965
 */
bobbens@2520
   966
int
bobbens@2520
   967
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
bobbens@2520
   968
                        Uint32 iterations)
bobbens@2520
   969
{
bobbens@2520
   970
   HRESULT ret;
bobbens@2520
   971
   Uint32 iter;
bobbens@2520
   972
bobbens@2520
   973
   /* Check if it's infinite. */
bobbens@2520
   974
   if (iterations == SDL_HAPTIC_INFINITY) {
bobbens@2521
   975
      iter = FF_INFINITE;
bobbens@2520
   976
   }
bobbens@2520
   977
   else
bobbens@2520
   978
      iter = iterations;
bobbens@2520
   979
bobbens@2520
   980
   /* Run the effect. */
bobbens@2520
   981
   ret = FFEffectStart(effect->hweffect->ref, iter, 0);
bobbens@2520
   982
   if (ret != FF_OK) {
bobbens@2541
   983
      SDL_SetError("Haptic: Unable to run the effect: %s.", FFStrError(ret));
bobbens@2520
   984
      return -1;
bobbens@2520
   985
   }
bobbens@2520
   986
bobbens@2520
   987
   return 0;
bobbens@2520
   988
}
bobbens@2520
   989
bobbens@2520
   990
bobbens@2520
   991
/*
bobbens@2520
   992
 * Stops an effect.
bobbens@2520
   993
 */
bobbens@2520
   994
int
bobbens@2520
   995
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2520
   996
{
bobbens@2520
   997
   HRESULT ret;
bobbens@2520
   998
bobbens@2520
   999
   ret = FFEffectStop(effect->hweffect->ref);
bobbens@2520
  1000
   if (ret != FF_OK) {
bobbens@2541
  1001
      SDL_SetError("Haptic: Unable to stop the effect: %s.", FFStrError(ret));
bobbens@2520
  1002
      return -1;
bobbens@2520
  1003
   }
bobbens@2520
  1004
bobbens@2520
  1005
   return 0;
bobbens@2520
  1006
}
bobbens@2520
  1007
bobbens@2520
  1008
bobbens@2520
  1009
/*
bobbens@2520
  1010
 * Frees the effect.
bobbens@2520
  1011
 */
bobbens@2520
  1012
void
bobbens@2520
  1013
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2520
  1014
{
bobbens@2520
  1015
   HRESULT ret;
bobbens@2520
  1016
bobbens@2520
  1017
   ret = FFDeviceReleaseEffect(haptic->hwdata->device, effect->hweffect->ref);
bobbens@2520
  1018
   if (ret != FF_OK) {
bobbens@2541
  1019
      SDL_SetError("Haptic: Error removing the effect from the device: %s.",
bobbens@2541
  1020
                   FFStrError(ret));
bobbens@2520
  1021
   }
bobbens@2520
  1022
   SDL_free(effect->hweffect->effect.lpvTypeSpecificParams);
bobbens@2520
  1023
   effect->hweffect->effect.lpvTypeSpecificParams = NULL;
bobbens@2520
  1024
   SDL_free(effect->hweffect);
bobbens@2520
  1025
   effect->hweffect = NULL;
bobbens@2520
  1026
}
bobbens@2520
  1027
bobbens@2520
  1028
bobbens@2520
  1029
/*
bobbens@2520
  1030
 * Gets the status of a haptic effect.
bobbens@2520
  1031
 */
bobbens@2520
  1032
int
bobbens@2520
  1033
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2520
  1034
{
bobbens@2520
  1035
   HRESULT ret;
bobbens@2520
  1036
   FFEffectStatusFlag status;
bobbens@2520
  1037
bobbens@2521
  1038
   ret = FFEffectGetEffectStatus(effect->hweffect->ref, &status);
bobbens@2520
  1039
   if (ret != FF_OK) {
bobbens@2541
  1040
      SDL_SetError("Haptic: Unable to get effect status: %s.", FFStrError(ret));
bobbens@2520
  1041
      return -1;
bobbens@2520
  1042
   }
bobbens@2520
  1043
bobbens@2520
  1044
   if (status == 0) return SDL_FALSE;
bobbens@2520
  1045
   return SDL_TRUE; /* Assume it's playing or emulated. */
bobbens@2520
  1046
}
bobbens@2520
  1047
bobbens@2520
  1048
bobbens@2520
  1049
/*
bobbens@2520
  1050
 * Sets the gain.
bobbens@2520
  1051
 */
bobbens@2520
  1052
int
bobbens@2520
  1053
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
bobbens@2520
  1054
{
bobbens@2520
  1055
   HRESULT ret;
bobbens@2520
  1056
   Uint32 val;
bobbens@2520
  1057
bobbens@2520
  1058
   val = gain * 100; /* Mac OS X uses 0 to 10,000 */
bobbens@2520
  1059
   ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device, FFPROP_FFGAIN, &val);
bobbens@2520
  1060
   if (ret != FF_OK) {
bobbens@2541
  1061
      SDL_SetError("Haptic: Error setting gain: %s.", FFStrError(ret));
bobbens@2520
  1062
      return -1;
bobbens@2520
  1063
   }
bobbens@2520
  1064
bobbens@2520
  1065
   return 0;
bobbens@2520
  1066
}
bobbens@2520
  1067
bobbens@2520
  1068
bobbens@2520
  1069
/*
bobbens@2520
  1070
 * Sets the autocentering.
bobbens@2520
  1071
 */
bobbens@2520
  1072
int
bobbens@2520
  1073
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
bobbens@2520
  1074
{
bobbens@2520
  1075
   HRESULT ret;
bobbens@2520
  1076
   Uint32 val;
bobbens@2520
  1077
bobbens@2520
  1078
   /* Mac OS X only has 0 (off) and 1 (on) */
bobbens@2520
  1079
   if (autocenter == 0) val = 0;
bobbens@2520
  1080
   else val = 1;
bobbens@2520
  1081
bobbens@2520
  1082
   ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
bobbens@2521
  1083
               FFPROP_AUTOCENTER, &val);
bobbens@2520
  1084
   if (ret != FF_OK) {
bobbens@2541
  1085
      SDL_SetError("Haptic: Error setting autocenter: %s.", FFStrError(ret));
bobbens@2520
  1086
      return -1;
bobbens@2520
  1087
   }
bobbens@2520
  1088
  
bobbens@2520
  1089
   return 0;
bobbens@2520
  1090
bobbens@2520
  1091
}
bobbens@2520
  1092
bobbens@2520
  1093
bobbens@2541
  1094
#endif /* SDL_HAPTIC_IOKIT */