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