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