From 798727aa58a472a492895641546d099d7e8cf81b Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 21 Nov 2023 15:00:42 +0100 Subject: [PATCH] Use a different server path for each session When scrcpy is run, a server is pushed to /data/local/tmp/scrcpy-server.jar. Running simultaneous scrcpy instances on the same device was not a problem, because the file was unlinked (removed) almost immediately once it started, avoiding any conflicts. In order to support executing new process using the scrcpy-server.jar at any time (to change the display power mode from a separate process), the server file must not be unlinked, so using different names are necessary to avoid conflicts. Reuse the scid mechanism already used for generating device socket names. Refs 4315be164823d2c8fc44b475b52af79bfee98ff1 Refs 439a1fd4ed3d7ada4c5098a6d52893dd4e87082a --- app/src/server.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/app/src/server.c b/app/src/server.c index d4726c2a..7efdb48f 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -18,7 +18,6 @@ #define SC_SERVER_FILENAME "scrcpy-server" #define SC_SERVER_PATH_DEFAULT PREFIX "/share/scrcpy/" SC_SERVER_FILENAME -#define SC_DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar" #define SC_ADB_PORT_DEFAULT 5555 #define SC_SOCKET_NAME_PREFIX "scrcpy_" @@ -117,7 +116,7 @@ error: } static bool -push_server(struct sc_intr *intr, const char *serial) { +push_server(struct sc_intr *intr, uint32_t scid, const char *serial) { char *server_path = get_server_path(); if (!server_path) { return false; @@ -127,7 +126,16 @@ push_server(struct sc_intr *intr, const char *serial) { free(server_path); return false; } - bool ok = sc_adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH, 0); + + char *device_server_path; + if (asprintf(&device_server_path, "/data/local/tmp/scrcpy-server-%08x.jar", + scid) == -1) { + LOG_OOM(); + free(server_path); + return false; + } + bool ok = sc_adb_push(intr, serial, server_path, device_server_path, 0); + free(device_server_path); free(server_path); return ok; } @@ -209,13 +217,20 @@ execute_server(struct sc_server *server, const char *serial = server->serial; assert(serial); + char *classpath; + if (asprintf(&classpath, "CLASSPATH=/data/local/tmp/scrcpy-server-%08x.jar", + params->scid) == -1) { + LOG_OOM(); + return SC_PROCESS_NONE; + } + const char *cmd[128]; unsigned count = 0; cmd[count++] = sc_adb_get_executable(); cmd[count++] = "-s"; cmd[count++] = serial; cmd[count++] = "shell"; - cmd[count++] = "CLASSPATH=" SC_DEVICE_SERVER_PATH; + cmd[count++] = classpath; cmd[count++] = "app_process"; #ifdef SERVER_DEBUGGER @@ -388,6 +403,7 @@ end: for (unsigned i = dyn_idx; i < count; ++i) { free((char *) cmd[i]); } + free(classpath); return pid; } @@ -937,7 +953,7 @@ run_server(void *data) { assert(serial); LOGD("Device serial: %s", serial); - ok = push_server(&server->intr, serial); + ok = push_server(&server->intr, params->scid, serial); if (!ok) { goto error_connection_failed; }