Skip to content

Commit

Permalink
Add --pause-on-error
Browse files Browse the repository at this point in the history
Add an option to make scrcpy pause just before it returns with an error
(a non-zero exit code). This prevents any host terminal to close so that
error messages can be read.

Refs #3817 <#3817>
Refs #3822 <#3822>
  • Loading branch information
rom1v committed Jun 29, 2023
1 parent 85b55b3 commit 2e759a2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/data/bash-completion/scrcpy
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ _scrcpy() {
--no-video-playback
--otg
-p --port=
--pause-on-error
--power-off-on-close
--prefer-text
--print-fps
Expand Down
1 change: 1 addition & 0 deletions app/data/zsh-completion/_scrcpy
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ arguments=(
'--no-video-playback[Disable video playback]'
'--otg[Run in OTG mode \(simulating physical keyboard and mouse\)]'
{-p,--port=}'[\[port\[\:port\]\] Set the TCP port \(range\) used by the client to listen]'
'--pause-on-error[Make scrcpy pause before exiting when an error occurs]'
'--power-off-on-close[Turn the device screen off when closing scrcpy]'
'--prefer-text[Inject alpha characters and space as text events instead of key events]'
'--print-fps[Start FPS counter, to print frame logs to the console]'
Expand Down
6 changes: 6 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ Set the TCP port (range) used by the client to listen.

Default is 27183:27199.

.TP
.B \-\-pause\-on\-error
Make scrcpy pause before exiting when an error occurs.

This prevents the terminal to close so that error messages can be read.

.TP
.B \-\-power\-off\-on\-close
Turn the device screen off when closing scrcpy.
Expand Down
29 changes: 29 additions & 0 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ enum {
OPT_AUDIO_SOURCE,
OPT_KILL_ADB_ON_CLOSE,
OPT_TIME_LIMIT,
OPT_PAUSE_ON_ERROR,
};

struct sc_option {
Expand Down Expand Up @@ -463,6 +464,13 @@ static const struct sc_option options[] = {
"Default is " STR(DEFAULT_LOCAL_PORT_RANGE_FIRST) ":"
STR(DEFAULT_LOCAL_PORT_RANGE_LAST) ".",
},
{
.longopt_id = OPT_PAUSE_ON_ERROR,
.longopt = "pause-on-error",
.text = "Make scrcpy pause before exiting when an error occurs.\n"
"This prevents the terminal to close so that error messages "
"can be read.",
},
{
.longopt_id = OPT_POWER_OFF_ON_CLOSE,
.longopt = "power-off-on-close",
Expand Down Expand Up @@ -1977,6 +1985,9 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
}
break;
case OPT_PAUSE_ON_ERROR:
args->pause_on_error = true;
break;
default:
// getopt prints the error message on stderr
return false;
Expand Down Expand Up @@ -2196,6 +2207,17 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return true;
}

static bool
sc_has_pause_on_error(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i) {
if (!strcmp("--pause-on-error", argv[i])) {
return true;
}
}

return false;
}

bool
scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
struct sc_getopt_adapter adapter;
Expand All @@ -2209,5 +2231,12 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {

sc_getopt_adapter_destroy(&adapter);

if (!ret && !args->pause_on_error) {
// Check if any argument equals "--pause-on-error", because this
// argument must be taken into account even if command line parsing
// failed
args->pause_on_error = sc_has_pause_on_error(argc, argv);
}

return ret;
}
1 change: 1 addition & 0 deletions app/src/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct scrcpy_cli_args {
struct scrcpy_options opts;
bool help;
bool version;
bool pause_on_error;
};

void
Expand Down
18 changes: 13 additions & 5 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ main_scrcpy(int argc, char *argv[]) {
.opts = scrcpy_options_default,
.help = false,
.version = false,
.pause_on_error = false,
};

#ifndef NDEBUG
args.opts.log_level = SC_LOG_LEVEL_DEBUG;
#endif

enum scrcpy_exit_code ret = SCRCPY_EXIT_FAILURE;

if (!scrcpy_parse_args(&args, argc, argv)) {
return SCRCPY_EXIT_FAILURE;
goto end;
}

sc_set_log_level(args.opts.log_level);
Expand All @@ -72,18 +75,23 @@ main_scrcpy(int argc, char *argv[]) {
#endif

if (!net_init()) {
return SCRCPY_EXIT_FAILURE;
goto end;
}

sc_log_configure();

#ifdef HAVE_USB
enum scrcpy_exit_code ret = args.opts.otg ? scrcpy_otg(&args.opts)
: scrcpy(&args.opts);
ret = args.opts.otg ? scrcpy_otg(&args.opts) : scrcpy(&args.opts);
#else
enum scrcpy_exit_code ret = scrcpy(&args.opts);
ret = scrcpy(&args.opts);
#endif

end:
if (ret != SCRCPY_EXIT_SUCCESS && args.pause_on_error) {
printf("Press Enter to continue...\n");
getchar();
}

return ret;
}

Expand Down

0 comments on commit 2e759a2

Please sign in to comment.