src/haptic/linux/SDL_syshaptic.c
author Edward Rudd <urkle@outoforder.cc>
Tue, 04 Feb 2014 15:44:09 -0500
changeset 8170 3b7bb407fcf7
parent 8149 681eb46b8ac4
child 8182 92f71edde3be
permissions -rw-r--r--
Rework haptic backend to properly support hotplugging of haptic devices.

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