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