Fixed bug where a Logitech wireless keyboard with built-in mouse touchpad didn't get recongized as both devices.

This commit is contained in:
Sam Lantinga 2013-10-05 21:15:55 -07:00
parent 529664278f
commit 90a219a375
5 changed files with 134 additions and 188 deletions

View File

@ -279,7 +279,7 @@ device_event(SDL_UDEV_deviceevent type, struct udev_device *dev)
{ {
const char *subsystem; const char *subsystem;
const char *val = NULL; const char *val = NULL;
SDL_UDEV_deviceclass devclass = 0; int devclass = 0;
const char *path; const char *path;
SDL_UDEV_CallbackList *item; SDL_UDEV_CallbackList *item;
@ -291,32 +291,26 @@ device_event(SDL_UDEV_deviceevent type, struct udev_device *dev)
subsystem = _this->udev_device_get_subsystem(dev); subsystem = _this->udev_device_get_subsystem(dev);
if (SDL_strcmp(subsystem, "sound") == 0) { if (SDL_strcmp(subsystem, "sound") == 0) {
devclass = SDL_UDEV_DEVICE_SOUND; devclass = SDL_UDEV_DEVICE_SOUND;
} } else if (SDL_strcmp(subsystem, "input") == 0) {
else if (SDL_strcmp(subsystem, "input") == 0) {
val = _this->udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"); val = _this->udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK");
if (val != NULL && SDL_strcmp(val, "1") == 0 ) { if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
devclass = SDL_UDEV_DEVICE_JOYSTICK; devclass |= SDL_UDEV_DEVICE_JOYSTICK;
} }
if (devclass == 0) {
val = _this->udev_device_get_property_value(dev, "ID_INPUT_MOUSE"); val = _this->udev_device_get_property_value(dev, "ID_INPUT_MOUSE");
if (val != NULL && SDL_strcmp(val, "1") == 0 ) { if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
devclass = SDL_UDEV_DEVICE_MOUSE; devclass |= SDL_UDEV_DEVICE_MOUSE;
}
} }
if (devclass == 0) {
val = _this->udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"); val = _this->udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD");
if (val != NULL && SDL_strcmp(val, "1") == 0 ) { if (val != NULL && SDL_strcmp(val, "1") == 0 ) {
devclass = SDL_UDEV_DEVICE_KEYBOARD; devclass |= SDL_UDEV_DEVICE_KEYBOARD;
}
} }
if (devclass == 0) { if (devclass == 0) {
return; return;
} }
} } else {
else {
return; return;
} }

View File

@ -46,15 +46,16 @@ typedef enum
SDL_UDEV_DEVICEREMOVED SDL_UDEV_DEVICEREMOVED
} SDL_UDEV_deviceevent; } SDL_UDEV_deviceevent;
/* A device can be any combination of these classes */
typedef enum typedef enum
{ {
SDL_UDEV_DEVICE_MOUSE = 0x0001, SDL_UDEV_DEVICE_MOUSE = 0x0001,
SDL_UDEV_DEVICE_KEYBOARD, SDL_UDEV_DEVICE_KEYBOARD = 0x0002,
SDL_UDEV_DEVICE_JOYSTICK, SDL_UDEV_DEVICE_JOYSTICK = 0x0004,
SDL_UDEV_DEVICE_SOUND SDL_UDEV_DEVICE_SOUND = 0x0008
} SDL_UDEV_deviceclass; } SDL_UDEV_deviceclass;
typedef void (*SDL_UDEV_Callback)(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class, const char *devpath); typedef void (*SDL_UDEV_Callback)(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath);
typedef struct SDL_UDEV_CallbackList { typedef struct SDL_UDEV_CallbackList {
SDL_UDEV_Callback callback; SDL_UDEV_Callback callback;

View File

@ -62,8 +62,8 @@ static void SDL_EVDEV_sync_device(SDL_evdevlist_item *item);
static int SDL_EVDEV_device_removed(const char *devpath); static int SDL_EVDEV_device_removed(const char *devpath);
#if SDL_USE_LIBUDEV #if SDL_USE_LIBUDEV
static int SDL_EVDEV_device_added(const SDL_UDEV_deviceclass devclass, const char *devpath); static int SDL_EVDEV_device_added(const char *devpath);
void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class, const char *devpath); void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath);
#endif /* SDL_USE_LIBUDEV */ #endif /* SDL_USE_LIBUDEV */
static SDL_Scancode EVDEV_Keycodes[] = { static SDL_Scancode EVDEV_Keycodes[] = {
@ -403,7 +403,6 @@ SDL_EVDEV_Init(void)
/* We need a physical terminal (not PTS) to be able to translate key code to symbols via the kernel tables */ /* We need a physical terminal (not PTS) to be able to translate key code to symbols via the kernel tables */
_this->console_fd = SDL_EVDEV_get_console_fd(); _this->console_fd = SDL_EVDEV_get_console_fd();
} }
_this->ref_count += 1; _this->ref_count += 1;
@ -445,32 +444,19 @@ SDL_EVDEV_Quit(void)
} }
#if SDL_USE_LIBUDEV #if SDL_USE_LIBUDEV
void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class, const char *devpath) void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
{ {
SDL_EVDEV_deviceclass devclass;
if (devpath == NULL) { if (devpath == NULL) {
return; return;
} }
switch( udev_class ) if (!(udev_class & (SDL_UDEV_DEVICE_MOUSE|SDL_UDEV_DEVICE_KEYBOARD))) {
{
case SDL_UDEV_DEVICE_MOUSE:
devclass = SDL_EVDEV_DEVICE_MOUSE;
break;
case SDL_UDEV_DEVICE_KEYBOARD:
devclass = SDL_EVDEV_DEVICE_KEYBOARD;
break;
default:
return; return;
} }
switch( udev_type ) switch( udev_type ) {
{
case SDL_UDEV_DEVICEADDED: case SDL_UDEV_DEVICEADDED:
SDL_EVDEV_device_added(devclass, devpath); SDL_EVDEV_device_added(devpath);
break; break;
case SDL_UDEV_DEVICEREMOVED: case SDL_UDEV_DEVICEREMOVED:
@ -479,9 +465,7 @@ void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclas
default: default:
break; break;
} }
} }
#endif /* SDL_USE_LIBUDEV */ #endif /* SDL_USE_LIBUDEV */
@ -507,20 +491,30 @@ SDL_EVDEV_Poll(void)
SDL_UDEV_Poll(); SDL_UDEV_Poll();
#endif #endif
mouse = SDL_GetMouse();
for (item = _this->first; item != NULL; item = item->next) { for (item = _this->first; item != NULL; item = item->next) {
while ((len = read(item->fd, events, (sizeof events))) > 0) { while ((len = read(item->fd, events, (sizeof events))) > 0) {
len /= sizeof(events[0]); len /= sizeof(events[0]);
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
switch(item->devclass) {
case SDL_EVDEV_DEVICE_KEYBOARD:
switch (events[i].type) { switch (events[i].type) {
case EV_KEY: case EV_KEY:
if (events[i].code >= BTN_MOUSE && events[i].code < BTN_MOUSE + SDL_arraysize(EVDEV_MouseButtons)) {
mouse_button = events[i].code - BTN_MOUSE;
if (events[i].value == 0) {
SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_RELEASED, EVDEV_MouseButtons[mouse_button]);
} else if (events[i].value == 1) {
SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_PRESSED, EVDEV_MouseButtons[mouse_button]);
}
break;
}
/* Probably keyboard */
scan_code = SDL_EVDEV_translate_keycode(events[i].code); scan_code = SDL_EVDEV_translate_keycode(events[i].code);
if (scan_code != SDL_SCANCODE_UNKNOWN) { if (scan_code != SDL_SCANCODE_UNKNOWN) {
if (events[i].value == 0) { if (events[i].value == 0) {
SDL_SendKeyboardKey(SDL_RELEASED, scan_code); SDL_SendKeyboardKey(SDL_RELEASED, scan_code);
} } else if (events[i].value == 1 || events[i].value == 2 /* Key repeated */ ) {
else if (events[i].value == 1 || events[i].value == 2 /* Key repeated */ ) {
SDL_SendKeyboardKey(SDL_PRESSED, scan_code); SDL_SendKeyboardKey(SDL_PRESSED, scan_code);
#ifdef SDL_INPUT_LINUXKD #ifdef SDL_INPUT_LINUXKD
if (_this->console_fd >= 0) { if (_this->console_fd >= 0) {
@ -550,8 +544,7 @@ SDL_EVDEV_Poll(void)
if ( modstate & KMOD_CAPS && isalpha(kval) ) { if ( modstate & KMOD_CAPS && isalpha(kval) ) {
if ( isupper(kval) ) { if ( isupper(kval) ) {
kval = tolower(kval); kval = tolower(kval);
} } else {
else {
kval = toupper(kval); kval = toupper(kval);
} }
} }
@ -562,27 +555,7 @@ SDL_EVDEV_Poll(void)
SDL_SendKeyboardText(keysym); SDL_SendKeyboardText(keysym);
} }
} }
#endif #endif /* SDL_INPUT_LINUXKD */
}
}
break;
default:
break;
}
break; /* SDL_EVDEV_DEVICE_KEYBOARD */
case SDL_EVDEV_DEVICE_MOUSE:
mouse = SDL_GetMouse();
switch (events[i].type) {
case EV_KEY:
mouse_button = events[i].code - BTN_MOUSE;
if (mouse_button >= 0 && mouse_button < SDL_arraysize(EVDEV_MouseButtons)) {
if (events[i].value == 0) {
SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_RELEASED, EVDEV_MouseButtons[mouse_button]);
}
else if (events[i].value == 1) {
SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_PRESSED, EVDEV_MouseButtons[mouse_button]);
} }
} }
break; break;
@ -616,18 +589,6 @@ SDL_EVDEV_Poll(void)
break; break;
} }
break; break;
default:
break;
}
break; /* SDL_EVDEV_DEVICE_MOUSE */
default:
break;
}
/* Handle events not specific to any type of device */
switch (events[i].type) {
case EV_SYN: case EV_SYN:
switch (events[i].code) { switch (events[i].code) {
case SYN_DROPPED: case SYN_DROPPED:
@ -636,8 +597,8 @@ SDL_EVDEV_Poll(void)
default: default:
break; break;
} }
break;
} }
} }
} }
} }
@ -665,7 +626,7 @@ SDL_EVDEV_sync_device(SDL_evdevlist_item *item)
#if SDL_USE_LIBUDEV #if SDL_USE_LIBUDEV
static int static int
SDL_EVDEV_device_added(const SDL_UDEV_deviceclass devclass, const char *devpath) SDL_EVDEV_device_added(const char *devpath)
{ {
SDL_evdevlist_item *item; SDL_evdevlist_item *item;
@ -681,9 +642,6 @@ SDL_EVDEV_device_added(const SDL_UDEV_deviceclass devclass, const char *devpath)
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
item->devclass = devclass;
item->fd = open(devpath, O_RDONLY, 0); item->fd = open(devpath, O_RDONLY, 0);
if (item->fd < 0) { if (item->fd < 0) {
SDL_free(item); SDL_free(item);

View File

@ -29,17 +29,10 @@
#include "SDL_events.h" #include "SDL_events.h"
#include <sys/stat.h> #include <sys/stat.h>
typedef enum
{
SDL_EVDEV_DEVICE_MOUSE = 0x0001,
SDL_EVDEV_DEVICE_KEYBOARD
} SDL_EVDEV_deviceclass;
typedef struct SDL_evdevlist_item typedef struct SDL_evdevlist_item
{ {
char *path; char *path;
int fd; int fd;
SDL_EVDEV_deviceclass devclass;
struct SDL_evdevlist_item *next; struct SDL_evdevlist_item *next;
} SDL_evdevlist_item; } SDL_evdevlist_item;

View File

@ -57,7 +57,7 @@
static int MaybeAddDevice(const char *path); static int MaybeAddDevice(const char *path);
#if SDL_USE_LIBUDEV #if SDL_USE_LIBUDEV
static int MaybeRemoveDevice(const char *path); static int MaybeRemoveDevice(const char *path);
void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class, const char *devpath); void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath);
#endif /* SDL_USE_LIBUDEV */ #endif /* SDL_USE_LIBUDEV */
@ -136,11 +136,11 @@ IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *gui
} }
#if SDL_USE_LIBUDEV #if SDL_USE_LIBUDEV
void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class, const char *devpath) void joystick_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
{ {
int instance; int instance;
if (devpath == NULL || udev_class != SDL_UDEV_DEVICE_JOYSTICK) { if (devpath == NULL || !(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) {
return; return;
} }