src/joystick/linux/SDL_sysjoystick.c
author Gabriel Jacobo <gabomdq@gmail.com>
Wed, 06 Nov 2013 09:48:45 -0300
changeset 7909 4434ab25abf3
parent 7860 2b0bcdea3a79
child 7916 8f16728ce87b
permissions -rw-r--r--
Fixes Bug 1944 - Linux events, joysticks having only hat are not read
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@1402
    21
#include "SDL_config.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
slouken@0
    29
/* This is the system specific header for 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
icculus@6734
    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 */
slouken@892
    62
slouken@0
    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;
icculus@6734
    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
icculus@6734
   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@7802
   237
    /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceAdded() function? */
slouken@7802
   238
#if !SDL_EVENTS_DISABLED
slouken@7802
   239
    event.type = SDL_JOYDEVICEADDED;
slouken@7802
   240
slouken@7802
   241
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@7802
   242
        event.jdevice.which = numjoysticks;
slouken@7802
   243
        if ( (SDL_EventOK == NULL) ||
slouken@7802
   244
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
slouken@7802
   245
            SDL_PushEvent(&event);
slouken@7802
   246
        }
slouken@7802
   247
    }
slouken@7802
   248
#endif /* !SDL_EVENTS_DISABLED */
slouken@7802
   249
icculus@6734
   250
    return numjoysticks++;
icculus@6734
   251
}
icculus@6734
   252
icculus@6749
   253
#if SDL_USE_LIBUDEV
icculus@6734
   254
/* !!! FIXME: I would love to dump this code and use libudev instead. */
icculus@6734
   255
static int
icculus@6734
   256
MaybeRemoveDevice(const char *path)
icculus@6734
   257
{
icculus@6734
   258
    SDL_joylist_item *item;
icculus@6734
   259
    SDL_joylist_item *prev = NULL;
slouken@7802
   260
#if !SDL_EVENTS_DISABLED
slouken@7802
   261
    SDL_Event event;
slouken@7802
   262
#endif
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@7802
   285
            /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceRemoved() function? */
slouken@7802
   286
#if !SDL_EVENTS_DISABLED
slouken@7802
   287
            event.type = SDL_JOYDEVICEREMOVED;
slouken@7802
   288
slouken@7802
   289
            if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@7802
   290
                event.jdevice.which = item->device_instance;
slouken@7802
   291
                if ( (SDL_EventOK == NULL) ||
slouken@7802
   292
                     (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
slouken@7802
   293
                    SDL_PushEvent(&event);
slouken@7802
   294
                }
slouken@7802
   295
            }
slouken@7802
   296
#endif /* !SDL_EVENTS_DISABLED */
slouken@7802
   297
icculus@6734
   298
            SDL_free(item->path);
icculus@6734
   299
            SDL_free(item->name);
icculus@6734
   300
            SDL_free(item);
icculus@6734
   301
            numjoysticks--;
icculus@6734
   302
            return retval;
icculus@6734
   303
        }
icculus@6734
   304
        prev = item;
icculus@6734
   305
    }
icculus@6734
   306
icculus@6734
   307
    return -1;
icculus@6734
   308
}
icculus@6749
   309
#endif
icculus@6734
   310
icculus@6734
   311
static int
icculus@6734
   312
JoystickInitWithoutUdev(void)
icculus@6734
   313
{
icculus@6734
   314
    int i;
icculus@6734
   315
    char path[PATH_MAX];
icculus@6734
   316
icculus@6734
   317
    /* !!! FIXME: only finds sticks if they're called /dev/input/event[0..31] */
icculus@6734
   318
    /* !!! FIXME:  we could at least readdir() through /dev/input...? */
icculus@6734
   319
    /* !!! FIXME:  (or delete this and rely on libudev?) */
icculus@6734
   320
    for (i = 0; i < 32; i++) {
icculus@6734
   321
        SDL_snprintf(path, SDL_arraysize(path), "/dev/input/event%d", i);
icculus@6734
   322
        MaybeAddDevice(path);
icculus@6734
   323
    }
icculus@6734
   324
icculus@6734
   325
    return numjoysticks;
icculus@6734
   326
}
icculus@6734
   327
icculus@6734
   328
icculus@6734
   329
#if SDL_USE_LIBUDEV
icculus@6734
   330
static int
icculus@6734
   331
JoystickInitWithUdev(void)
icculus@6734
   332
{
icculus@6734
   333
gabomdq@7772
   334
    if (SDL_UDEV_Init() < 0) {
gabomdq@7772
   335
        return SDL_SetError("Could not initialize UDEV");
icculus@6734
   336
    }
icculus@6734
   337
gabomdq@7772
   338
    /* Set up the udev callback */
gabomdq@7772
   339
    if ( SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
gabomdq@7772
   340
        SDL_UDEV_Quit();
gabomdq@7772
   341
        return SDL_SetError("Could not set up joystick <-> udev callback");
icculus@6734
   342
    }
gabomdq@7772
   343
    
gabomdq@7772
   344
    /* Force a scan to build the initial device list */
gabomdq@7772
   345
    SDL_UDEV_Scan();
icculus@6734
   346
icculus@6734
   347
    return numjoysticks;
icculus@6734
   348
}
icculus@6734
   349
#endif
icculus@6734
   350
slouken@1895
   351
int
slouken@1895
   352
SDL_SYS_JoystickInit(void)
slouken@0
   353
{
slouken@5317
   354
    /* First see if the user specified one or more joysticks to use */
slouken@1895
   355
    if (SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL) {
slouken@5317
   356
        char *envcopy, *envpath, *delim;
slouken@5317
   357
        envcopy = SDL_strdup(SDL_getenv("SDL_JOYSTICK_DEVICE"));
slouken@5317
   358
        envpath = envcopy;
slouken@5317
   359
        while (envpath != NULL) {
slouken@5317
   360
            delim = SDL_strchr(envpath, ':');
slouken@5317
   361
            if (delim != NULL) {
slouken@5317
   362
                *delim++ = '\0';
slouken@5317
   363
            }
icculus@6734
   364
            MaybeAddDevice(envpath);
slouken@5317
   365
            envpath = delim;
slouken@1895
   366
        }
slouken@5317
   367
        SDL_free(envcopy);
slouken@1895
   368
    }
slouken@554
   369
icculus@6734
   370
#if SDL_USE_LIBUDEV
gabomdq@7772
   371
    return JoystickInitWithUdev();
slouken@0
   372
#endif
slouken@0
   373
icculus@6734
   374
    return JoystickInitWithoutUdev();
slouken@0
   375
}
slouken@0
   376
slouken@6707
   377
int SDL_SYS_NumJoysticks()
slouken@6707
   378
{
icculus@6734
   379
    return numjoysticks;
icculus@6734
   380
}
icculus@6734
   381
slouken@6707
   382
void SDL_SYS_JoystickDetect()
slouken@6707
   383
{
icculus@6734
   384
#if SDL_USE_LIBUDEV
gabomdq@7772
   385
    SDL_UDEV_Poll();
icculus@6734
   386
#endif
gabomdq@7772
   387
    
slouken@6707
   388
}
slouken@6707
   389
slouken@6707
   390
SDL_bool SDL_SYS_JoystickNeedsPolling()
slouken@6707
   391
{
gabomdq@7772
   392
#if SDL_USE_LIBUDEV
gabomdq@7772
   393
    return SDL_TRUE;
gabomdq@7772
   394
#endif
gabomdq@7772
   395
    
gabomdq@7772
   396
    return SDL_FALSE;
icculus@6734
   397
}
icculus@6734
   398
icculus@6734
   399
static SDL_joylist_item *
icculus@6734
   400
JoystickByDevIndex(int device_index)
icculus@6734
   401
{
icculus@6734
   402
    SDL_joylist_item *item = SDL_joylist;
icculus@6734
   403
icculus@6734
   404
    if ((device_index < 0) || (device_index >= numjoysticks)) {
icculus@6734
   405
        return NULL;
icculus@6734
   406
    }
icculus@6734
   407
icculus@6734
   408
    while (device_index > 0) {
icculus@6734
   409
        SDL_assert(item != NULL);
icculus@6734
   410
        device_index--;
icculus@6734
   411
        item = item->next;
icculus@6734
   412
    }
icculus@6734
   413
icculus@6734
   414
    return item;
slouken@6707
   415
}
slouken@6707
   416
slouken@0
   417
/* Function to get the device-dependent name of a joystick */
slouken@1895
   418
const char *
slouken@6707
   419
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
slouken@0
   420
{
icculus@6734
   421
    return JoystickByDevIndex(device_index)->name;
slouken@0
   422
}
slouken@0
   423
slouken@6707
   424
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6707
   425
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
slouken@6707
   426
{
icculus@6734
   427
    return JoystickByDevIndex(device_index)->device_instance;
slouken@6707
   428
}
slouken@6707
   429
slouken@1895
   430
static int
slouken@1895
   431
allocate_hatdata(SDL_Joystick * joystick)
slouken@0
   432
{
slouken@1895
   433
    int i;
slouken@0
   434
slouken@1895
   435
    joystick->hwdata->hats =
slouken@1895
   436
        (struct hwdata_hat *) SDL_malloc(joystick->nhats *
slouken@1895
   437
                                         sizeof(struct hwdata_hat));
slouken@1895
   438
    if (joystick->hwdata->hats == NULL) {
slouken@1895
   439
        return (-1);
slouken@1895
   440
    }
slouken@1895
   441
    for (i = 0; i < joystick->nhats; ++i) {
slouken@1895
   442
        joystick->hwdata->hats[i].axis[0] = 1;
slouken@1895
   443
        joystick->hwdata->hats[i].axis[1] = 1;
slouken@1895
   444
    }
slouken@1895
   445
    return (0);
slouken@0
   446
}
slouken@0
   447
slouken@1895
   448
static int
slouken@1895
   449
allocate_balldata(SDL_Joystick * joystick)
slouken@0
   450
{
slouken@1895
   451
    int i;
slouken@0
   452
slouken@1895
   453
    joystick->hwdata->balls =
slouken@1895
   454
        (struct hwdata_ball *) SDL_malloc(joystick->nballs *
slouken@1895
   455
                                          sizeof(struct hwdata_ball));
slouken@1895
   456
    if (joystick->hwdata->balls == NULL) {
slouken@1895
   457
        return (-1);
slouken@1895
   458
    }
slouken@1895
   459
    for (i = 0; i < joystick->nballs; ++i) {
slouken@1895
   460
        joystick->hwdata->balls[i].axis[0] = 0;
slouken@1895
   461
        joystick->hwdata->balls[i].axis[1] = 0;
slouken@1895
   462
    }
slouken@1895
   463
    return (0);
slouken@0
   464
}
slouken@0
   465
icculus@6729
   466
static void
icculus@6729
   467
ConfigJoystick(SDL_Joystick * joystick, int fd)
slouken@0
   468
{
slouken@1895
   469
    int i, t;
slouken@3404
   470
    unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
slouken@3404
   471
    unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
slouken@3404
   472
    unsigned long relbit[NBITS(REL_MAX)] = { 0 };
slouken@0
   473
slouken@1895
   474
    /* See if this device uses the new unified event API */
slouken@1895
   475
    if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
slouken@1895
   476
        (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
slouken@1895
   477
        (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
slouken@0
   478
slouken@1895
   479
        /* Get the number of buttons, axes, and other thingamajigs */
slouken@1895
   480
        for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
slouken@1895
   481
            if (test_bit(i, keybit)) {
slouken@0
   482
#ifdef DEBUG_INPUT_EVENTS
slouken@1895
   483
                printf("Joystick has button: 0x%x\n", i);
slouken@0
   484
#endif
slouken@1895
   485
                joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
slouken@1895
   486
                ++joystick->nbuttons;
slouken@1895
   487
            }
slouken@1895
   488
        }
slouken@1895
   489
        for (i = BTN_MISC; i < BTN_JOYSTICK; ++i) {
slouken@1895
   490
            if (test_bit(i, keybit)) {
slouken@0
   491
#ifdef DEBUG_INPUT_EVENTS
slouken@1895
   492
                printf("Joystick has button: 0x%x\n", i);
slouken@0
   493
#endif
slouken@1895
   494
                joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
slouken@1895
   495
                ++joystick->nbuttons;
slouken@1895
   496
            }
slouken@1895
   497
        }
icculus@5562
   498
        for (i = 0; i < ABS_MISC; ++i) {
slouken@1895
   499
            /* Skip hats */
slouken@1895
   500
            if (i == ABS_HAT0X) {
slouken@1895
   501
                i = ABS_HAT3Y;
slouken@1895
   502
                continue;
slouken@1895
   503
            }
slouken@1895
   504
            if (test_bit(i, absbit)) {
slouken@5084
   505
                struct input_absinfo absinfo;
slouken@0
   506
slouken@5085
   507
                if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0)
slouken@1895
   508
                    continue;
slouken@0
   509
#ifdef DEBUG_INPUT_EVENTS
slouken@1895
   510
                printf("Joystick has absolute axis: %x\n", i);
slouken@1895
   511
                printf("Values = { %d, %d, %d, %d, %d }\n",
slouken@5084
   512
                       absinfo.value, absinfo.minimum, absinfo.maximum,
slouken@5084
   513
                       absinfo.fuzz, absinfo.flat);
slouken@0
   514
#endif /* DEBUG_INPUT_EVENTS */
slouken@1895
   515
                joystick->hwdata->abs_map[i] = joystick->naxes;
slouken@5084
   516
                if (absinfo.minimum == absinfo.maximum) {
slouken@1895
   517
                    joystick->hwdata->abs_correct[i].used = 0;
slouken@1895
   518
                } else {
slouken@1895
   519
                    joystick->hwdata->abs_correct[i].used = 1;
slouken@1895
   520
                    joystick->hwdata->abs_correct[i].coef[0] =
slouken@6845
   521
                        (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
slouken@1895
   522
                    joystick->hwdata->abs_correct[i].coef[1] =
slouken@6845
   523
                        (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
slouken@6845
   524
                    t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
slouken@1895
   525
                    if (t != 0) {
slouken@1895
   526
                        joystick->hwdata->abs_correct[i].coef[2] =
slouken@6845
   527
                            (1 << 28) / t;
slouken@1895
   528
                    } else {
slouken@1895
   529
                        joystick->hwdata->abs_correct[i].coef[2] = 0;
slouken@1895
   530
                    }
slouken@1895
   531
                }
slouken@1895
   532
                ++joystick->naxes;
slouken@1895
   533
            }
slouken@1895
   534
        }
slouken@1895
   535
        for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) {
slouken@1895
   536
            if (test_bit(i, absbit) || test_bit(i + 1, absbit)) {
slouken@0
   537
#ifdef DEBUG_INPUT_EVENTS
slouken@1895
   538
                printf("Joystick has hat %d\n", (i - ABS_HAT0X) / 2);
slouken@0
   539
#endif
slouken@1895
   540
                ++joystick->nhats;
slouken@1895
   541
            }
slouken@1895
   542
        }
slouken@1895
   543
        if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
slouken@1895
   544
            ++joystick->nballs;
slouken@1895
   545
        }
slouken@0
   546
slouken@1895
   547
        /* Allocate data to keep track of these thingamajigs */
slouken@1895
   548
        if (joystick->nhats > 0) {
slouken@1895
   549
            if (allocate_hatdata(joystick) < 0) {
slouken@1895
   550
                joystick->nhats = 0;
slouken@1895
   551
            }
slouken@1895
   552
        }
slouken@1895
   553
        if (joystick->nballs > 0) {
slouken@1895
   554
            if (allocate_balldata(joystick) < 0) {
slouken@1895
   555
                joystick->nballs = 0;
slouken@1895
   556
            }
slouken@1895
   557
        }
slouken@1895
   558
    }
slouken@0
   559
}
slouken@0
   560
slouken@892
   561
slouken@0
   562
/* Function to open a joystick for use.
slouken@0
   563
   The joystick to open is specified by the index field of the joystick.
slouken@0
   564
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@0
   565
   It returns 0, or -1 if there is an error.
slouken@0
   566
 */
slouken@1895
   567
int
slouken@6690
   568
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
slouken@0
   569
{
icculus@6734
   570
    SDL_joylist_item *item = JoystickByDevIndex(device_index);
icculus@6734
   571
    char *fname = NULL;
icculus@6734
   572
    int fd = -1;
slouken@892
   573
icculus@6734
   574
    if (item == NULL) {
icculus@7037
   575
        return SDL_SetError("No such device");
icculus@6734
   576
    }
slouken@892
   577
icculus@6734
   578
    fname = item->path;
icculus@6734
   579
    fd = open(fname, O_RDONLY, 0);
slouken@1895
   580
    if (fd < 0) {
icculus@7037
   581
        return SDL_SetError("Unable to open %s", fname);
slouken@1895
   582
    }
icculus@6734
   583
icculus@6751
   584
    joystick->instance_id = item->device_instance;
slouken@1895
   585
    joystick->hwdata = (struct joystick_hwdata *)
slouken@1895
   586
        SDL_malloc(sizeof(*joystick->hwdata));
slouken@1895
   587
    if (joystick->hwdata == NULL) {
icculus@6734
   588
        close(fd);
icculus@7037
   589
        return SDL_OutOfMemory();
slouken@1895
   590
    }
slouken@1895
   591
    SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
icculus@6752
   592
    joystick->hwdata->item = item;
icculus@6734
   593
    joystick->hwdata->guid = item->guid;
slouken@1895
   594
    joystick->hwdata->fd = fd;
icculus@6734
   595
    joystick->hwdata->fname = SDL_strdup(item->path);
icculus@6734
   596
    if (joystick->hwdata->fname == NULL) {
icculus@6734
   597
        SDL_free(joystick->hwdata);
icculus@6734
   598
        joystick->hwdata = NULL;
icculus@6734
   599
        close(fd);
icculus@7037
   600
        return SDL_OutOfMemory();
icculus@6734
   601
    }
icculus@6734
   602
icculus@6734
   603
    SDL_assert(item->hwdata == NULL);
icculus@6734
   604
    item->hwdata = joystick->hwdata;
slouken@0
   605
slouken@1895
   606
    /* Set the joystick to non-blocking read mode */
slouken@1895
   607
    fcntl(fd, F_SETFL, O_NONBLOCK);
slouken@0
   608
slouken@1895
   609
    /* Get the number of buttons and axes on the joystick */
icculus@6729
   610
    ConfigJoystick(joystick, fd);
slouken@554
   611
slouken@7191
   612
    /* mark joystick as fresh and ready */
slouken@6844
   613
    joystick->hwdata->fresh = 1;
slouken@6844
   614
slouken@1895
   615
    return (0);
slouken@0
   616
}
slouken@0
   617
slouken@6707
   618
/* Function to determine is this joystick is attached to the system right now */
slouken@6707
   619
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6707
   620
{
icculus@6752
   621
    return !joystick->closed && (joystick->hwdata->item != NULL);
slouken@6707
   622
}
slouken@6707
   623
slouken@7860
   624
static SDL_INLINE void
slouken@1895
   625
HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value)
slouken@0
   626
{
slouken@1895
   627
    struct hwdata_hat *the_hat;
slouken@1895
   628
    const Uint8 position_map[3][3] = {
slouken@1895
   629
        {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
slouken@1895
   630
        {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
slouken@1895
   631
        {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
slouken@1895
   632
    };
slouken@0
   633
slouken@1895
   634
    the_hat = &stick->hwdata->hats[hat];
slouken@1895
   635
    if (value < 0) {
slouken@1895
   636
        value = 0;
slouken@1895
   637
    } else if (value == 0) {
slouken@1895
   638
        value = 1;
slouken@1895
   639
    } else if (value > 0) {
slouken@1895
   640
        value = 2;
slouken@1895
   641
    }
slouken@1895
   642
    if (value != the_hat->axis[axis]) {
slouken@1895
   643
        the_hat->axis[axis] = value;
slouken@1895
   644
        SDL_PrivateJoystickHat(stick, hat,
slouken@3013
   645
                               position_map[the_hat->
slouken@3013
   646
                                            axis[1]][the_hat->axis[0]]);
slouken@1895
   647
    }
slouken@0
   648
}
slouken@0
   649
slouken@7860
   650
static SDL_INLINE void
slouken@1895
   651
HandleBall(SDL_Joystick * stick, Uint8 ball, int axis, int value)
slouken@0
   652
{
slouken@1895
   653
    stick->hwdata->balls[ball].axis[axis] += value;
slouken@0
   654
}
slouken@0
   655
slouken@0
   656
slouken@7860
   657
static SDL_INLINE int
icculus@6729
   658
AxisCorrect(SDL_Joystick * joystick, int which, int value)
slouken@0
   659
{
slouken@1895
   660
    struct axis_correct *correct;
slouken@0
   661
slouken@1895
   662
    correct = &joystick->hwdata->abs_correct[which];
slouken@1895
   663
    if (correct->used) {
slouken@6845
   664
        value *= 2;
slouken@1895
   665
        if (value > correct->coef[0]) {
slouken@1895
   666
            if (value < correct->coef[1]) {
slouken@1895
   667
                return 0;
slouken@1895
   668
            }
slouken@1895
   669
            value -= correct->coef[1];
slouken@1895
   670
        } else {
slouken@1895
   671
            value -= correct->coef[0];
slouken@1895
   672
        }
slouken@1895
   673
        value *= correct->coef[2];
slouken@6845
   674
        value >>= 13;
slouken@1895
   675
    }
slouken@554
   676
slouken@1895
   677
    /* Clamp and return */
slouken@1895
   678
    if (value < -32768)
slouken@1895
   679
        return -32768;
slouken@1895
   680
    if (value > 32767)
slouken@1895
   681
        return 32767;
slouken@554
   682
slouken@1895
   683
    return value;
slouken@0
   684
}
slouken@0
   685
slouken@7860
   686
static SDL_INLINE void
slouken@6844
   687
PollAllValues(SDL_Joystick * joystick)
slouken@6844
   688
{
slouken@6844
   689
    struct input_absinfo absinfo;
slouken@6844
   690
    int a, b = 0;
slouken@6844
   691
slouken@7191
   692
    /* Poll all axis */
slouken@6844
   693
    for (a = ABS_X; b < ABS_MAX; a++) {
slouken@6844
   694
        switch (a) {
slouken@6844
   695
        case ABS_HAT0X:
slouken@6844
   696
        case ABS_HAT0Y:
slouken@6844
   697
        case ABS_HAT1X:
slouken@6844
   698
        case ABS_HAT1Y:
slouken@6844
   699
        case ABS_HAT2X:
slouken@6844
   700
        case ABS_HAT2Y:
slouken@6844
   701
        case ABS_HAT3X:
slouken@6844
   702
        case ABS_HAT3Y:
slouken@7191
   703
            /* ingore hats */
slouken@6844
   704
            break;
slouken@6844
   705
        default:
slouken@6844
   706
            if (joystick->hwdata->abs_correct[b].used) {
slouken@6844
   707
                if (ioctl(joystick->hwdata->fd, EVIOCGABS(a), &absinfo) >= 0) {
slouken@6844
   708
                    absinfo.value = AxisCorrect(joystick, b, absinfo.value);
slouken@6844
   709
slouken@6844
   710
#ifdef DEBUG_INPUT_EVENTS
slouken@6844
   711
                    printf("Joystick : Re-read Axis %d (%d) val= %d\n",
slouken@6844
   712
                        joystick->hwdata->abs_map[b], a, absinfo.value);
slouken@6844
   713
#endif
slouken@6844
   714
                    SDL_PrivateJoystickAxis(joystick,
slouken@6844
   715
                            joystick->hwdata->abs_map[b],
slouken@6844
   716
                            absinfo.value);
slouken@6844
   717
                }
slouken@6844
   718
            }
slouken@6844
   719
            b++;
slouken@6844
   720
        }
slouken@6844
   721
    }
slouken@6844
   722
}
slouken@6844
   723
slouken@7860
   724
static SDL_INLINE void
icculus@6729
   725
HandleInputEvents(SDL_Joystick * joystick)
slouken@0
   726
{
slouken@1895
   727
    struct input_event events[32];
slouken@1895
   728
    int i, len;
slouken@1895
   729
    int code;
slouken@0
   730
slouken@6844
   731
    if (joystick->hwdata->fresh) {
slouken@6844
   732
        PollAllValues(joystick);
slouken@6844
   733
        joystick->hwdata->fresh = 0;
slouken@6844
   734
    }
slouken@6844
   735
slouken@1895
   736
    while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
slouken@1895
   737
        len /= sizeof(events[0]);
slouken@1895
   738
        for (i = 0; i < len; ++i) {
slouken@1895
   739
            code = events[i].code;
slouken@1895
   740
            switch (events[i].type) {
slouken@1895
   741
            case EV_KEY:
slouken@1895
   742
                if (code >= BTN_MISC) {
slouken@1895
   743
                    code -= BTN_MISC;
icculus@6728
   744
                    SDL_PrivateJoystickButton(joystick,
icculus@6728
   745
                                              joystick->hwdata->key_map[code],
icculus@6728
   746
                                              events[i].value);
slouken@1895
   747
                }
slouken@1895
   748
                break;
slouken@1895
   749
            case EV_ABS:
icculus@6326
   750
                if (code >= ABS_MISC) {
icculus@6326
   751
                    break;
icculus@6326
   752
                }
icculus@6326
   753
slouken@1895
   754
                switch (code) {
slouken@1895
   755
                case ABS_HAT0X:
slouken@1895
   756
                case ABS_HAT0Y:
slouken@1895
   757
                case ABS_HAT1X:
slouken@1895
   758
                case ABS_HAT1Y:
slouken@1895
   759
                case ABS_HAT2X:
slouken@1895
   760
                case ABS_HAT2Y:
slouken@1895
   761
                case ABS_HAT3X:
slouken@1895
   762
                case ABS_HAT3Y:
slouken@1895
   763
                    code -= ABS_HAT0X;
slouken@1895
   764
                    HandleHat(joystick, code / 2, code % 2, events[i].value);
slouken@1895
   765
                    break;
slouken@1895
   766
                default:
slouken@1895
   767
                    events[i].value =
icculus@6729
   768
                        AxisCorrect(joystick, code, events[i].value);
icculus@6728
   769
                    SDL_PrivateJoystickAxis(joystick,
icculus@6728
   770
                                            joystick->hwdata->abs_map[code],
icculus@6728
   771
                                            events[i].value);
slouken@1895
   772
                    break;
slouken@1895
   773
                }
slouken@1895
   774
                break;
slouken@1895
   775
            case EV_REL:
slouken@1895
   776
                switch (code) {
slouken@1895
   777
                case REL_X:
slouken@1895
   778
                case REL_Y:
slouken@1895
   779
                    code -= REL_X;
slouken@1895
   780
                    HandleBall(joystick, code / 2, code % 2, events[i].value);
slouken@1895
   781
                    break;
slouken@1895
   782
                default:
slouken@1895
   783
                    break;
slouken@1895
   784
                }
slouken@1895
   785
                break;
slouken@6844
   786
            case EV_SYN:
slouken@6844
   787
                switch (code) {
slouken@6844
   788
                case SYN_DROPPED :
slouken@6844
   789
#ifdef DEBUG_INPUT_EVENTS
philipp@7133
   790
                    printf("Event SYN_DROPPED detected\n");
slouken@6844
   791
#endif
slouken@6844
   792
                    PollAllValues(joystick);
slouken@6844
   793
                    break;
slouken@6844
   794
                default:
slouken@6844
   795
                    break;
slouken@6844
   796
                }
slouken@1895
   797
            default:
slouken@1895
   798
                break;
slouken@1895
   799
            }
slouken@1895
   800
        }
slouken@1895
   801
    }
slouken@0
   802
}
slouken@0
   803
slouken@1895
   804
void
slouken@1895
   805
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@0
   806
{
slouken@1895
   807
    int i;
slouken@1895
   808
icculus@6729
   809
    HandleInputEvents(joystick);
slouken@0
   810
slouken@1895
   811
    /* Deliver ball motion updates */
slouken@1895
   812
    for (i = 0; i < joystick->nballs; ++i) {
slouken@1895
   813
        int xrel, yrel;
slouken@0
   814
slouken@1895
   815
        xrel = joystick->hwdata->balls[i].axis[0];
slouken@1895
   816
        yrel = joystick->hwdata->balls[i].axis[1];
slouken@1895
   817
        if (xrel || yrel) {
slouken@1895
   818
            joystick->hwdata->balls[i].axis[0] = 0;
slouken@1895
   819
            joystick->hwdata->balls[i].axis[1] = 0;
slouken@1895
   820
            SDL_PrivateJoystickBall(joystick, (Uint8) i, xrel, yrel);
slouken@1895
   821
        }
slouken@1895
   822
    }
slouken@0
   823
}
slouken@0
   824
slouken@0
   825
/* Function to close a joystick after use */
slouken@1895
   826
void
slouken@1895
   827
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@0
   828
{
slouken@1895
   829
    if (joystick->hwdata) {
icculus@6728
   830
        close(joystick->hwdata->fd);
icculus@6752
   831
        if (joystick->hwdata->item) {
icculus@6752
   832
            joystick->hwdata->item->hwdata = NULL;
icculus@6752
   833
        }
icculus@6734
   834
        SDL_free(joystick->hwdata->hats);
icculus@6734
   835
        SDL_free(joystick->hwdata->balls);
icculus@6734
   836
        SDL_free(joystick->hwdata->fname);
slouken@1895
   837
        SDL_free(joystick->hwdata);
slouken@1895
   838
        joystick->hwdata = NULL;
slouken@1895
   839
    }
slouken@6690
   840
    joystick->closed = 1;
slouken@0
   841
}
slouken@0
   842
slouken@0
   843
/* Function to perform any system-specific joystick related cleanup */
slouken@1895
   844
void
slouken@1895
   845
SDL_SYS_JoystickQuit(void)
slouken@0
   846
{
icculus@6734
   847
    SDL_joylist_item *item = NULL;
icculus@6734
   848
    SDL_joylist_item *next = NULL;
icculus@6734
   849
icculus@6734
   850
    for (item = SDL_joylist; item; item = next) {
icculus@6734
   851
        next = item->next;
icculus@6734
   852
        SDL_free(item->path);
icculus@6734
   853
        SDL_free(item->name);
icculus@6734
   854
        SDL_free(item);
icculus@6734
   855
    }
icculus@6734
   856
icculus@6734
   857
    SDL_joylist = SDL_joylist_tail = NULL;
slouken@0
   858
icculus@6734
   859
    numjoysticks = 0;
icculus@6734
   860
    instance_counter = 0;
icculus@6734
   861
icculus@6734
   862
#if SDL_USE_LIBUDEV
gabomdq@7772
   863
    SDL_UDEV_DelCallback(joystick_udev_callback);
gabomdq@7772
   864
    SDL_UDEV_Quit();
icculus@6734
   865
#endif
slouken@0
   866
}
slouken@0
   867
slouken@6738
   868
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
slouken@6690
   869
{
icculus@6734
   870
    return JoystickByDevIndex(device_index)->guid;
slouken@6690
   871
}
slouken@6690
   872
slouken@6738
   873
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6690
   874
{
icculus@6734
   875
    return joystick->hwdata->guid;
slouken@6690
   876
}
slouken@6690
   877
slouken@1635
   878
#endif /* SDL_JOYSTICK_LINUX */
slouken@6693
   879
slouken@1895
   880
/* vi: set ts=4 sw=4 expandtab: */