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