Skip to content

Commit

Permalink
Factorize --list- options handling
Browse files Browse the repository at this point in the history
This will limit code duplication as more list options will be added.

PR #4213 <#4213>
  • Loading branch information
rom1v committed Oct 31, 2023
1 parent a2fb1b4 commit f085765
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -1993,10 +1993,10 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
#endif
case OPT_LIST_ENCODERS:
opts->list_encoders = true;
opts->list |= SC_OPTION_LIST_ENCODERS;
break;
case OPT_LIST_DISPLAYS:
opts->list_displays = true;
opts->list |= SC_OPTION_LIST_DISPLAYS;
break;
case OPT_REQUIRE_AUDIO:
opts->require_audio = true;
Expand Down
3 changes: 1 addition & 2 deletions app/src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ const struct scrcpy_options scrcpy_options_default = {
.video = true,
.audio = true,
.require_audio = false,
.list_encoders = false,
.list_displays = false,
.kill_adb_on_close = false,
.list = 0,
};
5 changes: 3 additions & 2 deletions app/src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ struct scrcpy_options {
bool video;
bool audio;
bool require_audio;
bool list_encoders;
bool list_displays;
bool kill_adb_on_close;
#define SC_OPTION_LIST_ENCODERS 0x1
#define SC_OPTION_LIST_DISPLAYS 0x2
uint8_t list;
};

extern const struct scrcpy_options scrcpy_options_default;
Expand Down
5 changes: 2 additions & 3 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,8 @@ scrcpy(struct scrcpy_options *options) {
.tcpip_dst = options->tcpip_dst,
.cleanup = options->cleanup,
.power_on = options->power_on,
.list_encoders = options->list_encoders,
.list_displays = options->list_displays,
.kill_adb_on_close = options->kill_adb_on_close,
.list = options->list,
};

static const struct sc_server_callbacks cbs = {
Expand All @@ -399,7 +398,7 @@ scrcpy(struct scrcpy_options *options) {

server_started = true;

if (options->list_encoders || options->list_displays) {
if (options->list) {
bool ok = await_for_server(NULL);
ret = ok ? SCRCPY_EXIT_SUCCESS : SCRCPY_EXIT_FAILURE;
goto end;
Expand Down
6 changes: 3 additions & 3 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ execute_server(struct sc_server *server,
// By default, power_on is true
ADD_PARAM("power_on=false");
}
if (params->list_encoders) {
if (params->list & SC_OPTION_LIST_ENCODERS) {
ADD_PARAM("list_encoders=true");
}
if (params->list_displays) {
if (params->list & SC_OPTION_LIST_DISPLAYS) {
ADD_PARAM("list_displays=true");
}

Expand Down Expand Up @@ -896,7 +896,7 @@ run_server(void *data) {

// If --list-* is passed, then the server just prints the requested data
// then exits.
if (params->list_encoders || params->list_displays) {
if (params->list) {
sc_pid pid = execute_server(server, params);
if (pid == SC_PROCESS_NONE) {
goto error_connection_failed;
Expand Down
3 changes: 1 addition & 2 deletions app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ struct sc_server_params {
bool select_tcpip;
bool cleanup;
bool power_on;
bool list_encoders;
bool list_displays;
bool kill_adb_on_close;
uint8_t list;
};

struct sc_server {
Expand Down
4 changes: 4 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public boolean getPowerOn() {
return powerOn;
}

public boolean getList() {
return listEncoders || listDisplays;
}

public boolean getListEncoders() {
return listEncoders;
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private static void internalMain(String... args) throws Exception {

Ln.initLogLevel(options.getLogLevel());

if (options.getListEncoders() || options.getListDisplays()) {
if (options.getList()) {
if (options.getCleanup()) {
CleanUp.unlinkSelf();
}
Expand Down

0 comments on commit f085765

Please sign in to comment.