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