From e11bc0cf169a851ef6137359d038cf566bdd2f8c Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Sat, 5 Mar 2022 15:47:58 +0100 Subject: [PATCH] Different exit codes for failure to connect and disconnect Modify the return logic such that exit code 1 is used when the initial connection fails, but if a session is established, and then the device disconnects, exit code 2 is emitted. Closes: Github:Genymobile/scrcpy#3083 Signed-off-by: martin f. krafft --- app/scrcpy.1 | 6 ++++++ app/src/main.c | 6 +++--- app/src/scrcpy.c | 32 +++++++++++++++++++++----------- app/src/scrcpy.h | 2 +- app/src/usb/scrcpy_otg.c | 6 +++--- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/app/scrcpy.1 b/app/scrcpy.1 index f9d4ba2471..75770abe0b 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -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) diff --git a/app/src/main.c b/app/src/main.c index 2d1575f8e4..b1e1ea19cf 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -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; } diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 3ed1d24910..f501372e2d 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -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 0; default: sc_screen_handle_event(&s->screen, &event); break; @@ -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; } @@ -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; @@ -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; @@ -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; } @@ -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; + } } diff --git a/app/src/scrcpy.h b/app/src/scrcpy.h index cdcecda7cd..e2d8849a4d 100644 --- a/app/src/scrcpy.h +++ b/app/src/scrcpy.h @@ -6,7 +6,7 @@ #include #include "options.h" -bool +int scrcpy(struct scrcpy_options *options); #endif diff --git a/app/src/usb/scrcpy_otg.c b/app/src/usb/scrcpy_otg.c index 1c53410eeb..4446d3b080 100644 --- a/app/src/usb/scrcpy_otg.c +++ b/app/src/usb/scrcpy_otg.c @@ -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 0; default: sc_screen_otg_handle_event(&s->screen_otg, &event); break; @@ -67,7 +67,7 @@ scrcpy_otg(struct scrcpy_options *options) { LOGW("Could not enable mouse focus clickthrough"); } - bool ret = false; + bool ret = 0; struct sc_hid_keyboard *keyboard = NULL; struct sc_hid_mouse *mouse = NULL;