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