diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c index f3792157b19502c0f2cea6f95a6cab2b0e430d99..b3a99e929f7e1dfbbccfed773a45ad5624c2d00a 100644 --- a/src/module/module_sdl2.c +++ b/src/module/module_sdl2.c @@ -36,7 +36,9 @@ struct module_sdl2_window { /* Thread related */ struct poller_thread self; - volatile int launched; /* SDL you sucks */ + volatile int launched; /* SDL you sucks */ + volatile bool is_in_preparation; /* Inside the MPV & SDL init */ + volatile bool has_preparation_failed; /* The init failed */ /* Things from the server */ struct queue *queue; @@ -303,12 +305,17 @@ static void * sdl_thread__(struct poller_thread_arg *arg) { struct module_sdl2_window *sdl2 = arg->args; + int redraw = 0; + bool ctrl = false; + sdl2->has_preparation_failed = true; // For now, will be cleared if no goto to end_of_init + SDL_Event event; uint64_t flags; - int w, h, redraw = 0; - bool ctrl = false; + int w, h; const Uint8 *state; + free(arg); + struct lkt_module module = { .handle = NULL, .reg = sdl2_reg, @@ -342,7 +349,7 @@ sdl_thread__(struct poller_thread_arg *arg) /* Init mpv here */ - RETURN_IF(init_mpv__((mpv_handle **)&sdl2->mpv, sdl2->db), "Failed to init mpv", false); + GOTO_IF(init_mpv__((mpv_handle **)&sdl2->mpv, sdl2->db), "Failed to init mpv", end_of_init); mpv_render_param params[] = { { MPV_RENDER_PARAM_API_TYPE, MPV_RENDER_API_TYPE_OPENGL }, { MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, @@ -352,11 +359,16 @@ sdl_thread__(struct poller_thread_arg *arg) { MPV_RENDER_PARAM_ADVANCED_CONTROL, &(int){ 1 } }, { 0 } }; - RETURN_IF(init_mpv_gl__((mpv_handle *)sdl2->mpv, (mpv_render_context **)&sdl2->mpv_gl, params), - "Failed to init mpv_gl", false); + GOTO_IF(init_mpv_gl__((mpv_handle *)sdl2->mpv, (mpv_render_context **)&sdl2->mpv_gl, params), + "Failed to init mpv_gl", end_of_init); + sdl2->has_preparation_failed = false; +end_of_init: while (!sdl2->launched) sched_yield(); + sdl2->is_in_preparation = 0; + if (sdl2->has_preparation_failed) + pthread_exit(NULL); ___unset_window_title(sdl2); LOG_INFO("WINDOW", "Started SDL thread"); @@ -475,9 +487,10 @@ module_sdl2_new(struct module_sdl2_window **win, struct queue *queue, lkt_db *db RETURN_UNLESS(*win, "Out of memory", false); memset(*win, 0, sizeof(struct module_sdl2_window)); - (*win)->queue = queue; - (*win)->db = db; - (*win)->set_seek = -1; + (*win)->queue = queue; + (*win)->db = db; + (*win)->set_seek = -1; + (*win)->is_in_preparation = true; /* Start the SDL thread */ arg = LKT_ALLOC_STRUCT(poller_thread_arg); @@ -490,7 +503,9 @@ module_sdl2_new(struct module_sdl2_window **win, struct queue *queue, lkt_db *db /* Finish */ SDL_DisableScreenSaver(); (*win)->launched = 1; - return true; + while ((*win)->is_in_preparation) + sched_yield(); + return !((*win)->has_preparation_failed); } static void