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/device_msg.c',
'src/display.c',
'src/events.c',
'src/icon.c',
'src/file_pusher.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 <stdbool.h>
#include <stdint.h>
#include <SDL_events.h>
enum {
@ -18,4 +20,9 @@ enum {
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

View file

@ -82,22 +82,10 @@ struct scrcpy {
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
static BOOL WINAPI windows_ctrl_handler(DWORD ctrl_type) {
if (ctrl_type == CTRL_C_EVENT) {
PUSH_EVENT(SDL_QUIT);
sc_push_event(SDL_QUIT);
return TRUE;
}
return FALSE;
@ -230,7 +218,7 @@ sc_recorder_on_ended(struct sc_recorder *recorder, bool success,
(void) userdata;
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);
if (status == SC_DEMUXER_STATUS_EOS) {
PUSH_EVENT(SC_EVENT_DEVICE_DISCONNECTED);
sc_push_event(SC_EVENT_DEVICE_DISCONNECTED);
} 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
// (unless --require-audio is set).
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
|| (status == SC_DEMUXER_STATUS_DISABLED
&& 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;
if (error) {
PUSH_EVENT(SC_EVENT_CONTROLLER_ERROR);
sc_push_event(SC_EVENT_CONTROLLER_ERROR);
} 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) userdata;
PUSH_EVENT(SC_EVENT_SERVER_CONNECTION_FAILED);
sc_push_event(SC_EVENT_SERVER_CONNECTION_FAILED);
}
static void
@ -296,7 +284,7 @@ sc_server_on_connected(struct sc_server *server, void *userdata) {
(void) server;
(void) userdata;
PUSH_EVENT(SC_EVENT_SERVER_CONNECTED);
sc_push_event(SC_EVENT_SERVER_CONNECTED);
}
static void
@ -314,7 +302,7 @@ sc_timeout_on_timeout(struct sc_timeout *timeout, void *userdata) {
(void) timeout;
(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

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.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)
int ret = SDL_PushEvent(&event);
if (ret < 0) {
bool ok = sc_push_event(SC_EVENT_SCREEN_INIT_SIZE);
if (!ok) {
LOGW("Could not post init size event: %s", SDL_GetError());
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
// this new frame instead
} else {
static SDL_Event new_frame_event = {
.type = SC_EVENT_NEW_FRAME,
};
// Post the event on the UI thread
int ret = SDL_PushEvent(&new_frame_event);
if (ret < 0) {
bool ok = sc_push_event(SC_EVENT_NEW_FRAME);
if (!ok) {
LOGW("Could not post new frame event: %s", SDL_GetError());
return false;
}

View file

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