src/haptic/linux/SDL_syshaptic.c
author Edgar Simo <bobbens@gmail.com>
Tue, 01 Jul 2008 09:22:22 +0000
branchgsoc2008_force_feedback
changeset 2480 b883974445fc
parent 2479 b9eb2cfe16cd
child 2481 5d0ea4576f20
permissions -rw-r--r--
Some more error reporting.
Added periodic effect.
Confirmed it works.
bobbens@2472
     1
/*
bobbens@2472
     2
    SDL - Simple DirectMedia Layer
bobbens@2472
     3
    Copyright (C) 2008 Edgar Simo
bobbens@2472
     4
bobbens@2472
     5
    This library is free software; you can redistribute it and/or
bobbens@2472
     6
    modify it under the terms of the GNU Lesser General Public
bobbens@2472
     7
    License as published by the Free Software Foundation; either
bobbens@2472
     8
    version 2.1 of the License, or (at your option) any later version.
bobbens@2472
     9
bobbens@2472
    10
    This library is distributed in the hope that it will be useful,
bobbens@2472
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
bobbens@2472
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bobbens@2472
    13
    Lesser General Public License for more details.
bobbens@2472
    14
bobbens@2472
    15
    You should have received a copy of the GNU Lesser General Public
bobbens@2472
    16
    License along with this library; if not, write to the Free Software
bobbens@2472
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bobbens@2472
    18
bobbens@2472
    19
    Sam Lantinga
bobbens@2472
    20
    slouken@libsdl.org
bobbens@2472
    21
*/
bobbens@2472
    22
#include "SDL_config.h"
bobbens@2472
    23
bobbens@2472
    24
#ifdef SDL_JOYSTICK_LINUX
bobbens@2472
    25
bobbens@2472
    26
#include "SDL_haptic.h"
bobbens@2472
    27
#include "../SDL_haptic_c.h"
bobbens@2472
    28
#include "../SDL_syshaptic.h"
bobbens@2472
    29
bobbens@2472
    30
#include <unistd.h> /* close */
bobbens@2472
    31
#include <linux/input.h>
bobbens@2472
    32
#include <sys/ioctl.h>
bobbens@2472
    33
#include <sys/types.h>
bobbens@2472
    34
#include <sys/stat.h>
bobbens@2472
    35
#include <fcntl.h>
bobbens@2472
    36
#include <linux/limits.h>
bobbens@2472
    37
#include <string.h>
bobbens@2480
    38
#include <errno.h>
bobbens@2472
    39
bobbens@2472
    40
bobbens@2472
    41
#define MAX_HAPTICS  32
bobbens@2472
    42
bobbens@2472
    43
bobbens@2475
    44
/*
bobbens@2475
    45
 * List of available haptic devices.
bobbens@2475
    46
 */
bobbens@2472
    47
static struct
bobbens@2472
    48
{
bobbens@2472
    49
   char *fname;
bobbens@2472
    50
   SDL_Haptic *haptic;
bobbens@2472
    51
} SDL_hapticlist[MAX_HAPTICS];
bobbens@2472
    52
bobbens@2475
    53
bobbens@2475
    54
/*
bobbens@2475
    55
 * Haptic system hardware data.
bobbens@2475
    56
 */
bobbens@2472
    57
struct haptic_hwdata
bobbens@2472
    58
{
bobbens@2472
    59
   int fd;
bobbens@2472
    60
};
bobbens@2472
    61
bobbens@2472
    62
bobbens@2477
    63
/*
bobbens@2477
    64
 * Haptic system effect data.
bobbens@2477
    65
 */
bobbens@2477
    66
struct haptic_hweffect
bobbens@2477
    67
{
bobbens@2479
    68
   struct ff_effect effect;
bobbens@2477
    69
};
bobbens@2477
    70
bobbens@2477
    71
bobbens@2475
    72
bobbens@2472
    73
#define test_bit(nr, addr) \
bobbens@2472
    74
   (((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
bobbens@2472
    75
#define EV_TEST(ev,f) \
bobbens@2472
    76
   if (test_bit((ev), features)) ret |= (f);
bobbens@2475
    77
/*
bobbens@2475
    78
 * Test whether a device has haptic properties.
bobbens@2475
    79
 * Returns available properties or 0 if there are none.
bobbens@2475
    80
 */
bobbens@2472
    81
static int
bobbens@2472
    82
EV_IsHaptic(int fd)
bobbens@2472
    83
{
bobbens@2472
    84
   unsigned int ret;
bobbens@2472
    85
   unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
bobbens@2472
    86
bobbens@2472
    87
   ret = 0;
bobbens@2472
    88
bobbens@2472
    89
   ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features);
bobbens@2472
    90
bobbens@2472
    91
   EV_TEST(FF_CONSTANT,   SDL_HAPTIC_CONSTANT);
bobbens@2472
    92
   EV_TEST(FF_PERIODIC,   SDL_HAPTIC_PERIODIC);
bobbens@2472
    93
   EV_TEST(FF_RAMP,       SDL_HAPTIC_RAMP);
bobbens@2472
    94
   EV_TEST(FF_SPRING,     SDL_HAPTIC_SPRING);
bobbens@2472
    95
   EV_TEST(FF_FRICTION,   SDL_HAPTIC_FRICTION);
bobbens@2472
    96
   EV_TEST(FF_DAMPER,     SDL_HAPTIC_DAMPER);
bobbens@2472
    97
   EV_TEST(FF_RUMBLE,     SDL_HAPTIC_RUMBLE);
bobbens@2472
    98
   EV_TEST(FF_INERTIA,    SDL_HAPTIC_INERTIA);
bobbens@2472
    99
   EV_TEST(FF_GAIN,       SDL_HAPTIC_GAIN);
bobbens@2472
   100
   EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
bobbens@2472
   101
bobbens@2472
   102
   return ret;
bobbens@2472
   103
}
bobbens@2472
   104
bobbens@2472
   105
int
bobbens@2472
   106
SDL_SYS_HapticInit(void)
bobbens@2472
   107
{
bobbens@2472
   108
   const char joydev_pattern[] = "/dev/input/event%d";
bobbens@2472
   109
   dev_t dev_nums[MAX_HAPTICS];
bobbens@2472
   110
   char path[PATH_MAX];
bobbens@2472
   111
   struct stat sb;
bobbens@2472
   112
   int fd;
bobbens@2472
   113
   int i, j, k;
bobbens@2472
   114
   int duplicate;
bobbens@2472
   115
   int numhaptics;
bobbens@2472
   116
bobbens@2472
   117
   numhaptics = 0;
bobbens@2472
   118
bobbens@2472
   119
   i = 0;
bobbens@2472
   120
   for (j = 0; j < MAX_HAPTICS; ++j) {
bobbens@2472
   121
bobbens@2472
   122
      snprintf(path, PATH_MAX, joydev_pattern, i++);
bobbens@2472
   123
bobbens@2472
   124
      /* check to see if file exists */
bobbens@2472
   125
      if (stat(path,&sb) != 0)
bobbens@2472
   126
         break;
bobbens@2472
   127
bobbens@2472
   128
      /* check for duplicates */
bobbens@2472
   129
      duplicate = 0;
bobbens@2472
   130
      for (k = 0; (k < numhaptics) && !duplicate; ++k) {
bobbens@2472
   131
         if (sb.st_rdev == dev_nums[k]) {
bobbens@2472
   132
            duplicate = 1;
bobbens@2472
   133
         }                                                     
bobbens@2472
   134
      }                                                         
bobbens@2472
   135
      if (duplicate) {
bobbens@2472
   136
         continue;
bobbens@2472
   137
      }
bobbens@2472
   138
bobbens@2472
   139
      /* try to open */
bobbens@2472
   140
      fd = open(path, O_RDWR, 0);
bobbens@2472
   141
      if (fd < 0) continue;
bobbens@2472
   142
bobbens@2472
   143
#ifdef DEBUG_INPUT_EVENTS
bobbens@2472
   144
      printf("Checking %s\n", path);
bobbens@2472
   145
#endif          
bobbens@2472
   146
bobbens@2472
   147
      /* see if it works */
bobbens@2472
   148
      if (EV_IsHaptic(fd)!=0) {
bobbens@2472
   149
         SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
bobbens@2472
   150
         SDL_hapticlist[numhaptics].haptic = NULL;
bobbens@2472
   151
         dev_nums[numhaptics] = sb.st_rdev;
bobbens@2472
   152
         ++numhaptics;
bobbens@2472
   153
      }
bobbens@2472
   154
      close(fd);
bobbens@2472
   155
   }
bobbens@2472
   156
bobbens@2472
   157
   return numhaptics;
bobbens@2472
   158
}
bobbens@2472
   159
bobbens@2472
   160
bobbens@2475
   161
/*
bobbens@2475
   162
 * Return the name of a haptic device, does not need to be opened.
bobbens@2475
   163
 */
bobbens@2472
   164
const char *
bobbens@2472
   165
SDL_SYS_HapticName(int index)
bobbens@2472
   166
{
bobbens@2472
   167
   int fd;
bobbens@2472
   168
   static char namebuf[128];
bobbens@2472
   169
   char *name;
bobbens@2472
   170
bobbens@2472
   171
   name = NULL;
bobbens@2472
   172
   fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
bobbens@2472
   173
   if (fd >= 0) {
bobbens@2472
   174
      if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
bobbens@2472
   175
         name = SDL_hapticlist[index].fname;
bobbens@2472
   176
      }
bobbens@2472
   177
      else {
bobbens@2472
   178
         name = namebuf;
bobbens@2472
   179
      }
bobbens@2472
   180
   }
bobbens@2472
   181
   close(fd);
bobbens@2472
   182
bobbens@2472
   183
   return name;
bobbens@2472
   184
}
bobbens@2472
   185
bobbens@2472
   186
bobbens@2475
   187
/*
bobbens@2475
   188
 * Opens a haptic device for usage.
bobbens@2475
   189
 */
bobbens@2472
   190
int
bobbens@2472
   191
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
bobbens@2472
   192
{
bobbens@2477
   193
   int i;
bobbens@2472
   194
   int fd;
bobbens@2472
   195
bobbens@2476
   196
   /* Open the character device */
bobbens@2472
   197
   fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
bobbens@2472
   198
   if (fd < 0) {
bobbens@2472
   199
      SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
bobbens@2477
   200
      return -1;
bobbens@2472
   201
   }
bobbens@2472
   202
bobbens@2476
   203
   /* Allocate the hwdata */
bobbens@2476
   204
   haptic->hwdata = (struct haptic_hwdata *)
bobbens@2477
   205
         SDL_malloc(sizeof(*haptic->hwdata));
bobbens@2476
   206
   if (haptic->hwdata == NULL) {
bobbens@2476
   207
      SDL_OutOfMemory();
bobbens@2477
   208
      goto open_err;
bobbens@2476
   209
   }
bobbens@2476
   210
   SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
bobbens@2478
   211
   /* Set the data */
bobbens@2476
   212
   haptic->hwdata->fd = fd;
bobbens@2478
   213
   haptic->supported = EV_IsHaptic(fd);
bobbens@2472
   214
bobbens@2477
   215
   /* Set the effects */
bobbens@2477
   216
   if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
bobbens@2477
   217
      SDL_SetError("Unable to query haptic device memory.");
bobbens@2477
   218
      goto open_err;
bobbens@2477
   219
   }
bobbens@2477
   220
   haptic->effects = (struct haptic_effect *)
bobbens@2477
   221
         SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
bobbens@2477
   222
   if (haptic->effects == NULL) {
bobbens@2477
   223
      SDL_OutOfMemory();
bobbens@2477
   224
      goto open_err;
bobbens@2477
   225
   }
bobbens@2477
   226
bobbens@2472
   227
   return 0;
bobbens@2477
   228
bobbens@2477
   229
   /* Error handling */
bobbens@2477
   230
open_err:
bobbens@2477
   231
   close(fd);
bobbens@2477
   232
   if (haptic->hwdata != NULL) {
bobbens@2477
   233
      free(haptic->hwdata);
bobbens@2477
   234
      haptic->hwdata = NULL;
bobbens@2477
   235
   }
bobbens@2477
   236
   return -1;
bobbens@2472
   237
}
bobbens@2472
   238
bobbens@2472
   239
bobbens@2475
   240
/*
bobbens@2475
   241
 * Closes the haptic device.
bobbens@2475
   242
 */
bobbens@2475
   243
void
bobbens@2475
   244
SDL_SYS_HapticClose(SDL_Haptic * haptic)
bobbens@2475
   245
{
bobbens@2476
   246
   if (haptic->hwdata) {
bobbens@2476
   247
bobbens@2476
   248
      /* Clean up */
bobbens@2476
   249
      close(haptic->hwdata->fd);
bobbens@2476
   250
bobbens@2476
   251
      /* Free */
bobbens@2476
   252
      SDL_free(haptic->hwdata);
bobbens@2476
   253
      haptic->hwdata = NULL;
bobbens@2477
   254
      SDL_free(haptic->effects);
bobbens@2477
   255
      haptic->neffects = 0;
bobbens@2476
   256
   }
bobbens@2475
   257
}
bobbens@2475
   258
bobbens@2475
   259
bobbens@2477
   260
/* 
bobbens@2477
   261
 * Clean up after system specific haptic stuff
bobbens@2477
   262
 */
bobbens@2474
   263
void
bobbens@2474
   264
SDL_SYS_HapticQuit(void)
bobbens@2474
   265
{
bobbens@2474
   266
   int i;
bobbens@2474
   267
bobbens@2474
   268
   for (i=0; SDL_hapticlist[i].fname != NULL; i++) {
bobbens@2474
   269
      SDL_free(SDL_hapticlist[i].fname);
bobbens@2474
   270
   }
bobbens@2474
   271
   SDL_hapticlist[0].fname = NULL;
bobbens@2474
   272
}
bobbens@2474
   273
bobbens@2477
   274
/*
bobbens@2479
   275
 * Initializes the linux effect struct from a haptic_effect.
bobbens@2479
   276
 */
bobbens@2479
   277
static int
bobbens@2480
   278
SDL_SYS_ToFFEffect( struct ff_effect * dest, SDL_HapticEffect * src )
bobbens@2479
   279
{
bobbens@2480
   280
   SDL_HapticConstant *constant;
bobbens@2480
   281
   SDL_HapticPeriodic *periodic;
bobbens@2480
   282
bobbens@2480
   283
   /* Clear up */
bobbens@2479
   284
   SDL_memset(dest, 0, sizeof(struct ff_effect));
bobbens@2480
   285
bobbens@2480
   286
   switch (src->type) {
bobbens@2479
   287
      case SDL_HAPTIC_CONSTANT:
bobbens@2480
   288
         constant = &src->constant;
bobbens@2480
   289
bobbens@2480
   290
         /* Header */
bobbens@2479
   291
         dest->type = FF_CONSTANT;
bobbens@2480
   292
         dest->direction = constant->direction;
bobbens@2480
   293
bobbens@2480
   294
         /* Replay */
bobbens@2480
   295
         dest->replay.length = constant->length;
bobbens@2480
   296
         dest->replay.delay = constant->delay;
bobbens@2480
   297
bobbens@2480
   298
         /* Trigger */
bobbens@2480
   299
         dest->trigger.button = constant->button;
bobbens@2480
   300
         dest->trigger.interval = constant->interval;
bobbens@2480
   301
bobbens@2480
   302
         /* Constant */
bobbens@2480
   303
         dest->u.constant.level = constant->level;
bobbens@2480
   304
bobbens@2480
   305
         /* Envelope */
bobbens@2480
   306
         dest->u.constant.envelope.attack_length = constant->attack_length;
bobbens@2480
   307
         dest->u.constant.envelope.attack_level = constant->attack_level;
bobbens@2480
   308
         dest->u.constant.envelope.fade_length = constant->fade_length;
bobbens@2480
   309
         dest->u.constant.envelope.fade_level = constant->fade_level;
bobbens@2480
   310
bobbens@2480
   311
         break;
bobbens@2480
   312
bobbens@2480
   313
      case SDL_HAPTIC_PERIODIC:
bobbens@2480
   314
         periodic = &src->periodic;
bobbens@2480
   315
bobbens@2480
   316
         /* Header */
bobbens@2480
   317
         dest->type = FF_PERIODIC;
bobbens@2480
   318
         dest->direction = periodic->direction;
bobbens@2480
   319
         
bobbens@2480
   320
         /* Replay */
bobbens@2480
   321
         dest->replay.length = periodic->length;
bobbens@2480
   322
         dest->replay.delay = periodic->delay;
bobbens@2480
   323
         
bobbens@2480
   324
         /* Trigger */
bobbens@2480
   325
         dest->trigger.button = periodic->button;
bobbens@2480
   326
         dest->trigger.interval = periodic->interval;
bobbens@2480
   327
         
bobbens@2480
   328
         /* Constant */
bobbens@2480
   329
         dest->u.periodic.waveform = periodic->waveform;
bobbens@2480
   330
         dest->u.periodic.period = periodic->period;
bobbens@2480
   331
         dest->u.periodic.magnitude = periodic->magnitude;
bobbens@2480
   332
         dest->u.periodic.offset = periodic->offset;
bobbens@2480
   333
         dest->u.periodic.phase = periodic->phase;
bobbens@2480
   334
         
bobbens@2480
   335
         /* Envelope */
bobbens@2480
   336
         dest->u.periodic.envelope.attack_length = periodic->attack_length;
bobbens@2480
   337
         dest->u.periodic.envelope.attack_level = periodic->attack_level;
bobbens@2480
   338
         dest->u.periodic.envelope.fade_length = periodic->fade_length;
bobbens@2480
   339
         dest->u.periodic.envelope.fade_level = periodic->fade_level;
bobbens@2479
   340
bobbens@2479
   341
         break;
bobbens@2479
   342
bobbens@2479
   343
      default:
bobbens@2479
   344
         SDL_SetError("Unknown haptic effect type.");
bobbens@2479
   345
         return -1;
bobbens@2479
   346
   }
bobbens@2479
   347
bobbens@2479
   348
   return 0;
bobbens@2479
   349
}
bobbens@2479
   350
bobbens@2479
   351
/*
bobbens@2477
   352
 * Creates a new haptic effect.
bobbens@2477
   353
 */
bobbens@2477
   354
int
bobbens@2480
   355
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
bobbens@2480
   356
      SDL_HapticEffect * base)
bobbens@2477
   357
{
bobbens@2480
   358
   struct ff_effect * linux_effect;
bobbens@2480
   359
bobbens@2479
   360
   /* Allocate the hardware effect */
bobbens@2479
   361
   effect->hweffect = (struct haptic_hweffect *) 
bobbens@2479
   362
         SDL_malloc(sizeof(struct haptic_hweffect));
bobbens@2479
   363
   if (effect->hweffect == NULL) {
bobbens@2479
   364
      SDL_OutOfMemory();
bobbens@2479
   365
      return -1;
bobbens@2479
   366
   }
bobbens@2479
   367
bobbens@2479
   368
   /* Prepare the ff_effect */
bobbens@2480
   369
   linux_effect = &effect->hweffect->effect;
bobbens@2480
   370
   if (SDL_SYS_ToFFEffect( linux_effect, base ) != 0) {
bobbens@2479
   371
      return -1;
bobbens@2479
   372
   }
bobbens@2480
   373
   linux_effect->id = -1; /* Have the kernel give it an id */
bobbens@2479
   374
bobbens@2479
   375
   /* Upload the effect */
bobbens@2480
   376
   if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
bobbens@2480
   377
      SDL_SetError("Error uploading effect to the haptic device: %s",
bobbens@2480
   378
            strerror(errno));
bobbens@2479
   379
      return -1;
bobbens@2479
   380
   }
bobbens@2479
   381
bobbens@2479
   382
   return 0;
bobbens@2477
   383
}
bobbens@2477
   384
bobbens@2474
   385
bobbens@2479
   386
/*
bobbens@2479
   387
 * Runs an effect.
bobbens@2479
   388
 */
bobbens@2479
   389
int
bobbens@2479
   390
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2479
   391
{
bobbens@2479
   392
   struct input_event run;
bobbens@2479
   393
bobbens@2479
   394
   /* Prepare to run the effect */
bobbens@2479
   395
   run.type = EV_FF;
bobbens@2479
   396
   run.code = effect->hweffect->effect.id;
bobbens@2479
   397
   run.value = 1;
bobbens@2479
   398
bobbens@2479
   399
   if (write(haptic->hwdata->fd, (const void*) &run, sizeof(run)) < 0) {
bobbens@2479
   400
      SDL_SetError("Unable to run the haptic effect.");
bobbens@2479
   401
      return -1;
bobbens@2479
   402
   }
bobbens@2479
   403
bobbens@2479
   404
   return 0;
bobbens@2479
   405
}
bobbens@2479
   406
bobbens@2479
   407
bobbens@2479
   408
/*
bobbens@2479
   409
 * Frees the effect
bobbens@2479
   410
 */
bobbens@2479
   411
void
bobbens@2479
   412
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
bobbens@2479
   413
{
bobbens@2479
   414
   if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
bobbens@2479
   415
      SDL_SetError("Error removing the effect from the haptic device.");
bobbens@2479
   416
   }
bobbens@2479
   417
   SDL_free(effect->hweffect);
bobbens@2479
   418
   effect->hweffect = NULL;
bobbens@2479
   419
}
bobbens@2479
   420
bobbens@2479
   421
bobbens@2472
   422
#endif /* SDL_HAPTIC_LINUX */