Skip to content

Commit

Permalink
Add specific exit code for device disconnection
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.

Fixes #3083 <#3083>
PR #3085 <#3085>
Signed-off-by: martin f. krafft <[email protected]>
Signed-off-by: Romain Vimont <[email protected]>
  • Loading branch information
madduck authored and rom1v committed Mar 6, 2022
1 parent 1f4c801 commit b3f5dfe
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 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 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)
Expand Down
16 changes: 8 additions & 8 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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;
}
18 changes: 9 additions & 9 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -265,20 +265,20 @@ 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;

// 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;
Expand Down Expand Up @@ -332,7 +332,7 @@ scrcpy(struct scrcpy_options *options) {
.on_disconnected = sc_server_on_disconnected,
};
if (!sc_server_init(&s->server, &params, &cbs, NULL)) {
return false;
return SCRCPY_EXIT_FAILURE;
}

if (!sc_server_start(&s->server)) {
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 12 additions & 1 deletion app/src/scrcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_code
scrcpy(struct scrcpy_options *options);

#endif
14 changes: 7 additions & 7 deletions app/src/usb/scrcpy_otg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions app/src/usb/scrcpy_otg.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include "common.h"

#include <stdbool.h>
#include "options.h"
#include "scrcpy.h"

bool
enum scrcpy_exit_code
scrcpy_otg(struct scrcpy_options *options);

#endif

0 comments on commit b3f5dfe

Please sign in to comment.