Allocate and format server command args

This paves the way to name all parameters.
This commit is contained in:
Romain Vimont 2020-05-06 00:19:10 +02:00
parent a0d98fe4ae
commit a845ea0794

View File

@ -231,22 +231,17 @@ enable_tunnel_any_port(struct server *server, struct port_range port_range) {
static process_t static process_t
execute_server(struct server *server, const struct server_params *params) { execute_server(struct server *server, const struct server_params *params) {
char max_size_string[6]; process_t result = PROCESS_NONE;
char bit_rate_string[11];
char max_fps_string[6]; char *cmd[128];
char lock_video_orientation_string[3]; int i = 0;
char display_id_string[6]; cmd[i++] = "shell";
sprintf(max_size_string, "%"PRIu16, params->max_size); cmd[i++] = "CLASSPATH=" DEVICE_SERVER_PATH;
sprintf(bit_rate_string, "%"PRIu32, params->bit_rate); cmd[i++] = "app_process";
sprintf(max_fps_string, "%"PRIu16, params->max_fps);
sprintf(lock_video_orientation_string, "%"PRIi8, params->lock_video_orientation);
sprintf(display_id_string, "%"PRIu16, params->display_id);
const char *const cmd[] = {
"shell",
"CLASSPATH=" DEVICE_SERVER_PATH,
"app_process",
#ifdef SERVER_DEBUGGER #ifdef SERVER_DEBUGGER
# define SERVER_DEBUGGER_PORT "5005" # define SERVER_DEBUGGER_PORT "5005"
cmd[i++] =
# 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,server=y,address=" "-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y,server=y,address="
@ -254,23 +249,37 @@ execute_server(struct server *server, const struct server_params *params) {
/* 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
"com.genymobile.scrcpy.Server", cmd[i++] = "/"; // unused
SCRCPY_VERSION, cmd[i++] = "com.genymobile.scrcpy.Server";
max_size_string, cmd[i++] = SCRCPY_VERSION;
bit_rate_string,
max_fps_string, int dyn_index = i; // from there, the strings are allocated
lock_video_orientation_string, #define ADD_PARAM(fmt, ...) \
server->tunnel_forward ? "true" : "false", cmd[i] = sc_asprintf(fmt, ## __VA_ARGS__); \
params->crop ? params->crop : "-", if (!cmd[i++]) { \
"true", // always send frame meta (packet boundaries + timestamp) goto end; \
params->control ? "true" : "false", }
display_id_string,
params->show_touches ? "true" : "false", #define STRBOOL(p) (p ? "true" : "false")
params->stay_awake ? "true" : "false",
}; ADD_PARAM("%"PRIu16, params->max_size);
ADD_PARAM("%"PRIu32, params->bit_rate);
ADD_PARAM("%"PRIu16, params->max_fps);
ADD_PARAM("%"PRIi8, params->lock_video_orientation);
ADD_PARAM("%s", STRBOOL(server->tunnel_forward));
ADD_PARAM("%s", params->crop ? params->crop : "-");
ADD_PARAM("true"); // always send frame meta (packet boundaries + timestamp)
ADD_PARAM("%s", STRBOOL(params->control));
ADD_PARAM("%"PRIu16, params->display_id);
ADD_PARAM("%s", STRBOOL(params->show_touches));
ADD_PARAM("%s", STRBOOL(params->stay_awake));
#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 "...");
@ -282,7 +291,14 @@ execute_server(struct server *server, const struct server_params *params) {
// Port: 5005 // Port: 5005
// Then click on "Debug" // Then click on "Debug"
#endif #endif
return adb_execute(server->serial, cmd, sizeof(cmd) / sizeof(cmd[0])); result = adb_execute(server->serial, (const char **) cmd, i);
end:
for (int j = i; j > dyn_index; --j) {
free(cmd[j - 1]);
}
return result;
} }
static socket_t static socket_t