src/haptic/linux/SDL_syshaptic.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 16 Jun 2015 00:57:45 -0400
changeset 9743 5f2ac7cec7e9
parent 9740 55e88e3bc7cc
child 9883 48cf7d652118
permissions -rw-r--r--
Haptic/Linux: Keep track of device numbers properly to track duplicates.

Fixes Bugzilla #3014.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #ifdef SDL_HAPTIC_LINUX
    24 
    25 #include "SDL_assert.h"
    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 #include "../../core/linux/SDL_udev.h"
    32 
    33 #include <unistd.h>             /* close */
    34 #include <linux/input.h>        /* Force feedback linux stuff. */
    35 #include <fcntl.h>              /* O_RDWR */
    36 #include <limits.h>             /* INT_MAX */
    37 #include <errno.h>              /* errno, strerror */
    38 #include <math.h>               /* atan2 */
    39 #include <sys/stat.h>           /* stat */
    40 
    41 /* Just in case. */
    42 #ifndef M_PI
    43 #  define M_PI     3.14159265358979323846
    44 #endif
    45 
    46 
    47 #define MAX_HAPTICS  32         /* It's doubtful someone has more then 32 evdev */
    48 
    49 static int MaybeAddDevice(const char *path);
    50 #if SDL_USE_LIBUDEV
    51 static int MaybeRemoveDevice(const char *path);
    52 void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath);
    53 #endif /* SDL_USE_LIBUDEV */
    54 
    55 /*
    56  * List of available haptic devices.
    57  */
    58 typedef struct SDL_hapticlist_item
    59 {
    60     char *fname;                /* Dev path name (like /dev/input/event1) */
    61     SDL_Haptic *haptic;         /* Associated haptic. */
    62     dev_t dev_num;
    63     struct SDL_hapticlist_item *next;
    64 } SDL_hapticlist_item;
    65 
    66 
    67 /*
    68  * Haptic system hardware data.
    69  */
    70 struct haptic_hwdata
    71 {
    72     int fd;                     /* File descriptor of the device. */
    73     char *fname;                /* Points to the name in SDL_hapticlist. */
    74 };
    75 
    76 
    77 /*
    78  * Haptic system effect data.
    79  */
    80 struct haptic_hweffect
    81 {
    82     struct ff_effect effect;    /* The linux kernel effect structure. */
    83 };
    84 
    85 static SDL_hapticlist_item *SDL_hapticlist = NULL;
    86 static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
    87 static int numhaptics = 0;
    88 
    89 #define test_bit(nr, addr) \
    90    (((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
    91 #define EV_TEST(ev,f) \
    92    if (test_bit((ev), features)) ret |= (f);
    93 /*
    94  * Test whether a device has haptic properties.
    95  * Returns available properties or 0 if there are none.
    96  */
    97 static int
    98 EV_IsHaptic(int fd)
    99 {
   100     unsigned int ret;
   101     unsigned long features[1 + FF_MAX / sizeof(unsigned long)];
   102 
   103     /* Ask device for what it has. */
   104     ret = 0;
   105     if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features) < 0) {
   106         return SDL_SetError("Haptic: Unable to get device's features: %s",
   107                             strerror(errno));
   108     }
   109 
   110     /* Convert supported features to SDL_HAPTIC platform-neutral features. */
   111     EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
   112     EV_TEST(FF_SINE, SDL_HAPTIC_SINE);
   113     /* !!! FIXME: put this back when we have more bits in 2.1 */
   114     /* EV_TEST(FF_SQUARE, SDL_HAPTIC_SQUARE); */
   115     EV_TEST(FF_TRIANGLE, SDL_HAPTIC_TRIANGLE);
   116     EV_TEST(FF_SAW_UP, SDL_HAPTIC_SAWTOOTHUP);
   117     EV_TEST(FF_SAW_DOWN, SDL_HAPTIC_SAWTOOTHDOWN);
   118     EV_TEST(FF_RAMP, SDL_HAPTIC_RAMP);
   119     EV_TEST(FF_SPRING, SDL_HAPTIC_SPRING);
   120     EV_TEST(FF_FRICTION, SDL_HAPTIC_FRICTION);
   121     EV_TEST(FF_DAMPER, SDL_HAPTIC_DAMPER);
   122     EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA);
   123     EV_TEST(FF_CUSTOM, SDL_HAPTIC_CUSTOM);
   124     EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN);
   125     EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
   126     EV_TEST(FF_RUMBLE, SDL_HAPTIC_LEFTRIGHT);
   127 
   128     /* Return what it supports. */
   129     return ret;
   130 }
   131 
   132 
   133 /*
   134  * Tests whether a device is a mouse or not.
   135  */
   136 static int
   137 EV_IsMouse(int fd)
   138 {
   139     unsigned long argp[40];
   140 
   141     /* Ask for supported features. */
   142     if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(argp)), argp) < 0) {
   143         return -1;
   144     }
   145 
   146     /* Currently we only test for BTN_MOUSE which can give fake positives. */
   147     if (test_bit(BTN_MOUSE, argp) != 0) {
   148         return 1;
   149     }
   150 
   151     return 0;
   152 }
   153 
   154 /*
   155  * Initializes the haptic subsystem by finding available devices.
   156  */
   157 int
   158 SDL_SYS_HapticInit(void)
   159 {
   160     const char joydev_pattern[] = "/dev/input/event%d";
   161     char path[PATH_MAX];
   162     int i, j;
   163 
   164     /*
   165      * Limit amount of checks to MAX_HAPTICS since we may or may not have
   166      * permission to some or all devices.
   167      */
   168     i = 0;
   169     for (j = 0; j < MAX_HAPTICS; ++j) {
   170 
   171         snprintf(path, PATH_MAX, joydev_pattern, i++);
   172         MaybeAddDevice(path);
   173     }
   174 
   175 #if SDL_USE_LIBUDEV
   176     if (SDL_UDEV_Init() < 0) {
   177         return SDL_SetError("Could not initialize UDEV");
   178     }
   179 
   180     if ( SDL_UDEV_AddCallback(haptic_udev_callback) < 0) {
   181         SDL_UDEV_Quit();
   182         return SDL_SetError("Could not setup haptic <-> udev callback");
   183     }
   184 #endif /* SDL_USE_LIBUDEV */
   185 
   186     return numhaptics;
   187 }
   188 
   189 int
   190 SDL_SYS_NumHaptics()
   191 {
   192     return numhaptics;
   193 }
   194 
   195 static SDL_hapticlist_item *
   196 HapticByDevIndex(int device_index)
   197 {
   198     SDL_hapticlist_item *item = SDL_hapticlist;
   199 
   200     if ((device_index < 0) || (device_index >= numhaptics)) {
   201         return NULL;
   202     }
   203 
   204     while (device_index > 0) {
   205         SDL_assert(item != NULL);
   206         --device_index;
   207         item = item->next;
   208     }
   209 
   210     return item;
   211 }
   212 
   213 #if SDL_USE_LIBUDEV
   214 void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
   215 {
   216     if (devpath == NULL || !(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
   217         return;
   218     }
   219 
   220     switch( udev_type )
   221     {
   222         case SDL_UDEV_DEVICEADDED:
   223             MaybeAddDevice(devpath);
   224             break;
   225 
   226         case SDL_UDEV_DEVICEREMOVED:
   227             MaybeRemoveDevice(devpath);
   228             break;
   229 
   230         default:
   231             break;
   232     }
   233 
   234 }
   235 #endif /* SDL_USE_LIBUDEV */
   236 
   237 static int
   238 MaybeAddDevice(const char *path)
   239 {
   240     struct stat sb;
   241     int fd;
   242     int success;
   243     SDL_hapticlist_item *item;
   244 
   245     if (path == NULL) {
   246         return -1;
   247     }
   248 
   249     /* check to see if file exists */
   250     if (stat(path, &sb) != 0) {
   251         return -1;
   252     }
   253 
   254     /* check for duplicates */
   255     for (item = SDL_hapticlist; item != NULL; item = item->next) {
   256         if (item->dev_num == sb.st_rdev) {
   257             return -1;  /* duplicate. */
   258         }
   259     }
   260 
   261     /* try to open */
   262     fd = open(path, O_RDWR, 0);
   263     if (fd < 0) {
   264         return -1;
   265     }
   266 
   267 #ifdef DEBUG_INPUT_EVENTS
   268     printf("Checking %s\n", path);
   269 #endif
   270 
   271     /* see if it works */
   272     success = EV_IsHaptic(fd);
   273     close(fd);
   274     if (success <= 0) {
   275         return -1;
   276     }
   277 
   278     item = (SDL_hapticlist_item *) SDL_calloc(1, sizeof (SDL_hapticlist_item));
   279     if (item == NULL) {
   280         return -1;
   281     }
   282 
   283     item->fname = SDL_strdup(path);
   284     if (item->fname == NULL) {
   285         SDL_free(item);
   286         return -1;
   287     }
   288 
   289     item->dev_num = sb.st_rdev;
   290 
   291     /* TODO: should we add instance IDs? */
   292     if (SDL_hapticlist_tail == NULL) {
   293         SDL_hapticlist = SDL_hapticlist_tail = item;
   294     } else {
   295         SDL_hapticlist_tail->next = item;
   296         SDL_hapticlist_tail = item;
   297     }
   298 
   299     ++numhaptics;
   300 
   301     /* !!! TODO: Send a haptic add event? */
   302 
   303     return numhaptics;
   304 }
   305 
   306 #if SDL_USE_LIBUDEV
   307 static int
   308 MaybeRemoveDevice(const char* path)
   309 {
   310     SDL_hapticlist_item *item;
   311     SDL_hapticlist_item *prev = NULL;
   312 
   313     if (path == NULL) {
   314         return -1;
   315     }
   316 
   317     for (item = SDL_hapticlist; item != NULL; item = item->next) {
   318         /* found it, remove it. */
   319         if (SDL_strcmp(path, item->fname) == 0) {
   320             const int retval = item->haptic ? item->haptic->index : -1;
   321 
   322             if (prev != NULL) {
   323                 prev->next = item->next;
   324             } else {
   325                 SDL_assert(SDL_hapticlist == item);
   326                 SDL_hapticlist = item->next;
   327             }
   328             if (item == SDL_hapticlist_tail) {
   329                 SDL_hapticlist_tail = prev;
   330             }
   331 
   332             /* Need to decrement the haptic count */
   333             --numhaptics;
   334             /* !!! TODO: Send a haptic remove event? */
   335 
   336             SDL_free(item->fname);
   337             SDL_free(item);
   338             return retval;
   339         }
   340         prev = item;
   341     }
   342 
   343     return -1;
   344 }
   345 #endif /* SDL_USE_LIBUDEV */
   346 
   347 /*
   348  * Gets the name from a file descriptor.
   349  */
   350 static const char *
   351 SDL_SYS_HapticNameFromFD(int fd)
   352 {
   353     static char namebuf[128];
   354 
   355     /* We use the evdev name ioctl. */
   356     if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
   357         return NULL;
   358     }
   359 
   360     return namebuf;
   361 }
   362 
   363 
   364 /*
   365  * Return the name of a haptic device, does not need to be opened.
   366  */
   367 const char *
   368 SDL_SYS_HapticName(int index)
   369 {
   370     SDL_hapticlist_item *item;
   371     int fd;
   372     const char *name;
   373 
   374     item = HapticByDevIndex(index);
   375     /* Open the haptic device. */
   376     name = NULL;
   377     fd = open(item->fname, O_RDONLY, 0);
   378 
   379     if (fd >= 0) {
   380 
   381         name = SDL_SYS_HapticNameFromFD(fd);
   382         if (name == NULL) {
   383             /* No name found, return device character device */
   384             name = item->fname;
   385         }
   386         close(fd);
   387     }
   388 
   389     return name;
   390 }
   391 
   392 
   393 /*
   394  * Opens the haptic device from the file descriptor.
   395  */
   396 static int
   397 SDL_SYS_HapticOpenFromFD(SDL_Haptic * haptic, int fd)
   398 {
   399     /* Allocate the hwdata */
   400     haptic->hwdata = (struct haptic_hwdata *)
   401         SDL_malloc(sizeof(*haptic->hwdata));
   402     if (haptic->hwdata == NULL) {
   403         SDL_OutOfMemory();
   404         goto open_err;
   405     }
   406     SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
   407 
   408     /* Set the data. */
   409     haptic->hwdata->fd = fd;
   410     haptic->supported = EV_IsHaptic(fd);
   411     haptic->naxes = 2;          /* Hardcoded for now, not sure if it's possible to find out. */
   412 
   413     /* Set the effects */
   414     if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
   415         SDL_SetError("Haptic: Unable to query device memory: %s",
   416                      strerror(errno));
   417         goto open_err;
   418     }
   419     haptic->nplaying = haptic->neffects;        /* Linux makes no distinction. */
   420     haptic->effects = (struct haptic_effect *)
   421         SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
   422     if (haptic->effects == NULL) {
   423         SDL_OutOfMemory();
   424         goto open_err;
   425     }
   426     /* Clear the memory */
   427     SDL_memset(haptic->effects, 0,
   428                sizeof(struct haptic_effect) * haptic->neffects);
   429 
   430     return 0;
   431 
   432     /* Error handling */
   433   open_err:
   434     close(fd);
   435     if (haptic->hwdata != NULL) {
   436         SDL_free(haptic->hwdata);
   437         haptic->hwdata = NULL;
   438     }
   439     return -1;
   440 }
   441 
   442 
   443 /*
   444  * Opens a haptic device for usage.
   445  */
   446 int
   447 SDL_SYS_HapticOpen(SDL_Haptic * haptic)
   448 {
   449     int fd;
   450     int ret;
   451     SDL_hapticlist_item *item;
   452 
   453     item = HapticByDevIndex(haptic->index);
   454     /* Open the character device */
   455     fd = open(item->fname, O_RDWR, 0);
   456     if (fd < 0) {
   457         return SDL_SetError("Haptic: Unable to open %s: %s",
   458                             item->fname, strerror(errno));
   459     }
   460 
   461     /* Try to create the haptic. */
   462     ret = SDL_SYS_HapticOpenFromFD(haptic, fd); /* Already closes on error. */
   463     if (ret < 0) {
   464         return -1;
   465     }
   466 
   467     /* Set the fname. */
   468     haptic->hwdata->fname = item->fname;
   469     return 0;
   470 }
   471 
   472 
   473 /*
   474  * Opens a haptic device from first mouse it finds for usage.
   475  */
   476 int
   477 SDL_SYS_HapticMouse(void)
   478 {
   479     int fd;
   480     int device_index = 0;
   481     SDL_hapticlist_item *item;
   482 
   483     for (item = SDL_hapticlist; item; item = item->next) {
   484         /* Open the device. */
   485         fd = open(item->fname, O_RDWR, 0);
   486         if (fd < 0) {
   487             return SDL_SetError("Haptic: Unable to open %s: %s",
   488                                 item->fname, strerror(errno));
   489         }
   490 
   491         /* Is it a mouse? */
   492         if (EV_IsMouse(fd)) {
   493             close(fd);
   494             return device_index;
   495         }
   496 
   497         close(fd);
   498 
   499         ++device_index;
   500     }
   501 
   502     return -1;
   503 }
   504 
   505 
   506 /*
   507  * Checks to see if a joystick has haptic features.
   508  */
   509 int
   510 SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
   511 {
   512     return EV_IsHaptic(joystick->hwdata->fd);
   513 }
   514 
   515 
   516 /*
   517  * Checks to see if the haptic device and joystick are in reality the same.
   518  */
   519 int
   520 SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
   521 {
   522     /* We are assuming Linux is using evdev which should trump the old
   523      * joystick methods. */
   524     if (SDL_strcmp(joystick->hwdata->fname, haptic->hwdata->fname) == 0) {
   525         return 1;
   526     }
   527     return 0;
   528 }
   529 
   530 
   531 /*
   532  * Opens a SDL_Haptic from a SDL_Joystick.
   533  */
   534 int
   535 SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
   536 {
   537     int device_index = 0;
   538     int fd;
   539     int ret;
   540     SDL_hapticlist_item *item;
   541 
   542     /* Find the joystick in the haptic list. */
   543     for (item = SDL_hapticlist; item; item = item->next) {
   544         if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {
   545             haptic->index = device_index;
   546             break;
   547         }
   548         ++device_index;
   549     }
   550     if (device_index >= MAX_HAPTICS) {
   551         return SDL_SetError("Haptic: Joystick doesn't have Haptic capabilities");
   552     }
   553 
   554     fd = open(joystick->hwdata->fname, O_RDWR, 0);
   555     if (fd < 0) {
   556         return SDL_SetError("Haptic: Unable to open %s: %s",
   557                             joystick->hwdata->fname, strerror(errno));
   558     }
   559     ret = SDL_SYS_HapticOpenFromFD(haptic, fd); /* Already closes on error. */
   560     if (ret < 0) {
   561         return -1;
   562     }
   563 
   564     haptic->hwdata->fname = item->fname;
   565     return 0;
   566 }
   567 
   568 
   569 /*
   570  * Closes the haptic device.
   571  */
   572 void
   573 SDL_SYS_HapticClose(SDL_Haptic * haptic)
   574 {
   575     if (haptic->hwdata) {
   576 
   577         /* Free effects. */
   578         SDL_free(haptic->effects);
   579         haptic->effects = NULL;
   580         haptic->neffects = 0;
   581 
   582         /* Clean up */
   583         close(haptic->hwdata->fd);
   584 
   585         /* Free */
   586         SDL_free(haptic->hwdata);
   587         haptic->hwdata = NULL;
   588     }
   589 
   590     /* Clear the rest. */
   591     SDL_memset(haptic, 0, sizeof(SDL_Haptic));
   592 }
   593 
   594 
   595 /*
   596  * Clean up after system specific haptic stuff
   597  */
   598 void
   599 SDL_SYS_HapticQuit(void)
   600 {
   601     SDL_hapticlist_item *item = NULL;
   602     SDL_hapticlist_item *next = NULL;
   603 
   604     for (item = SDL_hapticlist; item; item = next) {
   605         next = item->next;
   606         /* Opened and not closed haptics are leaked, this is on purpose.
   607          * Close your haptic devices after usage. */
   608         SDL_free(item->fname);
   609         item->fname = NULL;
   610     }
   611 
   612 #if SDL_USE_LIBUDEV
   613     SDL_UDEV_DelCallback(haptic_udev_callback);
   614     SDL_UDEV_Quit();
   615 #endif /* SDL_USE_LIBUDEV */
   616 
   617     numhaptics = 0;
   618     SDL_hapticlist = NULL;
   619     SDL_hapticlist_tail = NULL;
   620 }
   621 
   622 
   623 /*
   624  * Converts an SDL button to a ff_trigger button.
   625  */
   626 static Uint16
   627 SDL_SYS_ToButton(Uint16 button)
   628 {
   629     Uint16 ff_button;
   630 
   631     ff_button = 0;
   632 
   633     /*
   634      * Not sure what the proper syntax is because this actually isn't implemented
   635      * in the current kernel from what I've seen (2.6.26).
   636      */
   637     if (button != 0) {
   638         ff_button = BTN_GAMEPAD + button - 1;
   639     }
   640 
   641     return ff_button;
   642 }
   643 
   644 
   645 /*
   646  * Initializes the ff_effect usable direction from a SDL_HapticDirection.
   647  */
   648 static int
   649 SDL_SYS_ToDirection(Uint16 *dest, SDL_HapticDirection * src)
   650 {
   651     Uint32 tmp;
   652 
   653     switch (src->type) {
   654     case SDL_HAPTIC_POLAR:
   655         /* Linux directions start from south.
   656                 (and range from 0 to 0xFFFF)
   657                    Quoting include/linux/input.h, line 926:
   658                    Direction of the effect is encoded as follows:
   659                         0 deg -> 0x0000 (down)
   660                         90 deg -> 0x4000 (left)
   661                         180 deg -> 0x8000 (up)
   662                         270 deg -> 0xC000 (right)
   663                    The force pulls into the direction specified by Linux directions,
   664                    i.e. the opposite convention of SDL directions.
   665                     */
   666         tmp = ((src->dir[0] % 36000) * 0x8000) / 18000; /* convert to range [0,0xFFFF] */
   667         *dest = (Uint16) tmp;
   668         break;
   669 
   670     case SDL_HAPTIC_SPHERICAL:
   671             /*
   672                 We convert to polar, because that's the only supported direction on Linux.
   673                 The first value of a spherical direction is practically the same as a
   674                 Polar direction, except that we have to add 90 degrees. It is the angle
   675                 from EAST {1,0} towards SOUTH {0,1}.
   676                 --> add 9000
   677                 --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
   678             */
   679             tmp = ((src->dir[0]) + 9000) % 36000;    /* Convert to polars */
   680         tmp = (tmp * 0x8000) / 18000; /* convert to range [0,0xFFFF] */
   681         *dest = (Uint16) tmp;
   682         break;
   683 
   684     case SDL_HAPTIC_CARTESIAN:
   685         if (!src->dir[1])
   686             *dest = (src->dir[0] >= 0 ? 0x4000 : 0xC000);
   687         else if (!src->dir[0])
   688             *dest = (src->dir[1] >= 0 ? 0x8000 : 0);
   689         else {
   690             float f = atan2(src->dir[1], src->dir[0]);    /* Ideally we'd use fixed point math instead of floats... */
   691                     /*
   692                       atan2 takes the parameters: Y-axis-value and X-axis-value (in that order)
   693                        - Y-axis-value is the second coordinate (from center to SOUTH)
   694                        - X-axis-value is the first coordinate (from center to EAST)
   695                         We add 36000, because atan2 also returns negative values. Then we practically
   696                         have the first spherical value. Therefore we proceed as in case
   697                         SDL_HAPTIC_SPHERICAL and add another 9000 to get the polar value.
   698                       --> add 45000 in total
   699                       --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
   700                     */
   701                 tmp = (((Sint32) (f * 18000. / M_PI)) + 45000) % 36000;
   702             tmp = (tmp * 0x8000) / 18000; /* convert to range [0,0xFFFF] */
   703             *dest = (Uint16) tmp;
   704         }
   705         break;
   706 
   707     default:
   708         return SDL_SetError("Haptic: Unsupported direction type.");
   709     }
   710 
   711     return 0;
   712 }
   713 
   714 
   715 #define  CLAMP(x)    (((x) > 32767) ? 32767 : x)
   716 /*
   717  * Initializes the Linux effect struct from a haptic_effect.
   718  * Values above 32767 (for unsigned) are unspecified so we must clamp.
   719  */
   720 static int
   721 SDL_SYS_ToFFEffect(struct ff_effect *dest, SDL_HapticEffect * src)
   722 {
   723     SDL_HapticConstant *constant;
   724     SDL_HapticPeriodic *periodic;
   725     SDL_HapticCondition *condition;
   726     SDL_HapticRamp *ramp;
   727     SDL_HapticLeftRight *leftright;
   728 
   729     /* Clear up */
   730     SDL_memset(dest, 0, sizeof(struct ff_effect));
   731 
   732     switch (src->type) {
   733     case SDL_HAPTIC_CONSTANT:
   734         constant = &src->constant;
   735 
   736         /* Header */
   737         dest->type = FF_CONSTANT;
   738         if (SDL_SYS_ToDirection(&dest->direction, &constant->direction) == -1)
   739             return -1;
   740 
   741         /* Replay */
   742         dest->replay.length = (constant->length == SDL_HAPTIC_INFINITY) ?
   743             0 : CLAMP(constant->length);
   744         dest->replay.delay = CLAMP(constant->delay);
   745 
   746         /* Trigger */
   747         dest->trigger.button = SDL_SYS_ToButton(constant->button);
   748         dest->trigger.interval = CLAMP(constant->interval);
   749 
   750         /* Constant */
   751         dest->u.constant.level = constant->level;
   752 
   753         /* Envelope */
   754         dest->u.constant.envelope.attack_length =
   755             CLAMP(constant->attack_length);
   756         dest->u.constant.envelope.attack_level =
   757             CLAMP(constant->attack_level);
   758         dest->u.constant.envelope.fade_length = CLAMP(constant->fade_length);
   759         dest->u.constant.envelope.fade_level = CLAMP(constant->fade_level);
   760 
   761         break;
   762 
   763     case SDL_HAPTIC_SINE:
   764     /* !!! FIXME: put this back when we have more bits in 2.1 */
   765     /* case SDL_HAPTIC_SQUARE: */
   766     case SDL_HAPTIC_TRIANGLE:
   767     case SDL_HAPTIC_SAWTOOTHUP:
   768     case SDL_HAPTIC_SAWTOOTHDOWN:
   769         periodic = &src->periodic;
   770 
   771         /* Header */
   772         dest->type = FF_PERIODIC;
   773         if (SDL_SYS_ToDirection(&dest->direction, &periodic->direction) == -1)
   774             return -1;
   775 
   776         /* Replay */
   777         dest->replay.length = (periodic->length == SDL_HAPTIC_INFINITY) ?
   778             0 : CLAMP(periodic->length);
   779         dest->replay.delay = CLAMP(periodic->delay);
   780 
   781         /* Trigger */
   782         dest->trigger.button = SDL_SYS_ToButton(periodic->button);
   783         dest->trigger.interval = CLAMP(periodic->interval);
   784 
   785         /* Periodic */
   786         if (periodic->type == SDL_HAPTIC_SINE)
   787             dest->u.periodic.waveform = FF_SINE;
   788         /* !!! FIXME: put this back when we have more bits in 2.1 */
   789         /* else if (periodic->type == SDL_HAPTIC_SQUARE)
   790             dest->u.periodic.waveform = FF_SQUARE; */
   791         else if (periodic->type == SDL_HAPTIC_TRIANGLE)
   792             dest->u.periodic.waveform = FF_TRIANGLE;
   793         else if (periodic->type == SDL_HAPTIC_SAWTOOTHUP)
   794             dest->u.periodic.waveform = FF_SAW_UP;
   795         else if (periodic->type == SDL_HAPTIC_SAWTOOTHDOWN)
   796             dest->u.periodic.waveform = FF_SAW_DOWN;
   797         dest->u.periodic.period = CLAMP(periodic->period);
   798         dest->u.periodic.magnitude = periodic->magnitude;
   799         dest->u.periodic.offset = periodic->offset;
   800         /* Linux phase is defined in interval "[0x0000, 0x10000[", corresponds with "[0deg, 360deg[" phase shift. */
   801         dest->u.periodic.phase = ((Uint32)periodic->phase * 0x10000U) / 36000;
   802 
   803         /* Envelope */
   804         dest->u.periodic.envelope.attack_length =
   805             CLAMP(periodic->attack_length);
   806         dest->u.periodic.envelope.attack_level =
   807             CLAMP(periodic->attack_level);
   808         dest->u.periodic.envelope.fade_length = CLAMP(periodic->fade_length);
   809         dest->u.periodic.envelope.fade_level = CLAMP(periodic->fade_level);
   810 
   811         break;
   812 
   813     case SDL_HAPTIC_SPRING:
   814     case SDL_HAPTIC_DAMPER:
   815     case SDL_HAPTIC_INERTIA:
   816     case SDL_HAPTIC_FRICTION:
   817         condition = &src->condition;
   818 
   819         /* Header */
   820         if (condition->type == SDL_HAPTIC_SPRING)
   821             dest->type = FF_SPRING;
   822         else if (condition->type == SDL_HAPTIC_DAMPER)
   823             dest->type = FF_DAMPER;
   824         else if (condition->type == SDL_HAPTIC_INERTIA)
   825             dest->type = FF_INERTIA;
   826         else if (condition->type == SDL_HAPTIC_FRICTION)
   827             dest->type = FF_FRICTION;
   828         dest->direction = 0;    /* Handled by the condition-specifics. */
   829 
   830         /* Replay */
   831         dest->replay.length = (condition->length == SDL_HAPTIC_INFINITY) ?
   832             0 : CLAMP(condition->length);
   833         dest->replay.delay = CLAMP(condition->delay);
   834 
   835         /* Trigger */
   836         dest->trigger.button = SDL_SYS_ToButton(condition->button);
   837         dest->trigger.interval = CLAMP(condition->interval);
   838 
   839         /* Condition */
   840         /* X axis */
   841         dest->u.condition[0].right_saturation = condition->right_sat[0];
   842         dest->u.condition[0].left_saturation = condition->left_sat[0];
   843         dest->u.condition[0].right_coeff = condition->right_coeff[0];
   844         dest->u.condition[0].left_coeff = condition->left_coeff[0];
   845         dest->u.condition[0].deadband = condition->deadband[0];
   846         dest->u.condition[0].center = condition->center[0];
   847         /* Y axis */
   848         dest->u.condition[1].right_saturation = condition->right_sat[1];
   849         dest->u.condition[1].left_saturation = condition->left_sat[1];
   850         dest->u.condition[1].right_coeff = condition->right_coeff[1];
   851         dest->u.condition[1].left_coeff = condition->left_coeff[1];
   852         dest->u.condition[1].deadband = condition->deadband[1];
   853         dest->u.condition[1].center = condition->center[1];
   854 
   855         /*
   856          * There is no envelope in the linux force feedback api for conditions.
   857          */
   858 
   859         break;
   860 
   861     case SDL_HAPTIC_RAMP:
   862         ramp = &src->ramp;
   863 
   864         /* Header */
   865         dest->type = FF_RAMP;
   866         if (SDL_SYS_ToDirection(&dest->direction, &ramp->direction) == -1)
   867             return -1;
   868 
   869         /* Replay */
   870         dest->replay.length = (ramp->length == SDL_HAPTIC_INFINITY) ?
   871             0 : CLAMP(ramp->length);
   872         dest->replay.delay = CLAMP(ramp->delay);
   873 
   874         /* Trigger */
   875         dest->trigger.button = SDL_SYS_ToButton(ramp->button);
   876         dest->trigger.interval = CLAMP(ramp->interval);
   877 
   878         /* Ramp */
   879         dest->u.ramp.start_level = ramp->start;
   880         dest->u.ramp.end_level = ramp->end;
   881 
   882         /* Envelope */
   883         dest->u.ramp.envelope.attack_length = CLAMP(ramp->attack_length);
   884         dest->u.ramp.envelope.attack_level = CLAMP(ramp->attack_level);
   885         dest->u.ramp.envelope.fade_length = CLAMP(ramp->fade_length);
   886         dest->u.ramp.envelope.fade_level = CLAMP(ramp->fade_level);
   887 
   888         break;
   889 
   890     case SDL_HAPTIC_LEFTRIGHT:
   891         leftright = &src->leftright;
   892 
   893         /* Header */
   894         dest->type = FF_RUMBLE;
   895         dest->direction = 0;
   896 
   897         /* Replay */
   898         dest->replay.length = (leftright->length == SDL_HAPTIC_INFINITY) ?
   899             0 : CLAMP(leftright->length);
   900 
   901         /* Trigger */
   902         dest->trigger.button = 0;
   903         dest->trigger.interval = 0;
   904 
   905         /* Rumble */
   906         dest->u.rumble.strong_magnitude = leftright->large_magnitude;
   907         dest->u.rumble.weak_magnitude = leftright->small_magnitude;
   908 
   909         break;
   910 
   911 
   912     default:
   913         return SDL_SetError("Haptic: Unknown effect type.");
   914     }
   915 
   916     return 0;
   917 }
   918 
   919 
   920 /*
   921  * Creates a new haptic effect.
   922  */
   923 int
   924 SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
   925                         SDL_HapticEffect * base)
   926 {
   927     struct ff_effect *linux_effect;
   928 
   929     /* Allocate the hardware effect */
   930     effect->hweffect = (struct haptic_hweffect *)
   931         SDL_malloc(sizeof(struct haptic_hweffect));
   932     if (effect->hweffect == NULL) {
   933         return SDL_OutOfMemory();
   934     }
   935 
   936     /* Prepare the ff_effect */
   937     linux_effect = &effect->hweffect->effect;
   938     if (SDL_SYS_ToFFEffect(linux_effect, base) != 0) {
   939         goto new_effect_err;
   940     }
   941     linux_effect->id = -1;      /* Have the kernel give it an id */
   942 
   943     /* Upload the effect */
   944     if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
   945         SDL_SetError("Haptic: Error uploading effect to the device: %s",
   946                      strerror(errno));
   947         goto new_effect_err;
   948     }
   949 
   950     return 0;
   951 
   952   new_effect_err:
   953     SDL_free(effect->hweffect);
   954     effect->hweffect = NULL;
   955     return -1;
   956 }
   957 
   958 
   959 /*
   960  * Updates an effect.
   961  *
   962  * Note: Dynamically updating the direction can in some cases force
   963  * the effect to restart and run once.
   964  */
   965 int
   966 SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
   967                            struct haptic_effect *effect,
   968                            SDL_HapticEffect * data)
   969 {
   970     struct ff_effect linux_effect;
   971 
   972     /* Create the new effect */
   973     if (SDL_SYS_ToFFEffect(&linux_effect, data) != 0) {
   974         return -1;
   975     }
   976     linux_effect.id = effect->hweffect->effect.id;
   977 
   978     /* See if it can be uploaded. */
   979     if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) {
   980         return SDL_SetError("Haptic: Error updating the effect: %s",
   981                             strerror(errno));
   982     }
   983 
   984     /* Copy the new effect into memory. */
   985     SDL_memcpy(&effect->hweffect->effect, &linux_effect,
   986                sizeof(struct ff_effect));
   987 
   988     return effect->hweffect->effect.id;
   989 }
   990 
   991 
   992 /*
   993  * Runs an effect.
   994  */
   995 int
   996 SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
   997                         Uint32 iterations)
   998 {
   999     struct input_event run;
  1000 
  1001     /* Prepare to run the effect */
  1002     run.type = EV_FF;
  1003     run.code = effect->hweffect->effect.id;
  1004     /* We don't actually have infinity here, so we just do INT_MAX which is pretty damn close. */
  1005     run.value = (iterations > INT_MAX) ? INT_MAX : iterations;
  1006 
  1007     if (write(haptic->hwdata->fd, (const void *) &run, sizeof(run)) < 0) {
  1008         return SDL_SetError("Haptic: Unable to run the effect: %s", strerror(errno));
  1009     }
  1010 
  1011     return 0;
  1012 }
  1013 
  1014 
  1015 /*
  1016  * Stops an effect.
  1017  */
  1018 int
  1019 SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  1020 {
  1021     struct input_event stop;
  1022 
  1023     stop.type = EV_FF;
  1024     stop.code = effect->hweffect->effect.id;
  1025     stop.value = 0;
  1026 
  1027     if (write(haptic->hwdata->fd, (const void *) &stop, sizeof(stop)) < 0) {
  1028         return SDL_SetError("Haptic: Unable to stop the effect: %s",
  1029                             strerror(errno));
  1030     }
  1031 
  1032     return 0;
  1033 }
  1034 
  1035 
  1036 /*
  1037  * Frees the effect.
  1038  */
  1039 void
  1040 SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  1041 {
  1042     if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
  1043         SDL_SetError("Haptic: Error removing the effect from the device: %s",
  1044                      strerror(errno));
  1045     }
  1046     SDL_free(effect->hweffect);
  1047     effect->hweffect = NULL;
  1048 }
  1049 
  1050 
  1051 /*
  1052  * Gets the status of a haptic effect.
  1053  */
  1054 int
  1055 SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
  1056                               struct haptic_effect *effect)
  1057 {
  1058 #if 0                           /* Not supported atm. */
  1059     struct input_event ie;
  1060 
  1061     ie.type = EV_FF;
  1062     ie.type = EV_FF_STATUS;
  1063     ie.code = effect->hweffect->effect.id;
  1064 
  1065     if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
  1066         return SDL_SetError("Haptic: Error getting device status.");
  1067     }
  1068 
  1069     return 0;
  1070 #endif
  1071 
  1072     return -1;
  1073 }
  1074 
  1075 
  1076 /*
  1077  * Sets the gain.
  1078  */
  1079 int
  1080 SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
  1081 {
  1082     struct input_event ie;
  1083 
  1084     ie.type = EV_FF;
  1085     ie.code = FF_GAIN;
  1086     ie.value = (0xFFFFUL * gain) / 100;
  1087 
  1088     if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
  1089         return SDL_SetError("Haptic: Error setting gain: %s", strerror(errno));
  1090     }
  1091 
  1092     return 0;
  1093 }
  1094 
  1095 
  1096 /*
  1097  * Sets the autocentering.
  1098  */
  1099 int
  1100 SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
  1101 {
  1102     struct input_event ie;
  1103 
  1104     ie.type = EV_FF;
  1105     ie.code = FF_AUTOCENTER;
  1106     ie.value = (0xFFFFUL * autocenter) / 100;
  1107 
  1108     if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
  1109         return SDL_SetError("Haptic: Error setting autocenter: %s", strerror(errno));
  1110     }
  1111 
  1112     return 0;
  1113 }
  1114 
  1115 
  1116 /*
  1117  * Pausing is not supported atm by linux.
  1118  */
  1119 int
  1120 SDL_SYS_HapticPause(SDL_Haptic * haptic)
  1121 {
  1122     return -1;
  1123 }
  1124 
  1125 
  1126 /*
  1127  * Unpausing is not supported atm by linux.
  1128  */
  1129 int
  1130 SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
  1131 {
  1132     return -1;
  1133 }
  1134 
  1135 
  1136 /*
  1137  * Stops all the currently playing effects.
  1138  */
  1139 int
  1140 SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
  1141 {
  1142     int i, ret;
  1143 
  1144     /* Linux does not support this natively so we have to loop. */
  1145     for (i = 0; i < haptic->neffects; i++) {
  1146         if (haptic->effects[i].hweffect != NULL) {
  1147             ret = SDL_SYS_HapticStopEffect(haptic, &haptic->effects[i]);
  1148             if (ret < 0) {
  1149                 return SDL_SetError
  1150                     ("Haptic: Error while trying to stop all playing effects.");
  1151             }
  1152         }
  1153     }
  1154     return 0;
  1155 }
  1156 
  1157 #endif /* SDL_HAPTIC_LINUX */
  1158 
  1159 /* vi: set ts=4 sw=4 expandtab: */