src/haptic/linux/SDL_syshaptic.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 25 Aug 2008 09:55:03 +0000
changeset 2713 0906692aa6a4
child 3080 4094b4f1c3a1
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

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