diff --git a/inc/lektor/defines.h b/inc/lektor/defines.h index 600d4b503c1157c510fa4f94eb0d5cc77939c79a..e67248cc8c80926ec17a21570baf8fb3240bb2c1 100644 --- a/inc/lektor/defines.h +++ b/inc/lektor/defines.h @@ -14,15 +14,10 @@ #define LKT_DATABASE_NAME_KLANG "language" #define LKT_DATABASE_KARA_COLUMNT_ANY "any_col" -#define LEKTOR_TAG_MAX 256 - -#ifndef LKT_MESSAGE_ARGS_MAX -#define LKT_MESSAGE_ARGS_MAX 32 -#endif - -#ifndef LKT_MESSAGE_MAX -#define LKT_MESSAGE_MAX 2048 -#endif +#define LEKTOR_TAG_MAX 256 +#define LKT_MESSAGE_ARGS_MAX 32 +#define LKT_MESSAGE_MAX 2048 +#define LKT_DEFAULT_LIST_SIZE 10 enum mpd_idle_flag { MPD_IDLE_NONE = 0, diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c index 4c4fa4cb0dd703974b921078ace3f45ea17b3f36..1be1ec622b26b231bc78dc8dfd732ef112e9e7d1 100644 --- a/src/module/module_sdl2.c +++ b/src/module/module_sdl2.c @@ -18,6 +18,8 @@ #define WIDTH 400 #define HEIGHT 200 +static Uint32 wakeup_on_mpv_render_update, wakeup_on_mpv_events; + struct module_sdl2_window { /* Related to SDL2 */ SDL_Window *window; @@ -33,6 +35,7 @@ struct module_sdl2_window { /* Thread related */ pthread_mutex_t mtx; struct lkt_thread self; + volatile int launched; }; /* Thread related functions */ @@ -40,10 +43,14 @@ struct module_sdl2_window { static void * sdl_thread__(struct lkt_thread_arg *arg) { + struct lkt_win *win = arg->args; + struct lkt_thread *self = arg->self; struct module_sdl2_window *sdl2 = win->window; SDL_Event event; uint64_t flags, redraw = 0; int w, h; + free(arg); + (void) self; if (sdl2 == NULL) return false; @@ -90,9 +97,7 @@ loop: if (flags & MPV_RENDER_UPDATE_FRAME) redraw = 1; } - /* Handle mpv events. */ - if (event.type == wakeup_on_mpv_events) - lmpv_handle(win, sdl2->mpv, db, mpd_idle_events, (int *) &sdl2->mpv_time_pos, (int *) &sdl2->mpv_duration); + /* Handle mpv events are not done here. */ } if (redraw) { @@ -122,8 +127,6 @@ loop: /* Private functions. */ -static Uint32 wakeup_on_mpv_render_update, wakeup_on_mpv_events; - static inline void * get_proc_address_mpv(void *fn_ctx, const char *name) { @@ -178,11 +181,7 @@ init_mpv__(mpv_handle **ctx) { *ctx = lmpv_prepare(); int status; - if ((status = mpv_initialize(*ctx)) < 0) { - fprintf(stderr, " ! init_mpv__: failed to initialize mpv: %s\n", - mpv_error_string(status)); - return 1; - } + RETURN_IF((status = mpv_initialize(*ctx)) < 0, mpv_error_string(status), 1); RETURN_UNLESS(lmpv_observe_properties(*ctx), "Observe properties failed", 1); return 0; } @@ -191,11 +190,8 @@ static inline bool init_mpv_gl__(mpv_handle *mpv, mpv_render_context **mpv_gl, mpv_render_param *params) { int status; - if ((status = mpv_render_context_create(mpv_gl, mpv, params)) < 0) { - fprintf(stderr, " ! init_mpv_gl__: Failed to create render context: %s\n", - mpv_error_string(status)); - return 1; - } + RETURN_IF((status = mpv_render_context_create(mpv_gl, mpv, params)) < 0, + mpv_error_string(status), 1); wakeup_on_mpv_render_update = SDL_RegisterEvents(1); wakeup_on_mpv_events = SDL_RegisterEvents(1); @@ -222,11 +218,7 @@ module_sdl2_new(struct lkt_win *const win) if (sdl2 == NULL) { sdl2 = calloc(1, sizeof(struct module_sdl2_window)); memset(sdl2, 0, sizeof(struct module_sdl2_window)); - - if (sdl2 == NULL) { - fprintf(stderr, " ! module_sdl2_new: failed to allocate window, no memory\n"); - return false; - } + RETURN_UNLESS(sdl2, "Out of memory", false); /* Yeah, this is how it is done. */ pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; @@ -234,22 +226,17 @@ module_sdl2_new(struct lkt_win *const win) /* CREATE THE SDL2 WINDOW TODO */ SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "no"); /* It seems that sdl sucks. */ - if (SDL_Init(SDL_INIT_VIDEO) < 0) - goto error; + RETURN_IF(SDL_Init(SDL_INIT_VIDEO) < 0, "Failed to init SDL", false); sdl2->window = SDL_CreateWindow("lektord", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); - if (!sdl2->window) - goto error; - + RETURN_UNLESS(sdl2->window, "Failed to create the window", false); sdl2->glcontext = SDL_GL_CreateContext(sdl2->window); - if (!sdl2->glcontext) - goto error; + RETURN_UNLESS(sdl2->glcontext, "Failed to create the SDL context", false); } /* Init mpv here */ - if (init_mpv__(&sdl2->mpv)) - goto error; + RETURN_IF(init_mpv__(&sdl2->mpv), "Failed to init mpv", false); mpv_render_param params[] = { { MPV_RENDER_PARAM_API_TYPE, MPV_RENDER_API_TYPE_OPENGL }, @@ -259,7 +246,8 @@ module_sdl2_new(struct lkt_win *const win) .get_proc_address = get_proc_address_mpv, } }, - { // Can't use mpv_command with that thing, should change to mpv_command_async + { + // Can't use mpv_command with that thing, should change to mpv_command_async MPV_RENDER_PARAM_ADVANCED_CONTROL, &(int) { 1 @@ -269,15 +257,22 @@ module_sdl2_new(struct lkt_win *const win) }; /* Init mpv_gl here */ - if (init_mpv_gl__(sdl2->mpv, &sdl2->mpv_gl, params)) - goto error; + RETURN_IF(init_mpv_gl__(sdl2->mpv, &sdl2->mpv_gl, params), "Failed to init mpv_gl", false); + + /* 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; + RETURN_IF(lkt_th_new(&sdl2->self, LKT_DEFAULT_LIST_SIZE, sdl_thread__, arg), + "Failed to launch the SDL thread", false); + } + /* Finish */ SDL_SetWindowTitle(sdl2->window, "Lektord"); SDL_DisableScreenSaver(); win->window = sdl2; return true; -error: - return false; } void @@ -342,80 +337,8 @@ 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; - - /* 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; - - 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; - } - /* Handle mpv events. */ - if (event.type == wakeup_on_mpv_events) - lmpv_handle(win, sdl2->mpv, db, mpd_idle_events, (int *) &sdl2->mpv_time_pos, (int *) &sdl2->mpv_duration); - } - - 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) - { - .fbo = 0, .w = w, .h = h, - } - }, { - MPV_RENDER_PARAM_FLIP_Y, &(int) - { - 1 - } - }, {0} - }; - mpv_render_context_render(sdl2->mpv_gl, params); - SDL_GL_SwapWindow(sdl2->window); - redraw = 0; - } - goto loop; /* A loop without indentation. */ + RETURN_UNLESS(sdl2, "Can't handle events from a NULL window", false); + return ! lmpv_handle(win, sdl2->mpv, db, mpd_idle_events, + (int *) &sdl2->mpv_time_pos, + (int *) &sdl2->mpv_duration); } diff --git a/src/repo/async.c b/src/repo/async.c index 8fdb2ce7756dffcb677eb6ccc215f48f40060d81..7b604e9bc9828e54b0486174f174e95fb7295570 100644 --- a/src/repo/async.c +++ b/src/repo/async.c @@ -9,8 +9,6 @@ #include <lektor/thread.h> #include <limits.h> -#define LKT_DEFAULT_LIST_SIZE 10 - static struct lkt_thread repo_thread; static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;