Ignore accelerometers and improve debugging of player numbers

This commit is contained in:
Cameron Gutman 2021-07-25 18:49:11 -05:00
parent 4dd6ab97df
commit 12efeda8e1

View File

@ -142,13 +142,13 @@ static bool evdev_init_parms(struct input_device *dev, struct input_abs_parms *p
static void evdev_remove(int devindex) { static void evdev_remove(int devindex) {
numDevices--; numDevices--;
printf("Input device removed: %s (player %d)\n", libevdev_get_name(devices[devindex].dev), devices[devindex].controllerId);
if (devices[devindex].controllerId >= 0) if (devices[devindex].controllerId >= 0)
assignedControllerIds &= ~(1 << devices[devindex].controllerId); assignedControllerIds &= ~(1 << devices[devindex].controllerId);
if (devindex != numDevices && numDevices > 0) if (devindex != numDevices && numDevices > 0)
memcpy(&devices[devindex], &devices[numDevices], sizeof(struct input_device)); memcpy(&devices[devindex], &devices[numDevices], sizeof(struct input_device));
fprintf(stderr, "Removed input device\n");
} }
static short evdev_convert_value(struct input_event *ev, struct input_device *dev, struct input_abs_parms *parms, bool reverse) { static short evdev_convert_value(struct input_event *ev, struct input_device *dev, struct input_abs_parms *parms, bool reverse) {
@ -226,6 +226,7 @@ static bool evdev_handle_event(struct input_event *ev, struct input_device *dev)
if ((assignedControllerIds & (1 << i)) == 0) { if ((assignedControllerIds & (1 << i)) == 0) {
assignedControllerIds |= (1 << i); assignedControllerIds |= (1 << i);
dev->controllerId = i; dev->controllerId = i;
printf("Assigned %s as player %d\n", libevdev_get_name(dev->dev), i+1);
break; break;
} }
} }
@ -496,7 +497,7 @@ static bool evdev_handle_event(struct input_event *ev, struct input_device *dev)
} }
if (gamepadModified && (dev->buttonFlags & QUIT_BUTTONS) == QUIT_BUTTONS) { if (gamepadModified && (dev->buttonFlags & QUIT_BUTTONS) == QUIT_BUTTONS) {
LiSendMultiControllerEvent(dev->controllerId, 1, 0, 0, 0, 0, 0, 0, 0); LiSendMultiControllerEvent(dev->controllerId, assignedControllerIds, 0, 0, 0, 0, 0, 0, 0);
return false; return false;
} }
@ -628,7 +629,15 @@ void evdev_create(const char* device, struct mapping* mappings, bool verbose, in
bool is_mouse = libevdev_has_event_type(evdev, EV_REL) || libevdev_has_event_code(evdev, EV_KEY, BTN_LEFT); bool is_mouse = libevdev_has_event_type(evdev, EV_REL) || libevdev_has_event_code(evdev, EV_KEY, BTN_LEFT);
bool is_touchscreen = libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH); bool is_touchscreen = libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH);
// The gamepad classification logic comes from SDL // This classification logic comes from SDL
bool is_accelerometer =
((libevdev_has_event_code(evdev, EV_ABS, ABS_X) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_Y) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_Z)) ||
(libevdev_has_event_code(evdev, EV_ABS, ABS_RX) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_RY) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_RZ))) &&
!libevdev_has_event_type(evdev, EV_KEY);
bool is_gamepad = bool is_gamepad =
libevdev_has_event_code(evdev, EV_ABS, ABS_X) && libevdev_has_event_code(evdev, EV_ABS, ABS_X) &&
libevdev_has_event_code(evdev, EV_ABS, ABS_Y) && libevdev_has_event_code(evdev, EV_ABS, ABS_Y) &&
@ -652,6 +661,12 @@ void evdev_create(const char* device, struct mapping* mappings, bool verbose, in
if (is_gamepad) if (is_gamepad)
evdev_gamepads++; evdev_gamepads++;
if (is_accelerometer) {
libevdev_free(evdev);
close(fd);
return;
}
int dev = numDevices; int dev = numDevices;
numDevices++; numDevices++;