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