Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different exit codes for failure to connect and disconnect #3085

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ Set the initial window height.

Default is 0 (automatic).

.SH EXIT CODES
.B scrcpy
will exit with code 0 on normal program termination. If an initial
connection cannot be established, the exit code 1 will be returned. If the
device disconnects while a session is active, exit code 2 will be returned.

.SH SHORTCUTS

In the following list, MOD is the shortcut modifier. By default, it's (left)
Expand Down
6 changes: 3 additions & 3 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ main(int argc, char *argv[]) {
}

#ifdef HAVE_USB
bool ok = args.opts.otg ? scrcpy_otg(&args.opts)
int ret = args.opts.otg ? scrcpy_otg(&args.opts)
: scrcpy(&args.opts);
#else
bool ok = scrcpy(&args.opts);
int ret = scrcpy(&args.opts);
#endif

avformat_network_deinit(); // ignore failure

return ok ? 0 : 1;
return ret;
}
32 changes: 21 additions & 11 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,17 @@ sdl_configure(bool display, bool disable_screensaver) {
}
}

static bool
static int
event_loop(struct scrcpy *s) {
SDL_Event event;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
case EVENT_STREAM_STOPPED:
LOGW("Device disconnected");
return false;
return EVENT_STREAM_STOPPED;
case SDL_QUIT:
LOGD("User requested to quit");
return true;
return SDL_QUIT;
default:
sc_screen_handle_event(&s->screen, &event);
break;
Expand All @@ -168,20 +168,20 @@ event_loop(struct scrcpy *s) {
return false;
}

static bool
static int
await_for_server(void) {
SDL_Event event;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
LOGD("User requested to quit");
return false;
return SDL_QUIT;
case EVENT_SERVER_CONNECTION_FAILED:
LOGE("Server connection failed");
return false;
return EVENT_SERVER_CONNECTION_FAILED;
case EVENT_SERVER_CONNECTED:
LOGD("Server connected");
return true;
return 0;
default:
break;
}
Expand Down Expand Up @@ -262,7 +262,7 @@ sc_server_on_disconnected(struct sc_server *server, void *userdata) {
// event
}

bool
int
scrcpy(struct scrcpy_options *options) {
static struct scrcpy scrcpy;
struct scrcpy *s = &scrcpy;
Expand All @@ -275,7 +275,7 @@ scrcpy(struct scrcpy_options *options) {

atexit(SDL_Quit);

bool ret = false;
int ret = 0;

bool server_started = false;
bool file_pusher_initialized = false;
Expand Down Expand Up @@ -351,7 +351,8 @@ scrcpy(struct scrcpy_options *options) {
sdl_configure(options->display, options->disable_screensaver);

// Await for server without blocking Ctrl+C handling
if (!await_for_server()) {
ret = await_for_server();
if (ret > 0) {
goto end;
}

Expand Down Expand Up @@ -707,5 +708,14 @@ scrcpy(struct scrcpy_options *options) {

sc_server_destroy(&s->server);

return ret;
switch (ret) {
case EVENT_STREAM_STOPPED:
return 2;
case EVENT_SERVER_CONNECTION_FAILED:
return 1;
case SDL_QUIT:
return 0;
default:
return ret;
}
}
2 changes: 1 addition & 1 deletion app/src/scrcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <stdbool.h>
#include "options.h"

bool
int
scrcpy(struct scrcpy_options *options);

#endif
6 changes: 3 additions & 3 deletions app/src/usb/scrcpy_otg.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ event_loop(struct scrcpy_otg *s) {
switch (event.type) {
case EVENT_USB_DEVICE_DISCONNECTED:
LOGW("Device disconnected");
return false;
return EVENT_USB_DEVICE_DISCONNECTED;
case SDL_QUIT:
LOGD("User requested to quit");
return true;
return SDL_QUIT;
default:
sc_screen_otg_handle_event(&s->screen_otg, &event);
break;
Expand Down Expand Up @@ -67,7 +67,7 @@ scrcpy_otg(struct scrcpy_options *options) {
LOGW("Could not enable mouse focus clickthrough");
}

bool ret = false;
int ret = 0;

struct sc_hid_keyboard *keyboard = NULL;
struct sc_hid_mouse *mouse = NULL;
Expand Down