Display and control your Android device
Split gamepad device added/removed events
Romain Vimont4bd1c5981db3
diff --git a/app/src/input_events.h b/app/src/input_events.hindex c8966a35..ad3afa81 100644--- a/app/src/input_events.h+++ b/app/src/input_events.h@@ -412,18 +412,12 @@ struct sc_touch_event {float pressure;};-enum sc_gamepad_device_event_type {- SC_GAMEPAD_DEVICE_ADDED,- SC_GAMEPAD_DEVICE_REMOVED,-};-// As documented in <https://wiki.libsdl.org/SDL2/SDL_JoystickID>:// 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;uint32_t gamepad_id;};@@ -503,16 +497,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_axissc_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.cindex 3955c211..2e4337db 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 voidsc_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_device_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_device_event evt = {+ .gamepad_id = id,+ };+ im->gp->ops->process_gamepad_removed(im->gp, &evt);} else {// Nothing to doreturn;}-- 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 voiddiff --git a/app/src/trait/gamepad_processor.h b/app/src/trait/gamepad_processor.hindex 72479783..19629a9a 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 event** 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_device_event *event);++ /**+ * Process a gamepad device removed event+ *+ * This function is mandatory.+ */+ void+ (*process_gamepad_removed)(struct sc_gamepad_processor *gp,+ const struct sc_gamepad_device_event *event);/*** Process a gamepad axis eventdiff --git a/app/src/uhid/gamepad_uhid.c b/app/src/uhid/gamepad_uhid.cindex 62b0f653..42db63e7 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,+sc_gamepad_processor_process_gamepad_added(struct sc_gamepad_processor *gp,const struct sc_gamepad_device_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;- }+ 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);+ sc_gamepad_uhid_send_open(gamepad, &hid_open);+}- 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_device_event *event) {+ struct sc_gamepad_uhid *gamepad = DOWNCAST(gp);- sc_gamepad_uhid_send_close(gamepad, &hid_close);+ 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.cindex 37587532..4372379f 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,+sc_gamepad_processor_process_gamepad_added(struct sc_gamepad_processor *gp,const struct sc_gamepad_device_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;- }-- // 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);-- 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)");- }+ 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)");+ }+}++static void+sc_gamepad_processor_process_gamepad_removed(struct sc_gamepad_processor *gp,+ const struct sc_gamepad_device_event *event) {+ struct sc_gamepad_aoa *gamepad = DOWNCAST(gp);++ 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.cindex 18377074..368af125 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_device_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_device_event evt = {+ .gamepad_id = id,+ };+ gp->ops->process_gamepad_removed(gp, &evt);+ }}static void