Display and control your Android device
Add option to select USB or TCP/IP devices
Romain Vimont582161607e8c
diff --git a/app/scrcpy.1 b/app/scrcpy.1index 0f618cb4..317cec05 100644--- a/app/scrcpy.1+++ b/app/scrcpy.1@@ -43,6 +43,12 @@ The values are expressed in the device natural orientation (typically, portrait.B \-\-max\-sizevalue is computed on the cropped size.+.TP+.B \-d, \-\-select\-usb+Use USB device (if there is exactly one, like adb -d).++Also see \fB\-e\fR (\fB\-\-select\-tcpip\fR).+.TP.BI "\-\-disable-screensaver"Disable screensaver while scrcpy is running.@@ -62,6 +68,12 @@ Add a buffering delay (in milliseconds) before displaying. This increases latencDefault is 0 (no buffering).+.TP+.B \-e, \-\-select\-tcpip+Use TCP/IP device (if there is exactly one, like adb -e).++Also see \fB\-d\fR (\fB\-\-select\-usb\fR).+.TP.BI "\-\-encoder " nameUse a specific MediaCodec encoder (must be a H.264 encoder).diff --git a/app/src/adb/adb.c b/app/src/adb/adb.cindex 2bfa9f1e..e415eb4f 100644--- a/app/src/adb/adb.c+++ b/app/src/adb/adb.c@@ -451,6 +451,10 @@ sc_adb_accept_device(const struct sc_adb_device *device,}}return !strcmp(selector->serial, device->serial);+ case SC_ADB_DEVICE_SELECT_USB:+ return !sc_adb_is_serial_tcpip(device->serial);+ case SC_ADB_DEVICE_SELECT_TCPIP:+ return sc_adb_is_serial_tcpip(device->serial);default:assert(!"Missing SC_ADB_DEVICE_SELECT_* handling");break;@@ -542,6 +546,12 @@ sc_adb_select_device(struct sc_intr *intr,assert(selector->serial);LOGE("Could not find ADB device %s:", selector->serial);break;+ case SC_ADB_DEVICE_SELECT_USB:+ LOGE("Could not find any ADB device over USB:");+ break;+ case SC_ADB_DEVICE_SELECT_TCPIP:+ LOGE("Could not find any ADB device over TCP/IP:");+ break;default:assert(!"Unexpected selector type");break;@@ -562,12 +572,21 @@ sc_adb_select_device(struct sc_intr *intr,LOGE("Multiple (%" SC_PRIsizet ") ADB devices with serial %s:",sel_count, selector->serial);break;+ case SC_ADB_DEVICE_SELECT_USB:+ LOGE("Multiple (%" SC_PRIsizet ") ADB devices over USB:",+ sel_count);+ break;+ case SC_ADB_DEVICE_SELECT_TCPIP:+ LOGE("Multiple (%" SC_PRIsizet ") ADB devices over TCP/IP:",+ sel_count);+ break;default:assert(!"Unexpected selector type");break;}sc_adb_devices_log(SC_LOG_LEVEL_ERROR, devices, count);- LOGE("Select a device via -s (--serial)");+ LOGE("Select a device via -s (--serial), -d (--select-usb) or -e "+ "(--select-tcpip)");sc_adb_devices_destroy_all(devices, count);return false;}diff --git a/app/src/adb/adb.h b/app/src/adb/adb.hindex dc302a85..6ea6e897 100644--- a/app/src/adb/adb.h+++ b/app/src/adb/adb.h@@ -21,6 +21,8 @@ sc_adb_get_executable(void);enum sc_adb_device_selector_type {SC_ADB_DEVICE_SELECT_ALL,SC_ADB_DEVICE_SELECT_SERIAL,+ SC_ADB_DEVICE_SELECT_USB,+ SC_ADB_DEVICE_SELECT_TCPIP,};struct sc_adb_device_selector {diff --git a/app/src/cli.c b/app/src/cli.cindex 7567a10b..6e58feff 100644--- a/app/src/cli.c+++ b/app/src/cli.c@@ -118,6 +118,12 @@ static const struct sc_option options[] = {"(typically, portrait for a phone, landscape for a tablet). ""Any --max-size value is cmoputed on the cropped size.",},+ {+ .shortopt = 'd',+ .longopt = "select-usb",+ .text = "Use USB device (if there is exactly one, like adb -d).\n"+ "Also see -e (--select-tcpip).",+ },{.longopt_id = OPT_DISABLE_SCREENSAVER,.longopt = "disable-screensaver",@@ -141,6 +147,12 @@ static const struct sc_option options[] = {"This increases latency to compensate for jitter.\n""Default is 0 (no buffering).",},+ {+ .shortopt = 'e',+ .longopt = "select-tcpip",+ .text = "Use TCP/IP device (if there is exactly one, like adb -e).\n"+ "Also see -d (--select-usb).",+ },{.longopt_id = OPT_ENCODER_NAME,.longopt = "encoder",@@ -1320,6 +1332,12 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],return false;}break;+ case 'd':+ opts->select_usb = true;+ break;+ case 'e':+ opts->select_tcpip = true;+ break;case 'f':opts->fullscreen = true;break;@@ -1559,8 +1577,16 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],// If a TCP/IP address is provided, then tcpip must be enabledassert(opts->tcpip || !opts->tcpip_dst);- if (opts->serial && opts->tcpip_dst) {- LOGE("Incompatible options: -s/--serial and --tcpip with an argument");+ unsigned selectors = !!opts->serial+ + !!opts->tcpip_dst+ + opts->select_tcpip+ + opts->select_usb;+ if (selectors > 1) {+ LOGE("At most one device selector option may be passed, among:\n"+ " --serial (-s)\n"+ " --select-usb (-d)\n"+ " --select-tcpip (-e)\n"+ " --tcpip=<addr> (with an argument)");return false;}diff --git a/app/src/options.c b/app/src/options.cindex c94f798d..377c5590 100644--- a/app/src/options.c+++ b/app/src/options.c@@ -60,4 +60,6 @@ const struct scrcpy_options scrcpy_options_default = {.downsize_on_error = true,.tcpip = false,.tcpip_dst = NULL,+ .select_tcpip = false,+ .select_usb = false,};diff --git a/app/src/options.h b/app/src/options.hindex 1591a065..44104734 100644--- a/app/src/options.h+++ b/app/src/options.h@@ -135,6 +135,8 @@ struct scrcpy_options {bool downsize_on_error;bool tcpip;const char *tcpip_dst;+ bool select_usb;+ bool select_tcpip;};extern const struct scrcpy_options scrcpy_options_default;diff --git a/app/src/scrcpy.c b/app/src/scrcpy.cindex d0aa2627..c3a481d0 100644--- a/app/src/scrcpy.c+++ b/app/src/scrcpy.c@@ -297,6 +297,8 @@ scrcpy(struct scrcpy_options *options) {struct sc_server_params params = {.req_serial = options->serial,+ .select_usb = options->select_usb,+ .select_tcpip = options->select_tcpip,.log_level = options->log_level,.crop = options->crop,.port_range = options->port_range,diff --git a/app/src/server.c b/app/src/server.cindex c0c33a2c..0de51c7e 100644--- a/app/src/server.c+++ b/app/src/server.c@@ -687,10 +687,19 @@ run_server(void *data) {bool need_initial_serial = !params->tcpip_dst;if (need_initial_serial) {+ // At most one of the 3 following parameters may be set+ assert(!!params->req_serial+ + params->select_usb+ + params->select_tcpip <= 1);+struct sc_adb_device_selector selector;if (params->req_serial) {selector.type = SC_ADB_DEVICE_SELECT_SERIAL;selector.serial = params->req_serial;+ } else if (params->select_usb) {+ selector.type = SC_ADB_DEVICE_SELECT_USB;+ } else if (params->select_tcpip) {+ selector.type = SC_ADB_DEVICE_SELECT_TCPIP;} else {selector.type = SC_ADB_DEVICE_SELECT_ALL;}diff --git a/app/src/server.h b/app/src/server.hindex c2293118..5818b43c 100644--- a/app/src/server.h+++ b/app/src/server.h@@ -44,6 +44,8 @@ struct sc_server_params {bool downsize_on_error;bool tcpip;const char *tcpip_dst;+ bool select_usb;+ bool select_tcpip;};struct sc_server {