diff --git a/app/src/events.h b/app/src/events.h index a4d6f3df..abe1a72c 100644 --- a/app/src/events.h +++ b/app/src/events.h @@ -1,2 +1,4 @@ -#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) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 9e14d3c6..adc22e23 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -217,6 +217,29 @@ 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)) { + 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) { @@ -264,20 +287,27 @@ 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; + LOGD("Server disconnected"); + // Do nothing, will be managed by the "stream stopped" event } bool @@ -333,7 +363,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; } @@ -355,12 +385,13 @@ scrcpy(struct scrcpy_options *options) { sdl_configure(options->display, options->disable_screensaver); - 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)) { @@ -384,7 +415,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; @@ -430,11 +461,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, @@ -458,7 +489,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 318c07a4..dd5e6168 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -361,6 +361,7 @@ connect_to_server(struct server *server, uint32_t attempts, sc_tick delay) { // it worked! return socket; } + // TODO use mutex + condvar + bool stopped if (attempts) { sc_mutex_lock(&server->mutex); // Ignore timedwait return (spurious wake ups are harmless) @@ -437,9 +438,7 @@ static int run_server_connect(void *data) { struct server *server = data; - struct server_info info; - - if (!server_connect_to(server, &info)) { + if (!server_connect_to(server, &server->info)) { server->cbs->on_connection_failed(server, server->cbs_userdata); goto end; } diff --git a/app/src/server.h b/app/src/server.h index a17f5efa..695b9266 100644 --- a/app/src/server.h +++ b/app/src/server.h @@ -55,6 +55,7 @@ struct server { bool stopped; 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;