src/haptic/SDL_haptic.c
author Edgar Simo <bobbens@gmail.com>
Thu, 17 Jul 2008 11:47:48 +0000
branchgsoc2008_force_feedback
changeset 2523 366d84fdf8d1
parent 2519 af9df9662807
child 2524 1a55848ce198
permissions -rw-r--r--
Haptic subsystem handles haptic axes now.
Support for SDL_HAPTIC_SPHERICAL on linux.
More error checking.
Improved documentation.
Added missing SDLCALLs to SDL_haptic.h.
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_syshaptic.h"
bobbens@2489
    25
#include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
bobbens@2472
    26
bobbens@2472
    27
bobbens@2512
    28
Uint8 SDL_numhaptics = 0;
bobbens@2472
    29
SDL_Haptic **SDL_haptics = NULL;
bobbens@2472
    30
bobbens@2472
    31
bobbens@2472
    32
/*
bobbens@2472
    33
 * Initializes the Haptic devices.
bobbens@2472
    34
 */
bobbens@2472
    35
int
bobbens@2472
    36
SDL_HapticInit(void)
bobbens@2472
    37
{  
bobbens@2472
    38
   int arraylen;
bobbens@2472
    39
   int status;
bobbens@2472
    40
bobbens@2472
    41
   SDL_numhaptics = 0;
bobbens@2472
    42
   status = SDL_SYS_HapticInit();
bobbens@2472
    43
   if (status >= 0) {
bobbens@2472
    44
      arraylen = (status + 1) * sizeof(*SDL_haptics);
bobbens@2472
    45
      SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
bobbens@2514
    46
      if (SDL_haptics == NULL) { /* Out of memory. */
bobbens@2472
    47
         SDL_numhaptics = 0;
bobbens@2514
    48
      }
bobbens@2514
    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
bobbens@2472
    56
   return status;
bobbens@2472
    57
}
bobbens@2472
    58
bobbens@2472
    59
bobbens@2472
    60
/*
bobbens@2505
    61
 * Checks to see if the haptic device is valid
bobbens@2505
    62
 */
bobbens@2505
    63
static int
bobbens@2505
    64
ValidHaptic(SDL_Haptic ** haptic)
bobbens@2505
    65
{
bobbens@2505
    66
   int valid;
bobbens@2505
    67
   
bobbens@2505
    68
   if (*haptic == NULL) {
bobbens@2513
    69
      SDL_SetError("Haptic: Device hasn't been opened yet");
bobbens@2505
    70
      valid = 0;
bobbens@2505
    71
   } else {
bobbens@2505
    72
      valid = 1;
bobbens@2505
    73
   }
bobbens@2505
    74
   return valid;
bobbens@2505
    75
}
bobbens@2505
    76
bobbens@2505
    77
bobbens@2505
    78
/*
bobbens@2472
    79
 * Returns the number of available devices.
bobbens@2472
    80
 */
bobbens@2472
    81
int
bobbens@2472
    82
SDL_NumHaptics(void)
bobbens@2472
    83
{
bobbens@2472
    84
   return SDL_numhaptics;
bobbens@2472
    85
}
bobbens@2472
    86
bobbens@2472
    87
bobbens@2472
    88
/*
bobbens@2472
    89
 * Gets the name of a Haptic device by index.
bobbens@2472
    90
 */
bobbens@2472
    91
const char *
bobbens@2472
    92
SDL_HapticName(int device_index)
bobbens@2472
    93
{
bobbens@2472
    94
   if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
bobbens@2513
    95
      SDL_SetError("Haptic: There are %d haptic devices available", SDL_numhaptics);
bobbens@2472
    96
      return NULL;
bobbens@2472
    97
   }
bobbens@2472
    98
   return SDL_SYS_HapticName(device_index);
bobbens@2472
    99
}
bobbens@2472
   100
bobbens@2472
   101
bobbens@2472
   102
/*
bobbens@2475
   103
 * Opens a Haptic device.
bobbens@2472
   104
 */
bobbens@2472
   105
SDL_Haptic *
bobbens@2472
   106
SDL_HapticOpen(int device_index)
bobbens@2472
   107
{
bobbens@2472
   108
   int i;
bobbens@2472
   109
   SDL_Haptic *haptic;
bobbens@2472
   110
bobbens@2472
   111
   if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
bobbens@2513
   112
      SDL_SetError("Haptic: There are %d haptic devices available", SDL_numhaptics);
bobbens@2472
   113
      return NULL;
bobbens@2472
   114
   }
bobbens@2472
   115
bobbens@2472
   116
   /* If the haptic is already open, return it */
bobbens@2477
   117
   for (i=0; SDL_haptics[i]; i++) {             
bobbens@2472
   118
      if (device_index == SDL_haptics[i]->index) {
bobbens@2472
   119
         haptic = SDL_haptics[i];
bobbens@2472
   120
         ++haptic->ref_count;
bobbens@2472
   121
         return haptic;
bobbens@2472
   122
      }
bobbens@2472
   123
   }
bobbens@2472
   124
bobbens@2477
   125
   /* Create the haptic device */
bobbens@2472
   126
   haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
bobbens@2477
   127
   if (haptic == NULL) {
bobbens@2477
   128
      SDL_OutOfMemory();
bobbens@2477
   129
      return NULL;
bobbens@2472
   130
   }
bobbens@2477
   131
bobbens@2477
   132
   /* Initialize the haptic device */
bobbens@2477
   133
   SDL_memset(haptic, 0, (sizeof *haptic));
bobbens@2477
   134
   haptic->index = device_index;
bobbens@2477
   135
   if (SDL_SYS_HapticOpen(haptic) < 0) {
bobbens@2477
   136
      SDL_free(haptic);
bobbens@2477
   137
      return NULL;
bobbens@2472
   138
   }
bobbens@2477
   139
bobbens@2507
   140
   /* Disable autocenter and set gain to max. */
bobbens@2507
   141
   if (haptic->supported & SDL_HAPTIC_GAIN)
bobbens@2507
   142
      SDL_HapticSetGain(haptic,100);
bobbens@2507
   143
   if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
bobbens@2507
   144
      SDL_HapticSetAutocenter(haptic,0);
bobbens@2507
   145
bobbens@2477
   146
   /* Add haptic to list */
bobbens@2477
   147
   ++haptic->ref_count;
bobbens@2490
   148
   for (i=0; SDL_haptics[i]; i++)
bobbens@2477
   149
      /* Skip to next haptic */ ;
bobbens@2477
   150
   SDL_haptics[i] = haptic;
bobbens@2477
   151
bobbens@2472
   152
   return haptic;
bobbens@2472
   153
}
bobbens@2474
   154
bobbens@2474
   155
bobbens@2475
   156
/*
bobbens@2506
   157
 * Returns 1 if the device has been opened.
bobbens@2506
   158
 */
bobbens@2506
   159
int
bobbens@2506
   160
SDL_HapticOpened(int device_index)
bobbens@2506
   161
{
bobbens@2506
   162
   int i, opened;
bobbens@2506
   163
bobbens@2506
   164
   opened = 0;
bobbens@2506
   165
   for (i=0; SDL_haptics[i]; i++) {
bobbens@2506
   166
      if (SDL_haptics[i]->index == (Uint8) device_index) {
bobbens@2506
   167
         opened = 1;
bobbens@2506
   168
         break;
bobbens@2506
   169
      }
bobbens@2506
   170
   }
bobbens@2506
   171
   return opened;
bobbens@2506
   172
}
bobbens@2506
   173
bobbens@2506
   174
bobbens@2506
   175
/*
bobbens@2505
   176
 * Returns the index to a haptic device.
bobbens@2505
   177
 */
bobbens@2505
   178
int
bobbens@2505
   179
SDL_HapticIndex(SDL_Haptic * haptic)
bobbens@2505
   180
{
bobbens@2505
   181
   if (!ValidHaptic(&haptic)) {
bobbens@2505
   182
      return -1;
bobbens@2505
   183
   }
bobbens@2505
   184
bobbens@2505
   185
   return haptic->index;
bobbens@2505
   186
}
bobbens@2505
   187
bobbens@2505
   188
bobbens@2505
   189
/*
bobbens@2512
   190
 * Returns SDL_TRUE if mouse is haptic, SDL_FALSE if it isn't.
bobbens@2512
   191
 */
bobbens@2512
   192
int
bobbens@2512
   193
SDL_MouseIsHaptic(void)
bobbens@2512
   194
{
bobbens@2512
   195
   if (SDL_SYS_HapticMouse() < 0)
bobbens@2512
   196
      return SDL_FALSE;
bobbens@2512
   197
   return SDL_TRUE;
bobbens@2512
   198
}
bobbens@2512
   199
bobbens@2512
   200
bobbens@2512
   201
/*
bobbens@2512
   202
 * Returns the haptic device if mouse is haptic or NULL elsewise.
bobbens@2512
   203
 */
bobbens@2512
   204
SDL_Haptic *
bobbens@2512
   205
SDL_HapticOpenFromMouse(void)
bobbens@2512
   206
{
bobbens@2512
   207
   int device_index;
bobbens@2512
   208
bobbens@2512
   209
   device_index = SDL_SYS_HapticMouse();
bobbens@2512
   210
bobbens@2512
   211
   if (device_index < 0) {
bobbens@2513
   212
      SDL_SetError("Haptic: Mouse isn't a haptic device.");
bobbens@2512
   213
      return NULL;
bobbens@2512
   214
   }
bobbens@2512
   215
bobbens@2512
   216
   return SDL_HapticOpen(device_index);
bobbens@2512
   217
}
bobbens@2512
   218
bobbens@2512
   219
bobbens@2512
   220
/*
bobbens@2489
   221
 * Returns SDL_TRUE if joystick has haptic features.
bobbens@2489
   222
 */
bobbens@2489
   223
int
bobbens@2489
   224
SDL_JoystickIsHaptic(SDL_Joystick * joystick)
bobbens@2489
   225
{
bobbens@2489
   226
   int ret;
bobbens@2489
   227
bobbens@2490
   228
   /* Must be a valid joystick */
bobbens@2489
   229
   if (!SDL_PrivateJoystickValid(&joystick)) {
bobbens@2489
   230
      return -1;
bobbens@2489
   231
   }
bobbens@2489
   232
bobbens@2489
   233
   ret = SDL_SYS_JoystickIsHaptic(joystick);
bobbens@2489
   234
bobbens@2489
   235
   if (ret > 0) return SDL_TRUE;
bobbens@2489
   236
   else if (ret == 0) return SDL_FALSE;
bobbens@2489
   237
   else return -1;
bobbens@2489
   238
}
bobbens@2489
   239
bobbens@2489
   240
bobbens@2489
   241
/*
bobbens@2489
   242
 * Opens a haptic device from a joystick.
bobbens@2489
   243
 */
bobbens@2489
   244
SDL_Haptic *
bobbens@2489
   245
SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
bobbens@2489
   246
{
bobbens@2490
   247
   int i;
bobbens@2490
   248
   SDL_Haptic *haptic;
bobbens@2490
   249
bobbens@2490
   250
   /* Must be a valid joystick */
bobbens@2489
   251
   if (!SDL_PrivateJoystickValid(&joystick)) {
bobbens@2490
   252
      return NULL;
bobbens@2489
   253
   }
bobbens@2490
   254
bobbens@2490
   255
   /* Joystick must be haptic */
bobbens@2490
   256
   if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
bobbens@2490
   257
      return NULL;
bobbens@2490
   258
   }
bobbens@2490
   259
bobbens@2490
   260
   /* Check to see if joystick's haptic is already open */
bobbens@2490
   261
   for (i=0; SDL_haptics[i]; i++) {
bobbens@2491
   262
      if (SDL_SYS_JoystickSameHaptic(SDL_haptics[i],joystick)) {
bobbens@2490
   263
         haptic = SDL_haptics[i];
bobbens@2490
   264
         ++haptic->ref_count;
bobbens@2490
   265
         return haptic;
bobbens@2490
   266
      }
bobbens@2490
   267
   }
bobbens@2490
   268
bobbens@2490
   269
   /* Create the haptic device */
bobbens@2490
   270
   haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
bobbens@2490
   271
   if (haptic == NULL) {
bobbens@2490
   272
      SDL_OutOfMemory();
bobbens@2490
   273
      return NULL;
bobbens@2490
   274
   }
bobbens@2490
   275
bobbens@2490
   276
   /* Initialize the haptic device */
bobbens@2490
   277
   SDL_memset(haptic, 0, (sizeof *haptic));
bobbens@2490
   278
   if (SDL_SYS_HapticOpenFromJoystick(haptic,joystick) < 0) {
bobbens@2490
   279
      SDL_free(haptic);
bobbens@2490
   280
      return NULL;
bobbens@2490
   281
   }
bobbens@2490
   282
bobbens@2490
   283
   /* Add haptic to list */
bobbens@2490
   284
   ++haptic->ref_count;
bobbens@2490
   285
   for (i=0; SDL_haptics[i]; i++)
bobbens@2490
   286
      /* Skip to next haptic */ ;
bobbens@2490
   287
   SDL_haptics[i] = haptic;
bobbens@2490
   288
bobbens@2490
   289
   return haptic;
bobbens@2489
   290
}
bobbens@2489
   291
bobbens@2489
   292
bobbens@2489
   293
/*
bobbens@2475
   294
 * Closes a SDL_Haptic device.
bobbens@2475
   295
 */
bobbens@2475
   296
void
bobbens@2475
   297
SDL_HapticClose(SDL_Haptic * haptic)
bobbens@2475
   298
{
bobbens@2476
   299
   int i;
bobbens@2476
   300
bobbens@2476
   301
   /* Must be valid */
bobbens@2476
   302
   if (!ValidHaptic(&haptic)) {
bobbens@2476
   303
      return;
bobbens@2476
   304
   }
bobbens@2476
   305
bobbens@2476
   306
   /* Check if it's still in use */
bobbens@2476
   307
   if (--haptic->ref_count < 0) {
bobbens@2476
   308
      return;
bobbens@2476
   309
   }
bobbens@2476
   310
bobbens@2480
   311
   /* Close it, properly removing effects if needed */
bobbens@2480
   312
   for (i=0; i<haptic->neffects; i++) {
bobbens@2480
   313
      if (haptic->effects[i].hweffect != NULL) {
bobbens@2480
   314
         SDL_HapticDestroyEffect(haptic,i);
bobbens@2480
   315
      }
bobbens@2480
   316
   }
bobbens@2476
   317
   SDL_SYS_HapticClose(haptic);
bobbens@2476
   318
bobbens@2476
   319
   /* Remove from the list */
bobbens@2476
   320
   for (i = 0; SDL_haptics[i]; ++i) {
bobbens@2476
   321
      if (haptic == SDL_haptics[i]) {
bobbens@2476
   322
         SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
bobbens@2476
   323
               (SDL_numhaptics - i) * sizeof(haptic));
bobbens@2476
   324
         break;
bobbens@2476
   325
      }
bobbens@2476
   326
   }
bobbens@2476
   327
bobbens@2476
   328
   /* Free */
bobbens@2476
   329
   SDL_free(haptic);
bobbens@2475
   330
}
bobbens@2475
   331
bobbens@2475
   332
/*
bobbens@2475
   333
 * Cleans up after the subsystem.
bobbens@2475
   334
 */
bobbens@2474
   335
void
bobbens@2474
   336
SDL_HapticQuit(void)
bobbens@2474
   337
{
bobbens@2474
   338
   SDL_SYS_HapticQuit();
bobbens@2474
   339
   if (SDL_haptics != NULL) {
bobbens@2474
   340
      SDL_free(SDL_haptics);
bobbens@2474
   341
      SDL_haptics = NULL;
bobbens@2474
   342
   }
bobbens@2514
   343
   SDL_numhaptics = 0;
bobbens@2474
   344
}
bobbens@2477
   345
bobbens@2478
   346
/*
bobbens@2478
   347
 * Returns the number of effects a haptic device has.
bobbens@2478
   348
 */
bobbens@2478
   349
int
bobbens@2478
   350
SDL_HapticNumEffects(SDL_Haptic * haptic)
bobbens@2478
   351
{
bobbens@2478
   352
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   353
      return -1;
bobbens@2478
   354
   }
bobbens@2478
   355
bobbens@2478
   356
   return haptic->neffects;
bobbens@2478
   357
}
bobbens@2478
   358
bobbens@2515
   359
bobbens@2515
   360
/*
bobbens@2515
   361
 * Returns the number of effects a haptic device can play.
bobbens@2515
   362
 */
bobbens@2515
   363
int
bobbens@2515
   364
SDL_HapticNumEffectsPlaying(SDL_Haptic * haptic)
bobbens@2515
   365
{
bobbens@2515
   366
   if (!ValidHaptic(&haptic)) {
bobbens@2515
   367
      return -1;
bobbens@2515
   368
   }
bobbens@2515
   369
bobbens@2515
   370
   return haptic->nplaying;
bobbens@2515
   371
}
bobbens@2515
   372
bobbens@2515
   373
bobbens@2478
   374
/*
bobbens@2478
   375
 * Returns supported effects by the device.
bobbens@2478
   376
 */
bobbens@2478
   377
unsigned int
bobbens@2497
   378
SDL_HapticQuery(SDL_Haptic * haptic)
bobbens@2478
   379
{
bobbens@2478
   380
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   381
      return -1;
bobbens@2478
   382
   }
bobbens@2478
   383
bobbens@2478
   384
   return haptic->supported;
bobbens@2478
   385
}
bobbens@2478
   386
bobbens@2523
   387
bobbens@2523
   388
/*
bobbens@2523
   389
 * Returns the number of axis on the device.
bobbens@2523
   390
 */
bobbens@2523
   391
int
bobbens@2523
   392
SDL_HapticNumAxes(SDL_Haptic * haptic)
bobbens@2523
   393
{
bobbens@2523
   394
   if (!ValidHaptic(&haptic)) {
bobbens@2523
   395
      return -1;
bobbens@2523
   396
   }
bobbens@2523
   397
bobbens@2523
   398
   return haptic->naxes;
bobbens@2523
   399
}
bobbens@2523
   400
bobbens@2486
   401
/*
bobbens@2486
   402
 * Checks to see if the device can support the effect.
bobbens@2486
   403
 */
bobbens@2478
   404
int
bobbens@2478
   405
SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2478
   406
{
bobbens@2478
   407
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   408
      return -1;
bobbens@2478
   409
   }
bobbens@2478
   410
bobbens@2478
   411
   if ((haptic->supported & effect->type) != 0)
bobbens@2478
   412
      return SDL_TRUE;
bobbens@2478
   413
   return SDL_FALSE;
bobbens@2478
   414
}
bobbens@2477
   415
bobbens@2477
   416
/*
bobbens@2477
   417
 * Creates a new haptic effect.
bobbens@2477
   418
 */
bobbens@2477
   419
int
bobbens@2477
   420
SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2477
   421
{
bobbens@2477
   422
   int i;
bobbens@2477
   423
bobbens@2477
   424
   /* Check for device validity. */
bobbens@2477
   425
   if (!ValidHaptic(&haptic)) {
bobbens@2477
   426
      return -1;
bobbens@2477
   427
   }
bobbens@2477
   428
bobbens@2480
   429
   /* Check to see if effect is supported */
bobbens@2480
   430
   if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
bobbens@2513
   431
      SDL_SetError("Haptic: Effect not supported by haptic device.");
bobbens@2480
   432
      return -1;
bobbens@2480
   433
   }
bobbens@2480
   434
bobbens@2477
   435
   /* See if there's a free slot */
bobbens@2477
   436
   for (i=0; i<haptic->neffects; i++) {
bobbens@2477
   437
      if (haptic->effects[i].hweffect == NULL) {
bobbens@2477
   438
bobbens@2477
   439
         /* Now let the backend create the real effect */
bobbens@2480
   440
         if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
bobbens@2477
   441
            return -1; /* Backend failed to create effect */
bobbens@2477
   442
         }
bobbens@2477
   443
         return i;
bobbens@2477
   444
      }
bobbens@2477
   445
   }
bobbens@2477
   446
bobbens@2513
   447
   SDL_SetError("Haptic: Device has no free space left.");
bobbens@2477
   448
   return -1;
bobbens@2477
   449
}
bobbens@2477
   450
bobbens@2477
   451
/*
bobbens@2480
   452
 * Checks to see if an effect is valid.
bobbens@2480
   453
 */
bobbens@2480
   454
static int
bobbens@2480
   455
ValidEffect(SDL_Haptic * haptic, int effect)
bobbens@2480
   456
{
bobbens@2480
   457
   if ((effect < 0) || (effect >= haptic->neffects)) {
bobbens@2513
   458
      SDL_SetError("Haptic: Invalid effect identifier.");
bobbens@2480
   459
      return 0;
bobbens@2480
   460
   }
bobbens@2480
   461
   return 1;
bobbens@2480
   462
}
bobbens@2480
   463
bobbens@2480
   464
/*
bobbens@2488
   465
 * Updates an effect.
bobbens@2488
   466
 */
bobbens@2488
   467
int
bobbens@2488
   468
SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data)
bobbens@2488
   469
{
bobbens@2488
   470
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2488
   471
      return -1;
bobbens@2488
   472
   }
bobbens@2488
   473
bobbens@2488
   474
   /* Updates the effect */
bobbens@2488
   475
   if (SDL_SYS_HapticUpdateEffect(haptic,&haptic->effects[effect],data) < 0) {
bobbens@2488
   476
      return -1;
bobbens@2488
   477
   }
bobbens@2488
   478
bobbens@2488
   479
   return 0;
bobbens@2488
   480
}
bobbens@2488
   481
bobbens@2488
   482
bobbens@2488
   483
/*
bobbens@2477
   484
 * Runs the haptic effect on the device.
bobbens@2477
   485
 */
bobbens@2477
   486
int
bobbens@2519
   487
SDL_HapticRunEffect(SDL_Haptic * haptic, int effect, Uint32 iterations)
bobbens@2477
   488
{
bobbens@2480
   489
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   490
      return -1;
bobbens@2477
   491
   }
bobbens@2479
   492
bobbens@2479
   493
   /* Run the effect */
bobbens@2517
   494
   if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect], iterations) < 0) {
bobbens@2479
   495
      return -1;
bobbens@2479
   496
   }
bobbens@2479
   497
bobbens@2479
   498
   return 0;
bobbens@2477
   499
}
bobbens@2477
   500
bobbens@2477
   501
/*
bobbens@2485
   502
 * Stops the haptic effect on the device.
bobbens@2485
   503
 */
bobbens@2485
   504
int
bobbens@2485
   505
SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
bobbens@2485
   506
{
bobbens@2485
   507
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2485
   508
      return -1;
bobbens@2485
   509
   }
bobbens@2485
   510
bobbens@2485
   511
   /* Stop the effect */
bobbens@2485
   512
   if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) {
bobbens@2485
   513
      return -1;
bobbens@2485
   514
   }
bobbens@2485
   515
bobbens@2485
   516
   return 0;
bobbens@2485
   517
}
bobbens@2485
   518
bobbens@2485
   519
/*
bobbens@2477
   520
 * Gets rid of a haptic effect.
bobbens@2477
   521
 */
bobbens@2477
   522
void
bobbens@2477
   523
SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
bobbens@2477
   524
{
bobbens@2480
   525
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   526
      return;
bobbens@2477
   527
   }
bobbens@2479
   528
bobbens@2479
   529
   /* Not allocated */
bobbens@2479
   530
   if (haptic->effects[effect].hweffect == NULL) {
bobbens@2479
   531
      return;
bobbens@2479
   532
   }
bobbens@2479
   533
bobbens@2479
   534
   SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
bobbens@2477
   535
}
bobbens@2477
   536
bobbens@2483
   537
/*
bobbens@2495
   538
 * Gets the status of a haptic effect.
bobbens@2495
   539
 */
bobbens@2495
   540
int
bobbens@2495
   541
SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect)
bobbens@2495
   542
{
bobbens@2495
   543
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2495
   544
      return -1;
bobbens@2495
   545
   }
bobbens@2495
   546
bobbens@2495
   547
   if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
bobbens@2513
   548
      SDL_SetError("Haptic: Device does not support status queries.");
bobbens@2495
   549
      return -1;
bobbens@2495
   550
   }
bobbens@2495
   551
bobbens@2495
   552
   return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
bobbens@2495
   553
}
bobbens@2495
   554
bobbens@2495
   555
/*
bobbens@2483
   556
 * Sets the global gain of the device.
bobbens@2483
   557
 */
bobbens@2483
   558
int
bobbens@2483
   559
SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
bobbens@2483
   560
{
bobbens@2507
   561
   const char *env;
bobbens@2507
   562
   int real_gain, max_gain;
bobbens@2507
   563
bobbens@2483
   564
   if (!ValidHaptic(&haptic)) {
bobbens@2483
   565
      return -1;
bobbens@2483
   566
   }
bobbens@2477
   567
bobbens@2484
   568
   if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
bobbens@2513
   569
      SDL_SetError("Haptic: Device does not support setting gain.");
bobbens@2484
   570
      return -1;
bobbens@2484
   571
   }
bobbens@2484
   572
bobbens@2483
   573
   if ((gain < 0) || (gain > 100)) {
bobbens@2513
   574
      SDL_SetError("Haptic: Gain must be between 0 and 100.");
bobbens@2483
   575
      return -1;
bobbens@2483
   576
   }
bobbens@2483
   577
bobbens@2507
   578
   /* We use the envvar to get the maximum gain. */
bobbens@2507
   579
   env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
bobbens@2507
   580
   if (env != NULL) {
bobbens@2507
   581
      max_gain = SDL_atoi(env);
bobbens@2507
   582
bobbens@2507
   583
      /* Check for sanity. */
bobbens@2507
   584
      if (max_gain < 0) max_gain = 0;
bobbens@2507
   585
      else if (max_gain > 100) max_gain = 100;
bobbens@2507
   586
bobbens@2507
   587
      /* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
bobbens@2507
   588
      real_gain = (gain * max_gain) / 100;
bobbens@2507
   589
   }
bobbens@2507
   590
   else {
bobbens@2507
   591
      real_gain = gain;
bobbens@2507
   592
   }
bobbens@2507
   593
bobbens@2507
   594
   if (SDL_SYS_HapticSetGain(haptic,real_gain) < 0) {
bobbens@2483
   595
      return -1;
bobbens@2483
   596
   }
bobbens@2483
   597
bobbens@2483
   598
   return 0;
bobbens@2483
   599
}
bobbens@2483
   600
bobbens@2484
   601
/*
bobbens@2484
   602
 * Makes the device autocenter, 0 disables.
bobbens@2484
   603
 */
bobbens@2484
   604
int
bobbens@2484
   605
SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
bobbens@2484
   606
{
bobbens@2484
   607
   if (!ValidHaptic(&haptic)) {
bobbens@2484
   608
      return -1;
bobbens@2484
   609
   }
bobbens@2483
   610
bobbens@2484
   611
   if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
bobbens@2513
   612
      SDL_SetError("Haptic: Device does not support setting autocenter.");
bobbens@2484
   613
      return -1;
bobbens@2484
   614
   }
bobbens@2484
   615
bobbens@2484
   616
   if ((autocenter < 0) || (autocenter > 100)) {
bobbens@2513
   617
      SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
bobbens@2484
   618
      return -1;
bobbens@2484
   619
   }                                           
bobbens@2484
   620
bobbens@2484
   621
   if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) {
bobbens@2484
   622
      return -1;
bobbens@2484
   623
   }
bobbens@2484
   624
bobbens@2484
   625
   return 0;
bobbens@2484
   626
}
bobbens@2484
   627
bobbens@2484
   628