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