Skip to content

Commit

Permalink
Turn screen off when closing scrcpy
Browse files Browse the repository at this point in the history
Signed-off-by: Yu-Chen Lin <[email protected]>
  • Loading branch information
npes87184 committed Sep 29, 2019
1 parent 795d103 commit da7a2c2
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/src/control_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
case CONTROL_MSG_TYPE_COLLAPSE_NOTIFICATION_PANEL:
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
case CONTROL_MSG_TYPE_LOCK_SCREEN:
// no additional data
return 1;
default:
Expand Down
1 change: 1 addition & 0 deletions app/src/control_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum control_msg_type {
CONTROL_MSG_TYPE_GET_CLIPBOARD,
CONTROL_MSG_TYPE_SET_CLIPBOARD,
CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
CONTROL_MSG_TYPE_LOCK_SCREEN,
};

enum screen_power_mode {
Expand Down
54 changes: 33 additions & 21 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct args {
bool always_on_top;
bool turn_screen_off;
bool render_expired_frames;
bool lock_screen_when_closing;
};

static void usage(const char *arg0) {
Expand Down Expand Up @@ -118,6 +119,9 @@ static void usage(const char *arg0) {
" --window-title text\n"
" Set a custom window title.\n"
"\n"
" --lock-screen-when-closing\n"
" Lock device screen when closing scrcpy.\n"
"\n"
"Shortcuts:\n"
"\n"
" " CTRL_OR_CMD "+f\n"
Expand Down Expand Up @@ -309,34 +313,37 @@ guess_record_format(const char *filename) {
return 0;
}

#define OPT_RENDER_EXPIRED_FRAMES 1000
#define OPT_WINDOW_TITLE 1001
#define OPT_PUSH_TARGET 1002
#define OPT_RENDER_EXPIRED_FRAMES 1000
#define OPT_WINDOW_TITLE 1001
#define OPT_PUSH_TARGET 1002
#define OPT_LOCK_SCREEN_WHEN_CLOSING 1003

static bool
parse_args(struct args *args, int argc, char *argv[]) {
static const struct option long_options[] = {
{"always-on-top", no_argument, NULL, 'T'},
{"bit-rate", required_argument, NULL, 'b'},
{"crop", required_argument, NULL, 'c'},
{"fullscreen", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"max-size", required_argument, NULL, 'm'},
{"no-control", no_argument, NULL, 'n'},
{"no-display", no_argument, NULL, 'N'},
{"port", required_argument, NULL, 'p'},
{"push-target", required_argument, NULL,
{"always-on-top", no_argument, NULL, 'T'},
{"bit-rate", required_argument, NULL, 'b'},
{"crop", required_argument, NULL, 'c'},
{"fullscreen", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"max-size", required_argument, NULL, 'm'},
{"no-control", no_argument, NULL, 'n'},
{"no-display", no_argument, NULL, 'N'},
{"port", required_argument, NULL, 'p'},
{"push-target", required_argument, NULL,
OPT_PUSH_TARGET},
{"record", required_argument, NULL, 'r'},
{"record-format", required_argument, NULL, 'F'},
{"render-expired-frames", no_argument, NULL,
{"record", required_argument, NULL, 'r'},
{"record-format", required_argument, NULL, 'F'},
{"render-expired-frames", no_argument, NULL,
OPT_RENDER_EXPIRED_FRAMES},
{"serial", required_argument, NULL, 's'},
{"show-touches", no_argument, NULL, 't'},
{"turn-screen-off", no_argument, NULL, 'S'},
{"version", no_argument, NULL, 'v'},
{"window-title", required_argument, NULL,
{"serial", required_argument, NULL, 's'},
{"show-touches", no_argument, NULL, 't'},
{"turn-screen-off", no_argument, NULL, 'S'},
{"version", no_argument, NULL, 'v'},
{"window-title", required_argument, NULL,
OPT_WINDOW_TITLE},
{"lock-screen-when-closing", no_argument, NULL,
OPT_LOCK_SCREEN_WHEN_CLOSING},
{NULL, 0, NULL, 0 },
};
int c;
Expand Down Expand Up @@ -405,6 +412,9 @@ parse_args(struct args *args, int argc, char *argv[]) {
case OPT_PUSH_TARGET:
args->push_target = optarg;
break;
case OPT_LOCK_SCREEN_WHEN_CLOSING:
args->lock_screen_when_closing = true;
break;
default:
// getopt prints the error message on stderr
return false;
Expand Down Expand Up @@ -475,6 +485,7 @@ main(int argc, char *argv[]) {
.no_display = false,
.turn_screen_off = false,
.render_expired_frames = false,
.lock_screen_when_closing = false,
};
if (!parse_args(&args, argc, argv)) {
return 1;
Expand Down Expand Up @@ -521,6 +532,7 @@ main(int argc, char *argv[]) {
.display = !args.no_display,
.turn_screen_off = args.turn_screen_off,
.render_expired_frames = args.render_expired_frames,
.lock_screen_when_closing = args.lock_screen_when_closing,
};
int res = scrcpy(&options) ? 0 : 1;

Expand Down
14 changes: 14 additions & 0 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ wait_show_touches(process_t process) {
process_check_success(process, "show_touches");
}

static void
lock_screen(struct controller *controller) {
struct control_msg msg;
msg.type = CONTROL_MSG_TYPE_LOCK_SCREEN;

if (!controller_push_msg(controller, &msg)) {
LOGW("Could not request 'turn screen off'");
}
}

static SDL_LogPriority
sdl_priority_from_av_level(int level) {
switch (level) {
Expand Down Expand Up @@ -411,6 +421,10 @@ scrcpy(const struct scrcpy_options *options) {
ret = event_loop(options->display, options->control);
LOGD("quit...");

if (options->lock_screen_when_closing) {
lock_screen(&controller);
}

screen_destroy(&screen);

end:
Expand Down
1 change: 1 addition & 0 deletions app/src/scrcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct scrcpy_options {
bool display;
bool turn_screen_off;
bool render_expired_frames;
bool lock_screen_when_closing;
};

bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class ControlMessage {
public static final int TYPE_GET_CLIPBOARD = 7;
public static final int TYPE_SET_CLIPBOARD = 8;
public static final int TYPE_SET_SCREEN_POWER_MODE = 9;
public static final int TYPE_LOCK_SCREEN = 10;

private int type;
private String text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public ControlMessage next() {
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
case ControlMessage.TYPE_COLLAPSE_NOTIFICATION_PANEL:
case ControlMessage.TYPE_GET_CLIPBOARD:
case ControlMessage.TYPE_LOCK_SCREEN:
msg = ControlMessage.createEmpty(type);
break;
default:
Expand Down
11 changes: 11 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ private void handleEvent() throws IOException {
case ControlMessage.TYPE_BACK_OR_SCREEN_ON:
pressBackOrTurnScreenOn();
break;
case ControlMessage.TYPE_LOCK_SCREEN:
lockScreen();
break;
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
device.expandNotificationPanel();
break;
Expand Down Expand Up @@ -198,4 +201,12 @@ private boolean pressBackOrTurnScreenOn() {
int keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_POWER;
return injectKeycode(keycode);
}

private boolean lockScreen() {
if (!device.isScreenOn()) {
return true;
}
int keycode = KeyEvent.KEYCODE_POWER;
return injectKeycode(keycode);
}
}

0 comments on commit da7a2c2

Please sign in to comment.