Skip to content

Commit 04e5537

Browse files
committed
Pass server parameters as key=value pairs
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, and make them optional. This will also simplify running the server manually for debugging.
1 parent 2eb881c commit 04e5537

File tree

4 files changed

+105
-77
lines changed

4 files changed

+105
-77
lines changed

app/src/server.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ execute_server(struct sc_server *server,
163163
cmd[count++] = "/"; // unused
164164
cmd[count++] = "com.genymobile.scrcpy.Server";
165165
cmd[count++] = SCRCPY_VERSION;
166-
cmd[count++] = log_level_to_server_string(params->log_level);
167166

168167
unsigned dyn_idx = count; // from there, the strings are allocated
169168
#define ADD_PARAM(fmt, ...) { \
@@ -175,22 +174,25 @@ execute_server(struct sc_server *server,
175174
}
176175
#define STRBOOL(v) (v ? "true" : "false")
177176

178-
ADD_PARAM("%" PRIu16, params->max_size);
179-
ADD_PARAM("%" PRIu32, params->bit_rate);
180-
ADD_PARAM("%" PRIu16, params->max_fps);
181-
ADD_PARAM("%" PRIi8, params->lock_video_orientation);
182-
ADD_PARAM("%s", STRBOOL(server->tunnel.forward));
183-
ADD_PARAM("%s", params->crop ? params->crop : "-");
177+
ADD_PARAM("log_level=%s", log_level_to_server_string(params->log_level));
178+
ADD_PARAM("max_size=%" PRIu16, params->max_size);
179+
ADD_PARAM("bit_rate=%" PRIu32, params->bit_rate);
180+
ADD_PARAM("max_fps=%" PRIu16, params->max_fps);
181+
ADD_PARAM("lock_video_orientation=%" PRIi8, params->lock_video_orientation);
182+
ADD_PARAM("tunnel_forward=%s", STRBOOL(server->tunnel.forward));
183+
ADD_PARAM("crop=%s", params->crop ? params->crop : "");
184184
// always send frame meta (packet boundaries + timestamp)
185-
ADD_PARAM("true");
186-
ADD_PARAM("%s", STRBOOL(params->control));
187-
ADD_PARAM("%" PRIu32, params->display_id);
188-
ADD_PARAM("%s", STRBOOL(params->show_touches));
189-
ADD_PARAM("%s", STRBOOL(params->stay_awake));
190-
ADD_PARAM("%s", params->codec_options ? params->codec_options : "-");
191-
ADD_PARAM("%s", params->encoder_name ? params->encoder_name : "-");
192-
ADD_PARAM("%s", STRBOOL(params->power_off_on_close));
193-
ADD_PARAM("%s", STRBOOL(params->clipboard_autosync));
185+
ADD_PARAM("send_frame_meta=true");
186+
ADD_PARAM("control=%s", STRBOOL(params->control));
187+
ADD_PARAM("display_id=%" PRIu32, params->display_id);
188+
ADD_PARAM("show_touches=%s", STRBOOL(params->show_touches));
189+
ADD_PARAM("stay_awake=%s", STRBOOL(params->stay_awake));
190+
ADD_PARAM("codec_options=%s",
191+
params->codec_options ? params->codec_options : "");
192+
ADD_PARAM("encoder_name=%s",
193+
params->encoder_name ? params->encoder_name : "");
194+
ADD_PARAM("power_off_on_close=%s", STRBOOL(params->power_off_on_close));
195+
ADD_PARAM("clipboard_autosync=%s", STRBOOL(params->clipboard_autosync));
194196

195197
#undef ADD_PARAM
196198
#undef STRBOOL

server/src/main/java/com/genymobile/scrcpy/CodecOption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public Object getValue() {
2121
}
2222

2323
public static List<CodecOption> parse(String codecOptions) {
24-
if ("-".equals(codecOptions)) {
24+
if (codecOptions.isEmpty()) {
2525
return null;
2626
}
2727

server/src/main/java/com/genymobile/scrcpy/Options.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
import java.util.List;
66

77
public class Options {
8-
private Ln.Level logLevel;
8+
private Ln.Level logLevel = Ln.Level.DEBUG;
99
private int maxSize;
10-
private int bitRate;
10+
private int bitRate = 8000000;
1111
private int maxFps;
12-
private int lockedVideoOrientation;
12+
private int lockedVideoOrientation = -1;
1313
private boolean tunnelForward;
1414
private Rect crop;
15-
private boolean sendFrameMeta; // send PTS so that the client may record properly
16-
private boolean control;
15+
private boolean sendFrameMeta = true; // send PTS so that the client may record properly
16+
private boolean control = true;
1717
private int displayId;
1818
private boolean showTouches;
1919
private boolean stayAwake;
2020
private List<CodecOption> codecOptions;
2121
private String encoderName;
2222
private boolean powerOffScreenOnClose;
23-
private boolean clipboardAutosync;
23+
private boolean clipboardAutosync = true;
2424

2525
public Ln.Level getLogLevel() {
2626
return logLevel;

server/src/main/java/com/genymobile/scrcpy/Server.java

Lines changed: 80 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -160,67 +160,93 @@ private static Options createOptions(String... args) {
160160
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
161161
}
162162

163-
final int expectedParameters = 17;
164-
if (args.length != expectedParameters) {
165-
throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters");
166-
}
167-
168163
Options options = new Options();
169164

170-
Ln.Level level = Ln.Level.valueOf(args[1].toUpperCase(Locale.ENGLISH));
171-
options.setLogLevel(level);
172-
173-
int maxSize = Integer.parseInt(args[2]) & ~7; // multiple of 8
174-
options.setMaxSize(maxSize);
175-
176-
int bitRate = Integer.parseInt(args[3]);
177-
options.setBitRate(bitRate);
178-
179-
int maxFps = Integer.parseInt(args[4]);
180-
options.setMaxFps(maxFps);
181-
182-
int lockedVideoOrientation = Integer.parseInt(args[5]);
183-
options.setLockedVideoOrientation(lockedVideoOrientation);
184-
185-
// use "adb forward" instead of "adb tunnel"? (so the server must listen)
186-
boolean tunnelForward = Boolean.parseBoolean(args[6]);
187-
options.setTunnelForward(tunnelForward);
188-
189-
Rect crop = parseCrop(args[7]);
190-
options.setCrop(crop);
191-
192-
boolean sendFrameMeta = Boolean.parseBoolean(args[8]);
193-
options.setSendFrameMeta(sendFrameMeta);
194-
195-
boolean control = Boolean.parseBoolean(args[9]);
196-
options.setControl(control);
197-
198-
int displayId = Integer.parseInt(args[10]);
199-
options.setDisplayId(displayId);
200-
201-
boolean showTouches = Boolean.parseBoolean(args[11]);
202-
options.setShowTouches(showTouches);
203-
204-
boolean stayAwake = Boolean.parseBoolean(args[12]);
205-
options.setStayAwake(stayAwake);
206-
207-
List<CodecOption> codecOptions = CodecOption.parse(args[13]);
208-
options.setCodecOptions(codecOptions);
209-
210-
String encoderName = "-".equals(args[14]) ? null : args[14];
211-
options.setEncoderName(encoderName);
212-
213-
boolean powerOffScreenOnClose = Boolean.parseBoolean(args[15]);
214-
options.setPowerOffScreenOnClose(powerOffScreenOnClose);
215-
216-
boolean clipboardAutosync = Boolean.parseBoolean(args[16]);
217-
options.setClipboardAutosync(clipboardAutosync);
165+
for (int i = 1; i < args.length; ++i) {
166+
String arg = args[i];
167+
int equalIndex = arg.indexOf('=');
168+
if (equalIndex == -1) {
169+
throw new IllegalArgumentException("Invalid key=value pair: \"" + arg + "\"");
170+
}
171+
String key = arg.substring(0, equalIndex);
172+
String value = arg.substring(equalIndex + 1);
173+
switch (key) {
174+
case "log_level":
175+
Ln.Level level = Ln.Level.valueOf(value.toUpperCase(Locale.ENGLISH));
176+
options.setLogLevel(level);
177+
break;
178+
case "max_size":
179+
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
180+
options.setMaxSize(maxSize);
181+
break;
182+
case "bit_rate":
183+
int bitRate = Integer.parseInt(value);
184+
options.setBitRate(bitRate);
185+
break;
186+
case "max_fps":
187+
int maxFps = Integer.parseInt(value);
188+
options.setMaxFps(maxFps);
189+
break;
190+
case "lock_video_orientation":
191+
int lockedVideoOrientation = Integer.parseInt(value);
192+
options.setLockedVideoOrientation(lockedVideoOrientation);
193+
break;
194+
case "tunnel_forward":
195+
boolean tunnelForward = Boolean.parseBoolean(value);
196+
options.setTunnelForward(tunnelForward);
197+
break;
198+
case "crop":
199+
Rect crop = parseCrop(value);
200+
options.setCrop(crop);
201+
break;
202+
case "send_frame_meta":
203+
boolean sendFrameMeta = Boolean.parseBoolean(value);
204+
options.setSendFrameMeta(sendFrameMeta);
205+
break;
206+
case "control":
207+
boolean control = Boolean.parseBoolean(value);
208+
options.setControl(control);
209+
break;
210+
case "display_id":
211+
int displayId = Integer.parseInt(value);
212+
options.setDisplayId(displayId);
213+
break;
214+
case "show_touches":
215+
boolean showTouches = Boolean.parseBoolean(value);
216+
options.setShowTouches(showTouches);
217+
break;
218+
case "stay_awake":
219+
boolean stayAwake = Boolean.parseBoolean(value);
220+
options.setStayAwake(stayAwake);
221+
break;
222+
case "codec_options":
223+
List<CodecOption> codecOptions = CodecOption.parse(value);
224+
options.setCodecOptions(codecOptions);
225+
break;
226+
case "encoder_name":
227+
if (!value.isEmpty()) {
228+
options.setEncoderName(value);
229+
}
230+
break;
231+
case "power_off_on_close":
232+
boolean powerOffScreenOnClose = Boolean.parseBoolean(value);
233+
options.setPowerOffScreenOnClose(powerOffScreenOnClose);
234+
break;
235+
case "clipboard_autosync":
236+
boolean clipboardAutosync = Boolean.parseBoolean(value);
237+
options.setClipboardAutosync(clipboardAutosync);
238+
break;
239+
default:
240+
Ln.w("Unknown server option: " + key);
241+
break;
242+
}
243+
}
218244

219245
return options;
220246
}
221247

222248
private static Rect parseCrop(String crop) {
223-
if ("-".equals(crop)) {
249+
if (crop.isEmpty()) {
224250
return null;
225251
}
226252
// input format: "width:height:x:y"

0 commit comments

Comments
 (0)