Allocate and format server command args

This simplifies formatting.
This commit is contained in:
Romain Vimont 2021-11-24 21:10:18 +01:00
parent 2c3099e2de
commit 2eb881c5f1

View file

@ -139,23 +139,17 @@ log_level_to_server_string(enum sc_log_level level) {
static sc_pid static sc_pid
execute_server(struct sc_server *server, execute_server(struct sc_server *server,
const struct sc_server_params *params) { const struct sc_server_params *params) {
char max_size_string[6]; sc_pid pid = SC_PROCESS_NONE;
char bit_rate_string[11];
char max_fps_string[6]; const char *cmd[128];
char lock_video_orientation_string[5]; unsigned count = 0;
char display_id_string[11]; cmd[count++] = "shell";
sprintf(max_size_string, "%"PRIu16, params->max_size); cmd[count++] = "CLASSPATH=" SC_DEVICE_SERVER_PATH;
sprintf(bit_rate_string, "%"PRIu32, params->bit_rate); cmd[count++] = "app_process";
sprintf(max_fps_string, "%"PRIu16, params->max_fps);
sprintf(lock_video_orientation_string, "%"PRIi8,
params->lock_video_orientation);
sprintf(display_id_string, "%"PRIu32, params->display_id);
const char *const cmd[] = {
"shell",
"CLASSPATH=" SC_DEVICE_SERVER_PATH,
"app_process",
#ifdef SERVER_DEBUGGER #ifdef SERVER_DEBUGGER
# define SERVER_DEBUGGER_PORT "5005" # define SERVER_DEBUGGER_PORT "5005"
cmd[count++] =
# ifdef SERVER_DEBUGGER_METHOD_NEW # ifdef SERVER_DEBUGGER_METHOD_NEW
/* Android 9 and above */ /* Android 9 and above */
"-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y," "-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y,"
@ -164,28 +158,43 @@ execute_server(struct sc_server *server,
/* Android 8 and below */ /* Android 8 and below */
"-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=" "-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address="
# endif # endif
SERVER_DEBUGGER_PORT, SERVER_DEBUGGER_PORT;
#endif #endif
"/", // unused cmd[count++] = "/"; // unused
"com.genymobile.scrcpy.Server", cmd[count++] = "com.genymobile.scrcpy.Server";
SCRCPY_VERSION, cmd[count++] = SCRCPY_VERSION;
log_level_to_server_string(params->log_level), cmd[count++] = log_level_to_server_string(params->log_level);
max_size_string,
bit_rate_string, unsigned dyn_idx = count; // from there, the strings are allocated
max_fps_string, #define ADD_PARAM(fmt, ...) { \
lock_video_orientation_string, char *p = (char *) &cmd[count]; \
server->tunnel.forward ? "true" : "false", if (asprintf(&p, fmt, ## __VA_ARGS__) == -1) { \
params->crop ? params->crop : "-", goto end; \
"true", // always send frame meta (packet boundaries + timestamp) } \
params->control ? "true" : "false", cmd[count++] = p; \
display_id_string, }
params->show_touches ? "true" : "false", #define STRBOOL(v) (v ? "true" : "false")
params->stay_awake ? "true" : "false",
params->codec_options ? params->codec_options : "-", ADD_PARAM("%" PRIu16, params->max_size);
params->encoder_name ? params->encoder_name : "-", ADD_PARAM("%" PRIu32, params->bit_rate);
params->power_off_on_close ? "true" : "false", ADD_PARAM("%" PRIu16, params->max_fps);
params->clipboard_autosync ? "true" : "false", ADD_PARAM("%" PRIi8, params->lock_video_orientation);
}; ADD_PARAM("%s", STRBOOL(server->tunnel.forward));
ADD_PARAM("%s", params->crop ? params->crop : "-");
// always send frame meta (packet boundaries + timestamp)
ADD_PARAM("true");
ADD_PARAM("%s", STRBOOL(params->control));
ADD_PARAM("%" PRIu32, params->display_id);
ADD_PARAM("%s", STRBOOL(params->show_touches));
ADD_PARAM("%s", STRBOOL(params->stay_awake));
ADD_PARAM("%s", params->codec_options ? params->codec_options : "-");
ADD_PARAM("%s", params->encoder_name ? params->encoder_name : "-");
ADD_PARAM("%s", STRBOOL(params->power_off_on_close));
ADD_PARAM("%s", STRBOOL(params->clipboard_autosync));
#undef ADD_PARAM
#undef STRBOOL
#ifdef SERVER_DEBUGGER #ifdef SERVER_DEBUGGER
LOGI("Server debugger waiting for a client on device port " LOGI("Server debugger waiting for a client on device port "
SERVER_DEBUGGER_PORT "..."); SERVER_DEBUGGER_PORT "...");
@ -197,7 +206,14 @@ execute_server(struct sc_server *server,
// Port: 5005 // Port: 5005
// Then click on "Debug" // Then click on "Debug"
#endif #endif
return adb_execute(params->serial, cmd, ARRAY_LEN(cmd)); pid = adb_execute(params->serial, cmd, count);
end:
for (unsigned i = dyn_idx; i < count; ++i) {
free((char *) cmd[i]);
}
return pid;
} }
static bool static bool