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