Skip to content

Commit

Permalink
Different exit codes for failure to connect and disconnect
Browse files Browse the repository at this point in the history
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#3083
Signed-off-by: martin f. krafft <[email protected]>
  • Loading branch information
madduck committed Mar 5, 2022
1 parent adbe790 commit e11bc0c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
6 changes: 6 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
32 changes: 21 additions & 11 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion app/src/scrcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <stdbool.h>
#include "options.h"

bool
int
scrcpy(struct scrcpy_options *options);

#endif
6 changes: 3 additions & 3 deletions app/src/usb/scrcpy_otg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e11bc0c

Please sign in to comment.