src/joystick/linux/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jun 2019 10:35:47 -0700
changeset 12847 baae9331abc0
parent 12846 c54ce7eddcbe
child 12848 a9a7c1e48f0b
permissions -rw-r--r--
The hat index passed to the application should be zero-based with no holes
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
slouken@0
     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@0
     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@0
    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@0
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@0
    22
slouken@1635
    23
#ifdef SDL_JOYSTICK_LINUX
slouken@1635
    24
icculus@6729
    25
#ifndef SDL_INPUT_LINUXEV
icculus@6729
    26
#error SDL now requires a Linux 2.4+ kernel with /dev/input/event support.
icculus@6729
    27
#endif
icculus@6729
    28
philipp@8138
    29
/* This is the Linux implementation of the SDL joystick API */
slouken@0
    30
slouken@0
    31
#include <sys/stat.h>
slouken@12088
    32
#include <errno.h>              /* errno, strerror */
slouken@0
    33
#include <fcntl.h>
slouken@12088
    34
#include <limits.h>             /* For the definition of PATH_MAX */
slouken@0
    35
#include <sys/ioctl.h>
slouken@12088
    36
#include <unistd.h>
slouken@0
    37
#include <linux/joystick.h>
slouken@0
    38
icculus@6734
    39
#include "SDL_assert.h"
slouken@0
    40
#include "SDL_joystick.h"
icculus@6734
    41
#include "SDL_endian.h"
slouken@11532
    42
#include "../../events/SDL_events_c.h"
slouken@1361
    43
#include "../SDL_sysjoystick.h"
slouken@1361
    44
#include "../SDL_joystick_c.h"
slouken@11532
    45
#include "../steam/SDL_steamcontroller.h"
slouken@2713
    46
#include "SDL_sysjoystick_c.h"
slouken@12088
    47
#include "../hidapi/SDL_hidapijoystick_c.h"
slouken@0
    48
slouken@6910
    49
/* This isn't defined in older Linux kernel headers */
slouken@6910
    50
#ifndef SYN_DROPPED
slouken@6910
    51
#define SYN_DROPPED 3
slouken@6910
    52
#endif
slouken@6910
    53
gabomdq@7772
    54
#include "../../core/linux/SDL_udev.h"
icculus@6734
    55
gabomdq@7772
    56
static int MaybeAddDevice(const char *path);
gabomdq@7772
    57
#if SDL_USE_LIBUDEV
gabomdq@7772
    58
static int MaybeRemoveDevice(const char *path);
philipp@11094
    59
static void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath);
gabomdq@7772
    60
#endif /* SDL_USE_LIBUDEV */
slouken@892
    61
slouken@0
    62
icculus@6734
    63
/* A linked list of available joysticks */
icculus@6734
    64
typedef struct SDL_joylist_item
icculus@6734
    65
{
icculus@6734
    66
    int device_instance;
icculus@6734
    67
    char *path;   /* "/dev/input/event2" or whatever */
icculus@6734
    68
    char *name;   /* "SideWinder 3D Pro" or whatever */
slouken@6743
    69
    SDL_JoystickGUID guid;
icculus@6734
    70
    dev_t devnum;
icculus@6734
    71
    struct joystick_hwdata *hwdata;
icculus@6734
    72
    struct SDL_joylist_item *next;
slouken@11532
    73
slouken@11532
    74
    /* Steam Controller support */
slouken@11532
    75
    SDL_bool m_bSteamController;
icculus@6734
    76
} SDL_joylist_item;
icculus@6734
    77
icculus@6734
    78
static SDL_joylist_item *SDL_joylist = NULL;
icculus@6734
    79
static SDL_joylist_item *SDL_joylist_tail = NULL;
icculus@6734
    80
static int numjoysticks = 0;
icculus@6734
    81
slouken@11532
    82
slouken@0
    83
#define test_bit(nr, addr) \
icculus@6734
    84
    (((1UL << ((nr) % (sizeof(long) * 8))) & ((addr)[(nr) / (sizeof(long) * 8)])) != 0)
slouken@3404
    85
#define NBITS(x) ((((x)-1)/(sizeof(long) * 8))+1)
slouken@0
    86
slouken@1895
    87
static int
slouken@6743
    88
IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *guid)
slouken@0
    89
{
gabomdq@7909
    90
    struct input_id inpid;
slouken@10595
    91
    Uint16 *guid16 = (Uint16 *)guid->data;
gabomdq@7909
    92
gabomdq@7909
    93
#if !SDL_USE_LIBUDEV
gabomdq@7909
    94
    /* When udev is enabled we only get joystick devices here, so there's no need to test them */
slouken@3404
    95
    unsigned long evbit[NBITS(EV_MAX)] = { 0 };
slouken@3404
    96
    unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
slouken@3404
    97
    unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
slouken@0
    98
slouken@1895
    99
    if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
slouken@1895
   100
        (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
slouken@1895
   101
        (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
slouken@1895
   102
        return (0);
slouken@1895
   103
    }
bobbens@3079
   104
slouken@1895
   105
    if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
bobbens@3079
   106
          test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit))) {
slouken@1895
   107
        return 0;
bobbens@3079
   108
    }
gabomdq@7909
   109
#endif
icculus@6734
   110
icculus@6734
   111
    if (ioctl(fd, EVIOCGNAME(namebuflen), namebuf) < 0) {
icculus@6734
   112
        return 0;
icculus@6734
   113
    }
icculus@6734
   114
icculus@6734
   115
    if (ioctl(fd, EVIOCGID, &inpid) < 0) {
icculus@6734
   116
        return 0;
icculus@6734
   117
    }
icculus@6734
   118
slouken@12088
   119
#ifdef SDL_JOYSTICK_HIDAPI
slouken@12115
   120
    if (HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version)) {
slouken@12088
   121
        /* The HIDAPI driver is taking care of this device */
slouken@12088
   122
        return 0;
slouken@12088
   123
    }
slouken@12088
   124
#endif
slouken@12088
   125
slouken@6831
   126
#ifdef DEBUG_JOYSTICK
slouken@10595
   127
    printf("Joystick: %s, bustype = %d, vendor = 0x%.4x, product = 0x%.4x, version = %d\n", namebuf, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
slouken@6831
   128
#endif
slouken@6831
   129
slouken@6831
   130
    SDL_memset(guid->data, 0, sizeof(guid->data));
slouken@6831
   131
icculus@6734
   132
    /* We only need 16 bits for each of these; space them out to fill 128. */
icculus@6734
   133
    /* Byteswap so devices get same GUID on little/big endian platforms. */
slouken@10595
   134
    *guid16++ = SDL_SwapLE16(inpid.bustype);
slouken@10595
   135
    *guid16++ = 0;
slouken@6831
   136
slouken@10595
   137
    if (inpid.vendor && inpid.product) {
slouken@10595
   138
        *guid16++ = SDL_SwapLE16(inpid.vendor);
slouken@10595
   139
        *guid16++ = 0;
slouken@10595
   140
        *guid16++ = SDL_SwapLE16(inpid.product);
slouken@10595
   141
        *guid16++ = 0;
slouken@10595
   142
        *guid16++ = SDL_SwapLE16(inpid.version);
slouken@10595
   143
        *guid16++ = 0;
slouken@6831
   144
    } else {
slouken@6831
   145
        SDL_strlcpy((char*)guid16, namebuf, sizeof(guid->data) - 4);
slouken@6831
   146
    }
icculus@6734
   147
slouken@12088
   148
    if (SDL_ShouldIgnoreJoystick(namebuf, *guid)) {
slouken@11201
   149
        return 0;
slouken@11201
   150
    }
icculus@6734
   151
    return 1;
slouken@0
   152
}
slouken@0
   153
gabomdq@7772
   154
#if SDL_USE_LIBUDEV
philipp@11094
   155
static void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
gabomdq@7772
   156
{
slouken@9321
   157
    if (devpath == NULL) {
gabomdq@7772
   158
        return;
gabomdq@7772
   159
    }
slouken@9321
   160
slouken@9321
   161
    switch (udev_type) {
gabomdq@7772
   162
        case SDL_UDEV_DEVICEADDED:
slouken@9321
   163
            if (!(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
slouken@9321
   164
                return;
slouken@9321
   165
            }
slouken@7802
   166
            MaybeAddDevice(devpath);
gabomdq@7772
   167
            break;
gabomdq@7772
   168
            
gabomdq@7772
   169
        case SDL_UDEV_DEVICEREMOVED:
slouken@7802
   170
            MaybeRemoveDevice(devpath);
gabomdq@7772
   171
            break;
gabomdq@7772
   172
            
gabomdq@7772
   173
        default:
gabomdq@7772
   174
            break;
gabomdq@7772
   175
    }
gabomdq@7772
   176
    
gabomdq@7772
   177
}
gabomdq@7772
   178
#endif /* SDL_USE_LIBUDEV */
gabomdq@7772
   179
slouken@0
   180
icculus@6734
   181
/* !!! FIXME: I would love to dump this code and use libudev instead. */
icculus@6734
   182
static int
icculus@6734
   183
MaybeAddDevice(const char *path)
icculus@6734
   184
{
icculus@6734
   185
    struct stat sb;
icculus@6734
   186
    int fd = -1;
icculus@6734
   187
    int isstick = 0;
icculus@6734
   188
    char namebuf[128];
slouken@6743
   189
    SDL_JoystickGUID guid;
icculus@6734
   190
    SDL_joylist_item *item;
icculus@6734
   191
icculus@6734
   192
    if (path == NULL) {
icculus@6734
   193
        return -1;
icculus@6734
   194
    }
icculus@6734
   195
icculus@6734
   196
    if (stat(path, &sb) == -1) {
icculus@6734
   197
        return -1;
icculus@6734
   198
    }
icculus@6734
   199
icculus@6734
   200
    /* Check to make sure it's not already in list. */
icculus@6734
   201
    for (item = SDL_joylist; item != NULL; item = item->next) {
icculus@6734
   202
        if (sb.st_rdev == item->devnum) {
icculus@6734
   203
            return -1;  /* already have this one */
icculus@6734
   204
        }
icculus@6734
   205
    }
icculus@6734
   206
icculus@6734
   207
    fd = open(path, O_RDONLY, 0);
icculus@6734
   208
    if (fd < 0) {
icculus@6734
   209
        return -1;
icculus@6734
   210
    }
icculus@6734
   211
icculus@6734
   212
#ifdef DEBUG_INPUT_EVENTS
icculus@6734
   213
    printf("Checking %s\n", path);
icculus@6734
   214
#endif
icculus@6734
   215
icculus@6734
   216
    isstick = IsJoystick(fd, namebuf, sizeof (namebuf), &guid);
icculus@6734
   217
    close(fd);
icculus@6734
   218
    if (!isstick) {
icculus@6734
   219
        return -1;
icculus@6734
   220
    }
icculus@6734
   221
icculus@6734
   222
    item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
icculus@6734
   223
    if (item == NULL) {
icculus@6734
   224
        return -1;
icculus@6734
   225
    }
icculus@6734
   226
icculus@6734
   227
    SDL_zerop(item);
icculus@6734
   228
    item->devnum = sb.st_rdev;
icculus@6734
   229
    item->path = SDL_strdup(path);
icculus@6734
   230
    item->name = SDL_strdup(namebuf);
icculus@6734
   231
    item->guid = guid;
icculus@6734
   232
slouken@12088
   233
    if ((item->path == NULL) || (item->name == NULL)) {
icculus@6734
   234
         SDL_free(item->path);
icculus@6734
   235
         SDL_free(item->name);
icculus@6734
   236
         SDL_free(item);
icculus@6734
   237
         return -1;
icculus@6734
   238
    }
icculus@6734
   239
slouken@12088
   240
    item->device_instance = SDL_GetNextJoystickInstanceID();
icculus@6734
   241
    if (SDL_joylist_tail == NULL) {
icculus@6734
   242
        SDL_joylist = SDL_joylist_tail = item;
icculus@6734
   243
    } else {
icculus@6734
   244
        SDL_joylist_tail->next = item;
icculus@6734
   245
        SDL_joylist_tail = item;
icculus@6734
   246
    }
icculus@6734
   247
slouken@7916
   248
    /* Need to increment the joystick count before we post the event */
slouken@7916
   249
    ++numjoysticks;
slouken@7916
   250
slouken@12088
   251
    SDL_PrivateJoystickAdded(item->device_instance);
slouken@7802
   252
slouken@7916
   253
    return numjoysticks;
icculus@6734
   254
}
icculus@6734
   255
icculus@6749
   256
#if SDL_USE_LIBUDEV
icculus@6734
   257
/* !!! FIXME: I would love to dump this code and use libudev instead. */
icculus@6734
   258
static int
icculus@6734
   259
MaybeRemoveDevice(const char *path)
icculus@6734
   260
{
icculus@6734
   261
    SDL_joylist_item *item;
icculus@6734
   262
    SDL_joylist_item *prev = NULL;
icculus@6734
   263
icculus@6734
   264
    if (path == NULL) {
icculus@6734
   265
        return -1;
icculus@6734
   266
    }
slouken@6690
   267
icculus@6734
   268
    for (item = SDL_joylist; item != NULL; item = item->next) {
icculus@6734
   269
        /* found it, remove it. */
icculus@6734
   270
        if (SDL_strcmp(path, item->path) == 0) {
icculus@6734
   271
            const int retval = item->device_instance;
icculus@6734
   272
            if (item->hwdata) {
icculus@6752
   273
                item->hwdata->item = NULL;
icculus@6734
   274
            }
icculus@6734
   275
            if (prev != NULL) {
icculus@6734
   276
                prev->next = item->next;
icculus@6734
   277
            } else {
jorgen@6865
   278
                SDL_assert(SDL_joylist == item);
jorgen@6865
   279
                SDL_joylist = item->next;
jorgen@6865
   280
            }
jorgen@6865
   281
            if (item == SDL_joylist_tail) {
jorgen@6865
   282
                SDL_joylist_tail = prev;
icculus@6734
   283
            }
slouken@7802
   284
slouken@7916
   285
            /* Need to decrement the joystick count before we post the event */
slouken@7916
   286
            --numjoysticks;
slouken@7916
   287
slouken@10226
   288
            SDL_PrivateJoystickRemoved(item->device_instance);
slouken@7802
   289
icculus@6734
   290
            SDL_free(item->path);
icculus@6734
   291
            SDL_free(item->name);
icculus@6734
   292
            SDL_free(item);
icculus@6734
   293
            return retval;
icculus@6734
   294
        }
icculus@6734
   295
        prev = item;
icculus@6734
   296
    }
icculus@6734
   297
icculus@6734
   298
    return -1;
icculus@6734
   299
}
icculus@6749
   300
#endif
icculus@6734
   301
slouken@10609
   302
#if ! SDL_USE_LIBUDEV
icculus@6734
   303
static int
icculus@6734
   304
JoystickInitWithoutUdev(void)
icculus@6734
   305
{
icculus@6734
   306
    int i;
icculus@6734
   307
    char path[PATH_MAX];
icculus@6734
   308
icculus@6734
   309
    /* !!! FIXME: only finds sticks if they're called /dev/input/event[0..31] */
icculus@6734
   310
    /* !!! FIXME:  we could at least readdir() through /dev/input...? */
icculus@6734
   311
    /* !!! FIXME:  (or delete this and rely on libudev?) */
icculus@6734
   312
    for (i = 0; i < 32; i++) {
icculus@6734
   313
        SDL_snprintf(path, SDL_arraysize(path), "/dev/input/event%d", i);
icculus@6734
   314
        MaybeAddDevice(path);
icculus@6734
   315
    }
icculus@6734
   316
slouken@12088
   317
    return 0;
icculus@6734
   318
}
slouken@10609
   319
#endif
icculus@6734
   320
icculus@6734
   321
#if SDL_USE_LIBUDEV
icculus@6734
   322
static int
icculus@6734
   323
JoystickInitWithUdev(void)
icculus@6734
   324
{
gabomdq@7772
   325
    if (SDL_UDEV_Init() < 0) {
gabomdq@7772
   326
        return SDL_SetError("Could not initialize UDEV");
icculus@6734
   327
    }
icculus@6734
   328
gabomdq@7772
   329
    /* Set up the udev callback */
slouken@9321
   330
    if (SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
gabomdq@7772
   331
        SDL_UDEV_Quit();
gabomdq@7772
   332
        return SDL_SetError("Could not set up joystick <-> udev callback");
icculus@6734
   333
    }
gabomdq@7772
   334
    
gabomdq@7772
   335
    /* Force a scan to build the initial device list */
gabomdq@7772
   336
    SDL_UDEV_Scan();
icculus@6734
   337
slouken@12088
   338
    return 0;
icculus@6734
   339
}
icculus@6734
   340
#endif
icculus@6734
   341
slouken@11532
   342
static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickGUID guid, int *device_instance)
slouken@11532
   343
{
slouken@11532
   344
    SDL_joylist_item *item;
slouken@11532
   345
slouken@11532
   346
    item = (SDL_joylist_item *) SDL_calloc(1, sizeof (SDL_joylist_item));
slouken@11532
   347
    if (item == NULL) {
slouken@11532
   348
        return SDL_FALSE;
slouken@11532
   349
    }
slouken@11532
   350
slouken@11532
   351
    item->path = SDL_strdup("");
slouken@11532
   352
    item->name = SDL_strdup(name);
slouken@11532
   353
    item->guid = guid;
slouken@11532
   354
    item->m_bSteamController = SDL_TRUE;
slouken@11532
   355
slouken@11532
   356
    if ((item->path == NULL) || (item->name == NULL)) {
slouken@11532
   357
         SDL_free(item->path);
slouken@11532
   358
         SDL_free(item->name);
slouken@11532
   359
         SDL_free(item);
slouken@11532
   360
         return SDL_FALSE;
slouken@11532
   361
    }
slouken@11532
   362
slouken@12088
   363
    *device_instance = item->device_instance = SDL_GetNextJoystickInstanceID();
slouken@11532
   364
    if (SDL_joylist_tail == NULL) {
slouken@11532
   365
        SDL_joylist = SDL_joylist_tail = item;
slouken@11532
   366
    } else {
slouken@11532
   367
        SDL_joylist_tail->next = item;
slouken@11532
   368
        SDL_joylist_tail = item;
slouken@11532
   369
    }
slouken@11532
   370
slouken@11532
   371
    /* Need to increment the joystick count before we post the event */
slouken@11532
   372
    ++numjoysticks;
slouken@11532
   373
slouken@12088
   374
    SDL_PrivateJoystickAdded(item->device_instance);
slouken@11532
   375
slouken@11532
   376
    return SDL_TRUE;
slouken@11532
   377
}
slouken@11532
   378
slouken@11532
   379
static void SteamControllerDisconnectedCallback(int device_instance)
slouken@11532
   380
{
slouken@11532
   381
    SDL_joylist_item *item;
slouken@11532
   382
    SDL_joylist_item *prev = NULL;
slouken@11532
   383
slouken@11532
   384
    for (item = SDL_joylist; item != NULL; item = item->next) {
slouken@11532
   385
        /* found it, remove it. */
slouken@11532
   386
        if (item->device_instance == device_instance) {
slouken@11532
   387
            if (item->hwdata) {
slouken@11532
   388
                item->hwdata->item = NULL;
slouken@11532
   389
            }
slouken@11532
   390
            if (prev != NULL) {
slouken@11532
   391
                prev->next = item->next;
slouken@11532
   392
            } else {
slouken@11532
   393
                SDL_assert(SDL_joylist == item);
slouken@11532
   394
                SDL_joylist = item->next;
slouken@11532
   395
            }
slouken@11532
   396
            if (item == SDL_joylist_tail) {
slouken@11532
   397
                SDL_joylist_tail = prev;
slouken@11532
   398
            }
slouken@11532
   399
slouken@11532
   400
            /* Need to decrement the joystick count before we post the event */
slouken@11532
   401
            --numjoysticks;
slouken@11532
   402
slouken@11532
   403
            SDL_PrivateJoystickRemoved(item->device_instance);
slouken@11532
   404
slouken@11532
   405
            SDL_free(item->name);
slouken@11532
   406
            SDL_free(item);
slouken@11532
   407
            return;
slouken@11532
   408
        }
slouken@11532
   409
        prev = item;
slouken@11532
   410
    }
slouken@11532
   411
}
slouken@11532
   412
slouken@12088
   413
static int
slouken@12088
   414
LINUX_JoystickInit(void)
slouken@0
   415
{
slouken@5317
   416
    /* First see if the user specified one or more joysticks to use */
slouken@1895
   417
    if (SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL) {
slouken@5317
   418
        char *envcopy, *envpath, *delim;
slouken@5317
   419
        envcopy = SDL_strdup(SDL_getenv("SDL_JOYSTICK_DEVICE"));
slouken@5317
   420
        envpath = envcopy;
slouken@5317
   421
        while (envpath != NULL) {
slouken@5317
   422
            delim = SDL_strchr(envpath, ':');
slouken@5317
   423
            if (delim != NULL) {
slouken@5317
   424
                *delim++ = '\0';
slouken@5317
   425
            }
icculus@6734
   426
            MaybeAddDevice(envpath);
slouken@5317
   427
            envpath = delim;
slouken@1895
   428
        }
slouken@5317
   429
        SDL_free(envcopy);
slouken@1895
   430
    }
slouken@554
   431
slouken@11532
   432
    SDL_InitSteamControllers(SteamControllerConnectedCallback,
slouken@11532
   433
                             SteamControllerDisconnectedCallback);
slouken@11532
   434
icculus@6734
   435
#if SDL_USE_LIBUDEV
gabomdq@7772
   436
    return JoystickInitWithUdev();
slouken@10609
   437
#else 
slouken@10609
   438
    return JoystickInitWithoutUdev();
slouken@0
   439
#endif
slouken@0
   440
}
slouken@0
   441
slouken@12088
   442
static int
slouken@12088
   443
LINUX_JoystickGetCount(void)
slouken@6707
   444
{
icculus@6734
   445
    return numjoysticks;
icculus@6734
   446
}
icculus@6734
   447
slouken@12088
   448
static void
slouken@12088
   449
LINUX_JoystickDetect(void)
slouken@6707
   450
{
icculus@6734
   451
#if SDL_USE_LIBUDEV
gabomdq@7772
   452
    SDL_UDEV_Poll();
icculus@6734
   453
#endif
slouken@11532
   454
slouken@11532
   455
    SDL_UpdateSteamControllers();
slouken@6707
   456
}
slouken@6707
   457
icculus@6734
   458
static SDL_joylist_item *
icculus@6734
   459
JoystickByDevIndex(int device_index)
icculus@6734
   460
{
icculus@6734
   461
    SDL_joylist_item *item = SDL_joylist;
icculus@6734
   462
icculus@6734
   463
    if ((device_index < 0) || (device_index >= numjoysticks)) {
icculus@6734
   464
        return NULL;
icculus@6734
   465
    }
icculus@6734
   466
icculus@6734
   467
    while (device_index > 0) {
icculus@6734
   468
        SDL_assert(item != NULL);
icculus@6734
   469
        device_index--;
icculus@6734
   470
        item = item->next;
icculus@6734
   471
    }
icculus@6734
   472
icculus@6734
   473
    return item;
slouken@6707
   474
}
slouken@6707
   475
slouken@0
   476
/* Function to get the device-dependent name of a joystick */
slouken@12088
   477
static const char *
slouken@12088
   478
LINUX_JoystickGetDeviceName(int device_index)
slouken@0
   479
{
icculus@6734
   480
    return JoystickByDevIndex(device_index)->name;
slouken@0
   481
}
slouken@0
   482
slouken@12359
   483
static int
slouken@12359
   484
LINUX_JoystickGetDevicePlayerIndex(int device_index)
slouken@12359
   485
{
slouken@12359
   486
    return -1;
slouken@12359
   487
}
slouken@12359
   488
slouken@12088
   489
static SDL_JoystickGUID
slouken@12088
   490
LINUX_JoystickGetDeviceGUID( int device_index )
slouken@12088
   491
{
slouken@12088
   492
    return JoystickByDevIndex(device_index)->guid;
slouken@12088
   493
}
slouken@12088
   494
slouken@6707
   495
/* Function to perform the mapping from device index to the instance id for this index */
slouken@12088
   496
static SDL_JoystickID
slouken@12088
   497
LINUX_JoystickGetDeviceInstanceID(int device_index)
slouken@6707
   498
{
icculus@6734
   499
    return JoystickByDevIndex(device_index)->device_instance;
slouken@6707
   500
}
slouken@6707
   501
slouken@1895
   502
static int
slouken@1895
   503
allocate_hatdata(SDL_Joystick * joystick)
slouken@0
   504
{
slouken@1895
   505
    int i;
slouken@0
   506
slouken@1895
   507
    joystick->hwdata->hats =
slouken@1895
   508
        (struct hwdata_hat *) SDL_malloc(joystick->nhats *
slouken@1895
   509
                                         sizeof(struct hwdata_hat));
slouken@1895
   510
    if (joystick->hwdata->hats == NULL) {
slouken@1895
   511
        return (-1);
slouken@1895
   512
    }
slouken@1895
   513
    for (i = 0; i < joystick->nhats; ++i) {
slouken@1895
   514
        joystick->hwdata->hats[i].axis[0] = 1;
slouken@1895
   515
        joystick->hwdata->hats[i].axis[1] = 1;
slouken@1895
   516
    }
slouken@1895
   517
    return (0);
slouken@0
   518
}
slouken@0
   519
slouken@1895
   520
static int
slouken@1895
   521
allocate_balldata(SDL_Joystick * joystick)
slouken@0
   522
{
slouken@1895
   523
    int i;
slouken@0
   524
slouken@1895
   525
    joystick->hwdata->balls =
slouken@1895
   526
        (struct hwdata_ball *) SDL_malloc(joystick->nballs *
slouken@1895
   527
                                          sizeof(struct hwdata_ball));
slouken@1895
   528
    if (joystick->hwdata->balls == NULL) {
slouken@1895
   529
        return (-1);
slouken@1895
   530
    }
slouken@1895
   531
    for (i = 0; i < joystick->nballs; ++i) {
slouken@1895
   532
        joystick->hwdata->balls[i].axis[0] = 0;
slouken@1895
   533
        joystick->hwdata->balls[i].axis[1] = 0;
slouken@1895
   534
    }
slouken@1895
   535
    return (0);
slouken@0
   536
}
slouken@0
   537
icculus@6729
   538
static void
icculus@6729
   539
ConfigJoystick(SDL_Joystick * joystick, int fd)
slouken@0
   540
{
slouken@1895
   541
    int i, t;
slouken@3404
   542
    unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
slouken@3404
   543
    unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
slouken@3404
   544
    unsigned long relbit[NBITS(REL_MAX)] = { 0 };
slouken@12088
   545
    unsigned long ffbit[NBITS(FF_MAX)] = { 0 };
slouken@0
   546
slouken@1895
   547
    /* See if this device uses the new unified event API */
slouken@1895
   548
    if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
slouken@1895
   549
        (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
slouken@1895
   550
        (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
slouken@0
   551
slouken@1895
   552
        /* Get the number of buttons, axes, and other thingamajigs */
slouken@1895
   553
        for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
slouken@1895
   554
            if (test_bit(i, keybit)) {
slouken@0
   555
#ifdef DEBUG_INPUT_EVENTS
slouken@1895
   556
                printf("Joystick has button: 0x%x\n", i);
slouken@0
   557
#endif
slouken@10641
   558
                joystick->hwdata->key_map[i] = joystick->nbuttons;
slouken@1895
   559
                ++joystick->nbuttons;
slouken@1895
   560
            }
slouken@1895
   561
        }
slouken@10641
   562
        for (i = 0; i < BTN_JOYSTICK; ++i) {
slouken@1895
   563
            if (test_bit(i, keybit)) {
slouken@0
   564
#ifdef DEBUG_INPUT_EVENTS
slouken@1895
   565
                printf("Joystick has button: 0x%x\n", i);
slouken@0
   566
#endif
slouken@10641
   567
                joystick->hwdata->key_map[i] = joystick->nbuttons;
slouken@1895
   568
                ++joystick->nbuttons;
slouken@1895
   569
            }
slouken@1895
   570
        }
icculus@9630
   571
        for (i = 0; i < ABS_MAX; ++i) {
slouken@1895
   572
            /* Skip hats */
slouken@1895
   573
            if (i == ABS_HAT0X) {
slouken@1895
   574
                i = ABS_HAT3Y;
slouken@1895
   575
                continue;
slouken@1895
   576
            }
slouken@1895
   577
            if (test_bit(i, absbit)) {
slouken@5084
   578
                struct input_absinfo absinfo;
slouken@0
   579
slouken@8053
   580
                if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
slouken@1895
   581
                    continue;
slouken@8053
   582
                }
slouken@0
   583
#ifdef DEBUG_INPUT_EVENTS
slouken@8053
   584
                printf("Joystick has absolute axis: 0x%.2x\n", i);
slouken@1895
   585
                printf("Values = { %d, %d, %d, %d, %d }\n",
slouken@5084
   586
                       absinfo.value, absinfo.minimum, absinfo.maximum,
slouken@5084
   587
                       absinfo.fuzz, absinfo.flat);
slouken@0
   588
#endif /* DEBUG_INPUT_EVENTS */
slouken@1895
   589
                joystick->hwdata->abs_map[i] = joystick->naxes;
slouken@5084
   590
                if (absinfo.minimum == absinfo.maximum) {
slouken@1895
   591
                    joystick->hwdata->abs_correct[i].used = 0;
slouken@1895
   592
                } else {
slouken@1895
   593
                    joystick->hwdata->abs_correct[i].used = 1;
slouken@1895
   594
                    joystick->hwdata->abs_correct[i].coef[0] =
slouken@6845
   595
                        (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
slouken@1895
   596
                    joystick->hwdata->abs_correct[i].coef[1] =
slouken@6845
   597
                        (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
slouken@6845
   598
                    t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
slouken@1895
   599
                    if (t != 0) {
slouken@1895
   600
                        joystick->hwdata->abs_correct[i].coef[2] =
slouken@6845
   601
                            (1 << 28) / t;
slouken@1895
   602
                    } else {
slouken@1895
   603
                        joystick->hwdata->abs_correct[i].coef[2] = 0;
slouken@1895
   604
                    }
slouken@1895
   605
                }
slouken@1895
   606
                ++joystick->naxes;
slouken@1895
   607
            }
slouken@1895
   608
        }
slouken@1895
   609
        for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) {
slouken@1895
   610
            if (test_bit(i, absbit) || test_bit(i + 1, absbit)) {
slouken@8053
   611
                struct input_absinfo absinfo;
slouken@12846
   612
                int hat_index = (i - ABS_HAT0X) / 2;
slouken@8053
   613
slouken@8053
   614
                if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
slouken@8053
   615
                    continue;
slouken@8053
   616
                }
slouken@0
   617
#ifdef DEBUG_INPUT_EVENTS
slouken@12846
   618
                printf("Joystick has hat %d\n", hat_index);
slouken@8053
   619
                printf("Values = { %d, %d, %d, %d, %d }\n",
slouken@8053
   620
                       absinfo.value, absinfo.minimum, absinfo.maximum,
slouken@8053
   621
                       absinfo.fuzz, absinfo.flat);
slouken@8053
   622
#endif /* DEBUG_INPUT_EVENTS */
slouken@12846
   623
                joystick->hwdata->hats_indices[joystick->nhats++] = hat_index;
slouken@1895
   624
            }
slouken@1895
   625
        }
slouken@1895
   626
        if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
slouken@1895
   627
            ++joystick->nballs;
slouken@1895
   628
        }
slouken@0
   629
slouken@1895
   630
        /* Allocate data to keep track of these thingamajigs */
slouken@1895
   631
        if (joystick->nhats > 0) {
slouken@1895
   632
            if (allocate_hatdata(joystick) < 0) {
slouken@1895
   633
                joystick->nhats = 0;
slouken@1895
   634
            }
slouken@1895
   635
        }
slouken@1895
   636
        if (joystick->nballs > 0) {
slouken@1895
   637
            if (allocate_balldata(joystick) < 0) {
slouken@1895
   638
                joystick->nballs = 0;
slouken@1895
   639
            }
slouken@1895
   640
        }
slouken@1895
   641
    }
slouken@12088
   642
slouken@12088
   643
    if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(ffbit)), ffbit) >= 0) {
slouken@12088
   644
        if (test_bit(FF_RUMBLE, ffbit)) {
slouken@12088
   645
            joystick->hwdata->ff_rumble = SDL_TRUE;
slouken@12088
   646
        }
slouken@12088
   647
        if (test_bit(FF_SINE, ffbit)) {
slouken@12088
   648
            joystick->hwdata->ff_sine = SDL_TRUE;
slouken@12088
   649
        }
slouken@12088
   650
    }
slouken@0
   651
}
slouken@0
   652
slouken@892
   653
slouken@0
   654
/* Function to open a joystick for use.
philipp@9380
   655
   The joystick to open is specified by the device index.
slouken@0
   656
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@0
   657
   It returns 0, or -1 if there is an error.
slouken@0
   658
 */
slouken@12088
   659
static int
slouken@12088
   660
LINUX_JoystickOpen(SDL_Joystick * joystick, int device_index)
slouken@0
   661
{
icculus@6734
   662
    SDL_joylist_item *item = JoystickByDevIndex(device_index);
slouken@892
   663
icculus@6734
   664
    if (item == NULL) {
icculus@7037
   665
        return SDL_SetError("No such device");
icculus@6734
   666
    }
slouken@892
   667
icculus@6751
   668
    joystick->instance_id = item->device_instance;
slouken@1895
   669
    joystick->hwdata = (struct joystick_hwdata *)
slouken@11532
   670
        SDL_calloc(1, sizeof(*joystick->hwdata));
slouken@1895
   671
    if (joystick->hwdata == NULL) {
icculus@7037
   672
        return SDL_OutOfMemory();
slouken@1895
   673
    }
icculus@6752
   674
    joystick->hwdata->item = item;
icculus@6734
   675
    joystick->hwdata->guid = item->guid;
slouken@12088
   676
    joystick->hwdata->effect.id = -1;
slouken@11532
   677
    joystick->hwdata->m_bSteamController = item->m_bSteamController;
slouken@12410
   678
    SDL_memset(joystick->hwdata->abs_map, 0xFF, sizeof(joystick->hwdata->abs_map));
slouken@11532
   679
slouken@11532
   680
    if (item->m_bSteamController) {
slouken@11532
   681
        joystick->hwdata->fd = -1;
slouken@11532
   682
        SDL_GetSteamControllerInputs(&joystick->nbuttons,
slouken@11532
   683
                                     &joystick->naxes,
slouken@11532
   684
                                     &joystick->nhats);
slouken@11532
   685
    } else {
slouken@12088
   686
        int fd = open(item->path, O_RDWR, 0);
slouken@11532
   687
        if (fd < 0) {
slouken@11532
   688
            SDL_free(joystick->hwdata);
slouken@11532
   689
            joystick->hwdata = NULL;
slouken@11532
   690
            return SDL_SetError("Unable to open %s", item->path);
slouken@11532
   691
        }
slouken@11532
   692
slouken@11532
   693
        joystick->hwdata->fd = fd;
slouken@11532
   694
        joystick->hwdata->fname = SDL_strdup(item->path);
slouken@11532
   695
        if (joystick->hwdata->fname == NULL) {
slouken@11532
   696
            SDL_free(joystick->hwdata);
slouken@11532
   697
            joystick->hwdata = NULL;
slouken@11532
   698
            close(fd);
slouken@11532
   699
            return SDL_OutOfMemory();
slouken@11532
   700
        }
slouken@11532
   701
slouken@11532
   702
        /* Set the joystick to non-blocking read mode */
slouken@11532
   703
        fcntl(fd, F_SETFL, O_NONBLOCK);
slouken@11532
   704
slouken@11532
   705
        /* Get the number of buttons and axes on the joystick */
slouken@11532
   706
        ConfigJoystick(joystick, fd);
icculus@6734
   707
    }
icculus@6734
   708
icculus@6734
   709
    SDL_assert(item->hwdata == NULL);
icculus@6734
   710
    item->hwdata = joystick->hwdata;
slouken@0
   711
slouken@7191
   712
    /* mark joystick as fresh and ready */
slouken@6844
   713
    joystick->hwdata->fresh = 1;
slouken@6844
   714
slouken@1895
   715
    return (0);
slouken@0
   716
}
slouken@0
   717
slouken@12088
   718
static int
slouken@12088
   719
LINUX_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
slouken@12088
   720
{
slouken@12088
   721
    struct input_event event;
slouken@12088
   722
slouken@12333
   723
    if (joystick->hwdata->ff_rumble) {
slouken@12333
   724
        struct ff_effect *effect = &joystick->hwdata->effect;
slouken@12088
   725
slouken@12333
   726
        effect->type = FF_RUMBLE;
slouken@12333
   727
        effect->replay.length = SDL_min(duration_ms, 32767);
slouken@12333
   728
        effect->u.rumble.strong_magnitude = low_frequency_rumble;
slouken@12333
   729
        effect->u.rumble.weak_magnitude = high_frequency_rumble;
slouken@12333
   730
    } else if (joystick->hwdata->ff_sine) {
slouken@12333
   731
        /* Scale and average the two rumble strengths */
slouken@12333
   732
        Sint16 magnitude = (Sint16)(((low_frequency_rumble / 2) + (high_frequency_rumble / 2)) / 2);
slouken@12333
   733
        struct ff_effect *effect = &joystick->hwdata->effect;
slouken@12088
   734
slouken@12333
   735
        effect->type = FF_PERIODIC;
slouken@12333
   736
        effect->replay.length = SDL_min(duration_ms, 32767);
slouken@12333
   737
        effect->u.periodic.waveform = FF_SINE;
slouken@12333
   738
        effect->u.periodic.magnitude = magnitude;
slouken@12333
   739
    } else {
slouken@12333
   740
        return SDL_Unsupported();
slouken@12088
   741
    }
slouken@12088
   742
slouken@12088
   743
    if (ioctl(joystick->hwdata->fd, EVIOCSFF, &joystick->hwdata->effect) < 0) {
slouken@12088
   744
        return SDL_SetError("Couldn't update rumble effect: %s", strerror(errno));
slouken@12088
   745
    }
slouken@12088
   746
slouken@12088
   747
    event.type = EV_FF;
slouken@12088
   748
    event.code = joystick->hwdata->effect.id;
slouken@12088
   749
    event.value = 1;
slouken@12088
   750
    if (write(joystick->hwdata->fd, &event, sizeof(event)) < 0) {
slouken@12088
   751
        return SDL_SetError("Couldn't start rumble effect: %s", strerror(errno));
slouken@12088
   752
    }
slouken@12088
   753
    return 0;
slouken@12088
   754
}
slouken@12088
   755
slouken@7860
   756
static SDL_INLINE void
slouken@1895
   757
HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value)
slouken@0
   758
{
slouken@1895
   759
    struct hwdata_hat *the_hat;
slouken@1895
   760
    const Uint8 position_map[3][3] = {
slouken@1895
   761
        {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
slouken@1895
   762
        {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
slouken@1895
   763
        {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
slouken@1895
   764
    };
slouken@0
   765
slouken@12846
   766
    the_hat = &stick->hwdata->hats[stick->hwdata->hats_indices[hat]];
slouken@1895
   767
    if (value < 0) {
slouken@1895
   768
        value = 0;
slouken@1895
   769
    } else if (value == 0) {
slouken@1895
   770
        value = 1;
slouken@1895
   771
    } else if (value > 0) {
slouken@1895
   772
        value = 2;
slouken@1895
   773
    }
slouken@1895
   774
    if (value != the_hat->axis[axis]) {
slouken@1895
   775
        the_hat->axis[axis] = value;
slouken@12847
   776
        SDL_PrivateJoystickHat(stick, stick->hwdata->hats_indices[hat],
slouken@12847
   777
                               position_map[the_hat->axis[1]][the_hat->axis[0]]);
slouken@1895
   778
    }
slouken@0
   779
}
slouken@0
   780
slouken@7860
   781
static SDL_INLINE void
slouken@1895
   782
HandleBall(SDL_Joystick * stick, Uint8 ball, int axis, int value)
slouken@0
   783
{
slouken@1895
   784
    stick->hwdata->balls[ball].axis[axis] += value;
slouken@0
   785
}
slouken@0
   786
slouken@0
   787
slouken@7860
   788
static SDL_INLINE int
icculus@6729
   789
AxisCorrect(SDL_Joystick * joystick, int which, int value)
slouken@0
   790
{
slouken@1895
   791
    struct axis_correct *correct;
slouken@0
   792
slouken@1895
   793
    correct = &joystick->hwdata->abs_correct[which];
slouken@1895
   794
    if (correct->used) {
slouken@6845
   795
        value *= 2;
slouken@1895
   796
        if (value > correct->coef[0]) {
slouken@1895
   797
            if (value < correct->coef[1]) {
slouken@1895
   798
                return 0;
slouken@1895
   799
            }
slouken@1895
   800
            value -= correct->coef[1];
slouken@1895
   801
        } else {
slouken@1895
   802
            value -= correct->coef[0];
slouken@1895
   803
        }
slouken@1895
   804
        value *= correct->coef[2];
slouken@6845
   805
        value >>= 13;
slouken@1895
   806
    }
slouken@554
   807
slouken@1895
   808
    /* Clamp and return */
slouken@1895
   809
    if (value < -32768)
slouken@1895
   810
        return -32768;
slouken@1895
   811
    if (value > 32767)
slouken@1895
   812
        return 32767;
slouken@554
   813
slouken@1895
   814
    return value;
slouken@0
   815
}
slouken@0
   816
slouken@7860
   817
static SDL_INLINE void
slouken@6844
   818
PollAllValues(SDL_Joystick * joystick)
slouken@6844
   819
{
slouken@6844
   820
    struct input_absinfo absinfo;
slouken@12793
   821
    int i;
slouken@6844
   822
slouken@7191
   823
    /* Poll all axis */
slouken@12793
   824
    for (i = ABS_X; i < ABS_MAX; i++) {
slouken@12793
   825
        if (i == ABS_HAT0X) {
slouken@12793
   826
            i = ABS_HAT3Y;
slouken@12793
   827
            continue;
slouken@12793
   828
        }
slouken@12793
   829
        if (joystick->hwdata->abs_correct[i].used) {
slouken@12793
   830
            if (ioctl(joystick->hwdata->fd, EVIOCGABS(i), &absinfo) >= 0) {
slouken@12793
   831
                absinfo.value = AxisCorrect(joystick, i, absinfo.value);
slouken@6844
   832
slouken@6844
   833
#ifdef DEBUG_INPUT_EVENTS
slouken@12793
   834
                printf("Joystick : Re-read Axis %d (%d) val= %d\n",
slouken@12793
   835
                    joystick->hwdata->abs_map[i], i, absinfo.value);
slouken@6844
   836
#endif
slouken@12793
   837
                SDL_PrivateJoystickAxis(joystick,
slouken@12793
   838
                        joystick->hwdata->abs_map[i],
slouken@12793
   839
                        absinfo.value);
slouken@6844
   840
            }
slouken@6844
   841
        }
slouken@6844
   842
    }
slouken@6844
   843
}
slouken@6844
   844
slouken@7860
   845
static SDL_INLINE void
icculus@6729
   846
HandleInputEvents(SDL_Joystick * joystick)
slouken@0
   847
{
slouken@1895
   848
    struct input_event events[32];
slouken@1895
   849
    int i, len;
slouken@1895
   850
    int code;
slouken@0
   851
slouken@6844
   852
    if (joystick->hwdata->fresh) {
slouken@6844
   853
        PollAllValues(joystick);
slouken@6844
   854
        joystick->hwdata->fresh = 0;
slouken@6844
   855
    }
slouken@6844
   856
slouken@1895
   857
    while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
slouken@1895
   858
        len /= sizeof(events[0]);
slouken@1895
   859
        for (i = 0; i < len; ++i) {
slouken@1895
   860
            code = events[i].code;
slouken@1895
   861
            switch (events[i].type) {
slouken@1895
   862
            case EV_KEY:
slouken@10641
   863
                SDL_PrivateJoystickButton(joystick,
slouken@10641
   864
                                          joystick->hwdata->key_map[code],
slouken@10641
   865
                                          events[i].value);
slouken@1895
   866
                break;
slouken@1895
   867
            case EV_ABS:
slouken@1895
   868
                switch (code) {
slouken@1895
   869
                case ABS_HAT0X:
slouken@1895
   870
                case ABS_HAT0Y:
slouken@1895
   871
                case ABS_HAT1X:
slouken@1895
   872
                case ABS_HAT1Y:
slouken@1895
   873
                case ABS_HAT2X:
slouken@1895
   874
                case ABS_HAT2Y:
slouken@1895
   875
                case ABS_HAT3X:
slouken@1895
   876
                case ABS_HAT3Y:
slouken@1895
   877
                    code -= ABS_HAT0X;
slouken@1895
   878
                    HandleHat(joystick, code / 2, code % 2, events[i].value);
slouken@1895
   879
                    break;
slouken@1895
   880
                default:
slouken@12410
   881
                    if (joystick->hwdata->abs_map[code] != 0xFF) {
slouken@12410
   882
                        events[i].value =
slouken@12410
   883
                            AxisCorrect(joystick, code, events[i].value);
slouken@12410
   884
                        SDL_PrivateJoystickAxis(joystick,
slouken@12410
   885
                                                joystick->hwdata->abs_map[code],
slouken@12410
   886
                                                events[i].value);
slouken@12410
   887
                    }
slouken@1895
   888
                    break;
slouken@1895
   889
                }
slouken@1895
   890
                break;
slouken@1895
   891
            case EV_REL:
slouken@1895
   892
                switch (code) {
slouken@1895
   893
                case REL_X:
slouken@1895
   894
                case REL_Y:
slouken@1895
   895
                    code -= REL_X;
slouken@1895
   896
                    HandleBall(joystick, code / 2, code % 2, events[i].value);
slouken@1895
   897
                    break;
slouken@1895
   898
                default:
slouken@1895
   899
                    break;
slouken@1895
   900
                }
slouken@1895
   901
                break;
slouken@6844
   902
            case EV_SYN:
slouken@6844
   903
                switch (code) {
slouken@6844
   904
                case SYN_DROPPED :
slouken@6844
   905
#ifdef DEBUG_INPUT_EVENTS
philipp@7133
   906
                    printf("Event SYN_DROPPED detected\n");
slouken@6844
   907
#endif
slouken@6844
   908
                    PollAllValues(joystick);
slouken@6844
   909
                    break;
slouken@6844
   910
                default:
slouken@6844
   911
                    break;
slouken@6844
   912
                }
slouken@1895
   913
            default:
slouken@1895
   914
                break;
slouken@1895
   915
            }
slouken@1895
   916
        }
slouken@1895
   917
    }
slouken@0
   918
}
slouken@0
   919
slouken@12088
   920
static void
slouken@12088
   921
LINUX_JoystickUpdate(SDL_Joystick * joystick)
slouken@0
   922
{
slouken@1895
   923
    int i;
slouken@1895
   924
slouken@11532
   925
    if (joystick->hwdata->m_bSteamController) {
slouken@11532
   926
        SDL_UpdateSteamController(joystick);
slouken@11532
   927
        return;
slouken@11532
   928
    }
slouken@11532
   929
icculus@6729
   930
    HandleInputEvents(joystick);
slouken@0
   931
slouken@1895
   932
    /* Deliver ball motion updates */
slouken@1895
   933
    for (i = 0; i < joystick->nballs; ++i) {
slouken@1895
   934
        int xrel, yrel;
slouken@0
   935
slouken@1895
   936
        xrel = joystick->hwdata->balls[i].axis[0];
slouken@1895
   937
        yrel = joystick->hwdata->balls[i].axis[1];
slouken@1895
   938
        if (xrel || yrel) {
slouken@1895
   939
            joystick->hwdata->balls[i].axis[0] = 0;
slouken@1895
   940
            joystick->hwdata->balls[i].axis[1] = 0;
slouken@1895
   941
            SDL_PrivateJoystickBall(joystick, (Uint8) i, xrel, yrel);
slouken@1895
   942
        }
slouken@1895
   943
    }
slouken@0
   944
}
slouken@0
   945
slouken@0
   946
/* Function to close a joystick after use */
slouken@12088
   947
static void
slouken@12088
   948
LINUX_JoystickClose(SDL_Joystick * joystick)
slouken@0
   949
{
slouken@1895
   950
    if (joystick->hwdata) {
slouken@12088
   951
        if (joystick->hwdata->effect.id >= 0) {
slouken@12088
   952
            ioctl(joystick->hwdata->fd, EVIOCRMFF, joystick->hwdata->effect.id);
slouken@12088
   953
            joystick->hwdata->effect.id = -1;
slouken@12088
   954
        }
slouken@11532
   955
        if (joystick->hwdata->fd >= 0) {
slouken@11532
   956
            close(joystick->hwdata->fd);
slouken@11532
   957
        }
icculus@6752
   958
        if (joystick->hwdata->item) {
icculus@6752
   959
            joystick->hwdata->item->hwdata = NULL;
icculus@6752
   960
        }
icculus@6734
   961
        SDL_free(joystick->hwdata->hats);
icculus@6734
   962
        SDL_free(joystick->hwdata->balls);
icculus@6734
   963
        SDL_free(joystick->hwdata->fname);
slouken@1895
   964
        SDL_free(joystick->hwdata);
slouken@1895
   965
    }
slouken@0
   966
}
slouken@0
   967
slouken@0
   968
/* Function to perform any system-specific joystick related cleanup */
slouken@12088
   969
static void
slouken@12088
   970
LINUX_JoystickQuit(void)
slouken@0
   971
{
icculus@6734
   972
    SDL_joylist_item *item = NULL;
icculus@6734
   973
    SDL_joylist_item *next = NULL;
icculus@6734
   974
icculus@6734
   975
    for (item = SDL_joylist; item; item = next) {
icculus@6734
   976
        next = item->next;
icculus@6734
   977
        SDL_free(item->path);
icculus@6734
   978
        SDL_free(item->name);
icculus@6734
   979
        SDL_free(item);
icculus@6734
   980
    }
icculus@6734
   981
icculus@6734
   982
    SDL_joylist = SDL_joylist_tail = NULL;
slouken@0
   983
icculus@6734
   984
    numjoysticks = 0;
icculus@6734
   985
icculus@6734
   986
#if SDL_USE_LIBUDEV
gabomdq@7772
   987
    SDL_UDEV_DelCallback(joystick_udev_callback);
gabomdq@7772
   988
    SDL_UDEV_Quit();
icculus@6734
   989
#endif
slouken@11532
   990
slouken@11532
   991
    SDL_QuitSteamControllers();
slouken@0
   992
}
slouken@0
   993
slouken@12088
   994
SDL_JoystickDriver SDL_LINUX_JoystickDriver =
slouken@6690
   995
{
slouken@12088
   996
    LINUX_JoystickInit,
slouken@12088
   997
    LINUX_JoystickGetCount,
slouken@12088
   998
    LINUX_JoystickDetect,
slouken@12088
   999
    LINUX_JoystickGetDeviceName,
slouken@12359
  1000
    LINUX_JoystickGetDevicePlayerIndex,
slouken@12088
  1001
    LINUX_JoystickGetDeviceGUID,
slouken@12088
  1002
    LINUX_JoystickGetDeviceInstanceID,
slouken@12088
  1003
    LINUX_JoystickOpen,
slouken@12088
  1004
    LINUX_JoystickRumble,
slouken@12088
  1005
    LINUX_JoystickUpdate,
slouken@12088
  1006
    LINUX_JoystickClose,
slouken@12088
  1007
    LINUX_JoystickQuit,
slouken@12088
  1008
};
slouken@6690
  1009
slouken@1635
  1010
#endif /* SDL_JOYSTICK_LINUX */
slouken@6693
  1011
slouken@1895
  1012
/* vi: set ts=4 sw=4 expandtab: */