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