src/core/linux/SDL_udev.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 25 May 2015 16:22:09 -0700
changeset 9616 eb2a8d9cfb74
parent 9454 bdc0c1dda0a4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Wait for devices to finish initializing when inserted, before using them. Fixes hotplug issue with XBox 360 game controller.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     1
/*
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     2
  Simple DirectMedia Layer
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 7788
diff changeset
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     4
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     7
  arising from the use of this software.
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     8
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    12
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    16
     appreciated but is not required.
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    18
     misrepresented as being the original software.
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    20
*/
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    21
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    22
/* 
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    23
 * To list the properties of a device, try something like:
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    24
 * udevadm info -a -n snd/hwC0D0 (for a sound card)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    25
 * udevadm info --query=all -n input/event3 (for a keyboard, mouse, etc)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    26
 * udevadm info --query=property -n input/event2
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    27
 */
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    28
#include "SDL_udev.h"
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    29
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    30
#ifdef SDL_USE_LIBUDEV
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    31
9160
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
    32
#include <linux/input.h>
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
    33
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
    34
#include "SDL.h"
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
    35
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    36
static char* SDL_UDEV_LIBS[] = { "libudev.so.1", "libudev.so.0" };
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    37
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    38
#define _THIS SDL_UDEV_PrivateData *_this
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    39
static _THIS = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    40
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    41
static SDL_bool SDL_UDEV_load_sym(const char *fn, void **addr);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    42
static int SDL_UDEV_load_syms(void);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    43
static SDL_bool SDL_UDEV_hotplug_update_available(void);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    44
static void device_event(SDL_UDEV_deviceevent type, struct udev_device *dev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    45
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    46
static SDL_bool
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    47
SDL_UDEV_load_sym(const char *fn, void **addr)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    48
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    49
    *addr = SDL_LoadFunction(_this->udev_handle, fn);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    50
    if (*addr == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    51
        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    52
        return SDL_FALSE;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    53
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    54
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    55
    return SDL_TRUE;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    56
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    57
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    58
static int
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    59
SDL_UDEV_load_syms(void)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    60
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    61
    /* cast funcs to char* first, to please GCC's strict aliasing rules. */
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    62
    #define SDL_UDEV_SYM(x) \
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    63
        if (!SDL_UDEV_load_sym(#x, (void **) (char *) & _this->x)) return -1
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    64
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    65
    SDL_UDEV_SYM(udev_device_get_action);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    66
    SDL_UDEV_SYM(udev_device_get_devnode);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    67
    SDL_UDEV_SYM(udev_device_get_subsystem);
9160
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
    68
    SDL_UDEV_SYM(udev_device_get_parent_with_subsystem_devtype);
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    69
    SDL_UDEV_SYM(udev_device_get_property_value);
9160
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
    70
    SDL_UDEV_SYM(udev_device_get_sysattr_value);
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    71
    SDL_UDEV_SYM(udev_device_new_from_syspath);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    72
    SDL_UDEV_SYM(udev_device_unref);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    73
    SDL_UDEV_SYM(udev_enumerate_add_match_property);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    74
    SDL_UDEV_SYM(udev_enumerate_add_match_subsystem);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    75
    SDL_UDEV_SYM(udev_enumerate_get_list_entry);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    76
    SDL_UDEV_SYM(udev_enumerate_new);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    77
    SDL_UDEV_SYM(udev_enumerate_scan_devices);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    78
    SDL_UDEV_SYM(udev_enumerate_unref);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    79
    SDL_UDEV_SYM(udev_list_entry_get_name);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    80
    SDL_UDEV_SYM(udev_list_entry_get_next);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    81
    SDL_UDEV_SYM(udev_monitor_enable_receiving);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    82
    SDL_UDEV_SYM(udev_monitor_filter_add_match_subsystem_devtype);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    83
    SDL_UDEV_SYM(udev_monitor_get_fd);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    84
    SDL_UDEV_SYM(udev_monitor_new_from_netlink);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    85
    SDL_UDEV_SYM(udev_monitor_receive_device);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    86
    SDL_UDEV_SYM(udev_monitor_unref);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    87
    SDL_UDEV_SYM(udev_new);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    88
    SDL_UDEV_SYM(udev_unref);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    89
    SDL_UDEV_SYM(udev_device_new_from_devnum);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    90
    SDL_UDEV_SYM(udev_device_get_devnum);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    91
    #undef SDL_UDEV_SYM
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    92
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    93
    return 0;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    94
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    95
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    96
static SDL_bool
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    97
SDL_UDEV_hotplug_update_available(void)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    98
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    99
    if (_this->udev_mon != NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   100
        const int fd = _this->udev_monitor_get_fd(_this->udev_mon);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   101
        fd_set fds;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   102
        struct timeval tv;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   103
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   104
        FD_ZERO(&fds);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   105
        FD_SET(fd, &fds);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   106
        tv.tv_sec = 0;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   107
        tv.tv_usec = 0;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   108
        if ((select(fd+1, &fds, NULL, NULL, &tv) > 0) && (FD_ISSET(fd, &fds))) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   109
            return SDL_TRUE;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   110
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   111
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   112
    return SDL_FALSE;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   113
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   114
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   115
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   116
int
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   117
SDL_UDEV_Init(void)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   118
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   119
    int retval = 0;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   120
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   121
    if (_this == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   122
        _this = (SDL_UDEV_PrivateData *) SDL_calloc(1, sizeof(*_this));
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   123
        if(_this == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   124
            return SDL_OutOfMemory();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   125
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   126
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   127
        retval = SDL_UDEV_LoadLibrary();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   128
        if (retval < 0) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   129
            SDL_UDEV_Quit();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   130
            return retval;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   131
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   132
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   133
        /* Set up udev monitoring 
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   134
         * Listen for input devices (mouse, keyboard, joystick, etc) and sound devices
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   135
         */
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   136
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   137
        _this->udev = _this->udev_new();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   138
        if (_this->udev == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   139
            SDL_UDEV_Quit();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   140
            return SDL_SetError("udev_new() failed");
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   141
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   142
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   143
        _this->udev_mon = _this->udev_monitor_new_from_netlink(_this->udev, "udev");
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   144
        if (_this->udev_mon == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   145
            SDL_UDEV_Quit();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   146
            return SDL_SetError("udev_monitor_new_from_netlink() failed");
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   147
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   148
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   149
        _this->udev_monitor_filter_add_match_subsystem_devtype(_this->udev_mon, "input", NULL);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   150
        _this->udev_monitor_filter_add_match_subsystem_devtype(_this->udev_mon, "sound", NULL);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   151
        _this->udev_monitor_enable_receiving(_this->udev_mon);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   152
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   153
        /* Do an initial scan of existing devices */
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   154
        SDL_UDEV_Scan();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   155
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   156
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   157
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   158
    _this->ref_count += 1;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   159
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   160
    return retval;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   161
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   162
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   163
void
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   164
SDL_UDEV_Quit(void)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   165
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   166
    SDL_UDEV_CallbackList *item;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   167
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   168
    if (_this == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   169
        return;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   170
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   171
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   172
    _this->ref_count -= 1;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   173
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   174
    if (_this->ref_count < 1) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   175
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   176
        if (_this->udev_mon != NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   177
            _this->udev_monitor_unref(_this->udev_mon);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   178
            _this->udev_mon = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   179
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   180
        if (_this->udev != NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   181
            _this->udev_unref(_this->udev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   182
            _this->udev = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   183
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   184
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   185
        /* Remove existing devices */
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   186
        while (_this->first != NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   187
            item = _this->first;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   188
            _this->first = _this->first->next;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   189
            SDL_free(item);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   190
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   191
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   192
        SDL_UDEV_UnloadLibrary();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   193
        SDL_free(_this);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   194
        _this = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   195
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   196
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   197
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   198
void
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   199
SDL_UDEV_Scan(void)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   200
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   201
    struct udev_enumerate *enumerate = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   202
    struct udev_list_entry *devs = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   203
    struct udev_list_entry *item = NULL;  
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   204
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   205
    if (_this == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   206
        return;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   207
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   208
   
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   209
    enumerate = _this->udev_enumerate_new(_this->udev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   210
    if (enumerate == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   211
        SDL_UDEV_Quit();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   212
        SDL_SetError("udev_monitor_new_from_netlink() failed");
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   213
        return;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   214
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   215
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   216
    _this->udev_enumerate_add_match_subsystem(enumerate, "input");
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   217
    _this->udev_enumerate_add_match_subsystem(enumerate, "sound");
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   218
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   219
    _this->udev_enumerate_scan_devices(enumerate);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   220
    devs = _this->udev_enumerate_get_list_entry(enumerate);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   221
    for (item = devs; item; item = _this->udev_list_entry_get_next(item)) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   222
        const char *path = _this->udev_list_entry_get_name(item);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   223
        struct udev_device *dev = _this->udev_device_new_from_syspath(_this->udev, path);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   224
        if (dev != NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   225
            device_event(SDL_UDEV_DEVICEADDED, dev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   226
            _this->udev_device_unref(dev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   227
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   228
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   229
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   230
    _this->udev_enumerate_unref(enumerate);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   231
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   232
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   233
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   234
void
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   235
SDL_UDEV_UnloadLibrary(void)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   236
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   237
    if (_this == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   238
        return;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   239
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   240
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   241
    if (_this->udev_handle != NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   242
        SDL_UnloadObject(_this->udev_handle);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   243
        _this->udev_handle = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   244
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   245
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   246
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   247
int
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   248
SDL_UDEV_LoadLibrary(void)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   249
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   250
    int retval = 0, i;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   251
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   252
    if (_this == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   253
        return SDL_SetError("UDEV not initialized");
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   254
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   255
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   256
   
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   257
    if (_this->udev_handle == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   258
        for( i = 0 ; i < SDL_arraysize(SDL_UDEV_LIBS); i++) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   259
            _this->udev_handle = SDL_LoadObject(SDL_UDEV_LIBS[i]);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   260
            if (_this->udev_handle != NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   261
                retval = SDL_UDEV_load_syms();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   262
                if (retval < 0) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   263
                    SDL_UDEV_UnloadLibrary();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   264
                }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   265
                else {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   266
                    break;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   267
                }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   268
            }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   269
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   270
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   271
        if (_this->udev_handle == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   272
            retval = -1;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   273
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   274
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   275
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   276
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   277
    return retval;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   278
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   279
9160
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   280
#define BITS_PER_LONG           (sizeof(unsigned long) * 8)
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   281
#define NBITS(x)                ((((x)-1)/BITS_PER_LONG)+1)
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   282
#define OFF(x)                  ((x)%BITS_PER_LONG)
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   283
#define BIT(x)                  (1UL<<OFF(x))
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   284
#define LONG(x)                 ((x)/BITS_PER_LONG)
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   285
#define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   286
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   287
static void get_caps(struct udev_device *dev, struct udev_device *pdev, const char *attr, unsigned long *bitmask, size_t bitmask_len)
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   288
{
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   289
    const char *value;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   290
    char text[4096];
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   291
    char *word;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   292
    int i;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   293
    unsigned long v;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   294
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   295
    SDL_memset(bitmask, 0, bitmask_len*sizeof(*bitmask));
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   296
    value = _this->udev_device_get_sysattr_value(pdev, attr);
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   297
    if (!value) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   298
        return;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   299
    }
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   300
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   301
    SDL_strlcpy(text, value, sizeof(text));
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   302
    i = 0;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   303
    while ((word = SDL_strrchr(text, ' ')) != NULL) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   304
        v = SDL_strtoul(word+1, NULL, 16);
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   305
        if (i < bitmask_len) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   306
            bitmask[i] = v;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   307
        }
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   308
        ++i;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   309
        *word = '\0';
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   310
    }
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   311
    v = SDL_strtoul(text, NULL, 16);
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   312
    if (i < bitmask_len) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   313
        bitmask[i] = v;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   314
    }
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   315
}
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   316
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   317
static int
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   318
guess_device_class(struct udev_device *dev)
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   319
{
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   320
    int devclass = 0;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   321
    struct udev_device *pdev;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   322
    unsigned long bitmask_ev[NBITS(EV_MAX)];
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   323
    unsigned long bitmask_abs[NBITS(ABS_MAX)];
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   324
    unsigned long bitmask_key[NBITS(KEY_MAX)];
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   325
    unsigned long bitmask_rel[NBITS(REL_MAX)];
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   326
    unsigned long keyboard_mask;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   327
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   328
    /* walk up the parental chain until we find the real input device; the
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   329
     * argument is very likely a subdevice of this, like eventN */
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   330
    pdev = dev;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   331
    while (pdev && !_this->udev_device_get_sysattr_value(pdev, "capabilities/ev")) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   332
        pdev = _this->udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL);
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   333
    }
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   334
    if (!pdev) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   335
        return 0;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   336
    }
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   337
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   338
    get_caps(dev, pdev, "capabilities/ev", bitmask_ev, SDL_arraysize(bitmask_ev));
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   339
    get_caps(dev, pdev, "capabilities/abs", bitmask_abs, SDL_arraysize(bitmask_abs));
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   340
    get_caps(dev, pdev, "capabilities/rel", bitmask_rel, SDL_arraysize(bitmask_rel));
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   341
    get_caps(dev, pdev, "capabilities/key", bitmask_key, SDL_arraysize(bitmask_key));
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   342
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   343
    if (test_bit(EV_ABS, bitmask_ev) &&
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   344
        test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs)) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   345
        if (test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key)) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   346
            ; /* ID_INPUT_TABLET */
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   347
        } else if (test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key)) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   348
            ; /* ID_INPUT_TOUCHPAD */
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   349
        } else if (test_bit(BTN_MOUSE, bitmask_key)) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   350
            devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   351
        } else if (test_bit(BTN_TOUCH, bitmask_key)) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   352
            ; /* ID_INPUT_TOUCHSCREEN */
9454
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   353
        }
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   354
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   355
        if (test_bit(BTN_TRIGGER, bitmask_key) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   356
            test_bit(BTN_A, bitmask_key) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   357
            test_bit(BTN_1, bitmask_key) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   358
            test_bit(ABS_RX, bitmask_abs) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   359
            test_bit(ABS_RY, bitmask_abs) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   360
            test_bit(ABS_RZ, bitmask_abs) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   361
            test_bit(ABS_THROTTLE, bitmask_abs) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   362
            test_bit(ABS_RUDDER, bitmask_abs) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   363
            test_bit(ABS_WHEEL, bitmask_abs) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   364
            test_bit(ABS_GAS, bitmask_abs) ||
bdc0c1dda0a4 Fixed detecting the NVIDIA controller which shows up as both a mouse and a game controller
Sam Lantinga <slouken@libsdl.org>
parents: 9276
diff changeset
   365
            test_bit(ABS_BRAKE, bitmask_abs)) {
9160
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   366
            devclass |= SDL_UDEV_DEVICE_JOYSTICK; /* ID_INPUT_JOYSTICK */
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   367
        }
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   368
    }
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   369
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   370
    if (test_bit(EV_REL, bitmask_ev) &&
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   371
        test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel) &&
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   372
        test_bit(BTN_MOUSE, bitmask_key)) {
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   373
        devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   374
    }
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   375
9276
2b2cfda26085 Treat any key device as a keyboard
Sam Lantinga <slouken@libsdl.org>
parents: 9168
diff changeset
   376
    /* the first 32 bits are ESC, numbers, and Q to D; if we have any of
2b2cfda26085 Treat any key device as a keyboard
Sam Lantinga <slouken@libsdl.org>
parents: 9168
diff changeset
   377
     * those, consider it a keyboard device; do not test KEY_RESERVED, though */
9160
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   378
    keyboard_mask = 0xFFFFFFFE;
9276
2b2cfda26085 Treat any key device as a keyboard
Sam Lantinga <slouken@libsdl.org>
parents: 9168
diff changeset
   379
    if ((bitmask_key[0] & keyboard_mask) != 0)
9160
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   380
        devclass |= SDL_UDEV_DEVICE_KEYBOARD; /* ID_INPUT_KEYBOARD */
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   381
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   382
    return devclass;
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   383
}
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   384
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   385
static void 
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   386
device_event(SDL_UDEV_deviceevent type, struct udev_device *dev) 
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   387
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   388
    const char *subsystem;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   389
    const char *val = NULL;
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   390
    int devclass = 0;
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   391
    const char *path;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   392
    SDL_UDEV_CallbackList *item;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   393
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   394
    path = _this->udev_device_get_devnode(dev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   395
    if (path == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   396
        return;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   397
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   398
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   399
    subsystem = _this->udev_device_get_subsystem(dev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   400
    if (SDL_strcmp(subsystem, "sound") == 0) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   401
        devclass = SDL_UDEV_DEVICE_SOUND;
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   402
    } else if (SDL_strcmp(subsystem, "input") == 0) {
9168
b8e85a5ac99a [udev] Fixes #2654, ID_INPUT_KEY devices are ignored.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9160
diff changeset
   403
        /* udev rules reference: http://cgit.freedesktop.org/systemd/systemd/tree/src/udev/udev-builtin-input_id.c */
b8e85a5ac99a [udev] Fixes #2654, ID_INPUT_KEY devices are ignored.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9160
diff changeset
   404
        
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   405
        val = _this->udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   406
        if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   407
            devclass |= SDL_UDEV_DEVICE_JOYSTICK;
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   408
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   409
        
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   410
        val = _this->udev_device_get_property_value(dev, "ID_INPUT_MOUSE");
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   411
        if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   412
            devclass |= SDL_UDEV_DEVICE_MOUSE;
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   413
        }
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   414
9168
b8e85a5ac99a [udev] Fixes #2654, ID_INPUT_KEY devices are ignored.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9160
diff changeset
   415
        /* The undocumented rule is:
b8e85a5ac99a [udev] Fixes #2654, ID_INPUT_KEY devices are ignored.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9160
diff changeset
   416
           - All devices with keys get ID_INPUT_KEY
b8e85a5ac99a [udev] Fixes #2654, ID_INPUT_KEY devices are ignored.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9160
diff changeset
   417
           - From this subset, if they have ESC, numbers, and Q to D, it also gets ID_INPUT_KEYBOARD
b8e85a5ac99a [udev] Fixes #2654, ID_INPUT_KEY devices are ignored.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9160
diff changeset
   418
           
b8e85a5ac99a [udev] Fixes #2654, ID_INPUT_KEY devices are ignored.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9160
diff changeset
   419
           Ref: http://cgit.freedesktop.org/systemd/systemd/tree/src/udev/udev-builtin-input_id.c#n183
b8e85a5ac99a [udev] Fixes #2654, ID_INPUT_KEY devices are ignored.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9160
diff changeset
   420
        */
b8e85a5ac99a [udev] Fixes #2654, ID_INPUT_KEY devices are ignored.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 9160
diff changeset
   421
        val = _this->udev_device_get_property_value(dev, "ID_INPUT_KEY");
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   422
        if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   423
            devclass |= SDL_UDEV_DEVICE_KEYBOARD;
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   424
        }
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   425
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   426
        if (devclass == 0) {
9160
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   427
            /* Fall back to old style input classes */
8709
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   428
            val = _this->udev_device_get_property_value(dev, "ID_CLASS");
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   429
            if (val != NULL) {
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   430
                if (SDL_strcmp(val, "joystick") == 0) {
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   431
                    devclass = SDL_UDEV_DEVICE_JOYSTICK;
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   432
                } else if (SDL_strcmp(val, "mouse") == 0) {
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   433
                    devclass = SDL_UDEV_DEVICE_MOUSE;
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   434
                } else if (SDL_strcmp(val, "kbd") == 0) {
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   435
                    devclass = SDL_UDEV_DEVICE_KEYBOARD;
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   436
                } else {
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   437
                    return;
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   438
                }
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   439
            } else {
9160
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   440
                /* We could be linked with libudev on a system that doesn't have udev running */
25a97df364fb Fixed input device detection on systems that aren't running the udev daemon
Sam Lantinga <slouken@libsdl.org>
parents: 8709
diff changeset
   441
                devclass = guess_device_class(dev);
8709
a4346af9eec4 Fixed bug 2477 - [PATCH] Joysticks do not work on RHEL6/CentOS6 systems
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   442
            }
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   443
        }
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   444
    } else {
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   445
        return;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   446
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   447
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   448
    /* Process callbacks */
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   449
    for (item = _this->first; item != NULL; item = item->next) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   450
        item->callback(type, devclass, path);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   451
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   452
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   453
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   454
void 
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   455
SDL_UDEV_Poll(void)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   456
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   457
    struct udev_device *dev = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   458
    const char *action = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   459
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   460
    if (_this == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   461
        return;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   462
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   463
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   464
    while (SDL_UDEV_hotplug_update_available()) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   465
        dev = _this->udev_monitor_receive_device(_this->udev_mon);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   466
        if (dev == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   467
            break;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   468
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   469
        action = _this->udev_device_get_action(dev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   470
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   471
        if (SDL_strcmp(action, "add") == 0) {
9616
eb2a8d9cfb74 Wait for devices to finish initializing when inserted, before using them. Fixes hotplug issue with XBox 360 game controller.
Sam Lantinga <slouken@libsdl.org>
parents: 9454
diff changeset
   472
            /* Wait for the device to finish initialization */
eb2a8d9cfb74 Wait for devices to finish initializing when inserted, before using them. Fixes hotplug issue with XBox 360 game controller.
Sam Lantinga <slouken@libsdl.org>
parents: 9454
diff changeset
   473
            SDL_Delay(100);
eb2a8d9cfb74 Wait for devices to finish initializing when inserted, before using them. Fixes hotplug issue with XBox 360 game controller.
Sam Lantinga <slouken@libsdl.org>
parents: 9454
diff changeset
   474
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   475
            device_event(SDL_UDEV_DEVICEADDED, dev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   476
        } else if (SDL_strcmp(action, "remove") == 0) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   477
            device_event(SDL_UDEV_DEVICEREMOVED, dev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   478
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   479
        
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   480
        _this->udev_device_unref(dev);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   481
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   482
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   483
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   484
int 
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   485
SDL_UDEV_AddCallback(SDL_UDEV_Callback cb)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   486
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   487
    SDL_UDEV_CallbackList *item;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   488
    item = (SDL_UDEV_CallbackList *) SDL_calloc(1, sizeof (SDL_UDEV_CallbackList));
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   489
    if (item == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   490
        return SDL_OutOfMemory();
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   491
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   492
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   493
    item->callback = cb;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   494
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   495
    if (_this->last == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   496
        _this->first = _this->last = item;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   497
    } else {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   498
        _this->last->next = item;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   499
        _this->last = item;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   500
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   501
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   502
    return 1;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   503
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   504
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   505
void 
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   506
SDL_UDEV_DelCallback(SDL_UDEV_Callback cb)
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   507
{
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   508
    SDL_UDEV_CallbackList *item;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   509
    SDL_UDEV_CallbackList *prev = NULL;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   510
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   511
    for (item = _this->first; item != NULL; item = item->next) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   512
        /* found it, remove it. */
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   513
        if (item->callback == cb) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   514
            if (prev != NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   515
                prev->next = item->next;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   516
            } else {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   517
                SDL_assert(_this->first == item);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   518
                _this->first = item->next;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   519
            }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   520
            if (item == _this->last) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   521
                _this->last = prev;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   522
            }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   523
            SDL_free(item);
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   524
            return;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   525
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   526
        prev = item;
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   527
    }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   528
    
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   529
}
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   530
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   531
7788
25355ecf10aa Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.
Sam Lantinga <slouken@libsdl.org>
parents: 7753
diff changeset
   532
#endif /* SDL_USE_LIBUDEV */