From 013bb733ccdab861d910c6fa68b1d1e1db65d15b Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Sat, 18 Apr 2020 13:38:43 +0200 Subject: [PATCH] WIP: Fullscreen support --- src/module/module_sdl2.c | 129 ++++++++++++++++++++++----------------- 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c index 2c125274..a334610c 100644 --- a/src/module/module_sdl2.c +++ b/src/module/module_sdl2.c @@ -17,6 +17,7 @@ struct module_sdl2_window { /* Related to SDL2 */ SDL_Window *window; SDL_GLContext glcontext; + int is_fullscreen; /* Mpv related */ mpv_handle *mpv; @@ -196,6 +197,8 @@ module_sdl2_new(struct lkt_win *const win) if (sdl2->mpv_gl == NULL && init_mpv_gl__(sdl2->mpv, &sdl2->mpv_gl, params)) goto error; + SDL_SetWindowTitle(sdl2->window, "Lektord"); + SDL_DisableScreenSaver(); win->window = sdl2; return true; error: @@ -281,70 +284,86 @@ bool module_sdl2_handle_events(struct lkt_win *const win, sqlite3 *db, enum mpd_idle_flag *mpd_idle_events) { struct module_sdl2_window *sdl2 = win->window; + SDL_Event event; + uint64_t flags, redraw = 0; + int w, h; if (sdl2 == NULL) return false; - for (;;) { - SDL_Event event; - int redraw = 0; - if (SDL_WaitEvent(&event) != 1) - return false; + /* SDL events won't change the database, only the mpv one's will. + Do this to be able to put the SDL stuff inside a thread and don't change + to much things. */ +loop: + if (SDL_PollEvent(&event)) + return true; + + switch (event.type) { + case SDL_QUIT: + return true; + + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_EXPOSED) + redraw = 1; + + break; + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_SPACE) { + const char *cmd_pause[] = { "cycle", "pause", NULL }; + mpv_command_async(sdl2->mpv, 0, cmd_pause); + } + break; + + case SDL_KEYUP: + if (event.key.keysym.sym == SDLK_F11) { + if (sdl2->is_fullscreen) { + SDL_SetWindowFullscreen(sdl2->window, 0); + sdl2->is_fullscreen = 0; + } else { + /* May use SDL_WINDOW_FULLSCREEN_DESKTOP, need to check. */ + SDL_SetWindowFullscreen(sdl2->window, SDL_WINDOW_FULLSCREEN); + sdl2->is_fullscreen = 1; + } + } + break; - switch (event.type) { - case SDL_QUIT: - return true; - case SDL_WINDOWEVENT: - if (event.window.event == SDL_WINDOWEVENT_EXPOSED) + default: + if (event.type == wakeup_on_mpv_render_update) { + flags = mpv_render_context_update(sdl2->mpv_gl); + if (flags & MPV_RENDER_UPDATE_FRAME) redraw = 1; - break; - case SDL_KEYDOWN: - if (event.key.keysym.sym == SDLK_SPACE) { - const char *cmd_pause[] = { "cycle", "pause", NULL }; - mpv_command_async(sdl2->mpv, 0, cmd_pause); - } - break; - default: - if (event.type == wakeup_on_mpv_render_update) { - uint64_t flags = mpv_render_context_update(sdl2->mpv_gl); - if (flags & MPV_RENDER_UPDATE_FRAME) - redraw = 1; - } - // Happens when at least 1 new event is in the mpv event queue. - if (event.type == wakeup_on_mpv_events) { - // Handle all remaining mpv events. - for (;;) { - mpv_event *mp_event = mpv_wait_event(sdl2->mpv, 0); - if (mp_event->event_id == MPV_EVENT_NONE) - break; - printf("event: %s\n", mpv_event_name(mp_event->event_id)); - } + } + /* Handle mpv events. */ + if (event.type == wakeup_on_mpv_events) { + for (;;) { + mpv_event *mp_event = mpv_wait_event(sdl2->mpv, 0); + if (mp_event->event_id == MPV_EVENT_NONE) + break; + printf("event: %s\n", mpv_event_name(mp_event->event_id)); } } + } - if (redraw) { - int w, h; - SDL_GetWindowSize(sdl2->window, &w, &h); - mpv_render_param params[] = { - // Specify the default framebuffer (0) as target. This will - // render onto the entire screen. If you want to show the video - // in a smaller rectangle or apply fancy transformations, you'll - // need to render into a separate FBO and draw it manually. + if (redraw) { + SDL_GetWindowSize(sdl2->window, &w, &h); + /* Specify the default framebuffer (0) as target. This will + render onto the entire screen. If you want to show the video + in a smaller rectangle or apply fancy transformations, you'll + need to render into a separate FBO and draw it manually. */ + mpv_render_param params[] = { { + MPV_RENDER_PARAM_OPENGL_FBO, &(mpv_opengl_fbo) { - MPV_RENDER_PARAM_OPENGL_FBO, &(mpv_opengl_fbo) - { - .fbo = 0, .w = w, .h = h, - } - }, + .fbo = 0, .w = w, .h = h, + } + }, { + MPV_RENDER_PARAM_FLIP_Y, &(int) { - MPV_RENDER_PARAM_FLIP_Y, &(int) - { - 1 - } - }, - {0} - }; - mpv_render_context_render(sdl2->mpv_gl, params); - SDL_GL_SwapWindow(sdl2->window); - } + 1 + } + }, {0} + }; + mpv_render_context_render(sdl2->mpv_gl, params); + SDL_GL_SwapWindow(sdl2->window); + redraw = 0; } + goto loop; /* A loop without indentation. */ } -- GitLab