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