diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c index 67418b9aa5b7397673f1ec022eb73b2bc58d7622..61d5b86c50383c4bc7fe63cd929748e24621ce6f 100644 --- a/src/module/module_sdl2.c +++ b/src/module/module_sdl2.c @@ -38,6 +38,7 @@ struct module_sdl2_window { volatile int mpv_duration; /* Because don't need to be persistent */ volatile int state; volatile int hinib; + volatile int set_seek; /* Thread related */ struct poller_thread self; @@ -55,6 +56,7 @@ struct module_sdl2_window { static bool module_sdl2_get_elapsed(struct module_sdl2_window *, int *); static bool module_sdl2_get_duration(struct module_sdl2_window *, int *); static bool module_sdl2_set_volume(struct module_sdl2_window *, int); +static bool module_sdl2_set_position(struct module_sdl2_window *, int); static bool module_sdl2_load_file(struct module_sdl2_window *, const char *); static bool module_sdl2_toggle_pause(struct module_sdl2_window *); static void module_sdl2_free(struct module_sdl2_window *); @@ -196,6 +198,19 @@ mod_set_volume(va_list *va) return ! ret; } +static int +mod_set_position(va_list *va) +{ + va_list copy; + struct module_sdl2_window **win; + va_copy(copy, *va); + win = (struct module_sdl2_window **) va_arg(copy, void **); + int seconds = va_arg(copy, int); + bool ret = module_sdl2_set_position(*win, seconds); + va_end(copy); + return ! ret; +} + static int mod_get_duration(va_list *va) { @@ -235,7 +250,7 @@ REG_ADD_NAMED("load", mod_load_file) REG_ADD_NAMED("set_volume", mod_set_volume) REG_ADD_NAMED("get_duration", mod_get_duration) REG_ADD_NAMED("get_elapsed", mod_get_elapsed) -/* Add "set_position" */ +REG_ADD_NAMED("set_position", mod_set_position) REG_END() #if ! defined (LKT_STATIC_MODULE) REG_EXPORT(sdl2_reg) @@ -388,7 +403,7 @@ loop: if (event.type == wakeup_on_mpv_events) { lmpv_handle(&module, (mpv_handle *) sdl2->mpv, sdl2->queue, &sdl2->mpv_time_pos, &sdl2->mpv_duration, - &sdl2->state, &sdl2->hinib); + &sdl2->state, &sdl2->hinib, &sdl2->set_seek); } } @@ -434,8 +449,9 @@ module_sdl2_new(struct module_sdl2_window **win, struct queue *queue, RETURN_UNLESS(*win, "Out of memory", false); memset(*win, 0, sizeof(struct module_sdl2_window)); - (*win)->queue = queue; - (*win)->db = db; + (*win)->queue = queue; + (*win)->db = db; + (*win)->set_seek = -1; /* Start the SDL thread */ arg = malloc(sizeof(struct poller_thread_arg)); @@ -505,6 +521,14 @@ module_sdl2_set_volume(struct module_sdl2_window *win, int vol) return ! lmpv_set_volume((mpv_handle *) win->mpv, vol); } +static bool +module_sdl2_set_position(struct module_sdl2_window *win, int sec) +{ + RETURN_UNLESS(win && win->window, "Invalid arguments", false); + win->set_seek = sec; + return true; +} + static bool module_sdl2_get_duration(struct module_sdl2_window *win, int *dur_sec) { diff --git a/src/module/mpv.c b/src/module/mpv.c index 43fa8117bc65d150428841d238d6e3dfbdf63a5d..24130c237fbb12dd04e81030712305a8f51b13fc 100644 --- a/src/module/mpv.c +++ b/src/module/mpv.c @@ -105,6 +105,22 @@ error: return NULL; } +int +lmpv_set_position(mpv_handle *ctx, int pos) +{ + RETURN_UNLESS(ctx, "Missing mpv ctx", 1); + int status; + char str[LKT_LINE_MAX]; + memset(str, 0, LKT_LINE_MAX * sizeof(char)); + safe_snprintf(str, LKT_LINE_MAX, "%d", pos); + const char *cmd[] = {"seek", str, "absolute", NULL}; + if ((status = mpv_command_async(ctx, 0, cmd)) < 0) { + LOG_ERROR("WINDOW", "Failed to execute command: %s", mpv_error_string(status)); + return 1; + } + return 0; +} + int lmpv_set_volume(mpv_handle *ctx, int vol) { @@ -156,7 +172,7 @@ lmpv_toggle_pause(mpv_handle *ctx) int lmpv_handle(struct lkt_module *mod, mpv_handle *ctx, struct queue *queue, volatile int *time_pos, volatile int *time_duration, - volatile int *state, volatile int *hinib) + volatile int *state, volatile int *hinib, volatile int *set_seek) { size_t ao_volume; mpv_event *event = NULL; @@ -257,5 +273,13 @@ loop: LOG_WARN("WINDOW", "Unhandled mpv event '%s'", mpv_event_name(event->event_id)); break; } - goto loop; /* A loop without indentation. Ugly but better for not-really-wide screens */ + + /* Can I seek? */ + if (*set_seek >= 0 && *state != STATE_STOP) { + *set_seek = -1; + LOG_DEBUG("WINDOW", "Ignored set_seek"); + } + + /* A loop without indentation. Ugly but better for not-really-wide screens */ + goto loop; } diff --git a/src/module/mpv.h b/src/module/mpv.h index c285bcbfd2cf69d69eef7f34239d219bb23d9027..d50182e6f11b6c252132f27a33b798a58dec2b2e 100644 --- a/src/module/mpv.h +++ b/src/module/mpv.h @@ -12,13 +12,14 @@ int lmpv_observe_properties(mpv_handle *ctx); mpv_handle *lmpv_prepare(volatile sqlite3 *db); int lmpv_set_volume(mpv_handle *ctx, int vol); +int lmpv_set_position(mpv_handle *ctx, int pos); int lmpv_load_file(mpv_handle *ctx, const char *file); int lmpv_toggle_pause(mpv_handle *ctx); /* TODO: place all pointers inside a struct */ int lmpv_handle(struct lkt_module *mod, mpv_handle *ctx, struct queue *queue, volatile int *time_pos, volatile int *time_duration, - volatile int *state, volatile int *hinib); + volatile int *state, volatile int *hinib, volatile int *set_seek); enum { STATE_STOP = 0,