src/haptic/SDL_haptic.c
author Edgar Simo <bobbens@gmail.com>
Thu, 10 Jul 2008 16:42:43 +0000
branchgsoc2008_force_feedback
changeset 2514 840e1b6325c0
parent 2513 55fd9103a330
child 2515 030fc4375e63
permissions -rw-r--r--
Minor corrections.
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@2478
   359
/*
bobbens@2478
   360
 * Returns supported effects by the device.
bobbens@2478
   361
 */
bobbens@2478
   362
unsigned int
bobbens@2497
   363
SDL_HapticQuery(SDL_Haptic * haptic)
bobbens@2478
   364
{
bobbens@2478
   365
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   366
      return -1;
bobbens@2478
   367
   }
bobbens@2478
   368
bobbens@2478
   369
   return haptic->supported;
bobbens@2478
   370
}
bobbens@2478
   371
bobbens@2486
   372
/*
bobbens@2486
   373
 * Checks to see if the device can support the effect.
bobbens@2486
   374
 */
bobbens@2478
   375
int
bobbens@2478
   376
SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2478
   377
{
bobbens@2478
   378
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   379
      return -1;
bobbens@2478
   380
   }
bobbens@2478
   381
bobbens@2478
   382
   if ((haptic->supported & effect->type) != 0)
bobbens@2478
   383
      return SDL_TRUE;
bobbens@2478
   384
   return SDL_FALSE;
bobbens@2478
   385
}
bobbens@2477
   386
bobbens@2477
   387
/*
bobbens@2477
   388
 * Creates a new haptic effect.
bobbens@2477
   389
 */
bobbens@2477
   390
int
bobbens@2477
   391
SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2477
   392
{
bobbens@2477
   393
   int i;
bobbens@2477
   394
bobbens@2477
   395
   /* Check for device validity. */
bobbens@2477
   396
   if (!ValidHaptic(&haptic)) {
bobbens@2477
   397
      return -1;
bobbens@2477
   398
   }
bobbens@2477
   399
bobbens@2480
   400
   /* Check to see if effect is supported */
bobbens@2480
   401
   if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
bobbens@2513
   402
      SDL_SetError("Haptic: Effect not supported by haptic device.");
bobbens@2480
   403
      return -1;
bobbens@2480
   404
   }
bobbens@2480
   405
bobbens@2477
   406
   /* See if there's a free slot */
bobbens@2477
   407
   for (i=0; i<haptic->neffects; i++) {
bobbens@2477
   408
      if (haptic->effects[i].hweffect == NULL) {
bobbens@2477
   409
bobbens@2477
   410
         /* Now let the backend create the real effect */
bobbens@2480
   411
         if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
bobbens@2477
   412
            return -1; /* Backend failed to create effect */
bobbens@2477
   413
         }
bobbens@2477
   414
         return i;
bobbens@2477
   415
      }
bobbens@2477
   416
   }
bobbens@2477
   417
bobbens@2513
   418
   SDL_SetError("Haptic: Device has no free space left.");
bobbens@2477
   419
   return -1;
bobbens@2477
   420
}
bobbens@2477
   421
bobbens@2477
   422
/*
bobbens@2480
   423
 * Checks to see if an effect is valid.
bobbens@2480
   424
 */
bobbens@2480
   425
static int
bobbens@2480
   426
ValidEffect(SDL_Haptic * haptic, int effect)
bobbens@2480
   427
{
bobbens@2480
   428
   if ((effect < 0) || (effect >= haptic->neffects)) {
bobbens@2513
   429
      SDL_SetError("Haptic: Invalid effect identifier.");
bobbens@2480
   430
      return 0;
bobbens@2480
   431
   }
bobbens@2480
   432
   return 1;
bobbens@2480
   433
}
bobbens@2480
   434
bobbens@2480
   435
/*
bobbens@2488
   436
 * Updates an effect.
bobbens@2488
   437
 */
bobbens@2488
   438
int
bobbens@2488
   439
SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data)
bobbens@2488
   440
{
bobbens@2488
   441
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2488
   442
      return -1;
bobbens@2488
   443
   }
bobbens@2488
   444
bobbens@2488
   445
   /* Updates the effect */
bobbens@2488
   446
   if (SDL_SYS_HapticUpdateEffect(haptic,&haptic->effects[effect],data) < 0) {
bobbens@2488
   447
      return -1;
bobbens@2488
   448
   }
bobbens@2488
   449
bobbens@2488
   450
   return 0;
bobbens@2488
   451
}
bobbens@2488
   452
bobbens@2488
   453
bobbens@2488
   454
/*
bobbens@2477
   455
 * Runs the haptic effect on the device.
bobbens@2477
   456
 */
bobbens@2477
   457
int
bobbens@2477
   458
SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
bobbens@2477
   459
{
bobbens@2480
   460
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   461
      return -1;
bobbens@2477
   462
   }
bobbens@2479
   463
bobbens@2479
   464
   /* Run the effect */
bobbens@2479
   465
   if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) {
bobbens@2479
   466
      return -1;
bobbens@2479
   467
   }
bobbens@2479
   468
bobbens@2479
   469
   return 0;
bobbens@2477
   470
}
bobbens@2477
   471
bobbens@2477
   472
/*
bobbens@2485
   473
 * Stops the haptic effect on the device.
bobbens@2485
   474
 */
bobbens@2485
   475
int
bobbens@2485
   476
SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
bobbens@2485
   477
{
bobbens@2485
   478
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2485
   479
      return -1;
bobbens@2485
   480
   }
bobbens@2485
   481
bobbens@2485
   482
   /* Stop the effect */
bobbens@2485
   483
   if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) {
bobbens@2485
   484
      return -1;
bobbens@2485
   485
   }
bobbens@2485
   486
bobbens@2485
   487
   return 0;
bobbens@2485
   488
}
bobbens@2485
   489
bobbens@2485
   490
/*
bobbens@2477
   491
 * Gets rid of a haptic effect.
bobbens@2477
   492
 */
bobbens@2477
   493
void
bobbens@2477
   494
SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
bobbens@2477
   495
{
bobbens@2480
   496
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   497
      return;
bobbens@2477
   498
   }
bobbens@2479
   499
bobbens@2479
   500
   /* Not allocated */
bobbens@2479
   501
   if (haptic->effects[effect].hweffect == NULL) {
bobbens@2479
   502
      return;
bobbens@2479
   503
   }
bobbens@2479
   504
bobbens@2479
   505
   SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
bobbens@2477
   506
}
bobbens@2477
   507
bobbens@2483
   508
/*
bobbens@2495
   509
 * Gets the status of a haptic effect.
bobbens@2495
   510
 */
bobbens@2495
   511
int
bobbens@2495
   512
SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect)
bobbens@2495
   513
{
bobbens@2495
   514
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2495
   515
      return -1;
bobbens@2495
   516
   }
bobbens@2495
   517
bobbens@2495
   518
   if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
bobbens@2513
   519
      SDL_SetError("Haptic: Device does not support status queries.");
bobbens@2495
   520
      return -1;
bobbens@2495
   521
   }
bobbens@2495
   522
bobbens@2495
   523
   return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
bobbens@2495
   524
}
bobbens@2495
   525
bobbens@2495
   526
/*
bobbens@2483
   527
 * Sets the global gain of the device.
bobbens@2483
   528
 */
bobbens@2483
   529
int
bobbens@2483
   530
SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
bobbens@2483
   531
{
bobbens@2507
   532
   const char *env;
bobbens@2507
   533
   int real_gain, max_gain;
bobbens@2507
   534
bobbens@2483
   535
   if (!ValidHaptic(&haptic)) {
bobbens@2483
   536
      return -1;
bobbens@2483
   537
   }
bobbens@2477
   538
bobbens@2484
   539
   if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
bobbens@2513
   540
      SDL_SetError("Haptic: Device does not support setting gain.");
bobbens@2484
   541
      return -1;
bobbens@2484
   542
   }
bobbens@2484
   543
bobbens@2483
   544
   if ((gain < 0) || (gain > 100)) {
bobbens@2513
   545
      SDL_SetError("Haptic: Gain must be between 0 and 100.");
bobbens@2483
   546
      return -1;
bobbens@2483
   547
   }
bobbens@2483
   548
bobbens@2507
   549
   /* We use the envvar to get the maximum gain. */
bobbens@2507
   550
   env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
bobbens@2507
   551
   if (env != NULL) {
bobbens@2507
   552
      max_gain = SDL_atoi(env);
bobbens@2507
   553
bobbens@2507
   554
      /* Check for sanity. */
bobbens@2507
   555
      if (max_gain < 0) max_gain = 0;
bobbens@2507
   556
      else if (max_gain > 100) max_gain = 100;
bobbens@2507
   557
bobbens@2507
   558
      /* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
bobbens@2507
   559
      real_gain = (gain * max_gain) / 100;
bobbens@2507
   560
   }
bobbens@2507
   561
   else {
bobbens@2507
   562
      real_gain = gain;
bobbens@2507
   563
   }
bobbens@2507
   564
bobbens@2507
   565
   if (SDL_SYS_HapticSetGain(haptic,real_gain) < 0) {
bobbens@2483
   566
      return -1;
bobbens@2483
   567
   }
bobbens@2483
   568
bobbens@2483
   569
   return 0;
bobbens@2483
   570
}
bobbens@2483
   571
bobbens@2484
   572
/*
bobbens@2484
   573
 * Makes the device autocenter, 0 disables.
bobbens@2484
   574
 */
bobbens@2484
   575
int
bobbens@2484
   576
SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
bobbens@2484
   577
{
bobbens@2484
   578
   if (!ValidHaptic(&haptic)) {
bobbens@2484
   579
      return -1;
bobbens@2484
   580
   }
bobbens@2483
   581
bobbens@2484
   582
   if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
bobbens@2513
   583
      SDL_SetError("Haptic: Device does not support setting autocenter.");
bobbens@2484
   584
      return -1;
bobbens@2484
   585
   }
bobbens@2484
   586
bobbens@2484
   587
   if ((autocenter < 0) || (autocenter > 100)) {
bobbens@2513
   588
      SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
bobbens@2484
   589
      return -1;
bobbens@2484
   590
   }                                           
bobbens@2484
   591
bobbens@2484
   592
   if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) {
bobbens@2484
   593
      return -1;
bobbens@2484
   594
   }
bobbens@2484
   595
bobbens@2484
   596
   return 0;
bobbens@2484
   597
}
bobbens@2484
   598
bobbens@2484
   599