src/haptic/linux/SDL_syshaptic.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 16 Aug 2014 16:47:42 -0400
changeset 9070 8973a237f360
parent 9069 16b5dda99337
child 9249 35a4fab04296
permissions -rw-r--r--
Haptic: Fix the saturation and deadband parameters' available range.

There was a misconception that Linux's saturation and deadband parameters -
on which the corresponding SDL parameters were based - use only half of the
possible range.

Thanks, Elias!

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