src/haptic/linux/SDL_syshaptic.c
author Sam Lantinga
Mon, 18 Jan 2010 14:57:41 +0000
changeset 3680 7c18b38f0f9a
parent 3218 81773a1eac83
child 4514 0c71708adbcb
permissions -rw-r--r--
Fixed bug #920

From Martin:

Alright... I corrected SDL_SYS_ToDirection in SDL_syshaptic.c in the linux
directory of haptic. Now in all 3 cases the same value is returned, at least.
Therefore now it should behave the same way as on Windows.

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