src/joystick/linux/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 11 Feb 2013 16:51:00 -0800
changeset 6845 e14535915e3e
parent 6844 e09997fb33cc
child 6852 424a6a791947
permissions -rw-r--r--
Patch - Joystick coef[] doesn't support dial with low number of positions. Simon <simon@mungewell.org> I am working on joystick support for the SRW-S1 gaming wheel on Linux, this device has 3 dials with only a few positions each. At present SDL2 only fail to report the highest position value, due to the interger math used for coef[]'s. So with a 4 position switch I have input values (with evtest) -- Event: time 1358967246.173186, type 3 (EV_ABS), code 9 (ABS_GAS), value 2 Event: time 1358967246.173186, -------------- SYN_REPORT ------------ Event: time 1358967246.369150, type 3 (EV_ABS), code 9 (ABS_GAS), value 1 Event: time 1358967246.369150, -------------- SYN_REPORT ------------ Event: time 1358967246.930277, type 3 (EV_ABS), code 9 (ABS_GAS), value 0 Event: time 1358967246.930277, -------------- SYN_REPORT ------------ Event: time 1358967249.369832, type 3 (EV_ABS), code 9 (ABS_GAS), value 1 Event: time 1358967249.369832, -------------- SYN_REPORT ------------ Event: time 1358967249.514382, type 3 (EV_ABS), code 9 (ABS_GAS), value 2 Event: time 1358967249.514382, -------------- SYN_REPORT ------------ Event: time 1358967249.626189, type 3 (EV_ABS), code 9 (ABS_GAS), value 3 Event: time 1358967249.626189, -------------- SYN_REPORT ------------ -- Testjoystick reports -- Joystick has 6 axes, 1 hats, 0 balls, and 17 buttons Joystick 0 axis 5 value: 32767 Joystick 0 axis 5 value: 0 Joystick 0 axis 5 value: -32768 Joystick 0 axis 5 value: 0 Joystick 0 axis 5 value: 32767 Joystick 0 axis 5 value: 0 Joystick 0 axis 5 value: -32768 -- The attached patch 'shifts' the coef[], so that 1/2 values can be computed/seen and allows testjoystick to report correctly. -- Joystick has 6 axes, 1 hats, 0 balls, and 17 buttons Joystick 0 axis 5 value: -10923 Joystick 0 axis 5 value: 10922 Joystick 0 axis 5 value: 32767 Joystick 0 axis 5 value: 10922 Joystick 0 axis 5 value: -10923 Joystick 0 axis 5 value: -32768 Joystick 0 axis 5 value: -10923 Joystick 0 axis 5 value: 10922 Joystick 0 axis 5 value: 32767 -- Cheers, Simon
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
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 5562
diff changeset
     3
  Copyright (C) 1997-2012 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
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1379
diff changeset
    21
#include "SDL_config.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
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
/* This is the system specific header for the SDL joystick API */
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
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    50
/*
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    51
 * !!! FIXME: move all the udev stuff to src/core/linux, so I can reuse it
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    52
 * !!! FIXME:  for audio hardware disconnects.
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    53
 */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    54
#ifdef HAVE_LIBUDEV_H
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    55
#define SDL_USE_LIBUDEV 1
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    56
#include "SDL_loadso.h"
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    57
#include <libudev.h>
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    58
#include <sys/time.h>
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    59
#include <sys/types.h>
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    60
#include <unistd.h>
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    61
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    62
/* we never link directly to libudev. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    63
/* !!! FIXME: can we generalize this? ALSA, etc, do the same things. */
6754
c8cbb53af33e Load the runtime udev library, not the development one.
Sam Lantinga <slouken@libsdl.org>
parents: 6752
diff changeset
    64
static const char *udev_library = "libudev.so.0";
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    65
static void *udev_handle = NULL;
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
/* !!! FIXME: this is kinda ugly. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    68
static SDL_bool
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    69
load_udev_sym(const char *fn, void **addr)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    70
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    71
    *addr = SDL_LoadFunction(udev_handle, fn);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    72
    if (*addr == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    73
        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    74
        return SDL_FALSE;
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
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    77
    return SDL_TRUE;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    78
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    79
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    80
/* libudev entry points... */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    81
static const char *(*UDEV_udev_device_get_action)(struct udev_device *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    82
static const char *(*UDEV_udev_device_get_devnode)(struct udev_device *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    83
static const char *(*UDEV_udev_device_get_property_value)(struct udev_device *, const char *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    84
static struct udev_device *(*UDEV_udev_device_new_from_syspath)(struct udev *, const char *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    85
static void (*UDEV_udev_device_unref)(struct udev_device *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    86
static int (*UDEV_udev_enumerate_add_match_property)(struct udev_enumerate *, const char *, const char *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    87
static int (*UDEV_udev_enumerate_add_match_subsystem)(struct udev_enumerate *, const char *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    88
static struct udev_list_entry *(*UDEV_udev_enumerate_get_list_entry)(struct udev_enumerate *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    89
static struct udev_enumerate *(*UDEV_udev_enumerate_new)(struct udev *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    90
static int (*UDEV_udev_enumerate_scan_devices)(struct udev_enumerate *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    91
static void (*UDEV_udev_enumerate_unref)(struct udev_enumerate *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    92
static const char *(*UDEV_udev_list_entry_get_name)(struct udev_list_entry *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    93
static struct udev_list_entry *(*UDEV_udev_list_entry_get_next)(struct udev_list_entry *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    94
static int (*UDEV_udev_monitor_enable_receiving)(struct udev_monitor *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    95
static int (*UDEV_udev_monitor_filter_add_match_subsystem_devtype)(struct udev_monitor *, const char *, const char *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    96
static int (*UDEV_udev_monitor_get_fd)(struct udev_monitor *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    97
static struct udev_monitor *(*UDEV_udev_monitor_new_from_netlink)(struct udev *, const char *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    98
static struct udev_device *(*UDEV_udev_monitor_receive_device)(struct udev_monitor *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
    99
static void (*UDEV_udev_monitor_unref)(struct udev_monitor *) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   100
static struct udev *(*UDEV_udev_new)(void) = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   101
static void (*UDEV_udev_unref)(struct udev *) = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   102
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   103
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   104
load_udev_syms(void)
892
dc29e5907694 Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 872
diff changeset
   105
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   106
    /* cast funcs to char* first, to please GCC's strict aliasing rules. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   107
    #define SDL_UDEV_SYM(x) \
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   108
        if (!load_udev_sym(#x, (void **) (char *) &UDEV_##x)) return -1
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   109
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   110
    SDL_UDEV_SYM(udev_device_get_action);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   111
    SDL_UDEV_SYM(udev_device_get_devnode);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   112
    SDL_UDEV_SYM(udev_device_get_property_value);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   113
    SDL_UDEV_SYM(udev_device_new_from_syspath);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   114
    SDL_UDEV_SYM(udev_device_unref);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   115
    SDL_UDEV_SYM(udev_enumerate_add_match_property);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   116
    SDL_UDEV_SYM(udev_enumerate_add_match_subsystem);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   117
    SDL_UDEV_SYM(udev_enumerate_get_list_entry);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   118
    SDL_UDEV_SYM(udev_enumerate_new);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   119
    SDL_UDEV_SYM(udev_enumerate_scan_devices);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   120
    SDL_UDEV_SYM(udev_enumerate_unref);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   121
    SDL_UDEV_SYM(udev_list_entry_get_name);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   122
    SDL_UDEV_SYM(udev_list_entry_get_next);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   123
    SDL_UDEV_SYM(udev_monitor_enable_receiving);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   124
    SDL_UDEV_SYM(udev_monitor_filter_add_match_subsystem_devtype);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   125
    SDL_UDEV_SYM(udev_monitor_get_fd);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   126
    SDL_UDEV_SYM(udev_monitor_new_from_netlink);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   127
    SDL_UDEV_SYM(udev_monitor_receive_device);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   128
    SDL_UDEV_SYM(udev_monitor_unref);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   129
    SDL_UDEV_SYM(udev_new);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   130
    SDL_UDEV_SYM(udev_unref);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   131
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   132
    #undef SDL_UDEV_SYM
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   133
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   134
    return 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   135
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   136
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   137
static void
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   138
UnloadUDEVLibrary(void)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   139
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   140
    if (udev_handle != NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   141
        SDL_UnloadObject(udev_handle);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   142
        udev_handle = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   143
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   144
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   145
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   146
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   147
LoadUDEVLibrary(void)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   148
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   149
    int retval = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   150
    if (udev_handle == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   151
        udev_handle = SDL_LoadObject(udev_library);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   152
        if (udev_handle == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   153
            retval = -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   154
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   155
        } else {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   156
            retval = load_udev_syms();
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   157
            if (retval < 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   158
                UnloadUDEVLibrary();
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   159
            }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   160
        }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   161
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   162
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   163
    return retval;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   164
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   165
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   166
static struct udev *udev = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   167
static struct udev_monitor *udev_mon = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   168
#endif
892
dc29e5907694 Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 872
diff changeset
   169
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   170
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   171
/* 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
   172
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
   173
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   174
    int device_instance;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   175
    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
   176
    char *name;   /* "SideWinder 3D Pro" or whatever */
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
    dev_t devnum;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   179
    struct joystick_hwdata *hwdata;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   180
    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
   181
} SDL_joylist_item;
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
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
   184
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
   185
static int numjoysticks = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   186
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
   187
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   188
#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
   189
    (((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
   190
#define NBITS(x) ((((x)-1)/(sizeof(long) * 8))+1)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   191
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
   192
static int
6743
1835c3299b2a Fixed compiling Linux code
Sam Lantinga <slouken@libsdl.org>
parents: 6738
diff changeset
   193
IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *guid)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   194
{
3404
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
   195
    unsigned long evbit[NBITS(EV_MAX)] = { 0 };
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
   196
    unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
   197
    unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   198
    struct input_id inpid;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   199
    Uint16 *guid16 = (Uint16 *) ((char *) &guid->data);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   200
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
   201
    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
   202
        (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
   203
        (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
   204
        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
   205
    }
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
   206
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
   207
    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
   208
          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
   209
        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
   210
    }
6734
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
    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
   213
        return 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   214
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   215
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   216
    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
   217
        return 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   218
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   219
6831
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   220
#ifdef DEBUG_JOYSTICK
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   221
    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
   222
#endif
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   223
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   224
    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
   225
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   226
    /* 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
   227
    /* 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
   228
    *(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
   229
    *(guid16++) = 0;
6831
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   230
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   231
    if (inpid.vendor && inpid.product && inpid.version) {
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   232
        *(guid16++) = SDL_SwapLE16(inpid.vendor);
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   233
        *(guid16++) = 0;
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   234
        *(guid16++) = SDL_SwapLE16(inpid.product);
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   235
        *(guid16++) = 0;
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   236
        *(guid16++) = SDL_SwapLE16(inpid.version);
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   237
        *(guid16++) = 0;
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   238
    } else {
935b871e1ffe Fixed setting the GUID for Bluetooth joysticks
Sam Lantinga <slouken@libsdl.org>
parents: 6754
diff changeset
   239
        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
   240
    }
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   241
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   242
    return 1;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   243
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   244
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   245
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   246
/* !!! 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
   247
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   248
MaybeAddDevice(const char *path)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   249
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   250
    struct stat sb;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   251
    int fd = -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   252
    int isstick = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   253
    char namebuf[128];
6743
1835c3299b2a Fixed compiling Linux code
Sam Lantinga <slouken@libsdl.org>
parents: 6738
diff changeset
   254
    SDL_JoystickGUID guid;
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   255
    SDL_joylist_item *item;
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
    if (path == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   258
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   259
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   260
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   261
    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
   262
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   263
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   264
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   265
    /* 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
   266
    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
   267
        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
   268
            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
   269
        }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   270
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   271
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   272
    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
   273
    if (fd < 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   274
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   275
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   276
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   277
#ifdef DEBUG_INPUT_EVENTS
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   278
    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
   279
#endif
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   280
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   281
    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
   282
    close(fd);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   283
    if (!isstick) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   284
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   285
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   286
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   287
    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
   288
    if (item == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   289
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   290
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   291
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   292
    SDL_zerop(item);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   293
    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
   294
    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
   295
    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
   296
    item->guid = guid;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   297
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   298
    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
   299
         SDL_free(item->path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   300
         SDL_free(item->name);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   301
         SDL_free(item);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   302
         return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   303
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   304
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   305
    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
   306
    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
   307
        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
   308
    } else {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   309
        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
   310
        SDL_joylist_tail = item;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   311
    }
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
    return numjoysticks++;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   314
}
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
#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
   317
/* !!! 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
   318
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   319
MaybeRemoveDevice(const char *path)
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
    SDL_joylist_item *item;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   322
    SDL_joylist_item *prev = NULL;
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
    if (path == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   325
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   326
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   327
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   328
    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
   329
        /* found it, remove it. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   330
        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
   331
            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
   332
            if (item->hwdata) {
6752
8de11011eb1f Corrected Linux joystick things, fixes assertion failure from testjoystick.
Ryan C. Gordon <icculus@icculus.org>
parents: 6751
diff changeset
   333
                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
   334
            }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   335
            if (prev != NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   336
                prev->next = item->next;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   337
                if (item == SDL_joylist_tail) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   338
                    SDL_joylist_tail = prev;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   339
                }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   340
            } else {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   341
                SDL_assert(!SDL_joylist);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   342
                SDL_assert(!SDL_joylist_tail);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   343
                SDL_joylist = SDL_joylist_tail = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   344
            }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   345
            SDL_free(item->path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   346
            SDL_free(item->name);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   347
            SDL_free(item);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   348
            numjoysticks--;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   349
            return retval;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   350
        }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   351
        prev = item;
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
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   354
    return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   355
}
6749
87254cb5734f Fixed compiler warning.
Ryan C. Gordon <icculus@icculus.org>
parents: 6743
diff changeset
   356
#endif
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   357
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   358
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   359
JoystickInitWithoutUdev(void)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   360
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   361
    int i;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   362
    char path[PATH_MAX];
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   363
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   364
    /* !!! 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
   365
    /* !!! 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
   366
    /* !!! 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
   367
    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
   368
        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
   369
        MaybeAddDevice(path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   370
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   371
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   372
    return numjoysticks;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   373
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   374
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   375
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
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   377
static int
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   378
JoystickInitWithUdev(void)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   379
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   380
    struct udev_enumerate *enumerate = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   381
    struct udev_list_entry *devs = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   382
    struct udev_list_entry *item = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   383
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   384
    SDL_assert(udev == NULL);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   385
    udev = UDEV_udev_new();
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   386
    if (udev == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   387
        SDL_SetError("udev_new() failed");
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   388
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   389
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   390
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   391
    udev_mon = UDEV_udev_monitor_new_from_netlink(udev, "udev");
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   392
    if (udev_mon != NULL) {  /* okay if it's NULL, we just lose hotplugging. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   393
        UDEV_udev_monitor_filter_add_match_subsystem_devtype(udev_mon,
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   394
                                                             "input", NULL);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   395
        UDEV_udev_monitor_enable_receiving(udev_mon);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   396
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   397
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   398
    enumerate = UDEV_udev_enumerate_new(udev);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   399
    if (enumerate == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   400
        SDL_SetError("udev_enumerate_new() failed");
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   401
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   402
    }
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
    UDEV_udev_enumerate_add_match_subsystem(enumerate, "input");
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   405
    UDEV_udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1");
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   406
    UDEV_udev_enumerate_scan_devices(enumerate);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   407
    devs = UDEV_udev_enumerate_get_list_entry(enumerate);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   408
    for (item = devs; item; item = UDEV_udev_list_entry_get_next(item)) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   409
        const char *path = UDEV_udev_list_entry_get_name(item);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   410
        struct udev_device *dev = UDEV_udev_device_new_from_syspath(udev, path);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   411
        MaybeAddDevice(UDEV_udev_device_get_devnode(dev));
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   412
        UDEV_udev_device_unref(dev);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   413
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   414
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   415
    UDEV_udev_enumerate_unref(enumerate);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   416
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   417
    return numjoysticks;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   418
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   419
#endif
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   420
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
   421
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
   422
SDL_SYS_JoystickInit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   423
{
5317
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   424
    /* 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
   425
    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
   426
        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
   427
        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
   428
        envpath = envcopy;
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   429
        while (envpath != NULL) {
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   430
            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
   431
            if (delim != NULL) {
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   432
                *delim++ = '\0';
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   433
            }
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   434
            MaybeAddDevice(envpath);
5317
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   435
            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
   436
        }
5317
901a9e9ff0b7 Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   437
        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
   438
    }
554
38b1a98aeb11 Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents: 408
diff changeset
   439
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   440
#if SDL_USE_LIBUDEV
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   441
    if (LoadUDEVLibrary() == 0) {   /* okay if this fails, FOR NOW. */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   442
        return JoystickInitWithUdev();
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   443
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   444
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   445
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   446
    return JoystickInitWithoutUdev();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   447
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   448
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   449
int SDL_SYS_NumJoysticks()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   450
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   451
    return numjoysticks;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   452
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   453
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   454
static SDL_bool
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   455
HotplugUpdateAvailable(void)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   456
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   457
#if SDL_USE_LIBUDEV
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   458
    if (udev_mon != NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   459
        const int fd = UDEV_udev_monitor_get_fd(udev_mon);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   460
        fd_set fds;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   461
        struct timeval tv;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   462
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   463
        FD_ZERO(&fds);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   464
        FD_SET(fd, &fds);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   465
        tv.tv_sec = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   466
        tv.tv_usec = 0;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   467
        if ((select(fd+1, &fds, NULL, NULL, &tv) > 0) && (FD_ISSET(fd, &fds))) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   468
            return SDL_TRUE;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   469
        }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   470
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   471
#endif
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   472
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   473
    return SDL_FALSE;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   474
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   475
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   476
void SDL_SYS_JoystickDetect()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   477
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   478
#if SDL_USE_LIBUDEV
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   479
    struct udev_device *dev = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   480
    const char *devnode = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   481
    const char *action = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   482
    const char *val = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   483
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   484
    while (HotplugUpdateAvailable()) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   485
        dev = UDEV_udev_monitor_receive_device(udev_mon);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   486
        if (dev == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   487
            break;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   488
        }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   489
        val = UDEV_udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   490
        if ((!val) || (SDL_strcmp(val, "1") != 0)) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   491
            continue;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   492
        }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   493
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   494
        action = UDEV_udev_device_get_action(dev);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   495
        devnode = UDEV_udev_device_get_devnode(dev);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   496
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   497
        if (SDL_strcmp(action, "add") == 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   498
            const int device_index = MaybeAddDevice(devnode);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   499
            if (device_index != -1) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   500
                /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceAdded() function? */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   501
                #if !SDL_EVENTS_DISABLED
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   502
                SDL_Event event;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   503
                event.type = SDL_JOYDEVICEADDED;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   504
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   505
                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   506
                    event.jdevice.which = device_index;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   507
                    if ( (SDL_EventOK == NULL) ||
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   508
                         (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   509
                        SDL_PushEvent(&event);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   510
                    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   511
                }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   512
                #endif // !SDL_EVENTS_DISABLED
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   513
            }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   514
        } else if (SDL_strcmp(action, "remove") == 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   515
            const int inst = MaybeRemoveDevice(devnode);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   516
            if (inst != -1) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   517
                /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceRemoved() function? */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   518
                #if !SDL_EVENTS_DISABLED
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   519
                SDL_Event event;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   520
                event.type = SDL_JOYDEVICEREMOVED;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   521
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   522
                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   523
                    event.jdevice.which = inst;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   524
                    if ( (SDL_EventOK == NULL) ||
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   525
                         (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   526
                        SDL_PushEvent(&event);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   527
                    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   528
                }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   529
                #endif // !SDL_EVENTS_DISABLED 
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   530
            }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   531
        }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   532
        UDEV_udev_device_unref(dev);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   533
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   534
#endif
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   535
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   536
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   537
SDL_bool SDL_SYS_JoystickNeedsPolling()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   538
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   539
    /*
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   540
     * This results in a select() call, so technically we're polling to
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   541
     *  decide if we should poll, but I think this function is here because
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   542
     *  Windows has to do an enormous amount of work to detect new sticks,
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   543
     *  whereas libudev just needs to see if there's more data available on
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   544
     *  a socket...so this should be acceptable, I hope.
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   545
     */
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   546
    return HotplugUpdateAvailable();
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   547
}
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   548
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   549
static SDL_joylist_item *
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   550
JoystickByDevIndex(int device_index)
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   551
{
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   552
    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
   553
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   554
    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
   555
        return NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   556
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   557
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   558
    while (device_index > 0) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   559
        SDL_assert(item != NULL);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   560
        device_index--;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   561
        item = item->next;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   562
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   563
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   564
    return item;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   565
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   566
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   567
/* 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
   568
const char *
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   569
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   570
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   571
    return JoystickByDevIndex(device_index)->name;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   572
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   573
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   574
/* 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
   575
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   576
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   577
    return JoystickByDevIndex(device_index)->device_instance;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   578
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   579
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
   580
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
   581
allocate_hatdata(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   582
{
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
   583
    int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   584
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
   585
    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
   586
        (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
   587
                                         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
   588
    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
   589
        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
   590
    }
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
    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
   592
        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
   593
        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
   594
    }
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
   595
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   596
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   597
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
   598
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
   599
allocate_balldata(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   600
{
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
   601
    int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   602
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
   603
    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
   604
        (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
   605
                                          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
   606
    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
   607
        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
   608
    }
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
   609
    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
   610
        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
   611
        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
   612
    }
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
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   614
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   615
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   616
static void
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   617
ConfigJoystick(SDL_Joystick * joystick, int fd)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   618
{
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
   619
    int i, t;
3404
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
   620
    unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
   621
    unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
c9dcc73f6a36 Fixed bug #853
Sam Lantinga <slouken@libsdl.org>
parents: 3326
diff changeset
   622
    unsigned long relbit[NBITS(REL_MAX)] = { 0 };
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   623
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
   624
    /* 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
   625
    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
   626
        (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
   627
        (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   628
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
   629
        /* 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
   630
        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
   631
            if (test_bit(i, keybit)) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   632
#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
   633
                printf("Joystick has button: 0x%x\n", i);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   634
#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
   635
                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
   636
                ++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
   637
            }
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
        }
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
   639
        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
   640
            if (test_bit(i, keybit)) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   641
#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
   642
                printf("Joystick has button: 0x%x\n", i);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   643
#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
   644
                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
   645
                ++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
   646
            }
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
        }
5562
aa0e501baca0 Linux: Search a smaller set of potential joystick axes.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   648
        for (i = 0; i < ABS_MISC; ++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
   649
            /* 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
   650
            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
   651
                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
   652
                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
   653
            }
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
   654
            if (test_bit(i, absbit)) {
5084
ac2c68eb1bb9 Fixed bug #1080
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   655
                struct input_absinfo absinfo;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   656
5085
e7680e2c9f3c Fixed compile error
Sam Lantinga <slouken@libsdl.org>
parents: 5084
diff changeset
   657
                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
   658
                    continue;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   659
#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
   660
                printf("Joystick has absolute axis: %x\n", 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
   661
                printf("Values = { %d, %d, %d, %d, %d }\n",
5084
ac2c68eb1bb9 Fixed bug #1080
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   662
                       absinfo.value, absinfo.minimum, absinfo.maximum,
ac2c68eb1bb9 Fixed bug #1080
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   663
                       absinfo.fuzz, absinfo.flat);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   664
#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
   665
                joystick->hwdata->abs_map[i] = joystick->naxes;
5084
ac2c68eb1bb9 Fixed bug #1080
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   666
                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
   667
                    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
   668
                } 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
   669
                    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
   670
                    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
   671
                        (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
   672
                    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
   673
                        (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
   674
                    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
   675
                    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
   676
                        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
   677
                            (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
   678
                    } 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
   679
                        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
   680
                    }
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
                }
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
   682
                ++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
   683
            }
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
        }
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
        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
   686
            if (test_bit(i, absbit) || test_bit(i + 1, absbit)) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   687
#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
   688
                printf("Joystick has hat %d\n", (i - ABS_HAT0X) / 2);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   689
#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
   690
                ++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
   691
            }
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
   692
        }
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
   693
        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
   694
            ++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
   695
        }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   696
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
   697
        /* 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
   698
        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
   699
            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
   700
                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
   701
            }
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
   702
        }
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
   703
        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
   704
            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
   705
                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
   706
            }
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
   707
        }
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
   708
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   709
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   710
892
dc29e5907694 Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 872
diff changeset
   711
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   712
/* Function to open a joystick for use.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   713
   The joystick to open is specified by the index field of the joystick.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   714
   This should fill the nbuttons and naxes fields of the joystick structure.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   715
   It returns 0, or -1 if there is an error.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   716
 */
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
   717
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
   718
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   719
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   720
    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
   721
    char *fname = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   722
    int fd = -1;
892
dc29e5907694 Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 872
diff changeset
   723
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   724
    if (item == NULL) {
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   725
        SDL_SetError("No such device");
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   726
        return -1;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   727
    }
892
dc29e5907694 Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
Sam Lantinga <slouken@libsdl.org>
parents: 872
diff changeset
   728
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   729
    fname = item->path;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   730
    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
   731
    if (fd < 0) {
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   732
        SDL_SetError("Unable to open %s", fname);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   733
        return -1;
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
   734
    }
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   735
6751
a1f87fd7ed72 Corrected device instance value for Linux joysticks.
Ryan C. Gordon <icculus@icculus.org>
parents: 6749
diff changeset
   736
    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
   737
    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
   738
        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
   739
    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
   740
        close(fd);
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
   741
        SDL_OutOfMemory();
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
        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
   743
    }
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
    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
   745
    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
   746
    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
   747
    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
   748
    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
   749
    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
   750
        SDL_free(joystick->hwdata);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   751
        joystick->hwdata = NULL;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   752
        close(fd);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   753
        SDL_OutOfMemory();
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   754
        return (-1);
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   755
    }
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   756
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   757
    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
   758
    item->hwdata = joystick->hwdata;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   759
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
   760
    /* 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
   761
    fcntl(fd, F_SETFL, O_NONBLOCK);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   762
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
   763
    /* 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
   764
    ConfigJoystick(joystick, fd);
554
38b1a98aeb11 Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents: 408
diff changeset
   765
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   766
    // mark joystick as fresh and ready
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   767
    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
   768
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
   769
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   770
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   771
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   772
/* Function to determine is this joystick is attached to the system right now */
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   773
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   774
{
6752
8de11011eb1f Corrected Linux joystick things, fixes assertion failure from testjoystick.
Ryan C. Gordon <icculus@icculus.org>
parents: 6751
diff changeset
   775
    return !joystick->closed && (joystick->hwdata->item != NULL);
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   776
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   777
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
   778
static __inline__ 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
   779
HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   780
{
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
   781
    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
   782
    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
   783
        {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
   784
        {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
   785
        {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
   786
    };
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   787
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
   788
    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
   789
    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
   790
        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
   791
    } 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
   792
        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
   793
    } 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
   794
        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
   795
    }
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
   796
    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
   797
        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
   798
        SDL_PrivateJoystickHat(stick, hat,
3013
8cc00819c8d6 Reverted Bob's indent checkin
Sam Lantinga <slouken@libsdl.org>
parents: 3011
diff changeset
   799
                               position_map[the_hat->
8cc00819c8d6 Reverted Bob's indent checkin
Sam Lantinga <slouken@libsdl.org>
parents: 3011
diff changeset
   800
                                            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
   801
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   802
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   803
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
   804
static __inline__ 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
   805
HandleBall(SDL_Joystick * stick, Uint8 ball, int axis, int value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   806
{
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
   807
    stick->hwdata->balls[ball].axis[axis] += value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   808
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   809
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   810
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
   811
static __inline__ int
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   812
AxisCorrect(SDL_Joystick * joystick, int which, int value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   813
{
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
   814
    struct axis_correct *correct;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   815
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
   816
    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
   817
    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
   818
        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
   819
        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
   820
            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
   821
                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
   822
            }
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
            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
   824
        } 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
   825
            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
   826
        }
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
   827
        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
   828
        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
   829
    }
554
38b1a98aeb11 Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents: 408
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
    /* 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
   832
    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
   833
        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
   834
    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
   835
        return 32767;
554
38b1a98aeb11 Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents: 408
diff changeset
   836
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
   837
    return value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   838
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   839
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
   840
static __inline__ void
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   841
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
   842
{
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   843
    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
   844
    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
   845
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   846
    // Poll all axis
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   847
    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
   848
        switch (a) {
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   849
        case ABS_HAT0X:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   850
        case ABS_HAT0Y:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   851
        case ABS_HAT1X:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   852
        case ABS_HAT1Y:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   853
        case ABS_HAT2X:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   854
        case ABS_HAT2Y:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   855
        case ABS_HAT3X:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   856
        case ABS_HAT3Y:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   857
            // ingore hats
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   858
            break;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   859
        default:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   860
            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
   861
                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
   862
                    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
   863
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   864
#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
   865
                    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
   866
                        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
   867
#endif
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   868
                    SDL_PrivateJoystickAxis(joystick,
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   869
                            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
   870
                            absinfo.value);
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   871
                }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   872
            }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   873
            b++;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   874
        }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   875
    }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   876
}
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   877
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   878
static __inline__ void
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   879
HandleInputEvents(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   880
{
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
   881
    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
   882
    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
   883
    int code;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   884
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   885
    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
   886
        PollAllValues(joystick);
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   887
        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
   888
    }
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   889
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
   890
    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
   891
        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
   892
        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
   893
            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
   894
            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
   895
            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
   896
                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
   897
                    code -= BTN_MISC;
6728
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   898
                    SDL_PrivateJoystickButton(joystick,
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   899
                                              joystick->hwdata->key_map[code],
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   900
                                              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
   901
                }
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
   902
                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
   903
            case EV_ABS:
6326
57001e845b72 Fix from 1.2 branch: ignore bogus Linux evdev joystick axes.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   904
                if (code >= ABS_MISC) {
57001e845b72 Fix from 1.2 branch: ignore bogus Linux evdev joystick axes.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   905
                    break;
57001e845b72 Fix from 1.2 branch: ignore bogus Linux evdev joystick axes.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   906
                }
57001e845b72 Fix from 1.2 branch: ignore bogus Linux evdev joystick axes.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   907
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
   908
                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
   909
                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
   910
                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
   911
                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
   912
                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
   913
                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
   914
                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
   915
                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
   916
                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
   917
                    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
   918
                    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
   919
                    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
   920
                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
   921
                    events[i].value =
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   922
                        AxisCorrect(joystick, code, events[i].value);
6728
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   923
                    SDL_PrivateJoystickAxis(joystick,
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   924
                                            joystick->hwdata->abs_map[code],
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   925
                                            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
   926
                    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
   927
                }
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
   928
                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
   929
            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
   930
                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
   931
                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
   932
                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
   933
                    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
   934
                    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
   935
                    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
   936
                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
   937
                    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
   938
                }
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
   939
                break;
6844
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   940
            case EV_SYN:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   941
                switch (code) {
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   942
                case SYN_DROPPED :
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   943
#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
   944
                    printf("Event SYN_DROPPED dectected\n");
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   945
#endif
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   946
                    PollAllValues(joystick);
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   947
                    break;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   948
                default:
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   949
                    break;
e09997fb33cc Fix for dropped joystick events contributed by Simon <simon@mungewell.org>
Sam Lantinga <slouken@libsdl.org>
parents: 6831
diff changeset
   950
                }
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
   951
            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
   952
                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
   953
            }
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
   954
        }
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
   955
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   956
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   957
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
   958
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
   959
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   960
{
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
   961
    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
   962
6729
fdaeea9e7567 Removed old Linux joystick API (/dev/js*).
Ryan C. Gordon <icculus@icculus.org>
parents: 6728
diff changeset
   963
    HandleInputEvents(joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   964
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
   965
    /* 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
   966
    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
   967
        int xrel, yrel;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   968
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
   969
        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
   970
        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
   971
        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
   972
            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
   973
            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
   974
            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
   975
        }
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
   976
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   977
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   978
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   979
/* 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
   980
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
   981
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   982
{
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
   983
    if (joystick->hwdata) {
6728
0f8336c1f6d9 Removed the "logical" Linux joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 6707
diff changeset
   984
        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
   985
        if (joystick->hwdata->item) {
8de11011eb1f Corrected Linux joystick things, fixes assertion failure from testjoystick.
Ryan C. Gordon <icculus@icculus.org>
parents: 6751
diff changeset
   986
            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
   987
        }
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
   988
        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
   989
        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
   990
        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
   991
        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
   992
        joystick->hwdata = 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
   993
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6389
diff changeset
   994
    joystick->closed = 1;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   995
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   996
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   997
/* 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
   998
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
   999
SDL_SYS_JoystickQuit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
  1000
{
6734
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
  1001
    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
  1002
    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
  1003
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 6729
diff changeset
  1004
    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
  1005
        next = item->next;
7fdaee2be782 Updated Linux joystick code to support hotplug, GUIDs, etc.
Ryan C. Gordon <icculus@icculus.org>
p