diff --git a/inc/lektor/net.h b/inc/lektor/net.h index e7d244bb356414064b60588d5479ff8a87fd0998..4b83feda24a62c8b2ab57fa5c45804ee142c34ac 100644 --- a/inc/lektor/net.h +++ b/inc/lektor/net.h @@ -1,6 +1,7 @@ #pragma once #include <common/common.h> +#include <common/queue.h> #include <lektor/mkv.h> #include <lektor/config.h> #include <lektor/window.h> @@ -73,6 +74,7 @@ struct lkt_state { size_t fds_max; char host[HOST_NAME_MAX]; char port[INI_MAX_LINE_LEN]; + mqd_t queue; volatile sqlite3 *db; const char *kara_prefix; diff --git a/meson.build b/meson.build index 4c1dc2a02fdf4f1bf3b746db0b67e809d3bfe5d8..9d2322971f33735e289aff57c7ddd71b860f017b 100644 --- a/meson.build +++ b/meson.build @@ -13,6 +13,7 @@ project( 'lektor' ) libdl = meson.get_compiler('c').find_library('dl') +librt = meson.get_compiler('c').find_library('rt') dep_x11 = dependency('x11', required: false) dep_mpv = dependency('mpv', required: false) dep_sdl = dependency('sdl2', required: false) @@ -96,6 +97,8 @@ core_deps = [ dependency('sqlite3', version: '>= 3.31.0') , dependency('libcurl', required: true) , dependency('json-c', required: true) , dependency('threads', required: true) + , librt + , libdl ] manpath = custom_target( 'manpath' @@ -117,7 +120,7 @@ lib = both_libraries( 'lektor' , xxd.process('src/database/memory.sql') , manpath , include_directories: includes - , dependencies: [ core_deps, libdl, common_deps ] ) + , dependencies: [ core_deps, common_deps ] ) if get_option('static_liblektor') bin_deps = [ declare_dependency( link_with: lib.get_static_lib(), include_directories: includes) ] diff --git a/src/main/server.c b/src/main/server.c index 1e611eb08f7108220dc17357a2df13aa2e1d2dec..3ba06e9e2e374bd7fcbae1d4c1fad35b7271284a 100644 --- a/src/main/server.c +++ b/src/main/server.c @@ -79,9 +79,12 @@ normal_launch: executable_name = exe; /* Init the server */ - struct lkt_state srv; char *db_path = safe_malloc(PATH_MAX * sizeof(char)); char *kara_dir = safe_malloc(PATH_MAX * sizeof(char)); + struct lkt_state srv = { + .queue = lkt_queue_open(), + .kara_prefix = kara_dir, + }; memset(&srv, 0, sizeof(struct lkt_state)); /* Initialize the system. */ @@ -123,6 +126,14 @@ normal_launch: RETURN_IF(load_module_by_name(&srv, "player", &srv.win), "Can't load module player", 3); RETURN_IF(load_module_by_name(&srv, "repo", &srv.repo), "Can't load module repo", 3); + /* Get ENV */ + /* Not working -> race condition with player module */ + char *env_current = env_get(LKT_ENV_CURRENT); + if (env_current && !STR_MATCH(env_current, "NULL")) { + LOG_INFO_SCT("INIT", "Restart playback from %s", env_current); + lkt_queue_send(lkt_event_play_pos, (void *) (size_t) strtol(env_current, NULL, 0)); + } + lkt_listen(&srv); return EXIT_FAILURE; } diff --git a/src/net/listen.c b/src/net/listen.c index 21c74840950285cf82a7d4b950a2fc7b2de8cfc6..e87bcebf708bd943622669115a08a6e94c212263 100644 --- a/src/net/listen.c +++ b/src/net/listen.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <common/common.h> +#include <common/queue.h> #include <lektor/commands.h> #include <lektor/database.h> #include <lektor/net.h> @@ -713,6 +714,27 @@ lkt_client_auth(struct lkt_state *srv, size_t c, bool set) return srv->clients[c - 1].authentificated |= set; } +static inline void +handle_queue_events(struct lkt_state *srv) +{ + lkt_event evt; + char string[BUFFER_MAX]; /* TODO: A less dirty bomb */ +redo: + evt = lkt_queue_handle(srv->queue); + + switch (evt.type) { + case lkt_event_play_pos: + safe_snprintf(string, BUFFER_MAX, "%ld", (size_t) evt.attr); + command_play(srv->db, &srv->win, (char **) &string, &srv->mpd_idle_events); + break; + + case lkt_event_null: + default: + return; + } + goto redo; +} + void lkt_listen(struct lkt_state *srv) { @@ -727,14 +749,6 @@ lkt_listen(struct lkt_state *srv) srv->fds[0].events = POLLIN; srv->fds_len = 1; - /* Get ENV */ - /* Not working -> race condition with player module */ - // char *env_current = env_get(LKT_ENV_CURRENT); - // if (env_current && !STR_MATCH(env_current, "NULL")) { - // LOG_INFO_SCT("INIT", "Restart playback from %s", env_current); - // command_play(srv->db, &srv->win, &env_current, &srv->mpd_idle_events); - // } - /* Listen */ for (;;) { if (handle_network_events(srv) < 0) @@ -742,5 +756,6 @@ lkt_listen(struct lkt_state *srv) if (handle_idle_events(srv) < 0) break; srv->win.handle_events(&srv->win, srv->db, &srv->mpd_idle_events); + handle_queue_events(srv); } } diff --git a/src/queue.c b/src/queue.c index bd6585311bcae957fbb0b4022023944958a092cf..07bec454643fba428de45675bbf6bb361fca6b14 100644 --- a/src/queue.c +++ b/src/queue.c @@ -27,7 +27,7 @@ lkt_queue_open(void) exit(EXIT_FAILURE); default: - LOG_ERROR_SCT("QUEUE", "Unhandled error '%d'", errno); + LOG_ERROR_SCT("QUEUE", "Unhandled error '%s' (%d)", strerror(errno), errno); exit(EXIT_FAILURE); }