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