Skip to content

Commit

Permalink
Use shortcut mods as mouse capture keys
Browse files Browse the repository at this point in the history
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 <#5318>
  • Loading branch information
rom1v committed Sep 27, 2024
1 parent 7ec5460 commit 4d9dba2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
13 changes: 8 additions & 5 deletions app/src/mouse_capture.c
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion app/src/mouse_capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/src/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions app/src/usb/scrcpy_otg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params);
Expand Down
2 changes: 1 addition & 1 deletion app/src/usb/screen_otg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/src/usb/screen_otg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4d9dba2

Please sign in to comment.