From d2d18466d4bac6cb2d2d9f40ee07519e3342f204 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 30 Oct 2021 22:36:56 +0200 Subject: [PATCH] Factorize SDL event push Add a macro and inline function to call SDL_PushEvent() and log on error (without actually handling the error, because there is nothing we could do). --- app/src/scrcpy.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index d2e35bb2..05f8cba2 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -57,15 +57,22 @@ struct scrcpy { struct input_manager input_manager; }; +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 BOOL WINAPI windows_ctrl_handler(DWORD ctrl_type) { if (ctrl_type == CTRL_C_EVENT) { - SDL_Event event; - event.type = SDL_QUIT; - int ret = SDL_PushEvent(&event); - if (ret < 0) { - LOGW("Could not post SDL_QUIT event: %s", SDL_GetError()); - } + PUSH_EVENT(SDL_QUIT); return TRUE; } return FALSE; @@ -251,13 +258,7 @@ stream_on_eos(struct stream *stream, void *userdata) { (void) stream; (void) userdata; - SDL_Event stop_event; - stop_event.type = EVENT_STREAM_STOPPED; - int ret = SDL_PushEvent(&stop_event); - if (ret < 0) { - LOGE("Could not post stream stopped event: %s", SDL_GetError()); - // XXX What could we do? - } + PUSH_EVENT(EVENT_STREAM_STOPPED); } bool