From a582c0cc1b56a6705abce7c3171bfa4a7079fa15 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 7 Dec 2024 14:12:53 +0100 Subject: [PATCH] Split gamepad device added/removed events Use two separate event structs for gamepad device added and gamepad device removed. In theory, some data (like vendorId and productId) could be added specifically to "device added" events. --- app/src/input_events.h | 22 ++++---------- app/src/input_manager.c | 19 ++++++------ app/src/trait/gamepad_processor.h | 15 ++++++++-- app/src/uhid/gamepad_uhid.c | 45 +++++++++++++++------------- app/src/usb/gamepad_aoa.c | 49 ++++++++++++++++--------------- app/src/usb/screen_otg.c | 22 +++++++------- 6 files changed, 87 insertions(+), 85 deletions(-) diff --git a/app/src/input_events.h b/app/src/input_events.h index c8966a35..f29263ad 100644 --- a/app/src/input_events.h +++ b/app/src/input_events.h @@ -412,18 +412,16 @@ struct sc_touch_event { float pressure; }; -enum sc_gamepad_device_event_type { - SC_GAMEPAD_DEVICE_ADDED, - SC_GAMEPAD_DEVICE_REMOVED, -}; - // As documented in : // The ID value starts at 0 and increments from there. The value -1 is an // invalid ID. #define SC_GAMEPAD_ID_INVALID UINT32_C(-1) -struct sc_gamepad_device_event { - enum sc_gamepad_device_event_type type; +struct sc_gamepad_added_event { + uint32_t gamepad_id; +}; + +struct sc_gamepad_removed_event { uint32_t gamepad_id; }; @@ -503,16 +501,6 @@ sc_mouse_buttons_state_from_sdl(uint32_t buttons_state) { return buttons_state; } -static inline enum sc_gamepad_device_event_type -sc_gamepad_device_event_type_from_sdl_type(uint32_t type) { - assert(type == SDL_CONTROLLERDEVICEADDED - || type == SDL_CONTROLLERDEVICEREMOVED); - if (type == SDL_CONTROLLERDEVICEADDED) { - return SC_GAMEPAD_DEVICE_ADDED; - } - return SC_GAMEPAD_DEVICE_REMOVED; -} - static inline enum sc_gamepad_axis sc_gamepad_axis_from_sdl(uint8_t axis) { if (axis <= SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { diff --git a/app/src/input_manager.c b/app/src/input_manager.c index 3955c211..47b3fd06 100644 --- a/app/src/input_manager.c +++ b/app/src/input_manager.c @@ -908,7 +908,6 @@ sc_input_manager_process_mouse_wheel(struct sc_input_manager *im, static void sc_input_manager_process_gamepad_device(struct sc_input_manager *im, const SDL_ControllerDeviceEvent *event) { - SDL_JoystickID id; if (event->type == SDL_CONTROLLERDEVICEADDED) { SDL_GameController *gc = SDL_GameControllerOpen(event->which); if (!gc) { @@ -923,9 +922,12 @@ sc_input_manager_process_gamepad_device(struct sc_input_manager *im, return; } - id = SDL_JoystickInstanceID(joystick); + struct sc_gamepad_added_event evt = { + .gamepad_id = SDL_JoystickInstanceID(joystick), + }; + im->gp->ops->process_gamepad_added(im->gp, &evt); } else if (event->type == SDL_CONTROLLERDEVICEREMOVED) { - id = event->which; + SDL_JoystickID id = event->which; SDL_GameController *gc = SDL_GameControllerFromInstanceID(id); if (gc) { @@ -933,16 +935,15 @@ sc_input_manager_process_gamepad_device(struct sc_input_manager *im, } else { LOGW("Unknown gamepad device removed"); } + + struct sc_gamepad_removed_event evt = { + .gamepad_id = id, + }; + im->gp->ops->process_gamepad_removed(im->gp, &evt); } else { // Nothing to do return; } - - struct sc_gamepad_device_event evt = { - .type = sc_gamepad_device_event_type_from_sdl_type(event->type), - .gamepad_id = id, - }; - im->gp->ops->process_gamepad_device(im->gp, &evt); } static void diff --git a/app/src/trait/gamepad_processor.h b/app/src/trait/gamepad_processor.h index 72479783..6a592567 100644 --- a/app/src/trait/gamepad_processor.h +++ b/app/src/trait/gamepad_processor.h @@ -20,13 +20,22 @@ struct sc_gamepad_processor { struct sc_gamepad_processor_ops { /** - * Process a gamepad device added or removed + * Process a gamepad device added * * This function is mandatory. */ void - (*process_gamepad_device)(struct sc_gamepad_processor *gp, - const struct sc_gamepad_device_event *event); + (*process_gamepad_added)(struct sc_gamepad_processor *gp, + const struct sc_gamepad_added_event *event); + + /** + * Process a gamepad device removed + * + * This function is mandatory. + */ + void + (*process_gamepad_removed)(struct sc_gamepad_processor *gp, + const struct sc_gamepad_removed_event *event); /** * Process a gamepad axis event diff --git a/app/src/uhid/gamepad_uhid.c b/app/src/uhid/gamepad_uhid.c index 62b0f653..ed52ba3b 100644 --- a/app/src/uhid/gamepad_uhid.c +++ b/app/src/uhid/gamepad_uhid.c @@ -52,29 +52,31 @@ sc_gamepad_uhid_send_close(struct sc_gamepad_uhid *gamepad, } static void -sc_gamepad_processor_process_gamepad_device(struct sc_gamepad_processor *gp, - const struct sc_gamepad_device_event *event) { +sc_gamepad_processor_process_gamepad_added(struct sc_gamepad_processor *gp, + const struct sc_gamepad_added_event *event) { struct sc_gamepad_uhid *gamepad = DOWNCAST(gp); - if (event->type == SC_GAMEPAD_DEVICE_ADDED) { - struct sc_hid_open hid_open; - if (!sc_hid_gamepad_generate_open(&gamepad->hid, &hid_open, - event->gamepad_id)) { - return; - } - - sc_gamepad_uhid_send_open(gamepad, &hid_open); - } else { - assert(event->type == SC_GAMEPAD_DEVICE_REMOVED); - - struct sc_hid_close hid_close; - if (!sc_hid_gamepad_generate_close(&gamepad->hid, &hid_close, - event->gamepad_id)) { - return; - } - - sc_gamepad_uhid_send_close(gamepad, &hid_close); + struct sc_hid_open hid_open; + if (!sc_hid_gamepad_generate_open(&gamepad->hid, &hid_open, + event->gamepad_id)) { + return; } + + sc_gamepad_uhid_send_open(gamepad, &hid_open); +} + +static void +sc_gamepad_processor_process_gamepad_removed(struct sc_gamepad_processor *gp, + const struct sc_gamepad_removed_event *event) { + struct sc_gamepad_uhid *gamepad = DOWNCAST(gp); + + struct sc_hid_close hid_close; + if (!sc_hid_gamepad_generate_close(&gamepad->hid, &hid_close, + event->gamepad_id)) { + return; + } + + sc_gamepad_uhid_send_close(gamepad, &hid_close); } static void @@ -114,7 +116,8 @@ sc_gamepad_uhid_init(struct sc_gamepad_uhid *gamepad, gamepad->controller = controller; static const struct sc_gamepad_processor_ops ops = { - .process_gamepad_device = sc_gamepad_processor_process_gamepad_device, + .process_gamepad_added = sc_gamepad_processor_process_gamepad_added, + .process_gamepad_removed = sc_gamepad_processor_process_gamepad_removed, .process_gamepad_axis = sc_gamepad_processor_process_gamepad_axis, .process_gamepad_button = sc_gamepad_processor_process_gamepad_button, }; diff --git a/app/src/usb/gamepad_aoa.c b/app/src/usb/gamepad_aoa.c index 37587532..f8f15ffa 100644 --- a/app/src/usb/gamepad_aoa.c +++ b/app/src/usb/gamepad_aoa.c @@ -7,33 +7,35 @@ #define DOWNCAST(GP) container_of(GP, struct sc_gamepad_aoa, gamepad_processor) static void -sc_gamepad_processor_process_gamepad_device(struct sc_gamepad_processor *gp, - const struct sc_gamepad_device_event *event) { +sc_gamepad_processor_process_gamepad_added(struct sc_gamepad_processor *gp, + const struct sc_gamepad_added_event *event) { struct sc_gamepad_aoa *gamepad = DOWNCAST(gp); - if (event->type == SC_GAMEPAD_DEVICE_ADDED) { - struct sc_hid_open hid_open; - if (!sc_hid_gamepad_generate_open(&gamepad->hid, &hid_open, - event->gamepad_id)) { - return; - } + struct sc_hid_open hid_open; + if (!sc_hid_gamepad_generate_open(&gamepad->hid, &hid_open, + event->gamepad_id)) { + return; + } - // exit_on_error: false (a gamepad open failure should not exit scrcpy) - if (!sc_aoa_push_open(gamepad->aoa, &hid_open, false)) { - LOGW("Could not push AOA HID open (gamepad)"); - } - } else { - assert(event->type == SC_GAMEPAD_DEVICE_REMOVED); + // exit_on_error: false (a gamepad open failure should not exit scrcpy) + if (!sc_aoa_push_open(gamepad->aoa, &hid_open, false)) { + LOGW("Could not push AOA HID open (gamepad)"); + } +} - struct sc_hid_close hid_close; - if (!sc_hid_gamepad_generate_close(&gamepad->hid, &hid_close, - event->gamepad_id)) { - return; - } +static void +sc_gamepad_processor_process_gamepad_removed(struct sc_gamepad_processor *gp, + const struct sc_gamepad_removed_event *event) { + struct sc_gamepad_aoa *gamepad = DOWNCAST(gp); - if (!sc_aoa_push_close(gamepad->aoa, &hid_close)) { - LOGW("Could not push AOA HID close (gamepad)"); - } + struct sc_hid_close hid_close; + if (!sc_hid_gamepad_generate_close(&gamepad->hid, &hid_close, + event->gamepad_id)) { + return; + } + + if (!sc_aoa_push_close(gamepad->aoa, &hid_close)) { + LOGW("Could not push AOA HID close (gamepad)"); } } @@ -76,7 +78,8 @@ sc_gamepad_aoa_init(struct sc_gamepad_aoa *gamepad, struct sc_aoa *aoa) { sc_hid_gamepad_init(&gamepad->hid); static const struct sc_gamepad_processor_ops ops = { - .process_gamepad_device = sc_gamepad_processor_process_gamepad_device, + .process_gamepad_added = sc_gamepad_processor_process_gamepad_added, + .process_gamepad_removed = sc_gamepad_processor_process_gamepad_removed, .process_gamepad_axis = sc_gamepad_processor_process_gamepad_axis, .process_gamepad_button = sc_gamepad_processor_process_gamepad_button, }; diff --git a/app/src/usb/screen_otg.c b/app/src/usb/screen_otg.c index 18377074..50e9c242 100644 --- a/app/src/usb/screen_otg.c +++ b/app/src/usb/screen_otg.c @@ -175,7 +175,6 @@ sc_screen_otg_process_gamepad_device(struct sc_screen_otg *screen, assert(screen->gamepad); struct sc_gamepad_processor *gp = &screen->gamepad->gamepad_processor; - SDL_JoystickID id; if (event->type == SDL_CONTROLLERDEVICEADDED) { SDL_GameController *gc = SDL_GameControllerOpen(event->which); if (!gc) { @@ -190,9 +189,12 @@ sc_screen_otg_process_gamepad_device(struct sc_screen_otg *screen, return; } - id = SDL_JoystickInstanceID(joystick); + struct sc_gamepad_added_event evt = { + .gamepad_id = SDL_JoystickInstanceID(joystick), + }; + gp->ops->process_gamepad_added(gp, &evt); } else if (event->type == SDL_CONTROLLERDEVICEREMOVED) { - id = event->which; + SDL_JoystickID id = event->which; SDL_GameController *gc = SDL_GameControllerFromInstanceID(id); if (gc) { @@ -200,16 +202,12 @@ sc_screen_otg_process_gamepad_device(struct sc_screen_otg *screen, } else { LOGW("Unknown gamepad device removed"); } - } else { - // Nothing to do - return; - } - struct sc_gamepad_device_event evt = { - .type = sc_gamepad_device_event_type_from_sdl_type(event->type), - .gamepad_id = id, - }; - gp->ops->process_gamepad_device(gp, &evt); + struct sc_gamepad_removed_event evt = { + .gamepad_id = id, + }; + gp->ops->process_gamepad_removed(gp, &evt); + } } static void