Compare commits

...

3 Commits

Author SHA1 Message Date
Romain Vimont
870ced088e Name server parameters
The options values to configure the server were identified by their
command-line argument index.

Now that there are a lot of arguments, many of them being booleans, it
became unreadable and error-prone.

Identify the arguments by a key string instead.
2020-05-06 00:40:33 +02:00
Romain Vimont
a845ea0794 Allocate and format server command args
This paves the way to name all parameters.
2020-05-06 00:39:37 +02:00
Romain Vimont
a0d98fe4ae Add asprintf() compat function
Add sc_asprintf() similar to asprintf() which is not available
everywhere.
2020-05-06 00:18:52 +02:00
5 changed files with 151 additions and 70 deletions

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,38 @@ 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("max_size=%"PRIu16, params->max_size);
ADD_PARAM("bit_rate=%"PRIu32, params->bit_rate);
ADD_PARAM("max_fps=%"PRIu16, params->max_fps);
ADD_PARAM("lock_video_orientation=%"PRIi8, params->lock_video_orientation);
ADD_PARAM("tunnel_forward=%s", STRBOOL(server->tunnel_forward));
ADD_PARAM("crop=%s", params->crop ? params->crop : "");
// always send frame meta (packet boundaries + timestamp)
ADD_PARAM("send_frame_meta=true");
ADD_PARAM("control=%s", STRBOOL(params->control));
ADD_PARAM("display_id=%"PRIu16, params->display_id);
ADD_PARAM("show_touches=%s", STRBOOL(params->show_touches));
ADD_PARAM("stay_awake=%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 +292,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

View File

@ -1,7 +1,9 @@
#include "str_util.h" #include "str_util.h"
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -195,3 +197,33 @@ utf8_from_wide_char(const wchar_t *ws) {
} }
#endif #endif
char *
sc_asprintf(const char *fmt, ...) {
va_list va;
va_start(va, fmt);
char *s = sc_vasprintf(fmt, va);
va_end(va);
return s;
}
char *
sc_vasprintf(const char *fmt, va_list ap) {
va_list va;
va_copy(va, ap);
int len = vsnprintf(NULL, 0, fmt, va);
va_end(va);
char *str = malloc(len + 1);
if (!str) {
return NULL;
}
va_copy(va, ap);
int len2 = vsprintf(str, fmt, va);
(void) len2;
assert(len == len2);
va_end(va);
return str;
}

View File

@ -1,8 +1,10 @@
#ifndef STRUTIL_H #ifndef STRUTIL_H
#define STRUTIL_H #define STRUTIL_H
#include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h>
#include "config.h" #include "config.h"
@ -57,4 +59,13 @@ char *
utf8_from_wide_char(const wchar_t *s); utf8_from_wide_char(const wchar_t *s);
#endif #endif
// compatibility function similar to asprintf()
// (but returning the resulting string for convenience)
char *
sc_asprintf(const char *fmt, ...)
__attribute__((format(printf, 1, 2)));
char *
sc_vasprintf(const char *fmt, va_list ap);
#endif #endif

View File

@ -6,7 +6,7 @@ public class Options {
private int maxSize; private int maxSize;
private int bitRate; private int bitRate;
private int maxFps; private int maxFps;
private int lockedVideoOrientation; private int lockedVideoOrientation = -1;
private boolean tunnelForward; private boolean tunnelForward;
private Rect crop; private Rect crop;
private boolean sendFrameMeta; // send PTS so that the client may record properly private boolean sendFrameMeta; // send PTS so that the client may record properly

View File

@ -109,52 +109,73 @@ public final class Server {
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")"); "The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
} }
final int expectedParameters = 12;
if (args.length != expectedParameters) {
throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters");
}
Options options = new Options(); Options options = new Options();
int maxSize = Integer.parseInt(args[1]) & ~7; // multiple of 8 for (int i = 1; i < args.length; ++i) {
options.setMaxSize(maxSize); String arg = args[i];
int equalIndex = arg.indexOf('=');
if (equalIndex == -1) {
throw new IllegalArgumentException("Invalid key=value pair: \"" + arg + "\"");
}
String key = arg.substring(0, equalIndex);
String value = arg.substring(equalIndex + 1);
int bitRate = Integer.parseInt(args[2]); switch (key) {
options.setBitRate(bitRate); case "max_size":
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
int maxFps = Integer.parseInt(args[3]); options.setMaxSize(maxSize);
options.setMaxFps(maxFps); break;
case "bit_rate":
int lockedVideoOrientation = Integer.parseInt(args[4]); int bitRate = Integer.parseInt(value);
options.setLockedVideoOrientation(lockedVideoOrientation); options.setBitRate(bitRate);
break;
// use "adb forward" instead of "adb tunnel"? (so the server must listen) case "max_fps":
boolean tunnelForward = Boolean.parseBoolean(args[5]); int maxFps = Integer.parseInt(value);
options.setTunnelForward(tunnelForward); options.setMaxFps(maxFps);
break;
Rect crop = parseCrop(args[6]); case "lock_video_orientation":
options.setCrop(crop); int lockedVideoOrientation = Integer.parseInt(value);
options.setLockedVideoOrientation(lockedVideoOrientation);
boolean sendFrameMeta = Boolean.parseBoolean(args[7]); break;
options.setSendFrameMeta(sendFrameMeta); case "tunnel_forward":
// use "adb forward" instead of "adb tunnel"? (so the server must listen)
boolean control = Boolean.parseBoolean(args[8]); boolean tunnelForward = Boolean.parseBoolean(value);
options.setControl(control); options.setTunnelForward(tunnelForward);
break;
int displayId = Integer.parseInt(args[9]); case "crop":
options.setDisplayId(displayId); Rect crop = parseCrop(value);
options.setCrop(crop);
boolean showTouches = Boolean.parseBoolean(args[10]); break;
options.setShowTouches(showTouches); case "send_frame_meta":
boolean sendFrameMeta = Boolean.parseBoolean(value);
boolean stayAwake = Boolean.parseBoolean(args[11]); options.setSendFrameMeta(sendFrameMeta);
options.setStayAwake(stayAwake); break;
case "control":
boolean control = Boolean.parseBoolean(value);
options.setControl(control);
break;
case "display_id":
int displayId = Integer.parseInt(value);
options.setDisplayId(displayId);
break;
case "show_touches":
boolean showTouches = Boolean.parseBoolean(value);
options.setShowTouches(showTouches);
break;
case "stay_awake":
boolean stayAwake = Boolean.parseBoolean(value);
options.setStayAwake(stayAwake);
break;
default:
throw new IllegalArgumentException("Unknown parameter: " + key);
}
}
return options; return options;
} }
private static Rect parseCrop(String crop) { private static Rect parseCrop(String crop) {
if ("-".equals(crop)) { if (crop.isEmpty()) {
return null; return null;
} }
// input format: "width:height:x:y" // input format: "width:height:x:y"