src/haptic/SDL_haptic.c
author Edgar Simo <bobbens@gmail.com>
Tue, 08 Jul 2008 19:55:12 +0000
branchgsoc2008_force_feedback
changeset 2507 8ef1d0f4d0c1
parent 2506 ba8e99fe92c1
child 2512 ef147ee4896c
permissions -rw-r--r--
Gain is set to max on haptic device open.
Autocenter is disabled on haptic device open.
Maximum gain can be set by SDL_HAPTIC_GAIN_MAX.
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@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@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@2489
   191
 * Returns SDL_TRUE if joystick has haptic features.
bobbens@2489
   192
 */
bobbens@2489
   193
int
bobbens@2489
   194
SDL_JoystickIsHaptic(SDL_Joystick * joystick)
bobbens@2489
   195
{
bobbens@2489
   196
   int ret;
bobbens@2489
   197
bobbens@2490
   198
   /* Must be a valid joystick */
bobbens@2489
   199
   if (!SDL_PrivateJoystickValid(&joystick)) {
bobbens@2489
   200
      return -1;
bobbens@2489
   201
   }
bobbens@2489
   202
bobbens@2489
   203
   ret = SDL_SYS_JoystickIsHaptic(joystick);
bobbens@2489
   204
bobbens@2489
   205
   if (ret > 0) return SDL_TRUE;
bobbens@2489
   206
   else if (ret == 0) return SDL_FALSE;
bobbens@2489
   207
   else return -1;
bobbens@2489
   208
}
bobbens@2489
   209
bobbens@2489
   210
bobbens@2489
   211
/*
bobbens@2489
   212
 * Opens a haptic device from a joystick.
bobbens@2489
   213
 */
bobbens@2489
   214
SDL_Haptic *
bobbens@2489
   215
SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
bobbens@2489
   216
{
bobbens@2490
   217
   int i;
bobbens@2490
   218
   SDL_Haptic *haptic;
bobbens@2490
   219
bobbens@2490
   220
   /* Must be a valid joystick */
bobbens@2489
   221
   if (!SDL_PrivateJoystickValid(&joystick)) {
bobbens@2490
   222
      return NULL;
bobbens@2489
   223
   }
bobbens@2490
   224
bobbens@2490
   225
   /* Joystick must be haptic */
bobbens@2490
   226
   if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
bobbens@2490
   227
      return NULL;
bobbens@2490
   228
   }
bobbens@2490
   229
bobbens@2490
   230
   /* Check to see if joystick's haptic is already open */
bobbens@2490
   231
   for (i=0; SDL_haptics[i]; i++) {
bobbens@2491
   232
      if (SDL_SYS_JoystickSameHaptic(SDL_haptics[i],joystick)) {
bobbens@2490
   233
         haptic = SDL_haptics[i];
bobbens@2490
   234
         ++haptic->ref_count;
bobbens@2490
   235
         return haptic;
bobbens@2490
   236
      }
bobbens@2490
   237
   }
bobbens@2490
   238
bobbens@2490
   239
   /* Create the haptic device */
bobbens@2490
   240
   haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
bobbens@2490
   241
   if (haptic == NULL) {
bobbens@2490
   242
      SDL_OutOfMemory();
bobbens@2490
   243
      return NULL;
bobbens@2490
   244
   }
bobbens@2490
   245
bobbens@2490
   246
   /* Initialize the haptic device */
bobbens@2490
   247
   SDL_memset(haptic, 0, (sizeof *haptic));
bobbens@2490
   248
   if (SDL_SYS_HapticOpenFromJoystick(haptic,joystick) < 0) {
bobbens@2490
   249
      SDL_free(haptic);
bobbens@2490
   250
      return NULL;
bobbens@2490
   251
   }
bobbens@2490
   252
bobbens@2490
   253
   /* Add haptic to list */
bobbens@2490
   254
   ++haptic->ref_count;
bobbens@2490
   255
   for (i=0; SDL_haptics[i]; i++)
bobbens@2490
   256
      /* Skip to next haptic */ ;
bobbens@2490
   257
   SDL_haptics[i] = haptic;
bobbens@2490
   258
bobbens@2490
   259
   return haptic;
bobbens@2489
   260
}
bobbens@2489
   261
bobbens@2489
   262
bobbens@2489
   263
/*
bobbens@2475
   264
 * Closes a SDL_Haptic device.
bobbens@2475
   265
 */
bobbens@2475
   266
void
bobbens@2475
   267
SDL_HapticClose(SDL_Haptic * haptic)
bobbens@2475
   268
{
bobbens@2476
   269
   int i;
bobbens@2476
   270
bobbens@2476
   271
   /* Must be valid */
bobbens@2476
   272
   if (!ValidHaptic(&haptic)) {
bobbens@2476
   273
      return;
bobbens@2476
   274
   }
bobbens@2476
   275
bobbens@2476
   276
   /* Check if it's still in use */
bobbens@2476
   277
   if (--haptic->ref_count < 0) {
bobbens@2476
   278
      return;
bobbens@2476
   279
   }
bobbens@2476
   280
bobbens@2480
   281
   /* Close it, properly removing effects if needed */
bobbens@2480
   282
   for (i=0; i<haptic->neffects; i++) {
bobbens@2480
   283
      if (haptic->effects[i].hweffect != NULL) {
bobbens@2480
   284
         SDL_HapticDestroyEffect(haptic,i);
bobbens@2480
   285
      }
bobbens@2480
   286
   }
bobbens@2476
   287
   SDL_SYS_HapticClose(haptic);
bobbens@2476
   288
bobbens@2476
   289
   /* Remove from the list */
bobbens@2476
   290
   for (i = 0; SDL_haptics[i]; ++i) {
bobbens@2476
   291
      if (haptic == SDL_haptics[i]) {
bobbens@2476
   292
         SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
bobbens@2476
   293
               (SDL_numhaptics - i) * sizeof(haptic));
bobbens@2476
   294
         break;
bobbens@2476
   295
      }
bobbens@2476
   296
   }
bobbens@2476
   297
bobbens@2476
   298
   /* Free */
bobbens@2476
   299
   SDL_free(haptic);
bobbens@2475
   300
}
bobbens@2475
   301
bobbens@2475
   302
/*
bobbens@2475
   303
 * Cleans up after the subsystem.
bobbens@2475
   304
 */
bobbens@2474
   305
void
bobbens@2474
   306
SDL_HapticQuit(void)
bobbens@2474
   307
{
bobbens@2474
   308
   SDL_numhaptics = 0;
bobbens@2474
   309
bobbens@2474
   310
   SDL_SYS_HapticQuit();
bobbens@2474
   311
   if (SDL_haptics != NULL) {
bobbens@2474
   312
      SDL_free(SDL_haptics);
bobbens@2474
   313
      SDL_haptics = NULL;
bobbens@2474
   314
   }
bobbens@2474
   315
}
bobbens@2477
   316
bobbens@2478
   317
/*
bobbens@2478
   318
 * Returns the number of effects a haptic device has.
bobbens@2478
   319
 */
bobbens@2478
   320
int
bobbens@2478
   321
SDL_HapticNumEffects(SDL_Haptic * haptic)
bobbens@2478
   322
{
bobbens@2478
   323
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   324
      return -1;
bobbens@2478
   325
   }
bobbens@2478
   326
bobbens@2478
   327
   return haptic->neffects;
bobbens@2478
   328
}
bobbens@2478
   329
bobbens@2478
   330
/*
bobbens@2478
   331
 * Returns supported effects by the device.
bobbens@2478
   332
 */
bobbens@2478
   333
unsigned int
bobbens@2497
   334
SDL_HapticQuery(SDL_Haptic * haptic)
bobbens@2478
   335
{
bobbens@2478
   336
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   337
      return -1;
bobbens@2478
   338
   }
bobbens@2478
   339
bobbens@2478
   340
   return haptic->supported;
bobbens@2478
   341
}
bobbens@2478
   342
bobbens@2486
   343
/*
bobbens@2486
   344
 * Checks to see if the device can support the effect.
bobbens@2486
   345
 */
bobbens@2478
   346
int
bobbens@2478
   347
SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2478
   348
{
bobbens@2478
   349
   if (!ValidHaptic(&haptic)) {
bobbens@2478
   350
      return -1;
bobbens@2478
   351
   }
bobbens@2478
   352
bobbens@2478
   353
   if ((haptic->supported & effect->type) != 0)
bobbens@2478
   354
      return SDL_TRUE;
bobbens@2478
   355
   return SDL_FALSE;
bobbens@2478
   356
}
bobbens@2477
   357
bobbens@2477
   358
/*
bobbens@2477
   359
 * Creates a new haptic effect.
bobbens@2477
   360
 */
bobbens@2477
   361
int
bobbens@2477
   362
SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
bobbens@2477
   363
{
bobbens@2477
   364
   int i;
bobbens@2477
   365
bobbens@2477
   366
   /* Check for device validity. */
bobbens@2477
   367
   if (!ValidHaptic(&haptic)) {
bobbens@2477
   368
      return -1;
bobbens@2477
   369
   }
bobbens@2477
   370
bobbens@2480
   371
   /* Check to see if effect is supported */
bobbens@2480
   372
   if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
bobbens@2480
   373
      SDL_SetError("Haptic effect not supported by haptic device.");
bobbens@2480
   374
      return -1;
bobbens@2480
   375
   }
bobbens@2480
   376
bobbens@2477
   377
   /* See if there's a free slot */
bobbens@2477
   378
   for (i=0; i<haptic->neffects; i++) {
bobbens@2477
   379
      if (haptic->effects[i].hweffect == NULL) {
bobbens@2477
   380
bobbens@2477
   381
         /* Now let the backend create the real effect */
bobbens@2480
   382
         if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
bobbens@2477
   383
            return -1; /* Backend failed to create effect */
bobbens@2477
   384
         }
bobbens@2477
   385
         return i;
bobbens@2477
   386
      }
bobbens@2477
   387
   }
bobbens@2477
   388
bobbens@2477
   389
   SDL_SetError("Haptic device has no free space left.");
bobbens@2477
   390
   return -1;
bobbens@2477
   391
}
bobbens@2477
   392
bobbens@2477
   393
/*
bobbens@2480
   394
 * Checks to see if an effect is valid.
bobbens@2480
   395
 */
bobbens@2480
   396
static int
bobbens@2480
   397
ValidEffect(SDL_Haptic * haptic, int effect)
bobbens@2480
   398
{
bobbens@2480
   399
   if ((effect < 0) || (effect >= haptic->neffects)) {
bobbens@2480
   400
      SDL_SetError("Invalid haptic effect identifier.");
bobbens@2480
   401
      return 0;
bobbens@2480
   402
   }
bobbens@2480
   403
   return 1;
bobbens@2480
   404
}
bobbens@2480
   405
bobbens@2480
   406
/*
bobbens@2488
   407
 * Updates an effect.
bobbens@2488
   408
 */
bobbens@2488
   409
int
bobbens@2488
   410
SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data)
bobbens@2488
   411
{
bobbens@2488
   412
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2488
   413
      return -1;
bobbens@2488
   414
   }
bobbens@2488
   415
bobbens@2488
   416
   /* Updates the effect */
bobbens@2488
   417
   if (SDL_SYS_HapticUpdateEffect(haptic,&haptic->effects[effect],data) < 0) {
bobbens@2488
   418
      return -1;
bobbens@2488
   419
   }
bobbens@2488
   420
bobbens@2488
   421
   return 0;
bobbens@2488
   422
}
bobbens@2488
   423
bobbens@2488
   424
bobbens@2488
   425
/*
bobbens@2477
   426
 * Runs the haptic effect on the device.
bobbens@2477
   427
 */
bobbens@2477
   428
int
bobbens@2477
   429
SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
bobbens@2477
   430
{
bobbens@2480
   431
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   432
      return -1;
bobbens@2477
   433
   }
bobbens@2479
   434
bobbens@2479
   435
   /* Run the effect */
bobbens@2479
   436
   if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) {
bobbens@2479
   437
      return -1;
bobbens@2479
   438
   }
bobbens@2479
   439
bobbens@2479
   440
   return 0;
bobbens@2477
   441
}
bobbens@2477
   442
bobbens@2477
   443
/*
bobbens@2485
   444
 * Stops the haptic effect on the device.
bobbens@2485
   445
 */
bobbens@2485
   446
int
bobbens@2485
   447
SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
bobbens@2485
   448
{
bobbens@2485
   449
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2485
   450
      return -1;
bobbens@2485
   451
   }
bobbens@2485
   452
bobbens@2485
   453
   /* Stop the effect */
bobbens@2485
   454
   if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) {
bobbens@2485
   455
      return -1;
bobbens@2485
   456
   }
bobbens@2485
   457
bobbens@2485
   458
   return 0;
bobbens@2485
   459
}
bobbens@2485
   460
bobbens@2485
   461
/*
bobbens@2477
   462
 * Gets rid of a haptic effect.
bobbens@2477
   463
 */
bobbens@2477
   464
void
bobbens@2477
   465
SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
bobbens@2477
   466
{
bobbens@2480
   467
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2477
   468
      return;
bobbens@2477
   469
   }
bobbens@2479
   470
bobbens@2479
   471
   /* Not allocated */
bobbens@2479
   472
   if (haptic->effects[effect].hweffect == NULL) {
bobbens@2479
   473
      return;
bobbens@2479
   474
   }
bobbens@2479
   475
bobbens@2479
   476
   SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
bobbens@2477
   477
}
bobbens@2477
   478
bobbens@2483
   479
/*
bobbens@2495
   480
 * Gets the status of a haptic effect.
bobbens@2495
   481
 */
bobbens@2495
   482
int
bobbens@2495
   483
SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect)
bobbens@2495
   484
{
bobbens@2495
   485
   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
bobbens@2495
   486
      return -1;
bobbens@2495
   487
   }
bobbens@2495
   488
bobbens@2495
   489
   if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
bobbens@2495
   490
      SDL_SetError("Haptic device does not support status queries.");
bobbens@2495
   491
      return -1;
bobbens@2495
   492
   }
bobbens@2495
   493
bobbens@2495
   494
   return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
bobbens@2495
   495
}
bobbens@2495
   496
bobbens@2495
   497
/*
bobbens@2483
   498
 * Sets the global gain of the device.
bobbens@2483
   499
 */
bobbens@2483
   500
int
bobbens@2483
   501
SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
bobbens@2483
   502
{
bobbens@2507
   503
   const char *env;
bobbens@2507
   504
   int real_gain, max_gain;
bobbens@2507
   505
bobbens@2483
   506
   if (!ValidHaptic(&haptic)) {
bobbens@2483
   507
      return -1;
bobbens@2483
   508
   }
bobbens@2477
   509
bobbens@2484
   510
   if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
bobbens@2484
   511
      SDL_SetError("Haptic device does not support setting gain.");
bobbens@2484
   512
      return -1;
bobbens@2484
   513
   }
bobbens@2484
   514
bobbens@2483
   515
   if ((gain < 0) || (gain > 100)) {
bobbens@2483
   516
      SDL_SetError("Haptic gain must be between 0 and 100.");
bobbens@2483
   517
      return -1;
bobbens@2483
   518
   }
bobbens@2483
   519
bobbens@2507
   520
   /* We use the envvar to get the maximum gain. */
bobbens@2507
   521
   env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
bobbens@2507
   522
   if (env != NULL) {
bobbens@2507
   523
      max_gain = SDL_atoi(env);
bobbens@2507
   524
bobbens@2507
   525
      /* Check for sanity. */
bobbens@2507
   526
      if (max_gain < 0) max_gain = 0;
bobbens@2507
   527
      else if (max_gain > 100) max_gain = 100;
bobbens@2507
   528
bobbens@2507
   529
      /* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
bobbens@2507
   530
      real_gain = (gain * max_gain) / 100;
bobbens@2507
   531
   }
bobbens@2507
   532
   else {
bobbens@2507
   533
      real_gain = gain;
bobbens@2507
   534
   }
bobbens@2507
   535
bobbens@2507
   536
   if (SDL_SYS_HapticSetGain(haptic,real_gain) < 0) {
bobbens@2483
   537
      return -1;
bobbens@2483
   538
   }
bobbens@2483
   539
bobbens@2483
   540
   return 0;
bobbens@2483
   541
}
bobbens@2483
   542
bobbens@2484
   543
/*
bobbens@2484
   544
 * Makes the device autocenter, 0 disables.
bobbens@2484
   545
 */
bobbens@2484
   546
int
bobbens@2484
   547
SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
bobbens@2484
   548
{
bobbens@2484
   549
   if (!ValidHaptic(&haptic)) {
bobbens@2484
   550
      return -1;
bobbens@2484
   551
   }
bobbens@2483
   552
bobbens@2484
   553
   if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
bobbens@2484
   554
      SDL_SetError("Haptic device does not support setting autocenter.");
bobbens@2484
   555
      return -1;
bobbens@2484
   556
   }
bobbens@2484
   557
bobbens@2484
   558
   if ((autocenter < 0) || (autocenter > 100)) {
bobbens@2484
   559
      SDL_SetError("Haptic autocenter must be between 0 and 100.");
bobbens@2484
   560
      return -1;
bobbens@2484
   561
   }                                           
bobbens@2484
   562
bobbens@2484
   563
   if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) {
bobbens@2484
   564
      return -1;
bobbens@2484
   565
   }
bobbens@2484
   566
bobbens@2484
   567
   return 0;
bobbens@2484
   568
}
bobbens@2484
   569
bobbens@2484
   570