From b3f5dfe1de03ff8e19225aee08dcbd62d6089d56 Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Sat, 5 Mar 2022 15:47:58 +0100 Subject: [PATCH] Add specific exit code for device disconnection 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. Fixes #3083 PR #3085 Signed-off-by: martin f. krafft Signed-off-by: Romain Vimont --- app/scrcpy.1 | 6 ++++++ app/src/main.c | 16 ++++++++-------- app/src/scrcpy.c | 18 +++++++++--------- app/src/scrcpy.h | 13 ++++++++++++- app/src/usb/scrcpy_otg.c | 14 +++++++------- app/src/usb/scrcpy_otg.h | 4 ++-- 6 files changed, 44 insertions(+), 27 deletions(-) diff --git a/app/scrcpy.1 b/app/scrcpy.1 index f9d4ba24..f1ee732d 100644 --- a/app/scrcpy.1 +++ b/app/scrcpy.1 @@ -355,6 +355,12 @@ Set the initial window height. Default is 0 (automatic). +.SH EXIT STATUS +.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 2d1575f8..3334cbf9 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -40,19 +40,19 @@ main(int argc, char *argv[]) { #endif if (!scrcpy_parse_args(&args, argc, argv)) { - return 1; + return SCRCPY_EXIT_FAILURE; } sc_set_log_level(args.opts.log_level); if (args.help) { scrcpy_print_usage(argv[0]); - return 0; + return SCRCPY_EXIT_SUCCESS; } if (args.version) { scrcpy_print_version(); - return 0; + return SCRCPY_EXIT_SUCCESS; } #ifdef SCRCPY_LAVF_REQUIRES_REGISTER_ALL @@ -66,17 +66,17 @@ main(int argc, char *argv[]) { #endif if (avformat_network_init()) { - return 1; + return SCRCPY_EXIT_FAILURE; } #ifdef HAVE_USB - bool ok = args.opts.otg ? scrcpy_otg(&args.opts) - : scrcpy(&args.opts); + enum scrcpy_exit_code ret = args.opts.otg ? scrcpy_otg(&args.opts) + : scrcpy(&args.opts); #else - bool ok = scrcpy(&args.opts); + enum scrcpy_exit_code 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 8c549a4e..8fbfe394 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -149,23 +149,23 @@ sdl_configure(bool display, bool disable_screensaver) { } } -static bool +static enum scrcpy_exit_code 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 SCRCPY_EXIT_DISCONNECTED; case SDL_QUIT: LOGD("User requested to quit"); - return true; + return SCRCPY_EXIT_SUCCESS; default: sc_screen_handle_event(&s->screen, &event); break; } } - return false; + return SCRCPY_EXIT_FAILURE; } // Return true on success, false on error @@ -265,7 +265,7 @@ sc_server_on_disconnected(struct sc_server *server, void *userdata) { // event } -bool +enum scrcpy_exit_code scrcpy(struct scrcpy_options *options) { static struct scrcpy scrcpy; struct scrcpy *s = &scrcpy; @@ -273,12 +273,12 @@ scrcpy(struct scrcpy_options *options) { // Minimal SDL initialization if (SDL_Init(SDL_INIT_EVENTS)) { LOGE("Could not initialize SDL: %s", SDL_GetError()); - return false; + return SCRCPY_EXIT_FAILURE; } atexit(SDL_Quit); - bool ret = false; + enum scrcpy_exit_code ret = SCRCPY_EXIT_FAILURE; bool server_started = false; bool file_pusher_initialized = false; @@ -332,7 +332,7 @@ scrcpy(struct scrcpy_options *options) { .on_disconnected = sc_server_on_disconnected, }; if (!sc_server_init(&s->server, ¶ms, &cbs, NULL)) { - return false; + return SCRCPY_EXIT_FAILURE; } if (!sc_server_start(&s->server)) { @@ -361,7 +361,7 @@ scrcpy(struct scrcpy_options *options) { if (!connected) { // This is not an error, user requested to quit - ret = true; + ret = SCRCPY_EXIT_SUCCESS; goto end; } diff --git a/app/src/scrcpy.h b/app/src/scrcpy.h index cdcecda7..d4d494a3 100644 --- a/app/src/scrcpy.h +++ b/app/src/scrcpy.h @@ -6,7 +6,18 @@ #include #include "options.h" -bool +enum scrcpy_exit_code { + // Normal program termination + SCRCPY_EXIT_SUCCESS, + + // No connection could be established + SCRCPY_EXIT_FAILURE, + + // Device was disconnected while running + SCRCPY_EXIT_DISCONNECTED, +}; + +enum scrcpy_exit_code scrcpy(struct scrcpy_options *options); #endif diff --git a/app/src/usb/scrcpy_otg.c b/app/src/usb/scrcpy_otg.c index 1c53410e..db5e64d8 100644 --- a/app/src/usb/scrcpy_otg.c +++ b/app/src/usb/scrcpy_otg.c @@ -29,26 +29,26 @@ sc_usb_on_disconnected(struct sc_usb *usb, void *userdata) { } } -static bool +static enum scrcpy_exit_code event_loop(struct scrcpy_otg *s) { SDL_Event event; while (SDL_WaitEvent(&event)) { switch (event.type) { case EVENT_USB_DEVICE_DISCONNECTED: LOGW("Device disconnected"); - return false; + return SCRCPY_EXIT_DISCONNECTED; case SDL_QUIT: LOGD("User requested to quit"); - return true; + return SCRCPY_EXIT_SUCCESS; default: sc_screen_otg_handle_event(&s->screen_otg, &event); break; } } - return false; + return SCRCPY_EXIT_FAILURE; } -bool +enum scrcpy_exit_code scrcpy_otg(struct scrcpy_options *options) { static struct scrcpy_otg scrcpy_otg; struct scrcpy_otg *s = &scrcpy_otg; @@ -67,7 +67,7 @@ scrcpy_otg(struct scrcpy_options *options) { LOGW("Could not enable mouse focus clickthrough"); } - bool ret = false; + enum scrcpy_exit_code ret = SCRCPY_EXIT_FAILURE; struct sc_hid_keyboard *keyboard = NULL; struct sc_hid_mouse *mouse = NULL; @@ -90,7 +90,7 @@ scrcpy_otg(struct scrcpy_options *options) { }; bool ok = sc_usb_init(&s->usb); if (!ok) { - return false; + return SCRCPY_EXIT_FAILURE; } struct sc_usb_device usb_device; diff --git a/app/src/usb/scrcpy_otg.h b/app/src/usb/scrcpy_otg.h index 24b9cde8..e477660b 100644 --- a/app/src/usb/scrcpy_otg.h +++ b/app/src/usb/scrcpy_otg.h @@ -3,10 +3,10 @@ #include "common.h" -#include #include "options.h" +#include "scrcpy.h" -bool +enum scrcpy_exit_code scrcpy_otg(struct scrcpy_options *options); #endif