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