From 4ce7af42c635a6e9edd7c67902718d3a2c7f45fd Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 14 Mar 2022 12:29:33 +0100 Subject: [PATCH] Use $ANDROID_SERIAL if no selector is specified Like adb, read the ANDROID_SERIAL environment variable to select a device by serial if no explicit selection (-s, -d, -e or --tcpip=) is provided via the command line. Fixes #3111 PR #3113 --- README.md | 3 +++ app/scrcpy.1 | 4 ++++ app/src/cli.c | 5 +++++ app/src/server.c | 10 +++++++++- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c89b0c8d..614d1ed4 100644 --- a/README.md +++ b/README.md @@ -448,6 +448,9 @@ scrcpy --serial 0123456789abcdef scrcpy -s 0123456789abcdef # short version ``` +The serial may also be provided via the environment variable `ANDROID_SERIAL` +(also used by `adb`). + If the device is connected over TCP/IP: ```bash diff --git a/app/scrcpy.1 b/app/scrcpy.1 index f1ee732d..eb164475 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -477,6 +477,10 @@ Push file to device (see \fB\-\-push\-target\fR) .B ADB Path to adb. +.TP +.B ANDROID_SERIAL +Device serial to use if no selector (-s, -d, -e or --tcpip=) is specified. + .TP .B SCRCPY_ICON_PATH Path to the program icon. diff --git a/app/src/cli.c b/app/src/cli.c index bde5eb00..5dda86e5 100644 --- a/app/src/cli.c +++ b/app/src/cli.c @@ -660,6 +660,11 @@ static const struct sc_envvar envvars[] = { .name = "ADB", .text = "Path to adb executable", }, + { + .name = "ANDROID_SERIAL", + .text = "Device serial to use if no selector (-s, -d, -e or " + "--tcpip=) is specified", + }, { .name = "SCRCPY_ICON_PATH", .text = "Path to the program icon", diff --git a/app/src/server.c b/app/src/server.c index c12b03d4..6b61924b 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -707,7 +707,15 @@ run_server(void *data) { } else if (params->select_tcpip) { selector.type = SC_ADB_DEVICE_SELECT_TCPIP; } else { - selector.type = SC_ADB_DEVICE_SELECT_ALL; + // No explicit selection, check $ANDROID_SERIAL + const char *env_serial = getenv("ANDROID_SERIAL"); + if (env_serial) { + LOGI("Using ANDROID_SERIAL: %s", env_serial); + selector.type = SC_ADB_DEVICE_SELECT_SERIAL; + selector.serial = env_serial; + } else { + selector.type = SC_ADB_DEVICE_SELECT_ALL; + } } struct sc_adb_device device; ok = sc_adb_select_device(&server->intr, &selector, 0, &device);