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