src/joystick/linux/SDL_sysjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 26 May 2015 12:03:51 -0400
changeset 9630 6c86d3216f6a
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Linux joystick: Look at entire axis namespace for controls (thanks, "spaz16"!). This apparently has fallout: the PS4 (and maybe PS3?) controllers apparently report some bogus axes, but it won't change the axes we currently expect, and thus the game controller config string is still stable. Fixes Bugzilla #2719.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
     2
  Simple DirectMedia Layer
9619
b94b6d0bff0f Updated the copyright year to 2015
Sam Lantinga <slouken@libsdl.org>
parents: 9561
diff changeset
     3
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5317
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 8053
diff changeset
    21
#include "../../SDL_internal.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1551
diff changeset
    23
#ifdef SDL_JOYSTICK_LINUX
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1551
diff changeset
    24
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
    25
#ifndef SDL_INPUT_LINUXEV
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
    26
#error SDL now requires a Linux 2.4+ kernel with /dev/input/event support.
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
    27
#endif
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
    28
8138
0fbc97d82043 Fixed comments in joystick implementation files claiming to be headers.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8093
diff changeset
    29
/* This is the Linux implementation of the SDL joystick API */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
#include <sys/stat.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    32
#include <unistd.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
#include <fcntl.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
#include <sys/ioctl.h>
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
    35
#include <limits.h>             /* For the definition of PATH_MAX */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
#include <linux/joystick.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    38
#include "SDL_assert.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    39
#include "SDL_joystick.h"
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    40
#include "SDL_endian.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    41
#include "../SDL_sysjoystick.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    42
#include "../SDL_joystick_c.h"
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2285
diff changeset
    43
#include "SDL_sysjoystick_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    44
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    45
/* !!! FIXME: move this somewhere else. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    46
#if !SDL_EVENTS_DISABLED
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    47
#include "../../events/SDL_events_c.h"
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    48
#endif
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    49
6910
a3d4fd1ba315 Fixed compiling on older Linux systems without libudev
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    50
/* This isn't defined in older Linux kernel headers */
a3d4fd1ba315 Fixed compiling on older Linux systems without libudev
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    51
#ifndef SYN_DROPPED
a3d4fd1ba315 Fixed compiling on older Linux systems without libudev
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    52
#define SYN_DROPPED 3
a3d4fd1ba315 Fixed compiling on older Linux systems without libudev
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    53
#endif
a3d4fd1ba315 Fixed compiling on older Linux systems without libudev
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    54
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    55
#include "../../core/linux/SDL_udev.h"
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    56
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    57
static int MaybeAddDevice(const char *path);
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    58
#if SDL_USE_LIBUDEV
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    59
static int MaybeRemoveDevice(const char *path);
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7772
diff changeset
    60
void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath);
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    61
#endif /* SDL_USE_LIBUDEV */
892
dc29e5907694 Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 872
diff changeset
    62
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    63
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    64
/* A linked list of available joysticks */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    65
typedef struct SDL_joylist_item
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    66
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    67
    int device_instance;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    68
    char *path;   /* "/dev/input/event2" or whatever */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    69
    char *name;   /* "SideWinder 3D Pro" or whatever */
6743
1835c3299b2a Fixed compiling Linux code
Sam Lantinga <slouken@libsdl.org>
parents: 6738
diff changeset
    70
    SDL_JoystickGUID guid;
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    71
    dev_t devnum;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    72
    struct joystick_hwdata *hwdata;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    73
    struct SDL_joylist_item *next;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    74
} SDL_joylist_item;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    75
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    76
static SDL_joylist_item *SDL_joylist = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    77
static SDL_joylist_item *SDL_joylist_tail = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    78
static int numjoysticks = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    79
static int instance_counter = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    80
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    81
#define test_bit(nr, addr) \
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    82
    (((1UL << ((nr) % (sizeof(long) * 8))) & ((addr)[(nr) / (sizeof(long) * 8)])) != 0)
3404
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
    83
#define NBITS(x) ((((x)-1)/(sizeof(long) * 8))+1)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    84
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
    85
static int
6743
1835c3299b2a Fixed compiling Linux code
Sam Lantinga <slouken@libsdl.org>
parents: 6738
diff changeset
    86
IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *guid)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    87
{
7909
4434ab25abf3 Fixes Bug 1944 - Linux events, joysticks having only hat are not read
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7860
diff changeset
    88
    struct input_id inpid;
4434ab25abf3 Fixes Bug 1944 - Linux events, joysticks having only hat are not read
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7860
diff changeset
    89
    Uint16 *guid16 = (Uint16 *) ((char *) &guid->data);
4434ab25abf3 Fixes Bug 1944 - Linux events, joysticks having only hat are not read
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7860
diff changeset
    90
4434ab25abf3 Fixes Bug 1944 - Linux events, joysticks having only hat are not read
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7860
diff changeset
    91
#if !SDL_USE_LIBUDEV
4434ab25abf3 Fixes Bug 1944 - Linux events, joysticks having only hat are not read
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7860
diff changeset
    92
    /* When udev is enabled we only get joystick devices here, so there's no need to test them */
3404
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
    93
    unsigned long evbit[NBITS(EV_MAX)] = { 0 };
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
    94
    unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
    95
    unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    96
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
    97
    if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
    98
        (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
    99
        (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   100
        return (0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   101
    }
3079
cfc8b35ad6b1 Fixed haptic subsystem on linux 2.6.28 by lowering the EV_IsJoystick check (seems like some stuff was changed). Shouldn't break anything with earlier versions. Might need to be more robust if false positives show up.
Edgar Simo <bobbens@gmail.com>
parents: 3022
diff changeset
   102
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   103
    if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
3079
cfc8b35ad6b1 Fixed haptic subsystem on linux 2.6.28 by lowering the EV_IsJoystick check (seems like some stuff was changed). Shouldn't break anything with earlier versions. Might need to be more robust if false positives show up.
Edgar Simo <bobbens@gmail.com>
parents: 3022
diff changeset
   104
          test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit))) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   105
        return 0;
3079
cfc8b35ad6b1 Fixed haptic subsystem on linux 2.6.28 by lowering the EV_IsJoystick check (seems like some stuff was changed). Shouldn't break anything with earlier versions. Might need to be more robust if false positives show up.
Edgar Simo <bobbens@gmail.com>
parents: 3022
diff changeset
   106
    }
7909
4434ab25abf3 Fixes Bug 1944 - Linux events, joysticks having only hat are not read
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7860
diff changeset
   107
#endif
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   108
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   109
    if (ioctl(fd, EVIOCGNAME(namebuflen), namebuf) < 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   110
        return 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   111
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   112
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   113
    if (ioctl(fd, EVIOCGID, &inpid) < 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   114
        return 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   115
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   116
6831
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   117
#ifdef DEBUG_JOYSTICK
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   118
    printf("Joystick: %s, bustype = %d, vendor = 0x%x, product = 0x%x, version = %d\n", namebuf, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   119
#endif
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   120
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   121
    SDL_memset(guid->data, 0, sizeof(guid->data));
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   122
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   123
    /* We only need 16 bits for each of these; space them out to fill 128. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   124
    /* Byteswap so devices get same GUID on little/big endian platforms. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   125
    *(guid16++) = SDL_SwapLE16(inpid.bustype);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   126
    *(guid16++) = 0;
6831
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   127
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   128
    if (inpid.vendor && inpid.product && inpid.version) {
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   129
        *(guid16++) = SDL_SwapLE16(inpid.vendor);
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   130
        *(guid16++) = 0;
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   131
        *(guid16++) = SDL_SwapLE16(inpid.product);
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   132
        *(guid16++) = 0;
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   133
        *(guid16++) = SDL_SwapLE16(inpid.version);
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   134
        *(guid16++) = 0;
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   135
    } else {
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   136
        SDL_strlcpy((char*)guid16, namebuf, sizeof(guid->data) - 4);
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   137
    }
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   138
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   139
    return 1;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   140
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   141
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   142
#if SDL_USE_LIBUDEV
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7772
diff changeset
   143
void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   144
{
9321
01966f6feadc Fixed game controller hotplug support for some embedded Linux devices
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   145
    if (devpath == NULL) {
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   146
        return;
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   147
    }
9321
01966f6feadc Fixed game controller hotplug support for some embedded Linux devices
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   148
01966f6feadc Fixed game controller hotplug support for some embedded Linux devices
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   149
    switch (udev_type) {
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   150
        case SDL_UDEV_DEVICEADDED:
9321
01966f6feadc Fixed game controller hotplug support for some embedded Linux devices
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   151
            if (!(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
01966f6feadc Fixed game controller hotplug support for some embedded Linux devices
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   152
                return;
01966f6feadc Fixed game controller hotplug support for some embedded Linux devices
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   153
            }
7802
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   154
            MaybeAddDevice(devpath);
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   155
            break;
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   156
            
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   157
        case SDL_UDEV_DEVICEREMOVED:
7802
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   158
            MaybeRemoveDevice(devpath);
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   159
            break;
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   160
            
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   161
        default:
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   162
            break;
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   163
    }
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   164
    
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   165
}
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   166
#endif /* SDL_USE_LIBUDEV */
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   167
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   168
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   169
/* !!! FIXME: I would love to dump this code and use libudev instead. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   170
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   171
MaybeAddDevice(const char *path)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   172
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   173
    struct stat sb;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   174
    int fd = -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   175
    int isstick = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   176
    char namebuf[128];
6743
1835c3299b2a Fixed compiling Linux code
Sam Lantinga <slouken@libsdl.org>
parents: 6738
diff changeset
   177
    SDL_JoystickGUID guid;
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   178
    SDL_joylist_item *item;
7802
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   179
#if !SDL_EVENTS_DISABLED
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   180
    SDL_Event event;
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   181
#endif
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   182
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   183
    if (path == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   184
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   185
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   186
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   187
    if (stat(path, &sb) == -1) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   188
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   189
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   190
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   191
    /* Check to make sure it's not already in list. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   192
    for (item = SDL_joylist; item != NULL; item = item->next) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   193
        if (sb.st_rdev == item->devnum) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   194
            return -1;  /* already have this one */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   195
        }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   196
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   197
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   198
    fd = open(path, O_RDONLY, 0);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   199
    if (fd < 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   200
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   201
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   202
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   203
#ifdef DEBUG_INPUT_EVENTS
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   204
    printf("Checking %s\n", path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   205
#endif
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   206
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   207
    isstick = IsJoystick(fd, namebuf, sizeof (namebuf), &guid);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   208
    close(fd);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   209
    if (!isstick) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   210
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   211
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   212
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   213
    item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   214
    if (item == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   215
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   216
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   217
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   218
    SDL_zerop(item);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   219
    item->devnum = sb.st_rdev;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   220
    item->path = SDL_strdup(path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   221
    item->name = SDL_strdup(namebuf);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   222
    item->guid = guid;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   223
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   224
    if ( (item->path == NULL) || (item->name == NULL) ) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   225
         SDL_free(item->path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   226
         SDL_free(item->name);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   227
         SDL_free(item);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   228
         return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   229
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   230
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   231
    item->device_instance = instance_counter++;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   232
    if (SDL_joylist_tail == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   233
        SDL_joylist = SDL_joylist_tail = item;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   234
    } else {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   235
        SDL_joylist_tail->next = item;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   236
        SDL_joylist_tail = item;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   237
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   238
7916
8f16728ce87b Make sure the joystick count is correct when the added and removed events are dispatched, in case someone is watching for them with an event filter.
Sam Lantinga <slouken@libsdl.org>
parents: 7909
diff changeset
   239
    /* Need to increment the joystick count before we post the event */
8f16728ce87b Make sure the joystick count is correct when the added and removed events are dispatched, in case someone is watching for them with an event filter.
Sam Lantinga <slouken@libsdl.org>
parents: 7909
diff changeset
   240
    ++numjoysticks;
8f16728ce87b Make sure the joystick count is correct when the added and removed events are dispatched, in case someone is watching for them with an event filter.
Sam Lantinga <slouken@libsdl.org>
parents: 7909
diff changeset
   241
7802
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   242
    /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceAdded() function? */
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   243
#if !SDL_EVENTS_DISABLED
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   244
    event.type = SDL_JOYDEVICEADDED;
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   245
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   246
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
7916
8f16728ce87b Make sure the joystick count is correct when the added and removed events are dispatched, in case someone is watching for them with an event filter.
Sam Lantinga <slouken@libsdl.org>
parents: 7909
diff changeset
   247
        event.jdevice.which = (numjoysticks - 1);
7802
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   248
        if ( (SDL_EventOK == NULL) ||
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   249
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   250
            SDL_PushEvent(&event);
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   251
        }
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   252
    }
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   253
#endif /* !SDL_EVENTS_DISABLED */
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   254
7916
8f16728ce87b Make sure the joystick count is correct when the added and removed events are dispatched, in case someone is watching for them with an event filter.
Sam Lantinga <slouken@libsdl.org>
parents: 7909
diff changeset
   255
    return numjoysticks;
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   256
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   257
6749
87254cb5734f Fixed compiler warning.
Ryan C. Gordon <icculus@icculus.org>
parents: 6743
diff changeset
   258
#if SDL_USE_LIBUDEV
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   259
/* !!! FIXME: I would love to dump this code and use libudev instead. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   260
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   261
MaybeRemoveDevice(const char *path)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   262
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   263
    SDL_joylist_item *item;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   264
    SDL_joylist_item *prev = NULL;
7802
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   265
#if !SDL_EVENTS_DISABLED
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   266
    SDL_Event event;
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   267
#endif
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   268
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   269
    if (path == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   270
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   271
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   272
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   273
    for (item = SDL_joylist; item != NULL; item = item->next) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   274
        /* found it, remove it. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   275
        if (SDL_strcmp(path, item->path) == 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   276
            const int retval = item->device_instance;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   277
            if (item->hwdata) {
6752
8de11011eb1f Corrected Linux joystick things, fixes assertion failure from testjoystick.
Ryan C. Gordon <icculus@icculus.org>
parents: 6751
diff changeset
   278
                item->hwdata->item = NULL;
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   279
            }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   280
            if (prev != NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   281
                prev->next = item->next;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   282
            } else {
6865
670ebd20759d Fix a corruption when you remove first joystick on Linux.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6852
diff changeset
   283
                SDL_assert(SDL_joylist == item);
670ebd20759d Fix a corruption when you remove first joystick on Linux.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6852
diff changeset
   284
                SDL_joylist = item->next;
670ebd20759d Fix a corruption when you remove first joystick on Linux.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6852
diff changeset
   285
            }
670ebd20759d Fix a corruption when you remove first joystick on Linux.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6852
diff changeset
   286
            if (item == SDL_joylist_tail) {
670ebd20759d Fix a corruption when you remove first joystick on Linux.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6852
diff changeset
   287
                SDL_joylist_tail = prev;
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   288
            }
7802
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   289
7916
8f16728ce87b Make sure the joystick count is correct when the added and removed events are dispatched, in case someone is watching for them with an event filter.
Sam Lantinga <slouken@libsdl.org>
parents: 7909
diff changeset
   290
            /* Need to decrement the joystick count before we post the event */
8f16728ce87b Make sure the joystick count is correct when the added and removed events are dispatched, in case someone is watching for them with an event filter.
Sam Lantinga <slouken@libsdl.org>
parents: 7909
diff changeset
   291
            --numjoysticks;
8f16728ce87b Make sure the joystick count is correct when the added and removed events are dispatched, in case someone is watching for them with an event filter.
Sam Lantinga <slouken@libsdl.org>
parents: 7909
diff changeset
   292
7802
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   293
            /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceRemoved() function? */
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   294
#if !SDL_EVENTS_DISABLED
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   295
            event.type = SDL_JOYDEVICEREMOVED;
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   296
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   297
            if (SDL_GetEventState(event.type) == SDL_ENABLE) {
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   298
                event.jdevice.which = item->device_instance;
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   299
                if ( (SDL_EventOK == NULL) ||
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   300
                     (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   301
                    SDL_PushEvent(&event);
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   302
                }
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   303
            }
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   304
#endif /* !SDL_EVENTS_DISABLED */
d239ba573818 Report joystick added/removed events even if we don't have udev.
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
   305
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   306
            SDL_free(item->path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   307
            SDL_free(item->name);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   308
            SDL_free(item);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   309
            return retval;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   310
        }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   311
        prev = item;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   312
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   313
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   314
    return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   315
}
6749
87254cb5734f Fixed compiler warning.
Ryan C. Gordon <icculus@icculus.org>
parents: 6743
diff changeset
   316
#endif
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   317
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   318
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   319
JoystickInitWithoutUdev(void)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   320
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   321
    int i;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   322
    char path[PATH_MAX];
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   323
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   324
    /* !!! FIXME: only finds sticks if they're called /dev/input/event[0..31] */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   325
    /* !!! FIXME:  we could at least readdir() through /dev/input...? */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   326
    /* !!! FIXME:  (or delete this and rely on libudev?) */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   327
    for (i = 0; i < 32; i++) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   328
        SDL_snprintf(path, SDL_arraysize(path), "/dev/input/event%d", i);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   329
        MaybeAddDevice(path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   330
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   331
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   332
    return numjoysticks;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   333
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   334
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   335
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   336
#if SDL_USE_LIBUDEV
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   337
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   338
JoystickInitWithUdev(void)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   339
{
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   340
    if (SDL_UDEV_Init() < 0) {
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   341
        return SDL_SetError("Could not initialize UDEV");
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   342
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   343
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   344
    /* Set up the udev callback */
9321
01966f6feadc Fixed game controller hotplug support for some embedded Linux devices
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   345
    if (SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   346
        SDL_UDEV_Quit();
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   347
        return SDL_SetError("Could not set up joystick <-> udev callback");
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   348
    }
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   349
    
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   350
    /* Force a scan to build the initial device list */
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   351
    SDL_UDEV_Scan();
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   352
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   353
    return numjoysticks;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   354
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   355
#endif
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   356
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   357
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   358
SDL_SYS_JoystickInit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   359
{
5317
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   360
    /* First see if the user specified one or more joysticks to use */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   361
    if (SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL) {
5317
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   362
        char *envcopy, *envpath, *delim;
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   363
        envcopy = SDL_strdup(SDL_getenv("SDL_JOYSTICK_DEVICE"));
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   364
        envpath = envcopy;
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   365
        while (envpath != NULL) {
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   366
            delim = SDL_strchr(envpath, ':');
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   367
            if (delim != NULL) {
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   368
                *delim++ = '\0';
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   369
            }
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   370
            MaybeAddDevice(envpath);
5317
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   371
            envpath = delim;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   372
        }
5317
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   373
        SDL_free(envcopy);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   374
    }
554
38b1a98aeb11 Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents: 408
diff changeset
   375
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   376
#if SDL_USE_LIBUDEV
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   377
    return JoystickInitWithUdev();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   378
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   379
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   380
    return JoystickInitWithoutUdev();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   381
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   382
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   383
int SDL_SYS_NumJoysticks()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   384
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   385
    return numjoysticks;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   386
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   387
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   388
void SDL_SYS_JoystickDetect()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   389
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   390
#if SDL_USE_LIBUDEV
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   391
    SDL_UDEV_Poll();
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   392
#endif
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   393
    
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   394
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   395
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   396
static SDL_joylist_item *
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   397
JoystickByDevIndex(int device_index)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   398
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   399
    SDL_joylist_item *item = SDL_joylist;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   400
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   401
    if ((device_index < 0) || (device_index >= numjoysticks)) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   402
        return NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   403
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   404
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   405
    while (device_index > 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   406
        SDL_assert(item != NULL);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   407
        device_index--;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   408
        item = item->next;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   409
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   410
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   411
    return item;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   412
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   413
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   414
/* Function to get the device-dependent name of a joystick */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   415
const char *
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   416
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   417
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   418
    return JoystickByDevIndex(device_index)->name;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   419
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   420
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   421
/* Function to perform the mapping from device index to the instance id for this index */
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   422
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   423
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   424
    return JoystickByDevIndex(device_index)->device_instance;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   425
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   426
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   427
static int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   428
allocate_hatdata(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   429
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   430
    int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   431
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   432
    joystick->hwdata->hats =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   433
        (struct hwdata_hat *) SDL_malloc(joystick->nhats *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   434
                                         sizeof(struct hwdata_hat));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   435
    if (joystick->hwdata->hats == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   436
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   437
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   438
    for (i = 0; i < joystick->nhats; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   439
        joystick->hwdata->hats[i].axis[0] = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   440
        joystick->hwdata->hats[i].axis[1] = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   441
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   442
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   443
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   444
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   445
static int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   446
allocate_balldata(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   447
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   448
    int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   449
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   450
    joystick->hwdata->balls =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   451
        (struct hwdata_ball *) SDL_malloc(joystick->nballs *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   452
                                          sizeof(struct hwdata_ball));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   453
    if (joystick->hwdata->balls == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   454
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   455
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   456
    for (i = 0; i < joystick->nballs; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   457
        joystick->hwdata->balls[i].axis[0] = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   458
        joystick->hwdata->balls[i].axis[1] = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   459
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   460
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   461
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   462
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   463
static void
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   464
ConfigJoystick(SDL_Joystick * joystick, int fd)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   465
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   466
    int i, t;
3404
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
   467
    unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
   468
    unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
   469
    unsigned long relbit[NBITS(REL_MAX)] = { 0 };
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   470
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   471
    /* See if this device uses the new unified event API */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   472
    if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   473
        (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   474
        (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   475
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   476
        /* Get the number of buttons, axes, and other thingamajigs */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   477
        for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   478
            if (test_bit(i, keybit)) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   479
#ifdef DEBUG_INPUT_EVENTS
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   480
                printf("Joystick has button: 0x%x\n", i);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   481
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   482
                joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   483
                ++joystick->nbuttons;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   484
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   485
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   486
        for (i = BTN_MISC; i < BTN_JOYSTICK; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   487
            if (test_bit(i, keybit)) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   488
#ifdef DEBUG_INPUT_EVENTS
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   489
                printf("Joystick has button: 0x%x\n", i);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   490
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   491
                joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   492
                ++joystick->nbuttons;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   493
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   494
        }
9630
6c86d3216f6a Linux joystick: Look at entire axis namespace for controls (thanks, "spaz16"!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
   495
        for (i = 0; i < ABS_MAX; ++i) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   496
            /* Skip hats */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   497
            if (i == ABS_HAT0X) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   498
                i = ABS_HAT3Y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   499
                continue;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   500
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   501
            if (test_bit(i, absbit)) {
5084
ac2c68eb1bb9 Fixed bug #1080
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   502
                struct input_absinfo absinfo;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   503
8053
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   504
                if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   505
                    continue;
8053
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   506
                }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   507
#ifdef DEBUG_INPUT_EVENTS
8053
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   508
                printf("Joystick has absolute axis: 0x%.2x\n", i);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   509
                printf("Values = { %d, %d, %d, %d, %d }\n",
5084
ac2c68eb1bb9 Fixed bug #1080
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   510
                       absinfo.value, absinfo.minimum, absinfo.maximum,
ac2c68eb1bb9 Fixed bug #1080
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   511
                       absinfo.fuzz, absinfo.flat);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   512
#endif /* DEBUG_INPUT_EVENTS */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   513
                joystick->hwdata->abs_map[i] = joystick->naxes;
5084
ac2c68eb1bb9 Fixed bug #1080
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   514
                if (absinfo.minimum == absinfo.maximum) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   515
                    joystick->hwdata->abs_correct[i].used = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   516
                } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   517
                    joystick->hwdata->abs_correct[i].used = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   518
                    joystick->hwdata->abs_correct[i].coef[0] =
6845
e14535915e3e Patch - Joystick coef[] doesn't support dial with low number of positions.
Sam Lantinga <slouken@libsdl.org>
parents: 6844
diff changeset
   519
                        (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   520
                    joystick->hwdata->abs_correct[i].coef[1] =
6845
e14535915e3e Patch - Joystick coef[] doesn't support dial with low number of positions.
Sam Lantinga <slouken@libsdl.org>
parents: 6844
diff changeset
   521
                        (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
e14535915e3e Patch - Joystick coef[] doesn't support dial with low number of positions.
Sam Lantinga <slouken@libsdl.org>
parents: 6844
diff changeset
   522
                    t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   523
                    if (t != 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   524
                        joystick->hwdata->abs_correct[i].coef[2] =
6845
e14535915e3e Patch - Joystick coef[] doesn't support dial with low number of positions.
Sam Lantinga <slouken@libsdl.org>
parents: 6844
diff changeset
   525
                            (1 << 28) / t;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   526
                    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   527
                        joystick->hwdata->abs_correct[i].coef[2] = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   528
                    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   529
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   530
                ++joystick->naxes;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   531
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   532
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   533
        for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   534
            if (test_bit(i, absbit) || test_bit(i + 1, absbit)) {
8053
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   535
                struct input_absinfo absinfo;
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   536
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   537
                if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   538
                    continue;
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   539
                }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   540
#ifdef DEBUG_INPUT_EVENTS
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   541
                printf("Joystick has hat %d\n", (i - ABS_HAT0X) / 2);
8053
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   542
                printf("Values = { %d, %d, %d, %d, %d }\n",
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   543
                       absinfo.value, absinfo.minimum, absinfo.maximum,
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   544
                       absinfo.fuzz, absinfo.flat);
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7916
diff changeset
   545
#endif /* DEBUG_INPUT_EVENTS */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   546
                ++joystick->nhats;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   547
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   548
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   549
        if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   550
            ++joystick->nballs;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   551
        }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   552
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   553
        /* Allocate data to keep track of these thingamajigs */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   554
        if (joystick->nhats > 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   555
            if (allocate_hatdata(joystick) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   556
                joystick->nhats = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   557
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   558
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   559
        if (joystick->nballs > 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   560
            if (allocate_balldata(joystick) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   561
                joystick->nballs = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   562
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   563
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   564
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   565
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   566
892
dc29e5907694 Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 872
diff changeset
   567
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   568
/* Function to open a joystick for use.
9380
07b7c1005a23 Updated internal documentation comments.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9321
diff changeset
   569
   The joystick to open is specified by the device index.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   570
   This should fill the nbuttons and naxes fields of the joystick structure.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   571
   It returns 0, or -1 if there is an error.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   572
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   573
int
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   574
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   575
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   576
    SDL_joylist_item *item = JoystickByDevIndex(device_index);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   577
    char *fname = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   578
    int fd = -1;
892
dc29e5907694 Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 872
diff changeset
   579
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   580
    if (item == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6911
diff changeset
   581
        return SDL_SetError("No such device");
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   582
    }
892
dc29e5907694 Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 872
diff changeset
   583
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   584
    fname = item->path;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   585
    fd = open(fname, O_RDONLY, 0);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   586
    if (fd < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6911
diff changeset
   587
        return SDL_SetError("Unable to open %s", fname);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   588
    }
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   589
6751
a1f87fd7ed72 Corrected device instance value for Linux joysticks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6749
diff changeset
   590
    joystick->instance_id = item->device_instance;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   591
    joystick->hwdata = (struct joystick_hwdata *)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   592
        SDL_malloc(sizeof(*joystick->hwdata));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   593
    if (joystick->hwdata == NULL) {
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   594
        close(fd);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6911
diff changeset
   595
        return SDL_OutOfMemory();
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   596
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   597
    SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
6752
8de11011eb1f Corrected Linux joystick things, fixes assertion failure from testjoystick.
Ryan C. Gordon <icculus@icculus.org>
parents: 6751
diff changeset
   598
    joystick->hwdata->item = item;
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   599
    joystick->hwdata->guid = item->guid;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   600
    joystick->hwdata->fd = fd;
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   601
    joystick->hwdata->fname = SDL_strdup(item->path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   602
    if (joystick->hwdata->fname == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   603
        SDL_free(joystick->hwdata);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   604
        joystick->hwdata = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   605
        close(fd);
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6911
diff changeset
   606
        return SDL_OutOfMemory();
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   607
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   608
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   609
    SDL_assert(item->hwdata == NULL);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   610
    item->hwdata = joystick->hwdata;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   611
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   612
    /* Set the joystick to non-blocking read mode */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   613
    fcntl(fd, F_SETFL, O_NONBLOCK);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   614
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   615
    /* Get the number of buttons and axes on the joystick */
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   616
    ConfigJoystick(joystick, fd);
554
38b1a98aeb11 Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents: 408
diff changeset
   617
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   618
    /* mark joystick as fresh and ready */
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   619
    joystick->hwdata->fresh = 1;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   620
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   621
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   622
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   623
9561
d8ad01792399 Fixed typo in internal joystick documentation comments.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9433
diff changeset
   624
/* Function to determine if this joystick is attached to the system right now */
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   625
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   626
{
9433
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 9380
diff changeset
   627
    return joystick->hwdata->item != NULL;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   628
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   629
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7802
diff changeset
   630
static SDL_INLINE void
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   631
HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   632
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   633
    struct hwdata_hat *the_hat;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   634
    const Uint8 position_map[3][3] = {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   635
        {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   636
        {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   637
        {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   638
    };
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   639
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   640
    the_hat = &stick->hwdata->hats[hat];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   641
    if (value < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   642
        value = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   643
    } else if (value == 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   644
        value = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   645
    } else if (value > 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   646
        value = 2;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   647
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   648
    if (value != the_hat->axis[axis]) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   649
        the_hat->axis[axis] = value;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   650
        SDL_PrivateJoystickHat(stick, hat,
3013
8cc00819c8d6 Reverted Bob's indent checkin
Sam Lantinga <slouken@libsdl.org>
parents: 3011
diff changeset
   651
                               position_map[the_hat->
8cc00819c8d6 Reverted Bob's indent checkin
Sam Lantinga <slouken@libsdl.org>
parents: 3011
diff changeset
   652
                                            axis[1]][the_hat->axis[0]]);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   653
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   654
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   655
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7802
diff changeset
   656
static SDL_INLINE void
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   657
HandleBall(SDL_Joystick * stick, Uint8 ball, int axis, int value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   658
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   659
    stick->hwdata->balls[ball].axis[axis] += value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   660
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   661
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   662
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7802
diff changeset
   663
static SDL_INLINE int
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   664
AxisCorrect(SDL_Joystick * joystick, int which, int value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   665
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   666
    struct axis_correct *correct;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   667
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   668
    correct = &joystick->hwdata->abs_correct[which];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   669
    if (correct->used) {
6845
e14535915e3e Patch - Joystick coef[] doesn't support dial with low number of positions.
Sam Lantinga <slouken@libsdl.org>
parents: 6844
diff changeset
   670
        value *= 2;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   671
        if (value > correct->coef[0]) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   672
            if (value < correct->coef[1]) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   673
                return 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   674
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   675
            value -= correct->coef[1];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   676
        } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   677
            value -= correct->coef[0];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   678
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   679
        value *= correct->coef[2];
6845
e14535915e3e Patch - Joystick coef[] doesn't support dial with low number of positions.
Sam Lantinga <slouken@libsdl.org>
parents: 6844
diff changeset
   680
        value >>= 13;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   681
    }
554
38b1a98aeb11 Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents: 408
diff changeset
   682
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   683
    /* Clamp and return */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   684
    if (value < -32768)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   685
        return -32768;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   686
    if (value > 32767)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   687
        return 32767;
554
38b1a98aeb11 Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents: 408
diff changeset
   688
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   689
    return value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   690
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   691
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7802
diff changeset
   692
static SDL_INLINE void
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   693
PollAllValues(SDL_Joystick * joystick)
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   694
{
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   695
    struct input_absinfo absinfo;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   696
    int a, b = 0;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   697
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   698
    /* Poll all axis */
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   699
    for (a = ABS_X; b < ABS_MAX; a++) {
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   700
        switch (a) {
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   701
        case ABS_HAT0X:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   702
        case ABS_HAT0Y:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   703
        case ABS_HAT1X:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   704
        case ABS_HAT1Y:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   705
        case ABS_HAT2X:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   706
        case ABS_HAT2Y:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   707
        case ABS_HAT3X:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   708
        case ABS_HAT3Y:
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   709
            /* ingore hats */
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   710
            break;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   711
        default:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   712
            if (joystick->hwdata->abs_correct[b].used) {
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   713
                if (ioctl(joystick->hwdata->fd, EVIOCGABS(a), &absinfo) >= 0) {
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   714
                    absinfo.value = AxisCorrect(joystick, b, absinfo.value);
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   715
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   716
#ifdef DEBUG_INPUT_EVENTS
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   717
                    printf("Joystick : Re-read Axis %d (%d) val= %d\n",
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   718
                        joystick->hwdata->abs_map[b], a, absinfo.value);
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   719
#endif
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   720
                    SDL_PrivateJoystickAxis(joystick,
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   721
                            joystick->hwdata->abs_map[b],
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   722
                            absinfo.value);
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   723
                }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   724
            }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   725
            b++;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   726
        }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   727
    }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   728
}
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   729
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7802
diff changeset
   730
static SDL_INLINE void
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   731
HandleInputEvents(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   732
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   733
    struct input_event events[32];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   734
    int i, len;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   735
    int code;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   736
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   737
    if (joystick->hwdata->fresh) {
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   738
        PollAllValues(joystick);
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   739
        joystick->hwdata->fresh = 0;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   740
    }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   741
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   742
    while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   743
        len /= sizeof(events[0]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   744
        for (i = 0; i < len; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   745
            code = events[i].code;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   746
            switch (events[i].type) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   747
            case EV_KEY:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   748
                if (code >= BTN_MISC) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   749
                    code -= BTN_MISC;
6728
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   750
                    SDL_PrivateJoystickButton(joystick,
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   751
                                              joystick->hwdata->key_map[code],
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   752
                                              events[i].value);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   753
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   754
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   755
            case EV_ABS:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   756
                switch (code) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   757
                case ABS_HAT0X:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   758
                case ABS_HAT0Y:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   759
                case ABS_HAT1X:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   760
                case ABS_HAT1Y:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   761
                case ABS_HAT2X:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   762
                case ABS_HAT2Y:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   763
                case ABS_HAT3X:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   764
                case ABS_HAT3Y:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   765
                    code -= ABS_HAT0X;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   766
                    HandleHat(joystick, code / 2, code % 2, events[i].value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   767
                    break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   768
                default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   769
                    events[i].value =
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   770
                        AxisCorrect(joystick, code, events[i].value);
6728
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   771
                    SDL_PrivateJoystickAxis(joystick,
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   772
                                            joystick->hwdata->abs_map[code],
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   773
                                            events[i].value);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   774
                    break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   775
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   776
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   777
            case EV_REL:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   778
                switch (code) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   779
                case REL_X:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   780
                case REL_Y:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   781
                    code -= REL_X;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   782
                    HandleBall(joystick, code / 2, code % 2, events[i].value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   783
                    break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   784
                default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   785
                    break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   786
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   787
                break;
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   788
            case EV_SYN:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   789
                switch (code) {
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   790
                case SYN_DROPPED :
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   791
#ifdef DEBUG_INPUT_EVENTS
7133
87d4cb9bb66c Corrected spelling in C source files.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7037
diff changeset
   792
                    printf("Event SYN_DROPPED detected\n");
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   793
#endif
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   794
                    PollAllValues(joystick);
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   795
                    break;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   796
                default:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   797
                    break;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   798
                }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   799
            default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   800
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   801
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   802
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   803
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   804
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   805
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   806
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   807
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   808
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   809
    int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   810
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   811
    HandleInputEvents(joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   812
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   813
    /* Deliver ball motion updates */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   814
    for (i = 0; i < joystick->nballs; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   815
        int xrel, yrel;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   816
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   817
        xrel = joystick->hwdata->balls[i].axis[0];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   818
        yrel = joystick->hwdata->balls[i].axis[1];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   819
        if (xrel || yrel) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   820
            joystick->hwdata->balls[i].axis[0] = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   821
            joystick->hwdata->balls[i].axis[1] = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   822
            SDL_PrivateJoystickBall(joystick, (Uint8) i, xrel, yrel);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   823
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   824
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   825
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   826
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   827
/* Function to close a joystick after use */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   828
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   829
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   830
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   831
    if (joystick->hwdata) {
6728
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   832
        close(joystick->hwdata->fd);
6752
8de11011eb1f Corrected Linux joystick things, fixes assertion failure from testjoystick.
Ryan C. Gordon <icculus@icculus.org>
parents: 6751
diff changeset
   833
        if (joystick->hwdata->item) {
8de11011eb1f Corrected Linux joystick things, fixes assertion failure from testjoystick.
Ryan C. Gordon <icculus@icculus.org>
parents: 6751
diff changeset
   834
            joystick->hwdata->item->hwdata = NULL;
8de11011eb1f Corrected Linux joystick things, fixes assertion failure from testjoystick.
Ryan C. Gordon <icculus@icculus.org>
parents: 6751
diff changeset
   835
        }
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   836
        SDL_free(joystick->hwdata->hats);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   837
        SDL_free(joystick->hwdata->balls);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   838
        SDL_free(joystick->hwdata->fname);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   839
        SDL_free(joystick->hwdata);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   840
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   841
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   842
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   843
/* Function to perform any system-specific joystick related cleanup */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   844
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   845
SDL_SYS_JoystickQuit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   846
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   847
    SDL_joylist_item *item = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   848
    SDL_joylist_item *next = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   849
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   850
    for (item = SDL_joylist; item; item = next) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   851
        next = item->next;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   852
        SDL_free(item->path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   853
        SDL_free(item->name);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   854
        SDL_free(item);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   855
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   856
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   857
    SDL_joylist = SDL_joylist_tail = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   858
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   859
    numjoysticks = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   860
    instance_counter = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   861
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   862
#if SDL_USE_LIBUDEV
7772
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   863
    SDL_UDEV_DelCallback(joystick_udev_callback);
6b3cc8b7c589 Uses SDL_UDEV for Linux joystick hotplugging
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   864
    SDL_UDEV_Quit();
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   865
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   866
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   867
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6734
diff changeset
   868
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   869
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   870
    return JoystickByDevIndex(device_index)->guid;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   871
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   872
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6734
diff changeset
   873
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   874
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   875
    return joystick->hwdata->guid;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   876
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   877
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1551
diff changeset
   878
#endif /* SDL_JOYSTICK_LINUX */
6693
3c98e4ec2180 Fixed iOS joystick support for new API
Sam Lantinga <slouken@libsdl.org>
parents: 6691
diff changeset
   879
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1821
diff changeset
   880
/* vi: set ts=4 sw=4 expandtab: */