diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c index 85091c7f3e24bd41467ab89bc6c9bf5951b8e032..95a9dd5db39fc5acca3aab63ab5a8a234f4c447f 100644 --- a/src/module/module_sdl2.c +++ b/src/module/module_sdl2.c @@ -36,6 +36,9 @@ struct module_sdl2_window { pthread_mutex_t mtx; struct lkt_thread self; volatile int launched; + + /* SDL you sucks */ + volatile int is_sdl_init; }; /* Thread related functions */ @@ -43,15 +46,29 @@ struct module_sdl2_window { static void * sdl_thread__(struct lkt_thread_arg *arg) { - struct lkt_win *const win = arg->args; - struct lkt_thread *self = arg->self; - struct module_sdl2_window *sdl2 = win->window; + volatile struct lkt_win *const win = arg->args; + volatile struct module_sdl2_window *sdl2 = win->window; SDL_Event event; uint64_t flags; int w, h, redraw = 0; free(arg); - (void) self; - RETURN_UNLESS(sdl2 && sdl2->window, "Big nope here, the sdl window pointer is NULL", NULL); + + /* Init the SDL window + Yeah, SDL you sucks */ + + SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "no"); /* It seems that sdl sucks. */ + RETURN_IF(SDL_Init(SDL_INIT_VIDEO) < 0, "Failed to init SDL", NULL); + + sdl2->window = SDL_CreateWindow("lektord", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + WIDTH, HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); + RETURN_UNLESS(sdl2->window, "Failed to create the window", NULL); + sdl2->glcontext = SDL_GL_CreateContext((SDL_Window *) sdl2->window); + RETURN_UNLESS(sdl2->glcontext, "Failed to create the SDL context", NULL); + sdl2->is_sdl_init = 1; + + while (!sdl2->launched) + sched_yield(); + fprintf(stderr, " * Started SDL thread\n"); loop: @@ -63,14 +80,17 @@ loop: break; case SDL_WINDOWEVENT: - if (event.window.event == SDL_WINDOWEVENT_EXPOSED) + if (event.window.event == SDL_WINDOWEVENT_EXPOSED) { + fprintf(stderr, " . sdl_thread__: Window exposed\n"); redraw = 1; + } break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_SPACE) { const char *cmd_pause[] = { "cycle", "pause", NULL }; mpv_command_async((mpv_handle *) sdl2->mpv, 0, cmd_pause); + fprintf(stderr, " . sdl_thread__: Toggle pause\n"); } break; @@ -82,6 +102,7 @@ loop: SDL_SetWindowFullscreen((SDL_Window *) sdl2->window, SDL_WINDOW_FULLSCREEN); /* May use SDL_WINDOW_FULLSCREEN_DESKTOP, need to check. */ sdl2->is_fullscreen = 1 - sdl2->is_fullscreen; + fprintf(stderr, " . sdl_thread__: Toggle fullscreen\n"); } break; @@ -204,30 +225,30 @@ module_sdl2_new(struct lkt_win *const win) { RETURN_UNLESS(win, "Invalid arguments", false); - struct module_sdl2_window *sdl2 = win->window; - - if (sdl2 == NULL) { - sdl2 = calloc(1, sizeof(struct module_sdl2_window)); - RETURN_UNLESS(sdl2, "Out of memory", false); - memset(sdl2, 0, sizeof(struct module_sdl2_window)); + if (win->window == NULL) { + win->window = calloc(1, sizeof(struct module_sdl2_window)); + RETURN_UNLESS(win->window, "Out of memory", false); + memset(win->window, 0, sizeof(struct module_sdl2_window)); /* Yeah, this is how it is done. */ pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; - sdl2->mtx = mtx; - - /* CREATE THE SDL2 WINDOW TODO */ - SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "no"); /* It seems that sdl sucks. */ - RETURN_IF(SDL_Init(SDL_INIT_VIDEO) < 0, "Failed to init SDL", false); + ((struct module_sdl2_window *) win->window)->mtx = mtx; - sdl2->window = SDL_CreateWindow("lektord", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - WIDTH, HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); - RETURN_UNLESS(sdl2->window, "Failed to create the window", false); - sdl2->glcontext = SDL_GL_CreateContext((SDL_Window *) sdl2->window); - RETURN_UNLESS(sdl2->glcontext, "Failed to create the SDL context", false); + /* Start the SDL thread */ + struct lkt_thread_arg *arg = calloc(1, sizeof(struct lkt_thread_arg)); + RETURN_UNLESS(arg, "Out of memory", false); + arg->args = win; + RETURN_IF(lkt_th_new(&((struct module_sdl2_window *) win->window)->self, LKT_DEFAULT_LIST_SIZE, sdl_thread__, + arg), + "Failed to launch the SDL thread", false); } + while (!((struct module_sdl2_window *) win->window)->is_sdl_init) + sched_yield(); + /* Init mpv here */ - RETURN_IF(init_mpv__((mpv_handle **) &sdl2->mpv), "Failed to init mpv", false); + RETURN_IF(init_mpv__((mpv_handle **) & ((struct module_sdl2_window *) win->window)->mpv), + "Failed to init mpv", false); mpv_render_param params[] = { { MPV_RENDER_PARAM_API_TYPE, MPV_RENDER_API_TYPE_OPENGL }, @@ -248,24 +269,14 @@ module_sdl2_new(struct lkt_win *const win) }; /* Init mpv_gl here */ - RETURN_IF(init_mpv_gl__((mpv_handle *) sdl2->mpv, (mpv_render_context **) &sdl2->mpv_gl, params), + RETURN_IF(init_mpv_gl__((mpv_handle *) ((struct module_sdl2_window *) win->window)->mpv, + (mpv_render_context **) & ((struct module_sdl2_window *) win->window)->mpv_gl, params), "Failed to init mpv_gl", false); /* Finish */ - SDL_SetWindowTitle((SDL_Window *) sdl2->window, "Lektord"); + SDL_SetWindowTitle((SDL_Window *) ((struct module_sdl2_window *) win->window)->window, "Lektord"); SDL_DisableScreenSaver(); - win->window = sdl2; - - /* Start the SDL thread */ - if (!sdl2->launched) { - struct lkt_thread_arg *arg = calloc(1, sizeof(struct lkt_thread_arg)); - RETURN_UNLESS(arg, "Out of memory", false); - arg->args = win; - sdl2->launched = 1; - RETURN_IF(lkt_th_new(&sdl2->self, LKT_DEFAULT_LIST_SIZE, sdl_thread__, arg), - "Failed to launch the SDL thread", false); - } - + ((struct module_sdl2_window *) win->window)->launched = 1; return true; }