From a3cdf1a6b86ea22786e1f7d09b9c202feabc6949 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 5 Jun 2023 19:48:21 +0200 Subject: [PATCH] Add option to kill adb on close Killing adb on close by default would be incorrect, since it would break any other usage of adb in parallel. It could be easily done manually by calling "adb kill-server" once scrcpy terminates, but add an option --kill-adb-on-close for convenience. Fixes #205 Fixes #2580 Fixes #4049 --- app/data/bash-completion/scrcpy | 1 + app/data/zsh-completion/_scrcpy | 1 + app/scrcpy.1 | 4 ++++ app/src/cli.c | 9 +++++++++ app/src/options.c | 1 + app/src/options.h | 1 + app/src/scrcpy.c | 1 + app/src/server.c | 12 ++++++++++++ app/src/server.h | 1 + 9 files changed, 31 insertions(+) diff --git a/app/data/bash-completion/scrcpy b/app/data/bash-completion/scrcpy index a44d38f5..a34a1a44 100644 --- a/app/data/bash-completion/scrcpy +++ b/app/data/bash-completion/scrcpy @@ -20,6 +20,7 @@ _scrcpy() { --force-adb-forward --forward-all-clicks -h --help + --kill-adb-on-close -K --hid-keyboard --legacy-paste --list-displays diff --git a/app/data/zsh-completion/_scrcpy b/app/data/zsh-completion/_scrcpy index 8d3426dc..325ccb76 100644 --- a/app/data/zsh-completion/_scrcpy +++ b/app/data/zsh-completion/_scrcpy @@ -27,6 +27,7 @@ arguments=( '--force-adb-forward[Do not attempt to use \"adb reverse\" to connect to the device]' '--forward-all-clicks[Forward clicks to device]' {-h,--help}'[Print the help]' + '--kill-adb-on-close[Kill adb when scrcpy terminates]' {-K,--hid-keyboard}'[Simulate a physical keyboard by using HID over AOAv2]' '--legacy-paste[Inject computer clipboard text as a sequence of key events on Ctrl+v]' '--list-displays[List displays available on the device]' diff --git a/app/scrcpy.1 b/app/scrcpy.1 index 9b08f182..21c3ac8f 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -129,6 +129,10 @@ By default, right-click triggers BACK (or POWER on) and middle-click triggers HO .B \-h, \-\-help Print this help. +.TP +.B \-\-kill\-adb\-on\-close +Kill adb when scrcpy terminates. + .TP .B \-K, \-\-hid\-keyboard Simulate a physical keyboard by using HID over AOAv2. diff --git a/app/src/cli.c b/app/src/cli.c index 318a4230..c9b818a1 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -77,6 +77,7 @@ enum { OPT_NO_AUDIO_PLAYBACK, OPT_NO_VIDEO_PLAYBACK, OPT_AUDIO_SOURCE, + OPT_KILL_ADB_ON_CLOSE, }; struct sc_option { @@ -280,6 +281,11 @@ static const struct sc_option options[] = { .longopt = "help", .text = "Print this help.", }, + { + .longopt_id = OPT_KILL_ADB_ON_CLOSE, + .longopt = "kill-adb-on-close", + .text = "Kill adb when scrcpy terminates.", + }, { .shortopt = 'K', .longopt = "hid-keyboard", @@ -1944,6 +1950,9 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[], return false; } break; + case OPT_KILL_ADB_ON_CLOSE: + opts->kill_adb_on_close = true; + break; default: // getopt prints the error message on stderr return false; diff --git a/app/src/options.c b/app/src/options.c index e1373753..30b5cb56 100644 --- a/app/src/options.c +++ b/app/src/options.c @@ -80,4 +80,5 @@ const struct scrcpy_options scrcpy_options_default = { .require_audio = false, .list_encoders = false, .list_displays = false, + .kill_adb_on_close = false, }; diff --git a/app/src/options.h b/app/src/options.h index c33fafef..75f193b3 100644 --- a/app/src/options.h +++ b/app/src/options.h @@ -180,6 +180,7 @@ struct scrcpy_options { bool require_audio; bool list_encoders; bool list_displays; + bool kill_adb_on_close; }; extern const struct scrcpy_options scrcpy_options_default; diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 79007bf2..f9679ac1 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -364,6 +364,7 @@ scrcpy(struct scrcpy_options *options) { .power_on = options->power_on, .list_encoders = options->list_encoders, .list_displays = options->list_displays, + .kill_adb_on_close = options->kill_adb_on_close, }; static const struct sc_server_callbacks cbs = { diff --git a/app/src/server.c b/app/src/server.c index 2c0779d9..360e7e7c 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -794,6 +794,15 @@ sc_server_configure_tcpip_unknown_address(struct sc_server *server, return sc_server_connect_to_tcpip(server, ip_port); } +static void +sc_server_kill_adb_if_requested(struct sc_server *server) { + if (server->params.kill_adb_on_close) { + LOGI("Killing adb server..."); + unsigned flags = SC_ADB_NO_STDOUT | SC_ADB_NO_STDERR | SC_ADB_NO_LOGERR; + sc_adb_kill_server(&server->intr, flags); + } +} + static int run_server(void *data) { struct sc_server *server = data; @@ -993,9 +1002,12 @@ run_server(void *data) { sc_process_close(pid); + sc_server_kill_adb_if_requested(server); + return 0; error_connection_failed: + sc_server_kill_adb_if_requested(server); server->cbs->on_connection_failed(server, server->cbs_userdata); return -1; } diff --git a/app/src/server.h b/app/src/server.h index fad44e66..adba2652 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -58,6 +58,7 @@ struct sc_server_params { bool power_on; bool list_encoders; bool list_displays; + bool kill_adb_on_close; }; struct sc_server {