diff --git a/inc/lektor/module/mpv.h b/inc/lektor/module/mpv.h index a31a373b9a2b91e4f33342541d7ca2347473be7b..4f97e294e54c9da1956402af090c3ddcb73d9d2b 100644 --- a/inc/lektor/module/mpv.h +++ b/inc/lektor/module/mpv.h @@ -6,3 +6,8 @@ void lmpv_free(mpv_handle **ctx); mpv_handle *lmpv_new(unsigned long int wid); int lmpv_observe_properties(mpv_handle *ctx); mpv_handle *lmpv_prepare(void); + +int lmpv_is_paused(mpv_handle *ctx, int *ret); +int lmpv_set_volume(mpv_handle *ctx, int vol); +int lmpv_load_file(mpv_handle *ctx, const char *file); +int lmpv_toggle_pause(mpv_handle *ctx, int *pause); diff --git a/src/module/module_x11.c b/src/module/module_x11.c index a7885660af5606bd897432bcc8906b4c8e09a16e..1e8470356cfb7c4d951aa8c67c2593fd56c8e251 100644 --- a/src/module/module_x11.c +++ b/src/module/module_x11.c @@ -335,7 +335,6 @@ module_x11_close(struct lkt_win *const win) { if (win == NULL || win->window == NULL) return; - struct module_x11_window *const x11_win = win->window; lmpv_free(&x11_win->mpv); x11_win->child_display = NULL; @@ -365,24 +364,9 @@ module_x11_toggle_pause(struct lkt_win *const win, bool *new_paused) { if (win == NULL || win->window == NULL) return false; - - mpv_handle *ctx = ((struct module_x11_window *) win->window)->mpv; - - if (!ctx) { - fprintf(stderr, " ! module_x11_toggle_pause: failed due to missing mpv ctx\n"); - return false; - } - - const char *cmd[] = {"cycle", "pause", "up", NULL}; - int status; - - if ((status = mpv_command(ctx, cmd)) < 0) { - fprintf(stderr, "mpv_toggle_pause: Failed issue command: %s\n", - mpv_error_string(status)); - return false; - } - - return module_x11_is_paused(win, new_paused); + int pause, ret = ! lmpv_toggle_pause(((struct module_x11_window *) win->window)->mpv, &pause); + *new_paused = pause; + return ret; } bool @@ -390,34 +374,11 @@ module_x11_load_file(struct lkt_win *const win, const char *filepath) { if (win == NULL || win->window == NULL) return false; - struct module_x11_window *xwin = win->window; - mpv_handle *ctx = xwin->mpv; - - if (!ctx) { - fprintf(stderr, " ! module_x11_load_file: failed due to missing mpv ctx\n"); - return false; - } - - if (access(filepath, R_OK)) { - fprintf(stderr, " ! mpv_add_file: Faild to read file: %s\n", filepath); - return false; - } - - const char *cmd[] = {"loadfile", filepath, "replace", NULL}; - int status; - - if ((status = mpv_command(ctx, cmd)) < 0) { - fprintf(stderr, " ! mpv_add_file: Failed to add '%s': %s\n", - filepath, mpv_error_string(status)); - return false; - } - + bool ret = ! lmpv_load_file(xwin->mpv, filepath); xwin->mpv_duration = 0; xwin->mpv_time_pos = 0; - - fprintf(stderr, " . module_x11_load_file: added file %s\n", filepath); - return true; + return ret; } bool @@ -425,27 +386,7 @@ module_x11_set_volume(struct lkt_win *const win, int vol) { if (win == NULL || win->window == NULL) return false; - - mpv_handle *ctx = ((struct module_x11_window *) win->window)->mpv; - - if (!ctx) { - fprintf(stderr, " ! module_x11_set_volume: failed due to missing mpv ctx\n"); - return false; - } - - int status; - char str[5]; - memset(str, 0, 5); - snprintf(str, 4, "%d", vol); - const char *cmd[] = {"set", "ao-volume", str, NULL}; - - if ((status = mpv_command(ctx, cmd)) < 0) { - fprintf(stderr, " ! module_x11_set_volume: Failed to execute command: %s\n", - mpv_error_string(status)); - return false; - } - - return true; + return ! lmpv_set_volume(((struct module_x11_window *) win->window)->mpv, vol); } bool @@ -453,22 +394,9 @@ module_x11_is_paused(struct lkt_win *const win, bool *ret) { if (win == NULL || win->window == NULL) return false; - - mpv_handle *ctx = ((struct module_x11_window *) win->window)->mpv; - int status; - - if (!ctx) { - fprintf(stderr, " ! module_x11_is_paused: failed due to missing mpv ctx\n"); - return false; - } - - if ((status = mpv_get_property(ctx, "pause", MPV_FORMAT_FLAG, &ret)) < 0) { - fprintf(stderr, "mpv_is_paused: Failed to get pause property: %s\n", - mpv_error_string(status)); - return false; - } - - return true; + int ret_, sta = ! lmpv_is_paused(((struct module_x11_window *) win->window)->mpv, &ret_); + *ret = ret_; + return sta; } bool @@ -476,10 +404,8 @@ module_x11_get_duration(struct lkt_win *const win, int *dur_sec) { if (win == NULL || win->window == NULL) return false; - struct module_x11_window *win_x11 = win->window; *dur_sec = win_x11->mpv_duration; - return true; } @@ -488,10 +414,8 @@ module_x11_get_elapsed(struct lkt_win *const win, int *elapsed_sec) { if (win == NULL || win->window == NULL) return false; - struct module_x11_window *win_x11 = win->window; *elapsed_sec = win_x11->mpv_time_pos; - return true; } @@ -500,9 +424,5 @@ module_x11_handle_events(struct lkt_win *const win, sqlite3 *db, enum mpd_idle_f { if (win == NULL || win->window == NULL) return false; - - bool sta = true; - sta &= lx11_handle(win->window); - sta &= lmpv_handle(win, db, mpd_idle_events); - return sta; + return lx11_handle(win->window) && lmpv_handle(win, db, mpd_idle_events); } diff --git a/src/module/mpv.c b/src/module/mpv.c index 2b172bd5d62bee0afd669823db52b54fba70682d..62cc17bec1d89b7028d0bba599e420ff523e80b3 100644 --- a/src/module/mpv.c +++ b/src/module/mpv.c @@ -1,5 +1,7 @@ #include <lektor/module/mpv.h> #include <stdio.h> +#include <string.h> +#include <unistd.h> void lmpv_free(mpv_handle **ctx) @@ -110,3 +112,87 @@ error: lmpv_free(&ctx); return NULL; } + +int +lmpv_is_paused(mpv_handle *ctx, int *ret) +{ + int status; + + if (!ctx) { + fprintf(stderr, " ! lmpv_is_paused: failed due to missing mpv ctx\n"); + return 1; + } + + if ((status = mpv_get_property(ctx, "pause", MPV_FORMAT_FLAG, ret)) < 0) { + fprintf(stderr, "lmpv_is_paused: Failed to get pause property: %s\n", + mpv_error_string(status)); + return 1; + } + + return 0; +} + +int +lmpv_set_volume(mpv_handle *ctx, int vol) +{ + if (!ctx) { + fprintf(stderr, " ! lmpv_set_volume: failed due to missing mpv ctx\n"); + return 1; + } + + int status; + char str[5]; + memset(str, 0, 5); + snprintf(str, 4, "%d", vol); + const char *cmd[] = {"set", "ao-volume", str, NULL}; + + if ((status = mpv_command(ctx, cmd)) < 0) { + fprintf(stderr, " ! lmpv_set_volume: Failed to execute command: %s\n", + mpv_error_string(status)); + return 1; + } + return 0; +} + +int +lmpv_load_file(mpv_handle *ctx, const char *file) +{ + if (!ctx) { + fprintf(stderr, " ! lmpv_load_file: failed due to missing mpv ctx\n"); + return 1; + } + + if (access(file, R_OK)) { + fprintf(stderr, " ! lmpv_load_file: Faild to read file: %s\n", file); + return 1; + } + + const char *cmd[] = {"loadfile", file, "replace", NULL}; + int status; + + if ((status = mpv_command(ctx, cmd)) < 0) { + fprintf(stderr, " ! lmpv_load_file: Failed to add '%s': %s\n", + file, mpv_error_string(status)); + return 1; + } + return 0; +} + +int +lmpv_toggle_pause(mpv_handle *ctx, int *pause) +{ + if (!ctx) { + fprintf(stderr, " ! lmpv_toggle_pause: failed due to missing mpv ctx\n"); + return 1; + } + + const char *cmd[] = {"cycle", "pause", "up", NULL}; + int status; + + if ((status = mpv_command(ctx, cmd)) < 0) { + fprintf(stderr, "lmpv_toggle_pause: Failed issue command: %s\n", + mpv_error_string(status)); + return 1; + } + return lmpv_is_paused(ctx, pause); +}