Extract sc_push_event()

Expose a convenience function to push an event without args to the main
thread.
This commit is contained in:
Romain Vimont 2024-09-06 23:08:08 +02:00
parent cdafbec7e3
commit a3405ab62e
6 changed files with 44 additions and 39 deletions

View file

@ -15,6 +15,7 @@ src = [
'src/demuxer.c', 'src/demuxer.c',
'src/device_msg.c', 'src/device_msg.c',
'src/display.c', 'src/display.c',
'src/events.c',
'src/icon.c', 'src/icon.c',
'src/file_pusher.c', 'src/file_pusher.c',
'src/fps_counter.c', 'src/fps_counter.c',

19
app/src/events.c Normal file
View file

@ -0,0 +1,19 @@
#include "events.h"
#include "util/log.h"
bool
sc_push_event_impl(uint32_t type, const char *name) {
SDL_Event event;
event.type = type;
int ret = SDL_PushEvent(&event);
// ret < 0: error (queue full)
// ret == 0: event was filtered
// ret == 1: success
if (ret != 1) {
LOGE("Could not post %s event: %s", name, SDL_GetError());
return false;
}
return true;
}

View file

@ -3,6 +3,8 @@
#include "common.h" #include "common.h"
#include <stdbool.h>
#include <stdint.h>
#include <SDL_events.h> #include <SDL_events.h>
enum { enum {
@ -18,4 +20,9 @@ enum {
SC_EVENT_CONTROLLER_ERROR, SC_EVENT_CONTROLLER_ERROR,
}; };
bool
sc_push_event_impl(uint32_t type, const char *name);
#define sc_push_event(TYPE) sc_push_event_impl(TYPE, # TYPE)
#endif #endif

View file

@ -82,22 +82,10 @@ struct scrcpy {
struct sc_timeout timeout; struct sc_timeout timeout;
}; };
static inline void
push_event(uint32_t type, const char *name) {
SDL_Event event;
event.type = type;
int ret = SDL_PushEvent(&event);
if (ret < 0) {
LOGE("Could not post %s event: %s", name, SDL_GetError());
// What could we do?
}
}
#define PUSH_EVENT(TYPE) push_event(TYPE, # TYPE)
#ifdef _WIN32 #ifdef _WIN32
static BOOL WINAPI windows_ctrl_handler(DWORD ctrl_type) { static BOOL WINAPI windows_ctrl_handler(DWORD ctrl_type) {
if (ctrl_type == CTRL_C_EVENT) { if (ctrl_type == CTRL_C_EVENT) {
PUSH_EVENT(SDL_QUIT); sc_push_event(SDL_QUIT);
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
@ -230,7 +218,7 @@ sc_recorder_on_ended(struct sc_recorder *recorder, bool success,
(void) userdata; (void) userdata;
if (!success) { if (!success) {
PUSH_EVENT(SC_EVENT_RECORDER_ERROR); sc_push_event(SC_EVENT_RECORDER_ERROR);
} }
} }
@ -244,9 +232,9 @@ sc_video_demuxer_on_ended(struct sc_demuxer *demuxer,
assert(status != SC_DEMUXER_STATUS_DISABLED); assert(status != SC_DEMUXER_STATUS_DISABLED);
if (status == SC_DEMUXER_STATUS_EOS) { if (status == SC_DEMUXER_STATUS_EOS) {
PUSH_EVENT(SC_EVENT_DEVICE_DISCONNECTED); sc_push_event(SC_EVENT_DEVICE_DISCONNECTED);
} else { } else {
PUSH_EVENT(SC_EVENT_DEMUXER_ERROR); sc_push_event(SC_EVENT_DEMUXER_ERROR);
} }
} }
@ -260,11 +248,11 @@ sc_audio_demuxer_on_ended(struct sc_demuxer *demuxer,
// Contrary to the video demuxer, keep mirroring if only the audio fails // Contrary to the video demuxer, keep mirroring if only the audio fails
// (unless --require-audio is set). // (unless --require-audio is set).
if (status == SC_DEMUXER_STATUS_EOS) { if (status == SC_DEMUXER_STATUS_EOS) {
PUSH_EVENT(SC_EVENT_DEVICE_DISCONNECTED); sc_push_event(SC_EVENT_DEVICE_DISCONNECTED);
} else if (status == SC_DEMUXER_STATUS_ERROR } else if (status == SC_DEMUXER_STATUS_ERROR
|| (status == SC_DEMUXER_STATUS_DISABLED || (status == SC_DEMUXER_STATUS_DISABLED
&& options->require_audio)) { && options->require_audio)) {
PUSH_EVENT(SC_EVENT_DEMUXER_ERROR); sc_push_event(SC_EVENT_DEMUXER_ERROR);
} }
} }
@ -277,9 +265,9 @@ sc_controller_on_ended(struct sc_controller *controller, bool error,
(void) userdata; (void) userdata;
if (error) { if (error) {
PUSH_EVENT(SC_EVENT_CONTROLLER_ERROR); sc_push_event(SC_EVENT_CONTROLLER_ERROR);
} else { } else {
PUSH_EVENT(SC_EVENT_DEVICE_DISCONNECTED); sc_push_event(SC_EVENT_DEVICE_DISCONNECTED);
} }
} }
@ -288,7 +276,7 @@ sc_server_on_connection_failed(struct sc_server *server, void *userdata) {
(void) server; (void) server;
(void) userdata; (void) userdata;
PUSH_EVENT(SC_EVENT_SERVER_CONNECTION_FAILED); sc_push_event(SC_EVENT_SERVER_CONNECTION_FAILED);
} }
static void static void
@ -296,7 +284,7 @@ sc_server_on_connected(struct sc_server *server, void *userdata) {
(void) server; (void) server;
(void) userdata; (void) userdata;
PUSH_EVENT(SC_EVENT_SERVER_CONNECTED); sc_push_event(SC_EVENT_SERVER_CONNECTED);
} }
static void static void
@ -314,7 +302,7 @@ sc_timeout_on_timeout(struct sc_timeout *timeout, void *userdata) {
(void) timeout; (void) timeout;
(void) userdata; (void) userdata;
PUSH_EVENT(SC_EVENT_TIME_LIMIT_REACHED); sc_push_event(SC_EVENT_TIME_LIMIT_REACHED);
} }
// Generate a scrcpy id to differentiate multiple running scrcpy instances // Generate a scrcpy id to differentiate multiple running scrcpy instances

View file

@ -306,13 +306,9 @@ sc_screen_frame_sink_open(struct sc_frame_sink *sink,
screen->frame_size.width = ctx->width; screen->frame_size.width = ctx->width;
screen->frame_size.height = ctx->height; screen->frame_size.height = ctx->height;
static SDL_Event event = {
.type = SC_EVENT_SCREEN_INIT_SIZE,
};
// Post the event on the UI thread (the texture must be created from there) // Post the event on the UI thread (the texture must be created from there)
int ret = SDL_PushEvent(&event); bool ok = sc_push_event(SC_EVENT_SCREEN_INIT_SIZE);
if (ret < 0) { if (!ok) {
LOGW("Could not post init size event: %s", SDL_GetError()); LOGW("Could not post init size event: %s", SDL_GetError());
return false; return false;
} }
@ -352,13 +348,9 @@ sc_screen_frame_sink_push(struct sc_frame_sink *sink, const AVFrame *frame) {
// The SC_EVENT_NEW_FRAME triggered for the previous frame will consume // The SC_EVENT_NEW_FRAME triggered for the previous frame will consume
// this new frame instead // this new frame instead
} else { } else {
static SDL_Event new_frame_event = {
.type = SC_EVENT_NEW_FRAME,
};
// Post the event on the UI thread // Post the event on the UI thread
int ret = SDL_PushEvent(&new_frame_event); bool ok = sc_push_event(SC_EVENT_NEW_FRAME);
if (ret < 0) { if (!ok) {
LOGW("Could not post new frame event: %s", SDL_GetError()); LOGW("Could not post new frame event: %s", SDL_GetError());
return false; return false;
} }

View file

@ -21,10 +21,8 @@ sc_usb_on_disconnected(struct sc_usb *usb, void *userdata) {
(void) usb; (void) usb;
(void) userdata; (void) userdata;
SDL_Event event; bool ok = sc_push_event(SC_EVENT_USB_DEVICE_DISCONNECTED);
event.type = SC_EVENT_USB_DEVICE_DISCONNECTED; if (!ok) {
int ret = SDL_PushEvent(&event);
if (ret < 0) {
LOGE("Could not post USB disconnection event: %s", SDL_GetError()); LOGE("Could not post USB disconnection event: %s", SDL_GetError());
} }
} }