Sélectionner une révision Git
mpv.c 6,18 Kio
#define _POSIX_C_SOURCE 200809L
#include <common/common.h>
#include <lektor/module/mpv.h>
#include <lektor/commands.h>
#include <lektor/database.h>
#include <strings.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sqlite3.h>
void
lmpv_free(mpv_handle **ctx)
{
RETURN_UNLESS(ctx && *ctx, "Missing mpv ctx", NOTHING);
static const char *cmd[] = {"quit", NULL};
mpv_command(*ctx, cmd);
mpv_destroy(*ctx);
*ctx = NULL;
LOG_INFO_SCT("WINDOW", "%s", "The mpv context was destroyed");
}
mpv_handle *
lmpv_prepare(void)
{
mpv_handle *ctx = mpv_create();
int status;
RETURN_UNLESS(ctx, "Failed to create context", NULL);
#define MPV_SET_OPTION(opt, value) \
if ((status = mpv_set_option_string(ctx, opt, value)) < 0) { \
LOG_ERROR_SCT("WINDOW", "Failed to set %s to %s: %s", \
opt, value, mpv_error_string(status)); \
return NULL; \
}
MPV_SET_OPTION("input-default-bindings", "yes");
MPV_SET_OPTION("input-vo-keyboard", "yes");
MPV_SET_OPTION("replaygain", "track");
MPV_SET_OPTION("gpu-context", "x11");
MPV_SET_OPTION("demuxer-readahead-secs", "5.0");
MPV_SET_OPTION("demuxer-max-bytes", "100M");
MPV_SET_OPTION("hwdec", "yes");
return ctx;
}
int
lmpv_observe_properties(mpv_handle *ctx)
{
return (mpv_observe_property(ctx, 0, "ao-volume", MPV_FORMAT_INT64) >= 0) &&
(mpv_observe_property(ctx, 0, "duration", MPV_FORMAT_INT64) >= 0) &&
(mpv_observe_property(ctx, 0, "time-pos", MPV_FORMAT_INT64) >= 0) &&
(mpv_observe_property(ctx, 0, "pause", MPV_FORMAT_FLAG) >= 0);
}
mpv_handle *
lmpv_new(unsigned long int wid)
{
mpv_handle *ctx = lmpv_prepare();
int status;
if ((status = mpv_set_property(ctx, "wid", MPV_FORMAT_INT64, &wid)) < 0) {
LOG_ERROR_SCT("WINDOW", "Failed to set wid: %s", mpv_error_string(status));
goto error;
}
if ((status = mpv_initialize(ctx)) < 0) {
LOG_ERROR_SCT("WINDOW", "Failed to init mpv: %s", mpv_error_string(status));
goto error;
}
if (!lmpv_observe_properties(ctx)) {
LOG_ERROR_SCT("WINDOW", "%s", "Failed to observe properties");
goto error;
}
LOG_ERROR_SCT("WINDOW", "%s", "Create mpv context");
return ctx;
error:
lmpv_free(&ctx);
return NULL;
}
int
lmpv_set_volume(mpv_handle *ctx, int vol)
{
RETURN_UNLESS(ctx, "Missing mpv ctx", 1);
int status;
char str[5];
memset(str, 0, 5);
safe_snprintf(str, 4, "%d", vol);
const char *cmd[] = {"set", "ao-volume", str, NULL};
if ((status = mpv_command_async(ctx, 0, cmd)) < 0) {
LOG_ERROR_SCT("WINDOW", "Failed to execute command: %s", mpv_error_string(status));
return 1;
}
return 0;
}
int
lmpv_load_file(mpv_handle *ctx, const char *file)
{
RETURN_UNLESS(ctx, "Missing mpv ctx", 1);
RETURN_IF(access(file, R_OK), "Failed to read file", 1);
const char *cmd1[] = { "loadfile", file, "replace", NULL };
const char *cmd2[] = { "set", "pause", "0", NULL };
int status;
if ((status = mpv_command_async(ctx, 0, cmd1)) < 0) {
LOG_ERROR_SCT("WINDOW", "Failed to add '%s': %s", file, mpv_error_string(status));
return 1;
}
if ((status = mpv_command_async(ctx, 0, cmd2)) < 0) {
LOG_ERROR_SCT("WINDOW", "Failed to set state to play: %s", mpv_error_string(status));
return 1;
}
return 0;
}
int
lmpv_toggle_pause(mpv_handle *ctx)
{
RETURN_UNLESS(ctx, "Missing mpv ctx", 1);
const char *cmd[] = {"cycle", "pause", "up", NULL};
int status;
if ((status = mpv_command_async(ctx, 0, cmd)) < 0) {
LOG_ERROR_SCT("WINDOW", "Failed issus command: %s", mpv_error_string(status));
return 1;
}
return 0;
}
int
lmpv_handle(struct lkt_win *win, mpv_handle *ctx, volatile sqlite3 *db, mpd_idle_flag *mpd_idle_events,
volatile int *time_pos, volatile int *time_duration)
{
int ao_volume;
struct lkt_queue_state state;
mpv_event *event = NULL;
mpv_event_property *prop;
RETURN_UNLESS(db && mpd_idle_events && ctx && win, "Invalid argument", 1);
RETURN_UNLESS(database_queue_state(db, &state), "Failed to get queue state", 1);
loop:
event = mpv_wait_event(ctx, 0);
switch (event->event_id) {
case MPV_EVENT_PAUSE:
case MPV_EVENT_UNPAUSE:
database_queue_toggle_pause(db);
break;
case MPV_EVENT_SHUTDOWN:
*time_pos = (*time_duration = 0);
database_queue_stop(db);
win->close(win);
return 1;
case MPV_EVENT_NONE:
return 1;
case MPV_EVENT_IDLE:
if (state.current > 0 && *time_pos > 0)
command_next(db, win, mpd_idle_events);
break;
case MPV_EVENT_PROPERTY_CHANGE:
prop = (mpv_event_property *) event->data;
if (prop->format == MPV_FORMAT_NONE)
break;
// MPV volume (BUG: The flag is not MPV_FORMAT_NONE only at the end of the song...) //
if (STR_MATCH(prop->name, "ao-volume")
&& prop->format == MPV_FORMAT_INT64) {
ao_volume = *(int *) prop->data;
database_config_queue(db, "volume", ao_volume);
}
// File duration //
if (STR_MATCH(prop->name, "duration")
&& prop->format == MPV_FORMAT_INT64) {
*time_duration = *(int *) prop->data;
database_config_queue(db, "duration", *(int *) prop->data);
}
if (STR_MATCH(prop->name, "time-pos")
&& prop->format == MPV_FORMAT_INT64) {
*time_pos = *(int *) prop->data;
database_config_queue(db, "elapsed", *(int *) prop->data);
}
// Pause state //
if (STR_MATCH(prop->name, "pause")
&& prop->format == MPV_FORMAT_FLAG)
database_queue_set_paused(db, *(bool *) prop->data);
break;
/* Ignored */
case MPV_EVENT_VIDEO_RECONFIG:
case MPV_EVENT_AUDIO_RECONFIG:
case MPV_EVENT_COMMAND_REPLY:
break;
default:
LOG_WARN_SCT("WINDOW", "Unhandled mpv event '%s'", mpv_event_name(event->event_id));
break;
}
goto loop; /* A loop without indentation. */
}