Skip to content

Display and control your Android device

Share SDL hints between normal and OTG modes

Romain Vimont32d2f06f84ef
diff --git a/app/meson.build b/app/meson.build
index d3336daa..fd58098e 100644
--- a/app/meson.build
+++ b/app/meson.build
@@ -32,6 +32,7 @@ src = [
'src/recorder.c',
'src/scrcpy.c',
'src/screen.c',
+ 'src/sdl_hints.c',
'src/server.c',
'src/texture.c',
'src/version.c',
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index 87fdc7ba..05b16ed5 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -25,6 +25,7 @@
#include "mouse_sdk.h"
#include "recorder.h"
#include "screen.h"
+#include "sdl_hints.h"
#include "server.h"
#include "uhid/gamepad_uhid.h"
#include "uhid/keyboard_uhid.h"
@@ -110,49 +111,6 @@ sdl_configure_ctrl_c_windows(void) {
}
#endif // _WIN32
-static void
-sdl_set_hints(const char *render_driver, bool disable_screensaver) {
- if (render_driver && !SDL_SetHint(SDL_HINT_RENDER_DRIVER, render_driver)) {
- LOGW("Could not set render driver");
- }
-
- // App name used in various contexts (such as PulseAudio)
- if (!SDL_SetHint(SDL_HINT_APP_NAME, "scrcpy")) {
- LOGW("Could not set app name");
- }
-
- // Handle a click to gain focus as any other click
- if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1")) {
- LOGW("Could not enable mouse focus clickthrough");
- }
-
- // Disable synthetic mouse events from touch events
- // Touch events with id SDL_TOUCH_MOUSEID are ignored anyway, but it is
- // better not to generate them in the first place.
- if (!SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0")) {
- LOGW("Could not disable synthetic mouse events");
- }
-
- // Disable compositor bypassing on X11
- if (!SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0")) {
- LOGW("Could not disable X11 compositor bypass");
- }
-
- // Do not minimize on focus loss
- if (!SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0")) {
- LOGW("Could not disable minimize on focus loss");
- }
-
- if (!SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1")) {
- LOGW("Could not allow joystick background events");
- }
-
- if (!disable_screensaver
- && !SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1")) {
- LOGW("Could not enable screensaver");
- }
-}
-
static enum scrcpy_exit_code
event_loop(struct scrcpy *s, bool has_screen) {
SDL_Event event;
@@ -484,7 +442,7 @@ scrcpy(struct scrcpy_options *options) {
// Set hints before starting the server thread to avoid race conditions in
// SDL
- sdl_set_hints(options->render_driver, options->disable_screensaver);
+ sc_sdl_set_hints(options->render_driver, options->disable_screensaver);
if (!sc_server_start(&s->server)) {
goto end;
diff --git a/app/src/sdl_hints.c b/app/src/sdl_hints.c
new file mode 100644
index 00000000..b7fc55b4
--- /dev/null
+++ b/app/src/sdl_hints.c
@@ -0,0 +1,47 @@
+#include "sdl_hints.h"
+
+#include "util/log.h"
+
+void
+sc_sdl_set_hints(const char *render_driver, bool disable_screensaver) {
+ if (render_driver && !SDL_SetHint(SDL_HINT_RENDER_DRIVER, render_driver)) {
+ LOGW("Could not set render driver");
+ }
+
+ // App name used in various contexts (such as PulseAudio)
+ if (!SDL_SetHint(SDL_HINT_APP_NAME, "scrcpy")) {
+ LOGW("Could not set app name");
+ }
+
+ // Handle a click to gain focus as any other click
+ if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1")) {
+ LOGW("Could not enable mouse focus clickthrough");
+ }
+
+ // Disable synthetic mouse events from touch events
+ // Touch events with id SDL_TOUCH_MOUSEID are ignored anyway, but it is
+ // better not to generate them in the first place.
+ if (!SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0")) {
+ LOGW("Could not disable synthetic mouse events");
+ }
+
+ // Disable compositor bypassing on X11
+ if (!SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0")) {
+ LOGW("Could not disable X11 compositor bypass");
+ }
+
+ // Do not minimize on focus loss
+ if (!SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0")) {
+ LOGW("Could not disable minimize on focus loss");
+ }
+
+ if (!SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1")) {
+ LOGW("Could not allow joystick background events");
+ }
+
+ if (!disable_screensaver
+ && !SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1")) {
+ LOGW("Could not enable screensaver");
+ }
+}
+
diff --git a/app/src/sdl_hints.h b/app/src/sdl_hints.h
new file mode 100644
index 00000000..7bce55d6
--- /dev/null
+++ b/app/src/sdl_hints.h
@@ -0,0 +1,12 @@
+#ifndef SC_SDL_HINTS_H
+#define SC_SDL_HINTS_H
+
+#include "common.h"
+
+#include <SDL3/SDL_hints.h>
+
+void
+sc_sdl_set_hints(const char *render_driver, bool disable_screensaver);
+
+#endif
+
diff --git a/app/src/usb/scrcpy_otg.c b/app/src/usb/scrcpy_otg.c
index 77189209..ba2768b3 100644
--- a/app/src/usb/scrcpy_otg.c
+++ b/app/src/usb/scrcpy_otg.c
@@ -10,6 +10,7 @@
#endif
#include "events.h"
#include "screen.h"
+#include "sdl_hints.h"
#include "usb/aoa_hid.h"
#include "usb/gamepad_aoa.h"
#include "usb/keyboard_aoa.h"
@@ -64,9 +65,7 @@ scrcpy_otg(struct scrcpy_options *options) {
const char *serial = options->serial;
- if (!SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1")) {
- LOGW("Could not allow joystick background events");
- }
+ sc_sdl_set_hints(options->render_driver, options->disable_screensaver);
// Minimal SDL initialization
if (!SDL_Init(SDL_INIT_EVENTS)) {
@@ -83,10 +82,6 @@ scrcpy_otg(struct scrcpy_options *options) {
atexit(SDL_Quit);
- if (!SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1")) {
- LOGW("Could not enable mouse focus clickthrough");
- }
-
enum scrcpy_exit_code ret = SCRCPY_EXIT_FAILURE;
struct sc_key_processor *kp = NULL;