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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 93a374e..23a5a4c 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -138,8 +138,6 @@ IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *gui
#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 @@ void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, cons
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 @@ MaybeAddDevice(const char *path)
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 @@ MaybeAddDevice(const char *path)
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 @@ MaybeRemoveDevice(const char *path)
{
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 @@ MaybeRemoveDevice(const char *path)
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);