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 <https://github.com/Genymobile/scrcpy/issues/3083>
PR #3085 <https://github.com/Genymobile/scrcpy/pull/3085>
Signed-off-by: martin f. krafft <madduck@madduck.net>
Signed-off-by: Romain Vimont <rom@rom1v.com>
This commit is contained in:
martin f. krafft 2022-03-05 15:47:58 +01:00 committed by Romain Vimont
parent 1f4c801f3c
commit b3f5dfe1de
6 changed files with 44 additions and 27 deletions

View file

@ -355,6 +355,12 @@ Set the initial window height.
Default is 0 (automatic). 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 .SH SHORTCUTS
In the following list, MOD is the shortcut modifier. By default, it's (left) In the following list, MOD is the shortcut modifier. By default, it's (left)

View file

@ -40,19 +40,19 @@ main(int argc, char *argv[]) {
#endif #endif
if (!scrcpy_parse_args(&args, argc, argv)) { if (!scrcpy_parse_args(&args, argc, argv)) {
return 1; return SCRCPY_EXIT_FAILURE;
} }
sc_set_log_level(args.opts.log_level); sc_set_log_level(args.opts.log_level);
if (args.help) { if (args.help) {
scrcpy_print_usage(argv[0]); scrcpy_print_usage(argv[0]);
return 0; return SCRCPY_EXIT_SUCCESS;
} }
if (args.version) { if (args.version) {
scrcpy_print_version(); scrcpy_print_version();
return 0; return SCRCPY_EXIT_SUCCESS;
} }
#ifdef SCRCPY_LAVF_REQUIRES_REGISTER_ALL #ifdef SCRCPY_LAVF_REQUIRES_REGISTER_ALL
@ -66,17 +66,17 @@ main(int argc, char *argv[]) {
#endif #endif
if (avformat_network_init()) { if (avformat_network_init()) {
return 1; return SCRCPY_EXIT_FAILURE;
} }
#ifdef HAVE_USB #ifdef HAVE_USB
bool ok = args.opts.otg ? scrcpy_otg(&args.opts) enum scrcpy_exit_code ret = args.opts.otg ? scrcpy_otg(&args.opts)
: scrcpy(&args.opts); : scrcpy(&args.opts);
#else #else
bool ok = scrcpy(&args.opts); enum scrcpy_exit_code ret = scrcpy(&args.opts);
#endif #endif
avformat_network_deinit(); // ignore failure avformat_network_deinit(); // ignore failure
return ok ? 0 : 1; return ret;
} }

View file

@ -149,23 +149,23 @@ sdl_configure(bool display, bool disable_screensaver) {
} }
} }
static bool static enum scrcpy_exit_code
event_loop(struct scrcpy *s) { event_loop(struct scrcpy *s) {
SDL_Event event; SDL_Event event;
while (SDL_WaitEvent(&event)) { while (SDL_WaitEvent(&event)) {
switch (event.type) { switch (event.type) {
case EVENT_STREAM_STOPPED: case EVENT_STREAM_STOPPED:
LOGW("Device disconnected"); LOGW("Device disconnected");
return false; return SCRCPY_EXIT_DISCONNECTED;
case SDL_QUIT: case SDL_QUIT:
LOGD("User requested to quit"); LOGD("User requested to quit");
return true; return SCRCPY_EXIT_SUCCESS;
default: default:
sc_screen_handle_event(&s->screen, &event); sc_screen_handle_event(&s->screen, &event);
break; break;
} }
} }
return false; return SCRCPY_EXIT_FAILURE;
} }
// Return true on success, false on error // Return true on success, false on error
@ -265,7 +265,7 @@ sc_server_on_disconnected(struct sc_server *server, void *userdata) {
// event // event
} }
bool enum scrcpy_exit_code
scrcpy(struct scrcpy_options *options) { scrcpy(struct scrcpy_options *options) {
static struct scrcpy scrcpy; static struct scrcpy scrcpy;
struct scrcpy *s = &scrcpy; struct scrcpy *s = &scrcpy;
@ -273,12 +273,12 @@ scrcpy(struct scrcpy_options *options) {
// Minimal SDL initialization // Minimal SDL initialization
if (SDL_Init(SDL_INIT_EVENTS)) { if (SDL_Init(SDL_INIT_EVENTS)) {
LOGE("Could not initialize SDL: %s", SDL_GetError()); LOGE("Could not initialize SDL: %s", SDL_GetError());
return false; return SCRCPY_EXIT_FAILURE;
} }
atexit(SDL_Quit); atexit(SDL_Quit);
bool ret = false; enum scrcpy_exit_code ret = SCRCPY_EXIT_FAILURE;
bool server_started = false; bool server_started = false;
bool file_pusher_initialized = false; bool file_pusher_initialized = false;
@ -332,7 +332,7 @@ scrcpy(struct scrcpy_options *options) {
.on_disconnected = sc_server_on_disconnected, .on_disconnected = sc_server_on_disconnected,
}; };
if (!sc_server_init(&s->server, &params, &cbs, NULL)) { if (!sc_server_init(&s->server, &params, &cbs, NULL)) {
return false; return SCRCPY_EXIT_FAILURE;
} }
if (!sc_server_start(&s->server)) { if (!sc_server_start(&s->server)) {
@ -361,7 +361,7 @@ scrcpy(struct scrcpy_options *options) {
if (!connected) { if (!connected) {
// This is not an error, user requested to quit // This is not an error, user requested to quit
ret = true; ret = SCRCPY_EXIT_SUCCESS;
goto end; goto end;
} }

View file

@ -6,7 +6,18 @@
#include <stdbool.h> #include <stdbool.h>
#include "options.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_code
scrcpy(struct scrcpy_options *options); scrcpy(struct scrcpy_options *options);
#endif #endif

View file

@ -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) { event_loop(struct scrcpy_otg *s) {
SDL_Event event; SDL_Event event;
while (SDL_WaitEvent(&event)) { while (SDL_WaitEvent(&event)) {
switch (event.type) { switch (event.type) {
case EVENT_USB_DEVICE_DISCONNECTED: case EVENT_USB_DEVICE_DISCONNECTED:
LOGW("Device disconnected"); LOGW("Device disconnected");
return false; return SCRCPY_EXIT_DISCONNECTED;
case SDL_QUIT: case SDL_QUIT:
LOGD("User requested to quit"); LOGD("User requested to quit");
return true; return SCRCPY_EXIT_SUCCESS;
default: default:
sc_screen_otg_handle_event(&s->screen_otg, &event); sc_screen_otg_handle_event(&s->screen_otg, &event);
break; break;
} }
} }
return false; return SCRCPY_EXIT_FAILURE;
} }
bool enum scrcpy_exit_code
scrcpy_otg(struct scrcpy_options *options) { scrcpy_otg(struct scrcpy_options *options) {
static struct scrcpy_otg scrcpy_otg; static struct scrcpy_otg scrcpy_otg;
struct scrcpy_otg *s = &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"); 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_keyboard *keyboard = NULL;
struct sc_hid_mouse *mouse = NULL; struct sc_hid_mouse *mouse = NULL;
@ -90,7 +90,7 @@ scrcpy_otg(struct scrcpy_options *options) {
}; };
bool ok = sc_usb_init(&s->usb); bool ok = sc_usb_init(&s->usb);
if (!ok) { if (!ok) {
return false; return SCRCPY_EXIT_FAILURE;
} }
struct sc_usb_device usb_device; struct sc_usb_device usb_device;

View file

@ -3,10 +3,10 @@
#include "common.h" #include "common.h"
#include <stdbool.h>
#include "options.h" #include "options.h"
#include "scrcpy.h"
bool enum scrcpy_exit_code
scrcpy_otg(struct scrcpy_options *options); scrcpy_otg(struct scrcpy_options *options);
#endif #endif