Display and control your Android device
Add specific exit code for device disconnection
martin f. krafftb3f5dfe1de03
diff --git a/app/scrcpy.1 b/app/scrcpy.1index 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 SHORTCUTSIn the following list, MOD is the shortcut modifier. By default, it's (left)diff --git a/app/src/main.c b/app/src/main.cindex 2d1575f8..3334cbf9 100644--- a/app/src/main.c+++ b/app/src/main.c@@ -40,19 +40,19 @@ main(int argc, char *argv[]) {#endifif (!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[]) {#endifif (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);#endifavformat_network_deinit(); // ignore failure- return ok ? 0 : 1;+ return ret;}diff --git a/app/src/scrcpy.c b/app/src/scrcpy.cindex 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_codeevent_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_codescrcpy(struct scrcpy_options *options) {static struct scrcpy scrcpy;struct scrcpy *s = &scrcpy;@@ -273,12 +273,12 @@ scrcpy(struct scrcpy_options *options) {// Minimal SDL initializationif (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.hindex cdcecda7..d4d494a3 100644--- a/app/src/scrcpy.h+++ b/app/src/scrcpy.h@@ -6,7 +6,18 @@#include <stdbool.h>#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_codescrcpy(struct scrcpy_options *options);#endifdiff --git a/app/src/usb/scrcpy_otg.c b/app/src/usb/scrcpy_otg.cindex 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_codeevent_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_codescrcpy_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.hindex 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 <stdbool.h>#include "options.h"+#include "scrcpy.h"-bool+enum scrcpy_exit_codescrcpy_otg(struct scrcpy_options *options);#endif