src/haptic/darwin/SDL_syshaptic.c
author Edgar Simo <bobbens@gmail.com>
Tue, 15 Jul 2008 17:03:34 +0000
branchgsoc2008_force_feedback
changeset 2521 7aa91c21ce5f
parent 2520 6aee9eb4fc6d
child 2522 0877146be013
permissions -rw-r--r--
Many typo and silly mistake fixes.
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@2521
    32
#include <IOKit/IOTypes.h>
bobbens@2520
    33
#include <ForceFeedback/ForceFeedback.h>
bobbens@2520
    34
#include <ForceFeedback/ForceFeedbackConstants.h>
bobbens@2520
    35
bobbens@2520
    36
bobbens@2520
    37
#define MAX_HAPTICS  32
bobbens@2520
    38
bobbens@2520
    39
bobbens@2520
    40
/*
bobbens@2520
    41
 * List of available haptic devices.
bobbens@2520
    42
 */
bobbens@2520
    43
static struct
bobbens@2520
    44
{
bobbens@2520
    45
   io_service_t dev;
bobbens@2520
    46
   SDL_Haptic *haptic;
bobbens@2520
    47
} SDL_hapticlist[MAX_HAPTICS];
bobbens@2520
    48
bobbens@2520
    49
bobbens@2520
    50
/*
bobbens@2520
    51
 * Haptic system hardware data.
bobbens@2520
    52
 */
bobbens@2520
    53
struct haptic_hwdata
bobbens@2520
    54
{
bobbens@2520
    55
   FFDeviceObjectReference device; /* Hardware device. */
bobbens@2520
    56
};
bobbens@2520
    57
bobbens@2520
    58
bobbens@2520
    59
/*
bobbens@2520
    60
 * Haptic system effect data.
bobbens@2520
    61
 */
bobbens@2520
    62
struct haptic_hweffect
bobbens@2520
    63
{
bobbens@2520
    64
   FFEffectObjectReference ref; /* Reference. */
bobbens@2520
    65
   struct FFEFFECT effect; /* Hardware effect. */
bobbens@2520
    66
};
bobbens@2520
    67
bobbens@2520
    68
bobbens@2520
    69
/*
bobbens@2520
    70
 * Initializes the haptic subsystem.
bobbens@2520
    71
 */
bobbens@2520
    72
int
bobbens@2520
    73
SDL_SYS_HapticInit(void)
bobbens@2520
    74
{
bobbens@2520
    75
   int numhaptics;
bobbens@2520
    76
   IOReturn result;
bobbens@2520
    77
   io_iterator_t iter;
bobbens@2520
    78
   CFDictionaryRef match;
bobbens@2521
    79
   io_service_t device;
bobbens@2520
    80
bobbens@2520
    81
   /* Get HID devices. */
bobbens@2520
    82
   match = IOServiceMatching(kIOHIDDeviceKey);
bobbens@2520
    83
   if (match == NULL) {
bobbens@2520
    84
      SDL_SetError("Haptic: Failed to get IOServiceMatching.");
bobbens@2520
    85
      return -1;
bobbens@2520
    86
   }
bobbens@2520
    87
bobbens@2520
    88
   /* Now search I/O Registry for matching devices. */
bobbens@2520
    89
   result = IOServiceGetMatchingServices(kIOMasterPortDefault, match, &iter);
bobbens@2520
    90
   if (result != kIOReturnSuccess) {
bobbens@2520
    91
      SDL_SetError("Haptic: Couldn't create a HID object iterator.");
bobbens@2520
    92
      return -1;
bobbens@2520
    93
   }
bobbens@2520
    94
   /* IOServiceGetMatchingServices consumes dictionary. */
bobbens@2520
    95
bobbens@2520
    96
   numhaptics = 0;
bobbens@2520
    97
   while ((device = IOIteratorNext(iter)) != IO_OBJECT_NULL) {
bobbens@2520
    98
bobbens@2520
    99
      /* Check for force feedback. */
bobbens@2520
   100
      if (FFIsForceFeedback(device) == FF_OK) {
bobbens@2520
   101
         SDL_hapticlist[numhaptics].dev = device;
bobbens@2520
   102
         SDL_hapticlist[numhaptics].haptic = NULL;
bobbens@2520
   103
         numhaptics++;
bobbens@2520
   104
      }
bobbens@2520
   105
bobbens@2520
   106
      /* Reached haptic limit. */
bobbens@2520
   107
      if (numhaptics >= MAX_HAPTICS)
bobbens@2520
   108
         break;
bobbens@2520
   109
   }
bobbens@2520
   110
   IOObjectRelease(iter);
bobbens@2520
   111
bobbens@2520
   112
   return numhaptics;
bobbens@2520
   113
}
bobbens@2520
   114
bobbens@2520
   115
bobbens@2520
   116
/*
bobbens@2520
   117
 * Return the name of a haptic device, does not need to be opened.
bobbens@2520
   118
 */
bobbens@2520
   119
const char *
bobbens@2520
   120
SDL_SYS_HapticName(int index)
bobbens@2520
   121
{
bobbens@2520
   122
   return NULL;
bobbens@2520
   123
}
bobbens@2520
   124
bobbens@2520
   125
bobbens@2520
   126
#define FF_TEST(ff, s) \
bobbens@2520
   127
if (features.supportedEffects & ff) supported |= s
bobbens@2520
   128
/*
bobbens@2520
   129
 * Gets supported features.
bobbens@2520
   130
 */
bobbens@2520
   131
static unsigned int
bobbens@2520
   132
GetSupportedFeatures(FFDeviceObjectReference device,
bobbens@2520
   133
                     int *neffects, int *nplaying)
bobbens@2520
   134
{
bobbens@2520
   135
   HRESULT ret;
bobbens@2520
   136
   FFCAPABILITIES features;
bobbens@2520
   137
   unsigned int supported;
bobbens@2520
   138
   Uint32 val;
bobbens@2520
   139
bobbens@2520
   140
   ret = FFDeviceGetForceFeedbackCapabilities(device, &features);
bobbens@2520
   141
   if (ret != FF_OK) {
bobbens@2520
   142
      SDL_SetError("Haptic: Unable to get device's supported features.");
bobbens@2520
   143
      return 0;
bobbens@2520
   144
   }
bobbens@2520
   145
bobbens@2520
   146
   supported = 0;
bobbens@2520
   147
bobbens@2520
   148
   /* Get maximum effects. */
bobbens@2520
   149
   *neffects = features.storageCapacity;
bobbens@2520
   150
   *nplaying = features.playbackCapacity;
bobbens@2520
   151
bobbens@2520
   152
   /* Test for effects. */
bobbens@2520
   153
   FF_TEST(FFCAP_ET_CONSTANTFORCE, SDL_HAPTIC_CONSTANT);
bobbens@2520
   154
   FF_TEST(FFCAP_ET_RAMPFORCE,     SDL_HAPTIC_RAMP);
bobbens@2520
   155
   FF_TEST(FFCAP_ET_SQUARE,        SDL_HAPTIC_SQUARE);
bobbens@2520
   156
   FF_TEST(FFCAP_ET_SINE,          SDL_HAPTIC_SINE);
bobbens@2520
   157
   FF_TEST(FFCAP_ET_TRIANGLE,      SDL_HAPTIC_TRIANGLE);
bobbens@2520
   158
   FF_TEST(FFCAP_ET_SAWTOOTHUP,    SDL_HAPTIC_SAWTOOTHUP);
bobbens@2520
   159
   FF_TEST(FFCAP_ET_SAWTOOTHDOWN,  SDL_HAPTIC_SAWTOOTHDOWN);
bobbens@2520
   160
   FF_TEST(FFCAP_ET_SPRING,        SDL_HAPTIC_SPRING);
bobbens@2520
   161
   FF_TEST(FFCAP_ET_DAMPER,        SDL_HAPTIC_DAMPER);
bobbens@2520
   162
   FF_TEST(FFCAP_ET_INERTIA,       SDL_HAPTIC_INERTIA);
bobbens@2520
   163
   FF_TEST(FFCAP_ET_FRICTION,      SDL_HAPTIC_FRICTION);
bobbens@2520
   164
   FF_TEST(FFCAP_ET_CUSTOMFORCE,   SDL_HAPTIC_CUSTOM);
bobbens@2520
   165
bobbens@2520
   166
   /* Check if supports gain. */
bobbens@2520
   167
   ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_FFGAIN,
bobbens@2520
   168
                                          val, sizeof(val));
bobbens@2520
   169
   if (ret == FF_OK) supported |= SDL_HAPTIC_GAIN;
bobbens@2520
   170
   else if (ret != FFERR_UNSUPPORTED) {
bobbens@2520
   171
      SDL_SetError("Haptic: Unable to get if device supports gain.");
bobbens@2520
   172
      return 0;
bobbens@2520
   173
   }
bobbens@2520
   174
bobbens@2520
   175
   /* Checks if supports autocenter. */
bobbens@2521
   176
   ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_AUTOCENTER,
bobbens@2520
   177
                                          val, sizeof(val));
bobbens@2520
   178
   if (ret == FF_OK) supported |= SDL_HAPTIC_AUTOCENTER;
bobbens@2520
   179
   else if (ret != FFERR_UNSUPPORTED) {
bobbens@2520
   180
      SDL_SetError("Haptic: Unable to get if device supports autocenter.");
bobbens@2520
   181
      return 0;
bobbens@2520
   182
   }
bobbens@2520
   183
bobbens@2520
   184
   /* Always supported features. */
bobbens@2520
   185
   supported |= SDL_HAPTIC_STATUS;
bobbens@2520
   186
   return supported;
bobbens@2520
   187
}
bobbens@2520
   188
bobbens@2520
   189
bobbens@2520
   190
/*
bobbens@2520
   191
 * Opens the haptic device from the file descriptor.
bobbens@2520
   192
 */
bobbens@2520
   193
static int
bobbens@2520
   194
SDL_SYS_HapticOpenFromService(SDL_Haptic * haptic, io_service_t service)
bobbens@2520
   195
{
bobbens@2520
   196
   /* Allocate the hwdata */
bobbens@2520
   197
   haptic->hwdata = (struct haptic_hwdata *)
bobbens@2520
   198
         SDL_malloc(sizeof(*haptic->hwdata));
bobbens@2520
   199
   if (haptic->hwdata == NULL) {
bobbens@2520
   200
      SDL_OutOfMemory();
bobbens@2520
   201
      goto creat_err;
bobbens@2520
   202
   }
bobbens@2520
   203
   SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
bobbens@2520
   204
  
bobbens@2520
   205
   /* Open the device */
bobbens@2520
   206
   if (FFCreateDevice( &service, &haptic->hwdata->device ) != FF_OK) {
bobbens@2520
   207
      SDL_SetError("Haptic: Unable to create device from service.");
bobbens@2520
   208
      goto creat_err;
bobbens@2520
   209
   }
bobbens@2520
   210
bobbens@2520
   211
   /* Get supported features. */
bobbens@2520
   212
   haptic->supported = GetSupportedFeatures(haptic->hwdata->device,
bobbens@2520
   213
                                            &haptic->neffects, &haptic->nplaying);
bobbens@2520
   214
   if (haptic->supported == 0) { /* Error since device supports nothing. */
bobbens@2520
   215
      goto open_err;
bobbens@2520
   216
   }
bobbens@2520
   217
   haptic->effects = (struct haptic_effect *)
bobbens@2520
   218
         SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
bobbens@2520
   219
   if (haptic->effects == NULL) {
bobbens@2520
   220
      SDL_OutOfMemory();
bobbens@2520
   221
      goto open_err;
bobbens@2520
   222
   }
bobbens@2520
   223
   /* Clear the memory */
bobbens@2520
   224
   SDL_memset(haptic->effects, 0,
bobbens@2520
   225
         sizeof(struct haptic_effect) * haptic->neffects);
bobbens@2520
   226
   
bobbens@2520
   227
   return 0;
bobbens@2520
   228
   
bobbens@2520
   229
   /* Error handling */
bobbens@2520
   230
open_err:
bobbens@2520
   231
   FFReleaseDevice(haptic->hwdata->device);
bobbens@2520
   232
creat_err:
bobbens@2520
   233
   if (haptic->hwdata != NULL) {
bobbens@2520
   234
      free(haptic->hwdata);
bobbens@2520
   235
      haptic->hwdata = NULL;                                              
bobbens@2520
   236
   }
bobbens@2520
   237
   return -1;
bobbens@2520
   238
bobbens@2520
   239
}
bobbens@2520
   240
bobbens@2520
   241
bobbens@2520
   242
/*
bobbens@2520
   243
 * Opens a haptic device for usage.
bobbens@2520
   244
 */
bobbens@2520
   245
int
bobbens@2520
   246
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
bobbens@2520
   247
{
bobbens@2520
   248
   return SDL_SYS_HapticOpenFromService(haptic,
bobbens@2520
   249
                SDL_hapticlist[haptic->index].device);
bobbens@2520
   250
}
bobbens@2520
   251
bobbens@2520
   252
bobbens@2520
   253
/*
bobbens@2520
   254
 * Opens a haptic device from first mouse it finds for usage.
bobbens@2520
   255
 */
bobbens@2520
   256
int
bobbens@2520
   257
SDL_SYS_HapticMouse(void)
bobbens@2520
   258
{
bobbens@2520
   259
   return -1;
bobbens@2520
   260
}
bobbens@2520
   261
bobbens@2520
   262
bobbens@2520
   263
/*
bobbens@2520
   264
 * Checks to see if a joystick has haptic features.
bobbens@2520
   265
 */
bobbens@2520
   266
int
bobbens@2520
   267
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
bobbens@2520
   268
{
bobbens@2520
   269
   return SDL_FALSE;
bobbens@2520
   270
}
bobbens@2520
   271
bobbens@2520
   272
bobbens@2520
   273
/*
bobbens@2520
   274
 * Checks to see if the haptic device and joystick and in reality the same.
bobbens@2520
   275
 */
bobbens@2520
   276
int
bobbens@2520
   277
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
bobbens@2520
   278
{
bobbens@2520
   279
   if (SDL_strcmp(joystick->name,haptic->name)==0) {
bobbens@2520
   280
      return 1;
bobbens@2520
   281
   }
bobbens@2520
   282
   return 0;
bobbens@2520
   283
}
bobbens@2520
   284
bobbens@2520
   285
bobbens@2520
   286
/*
bobbens@2520
   287
 * Opens a SDL_Haptic from a SDL_Joystick.
bobbens@2520
   288
 */
bobbens@2520
   289
int
bobbens@2520
   290
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
bobbens@2520
   291
{
bobbens@2520
   292
   return -1;
bobbens@2520
   293
}
bobbens@2520
   294
bobbens@2520
   295
bobbens@2520
   296
/*
bobbens@2520
   297
 * Closes the haptic device.
bobbens@2520
   298
 */
bobbens@2520
   299
void
bobbens@2520
   300
SDL_SYS_HapticClose(SDL_Haptic * haptic)
bobbens@2520
   301
{
bobbens@2520
   302
   int i;
bobbens@2520
   303
bobbens@2520
   304
   if (haptic->hwdata) {
bobbens@2520
   305
bobbens@2520
   306
      /* Clean up */
bobbens@2520
   307
      FFReleaseDevice(haptic->hwdata->device);
bobbens@2520
   308
bobbens@2520
   309
      /* Free */
bobbens@2520
   310
      SDL_free(haptic->hwdata);
bobbens@2520
   311
      haptic->hwdata = NULL;
bobbens@2520
   312
      for (i=0; i<haptic->neffects; i++) {
bobbens@2520
   313
         if (haptic->effects[i].hweffect != NULL)
bobbens@2520
   314
            SDL_free(haptic->effects[i].hweffect->effect.lpvTypeSpecificParams);
bobbens@2520
   315
      }
bobbens@2520
   316
      SDL_free(haptic->effects);
bobbens@2520
   317
      haptic->neffects = 0;
bobbens@2520
   318
   }
bobbens@2520
   319
}
bobbens@2520
   320
bobbens@2520
   321
bobbens@2520
   322
/* 
bobbens@2520
   323
 * Clean up after system specific haptic stuff
bobbens@2520
   324
 */
bobbens@2520
   325
void
bobbens@2520
   326
SDL_SYS_HapticQuit(void)
bobbens@2520
   327
{
bobbens@2520
   328
   int i;
bobbens@2520
   329
bobbens@2520
   330
   for (i=0; i < SDL_numhaptics; i++) {
bobbens@2520
   331
      IOObjectRelease(SDL_hapticlist[i].dev);
bobbens@2520
   332
      /* TODO free effects. */
bobbens@2520
   333
   }
bobbens@2520
   334
}
bobbens@2520
   335
bobbens@2520
   336
bobbens@2520
   337
/*
bobbens@2520
   338
 * Sets the direction.
bobbens@2520
   339
 */
bobbens@2520
   340
static int
bobbens@2520
   341
SDL_SYS_SetDirection( FFEFFECT * effect, SDL_HapticDirection *dir, int axes )
bobbens@2520
   342
{
bobbens@2521
   343
   LONG *rglDir;
bobbens@2520
   344
   dir = SDL_malloc( sizeof(LONG) * axes );
bobbens@2520
   345
   if (dir == NULL) {
bobbens@2520
   346
      SDL_OutOfMemory();
bobbens@2520
   347
      return -1;
bobbens@2520
   348
   }
bobbens@2520
   349
   SDL_memset( dir, 0, sizeof(LONG) * axes );
bobbens@2521
   350
   effect->rglDirection = rglDir;
bobbens@2520
   351
bobbens@2520
   352
   switch (dir->type) {
bobbens@2520
   353
      case SDL_HAPTIC_POLAR:
bobbens@2520
   354
         effect->dwFlags |= FFEFF_POLAR;
bobbens@2521
   355
         rglDir[0] = dir->dir[0];
bobbens@2520
   356
         return 0;
bobbens@2520
   357
      case SDL_HAPTIC_CARTESIAN:
bobbens@2520
   358
         effects->dwFlags |= FFEFF_CARTESIAN;
bobbens@2521
   359
         rglDir[0] = dir->dir[0];
bobbens@2521
   360
         rglDir[1] = dir->dir[1];
bobbens@2521
   361
         rglDir[2] = dir->dir[2];
bobbens@2520
   362
         return 0;
bobbens@2521
   363
      case SDL_HAPTIC_SPHERICAL:
bobbens@2520
   364
         effects->dwFlags |= FFEFF_SPHERICAL;
bobbens@2521
   365
         rglDir[0] = dir->dir[0];
bobbens@2521
   366
         rglDir[1] = dir->dir[1];
bobbens@2521
   367
         rglDir[2] = dir->dir[2];
bobbens@2520
   368
         return 0;
bobbens@2520
   369
bobbens@2520
   370
      default:
bobbens@2520
   371
         SDL_SetError("Haptic: Unknown direction type.");
bobbens@2520
   372
         return -1;
bobbens@2520
   373
   }
bobbens@2520
   374
}
bobbens@2520
   375
bobbens@2520
   376
#define CONVERT(x)   (((x)*10000) / 0xFFFF )
bobbens@2520
   377
/*
bobbens@2520
   378
 * Creates the FFStruct
bobbens@2520
   379
 */
bobbens@2520
   380
static int
bobbens@2520
   381
SDL_SYS_ToFFEFFECT( FFEFFECT * dest, SDL_HapticEffect * src )
bobbens@2520
   382
{
bobbens@2520
   383
   FFCONSTANTFORCE *constant;
bobbens@2520
   384
   FFPERIODIC *periodic;
bobbens@2520
   385
   FFCONDITION *condition;
bobbens@2520
   386
   FFRAMPFORCE *ramp;
bobbens@2520
   387
   FFCUSTOMFORCE *custom;
bobbens@2520
   388
   SDL_HapticConstant *hap_constant;
bobbens@2521
   389
   SDL_HapticPeriodic *hap_periodic;
bobbens@2520
   390
   SDL_HapticCondition *hap_condition;
bobbens@2520
   391
   SDL_HapticRamp *hap_ramp;
bobbens@2520
   392
bobbens@2520
   393
   /* Set global stuff. */
bobbens@2520
   394
   SDL_memset(dest, 0, sizeof(FFEFFECT));
bobbens@2520
   395
   dest->dwSize = sizeof(FFEFFECT); /* Set the structure size. */
bobbens@2520
   396
   dest->dwSamplePeriod = 0; /* Not used by us. */
bobbens@2520
   397
   dest->dwGain = 10000; /* Gain is set globally, not locally. */
bobbens@2520
   398
   dest->lpEnvelope.dwSize = sizeof(FFENVELOPE); /* Always should be this. */
bobbens@2520
   399
bobbens@2520
   400
   switch (src->type) {
bobbens@2520
   401
      case SDL_HAPTIC_CONSTANT:
bobbens@2520
   402
         hap_constant = &src->constant;
bobbens@2520
   403
         constant = SDL_malloc( sizeof(FFCONSTANTFORCE) );
bobbens@2520
   404
bobbens@2520
   405
         /* Specifics */
bobbens@2520
   406
         constant->lMagnitude = CONVERT(hap_constant->level);
bobbens@2520
   407
         dest->cbTypeSpecificParams = sizeof(FFCONSTANTFORCE); 
bobbens@2520
   408
         dest->lpvTypeSpecificParams = constant;
bobbens@2520
   409
bobbens@2520
   410
         /* Generics */
bobbens@2521
   411
         dest->dwDuration = hap_constant->length * 1000; /* In microseconds. */
bobbens@2520
   412
         dest->dwTriggerButton = FFJOFS_BUTTON(hap_constant->button);
bobbens@2520
   413
         dest->dwTriggerRepeatInterval = hap_constant->interval;
bobbens@2521
   414
         dest->dwStartDelay = hap_constant->delay * 1000; /* In microseconds. */
bobbens@2520
   415
bobbens@2520
   416
         /* Axes */
bobbens@2520
   417
         dest->cAxes = 2; /* TODO handle */
bobbens@2520
   418
         dest->rgdwAxes = 0;
bobbens@2520
   419
bobbens@2520
   420
         /* Direction. */
bobbens@2520
   421
         if (SDL_SYS_SetDirection(dest, hap_constant->direction, dest->cAxes) < 0) {
bobbens@2520
   422
            return -1;
bobbens@2520
   423
         }
bobbens@2520
   424
         
bobbens@2520
   425
         /* Envelope */
bobbens@2520
   426
         dest->lpEnvelope.dwAttackLevel = CONVERT(hap_constant->attack_level);
bobbens@2520
   427
         dest->lpEnvelope.dwAttackTime = hap_constant->attack_length * 1000;
bobbens@2520
   428
         dest->lpEnvelope.dwFadeLevel = CONVERT(hap_constant->fade_level);
bobbens@2520
   429
         dest->lpEnvelope.dwFadeTime = hap_constant->fade_length * 1000;
bobbens@2520
   430
bobbens@2520
   431
         break;
bobbens@2520
   432
bobbens@2520
   433
         /* TODO finish */
bobbens@2520
   434
bobbens@2520
   435
      case SDL_HAPTIC_SINE:
bobbens@2520
   436
      case SDL_HAPTIC_SQUARE:
bobbens@2520
   437
      case SDL_HAPTIC_TRIANGLE:
bobbens@2520
   438
      case SDL_HAPTIC_SAWTOOTHUP:
bobbens@2520
   439
      case SDL_HAPTIC_SAWTOOTHDOWN:
bobbens@2520
   440
         periodic = &src->periodic;
bobbens@2520
   441
bobbens@2520
   442
         break;
bobbens@2520
   443
bobbens@2520
   444
      case SDL_HAPTIC_SPRING:
bobbens@2520
   445
      case SDL_HAPTIC_DAMPER:
bobbens@2520
   446
      case SDL_HAPTIC_INERTIA:
bobbens@2520
   447
      case SDL_HAPTIC_FRICTION:
bobbens@2520
   448
         condition = &src->condition;
bobbens@2520
   449
bobbens@2520
   450
         break;
bobbens@2520
   451
bobbens@2520
   452
      case SDL_HAPTIC_RAMP:
bobbens@2520
   453
         ramp = &src->ramp;
bobbens@2520
   454
bobbens@2520
   455
         break;
bobbens@2520
   456
bobbens@2520
   457
bobbens@2520
   458
      default:
bobbens@2520
   459
         SDL_SetError("Haptic: Unknown effect type.");
bobbens@2520
   460
         return -1;
bobbens@2520
   461
   }
bobbens@2520
   462
bobbens@2520
   463
   return 0;
bobbens@2520
   464
}
bobbens@2520
   465
bobbens@2520
   466
bobbens@2520
   467
/*
bobbens@2520
   468
 * Gets the effect type from the generic SDL haptic effect wrapper.
bobbens@2520
   469
 */
bobbens@2520
   470
CFUUIDRef SDL_SYS_HapticEffectType(struct haptic_effect * effect)
bobbens@2520
   471
{
bobbens@2521
   472
   switch (effect->effect.type) {
bobbens@2520
   473
      case SDL_HAPTIC_CONSTANT:
bobbens@2520
   474
         return kFFEffectType_ConstantForce_ID;
bobbens@2520
   475
bobbens@2520
   476
      case SDL_HAPTIC_RAMP:
bobbens@2520
   477
         return kFFEffectType_RampForce_ID;
bobbens@2520
   478
bobbens@2520
   479
      case SDL_HAPTIC_SQUARE:
bobbens@2520
   480
         return kFFEffectType_Square_ID;
bobbens@2520
   481
bobbens@2520
   482
      case SDL_HAPTIC_SINE:
bobbens@2520
   483
         return kFFEffectType_Sine_ID;
bobbens@2520
   484
bobbens@2521
   485
      case SDL_HAPTIC_TRIANGLE:
bobbens@2520
   486
         return kFFEffectType_Triangle_ID;
bobbens@2520
   487
bobbens@2520
   488
      case SDL_HAPTIC_SAWTOOTHUP:
bobbens@2520
   489
         return kFFEffectType_SawtoothUp_ID;
bobbens@2520
   490
bobbens@2520
   491
      case SDL_HAPTIC_SAWTOOTHDOWN:
bobbens@2520
   492
         return kFFEffectType_SawtoothDown_ID;
bobbens@2520
   493
bobbens@2520
   494
      case SDL_HAPTIC_SPRING:
bobbens@2520
   495
         return kFFEffectType_Spring_ID;
bobbens@2520
   496
bobbens@2520
   497
      case SDL_HAPTIC_DAMPER:
bobbens@2520
   498
         return kFFEffectType_Damper_ID;
bobbens@2520
   499
bobbens@2520
   500
      case SDL_HAPTIC_INERTIA:
bobbens@2520
   501
         return kFFEffectType_Inertia_ID;
bobbens@2520
   502
bobbens@2520
   503
      case SDL_HAPTIC_FRICTION:
bobbens@2520
   504
         return kFFEffectType_Friction_ID;
bobbens@2520
   505
bobbens@2520
   506
      case SDL_HAPTIC_CUSTOM:
bobbens@2520
   507
         return kFFEffectType_CustomForce_ID;
bobbens@2520
   508
bobbens@2520
   509
      default:
bobbens@2520
   510
         SDL_SetError("Haptic: Unknown effect type.");
bobbens@2520
   511
         return NULL;
bobbens@2520
   512
   }
bobbens@2520
   513
}
bobbens@2520
   514
bobbens@2520
   515
bobbens@2520
   516
/*
bobbens@2520
   517
 * Creates a new haptic effect.
bobbens@2520
   518
 */
bobbens@2520
   519
int
bobbens@2520
   520
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
bobbens@2520
   521
      SDL_HapticEffect * base)
bobbens@2520
   522
{
bobbens@2520
   523
   HRESULT ret;
bobbens@2520
   524
   CFUUIDRef type;
bobbens@2520
   525
bobbens@2520
   526
   /* Alloc the effect. */
bobbens@2520
   527
   effect->hweffect = (struct haptic_hweffect *)
bobbens@2520
   528
         SDL_malloc(sizeof(struct haptic_hweffect));
bobbens@2520
   529
   if (effect->hweffect == NULL) {
bobbens@2520
   530
      SDL_OutOfMemory();
bobbens@2520
   531
      return -1;
bobbens@2520
   532
   }
bobbens@2520
   533
bobbens@2520
   534
   /* Get the type. */
bobbens@2520
   535
   type = SDL_SYS_HapticEffectType(effect);
bobbens@2520
   536
   if (type == NULL) {
bobbens@2520
   537
      SDL_free(effect->hweffect);
bobbens@2520
   538
      effect->hweffect = NULL;
bobbens@2520
   539
      return -1;
bobbens@2520
   540
   }
bobbens@2520
   541
bobbens@2520
   542
   /* Get the effect. */
bobbens@2521
   543
   if (SDL_SYS_ToFFEFFECT( &effect->hweffect->effect, &effect->effect ) < 0) {
bobbens@2520
   544
      /* TODO cleanup alloced stuff. */
bobbens@2520
   545
      return -1;
bobbens@2520
   546
   }
bobbens@2520
   547
bobbens@2520
   548
   ret = FFDeviceCreateEffect( haptic->hwdata->device, type,
bobbens@2520
   549
         &effect->hweffect->effect, &effect->hweffect->ref );
bobbens@2520
   550
}
bobbens@2520
   551
bobbens@2520
   552
bobbens@2520
   553
/*
bobbens@2520
   554
 * Updates an effect.
bobbens@2520
   555
 */
bobbens@2520
   556
int SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
bobbens@2520
   557
      struct haptic_effect * effect, SDL_HapticEffect * data)
bobbens@2520
   558
{
bobbens@2520
   559
   /* TODO */
bobbens@2520
   560
   return -1;
bobbens@2520
   561
}
bobbens@2520
   562
bobbens@2520
   563
bobbens@2520
   564
/*
bobbens@2520
   565
 * Runs an effect.
bobbens@2520
   566
 */
bobbens@2520
   567
int
bobbens@2520
   568
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
bobbens@2520
   569
                        Uint32 iterations)
bobbens@2520
   570
{
bobbens@2520
   571
   HRESULT ret;
bobbens@2520
   572
   Uint32 iter;
bobbens@2520
   573
bobbens@2520
   574
   /* Check if it's infinite. */
bobbens@2520
   575
   if (iterations == SDL_HAPTIC_INFINITY) {
bobbens@2521
   576
      iter = FF_INFINITE;
bobbens@2520
   577
   }
bobbens@2520
   578
   else
bobbens@2520
   579
      iter = iterations;
bobbens@2520
   580
bobbens@2520
   581
   /* Run the effect. */
bobbens@2520
   582
   ret = FFEffectStart(effect->hweffect->ref, iter, 0);
bobbens@2520
   583
   if (ret != FF_OK) {
bobbens@2520
   584
      SDL_SetError("Haptic: Unable to run the effect.");
bobbens@2520
   585
      return -1;
bobbens@2520
   586
   }
bobbens@2520
   587
bobbens@2520
   588
   return 0;
bobbens@2520
   589
}
bobbens@2520
   590
bobbens@2520
   591
bobbens@2520
   592
/*
bobbens@2520
   593
 * Stops an effect.
bobbens@2520
   594
 */
bobbens@2520
   595
int
bobbens@2520
   596
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2520
   597
{
bobbens@2520
   598
   HRESULT ret;
bobbens@2520
   599
bobbens@2520
   600
   ret = FFEffectStop(effect->hweffect->ref);
bobbens@2520
   601
   if (ret != FF_OK) {
bobbens@2520
   602
      SDL_SetError("Haptic: Unable to stop the effect.");
bobbens@2520
   603
      return -1;
bobbens@2520
   604
   }
bobbens@2520
   605
bobbens@2520
   606
   return 0;
bobbens@2520
   607
}
bobbens@2520
   608
bobbens@2520
   609
bobbens@2520
   610
/*
bobbens@2520
   611
 * Frees the effect.
bobbens@2520
   612
 */
bobbens@2520
   613
void
bobbens@2520
   614
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2520
   615
{
bobbens@2520
   616
   HRESULT ret;
bobbens@2520
   617
bobbens@2520
   618
   ret = FFDeviceReleaseEffect(haptic->hwdata->device, effect->hweffect->ref);
bobbens@2520
   619
   if (ret != FF_OK) {
bobbens@2520
   620
      SDL_SetError("Haptic: Error removing the effect from the device.");
bobbens@2520
   621
   }
bobbens@2520
   622
   SDL_free(effect->hweffect->effect.lpvTypeSpecificParams);
bobbens@2520
   623
   effect->hweffect->effect.lpvTypeSpecificParams = NULL;
bobbens@2520
   624
   SDL_free(effect->hweffect);
bobbens@2520
   625
   effect->hweffect = NULL;
bobbens@2520
   626
}
bobbens@2520
   627
bobbens@2520
   628
bobbens@2520
   629
/*
bobbens@2520
   630
 * Gets the status of a haptic effect.
bobbens@2520
   631
 */
bobbens@2520
   632
int
bobbens@2520
   633
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2520
   634
{
bobbens@2520
   635
   HRESULT ret;
bobbens@2520
   636
   FFEffectStatusFlag status;
bobbens@2520
   637
bobbens@2521
   638
   ret = FFEffectGetEffectStatus(effect->hweffect->ref, &status);
bobbens@2520
   639
   if (ret != FF_OK) {
bobbens@2520
   640
      SDL_SetError("Haptic: Unable to get effect status.");
bobbens@2520
   641
      return -1;
bobbens@2520
   642
   }
bobbens@2520
   643
bobbens@2520
   644
   if (status == 0) return SDL_FALSE;
bobbens@2520
   645
   return SDL_TRUE; /* Assume it's playing or emulated. */
bobbens@2520
   646
}
bobbens@2520
   647
bobbens@2520
   648
bobbens@2520
   649
/*
bobbens@2520
   650
 * Sets the gain.
bobbens@2520
   651
 */
bobbens@2520
   652
int
bobbens@2520
   653
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
bobbens@2520
   654
{
bobbens@2520
   655
   HRESULT ret;
bobbens@2520
   656
   Uint32 val;
bobbens@2520
   657
bobbens@2520
   658
   val = gain * 100; /* Mac OS X uses 0 to 10,000 */
bobbens@2520
   659
   ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device, FFPROP_FFGAIN, &val);
bobbens@2520
   660
   if (ret != FF_OK) {
bobbens@2520
   661
      SDL_SetError("Haptic: Error setting gain.");
bobbens@2520
   662
      return -1;
bobbens@2520
   663
   }
bobbens@2520
   664
bobbens@2520
   665
   return 0;
bobbens@2520
   666
}
bobbens@2520
   667
bobbens@2520
   668
bobbens@2520
   669
/*
bobbens@2520
   670
 * Sets the autocentering.
bobbens@2520
   671
 */
bobbens@2520
   672
int
bobbens@2520
   673
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
bobbens@2520
   674
{
bobbens@2520
   675
   HRESULT ret;
bobbens@2520
   676
   Uint32 val;
bobbens@2520
   677
bobbens@2520
   678
   /* Mac OS X only has 0 (off) and 1 (on) */
bobbens@2520
   679
   if (autocenter == 0) val = 0;
bobbens@2520
   680
   else val = 1;
bobbens@2520
   681
bobbens@2520
   682
   ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device,
bobbens@2521
   683
               FFPROP_AUTOCENTER, &val);
bobbens@2520
   684
   if (ret != FF_OK) {
bobbens@2520
   685
      SDL_SetError("Haptic: Error setting autocenter.");
bobbens@2520
   686
      return -1;
bobbens@2520
   687
   }
bobbens@2520
   688
  
bobbens@2520
   689
   return 0;
bobbens@2520
   690
bobbens@2520
   691
}
bobbens@2520
   692
bobbens@2520
   693
bobbens@2520
   694
#endif /* SDL_HAPTIC_LINUX */