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