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