src/haptic/SDL_haptic.c
author Edgar Simo <bobbens@gmail.com>
Wed, 02 Jul 2008 08:24:35 +0000
branchgsoc2008_force_feedback
changeset 2489 96adc8025331
parent 2488 8e2bdbccf7ff
child 2490 be9b206d44af
permissions -rw-r--r--
Exposed some of the joystick stuff to the haptic subsystem.
Added SDL_JoystickIsHaptic().
bobbens@2472
     1
/*
bobbens@2472
     2
    SDL - Simple DirectMedia Layer
bobbens@2472
     3
    Copyright (C) 2008 Edgar Simo
bobbens@2472
     4
bobbens@2472
     5
    This library is free software; you can redistribute it and/or
bobbens@2472
     6
    modify it under the terms of the GNU Lesser General Public
bobbens@2472
     7
    License as published by the Free Software Foundation; either
bobbens@2472
     8
    version 2.1 of the License, or (at your option) any later version.
bobbens@2472
     9
bobbens@2472
    10
    This library is distributed in the hope that it will be useful,
bobbens@2472
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
bobbens@2472
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bobbens@2472
    13
    Lesser General Public License for more details.
bobbens@2472
    14
bobbens@2472
    15
    You should have received a copy of the GNU Lesser General Public
bobbens@2472
    16
    License along with this library; if not, write to the Free Software
bobbens@2472
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bobbens@2472
    18
bobbens@2472
    19
    Sam Lantinga
bobbens@2472
    20
    slouken@libsdl.org
bobbens@2472
    21
*/
bobbens@2472
    22
#include "SDL_config.h"
bobbens@2472
    23
bobbens@2472
    24
#include "SDL_haptic_c.h"
bobbens@2472
    25
#include "SDL_syshaptic.h"
bobbens@2489
    26
#include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
bobbens@2472
    27
bobbens@2472
    28
bobbens@2472
    29
static Uint8 SDL_numhaptics = 0;
bobbens@2472
    30
SDL_Haptic **SDL_haptics = NULL;
bobbens@2472
    31
static SDL_Haptic *default_haptic = NULL;
bobbens@2472
    32
bobbens@2472
    33
bobbens@2472
    34
/*
bobbens@2472
    35
 * Initializes the Haptic devices.
bobbens@2472
    36
 */
bobbens@2472
    37
int
bobbens@2472
    38
SDL_HapticInit(void)
bobbens@2472
    39
{  
bobbens@2472
    40
   int arraylen;
bobbens@2472
    41
   int status;
bobbens@2472
    42
bobbens@2472
    43
   SDL_numhaptics = 0;
bobbens@2472
    44
   status = SDL_SYS_HapticInit();
bobbens@2472
    45
   if (status >= 0) {
bobbens@2472
    46
      arraylen = (status + 1) * sizeof(*SDL_haptics);
bobbens@2472
    47
      SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
bobbens@2472
    48
      if (SDL_haptics == NULL) {
bobbens@2472
    49
         SDL_numhaptics = 0;
bobbens@2472
    50
      } else {
bobbens@2472
    51
         SDL_memset(SDL_haptics, 0, arraylen);
bobbens@2472
    52
         SDL_numhaptics = status;
bobbens@2472
    53
      }
bobbens@2472
    54
      status = 0;
bobbens@2472
    55
   }
bobbens@2472
    56
   default_haptic = NULL;
bobbens@2472
    57
bobbens@2472
    58
   return status;
bobbens@2472
    59
}
bobbens@2472
    60
bobbens@2472
    61
bobbens@2472
    62
/*
bobbens@2472
    63
 * Returns the number of available devices.
bobbens@2472
    64
 */
bobbens@2472
    65
int
bobbens@2472
    66
SDL_NumHaptics(void)
bobbens@2472
    67
{
bobbens@2472
    68
   return SDL_numhaptics;
bobbens@2472
    69
}
bobbens@2472
    70
bobbens@2472
    71
bobbens@2472
    72
/*
bobbens@2472
    73
 * Gets the name of a Haptic device by index.
bobbens@2472
    74
 */
bobbens@2472
    75
const char *
bobbens@2472
    76
SDL_HapticName(int device_index)
bobbens@2472
    77
{
bobbens@2472
    78
   if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
bobbens@2472
    79
      SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
bobbens@2472
    80
      return NULL;
bobbens@2472
    81
   }
bobbens@2472
    82
   return SDL_SYS_HapticName(device_index);
bobbens@2472
    83
}
bobbens@2472
    84
bobbens@2472
    85
bobbens@2472
    86
/*
bobbens@2475
    87
 * Opens a Haptic device.
bobbens@2472
    88
 */
bobbens@2472
    89
SDL_Haptic *
bobbens@2472
    90
SDL_HapticOpen(int device_index)
bobbens@2472
    91
{
bobbens@2472
    92
   int i;
bobbens@2472
    93
   SDL_Haptic *haptic;
bobbens@2472
    94
bobbens@2472
    95
   if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
bobbens@2472
    96
      SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
bobbens@2472
    97
      return NULL;
bobbens@2472
    98
   }
bobbens@2472
    99
bobbens@2472
   100
   /* If the haptic is already open, return it */
bobbens@2477
   101
   for (i=0; SDL_haptics[i]; i++) {             
bobbens@2472
   102
      if (device_index == SDL_haptics[i]->index) {
bobbens@2472
   103
         haptic = SDL_haptics[i];
bobbens@2472
   104
         ++haptic->ref_count;
bobbens@2472
   105
         return haptic;
bobbens@2472
   106
      }
bobbens@2472
   107
   }
bobbens@2472
   108
bobbens@2477
   109
   /* Create the haptic device */
bobbens@2472
   110
   haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
bobbens@2477
   111
   if (haptic == NULL) {
bobbens@2477
   112
      SDL_OutOfMemory();
bobbens@2477
   113
      return NULL;
bobbens@2472
   114
   }
bobbens@2477
   115
bobbens@2477
   116
   /* Initialize the haptic device */
bobbens@2477
   117
   SDL_memset(haptic, 0, (sizeof *haptic));
bobbens@2477
   118
   haptic->index = device_index;
bobbens@2477
   119
   if (SDL_SYS_HapticOpen(haptic) < 0) {
bobbens@2477
   120
      SDL_free(haptic);
bobbens@2477
   121
      return NULL;
bobbens@2472
   122
   }
bobbens@2477
   123
bobbens@2477
   124
   /* Add haptic to list */
bobbens@2477
   125
   ++haptic->ref_count;
bobbens@2477
   126
   for (i = 0; SDL_haptics[i]; ++i)
bobbens@2477
   127
      /* Skip to next haptic */ ;
bobbens@2477
   128
   SDL_haptics[i] = haptic;
bobbens@2477
   129
bobbens@2472
   130
   return haptic;
bobbens@2472
   131
}
bobbens@2474
   132
bobbens@2474
   133
bobbens@2475
   134
/*
bobbens@2489
   135
 * Returns SDL_TRUE if joystick has haptic features.
bobbens@2489
   136
 */
bobbens@2489
   137
int
bobbens@2489
   138
SDL_JoystickIsHaptic(SDL_Joystick * joystick)
bobbens@2489
   139
{
bobbens@2489
   140
   int ret;
bobbens@2489
   141
bobbens@2489
   142
   if (!SDL_PrivateJoystickValid(&joystick)) {
bobbens@2489
   143
      return -1;
bobbens@2489
   144
   }
bobbens@2489
   145
bobbens@2489
   146
   ret = SDL_SYS_JoystickIsHaptic(joystick);
bobbens@2489
   147
bobbens@2489
   148
   if (ret > 0) return SDL_TRUE;
bobbens@2489
   149
   else if (ret == 0) return SDL_FALSE;
bobbens@2489
   150
   else return -1;
bobbens@2489
   151
}
bobbens@2489
   152
bobbens@2489
   153
bobbens@2489
   154
/*
bobbens@2489
   155
 * Opens a haptic device from a joystick.
bobbens@2489
   156
 */
bobbens@2489
   157
SDL_Haptic *
bobbens@2489
   158
SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
bobbens@2489
   159
{
bobbens@2489
   160
   if (!SDL_PrivateJoystickValid(&joystick)) {
bobbens@2489
   161
      return -1;
bobbens@2489
   162
   }
bobbens@2489
   163
   return -1;
bobbens@2489
   164
}
bobbens@2489
   165
bobbens@2489
   166
bobbens@2489
   167
/*
bobbens@2476
   168
 * Checks to see if the haptic device is valid
bobbens@2476
   169
 */
bobbens@2476
   170
static int
bobbens@2476
   171
ValidHaptic(SDL_Haptic ** haptic)
bobbens@2476
   172
{
bobbens@2476
   173
   int valid;
bobbens@2476
   174
bobbens@2476
   175
   if (*haptic == NULL) {
bobbens@2476
   176
      SDL_SetError("Haptic device hasn't been opened yet");
bobbens@2476
   177
      valid = 0;
bobbens@2476
   178
   } else {
bobbens@2476
   179
      valid = 1;
bobbens@2476
   180
   }
bobbens@2476
   181
   return valid;
bobbens@2476
   182
}
bobbens@2476
   183
bobbens@2476
   184
bobbens@2476
   185
/*
bobbens@2475
   186
 * Closes a SDL_Haptic device.
bobbens@2475
   187
 */
bobbens@2475
   188
void
bobbens@2475
   189
SDL_HapticClose(SDL_Haptic * haptic)
bobbens@2475
   190
{
bobbens@2476
   191
   int i;
bobbens@2476
   192
bobbens@2476
   193
   /* Must be valid */
bobbens@2476
   194
   if (!ValidHaptic(&haptic)) {
bobbens@2476
   195
      return;
bobbens@2476
   196
   }
bobbens@2476
   197
bobbens@2476
   198
   /* Check if it's still in use */
bobbens@2476
   199
   if (--haptic->ref_count < 0) {
bobbens@2476
   200
      return;
bobbens@2476
   201
   }
bobbens@2476
   202
bobbens@2480
   203
   /* Close it, properly removing effects if needed */
bobbens@2480
   204
   for (i=0; i<haptic->neffects; i++) {
bobbens@2480
   205
      if (haptic->effects[i].hweffect != NULL) {
bobbens@2480
   206
         SDL_HapticDestroyEffect(haptic,i);
bobbens@2480
   207
      }
bobbens@2480
   208
   }
bobbens@2476
   209
   SDL_SYS_HapticClose(haptic);
bobbens@2476
   210
bobbens@2476
   211
   /* Remove from the list */
bobbens@2476
   212
   for (i = 0; SDL_haptics[i]; ++i) {
bobbens@2476
   213
      if (haptic == SDL_haptics[i]) {
bobbens@2476
   214
         SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
bobbens@2476
   215
               (SDL_numhaptics - i) * sizeof(haptic));
bobbens@2476
   216
         break;
bobbens@2476
   217
      }
bobbens@2476
   218
   }
bobbens@2476
   219
bobbens@2476
   220
   /* Free */
bobbens@2476
   221
   SDL_free(haptic);
bobbens@2475
   222
}
bobbens@2475
   223
bobbens@2475
   224
/*
bobbens@2475
   225
 * Cleans up after the subsystem.
bobbens@2475
   226
 */
bobbens@2474
   227
void
bobbens@2474
   228
SDL_HapticQuit(void)
bobbens@2474
   229
{
bobbens@2474
   230
   SDL_numhaptics = 0;
bobbens@2474
   231
bobbens@2474
   232
   SDL_SYS_HapticQuit();
bobbens@2474
   233
   if (SDL_haptics != NULL) {
bobbens@2474
   234
      SDL_free(SDL_haptics);
bobbens@2474
   235
      SDL_haptics = NULL;
bobbens@2474
   236
   }
bobbens@2474
   237
}
bobbens@2477
   238
bobbens@2478
   239
/*
bobbens@2478
   240
 * Returns the number of effects a haptic device has.
bobbens@2478
   241
 */
bobbens@2478
   242
int
bobbens@2478
   243
SDL_HapticNumEffects(SDL_Haptic * haptic)
bobbens@2478
   244
{
bobbens@2478
   245
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   246
      return -1;
bobbens@2478
   247
   }
bobbens@2478
   248
bobbens@2478
   249
   return haptic->neffects;
bobbens@2478
   250
}
bobbens@2478
   251
bobbens@2478
   252
/*
bobbens@2478
   253
 * Returns supported effects by the device.
bobbens@2478
   254
 */
bobbens@2478
   255
unsigned int
bobbens@2478
   256
SDL_HapticQueryEffects(SDL_Haptic * haptic)
bobbens@2478
   257
{
bobbens@2478
   258
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   259
      return -1;
bobbens@2478
   260
   }
bobbens@2478
   261
bobbens@2478
   262
   return haptic->supported;
bobbens@2478
   263
}
bobbens@2478
   264
bobbens@2486
   265
/*
bobbens@2486
   266
 * Checks to see if the device can support the effect.
bobbens@2486
   267
 */
bobbens@2478
   268
int
bobbens@2478
   269
SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2478
   270
{
bobbens@2478
   271
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   272
      return -1;
bobbens@2478
   273
   }
bobbens@2478
   274
bobbens@2478
   275
   if ((haptic->supported & effect->type) != 0)
bobbens@2478
   276
      return SDL_TRUE;
bobbens@2478
   277
   return SDL_FALSE;
bobbens@2478
   278
}
bobbens@2477
   279
bobbens@2477
   280
/*
bobbens@2477
   281
 * Creates a new haptic effect.
bobbens@2477
   282
 */
bobbens@2477
   283
int
bobbens@2477
   284
SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2477
   285
{
bobbens@2477
   286
   int i;
bobbens@2477
   287
bobbens@2477
   288
   /* Check for device validity. */
bobbens@2477
   289
   if (!ValidHaptic(&haptic)) {
bobbens@2477
   290
      return -1;
bobbens@2477
   291
   }
bobbens@2477
   292
bobbens@2480
   293
   /* Check to see if effect is supported */
bobbens@2480
   294
   if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
bobbens@2480
   295
      SDL_SetError("Haptic effect not supported by haptic device.");
bobbens@2480
   296
      return -1;
bobbens@2480
   297
   }
bobbens@2480
   298
bobbens@2477
   299
   /* See if there's a free slot */
bobbens@2477
   300
   for (i=0; i<haptic->neffects; i++) {
bobbens@2477
   301
      if (haptic->effects[i].hweffect == NULL) {
bobbens@2477
   302
bobbens@2477
   303
         /* Now let the backend create the real effect */
bobbens@2480
   304
         if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
bobbens@2477
   305
            return -1; /* Backend failed to create effect */
bobbens@2477
   306
         }
bobbens@2477
   307
         return i;
bobbens@2477
   308
      }
bobbens@2477
   309
   }
bobbens@2477
   310
bobbens@2477
   311
   SDL_SetError("Haptic device has no free space left.");
bobbens@2477
   312
   return -1;
bobbens@2477
   313
}
bobbens@2477
   314
bobbens@2477
   315
/*
bobbens@2480
   316
 * Checks to see if an effect is valid.
bobbens@2480
   317
 */
bobbens@2480
   318
static int
bobbens@2480
   319
ValidEffect(SDL_Haptic * haptic, int effect)
bobbens@2480
   320
{
bobbens@2480
   321
   if ((effect < 0) || (effect >= haptic->neffects)) {
bobbens@2480
   322
      SDL_SetError("Invalid haptic effect identifier.");
bobbens@2480
   323
      return 0;
bobbens@2480
   324
   }
bobbens@2480
   325
   return 1;
bobbens@2480
   326
}
bobbens@2480
   327
bobbens@2480
   328
/*
bobbens@2488
   329
 * Updates an effect.
bobbens@2488
   330
 */
bobbens@2488
   331
int
bobbens@2488
   332
SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data)
bobbens@2488
   333
{
bobbens@2488
   334
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2488
   335
      return -1;
bobbens@2488
   336
   }
bobbens@2488
   337
bobbens@2488
   338
   /* Updates the effect */
bobbens@2488
   339
   if (SDL_SYS_HapticUpdateEffect(haptic,&haptic->effects[effect],data) < 0) {
bobbens@2488
   340
      return -1;
bobbens@2488
   341
   }
bobbens@2488
   342
bobbens@2488
   343
   return 0;
bobbens@2488
   344
}
bobbens@2488
   345
bobbens@2488
   346
bobbens@2488
   347
/*
bobbens@2477
   348
 * Runs the haptic effect on the device.
bobbens@2477
   349
 */
bobbens@2477
   350
int
bobbens@2477
   351
SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
bobbens@2477
   352
{
bobbens@2480
   353
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   354
      return -1;
bobbens@2477
   355
   }
bobbens@2479
   356
bobbens@2479
   357
   /* Run the effect */
bobbens@2479
   358
   if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) {
bobbens@2479
   359
      return -1;
bobbens@2479
   360
   }
bobbens@2479
   361
bobbens@2479
   362
   return 0;
bobbens@2477
   363
}
bobbens@2477
   364
bobbens@2477
   365
/*
bobbens@2485
   366
 * Stops the haptic effect on the device.
bobbens@2485
   367
 */
bobbens@2485
   368
int
bobbens@2485
   369
SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
bobbens@2485
   370
{
bobbens@2485
   371
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2485
   372
      return -1;
bobbens@2485
   373
   }
bobbens@2485
   374
bobbens@2485
   375
   /* Stop the effect */
bobbens@2485
   376
   if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) {
bobbens@2485
   377
      return -1;
bobbens@2485
   378
   }
bobbens@2485
   379
bobbens@2485
   380
   return 0;
bobbens@2485
   381
}
bobbens@2485
   382
bobbens@2485
   383
/*
bobbens@2477
   384
 * Gets rid of a haptic effect.
bobbens@2477
   385
 */
bobbens@2477
   386
void
bobbens@2477
   387
SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
bobbens@2477
   388
{
bobbens@2480
   389
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   390
      return;
bobbens@2477
   391
   }
bobbens@2479
   392
bobbens@2479
   393
   /* Not allocated */
bobbens@2479
   394
   if (haptic->effects[effect].hweffect == NULL) {
bobbens@2479
   395
      return;
bobbens@2479
   396
   }
bobbens@2479
   397
bobbens@2479
   398
   SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
bobbens@2477
   399
}
bobbens@2477
   400
bobbens@2483
   401
/*
bobbens@2483
   402
 * Sets the global gain of the device.
bobbens@2483
   403
 */
bobbens@2483
   404
int
bobbens@2483
   405
SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
bobbens@2483
   406
{
bobbens@2483
   407
   if (!ValidHaptic(&haptic)) {
bobbens@2483
   408
      return -1;
bobbens@2483
   409
   }
bobbens@2477
   410
bobbens@2484
   411
   if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
bobbens@2484
   412
      SDL_SetError("Haptic device does not support setting gain.");
bobbens@2484
   413
      return -1;
bobbens@2484
   414
   }
bobbens@2484
   415
bobbens@2483
   416
   if ((gain < 0) || (gain > 100)) {
bobbens@2483
   417
      SDL_SetError("Haptic gain must be between 0 and 100.");
bobbens@2483
   418
      return -1;
bobbens@2483
   419
   }
bobbens@2483
   420
bobbens@2483
   421
   if (SDL_SYS_HapticSetGain(haptic,gain) < 0) {
bobbens@2483
   422
      return -1;
bobbens@2483
   423
   }
bobbens@2483
   424
bobbens@2483
   425
   return 0;
bobbens@2483
   426
}
bobbens@2483
   427
bobbens@2484
   428
/*
bobbens@2484
   429
 * Makes the device autocenter, 0 disables.
bobbens@2484
   430
 */
bobbens@2484
   431
int
bobbens@2484
   432
SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
bobbens@2484
   433
{
bobbens@2484
   434
   if (!ValidHaptic(&haptic)) {
bobbens@2484
   435
      return -1;
bobbens@2484
   436
   }
bobbens@2483
   437
bobbens@2484
   438
   if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
bobbens@2484
   439
      SDL_SetError("Haptic device does not support setting autocenter.");
bobbens@2484
   440
      return -1;
bobbens@2484
   441
   }
bobbens@2484
   442
bobbens@2484
   443
   if ((autocenter < 0) || (autocenter > 100)) {
bobbens@2484
   444
      SDL_SetError("Haptic autocenter must be between 0 and 100.");
bobbens@2484
   445
      return -1;
bobbens@2484
   446
   }                                           
bobbens@2484
   447
bobbens@2484
   448
   if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) {
bobbens@2484
   449
      return -1;
bobbens@2484
   450
   }
bobbens@2484
   451
bobbens@2484
   452
   return 0;
bobbens@2484
   453
}
bobbens@2484
   454
bobbens@2484
   455