File indexing completed on 2025-02-23 04:35:17

0001 // SPDX-FileCopyrightText: 2019 Linus Jahn <lnj@kaidan.im>
0002 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
0003 // SPDX-License-Identifier: GPL-3.0-or-later
0004 
0005 #include "mpvobject.h"
0006 
0007 #include <MpvController>
0008 
0009 #include <QDir>
0010 #include <QOpenGLContext>
0011 #include <QOpenGLFramebufferObject>
0012 #include <QQuickWindow>
0013 #include <QStandardPaths>
0014 
0015 MpvObject::MpvObject(QQuickItem *parent)
0016     : MpvAbstractItem(parent)
0017 {
0018     setProperty(QStringLiteral("terminal"), QStringLiteral("yes"));
0019     setProperty(QStringLiteral("keep-open"), QStringLiteral("always"));
0020     setProperty(QStringLiteral("audio-client-name"), QStringLiteral("org.kde.plasmatube"));
0021     setProperty(QStringLiteral("wayland-app-id"), QStringLiteral("org.kde.plasmatube"));
0022 
0023     observeProperty(QStringLiteral("duration"), MPV_FORMAT_DOUBLE);
0024     observeProperty(QStringLiteral("time-pos"), MPV_FORMAT_DOUBLE);
0025     observeProperty(QStringLiteral("pause"), MPV_FORMAT_FLAG);
0026 
0027     connect(mpvController(), &MpvController::propertyChanged, this, &MpvObject::onPropertyChanged, Qt::QueuedConnection);
0028 }
0029 
0030 qreal MpvObject::position() const
0031 {
0032     return m_position;
0033 }
0034 
0035 qreal MpvObject::duration() const
0036 {
0037     return m_duration;
0038 }
0039 
0040 bool MpvObject::paused() const
0041 {
0042     return m_paused;
0043 }
0044 
0045 void MpvObject::play()
0046 {
0047     if (!paused()) {
0048         return;
0049     }
0050     setProperty(QStringLiteral("pause"), false);
0051 }
0052 
0053 void MpvObject::pause()
0054 {
0055     if (paused()) {
0056         return;
0057     }
0058     setProperty(QStringLiteral("pause"), true);
0059 }
0060 
0061 void MpvObject::stop()
0062 {
0063     setPosition(0);
0064     setProperty(QStringLiteral("stop"), true);
0065     m_stopped = true;
0066     Q_EMIT stoppedChanged();
0067 }
0068 
0069 void MpvObject::setPosition(double value)
0070 {
0071     if (value == position()) {
0072         return;
0073     }
0074     setProperty(QStringLiteral("time-pos"), value);
0075     Q_EMIT positionChanged();
0076 }
0077 
0078 void MpvObject::seek(qreal offset)
0079 {
0080     Q_EMIT command(QStringList() << QStringLiteral("add") << QStringLiteral("time-pos") << QString::number(offset));
0081 }
0082 
0083 bool MpvObject::stopped() const
0084 {
0085     return m_stopped;
0086 }
0087 
0088 void MpvObject::onPropertyChanged(const QString &property, const QVariant &value)
0089 {
0090     if (property == QStringLiteral("time-pos")) {
0091         double time = value.toDouble();
0092         m_position = time;
0093         Q_EMIT positionChanged();
0094 
0095         if (m_position > 0.0 && m_stopped) {
0096             m_stopped = false;
0097             Q_EMIT stoppedChanged();
0098         }
0099     } else if (property == QStringLiteral("duration")) {
0100         double time = value.toDouble();
0101         m_duration = time;
0102         Q_EMIT durationChanged();
0103     } else if (property == QStringLiteral("pause")) {
0104         m_paused = value.toBool();
0105         Q_EMIT pausedChanged();
0106     }
0107 }
0108 
0109 #include "moc_mpvobject.cpp"