From ff9fb5994dbe555be8835a5f8d06b03e9f3b1b27 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 27 Sep 2024 18:32:39 +0200 Subject: [PATCH] Use shortcut mods as mouse capture keys Instead of using separate hardcoded keys for mouse capture/uncapture, use the shortcut mods. By changing the shortcut mods (for example --shortcut-mod=rctrl), it allows to forward Alt and Super to the device. Fixes #5318 PR #5322 --- app/src/mouse_capture.c | 13 ++++++++----- app/src/mouse_capture.h | 5 ++++- app/src/screen.c | 2 +- app/src/usb/scrcpy_otg.c | 1 + app/src/usb/screen_otg.c | 2 +- app/src/usb/screen_otg.h | 1 + 6 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/src/mouse_capture.c b/app/src/mouse_capture.c index 1420bad6..ee96ae60 100644 --- a/app/src/mouse_capture.c +++ b/app/src/mouse_capture.c @@ -1,16 +1,19 @@ #include "mouse_capture.h" +#include "shortcut_mod.h" #include "util/log.h" void -sc_mouse_capture_init(struct sc_mouse_capture *mc, SDL_Window *window) { +sc_mouse_capture_init(struct sc_mouse_capture *mc, SDL_Window *window, + uint8_t shortcut_mods) { mc->window = window; + mc->sdl_mouse_capture_keys = sc_shortcut_mods_to_sdl(shortcut_mods); mc->mouse_capture_key_pressed = SDLK_UNKNOWN; } static inline bool -sc_mouse_capture_is_capture_key(SDL_Keycode key) { - return key == SDLK_LALT || key == SDLK_LGUI || key == SDLK_RGUI; +sc_mouse_capture_is_capture_key(struct sc_mouse_capture *mc, SDL_Keycode key) { + return sc_shortcut_mods_is_shortcut_key(mc->sdl_mouse_capture_keys, key); } bool @@ -25,7 +28,7 @@ sc_mouse_capture_handle_event(struct sc_mouse_capture *mc, break; case SDL_KEYDOWN: { SDL_Keycode key = event->key.keysym.sym; - if (sc_mouse_capture_is_capture_key(key)) { + if (sc_mouse_capture_is_capture_key(mc, key)) { if (!mc->mouse_capture_key_pressed) { mc->mouse_capture_key_pressed = key; } else { @@ -42,7 +45,7 @@ sc_mouse_capture_handle_event(struct sc_mouse_capture *mc, SDL_Keycode key = event->key.keysym.sym; SDL_Keycode cap = mc->mouse_capture_key_pressed; mc->mouse_capture_key_pressed = 0; - if (sc_mouse_capture_is_capture_key(key)) { + if (sc_mouse_capture_is_capture_key(mc, key)) { if (key == cap) { // A mouse capture key has been pressed then released: // toggle the capture mouse mode diff --git a/app/src/mouse_capture.h b/app/src/mouse_capture.h index 53018c19..f352cc13 100644 --- a/app/src/mouse_capture.h +++ b/app/src/mouse_capture.h @@ -9,14 +9,17 @@ struct sc_mouse_capture { SDL_Window *window; + uint16_t sdl_mouse_capture_keys; // To enable/disable mouse capture, a mouse capture key (LALT, LGUI or // RGUI) must be pressed. This variable tracks the pressed capture key. SDL_Keycode mouse_capture_key_pressed; + }; void -sc_mouse_capture_init(struct sc_mouse_capture *mc, SDL_Window *window); +sc_mouse_capture_init(struct sc_mouse_capture *mc, SDL_Window *window, + uint8_t shortcut_mods); void sc_mouse_capture_set_active(struct sc_mouse_capture *mc, bool capture); diff --git a/app/src/screen.c b/app/src/screen.c index 146f10a5..1d694f12 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -445,7 +445,7 @@ sc_screen_init(struct sc_screen *screen, sc_input_manager_init(&screen->im, &im_params); // Initialize even if not used for simplicity - sc_mouse_capture_init(&screen->mc, screen->window); + sc_mouse_capture_init(&screen->mc, screen->window, params->shortcut_mods); #ifdef CONTINUOUS_RESIZING_WORKAROUND if (screen->video) { diff --git a/app/src/usb/scrcpy_otg.c b/app/src/usb/scrcpy_otg.c index 9595face..1a7e9544 100644 --- a/app/src/usb/scrcpy_otg.c +++ b/app/src/usb/scrcpy_otg.c @@ -185,6 +185,7 @@ scrcpy_otg(struct scrcpy_options *options) { .window_width = options->window_width, .window_height = options->window_height, .window_borderless = options->window_borderless, + .shortcut_mods = options->shortcut_mods, }; ok = sc_screen_otg_init(&s->screen_otg, ¶ms); diff --git a/app/src/usb/screen_otg.c b/app/src/usb/screen_otg.c index aabb8a7f..18377074 100644 --- a/app/src/usb/screen_otg.c +++ b/app/src/usb/screen_otg.c @@ -70,7 +70,7 @@ sc_screen_otg_init(struct sc_screen_otg *screen, LOGW("Could not load icon"); } - sc_mouse_capture_init(&screen->mc, screen->window); + sc_mouse_capture_init(&screen->mc, screen->window, params->shortcut_mods); if (screen->mouse) { // Capture mouse on start diff --git a/app/src/usb/screen_otg.h b/app/src/usb/screen_otg.h index 850a6ae5..427723ad 100644 --- a/app/src/usb/screen_otg.h +++ b/app/src/usb/screen_otg.h @@ -35,6 +35,7 @@ struct sc_screen_otg_params { uint16_t window_width; uint16_t window_height; bool window_borderless; + uint8_t shortcut_mods; // OR of enum sc_shortcut_mod values }; bool