Add option to turn device screen off
In addition to the shortcut (Ctrl+o) to turn the device screen off, add a command-line argument to turn it off on start.
This commit is contained in:
parent
7f07b13446
commit
8e66b33000
4 changed files with 35 additions and 6 deletions
|
@ -28,6 +28,7 @@ struct args {
|
||||||
uint16_t max_size;
|
uint16_t max_size;
|
||||||
uint32_t bit_rate;
|
uint32_t bit_rate;
|
||||||
bool always_on_top;
|
bool always_on_top;
|
||||||
|
bool turn_screen_off;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void usage(const char *arg0) {
|
static void usage(const char *arg0) {
|
||||||
|
@ -82,6 +83,9 @@ static void usage(const char *arg0) {
|
||||||
" The device serial number. Mandatory only if several devices\n"
|
" The device serial number. Mandatory only if several devices\n"
|
||||||
" are connected to adb.\n"
|
" are connected to adb.\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
" -S, --turn-screen-off\n"
|
||||||
|
" Turn the device screen off immediately.\n"
|
||||||
|
"\n"
|
||||||
" -t, --show-touches\n"
|
" -t, --show-touches\n"
|
||||||
" Enable \"show touches\" on start, disable on quit.\n"
|
" Enable \"show touches\" on start, disable on quit.\n"
|
||||||
" It only shows physical touches (not clicks from scrcpy).\n"
|
" It only shows physical touches (not clicks from scrcpy).\n"
|
||||||
|
@ -299,11 +303,12 @@ parse_args(struct args *args, int argc, char *argv[]) {
|
||||||
{"record-format", required_argument, NULL, 'f'},
|
{"record-format", required_argument, NULL, 'f'},
|
||||||
{"serial", required_argument, NULL, 's'},
|
{"serial", required_argument, NULL, 's'},
|
||||||
{"show-touches", no_argument, NULL, 't'},
|
{"show-touches", no_argument, NULL, 't'},
|
||||||
|
{"turn-screen-off", no_argument, NULL, 'S'},
|
||||||
{"version", no_argument, NULL, 'v'},
|
{"version", no_argument, NULL, 'v'},
|
||||||
{NULL, 0, NULL, 0 },
|
{NULL, 0, NULL, 0 },
|
||||||
};
|
};
|
||||||
int c;
|
int c;
|
||||||
while ((c = getopt_long(argc, argv, "b:c:fF:hm:nNp:r:s:tTv", long_options,
|
while ((c = getopt_long(argc, argv, "b:c:fF:hm:nNp:r:s:StTv", long_options,
|
||||||
NULL)) != -1) {
|
NULL)) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'b':
|
case 'b':
|
||||||
|
@ -347,6 +352,9 @@ parse_args(struct args *args, int argc, char *argv[]) {
|
||||||
case 's':
|
case 's':
|
||||||
args->serial = optarg;
|
args->serial = optarg;
|
||||||
break;
|
break;
|
||||||
|
case 'S':
|
||||||
|
args->turn_screen_off = true;
|
||||||
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
args->show_touches = true;
|
args->show_touches = true;
|
||||||
break;
|
break;
|
||||||
|
@ -417,6 +425,7 @@ main(int argc, char *argv[]) {
|
||||||
.always_on_top = false,
|
.always_on_top = false,
|
||||||
.no_control = false,
|
.no_control = false,
|
||||||
.no_display = false,
|
.no_display = false,
|
||||||
|
.turn_screen_off = false,
|
||||||
};
|
};
|
||||||
if (!parse_args(&args, argc, argv)) {
|
if (!parse_args(&args, argc, argv)) {
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -457,6 +466,7 @@ main(int argc, char *argv[]) {
|
||||||
.always_on_top = args.always_on_top,
|
.always_on_top = args.always_on_top,
|
||||||
.control = !args.no_control,
|
.control = !args.no_control,
|
||||||
.display = !args.no_display,
|
.display = !args.no_display,
|
||||||
|
.turn_screen_off = args.turn_screen_off,
|
||||||
};
|
};
|
||||||
int res = scrcpy(&options) ? 0 : 1;
|
int res = scrcpy(&options) ? 0 : 1;
|
||||||
|
|
||||||
|
|
|
@ -375,6 +375,16 @@ scrcpy(const struct scrcpy_options *options) {
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options->turn_screen_off) {
|
||||||
|
struct control_msg msg;
|
||||||
|
msg.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
|
||||||
|
msg.set_screen_power_mode.mode = SCREEN_POWER_MODE_OFF;
|
||||||
|
|
||||||
|
if (!controller_push_msg(&controller, &msg)) {
|
||||||
|
LOGW("Cannot request 'set screen power mode'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (options->fullscreen) {
|
if (options->fullscreen) {
|
||||||
screen_switch_fullscreen(&screen);
|
screen_switch_fullscreen(&screen);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ struct scrcpy_options {
|
||||||
bool always_on_top;
|
bool always_on_top;
|
||||||
bool control;
|
bool control;
|
||||||
bool display;
|
bool display;
|
||||||
|
bool turn_screen_off;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|
|
@ -53,9 +53,21 @@ public class Controller {
|
||||||
coords.setAxisValue(MotionEvent.AXIS_VSCROLL, vScroll);
|
coords.setAxisValue(MotionEvent.AXIS_VSCROLL, vScroll);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("checkstyle:MagicNumber")
|
||||||
public void control() throws IOException {
|
public void control() throws IOException {
|
||||||
// on start, power on the device
|
// on start, power on the device
|
||||||
powerOn();
|
if (!device.isScreenOn()) {
|
||||||
|
injectKeycode(KeyEvent.KEYCODE_POWER);
|
||||||
|
|
||||||
|
// dirty hack
|
||||||
|
// After POWER is injected, the device is powered on asynchronously.
|
||||||
|
// To turn the device screen off while mirroring, the client will send a message that
|
||||||
|
// would be handled before the device is actually powered on, so its effect would
|
||||||
|
// be "canceled" once the device is turned back on.
|
||||||
|
// Adding this delay prevents to handle the message before the device is actually
|
||||||
|
// powered on.
|
||||||
|
SystemClock.sleep(500);
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
handleEvent();
|
handleEvent();
|
||||||
|
@ -182,10 +194,6 @@ public class Controller {
|
||||||
return device.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
return device.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean powerOn() {
|
|
||||||
return device.isScreenOn() || injectKeycode(KeyEvent.KEYCODE_POWER);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean pressBackOrTurnScreenOn() {
|
private boolean pressBackOrTurnScreenOn() {
|
||||||
int keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_POWER;
|
int keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_POWER;
|
||||||
return injectKeycode(keycode);
|
return injectKeycode(keycode);
|
||||||
|
|
Loading…
Reference in a new issue