From ad5a107ee1f3bc9712290465825e6f17934271a7 Mon Sep 17 00:00:00 2001 From: Ripose Date: Mon, 8 Jul 2024 18:53:33 -0700 Subject: [PATCH] src/gui/widgets/mpv/mpvwidget: set mpv options from command line Call mpv_set_option_string for every top level option passed to mpv from the commandline. Previously options were only loaded per file, but this makes top-level options persistent. This fixes some compatability issues with third-party software such as the jellyfin-mpv-shim. Related #217 --- src/gui/widgets/mpv/mpvwidget.cpp | 68 +++++++++++++++++++++++++++---- src/gui/widgets/mpv/mpvwidget.h | 5 +++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/gui/widgets/mpv/mpvwidget.cpp b/src/gui/widgets/mpv/mpvwidget.cpp index 60405cb..d441d13 100644 --- a/src/gui/widgets/mpv/mpvwidget.cpp +++ b/src/gui/widgets/mpv/mpvwidget.cpp @@ -453,6 +453,66 @@ void MpvWidget::initTimer() m_cursorTimer->forceTimeout(); } +void MpvWidget::initOptions() +{ + /* Hardcoded options */ + mpv_set_option_string(m_mpv, "config", "yes"); + mpv_set_option_string(m_mpv, "input-default-bindings", "yes"); + mpv_set_option_string(m_mpv, "keep-open", "yes"); + mpv_set_option_string(m_mpv, "screenshot-directory", "~~desktop/"); + mpv_set_option_string(m_mpv, "terminal", "yes"); + mpv_set_option_string(m_mpv, "vo", "libmpv"); + mpv_set_option_string(m_mpv, "ytdl", "yes"); + + /* Commandline options */ + QStringList args = QApplication::arguments(); + for (qsizetype i = 0; i < args.size(); ++i) + { + /* Skip non-options */ + if (!args[i].startsWith("--")) + { + continue; + } + + /* Skip per file options */ + if (args[i] == "--{") + { + qsizetype depth = 1; + ++i; + while (i < args.size() && depth > 0) + { + if (args[i] == "--{") + { + ++depth; + } + else if (args[i] == "--}") + { + --depth; + } + ++i; + } + continue; + } + + QString arg = args[i].last(args[i].size() - 2); + if (arg.startsWith("no-")) + { + arg = arg.last(arg.size() - 3) + .append("=no"); + } + + qsizetype sepIndex = arg.indexOf('='); + if (sepIndex == -1) + { + continue; + } + + QByteArray option = arg.first(sepIndex).toUtf8(); + QByteArray value = arg.mid(sepIndex + 1).toUtf8(); + mpv_set_option_string(m_mpv, option, value); + } +} + /* End Initialization Functions */ /* Begin OpenGL Functions */ @@ -503,13 +563,7 @@ void MpvWidget::initializeGL() ); /* Initialize the mpv context */ - mpv_set_option_string(m_mpv, "config", "yes"); - mpv_set_option_string(m_mpv, "input-default-bindings", "yes"); - mpv_set_option_string(m_mpv, "keep-open", "yes"); - mpv_set_option_string(m_mpv, "screenshot-directory", "~~desktop/"); - mpv_set_option_string(m_mpv, "terminal", "yes"); - mpv_set_option_string(m_mpv, "vo", "libmpv"); - mpv_set_option_string(m_mpv, "ytdl", "yes"); + initOptions(); QByteArray configDir = DirectoryUtils::getConfigDir().toUtf8(); mpv_set_option_string(m_mpv, "config-dir", configDir); diff --git a/src/gui/widgets/mpv/mpvwidget.h b/src/gui/widgets/mpv/mpvwidget.h index fab017b..c541bbe 100644 --- a/src/gui/widgets/mpv/mpvwidget.h +++ b/src/gui/widgets/mpv/mpvwidget.h @@ -321,6 +321,11 @@ private Q_SLOTS: */ void initTimer(); + /** + * Initializes mpv options from the command line. + */ + void initOptions(); + /** * Shows the cursor over this widget. */