Report joystick added/removed events even if we don't have udev.
authorSam Lantinga <slouken@libsdl.org>
Thu, 10 Oct 2013 20:58:20 -0700
changeset 7802 d239ba573818
parent 7801 f00cc0a8cd5d
child 7803 97a1127629fb
Report joystick added/removed events even if we don't have udev. T. Joseph Carter As discussed (possibly to death), the Linux joystick driver does not actually report events for added or removed joysticks when you haven't got udev support. We simply cannot know about removed joysticks without udev. But we can (and we should) report adding them. This brings the legacy case in line with pretty much the rest of SDL's joystick drivers.
src/joystick/linux/SDL_sysjoystick.c
--- a/src/joystick/linux/SDL_sysjoystick.c	Mon Oct 07 16:01:40 2013 -0700
+++ b/src/joystick/linux/SDL_sysjoystick.c	Thu Oct 10 20:58:20 2013 -0700
@@ -138,8 +138,6 @@
 #if SDL_USE_LIBUDEV
 void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
 {
-    int instance;
-    
     if (devpath == NULL || !(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
         return;
     }
@@ -147,41 +145,11 @@
     switch( udev_type )
     {
         case SDL_UDEV_DEVICEADDED:
-            instance = MaybeAddDevice(devpath);
-            if (instance != -1) {
-                /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceAdded() function? */
-                #if !SDL_EVENTS_DISABLED
-                SDL_Event event;
-                event.type = SDL_JOYDEVICEADDED;
-
-                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
-                    event.jdevice.which = instance;
-                    if ( (SDL_EventOK == NULL) ||
-                         (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
-                        SDL_PushEvent(&event);
-                    }
-                }
-                #endif /* !SDL_EVENTS_DISABLED */
-            }
+            MaybeAddDevice(devpath);
             break;
             
         case SDL_UDEV_DEVICEREMOVED:
-            instance = MaybeRemoveDevice(devpath);
-            if (instance != -1) {
-                /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceRemoved() function? */
-                #if !SDL_EVENTS_DISABLED
-                SDL_Event event;
-                event.type = SDL_JOYDEVICEREMOVED;
-
-                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
-                    event.jdevice.which = instance;
-                    if ( (SDL_EventOK == NULL) ||
-                         (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
-                        SDL_PushEvent(&event);
-                    }
-                }
-                #endif /* !SDL_EVENTS_DISABLED */
-            }
+            MaybeRemoveDevice(devpath);
             break;
             
         default:
@@ -202,6 +170,9 @@
     char namebuf[128];
     SDL_JoystickGUID guid;
     SDL_joylist_item *item;
+#if !SDL_EVENTS_DISABLED
+    SDL_Event event;
+#endif
 
     if (path == NULL) {
         return -1;
@@ -259,6 +230,19 @@
         SDL_joylist_tail = item;
     }
 
+    /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceAdded() function? */
+#if !SDL_EVENTS_DISABLED
+    event.type = SDL_JOYDEVICEADDED;
+
+    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
+        event.jdevice.which = numjoysticks;
+        if ( (SDL_EventOK == NULL) ||
+             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
+            SDL_PushEvent(&event);
+        }
+    }
+#endif /* !SDL_EVENTS_DISABLED */
+
     return numjoysticks++;
 }
 
@@ -269,6 +253,9 @@
 {
     SDL_joylist_item *item;
     SDL_joylist_item *prev = NULL;
+#if !SDL_EVENTS_DISABLED
+    SDL_Event event;
+#endif
 
     if (path == NULL) {
         return -1;
@@ -290,6 +277,20 @@
             if (item == SDL_joylist_tail) {
                 SDL_joylist_tail = prev;
             }
+
+            /* !!! FIXME: Move this to an SDL_PrivateJoyDeviceRemoved() function? */
+#if !SDL_EVENTS_DISABLED
+            event.type = SDL_JOYDEVICEREMOVED;
+
+            if (SDL_GetEventState(event.type) == SDL_ENABLE) {
+                event.jdevice.which = item->device_instance;
+                if ( (SDL_EventOK == NULL) ||
+                     (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
+                    SDL_PushEvent(&event);
+                }
+            }
+#endif /* !SDL_EVENTS_DISABLED */
+
             SDL_free(item->path);
             SDL_free(item->name);
             SDL_free(item);