From d7106d70090b348c461ed2d24c851c52491f6110 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 30 Oct 2021 18:59:46 +0200 Subject: [PATCH] wip --- app/src/events.h | 7 ++++-- app/src/scrcpy.c | 59 +++++++++++++++++++++++++++++++++++++++--------- app/src/server.c | 8 ++----- app/src/server.h | 2 ++ 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/app/src/events.h b/app/src/events.h index a4d6f3df..576f3566 100644 --- a/app/src/events.h +++ b/app/src/events.h @@ -1,2 +1,5 @@ -#define EVENT_NEW_FRAME SDL_USEREVENT -#define EVENT_STREAM_STOPPED (SDL_USEREVENT + 1) +#define EVENT_NEW_FRAME SDL_USEREVENT +#define EVENT_STREAM_STOPPED (SDL_USEREVENT + 1) +#define EVENT_SERVER_CONNECTION_FAILED (SDL_USEREVENT + 2) +#define EVENT_SERVER_CONNECTED (SDL_USEREVENT + 3) +#define EVENT_SERVER_DISCONNECTED (SDL_USEREVENT + 4) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index eeb78fb4..0a72b671 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -158,6 +158,10 @@ static enum event_result handle_event(struct scrcpy *s, const struct scrcpy_options *options, SDL_Event *event) { switch (event->type) { + case EVENT_SERVER_DISCONNECTED: + LOGD("Server disconnected"); + // Do nothing, will be managed by the "stream stopped" event + break; case EVENT_STREAM_STOPPED: LOGD("Video stream stopped"); return EVENT_RESULT_STOPPED_BY_EOS; @@ -216,6 +220,32 @@ event_loop(struct scrcpy *s, const struct scrcpy_options *options) { return false; } +static bool +await_for_server(void) { + SDL_Event event; + while (SDL_WaitEvent(&event)) { + // Should never receive disconnected event before connected + assert(event.type != EVENT_SERVER_DISCONNECTED); + + switch (event.type) { + case SDL_QUIT: + LOGD("User requested to quit"); + return false; + case EVENT_SERVER_CONNECTION_FAILED: + LOGE("Server connection failed"); + return false; + case EVENT_SERVER_CONNECTED: + LOGD("Server connected"); + return true; + default: + break; + } + } + + LOGE("SDL_WaitEvent() error: %s", SDL_GetError()); + return false; +} + static SDL_LogPriority sdl_priority_from_av_level(int level) { switch (level) { @@ -263,20 +293,26 @@ stream_on_eos(struct stream *stream, void *userdata) { static void server_on_connection_failed(struct server *server, void *userdata) { - struct scrcpy *scrcpy = userdata; + (void) server; + (void) userdata; + PUSH_EVENT(EVENT_SERVER_CONNECTION_FAILED); } static void server_on_connected(struct server *server, void *userdata) { - struct scrcpy *scrcpy = userdata; + (void) server; + (void) userdata; + PUSH_EVENT(EVENT_SERVER_CONNECTED); } static void server_on_disconnected(struct server *server, void *userdata) { - struct scrcpy *scrcpy = userdata; + (void) server; + (void) userdata; + PUSH_EVENT(EVENT_SERVER_DISCONNECTED); } bool @@ -325,7 +361,7 @@ scrcpy(struct scrcpy_options *options) { .on_connected = server_on_connected, .on_disconnected = server_on_disconnected, }; - if (!server_init(&s->server, ¶ms, &cbs, s)) { + if (!server_init(&s->server, ¶ms, &cbs, NULL)) { return false; } @@ -340,12 +376,13 @@ scrcpy(struct scrcpy_options *options) { goto end; } - struct server_info info; - - if (!server_connect_to(&s->server, &info)) { + // Await for server without blocking Ctrl+C handling + if (!await_for_server()) { goto end; } + struct server_info *info = &s->server.info; + if (options->display && options->control) { if (!file_handler_init(&s->file_handler, options->serial, options->push_target)) { @@ -369,7 +406,7 @@ scrcpy(struct scrcpy_options *options) { if (!recorder_init(&s->recorder, options->record_filename, options->record_format, - info.frame_size)) { + info->frame_size)) { goto end; } rec = &s->recorder; @@ -415,11 +452,11 @@ scrcpy(struct scrcpy_options *options) { if (options->display) { const char *window_title = - options->window_title ? options->window_title : info.device_name; + options->window_title ? options->window_title : info->device_name; struct screen_params screen_params = { .window_title = window_title, - .frame_size = info.frame_size, + .frame_size = info->frame_size, .always_on_top = options->always_on_top, .window_x = options->window_x, .window_y = options->window_y, @@ -443,7 +480,7 @@ scrcpy(struct scrcpy_options *options) { #ifdef HAVE_V4L2 if (options->v4l2_device) { if (!sc_v4l2_sink_init(&s->v4l2_sink, options->v4l2_device, - info.frame_size, options->v4l2_buffer)) { + info->frame_size, options->v4l2_buffer)) { goto end; } diff --git a/app/src/server.c b/app/src/server.c index 482fe3b1..3962c057 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -418,17 +418,13 @@ static int run_server_connect(void *data) { struct server *server = data; - char device_name[DEVICE_NAME_FIELD_LENGTH]; - struct sc_size frame_size; - - if (!server_connect_to(server, device_name, &frame_size)) { + if (!server_connect_to(server, &server->info)) { server->cbs->on_connection_failed(server, server->cbs_userdata); goto end; } server->connected = true; - server->cbs->on_connected(server, device_name, frame_size, - server->cbs_userdata); + server->cbs->on_connected(server, server->cbs_userdata); end: return 0; diff --git a/app/src/server.h b/app/src/server.h index 472f4210..f09db9c5 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -49,7 +49,9 @@ struct server { sc_mutex mutex; sc_cond process_terminated_cond; bool process_terminated; + bool connected; // written by connect_thread + struct server_info info; // initialized once connected sc_socket server_socket; // only used if !tunnel_forward sc_socket video_socket;