src/haptic/linux/SDL_syshaptic.c
author Edgar Simo <bobbens@gmail.com>
Tue, 01 Jul 2008 18:35:05 +0000
branchgsoc2008_force_feedback
changeset 2488 8e2bdbccf7ff
parent 2487 4c8e25ef2d97
child 2489 96adc8025331
permissions -rw-r--r--
Added SDL_HapticUpdateEffect().
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
#ifdef SDL_JOYSTICK_LINUX
bobbens@2472
    25
bobbens@2472
    26
#include "SDL_haptic.h"
bobbens@2472
    27
#include "../SDL_haptic_c.h"
bobbens@2472
    28
#include "../SDL_syshaptic.h"
bobbens@2472
    29
bobbens@2472
    30
#include <unistd.h> /* close */
bobbens@2472
    31
#include <linux/input.h>
bobbens@2472
    32
#include <sys/ioctl.h>
bobbens@2472
    33
#include <sys/types.h>
bobbens@2472
    34
#include <sys/stat.h>
bobbens@2472
    35
#include <fcntl.h>
bobbens@2472
    36
#include <linux/limits.h>
bobbens@2472
    37
#include <string.h>
bobbens@2472
    38
bobbens@2472
    39
bobbens@2472
    40
#define MAX_HAPTICS  32
bobbens@2472
    41
bobbens@2472
    42
bobbens@2475
    43
/*
bobbens@2475
    44
 * List of available haptic devices.
bobbens@2475
    45
 */
bobbens@2472
    46
static struct
bobbens@2472
    47
{
bobbens@2472
    48
   char *fname;
bobbens@2472
    49
   SDL_Haptic *haptic;
bobbens@2472
    50
} SDL_hapticlist[MAX_HAPTICS];
bobbens@2472
    51
bobbens@2475
    52
bobbens@2475
    53
/*
bobbens@2475
    54
 * Haptic system hardware data.
bobbens@2475
    55
 */
bobbens@2472
    56
struct haptic_hwdata
bobbens@2472
    57
{
bobbens@2472
    58
   int fd;
bobbens@2472
    59
};
bobbens@2472
    60
bobbens@2472
    61
bobbens@2477
    62
/*
bobbens@2477
    63
 * Haptic system effect data.
bobbens@2477
    64
 */
bobbens@2477
    65
struct haptic_hweffect
bobbens@2477
    66
{
bobbens@2479
    67
   struct ff_effect effect;
bobbens@2477
    68
};
bobbens@2477
    69
bobbens@2477
    70
bobbens@2475
    71
bobbens@2472
    72
#define test_bit(nr, addr) \
bobbens@2472
    73
   (((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
bobbens@2472
    74
#define EV_TEST(ev,f) \
bobbens@2472
    75
   if (test_bit((ev), features)) ret |= (f);
bobbens@2475
    76
/*
bobbens@2475
    77
 * Test whether a device has haptic properties.
bobbens@2475
    78
 * Returns available properties or 0 if there are none.
bobbens@2475
    79
 */
bobbens@2472
    80
static int
bobbens@2472
    81
EV_IsHaptic(int fd)
bobbens@2472
    82
{
bobbens@2472
    83
   unsigned int ret;
bobbens@2472
    84
   unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
bobbens@2472
    85
bobbens@2472
    86
   ret = 0;
bobbens@2472
    87
bobbens@2472
    88
   ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features);
bobbens@2472
    89
bobbens@2472
    90
   EV_TEST(FF_CONSTANT,   SDL_HAPTIC_CONSTANT);
bobbens@2487
    91
   EV_TEST(FF_PERIODIC,   SDL_HAPTIC_SINE |
bobbens@2487
    92
                          SDL_HAPTIC_SQUARE |
bobbens@2487
    93
                          SDL_HAPTIC_TRIANGLE |
bobbens@2487
    94
                          SDL_HAPTIC_SAWTOOTHUP |
bobbens@2487
    95
                          SDL_HAPTIC_SAWTOOTHDOWN);
bobbens@2472
    96
   EV_TEST(FF_RAMP,       SDL_HAPTIC_RAMP);
bobbens@2472
    97
   EV_TEST(FF_SPRING,     SDL_HAPTIC_SPRING);
bobbens@2472
    98
   EV_TEST(FF_FRICTION,   SDL_HAPTIC_FRICTION);
bobbens@2472
    99
   EV_TEST(FF_DAMPER,     SDL_HAPTIC_DAMPER);
bobbens@2472
   100
   EV_TEST(FF_INERTIA,    SDL_HAPTIC_INERTIA);
bobbens@2486
   101
   EV_TEST(FF_CUSTOM,     SDL_HAPTIC_CUSTOM);
bobbens@2472
   102
   EV_TEST(FF_GAIN,       SDL_HAPTIC_GAIN);
bobbens@2472
   103
   EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
bobbens@2472
   104
bobbens@2472
   105
   return ret;
bobbens@2472
   106
}
bobbens@2472
   107
bobbens@2472
   108
int
bobbens@2472
   109
SDL_SYS_HapticInit(void)
bobbens@2472
   110
{
bobbens@2472
   111
   const char joydev_pattern[] = "/dev/input/event%d";
bobbens@2472
   112
   dev_t dev_nums[MAX_HAPTICS];
bobbens@2472
   113
   char path[PATH_MAX];
bobbens@2472
   114
   struct stat sb;
bobbens@2472
   115
   int fd;
bobbens@2472
   116
   int i, j, k;
bobbens@2472
   117
   int duplicate;
bobbens@2472
   118
   int numhaptics;
bobbens@2472
   119
bobbens@2472
   120
   numhaptics = 0;
bobbens@2472
   121
bobbens@2472
   122
   i = 0;
bobbens@2472
   123
   for (j = 0; j < MAX_HAPTICS; ++j) {
bobbens@2472
   124
bobbens@2472
   125
      snprintf(path, PATH_MAX, joydev_pattern, i++);
bobbens@2472
   126
bobbens@2472
   127
      /* check to see if file exists */
bobbens@2472
   128
      if (stat(path,&sb) != 0)
bobbens@2472
   129
         break;
bobbens@2472
   130
bobbens@2472
   131
      /* check for duplicates */
bobbens@2472
   132
      duplicate = 0;
bobbens@2472
   133
      for (k = 0; (k < numhaptics) && !duplicate; ++k) {
bobbens@2472
   134
         if (sb.st_rdev == dev_nums[k]) {
bobbens@2472
   135
            duplicate = 1;
bobbens@2472
   136
         }                                                     
bobbens@2472
   137
      }                                                         
bobbens@2472
   138
      if (duplicate) {
bobbens@2472
   139
         continue;
bobbens@2472
   140
      }
bobbens@2472
   141
bobbens@2472
   142
      /* try to open */
bobbens@2472
   143
      fd = open(path, O_RDWR, 0);
bobbens@2472
   144
      if (fd < 0) continue;
bobbens@2472
   145
bobbens@2472
   146
#ifdef DEBUG_INPUT_EVENTS
bobbens@2472
   147
      printf("Checking %s\n", path);
bobbens@2472
   148
#endif          
bobbens@2472
   149
bobbens@2472
   150
      /* see if it works */
bobbens@2472
   151
      if (EV_IsHaptic(fd)!=0) {
bobbens@2472
   152
         SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
bobbens@2472
   153
         SDL_hapticlist[numhaptics].haptic = NULL;
bobbens@2472
   154
         dev_nums[numhaptics] = sb.st_rdev;
bobbens@2472
   155
         ++numhaptics;
bobbens@2472
   156
      }
bobbens@2472
   157
      close(fd);
bobbens@2472
   158
   }
bobbens@2472
   159
bobbens@2472
   160
   return numhaptics;
bobbens@2472
   161
}
bobbens@2472
   162
bobbens@2472
   163
bobbens@2475
   164
/*
bobbens@2475
   165
 * Return the name of a haptic device, does not need to be opened.
bobbens@2475
   166
 */
bobbens@2472
   167
const char *
bobbens@2472
   168
SDL_SYS_HapticName(int index)
bobbens@2472
   169
{
bobbens@2472
   170
   int fd;
bobbens@2472
   171
   static char namebuf[128];
bobbens@2472
   172
   char *name;
bobbens@2472
   173
bobbens@2472
   174
   name = NULL;
bobbens@2472
   175
   fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
bobbens@2472
   176
   if (fd >= 0) {
bobbens@2472
   177
      if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
bobbens@2472
   178
         name = SDL_hapticlist[index].fname;
bobbens@2472
   179
      }
bobbens@2472
   180
      else {
bobbens@2472
   181
         name = namebuf;
bobbens@2472
   182
      }
bobbens@2472
   183
   }
bobbens@2472
   184
   close(fd);
bobbens@2472
   185
bobbens@2472
   186
   return name;
bobbens@2472
   187
}
bobbens@2472
   188
bobbens@2472
   189
bobbens@2475
   190
/*
bobbens@2475
   191
 * Opens a haptic device for usage.
bobbens@2475
   192
 */
bobbens@2472
   193
int
bobbens@2472
   194
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
bobbens@2472
   195
{
bobbens@2477
   196
   int i;
bobbens@2472
   197
   int fd;
bobbens@2472
   198
bobbens@2476
   199
   /* Open the character device */
bobbens@2472
   200
   fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
bobbens@2472
   201
   if (fd < 0) {
bobbens@2472
   202
      SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
bobbens@2477
   203
      return -1;
bobbens@2472
   204
   }
bobbens@2472
   205
bobbens@2476
   206
   /* Allocate the hwdata */
bobbens@2476
   207
   haptic->hwdata = (struct haptic_hwdata *)
bobbens@2477
   208
         SDL_malloc(sizeof(*haptic->hwdata));
bobbens@2476
   209
   if (haptic->hwdata == NULL) {
bobbens@2476
   210
      SDL_OutOfMemory();
bobbens@2477
   211
      goto open_err;
bobbens@2476
   212
   }
bobbens@2476
   213
   SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
bobbens@2478
   214
   /* Set the data */
bobbens@2476
   215
   haptic->hwdata->fd = fd;
bobbens@2478
   216
   haptic->supported = EV_IsHaptic(fd);
bobbens@2472
   217
bobbens@2477
   218
   /* Set the effects */
bobbens@2477
   219
   if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
bobbens@2477
   220
      SDL_SetError("Unable to query haptic device memory.");
bobbens@2477
   221
      goto open_err;
bobbens@2477
   222
   }
bobbens@2477
   223
   haptic->effects = (struct haptic_effect *)
bobbens@2477
   224
         SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
bobbens@2477
   225
   if (haptic->effects == NULL) {
bobbens@2477
   226
      SDL_OutOfMemory();
bobbens@2477
   227
      goto open_err;
bobbens@2477
   228
   }
bobbens@2483
   229
   /* Clear the memory */
bobbens@2483
   230
   SDL_memset(haptic->effects, 0,
bobbens@2483
   231
         sizeof(struct haptic_effect) * haptic->neffects);
bobbens@2477
   232
bobbens@2472
   233
   return 0;
bobbens@2477
   234
bobbens@2477
   235
   /* Error handling */
bobbens@2477
   236
open_err:
bobbens@2477
   237
   close(fd);
bobbens@2477
   238
   if (haptic->hwdata != NULL) {
bobbens@2477
   239
      free(haptic->hwdata);
bobbens@2477
   240
      haptic->hwdata = NULL;
bobbens@2477
   241
   }
bobbens@2477
   242
   return -1;
bobbens@2472
   243
}
bobbens@2472
   244
bobbens@2472
   245
bobbens@2475
   246
/*
bobbens@2475
   247
 * Closes the haptic device.
bobbens@2475
   248
 */
bobbens@2475
   249
void
bobbens@2475
   250
SDL_SYS_HapticClose(SDL_Haptic * haptic)
bobbens@2475
   251
{
bobbens@2476
   252
   if (haptic->hwdata) {
bobbens@2476
   253
bobbens@2476
   254
      /* Clean up */
bobbens@2476
   255
      close(haptic->hwdata->fd);
bobbens@2476
   256
bobbens@2476
   257
      /* Free */
bobbens@2476
   258
      SDL_free(haptic->hwdata);
bobbens@2476
   259
      haptic->hwdata = NULL;
bobbens@2477
   260
      SDL_free(haptic->effects);
bobbens@2477
   261
      haptic->neffects = 0;
bobbens@2476
   262
   }
bobbens@2475
   263
}
bobbens@2475
   264
bobbens@2475
   265
bobbens@2477
   266
/* 
bobbens@2477
   267
 * Clean up after system specific haptic stuff
bobbens@2477
   268
 */
bobbens@2474
   269
void
bobbens@2474
   270
SDL_SYS_HapticQuit(void)
bobbens@2474
   271
{
bobbens@2474
   272
   int i;
bobbens@2474
   273
bobbens@2474
   274
   for (i=0; SDL_hapticlist[i].fname != NULL; i++) {
bobbens@2474
   275
      SDL_free(SDL_hapticlist[i].fname);
bobbens@2474
   276
   }
bobbens@2474
   277
   SDL_hapticlist[0].fname = NULL;
bobbens@2474
   278
}
bobbens@2474
   279
bobbens@2481
   280
#define  CLAMP(x)    (((x) > 32767) ? 32767 : x)
bobbens@2477
   281
/*
bobbens@2479
   282
 * Initializes the linux effect struct from a haptic_effect.
bobbens@2481
   283
 * Values above 32767 (for unsigned) are unspecified so we must clamp.
bobbens@2479
   284
 */
bobbens@2479
   285
static int
bobbens@2480
   286
SDL_SYS_ToFFEffect( struct ff_effect * dest, SDL_HapticEffect * src )
bobbens@2479
   287
{
bobbens@2482
   288
   int i;
bobbens@2480
   289
   SDL_HapticConstant *constant;
bobbens@2480
   290
   SDL_HapticPeriodic *periodic;
bobbens@2482
   291
   SDL_HapticCondition *condition;
bobbens@2482
   292
   SDL_HapticRamp *ramp;
bobbens@2480
   293
bobbens@2480
   294
   /* Clear up */
bobbens@2479
   295
   SDL_memset(dest, 0, sizeof(struct ff_effect));
bobbens@2480
   296
bobbens@2480
   297
   switch (src->type) {
bobbens@2479
   298
      case SDL_HAPTIC_CONSTANT:
bobbens@2480
   299
         constant = &src->constant;
bobbens@2480
   300
bobbens@2480
   301
         /* Header */
bobbens@2479
   302
         dest->type = FF_CONSTANT;
bobbens@2481
   303
         dest->direction = CLAMP(constant->direction);
bobbens@2480
   304
bobbens@2480
   305
         /* Replay */
bobbens@2481
   306
         dest->replay.length = CLAMP(constant->length);
bobbens@2481
   307
         dest->replay.delay = CLAMP(constant->delay);
bobbens@2480
   308
bobbens@2480
   309
         /* Trigger */
bobbens@2481
   310
         dest->trigger.button = CLAMP(constant->button);
bobbens@2481
   311
         dest->trigger.interval = CLAMP(constant->interval);
bobbens@2480
   312
bobbens@2480
   313
         /* Constant */
bobbens@2480
   314
         dest->u.constant.level = constant->level;
bobbens@2480
   315
bobbens@2480
   316
         /* Envelope */
bobbens@2481
   317
         dest->u.constant.envelope.attack_length = CLAMP(constant->attack_length);
bobbens@2481
   318
         dest->u.constant.envelope.attack_level = CLAMP(constant->attack_level);
bobbens@2481
   319
         dest->u.constant.envelope.fade_length = CLAMP(constant->fade_length);
bobbens@2481
   320
         dest->u.constant.envelope.fade_level = CLAMP(constant->fade_level);
bobbens@2480
   321
bobbens@2480
   322
         break;
bobbens@2480
   323
bobbens@2487
   324
      case SDL_HAPTIC_SINE:
bobbens@2487
   325
      case SDL_HAPTIC_SQUARE:
bobbens@2487
   326
      case SDL_HAPTIC_TRIANGLE:
bobbens@2487
   327
      case SDL_HAPTIC_SAWTOOTHUP:
bobbens@2487
   328
      case SDL_HAPTIC_SAWTOOTHDOWN:
bobbens@2480
   329
         periodic = &src->periodic;
bobbens@2480
   330
bobbens@2480
   331
         /* Header */
bobbens@2480
   332
         dest->type = FF_PERIODIC;
bobbens@2481
   333
         dest->direction = CLAMP(periodic->direction);
bobbens@2480
   334
         
bobbens@2480
   335
         /* Replay */
bobbens@2481
   336
         dest->replay.length = CLAMP(periodic->length);
bobbens@2481
   337
         dest->replay.delay = CLAMP(periodic->delay);
bobbens@2480
   338
         
bobbens@2480
   339
         /* Trigger */
bobbens@2481
   340
         dest->trigger.button = CLAMP(periodic->button);
bobbens@2481
   341
         dest->trigger.interval = CLAMP(periodic->interval);
bobbens@2480
   342
         
bobbens@2487
   343
         /* Periodic */
bobbens@2487
   344
         if (periodic->type == SDL_HAPTIC_SINE)
bobbens@2487
   345
            dest->u.periodic.waveform = FF_SINE;
bobbens@2487
   346
         else if (periodic->type == SDL_HAPTIC_SQUARE)
bobbens@2487
   347
            dest->u.periodic.waveform = FF_SQUARE;
bobbens@2487
   348
         else if (periodic->type == SDL_HAPTIC_TRIANGLE)       
bobbens@2487
   349
            dest->u.periodic.waveform = FF_TRIANGLE;
bobbens@2487
   350
         else if (periodic->type == SDL_HAPTIC_SAWTOOTHUP)       
bobbens@2487
   351
            dest->u.periodic.waveform = FF_SAW_UP;
bobbens@2487
   352
         else if (periodic->type == SDL_HAPTIC_SAWTOOTHDOWN)       
bobbens@2487
   353
            dest->u.periodic.waveform = FF_SAW_DOWN;
bobbens@2481
   354
         dest->u.periodic.period = CLAMP(periodic->period);
bobbens@2480
   355
         dest->u.periodic.magnitude = periodic->magnitude;
bobbens@2480
   356
         dest->u.periodic.offset = periodic->offset;
bobbens@2481
   357
         dest->u.periodic.phase = CLAMP(periodic->phase);
bobbens@2480
   358
         
bobbens@2480
   359
         /* Envelope */
bobbens@2481
   360
         dest->u.periodic.envelope.attack_length = CLAMP(periodic->attack_length);
bobbens@2481
   361
         dest->u.periodic.envelope.attack_level = CLAMP(periodic->attack_level);
bobbens@2481
   362
         dest->u.periodic.envelope.fade_length = CLAMP(periodic->fade_length);
bobbens@2481
   363
         dest->u.periodic.envelope.fade_level = CLAMP(periodic->fade_level);
bobbens@2479
   364
bobbens@2479
   365
         break;
bobbens@2479
   366
bobbens@2482
   367
      case SDL_HAPTIC_SPRING:
bobbens@2482
   368
      case SDL_HAPTIC_DAMPER:
bobbens@2482
   369
      case SDL_HAPTIC_INERTIA:
bobbens@2482
   370
      case SDL_HAPTIC_FRICTION:
bobbens@2482
   371
         condition = &src->condition;
bobbens@2482
   372
bobbens@2482
   373
         /* Header */
bobbens@2482
   374
         if (dest->type == SDL_HAPTIC_SPRING)
bobbens@2482
   375
            dest->type = FF_SPRING;
bobbens@2482
   376
         else if (dest->type == SDL_HAPTIC_DAMPER)
bobbens@2482
   377
            dest->type = FF_DAMPER;
bobbens@2482
   378
         else if (dest->type == SDL_HAPTIC_INERTIA)
bobbens@2482
   379
            dest->type = FF_INERTIA;
bobbens@2482
   380
         else if (dest->type == SDL_HAPTIC_FRICTION)
bobbens@2482
   381
            dest->type = FF_FRICTION;
bobbens@2482
   382
         dest->direction = CLAMP(condition->direction);
bobbens@2482
   383
bobbens@2482
   384
         /* Replay */
bobbens@2482
   385
         dest->replay.length = CLAMP(condition->length);
bobbens@2482
   386
         dest->replay.delay = CLAMP(condition->delay);
bobbens@2482
   387
bobbens@2482
   388
         /* Trigger */
bobbens@2482
   389
         dest->trigger.button = CLAMP(condition->button);
bobbens@2482
   390
         dest->trigger.interval = CLAMP(condition->interval);
bobbens@2482
   391
bobbens@2482
   392
         /* Condition - TODO handle axes */
bobbens@2482
   393
         dest->u.condition[0].right_saturation = CLAMP(condition->right_sat);
bobbens@2482
   394
         dest->u.condition[0].left_saturation = CLAMP(condition->left_sat);
bobbens@2482
   395
         dest->u.condition[0].right_coeff = condition->right_coeff;
bobbens@2482
   396
         dest->u.condition[0].left_coeff = condition->left_coeff;
bobbens@2482
   397
         dest->u.condition[0].deadband = CLAMP(condition->deadband);
bobbens@2482
   398
         dest->u.condition[0].center = condition->center;
bobbens@2482
   399
bobbens@2482
   400
         break;
bobbens@2482
   401
bobbens@2482
   402
      case SDL_HAPTIC_RAMP:
bobbens@2482
   403
         ramp = &src->ramp;
bobbens@2482
   404
bobbens@2482
   405
         /* Header */
bobbens@2482
   406
         dest->type = FF_RAMP;
bobbens@2482
   407
         dest->direction = CLAMP(ramp->direction);
bobbens@2482
   408
bobbens@2482
   409
         /* Replay */
bobbens@2482
   410
         dest->replay.length = CLAMP(ramp->length);
bobbens@2482
   411
         dest->replay.delay = CLAMP(ramp->delay);
bobbens@2482
   412
bobbens@2482
   413
         /* Trigger */
bobbens@2482
   414
         dest->trigger.button = CLAMP(ramp->button);
bobbens@2482
   415
         dest->trigger.interval = CLAMP(ramp->interval);
bobbens@2482
   416
bobbens@2482
   417
         /* Ramp */
bobbens@2482
   418
         dest->u.ramp.start_level = ramp->start;
bobbens@2482
   419
         dest->u.ramp.end_level = ramp->end;
bobbens@2482
   420
bobbens@2482
   421
         /* Envelope */
bobbens@2482
   422
         dest->u.ramp.envelope.attack_length = CLAMP(ramp->attack_length);
bobbens@2482
   423
         dest->u.ramp.envelope.attack_level = CLAMP(ramp->attack_level);
bobbens@2482
   424
         dest->u.ramp.envelope.fade_length = CLAMP(ramp->fade_length);
bobbens@2482
   425
         dest->u.ramp.envelope.fade_level = CLAMP(ramp->fade_level);
bobbens@2482
   426
bobbens@2482
   427
         break;
bobbens@2482
   428
bobbens@2482
   429
bobbens@2479
   430
      default:
bobbens@2479
   431
         SDL_SetError("Unknown haptic effect type.");
bobbens@2479
   432
         return -1;
bobbens@2479
   433
   }
bobbens@2479
   434
bobbens@2479
   435
   return 0;
bobbens@2479
   436
}
bobbens@2479
   437
bobbens@2483
   438
bobbens@2479
   439
/*
bobbens@2477
   440
 * Creates a new haptic effect.
bobbens@2477
   441
 */
bobbens@2477
   442
int
bobbens@2480
   443
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
bobbens@2480
   444
      SDL_HapticEffect * base)
bobbens@2477
   445
{
bobbens@2480
   446
   struct ff_effect * linux_effect;
bobbens@2480
   447
bobbens@2479
   448
   /* Allocate the hardware effect */
bobbens@2479
   449
   effect->hweffect = (struct haptic_hweffect *) 
bobbens@2479
   450
         SDL_malloc(sizeof(struct haptic_hweffect));
bobbens@2479
   451
   if (effect->hweffect == NULL) {
bobbens@2479
   452
      SDL_OutOfMemory();
bobbens@2479
   453
      return -1;
bobbens@2479
   454
   }
bobbens@2479
   455
bobbens@2479
   456
   /* Prepare the ff_effect */
bobbens@2480
   457
   linux_effect = &effect->hweffect->effect;
bobbens@2480
   458
   if (SDL_SYS_ToFFEffect( linux_effect, base ) != 0) {
bobbens@2488
   459
      goto new_effect_err;
bobbens@2479
   460
   }
bobbens@2480
   461
   linux_effect->id = -1; /* Have the kernel give it an id */
bobbens@2479
   462
bobbens@2479
   463
   /* Upload the effect */
bobbens@2480
   464
   if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
bobbens@2483
   465
      SDL_SetError("Error uploading effect to the haptic device.");
bobbens@2488
   466
      goto new_effect_err;
bobbens@2488
   467
   }
bobbens@2488
   468
bobbens@2488
   469
   return 0;
bobbens@2488
   470
bobbens@2488
   471
new_effect_err:
bobbens@2488
   472
   free(effect->hweffect);
bobbens@2488
   473
   effect->hweffect = NULL;
bobbens@2488
   474
   return -1;
bobbens@2488
   475
}
bobbens@2488
   476
bobbens@2488
   477
bobbens@2488
   478
/*
bobbens@2488
   479
 * Updates an effect.
bobbens@2488
   480
 *
bobbens@2488
   481
 * Note: Dynamically updating the direction can in some cases force
bobbens@2488
   482
 * the effect to restart and run once.
bobbens@2488
   483
 */
bobbens@2488
   484
int SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
bobbens@2488
   485
      struct haptic_effect * effect, SDL_HapticEffect * data)
bobbens@2488
   486
{
bobbens@2488
   487
   struct ff_effect linux_effect;
bobbens@2488
   488
bobbens@2488
   489
   /* Create the new effect */
bobbens@2488
   490
   if (SDL_SYS_ToFFEffect( &linux_effect, data ) != 0) {
bobbens@2488
   491
      return -1;
bobbens@2488
   492
   }
bobbens@2488
   493
   linux_effect.id = effect->hweffect->effect.id;
bobbens@2488
   494
bobbens@2488
   495
   /* See if it can be uploaded. */
bobbens@2488
   496
   if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) {
bobbens@2488
   497
      SDL_SetError("Error updating the haptic effect.");
bobbens@2479
   498
      return -1;
bobbens@2479
   499
   }
bobbens@2479
   500
bobbens@2488
   501
   /* Copy the new effect into memory. */
bobbens@2488
   502
   SDL_memcpy( &effect->hweffect->effect, &linux_effect, sizeof(struct ff_effect) );
bobbens@2488
   503
bobbens@2488
   504
   return effect->hweffect->effect.id;
bobbens@2477
   505
}
bobbens@2477
   506
bobbens@2474
   507
bobbens@2479
   508
/*
bobbens@2479
   509
 * Runs an effect.
bobbens@2479
   510
 */
bobbens@2479
   511
int
bobbens@2479
   512
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2479
   513
{
bobbens@2479
   514
   struct input_event run;
bobbens@2479
   515
bobbens@2479
   516
   /* Prepare to run the effect */
bobbens@2479
   517
   run.type = EV_FF;
bobbens@2479
   518
   run.code = effect->hweffect->effect.id;
bobbens@2479
   519
   run.value = 1;
bobbens@2479
   520
bobbens@2479
   521
   if (write(haptic->hwdata->fd, (const void*) &run, sizeof(run)) < 0) {
bobbens@2479
   522
      SDL_SetError("Unable to run the haptic effect.");
bobbens@2479
   523
      return -1;
bobbens@2479
   524
   }
bobbens@2479
   525
bobbens@2479
   526
   return 0;
bobbens@2479
   527
}
bobbens@2479
   528
bobbens@2479
   529
bobbens@2479
   530
/*
bobbens@2485
   531
 * Stops an effect.
bobbens@2485
   532
 */
bobbens@2485
   533
int
bobbens@2485
   534
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2485
   535
{
bobbens@2485
   536
   struct input_event stop;
bobbens@2485
   537
bobbens@2485
   538
   stop.type = EV_FF;
bobbens@2485
   539
   stop.code = effect->hweffect->effect.id;
bobbens@2485
   540
   stop.value = 0;
bobbens@2485
   541
bobbens@2485
   542
   if (write(haptic->hwdata->fd, (const void*) &stop, sizeof(stop)) < 0) {
bobbens@2485
   543
      SDL_SetError("Unable to stop the haptic effect.");
bobbens@2485
   544
      return -1;
bobbens@2485
   545
   }
bobbens@2485
   546
bobbens@2485
   547
   return 0;
bobbens@2485
   548
}
bobbens@2485
   549
bobbens@2485
   550
bobbens@2485
   551
/*
bobbens@2487
   552
 * Frees the effect.
bobbens@2479
   553
 */
bobbens@2479
   554
void
bobbens@2479
   555
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2479
   556
{
bobbens@2479
   557
   if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
bobbens@2479
   558
      SDL_SetError("Error removing the effect from the haptic device.");
bobbens@2479
   559
   }
bobbens@2479
   560
   SDL_free(effect->hweffect);
bobbens@2479
   561
   effect->hweffect = NULL;
bobbens@2479
   562
}
bobbens@2479
   563
bobbens@2479
   564
bobbens@2483
   565
/*
bobbens@2483
   566
 * Sets the gain.
bobbens@2483
   567
 */
bobbens@2483
   568
int
bobbens@2483
   569
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
bobbens@2483
   570
{
bobbens@2483
   571
   struct input_event ie;
bobbens@2483
   572
bobbens@2483
   573
   ie.type = EV_FF;
bobbens@2483
   574
   ie.code = FF_GAIN;
bobbens@2483
   575
   ie.value = (0xFFFFUL * gain) / 100;
bobbens@2483
   576
bobbens@2484
   577
   if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
bobbens@2483
   578
      SDL_SetError("Error setting gain.");
bobbens@2484
   579
      return -1;
bobbens@2483
   580
   }
bobbens@2483
   581
bobbens@2484
   582
   return 0;
bobbens@2483
   583
}
bobbens@2483
   584
bobbens@2483
   585
bobbens@2484
   586
/*
bobbens@2484
   587
 * Sets the autocentering.
bobbens@2484
   588
 */
bobbens@2484
   589
int
bobbens@2484
   590
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
bobbens@2484
   591
{
bobbens@2484
   592
   struct input_event ie;
bobbens@2484
   593
bobbens@2484
   594
   ie.type = EV_FF;
bobbens@2484
   595
   ie.code = FF_AUTOCENTER;
bobbens@2484
   596
   ie.value = (0xFFFFUL * autocenter) / 100;
bobbens@2484
   597
bobbens@2484
   598
   if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
bobbens@2484
   599
      SDL_SetError("Error setting autocenter.");
bobbens@2484
   600
      return -1;
bobbens@2484
   601
   }
bobbens@2484
   602
bobbens@2484
   603
   return 0;
bobbens@2484
   604
}
bobbens@2484
   605
bobbens@2484
   606
bobbens@2484
   607
bobbens@2472
   608
#endif /* SDL_HAPTIC_LINUX */