Uses SDL_UDEV for Linux joystick hotplugging
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 01 Oct 2013 08:47:06 -0300
changeset 77726b3cc8b7c589
parent 7771 4434498bf4b9
child 7773 4af50952c730
Uses SDL_UDEV for Linux joystick hotplugging
src/joystick/linux/SDL_sysjoystick.c
     1.1 --- a/src/joystick/linux/SDL_sysjoystick.c	Mon Sep 30 22:35:32 2013 -0700
     1.2 +++ b/src/joystick/linux/SDL_sysjoystick.c	Tue Oct 01 08:47:06 2013 -0300
     1.3 @@ -52,124 +52,13 @@
     1.4  #define SYN_DROPPED 3
     1.5  #endif
     1.6  
     1.7 -/*
     1.8 - * !!! FIXME: move all the udev stuff to src/core/linux, so I can reuse it
     1.9 - * !!! FIXME:  for audio hardware disconnects.
    1.10 - */
    1.11 -#ifdef HAVE_LIBUDEV_H
    1.12 -#define SDL_USE_LIBUDEV 1
    1.13 -#include "SDL_loadso.h"
    1.14 -#include <libudev.h>
    1.15 -#include <sys/time.h>
    1.16 -#include <sys/types.h>
    1.17 +#include "../../core/linux/SDL_udev.h"
    1.18  
    1.19 -/* we never link directly to libudev. */
    1.20 -/* !!! FIXME: can we generalize this? ALSA, etc, do the same things. */
    1.21 -static const char *udev_library = "libudev.so.0";
    1.22 -static void *udev_handle = NULL;
    1.23 -
    1.24 -/* !!! FIXME: this is kinda ugly. */
    1.25 -static SDL_bool
    1.26 -load_udev_sym(const char *fn, void **addr)
    1.27 -{
    1.28 -    *addr = SDL_LoadFunction(udev_handle, fn);
    1.29 -    if (*addr == NULL) {
    1.30 -        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
    1.31 -        return SDL_FALSE;
    1.32 -    }
    1.33 -
    1.34 -    return SDL_TRUE;
    1.35 -}
    1.36 -
    1.37 -/* libudev entry points... */
    1.38 -static const char *(*UDEV_udev_device_get_action)(struct udev_device *) = NULL;
    1.39 -static const char *(*UDEV_udev_device_get_devnode)(struct udev_device *) = NULL;
    1.40 -static const char *(*UDEV_udev_device_get_property_value)(struct udev_device *, const char *) = NULL;
    1.41 -static struct udev_device *(*UDEV_udev_device_new_from_syspath)(struct udev *, const char *) = NULL;
    1.42 -static void (*UDEV_udev_device_unref)(struct udev_device *) = NULL;
    1.43 -static int (*UDEV_udev_enumerate_add_match_property)(struct udev_enumerate *, const char *, const char *) = NULL;
    1.44 -static int (*UDEV_udev_enumerate_add_match_subsystem)(struct udev_enumerate *, const char *) = NULL;
    1.45 -static struct udev_list_entry *(*UDEV_udev_enumerate_get_list_entry)(struct udev_enumerate *) = NULL;
    1.46 -static struct udev_enumerate *(*UDEV_udev_enumerate_new)(struct udev *) = NULL;
    1.47 -static int (*UDEV_udev_enumerate_scan_devices)(struct udev_enumerate *) = NULL;
    1.48 -static void (*UDEV_udev_enumerate_unref)(struct udev_enumerate *) = NULL;
    1.49 -static const char *(*UDEV_udev_list_entry_get_name)(struct udev_list_entry *) = NULL;
    1.50 -static struct udev_list_entry *(*UDEV_udev_list_entry_get_next)(struct udev_list_entry *) = NULL;
    1.51 -static int (*UDEV_udev_monitor_enable_receiving)(struct udev_monitor *) = NULL;
    1.52 -static int (*UDEV_udev_monitor_filter_add_match_subsystem_devtype)(struct udev_monitor *, const char *, const char *) = NULL;
    1.53 -static int (*UDEV_udev_monitor_get_fd)(struct udev_monitor *) = NULL;
    1.54 -static struct udev_monitor *(*UDEV_udev_monitor_new_from_netlink)(struct udev *, const char *) = NULL;
    1.55 -static struct udev_device *(*UDEV_udev_monitor_receive_device)(struct udev_monitor *) = NULL;
    1.56 -static void (*UDEV_udev_monitor_unref)(struct udev_monitor *) = NULL;
    1.57 -static struct udev *(*UDEV_udev_new)(void) = NULL;
    1.58 -static void (*UDEV_udev_unref)(struct udev *) = NULL;
    1.59 -
    1.60 -static int
    1.61 -load_udev_syms(void)
    1.62 -{
    1.63 -    /* cast funcs to char* first, to please GCC's strict aliasing rules. */
    1.64 -    #define SDL_UDEV_SYM(x) \
    1.65 -        if (!load_udev_sym(#x, (void **) (char *) &UDEV_##x)) return -1
    1.66 -
    1.67 -    SDL_UDEV_SYM(udev_device_get_action);
    1.68 -    SDL_UDEV_SYM(udev_device_get_devnode);
    1.69 -    SDL_UDEV_SYM(udev_device_get_property_value);
    1.70 -    SDL_UDEV_SYM(udev_device_new_from_syspath);
    1.71 -    SDL_UDEV_SYM(udev_device_unref);
    1.72 -    SDL_UDEV_SYM(udev_enumerate_add_match_property);
    1.73 -    SDL_UDEV_SYM(udev_enumerate_add_match_subsystem);
    1.74 -    SDL_UDEV_SYM(udev_enumerate_get_list_entry);
    1.75 -    SDL_UDEV_SYM(udev_enumerate_new);
    1.76 -    SDL_UDEV_SYM(udev_enumerate_scan_devices);
    1.77 -    SDL_UDEV_SYM(udev_enumerate_unref);
    1.78 -    SDL_UDEV_SYM(udev_list_entry_get_name);
    1.79 -    SDL_UDEV_SYM(udev_list_entry_get_next);
    1.80 -    SDL_UDEV_SYM(udev_monitor_enable_receiving);
    1.81 -    SDL_UDEV_SYM(udev_monitor_filter_add_match_subsystem_devtype);
    1.82 -    SDL_UDEV_SYM(udev_monitor_get_fd);
    1.83 -    SDL_UDEV_SYM(udev_monitor_new_from_netlink);
    1.84 -    SDL_UDEV_SYM(udev_monitor_receive_device);
    1.85 -    SDL_UDEV_SYM(udev_monitor_unref);
    1.86 -    SDL_UDEV_SYM(udev_new);
    1.87 -    SDL_UDEV_SYM(udev_unref);
    1.88 -
    1.89 -    #undef SDL_UDEV_SYM
    1.90 -
    1.91 -    return 0;
    1.92 -}
    1.93 -
    1.94 -static void
    1.95 -UnloadUDEVLibrary(void)
    1.96 -{
    1.97 -    if (udev_handle != NULL) {
    1.98 -        SDL_UnloadObject(udev_handle);
    1.99 -        udev_handle = NULL;
   1.100 -    }
   1.101 -}
   1.102 -
   1.103 -static int
   1.104 -LoadUDEVLibrary(void)
   1.105 -{
   1.106 -    int retval = 0;
   1.107 -    if (udev_handle == NULL) {
   1.108 -        udev_handle = SDL_LoadObject(udev_library);
   1.109 -        if (udev_handle == NULL) {
   1.110 -            retval = -1;
   1.111 -            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
   1.112 -        } else {
   1.113 -            retval = load_udev_syms();
   1.114 -            if (retval < 0) {
   1.115 -                UnloadUDEVLibrary();
   1.116 -            }
   1.117 -        }
   1.118 -    }
   1.119 -
   1.120 -    return retval;
   1.121 -}
   1.122 -
   1.123 -static struct udev *udev = NULL;
   1.124 -static struct udev_monitor *udev_mon = NULL;
   1.125 -#endif
   1.126 +static int MaybeAddDevice(const char *path);
   1.127 +#if SDL_USE_LIBUDEV
   1.128 +static int MaybeRemoveDevice(const char *path);
   1.129 +void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class, const char *devpath);
   1.130 +#endif /* SDL_USE_LIBUDEV */
   1.131  
   1.132  
   1.133  /* A linked list of available joysticks */
   1.134 @@ -246,6 +135,62 @@
   1.135      return 1;
   1.136  }
   1.137  
   1.138 +#if SDL_USE_LIBUDEV
   1.139 +void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class, const char *devpath)
   1.140 +{
   1.141 +    int instance;
   1.142 +    
   1.143 +    if (devpath == NULL || udev_class != SDL_UDEV_DEVICE_JOYSTICK) {
   1.144 +        return;
   1.145 +    }
   1.146 +    
   1.147 +    switch( udev_type )
   1.148 +    {
   1.149 +        case SDL_UDEV_DEVICEADDED:
   1.150 +            instance = MaybeAddDevice(devpath);
   1.151 +            if (instance != -1) {
   1.152 +                /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceAdded() function? */
   1.153 +                #if !SDL_EVENTS_DISABLED
   1.154 +                SDL_Event event;
   1.155 +                event.type = SDL_JOYDEVICEADDED;
   1.156 +
   1.157 +                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   1.158 +                    event.jdevice.which = instance;
   1.159 +                    if ( (SDL_EventOK == NULL) ||
   1.160 +                         (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
   1.161 +                        SDL_PushEvent(&event);
   1.162 +                    }
   1.163 +                }
   1.164 +                #endif /* !SDL_EVENTS_DISABLED */
   1.165 +            }
   1.166 +            break;
   1.167 +            
   1.168 +        case SDL_UDEV_DEVICEREMOVED:
   1.169 +            instance = MaybeRemoveDevice(devpath);
   1.170 +            if (instance != -1) {
   1.171 +                /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceRemoved() function? */
   1.172 +                #if !SDL_EVENTS_DISABLED
   1.173 +                SDL_Event event;
   1.174 +                event.type = SDL_JOYDEVICEREMOVED;
   1.175 +
   1.176 +                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   1.177 +                    event.jdevice.which = instance;
   1.178 +                    if ( (SDL_EventOK == NULL) ||
   1.179 +                         (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
   1.180 +                        SDL_PushEvent(&event);
   1.181 +                    }
   1.182 +                }
   1.183 +                #endif /* !SDL_EVENTS_DISABLED */
   1.184 +            }
   1.185 +            break;
   1.186 +            
   1.187 +        default:
   1.188 +            break;
   1.189 +    }
   1.190 +    
   1.191 +}
   1.192 +#endif /* SDL_USE_LIBUDEV */
   1.193 +
   1.194  
   1.195  /* !!! FIXME: I would love to dump this code and use libudev instead. */
   1.196  static int
   1.197 @@ -380,40 +325,19 @@
   1.198  static int
   1.199  JoystickInitWithUdev(void)
   1.200  {
   1.201 -    struct udev_enumerate *enumerate = NULL;
   1.202 -    struct udev_list_entry *devs = NULL;
   1.203 -    struct udev_list_entry *item = NULL;
   1.204  
   1.205 -    SDL_assert(udev == NULL);
   1.206 -    udev = UDEV_udev_new();
   1.207 -    if (udev == NULL) {
   1.208 -        return SDL_SetError("udev_new() failed");
   1.209 +    if (SDL_UDEV_Init() < 0) {
   1.210 +        return SDL_SetError("Could not initialize UDEV");
   1.211      }
   1.212  
   1.213 -    udev_mon = UDEV_udev_monitor_new_from_netlink(udev, "udev");
   1.214 -    if (udev_mon != NULL) {  /* okay if it's NULL, we just lose hotplugging. */
   1.215 -        UDEV_udev_monitor_filter_add_match_subsystem_devtype(udev_mon,
   1.216 -                                                             "input", NULL);
   1.217 -        UDEV_udev_monitor_enable_receiving(udev_mon);
   1.218 +    /* Set up the udev callback */
   1.219 +    if ( SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
   1.220 +        SDL_UDEV_Quit();
   1.221 +        return SDL_SetError("Could not set up joystick <-> udev callback");
   1.222      }
   1.223 -
   1.224 -    enumerate = UDEV_udev_enumerate_new(udev);
   1.225 -    if (enumerate == NULL) {
   1.226 -        return SDL_SetError("udev_enumerate_new() failed");
   1.227 -    }
   1.228 -
   1.229 -    UDEV_udev_enumerate_add_match_subsystem(enumerate, "input");
   1.230 -    UDEV_udev_enumerate_add_match_property(enumerate, "ID_INPUT_JOYSTICK", "1");
   1.231 -    UDEV_udev_enumerate_scan_devices(enumerate);
   1.232 -    devs = UDEV_udev_enumerate_get_list_entry(enumerate);
   1.233 -    for (item = devs; item; item = UDEV_udev_list_entry_get_next(item)) {
   1.234 -        const char *path = UDEV_udev_list_entry_get_name(item);
   1.235 -        struct udev_device *dev = UDEV_udev_device_new_from_syspath(udev, path);
   1.236 -        MaybeAddDevice(UDEV_udev_device_get_devnode(dev));
   1.237 -        UDEV_udev_device_unref(dev);
   1.238 -    }
   1.239 -
   1.240 -    UDEV_udev_enumerate_unref(enumerate);
   1.241 +    
   1.242 +    /* Force a scan to build the initial device list */
   1.243 +    SDL_UDEV_Scan();
   1.244  
   1.245      return numjoysticks;
   1.246  }
   1.247 @@ -439,9 +363,7 @@
   1.248      }
   1.249  
   1.250  #if SDL_USE_LIBUDEV
   1.251 -    if (LoadUDEVLibrary() == 0) {   /* okay if this fails, FOR NOW. */
   1.252 -        return JoystickInitWithUdev();
   1.253 -    }
   1.254 +    return JoystickInitWithUdev();
   1.255  #endif
   1.256  
   1.257      return JoystickInitWithoutUdev();
   1.258 @@ -452,99 +374,21 @@
   1.259      return numjoysticks;
   1.260  }
   1.261  
   1.262 -static SDL_bool
   1.263 -HotplugUpdateAvailable(void)
   1.264 -{
   1.265 -#if SDL_USE_LIBUDEV
   1.266 -    if (udev_mon != NULL) {
   1.267 -        const int fd = UDEV_udev_monitor_get_fd(udev_mon);
   1.268 -        fd_set fds;
   1.269 -        struct timeval tv;
   1.270 -
   1.271 -        FD_ZERO(&fds);
   1.272 -        FD_SET(fd, &fds);
   1.273 -        tv.tv_sec = 0;
   1.274 -        tv.tv_usec = 0;
   1.275 -        if ((select(fd+1, &fds, NULL, NULL, &tv) > 0) && (FD_ISSET(fd, &fds))) {
   1.276 -            return SDL_TRUE;
   1.277 -        }
   1.278 -    }
   1.279 -#endif
   1.280 -
   1.281 -    return SDL_FALSE;
   1.282 -}
   1.283 -
   1.284  void SDL_SYS_JoystickDetect()
   1.285  {
   1.286  #if SDL_USE_LIBUDEV
   1.287 -    struct udev_device *dev = NULL;
   1.288 -    const char *devnode = NULL;
   1.289 -    const char *action = NULL;
   1.290 -    const char *val = NULL;
   1.291 -
   1.292 -    while (HotplugUpdateAvailable()) {
   1.293 -        dev = UDEV_udev_monitor_receive_device(udev_mon);
   1.294 -        if (dev == NULL) {
   1.295 -            break;
   1.296 -        }
   1.297 -        val = UDEV_udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
   1.298 -        if ((!val) || (SDL_strcmp(val, "1") != 0)) {
   1.299 -            continue;
   1.300 -        }
   1.301 -
   1.302 -        action = UDEV_udev_device_get_action(dev);
   1.303 -        devnode = UDEV_udev_device_get_devnode(dev);
   1.304 -
   1.305 -        if (SDL_strcmp(action, "add") == 0) {
   1.306 -            const int device_index = MaybeAddDevice(devnode);
   1.307 -            if (device_index != -1) {
   1.308 -                /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceAdded() function? */
   1.309 -                #if !SDL_EVENTS_DISABLED
   1.310 -                SDL_Event event;
   1.311 -                event.type = SDL_JOYDEVICEADDED;
   1.312 -
   1.313 -                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   1.314 -                    event.jdevice.which = device_index;
   1.315 -                    if ( (SDL_EventOK == NULL) ||
   1.316 -                         (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
   1.317 -                        SDL_PushEvent(&event);
   1.318 -                    }
   1.319 -                }
   1.320 -                #endif /* !SDL_EVENTS_DISABLED */
   1.321 -            }
   1.322 -        } else if (SDL_strcmp(action, "remove") == 0) {
   1.323 -            const int inst = MaybeRemoveDevice(devnode);
   1.324 -            if (inst != -1) {
   1.325 -                /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceRemoved() function? */
   1.326 -                #if !SDL_EVENTS_DISABLED
   1.327 -                SDL_Event event;
   1.328 -                event.type = SDL_JOYDEVICEREMOVED;
   1.329 -
   1.330 -                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   1.331 -                    event.jdevice.which = inst;
   1.332 -                    if ( (SDL_EventOK == NULL) ||
   1.333 -                         (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
   1.334 -                        SDL_PushEvent(&event);
   1.335 -                    }
   1.336 -                }
   1.337 -                #endif /* !SDL_EVENTS_DISABLED */
   1.338 -            }
   1.339 -        }
   1.340 -        UDEV_udev_device_unref(dev);
   1.341 -    }
   1.342 +    SDL_UDEV_Poll();
   1.343  #endif
   1.344 +    
   1.345  }
   1.346  
   1.347  SDL_bool SDL_SYS_JoystickNeedsPolling()
   1.348  {
   1.349 -    /*
   1.350 -     * This results in a select() call, so technically we're polling to
   1.351 -     *  decide if we should poll, but I think this function is here because
   1.352 -     *  Windows has to do an enormous amount of work to detect new sticks,
   1.353 -     *  whereas libudev just needs to see if there's more data available on
   1.354 -     *  a socket...so this should be acceptable, I hope.
   1.355 -     */
   1.356 -    return HotplugUpdateAvailable();
   1.357 +#if SDL_USE_LIBUDEV
   1.358 +    return SDL_TRUE;
   1.359 +#endif
   1.360 +    
   1.361 +    return SDL_FALSE;
   1.362  }
   1.363  
   1.364  static SDL_joylist_item *
   1.365 @@ -1011,15 +855,8 @@
   1.366      instance_counter = 0;
   1.367  
   1.368  #if SDL_USE_LIBUDEV
   1.369 -    if (udev_mon != NULL) {
   1.370 -        UDEV_udev_monitor_unref(udev_mon);
   1.371 -        udev_mon = NULL;
   1.372 -    }
   1.373 -    if (udev != NULL) {
   1.374 -        UDEV_udev_unref(udev);
   1.375 -        udev = NULL;
   1.376 -    }
   1.377 -    UnloadUDEVLibrary();
   1.378 +    SDL_UDEV_DelCallback(joystick_udev_callback);
   1.379 +    SDL_UDEV_Quit();
   1.380  #endif
   1.381  }
   1.382