Skip to content

Commit

Permalink
Add --lock-video-orientation=initial
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rom1v committed Apr 25, 2021
1 parent 151bc16 commit fd0dc6c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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°
Expand Down
4 changes: 2 additions & 2 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions app/src/scrcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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, \
Expand Down
3 changes: 3 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/ScreenInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public ScreenInfo withDeviceRotation(int newDeviceRotation) {

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) {
Expand Down

0 comments on commit fd0dc6c

Please sign in to comment.