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 <madduck@madduck.net>
This commit is contained in:
martin f. krafft 2022-03-05 15:47:58 +01:00
commit 058bb04ee5
5 changed files with 34 additions and 18 deletions

View file

@ -355,6 +355,12 @@ Set the initial window height.
Default is 0 (automatic). 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 .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

@ -70,13 +70,13 @@ main(int argc, char *argv[]) {
} }
#ifdef HAVE_USB #ifdef HAVE_USB
bool ok = args.opts.otg ? scrcpy_otg(&args.opts) int ret = args.opts.otg ? scrcpy_otg(&args.opts)
: scrcpy(&args.opts); : scrcpy(&args.opts);
#else #else
bool ok = scrcpy(&args.opts); int 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,17 +149,17 @@ sdl_configure(bool display, bool disable_screensaver) {
} }
} }
static bool static int
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 EVENT_STREAM_STOPPED;
case SDL_QUIT: case SDL_QUIT:
LOGD("User requested to quit"); LOGD("User requested to quit");
return true; return SDL_QUIT;
default: default:
sc_screen_handle_event(&s->screen, &event); sc_screen_handle_event(&s->screen, &event);
break; break;
@ -168,20 +168,20 @@ event_loop(struct scrcpy *s) {
return false; return false;
} }
static bool static int
await_for_server(void) { await_for_server(void) {
SDL_Event event; SDL_Event event;
while (SDL_WaitEvent(&event)) { while (SDL_WaitEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_QUIT: case SDL_QUIT:
LOGD("User requested to quit"); LOGD("User requested to quit");
return false; return SDL_QUIT;
case EVENT_SERVER_CONNECTION_FAILED: case EVENT_SERVER_CONNECTION_FAILED:
LOGE("Server connection failed"); LOGE("Server connection failed");
return false; return EVENT_SERVER_CONNECTION_FAILED;
case EVENT_SERVER_CONNECTED: case EVENT_SERVER_CONNECTED:
LOGD("Server connected"); LOGD("Server connected");
return true; return 0;
default: default:
break; break;
} }
@ -262,7 +262,7 @@ sc_server_on_disconnected(struct sc_server *server, void *userdata) {
// event // event
} }
bool int
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;
@ -275,7 +275,7 @@ scrcpy(struct scrcpy_options *options) {
atexit(SDL_Quit); atexit(SDL_Quit);
bool ret = false; int ret = 0;
bool server_started = false; bool server_started = false;
bool file_pusher_initialized = false; bool file_pusher_initialized = false;
@ -351,7 +351,8 @@ scrcpy(struct scrcpy_options *options) {
sdl_configure(options->display, options->disable_screensaver); sdl_configure(options->display, options->disable_screensaver);
// Await for server without blocking Ctrl+C handling // Await for server without blocking Ctrl+C handling
if (!await_for_server()) { ret = await_for_server();
if (ret > 0) {
goto end; goto end;
} }
@ -707,5 +708,14 @@ end:
sc_server_destroy(&s->server); 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;
}
} }

View file

@ -6,7 +6,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "options.h" #include "options.h"
bool int
scrcpy(struct scrcpy_options *options); scrcpy(struct scrcpy_options *options);
#endif #endif

View file

@ -36,10 +36,10 @@ event_loop(struct scrcpy_otg *s) {
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 EVENT_USB_DEVICE_DISCONNECTED;
case SDL_QUIT: case SDL_QUIT:
LOGD("User requested to quit"); LOGD("User requested to quit");
return true; return SDL_QUIT;
default: default:
sc_screen_otg_handle_event(&s->screen_otg, &event); sc_screen_otg_handle_event(&s->screen_otg, &event);
break; break;
@ -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; int ret = 0;
struct sc_hid_keyboard *keyboard = NULL; struct sc_hid_keyboard *keyboard = NULL;
struct sc_hid_mouse *mouse = NULL; struct sc_hid_mouse *mouse = NULL;