From fd0dc6c0cdc22a283e56d1852bbccc57138fb12d Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 19 Apr 2021 18:42:20 +0200 Subject: [PATCH] Add --lock-video-orientation=initial Add a new mode to the --lock-video-orientation option, to lock the initial orientation of the device. This avoids to pass an explicit value (0, 1, 2 or 3) and think about which is the right one. --- README.md | 1 + app/scrcpy.1 | 4 +-- app/src/cli.c | 26 ++++++++++++++----- app/src/scrcpy.h | 14 ++++++++-- .../java/com/genymobile/scrcpy/Device.java | 3 +++ .../com/genymobile/scrcpy/ScreenInfo.java | 6 +++++ 6 files changed, 44 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d9aeb142..037c5828 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,7 @@ If `--max-size` is also specified, resizing is applied after cropping. To lock the orientation of the mirroring: ```bash +scrcpy --lock-video-orientation initial # initial (current) orientation scrcpy --lock-video-orientation 0 # natural orientation scrcpy --lock-video-orientation 1 # 90° counterclockwise scrcpy --lock-video-orientation 2 # 180° diff --git a/app/scrcpy.1 b/app/scrcpy.1 index ea9bce9b..89b7ee75 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -84,9 +84,9 @@ This is a workaround for some devices not behaving as expected when setting the .TP .BI "\-\-lock\-video\-orientation " value -Lock video orientation to \fIvalue\fR. Possible values are -1 (unlocked), 0, 1, 2 and 3. Natural device orientation is 0, and each increment adds a 90 degrees otation counterclockwise. +Lock video orientation to \fIvalue\fR. Possible values are "unlocked", "initial" (locked to the initial orientation), 0, 1, 2 and 3. Natural device orientation is 0, and each increment adds a 90 degrees otation counterclockwise. -Default is -1 (unlocked). +Default is "unlocked". .TP .BI "\-\-max\-fps " value diff --git a/app/src/cli.c b/app/src/cli.c index ec3c1294..05e178e0 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -81,10 +81,11 @@ scrcpy_print_usage(const char *arg0) { "\n" " --lock-video-orientation value\n" " Lock video orientation to value.\n" - " Possible values are -1 (unlocked), 0, 1, 2 and 3.\n" + " Possible values are \"unlocked\", \"initial\" (locked to the\n" + " initial orientation), 0, 1, 2 and 3.\n" " Natural device orientation is 0, and each increment adds a\n" " 90 degrees rotation counterclockwise.\n" - " Default is -1 (unlocked).\n" + " Default is \"unlocked\".\n" "\n" " --max-fps value\n" " Limit the frame rate of screen capture (officially supported\n" @@ -383,15 +384,27 @@ parse_max_fps(const char *s, uint16_t *max_fps) { } static bool -parse_lock_video_orientation(const char *s, int8_t *lock_video_orientation) { +parse_lock_video_orientation(const char *s, + enum sc_lock_video_orientation *lock_mode) { + if (!strcmp(s, "initial")) { + // Without argument, lock the initial orientation + *lock_mode = SC_LOCK_VIDEO_ORIENTATION_INITIAL; + return true; + } + + if (!strcmp(s, "unlocked")) { + *lock_mode = SC_LOCK_VIDEO_ORIENTATION_UNLOCKED; + return true; + } + long value; - bool ok = parse_integer_arg(s, &value, false, -1, 3, + bool ok = parse_integer_arg(s, &value, false, 0, 3, "lock video orientation"); if (!ok) { return false; } - *lock_video_orientation = (int8_t) value; + *lock_mode = (enum sc_lock_video_orientation) value; return true; } @@ -765,7 +778,8 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) { } break; case OPT_LOCK_VIDEO_ORIENTATION: - if (!parse_lock_video_orientation(optarg, &opts->lock_video_orientation)) { + if (!parse_lock_video_orientation(optarg, + &opts->lock_video_orientation)) { return false; } break; diff --git a/app/src/scrcpy.h b/app/src/scrcpy.h index f91cb6b8..7c2689ab 100644 --- a/app/src/scrcpy.h +++ b/app/src/scrcpy.h @@ -20,6 +20,16 @@ enum sc_record_format { SC_RECORD_FORMAT_MKV, }; +enum sc_lock_video_orientation { + SC_LOCK_VIDEO_ORIENTATION_UNLOCKED = -1, + // lock the current orientation when scrcpy starts + SC_LOCK_VIDEO_ORIENTATION_INITIAL = -2, + SC_LOCK_VIDEO_ORIENTATION_0 = 0, + SC_LOCK_VIDEO_ORIENTATION_1, + SC_LOCK_VIDEO_ORIENTATION_2, + SC_LOCK_VIDEO_ORIENTATION_3, +}; + #define SC_MAX_SHORTCUT_MODS 8 enum sc_shortcut_mod { @@ -59,7 +69,7 @@ struct scrcpy_options { uint16_t max_size; uint32_t bit_rate; uint16_t max_fps; - int8_t lock_video_orientation; + enum sc_lock_video_orientation lock_video_orientation; uint8_t rotation; int16_t window_x; // SC_WINDOW_POSITION_UNDEFINED for "auto" int16_t window_y; // SC_WINDOW_POSITION_UNDEFINED for "auto" @@ -106,7 +116,7 @@ struct scrcpy_options { .max_size = 0, \ .bit_rate = DEFAULT_BIT_RATE, \ .max_fps = 0, \ - .lock_video_orientation = -1, \ + .lock_video_orientation = SC_LOCK_VIDEO_ORIENTATION_UNLOCKED, \ .rotation = 0, \ .window_x = SC_WINDOW_POSITION_UNDEFINED, \ .window_y = SC_WINDOW_POSITION_UNDEFINED, \ diff --git a/server/src/main/java/com/genymobile/scrcpy/Device.java b/server/src/main/java/com/genymobile/scrcpy/Device.java index a63976fd..2d67ab6f 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Device.java +++ b/server/src/main/java/com/genymobile/scrcpy/Device.java @@ -25,6 +25,9 @@ public final class Device { public static final int POWER_MODE_OFF = SurfaceControl.POWER_MODE_OFF; public static final int POWER_MODE_NORMAL = SurfaceControl.POWER_MODE_NORMAL; + public static final int LOCK_VIDEO_ORIENTATION_UNLOCKED = -1; + public static final int LOCK_VIDEO_ORIENTATION_INITIAL = -2; + private static final ServiceManager SERVICE_MANAGER = new ServiceManager(); public interface RotationListener { diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenInfo.java b/server/src/main/java/com/genymobile/scrcpy/ScreenInfo.java index 10acfb50..c27322ef 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenInfo.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenInfo.java @@ -82,6 +82,12 @@ public final class ScreenInfo { public static ScreenInfo computeScreenInfo(DisplayInfo displayInfo, Rect crop, int maxSize, int lockedVideoOrientation) { int rotation = displayInfo.getRotation(); + + if (lockedVideoOrientation == Device.LOCK_VIDEO_ORIENTATION_INITIAL) { + // The user requested to lock the video orientation to the current orientation + lockedVideoOrientation = rotation; + } + Size deviceSize = displayInfo.getSize(); Rect contentRect = new Rect(0, 0, deviceSize.getWidth(), deviceSize.getHeight()); if (crop != null) {