src/haptic/SDL_haptic.c
author Edgar Simo <bobbens@gmail.com>
Tue, 01 Jul 2008 16:19:54 +0000
branchgsoc2008_force_feedback
changeset 2486 24dd8b8669fa
parent 2485 67978eea6d10
child 2488 8e2bdbccf7ff
permissions -rw-r--r--
Added SDL_HAPTIC_CUSTOM (not supported on linux).
Exposed SDL_HapticEffectSupported().
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@2472
    26
bobbens@2472
    27
bobbens@2472
    28
static Uint8 SDL_numhaptics = 0;
bobbens@2472
    29
SDL_Haptic **SDL_haptics = NULL;
bobbens@2472
    30
static SDL_Haptic *default_haptic = NULL;
bobbens@2472
    31
bobbens@2472
    32
bobbens@2472
    33
/*
bobbens@2472
    34
 * Initializes the Haptic devices.
bobbens@2472
    35
 */
bobbens@2472
    36
int
bobbens@2472
    37
SDL_HapticInit(void)
bobbens@2472
    38
{  
bobbens@2472
    39
   int arraylen;
bobbens@2472
    40
   int status;
bobbens@2472
    41
bobbens@2472
    42
   SDL_numhaptics = 0;
bobbens@2472
    43
   status = SDL_SYS_HapticInit();
bobbens@2472
    44
   if (status >= 0) {
bobbens@2472
    45
      arraylen = (status + 1) * sizeof(*SDL_haptics);
bobbens@2472
    46
      SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
bobbens@2472
    47
      if (SDL_haptics == NULL) {
bobbens@2472
    48
         SDL_numhaptics = 0;
bobbens@2472
    49
      } else {
bobbens@2472
    50
         SDL_memset(SDL_haptics, 0, arraylen);
bobbens@2472
    51
         SDL_numhaptics = status;
bobbens@2472
    52
      }
bobbens@2472
    53
      status = 0;
bobbens@2472
    54
   }
bobbens@2472
    55
   default_haptic = NULL;
bobbens@2472
    56
bobbens@2472
    57
   return status;
bobbens@2472
    58
}
bobbens@2472
    59
bobbens@2472
    60
bobbens@2472
    61
/*
bobbens@2472
    62
 * Returns the number of available devices.
bobbens@2472
    63
 */
bobbens@2472
    64
int
bobbens@2472
    65
SDL_NumHaptics(void)
bobbens@2472
    66
{
bobbens@2472
    67
   return SDL_numhaptics;
bobbens@2472
    68
}
bobbens@2472
    69
bobbens@2472
    70
bobbens@2472
    71
/*
bobbens@2472
    72
 * Gets the name of a Haptic device by index.
bobbens@2472
    73
 */
bobbens@2472
    74
const char *
bobbens@2472
    75
SDL_HapticName(int device_index)
bobbens@2472
    76
{
bobbens@2472
    77
   if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
bobbens@2472
    78
      SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
bobbens@2472
    79
      return NULL;
bobbens@2472
    80
   }
bobbens@2472
    81
   return SDL_SYS_HapticName(device_index);
bobbens@2472
    82
}
bobbens@2472
    83
bobbens@2472
    84
bobbens@2472
    85
/*
bobbens@2475
    86
 * Opens a Haptic device.
bobbens@2472
    87
 */
bobbens@2472
    88
SDL_Haptic *
bobbens@2472
    89
SDL_HapticOpen(int device_index)
bobbens@2472
    90
{
bobbens@2472
    91
   int i;
bobbens@2472
    92
   SDL_Haptic *haptic;
bobbens@2472
    93
bobbens@2472
    94
   if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
bobbens@2472
    95
      SDL_SetError("There are %d haptic devices available", SDL_numhaptics);
bobbens@2472
    96
      return NULL;
bobbens@2472
    97
   }
bobbens@2472
    98
bobbens@2472
    99
   /* If the haptic is already open, return it */
bobbens@2477
   100
   for (i=0; SDL_haptics[i]; i++) {             
bobbens@2472
   101
      if (device_index == SDL_haptics[i]->index) {
bobbens@2472
   102
         haptic = SDL_haptics[i];
bobbens@2472
   103
         ++haptic->ref_count;
bobbens@2472
   104
         return haptic;
bobbens@2472
   105
      }
bobbens@2472
   106
   }
bobbens@2472
   107
bobbens@2477
   108
   /* Create the haptic device */
bobbens@2472
   109
   haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
bobbens@2477
   110
   if (haptic == NULL) {
bobbens@2477
   111
      SDL_OutOfMemory();
bobbens@2477
   112
      return NULL;
bobbens@2472
   113
   }
bobbens@2477
   114
bobbens@2477
   115
   /* Initialize the haptic device */
bobbens@2477
   116
   SDL_memset(haptic, 0, (sizeof *haptic));
bobbens@2477
   117
   haptic->index = device_index;
bobbens@2477
   118
   if (SDL_SYS_HapticOpen(haptic) < 0) {
bobbens@2477
   119
      SDL_free(haptic);
bobbens@2477
   120
      return NULL;
bobbens@2472
   121
   }
bobbens@2477
   122
bobbens@2477
   123
   /* Add haptic to list */
bobbens@2477
   124
   ++haptic->ref_count;
bobbens@2477
   125
   for (i = 0; SDL_haptics[i]; ++i)
bobbens@2477
   126
      /* Skip to next haptic */ ;
bobbens@2477
   127
   SDL_haptics[i] = haptic;
bobbens@2477
   128
bobbens@2472
   129
   return haptic;
bobbens@2472
   130
}
bobbens@2474
   131
bobbens@2474
   132
bobbens@2475
   133
/*
bobbens@2476
   134
 * Checks to see if the haptic device is valid
bobbens@2476
   135
 */
bobbens@2476
   136
static int
bobbens@2476
   137
ValidHaptic(SDL_Haptic ** haptic)
bobbens@2476
   138
{
bobbens@2476
   139
   int valid;
bobbens@2476
   140
bobbens@2476
   141
   if (*haptic == NULL) {
bobbens@2476
   142
      SDL_SetError("Haptic device hasn't been opened yet");
bobbens@2476
   143
      valid = 0;
bobbens@2476
   144
   } else {
bobbens@2476
   145
      valid = 1;
bobbens@2476
   146
   }
bobbens@2476
   147
   return valid;
bobbens@2476
   148
}
bobbens@2476
   149
bobbens@2476
   150
bobbens@2476
   151
/*
bobbens@2475
   152
 * Closes a SDL_Haptic device.
bobbens@2475
   153
 */
bobbens@2475
   154
void
bobbens@2475
   155
SDL_HapticClose(SDL_Haptic * haptic)
bobbens@2475
   156
{
bobbens@2476
   157
   int i;
bobbens@2476
   158
bobbens@2476
   159
   /* Must be valid */
bobbens@2476
   160
   if (!ValidHaptic(&haptic)) {
bobbens@2476
   161
      return;
bobbens@2476
   162
   }
bobbens@2476
   163
bobbens@2476
   164
   /* Check if it's still in use */
bobbens@2476
   165
   if (--haptic->ref_count < 0) {
bobbens@2476
   166
      return;
bobbens@2476
   167
   }
bobbens@2476
   168
bobbens@2480
   169
   /* Close it, properly removing effects if needed */
bobbens@2480
   170
   for (i=0; i<haptic->neffects; i++) {
bobbens@2480
   171
      if (haptic->effects[i].hweffect != NULL) {
bobbens@2480
   172
         SDL_HapticDestroyEffect(haptic,i);
bobbens@2480
   173
      }
bobbens@2480
   174
   }
bobbens@2476
   175
   SDL_SYS_HapticClose(haptic);
bobbens@2476
   176
bobbens@2476
   177
   /* Remove from the list */
bobbens@2476
   178
   for (i = 0; SDL_haptics[i]; ++i) {
bobbens@2476
   179
      if (haptic == SDL_haptics[i]) {
bobbens@2476
   180
         SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
bobbens@2476
   181
               (SDL_numhaptics - i) * sizeof(haptic));
bobbens@2476
   182
         break;
bobbens@2476
   183
      }
bobbens@2476
   184
   }
bobbens@2476
   185
bobbens@2476
   186
   /* Free */
bobbens@2476
   187
   SDL_free(haptic);
bobbens@2475
   188
}
bobbens@2475
   189
bobbens@2475
   190
/*
bobbens@2475
   191
 * Cleans up after the subsystem.
bobbens@2475
   192
 */
bobbens@2474
   193
void
bobbens@2474
   194
SDL_HapticQuit(void)
bobbens@2474
   195
{
bobbens@2474
   196
   SDL_numhaptics = 0;
bobbens@2474
   197
bobbens@2474
   198
   SDL_SYS_HapticQuit();
bobbens@2474
   199
   if (SDL_haptics != NULL) {
bobbens@2474
   200
      SDL_free(SDL_haptics);
bobbens@2474
   201
      SDL_haptics = NULL;
bobbens@2474
   202
   }
bobbens@2474
   203
}
bobbens@2477
   204
bobbens@2478
   205
/*
bobbens@2478
   206
 * Returns the number of effects a haptic device has.
bobbens@2478
   207
 */
bobbens@2478
   208
int
bobbens@2478
   209
SDL_HapticNumEffects(SDL_Haptic * haptic)
bobbens@2478
   210
{
bobbens@2478
   211
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   212
      return -1;
bobbens@2478
   213
   }
bobbens@2478
   214
bobbens@2478
   215
   return haptic->neffects;
bobbens@2478
   216
}
bobbens@2478
   217
bobbens@2478
   218
/*
bobbens@2478
   219
 * Returns supported effects by the device.
bobbens@2478
   220
 */
bobbens@2478
   221
unsigned int
bobbens@2478
   222
SDL_HapticQueryEffects(SDL_Haptic * haptic)
bobbens@2478
   223
{
bobbens@2478
   224
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   225
      return -1;
bobbens@2478
   226
   }
bobbens@2478
   227
bobbens@2478
   228
   return haptic->supported;
bobbens@2478
   229
}
bobbens@2478
   230
bobbens@2486
   231
/*
bobbens@2486
   232
 * Checks to see if the device can support the effect.
bobbens@2486
   233
 */
bobbens@2478
   234
int
bobbens@2478
   235
SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2478
   236
{
bobbens@2478
   237
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   238
      return -1;
bobbens@2478
   239
   }
bobbens@2478
   240
bobbens@2478
   241
   if ((haptic->supported & effect->type) != 0)
bobbens@2478
   242
      return SDL_TRUE;
bobbens@2478
   243
   return SDL_FALSE;
bobbens@2478
   244
}
bobbens@2477
   245
bobbens@2477
   246
/*
bobbens@2477
   247
 * Creates a new haptic effect.
bobbens@2477
   248
 */
bobbens@2477
   249
int
bobbens@2477
   250
SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2477
   251
{
bobbens@2477
   252
   int i;
bobbens@2477
   253
bobbens@2477
   254
   /* Check for device validity. */
bobbens@2477
   255
   if (!ValidHaptic(&haptic)) {
bobbens@2477
   256
      return -1;
bobbens@2477
   257
   }
bobbens@2477
   258
bobbens@2480
   259
   /* Check to see if effect is supported */
bobbens@2480
   260
   if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
bobbens@2480
   261
      SDL_SetError("Haptic effect not supported by haptic device.");
bobbens@2480
   262
      return -1;
bobbens@2480
   263
   }
bobbens@2480
   264
bobbens@2477
   265
   /* See if there's a free slot */
bobbens@2477
   266
   for (i=0; i<haptic->neffects; i++) {
bobbens@2477
   267
      if (haptic->effects[i].hweffect == NULL) {
bobbens@2477
   268
bobbens@2477
   269
         /* Now let the backend create the real effect */
bobbens@2480
   270
         if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
bobbens@2477
   271
            return -1; /* Backend failed to create effect */
bobbens@2477
   272
         }
bobbens@2477
   273
         return i;
bobbens@2477
   274
      }
bobbens@2477
   275
   }
bobbens@2477
   276
bobbens@2477
   277
   SDL_SetError("Haptic device has no free space left.");
bobbens@2477
   278
   return -1;
bobbens@2477
   279
}
bobbens@2477
   280
bobbens@2477
   281
/*
bobbens@2480
   282
 * Checks to see if an effect is valid.
bobbens@2480
   283
 */
bobbens@2480
   284
static int
bobbens@2480
   285
ValidEffect(SDL_Haptic * haptic, int effect)
bobbens@2480
   286
{
bobbens@2480
   287
   if ((effect < 0) || (effect >= haptic->neffects)) {
bobbens@2480
   288
      SDL_SetError("Invalid haptic effect identifier.");
bobbens@2480
   289
      return 0;
bobbens@2480
   290
   }
bobbens@2480
   291
   return 1;
bobbens@2480
   292
}
bobbens@2480
   293
bobbens@2480
   294
/*
bobbens@2477
   295
 * Runs the haptic effect on the device.
bobbens@2477
   296
 */
bobbens@2477
   297
int
bobbens@2477
   298
SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
bobbens@2477
   299
{
bobbens@2480
   300
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   301
      return -1;
bobbens@2477
   302
   }
bobbens@2479
   303
bobbens@2479
   304
   /* Run the effect */
bobbens@2479
   305
   if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) {
bobbens@2479
   306
      return -1;
bobbens@2479
   307
   }
bobbens@2479
   308
bobbens@2479
   309
   return 0;
bobbens@2477
   310
}
bobbens@2477
   311
bobbens@2477
   312
/*
bobbens@2485
   313
 * Stops the haptic effect on the device.
bobbens@2485
   314
 */
bobbens@2485
   315
int
bobbens@2485
   316
SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
bobbens@2485
   317
{
bobbens@2485
   318
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2485
   319
      return -1;
bobbens@2485
   320
   }
bobbens@2485
   321
bobbens@2485
   322
   /* Stop the effect */
bobbens@2485
   323
   if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) {
bobbens@2485
   324
      return -1;
bobbens@2485
   325
   }
bobbens@2485
   326
bobbens@2485
   327
   return 0;
bobbens@2485
   328
}
bobbens@2485
   329
bobbens@2485
   330
/*
bobbens@2477
   331
 * Gets rid of a haptic effect.
bobbens@2477
   332
 */
bobbens@2477
   333
void
bobbens@2477
   334
SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
bobbens@2477
   335
{
bobbens@2480
   336
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   337
      return;
bobbens@2477
   338
   }
bobbens@2479
   339
bobbens@2479
   340
   /* Not allocated */
bobbens@2479
   341
   if (haptic->effects[effect].hweffect == NULL) {
bobbens@2479
   342
      return;
bobbens@2479
   343
   }
bobbens@2479
   344
bobbens@2479
   345
   SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
bobbens@2477
   346
}
bobbens@2477
   347
bobbens@2483
   348
/*
bobbens@2483
   349
 * Sets the global gain of the device.
bobbens@2483
   350
 */
bobbens@2483
   351
int
bobbens@2483
   352
SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
bobbens@2483
   353
{
bobbens@2483
   354
   if (!ValidHaptic(&haptic)) {
bobbens@2483
   355
      return -1;
bobbens@2483
   356
   }
bobbens@2477
   357
bobbens@2484
   358
   if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
bobbens@2484
   359
      SDL_SetError("Haptic device does not support setting gain.");
bobbens@2484
   360
      return -1;
bobbens@2484
   361
   }
bobbens@2484
   362
bobbens@2483
   363
   if ((gain < 0) || (gain > 100)) {
bobbens@2483
   364
      SDL_SetError("Haptic gain must be between 0 and 100.");
bobbens@2483
   365
      return -1;
bobbens@2483
   366
   }
bobbens@2483
   367
bobbens@2483
   368
   if (SDL_SYS_HapticSetGain(haptic,gain) < 0) {
bobbens@2483
   369
      return -1;
bobbens@2483
   370
   }
bobbens@2483
   371
bobbens@2483
   372
   return 0;
bobbens@2483
   373
}
bobbens@2483
   374
bobbens@2484
   375
/*
bobbens@2484
   376
 * Makes the device autocenter, 0 disables.
bobbens@2484
   377
 */
bobbens@2484
   378
int
bobbens@2484
   379
SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
bobbens@2484
   380
{
bobbens@2484
   381
   if (!ValidHaptic(&haptic)) {
bobbens@2484
   382
      return -1;
bobbens@2484
   383
   }
bobbens@2483
   384
bobbens@2484
   385
   if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
bobbens@2484
   386
      SDL_SetError("Haptic device does not support setting autocenter.");
bobbens@2484
   387
      return -1;
bobbens@2484
   388
   }
bobbens@2484
   389
bobbens@2484
   390
   if ((autocenter < 0) || (autocenter > 100)) {
bobbens@2484
   391
      SDL_SetError("Haptic autocenter must be between 0 and 100.");
bobbens@2484
   392
      return -1;
bobbens@2484
   393
   }                                           
bobbens@2484
   394
bobbens@2484
   395
   if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) {
bobbens@2484
   396
      return -1;
bobbens@2484
   397
   }
bobbens@2484
   398
bobbens@2484
   399
   return 0;
bobbens@2484
   400
}
bobbens@2484
   401
bobbens@2484
   402