From 4f7da53bb4875fc4045678afcf818af16ffd8ff3 Mon Sep 17 00:00:00 2001 From: NamedKitten Date: Thu, 20 Dec 2018 14:37:29 +0000 Subject: [PATCH] [Backend] Moved backends into their own folder. --- CMakeLists.txt | 5 +- .../DirectMPV/DirectMPVBackend.cpp} | 74 +++++++++---------- .../DirectMPV/DirectMPVBackend.hpp} | 14 ++-- .../MPV/MPVBackend.cpp} | 64 ++++++++-------- .../MPV/MPVBackend.hpp} | 16 ++-- src/enums.hpp | 4 +- src/main.cpp | 18 +++-- src/qml/main.qml | 5 ++ src/qmldebugger.cpp | 33 +++++++++ src/qmldebugger.h | 16 ++++ 10 files changed, 153 insertions(+), 96 deletions(-) rename src/{DirectMpvPlayerBackend.cpp => Backends/DirectMPV/DirectMPVBackend.cpp} (88%) rename src/{DirectMpvPlayerBackend.h => Backends/DirectMPV/DirectMPVBackend.hpp} (92%) rename src/{MpvPlayerBackend.cpp => Backends/MPV/MPVBackend.cpp} (91%) rename src/{MpvPlayerBackend.h => Backends/MPV/MPVBackend.hpp} (91%) create mode 100644 src/qmldebugger.cpp create mode 100644 src/qmldebugger.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e20aae8..b0ba17b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,16 +37,17 @@ endif(X11_FOUND AND Xext_FOUND) set(SOURCES src/main.cpp - src/DirectMpvPlayerBackend.cpp + src/Backends/DirectMPV/DirectMPVBackend.cpp src/utils.cpp src/enums.hpp src/Process.cpp src/ThumbnailCache.cpp src/logger.cpp + src/qmldebugger.cpp ) if(MPV_VERSION VERSION_GREATER_EQUAL "1.28.0") -set(SOURCES ${SOURCES} src/MpvPlayerBackend.cpp) +set(SOURCES ${SOURCES} src/Backends/MPV/MPVBackend.cpp) else() add_definitions(-DDISABLE_MpvPlayerBackend) endif(MPV_VERSION VERSION_GREATER_EQUAL "1.28.0") diff --git a/src/DirectMpvPlayerBackend.cpp b/src/Backends/DirectMPV/DirectMPVBackend.cpp similarity index 88% rename from src/DirectMpvPlayerBackend.cpp rename to src/Backends/DirectMPV/DirectMPVBackend.cpp index d1e2944..1894c79 100644 --- a/src/DirectMpvPlayerBackend.cpp +++ b/src/Backends/DirectMPV/DirectMPVBackend.cpp @@ -2,9 +2,9 @@ #include #include -#include "DirectMpvPlayerBackend.h" +#include "src/Backends/DirectMPV/DirectMPVBackend.hpp" -#include "utils.hpp" +#include "src/utils.hpp" #include #include #include @@ -17,7 +17,7 @@ void wakeup(void* ctx) { - QCoreApplication::postEvent((DirectMpvPlayerBackend*)ctx, + QCoreApplication::postEvent((DirectMPVBackend*)ctx, new QEvent(QEvent::User)); } @@ -66,7 +66,7 @@ MpvRenderer::paint() window->resetOpenGLState(); } -DirectMpvPlayerBackend::DirectMpvPlayerBackend(QQuickItem* parent) +DirectMPVBackend::DirectMPVBackend(QQuickItem* parent) : QQuickItem(parent) , mpv_gl(0) , renderer(0) @@ -117,27 +117,27 @@ DirectMpvPlayerBackend::DirectMpvPlayerBackend(QQuickItem* parent) if (!mpv_gl) throw std::runtime_error("OpenGL not compiled in"); mpv_opengl_cb_set_update_callback( - mpv_gl, DirectMpvPlayerBackend::on_update, (void*)this); + mpv_gl, DirectMPVBackend::on_update, (void*)this); connect(this, - &DirectMpvPlayerBackend::onUpdate, + &DirectMPVBackend::onUpdate, this, - &DirectMpvPlayerBackend::doUpdate, + &DirectMPVBackend::doUpdate, Qt::QueuedConnection); connect(this, - &DirectMpvPlayerBackend::positionChanged, - &DirectMpvPlayerBackend::updateDurationString); + &DirectMPVBackend::positionChanged, + &DirectMPVBackend::updateDurationString); connect(this, - &DirectMpvPlayerBackend::durationChanged, - &DirectMpvPlayerBackend::updateDurationString); + &DirectMPVBackend::durationChanged, + &DirectMPVBackend::updateDurationString); connect(this, &QQuickItem::windowChanged, this, - &DirectMpvPlayerBackend::handleWindowChanged); + &DirectMPVBackend::handleWindowChanged); } -DirectMpvPlayerBackend::~DirectMpvPlayerBackend() +DirectMPVBackend::~DirectMPVBackend() { printf("Shutting down...\n"); qApp->quit(); @@ -145,7 +145,7 @@ DirectMpvPlayerBackend::~DirectMpvPlayerBackend() } void -DirectMpvPlayerBackend::sync() +DirectMPVBackend::sync() { if (!renderer) { @@ -163,13 +163,13 @@ DirectMpvPlayerBackend::sync() } void -DirectMpvPlayerBackend::swapped() +DirectMPVBackend::swapped() { mpv_opengl_cb_report_flip(mpv_gl, 0); } void -DirectMpvPlayerBackend::cleanup() +DirectMPVBackend::cleanup() { if (renderer) { delete renderer; @@ -178,45 +178,45 @@ DirectMpvPlayerBackend::cleanup() } void -DirectMpvPlayerBackend::on_update(void* ctx) +DirectMPVBackend::on_update(void* ctx) { - DirectMpvPlayerBackend* self = (DirectMpvPlayerBackend*)ctx; + DirectMPVBackend* self = (DirectMPVBackend*)ctx; emit self->onUpdate(); } void -DirectMpvPlayerBackend::doUpdate() +DirectMPVBackend::doUpdate() { window()->update(); update(); } QVariant -DirectMpvPlayerBackend::getProperty(const QString& name) const +DirectMPVBackend::getProperty(const QString& name) const { return mpv::qt::get_property_variant(mpv, name); } void -DirectMpvPlayerBackend::command(const QVariant& params) +DirectMPVBackend::command(const QVariant& params) { mpv::qt::command_variant(mpv, params); } void -DirectMpvPlayerBackend::setProperty(const QString& name, const QVariant& value) +DirectMPVBackend::setProperty(const QString& name, const QVariant& value) { mpv::qt::set_property_variant(mpv, name, value); } void -DirectMpvPlayerBackend::setOption(const QString& name, const QVariant& value) +DirectMPVBackend::setOption(const QString& name, const QVariant& value) { mpv::qt::set_option_variant(mpv, name, value); } void -DirectMpvPlayerBackend::launchAboutQt() +DirectMPVBackend::launchAboutQt() { QApplication* qapp = qobject_cast(QCoreApplication::instance()); @@ -224,13 +224,13 @@ DirectMpvPlayerBackend::launchAboutQt() } QVariant -DirectMpvPlayerBackend::playerCommand(const Enums::Commands& cmd) +DirectMPVBackend::playerCommand(const Enums::Commands& cmd) { return playerCommand(cmd, QVariant("NoArgProvided")); } QVariant -DirectMpvPlayerBackend::playerCommand(const Enums::Commands& cmd, +DirectMPVBackend::playerCommand(const Enums::Commands& cmd, const QVariant& args) { switch (cmd) { @@ -392,37 +392,37 @@ DirectMpvPlayerBackend::playerCommand(const Enums::Commands& cmd, } void -DirectMpvPlayerBackend::handleWindowChanged(QQuickWindow* win) +DirectMPVBackend::handleWindowChanged(QQuickWindow* win) { if (!win) return; connect(win, &QQuickWindow::beforeSynchronizing, this, - &DirectMpvPlayerBackend::sync, + &DirectMPVBackend::sync, Qt::DirectConnection); connect(win, &QQuickWindow::sceneGraphInvalidated, this, - &DirectMpvPlayerBackend::cleanup, + &DirectMPVBackend::cleanup, Qt::DirectConnection); connect(win, &QQuickWindow::frameSwapped, this, - &DirectMpvPlayerBackend::swapped, + &DirectMPVBackend::swapped, Qt::DirectConnection); win->setClearBeforeRendering(false); } void -DirectMpvPlayerBackend::toggleOnTop() +DirectMPVBackend::toggleOnTop() { onTop = !onTop; Utils::AlwaysOnTop(window()->winId(), onTop); } bool -DirectMpvPlayerBackend::event(QEvent* event) +DirectMPVBackend::event(QEvent* event) { if (event->type() == QEvent::User) { on_mpv_events(); @@ -431,7 +431,7 @@ DirectMpvPlayerBackend::event(QEvent* event) } void -DirectMpvPlayerBackend::on_mpv_events() +DirectMPVBackend::on_mpv_events() { while (mpv) { mpv_event* event = mpv_wait_event(mpv, 0); @@ -443,7 +443,7 @@ DirectMpvPlayerBackend::on_mpv_events() } void -DirectMpvPlayerBackend::updateDurationString(int numTime) +DirectMPVBackend::updateDurationString(int numTime) { QVariant speed = getProperty("speed"); QMetaMethod metaMethod = sender()->metaObject()->method(senderSignalIndex()); @@ -474,13 +474,13 @@ DirectMpvPlayerBackend::updateDurationString(int numTime) } void -DirectMpvPlayerBackend::updateAppImage() +DirectMPVBackend::updateAppImage() { Utils::updateAppImage(); } QVariantMap -DirectMpvPlayerBackend::getAudioDevices() const +DirectMPVBackend::getAudioDevices() const { QVariant drivers = getProperty("audio-device-list"); QVariant currentDevice = getProperty("audio-device"); @@ -502,7 +502,7 @@ DirectMpvPlayerBackend::getAudioDevices() const } void -DirectMpvPlayerBackend::handle_mpv_event(mpv_event* event) +DirectMPVBackend::handle_mpv_event(mpv_event* event) { switch (event->event_id) { case MPV_EVENT_PROPERTY_CHANGE: { diff --git a/src/DirectMpvPlayerBackend.h b/src/Backends/DirectMPV/DirectMPVBackend.hpp similarity index 92% rename from src/DirectMpvPlayerBackend.h rename to src/Backends/DirectMPV/DirectMPVBackend.hpp index d06a348..572e56b 100644 --- a/src/DirectMpvPlayerBackend.h +++ b/src/Backends/DirectMPV/DirectMPVBackend.hpp @@ -1,5 +1,5 @@ -#ifndef DirectMpvPlayerBackend_H -#define DirectMpvPlayerBackend_H +#ifndef DirectMPVBackend_H +#define DirectMPVBackend_H #include #include @@ -11,8 +11,8 @@ #include #include -#include "backendinterface.hpp" -#include "enums.hpp" +#include "src/backendinterface.hpp" +#include "src/enums.hpp" class MpvRenderer : public QObject { @@ -31,7 +31,7 @@ public slots: void paint(); }; -class DirectMpvPlayerBackend +class DirectMPVBackend : public QQuickItem , public BackendInterface { @@ -62,8 +62,8 @@ public: } bool logging() const { return m_logging; } - DirectMpvPlayerBackend(QQuickItem* parent = 0); - virtual ~DirectMpvPlayerBackend(); + DirectMPVBackend(QQuickItem* parent = 0); + virtual ~DirectMPVBackend(); public slots: QVariant playerCommand(const Enums::Commands& command, const QVariant& args); diff --git a/src/MpvPlayerBackend.cpp b/src/Backends/MPV/MPVBackend.cpp similarity index 91% rename from src/MpvPlayerBackend.cpp rename to src/Backends/MPV/MPVBackend.cpp index d44b86e..657bb96 100644 --- a/src/MpvPlayerBackend.cpp +++ b/src/Backends/MPV/MPVBackend.cpp @@ -1,6 +1,6 @@ -#include "MpvPlayerBackend.h" -#include "logger.h" -#include "utils.hpp" +#include "src/Backends/MPV/MPVBackend.hpp" +#include "src/logger.h" +#include "src/utils.hpp" #include #include #include @@ -27,14 +27,14 @@ namespace { void wakeup(void* ctx) { - QCoreApplication::postEvent((MpvPlayerBackend*)ctx, new QEvent(QEvent::User)); + QCoreApplication::postEvent((MPVBackend*)ctx, new QEvent(QEvent::User)); } void on_mpv_redraw(void* ctx) { QMetaObject::invokeMethod( - reinterpret_cast(ctx), "update", Qt::QueuedConnection); + reinterpret_cast(ctx), "update", Qt::QueuedConnection); } static void* @@ -53,10 +53,10 @@ get_proc_address_mpv(void* ctx, const char* name) class MpvRenderer : public QQuickFramebufferObject::Renderer { - MpvPlayerBackend* obj; + MPVBackend* obj; public: - MpvRenderer(MpvPlayerBackend* new_obj) + MpvRenderer(MPVBackend* new_obj) : obj{ new_obj } {} @@ -124,7 +124,7 @@ public: } }; -MpvPlayerBackend::MpvPlayerBackend(QQuickItem* parent) +MPVBackend::MPVBackend(QQuickItem* parent) : QQuickFramebufferObject(parent) , mpv{ mpv_create() } , mpv_gl(nullptr) @@ -171,23 +171,23 @@ MpvPlayerBackend::MpvPlayerBackend(QQuickItem* parent) throw std::runtime_error("could not initialize mpv context"); connect(this, - &MpvPlayerBackend::onUpdate, + &MPVBackend::onUpdate, this, - &MpvPlayerBackend::doUpdate, + &MPVBackend::doUpdate, Qt::QueuedConnection); connect(this, - &MpvPlayerBackend::positionChanged, + &MPVBackend::positionChanged, this, - &MpvPlayerBackend::updateDurationString, + &MPVBackend::updateDurationString, Qt::QueuedConnection); connect(this, - &MpvPlayerBackend::durationChanged, + &MPVBackend::durationChanged, this, - &MpvPlayerBackend::updateDurationString, + &MPVBackend::updateDurationString, Qt::QueuedConnection); } -MpvPlayerBackend::~MpvPlayerBackend() +MPVBackend::~MPVBackend() { printf("Shutting down...\n"); Utils::SetDPMS(true); @@ -198,33 +198,33 @@ MpvPlayerBackend::~MpvPlayerBackend() } void -MpvPlayerBackend::on_update(void* ctx) +MPVBackend::on_update(void* ctx) { - MpvPlayerBackend* self = (MpvPlayerBackend*)ctx; + MPVBackend* self = (MPVBackend*)ctx; emit self->onUpdate(); } void -MpvPlayerBackend::doUpdate() +MPVBackend::doUpdate() { update(); } QVariant -MpvPlayerBackend::getProperty(const QString& name) const +MPVBackend::getProperty(const QString& name) const { return mpv::qt::get_property_variant(mpv, name); } void -MpvPlayerBackend::command(const QVariant& params) +MPVBackend::command(const QVariant& params) { mpv::qt::node_builder node(params); mpv_command_node(mpv, node.node(), nullptr); } void -MpvPlayerBackend::setProperty(const QString& name, const QVariant& value) +MPVBackend::setProperty(const QString& name, const QVariant& value) { mpv::qt::node_builder node(value); qDebug() << "Setting property" << name << "to" << value; @@ -232,19 +232,19 @@ MpvPlayerBackend::setProperty(const QString& name, const QVariant& value) } void -MpvPlayerBackend::setOption(const QString& name, const QVariant& value) +MPVBackend::setOption(const QString& name, const QVariant& value) { mpv::qt::set_option_variant(mpv, name, value); } QVariant -MpvPlayerBackend::playerCommand(const Enums::Commands& cmd) +MPVBackend::playerCommand(const Enums::Commands& cmd) { return playerCommand(cmd, QVariant("NoArgProvided")); } QVariant -MpvPlayerBackend::playerCommand(const Enums::Commands& cmd, +MPVBackend::playerCommand(const Enums::Commands& cmd, const QVariant& args) { switch (cmd) { @@ -414,14 +414,14 @@ MpvPlayerBackend::playerCommand(const Enums::Commands& cmd, } void -MpvPlayerBackend::toggleOnTop() +MPVBackend::toggleOnTop() { onTop = !onTop; Utils::AlwaysOnTop(window()->winId(), onTop); } bool -MpvPlayerBackend::event(QEvent* event) +MPVBackend::event(QEvent* event) { if (event->type() == QEvent::User) { on_mpv_events(); @@ -430,7 +430,7 @@ MpvPlayerBackend::event(QEvent* event) } void -MpvPlayerBackend::on_mpv_events() +MPVBackend::on_mpv_events() { while (mpv) { mpv_event* event = mpv_wait_event(mpv, 0); @@ -442,7 +442,7 @@ MpvPlayerBackend::on_mpv_events() } void -MpvPlayerBackend::updateDurationString(int numTime) +MPVBackend::updateDurationString(int numTime) { QVariant speed = getProperty("speed"); QMetaMethod metaMethod = sender()->metaObject()->method(senderSignalIndex()); @@ -473,7 +473,7 @@ MpvPlayerBackend::updateDurationString(int numTime) } QVariantMap -MpvPlayerBackend::getAudioDevices(const QVariant& drivers) const +MPVBackend::getAudioDevices(const QVariant& drivers) const { QVariantMap newDrivers; @@ -486,7 +486,7 @@ MpvPlayerBackend::getAudioDevices(const QVariant& drivers) const } void -MpvPlayerBackend::handle_mpv_event(mpv_event* event) +MPVBackend::handle_mpv_event(mpv_event* event) { switch (event->event_id) { case MPV_EVENT_PROPERTY_CHANGE: { @@ -590,10 +590,10 @@ MpvPlayerBackend::handle_mpv_event(mpv_event* event) } QQuickFramebufferObject::Renderer* -MpvPlayerBackend::createRenderer() const +MPVBackend::createRenderer() const { window()->setIcon(QIcon(":/icon.png")); window()->setPersistentOpenGLContext(true); window()->setPersistentSceneGraph(true); - return new MpvRenderer(const_cast(this)); + return new MpvRenderer(const_cast(this)); } diff --git a/src/MpvPlayerBackend.h b/src/Backends/MPV/MPVBackend.hpp similarity index 91% rename from src/MpvPlayerBackend.h rename to src/Backends/MPV/MPVBackend.hpp index 324330f..9efc040 100644 --- a/src/MpvPlayerBackend.h +++ b/src/Backends/MPV/MPVBackend.hpp @@ -1,5 +1,5 @@ -#ifndef MpvPlayerBackend_H -#define MpvPlayerBackend_H +#ifndef MPVBackend_H +#define MPVBackend_H #include #include @@ -11,13 +11,13 @@ #include #include -#include "backendinterface.hpp" -#include "enums.hpp" -#include "utils.hpp" +#include "src/backendinterface.hpp" +#include "src/enums.hpp" +#include "src/utils.hpp" class MpvRenderer; -class MpvPlayerBackend +class MPVBackend : public QQuickFramebufferObject , public BackendInterface { @@ -42,8 +42,8 @@ class MpvPlayerBackend public: static void on_update(void* ctx); - MpvPlayerBackend(QQuickItem* parent = 0); - virtual ~MpvPlayerBackend(); + MPVBackend(QQuickItem* parent = 0); + virtual ~MPVBackend(); virtual Renderer* createRenderer() const; void setLogging(bool a) diff --git a/src/enums.hpp b/src/enums.hpp index aa5b505..3a2e5c5 100644 --- a/src/enums.hpp +++ b/src/enums.hpp @@ -50,8 +50,8 @@ Q_ENUM_NS(Commands) enum class Backends : int { - MpvBackend = 0, - DirectMpvBackend = 1 + MPVBackend = 0, + DirectMPVBackend = 1 }; Q_ENUM_NS(Backends) diff --git a/src/main.cpp b/src/main.cpp index 90d55a9..8286aff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,9 @@ -#include "DirectMpvPlayerBackend.h" +#include "Backends/DirectMPV/DirectMPVBackend.hpp" #ifndef DISABLE_MpvPlayerBackend -#include "MpvPlayerBackend.h" +#include "Backends/MPV/MPVBackend.hpp" #endif +#include "qmldebugger.h" #include "enums.hpp" #include "logger.h" #include "utils.hpp" @@ -159,14 +160,15 @@ main(int argc, char* argv[]) qRegisterMetaType("Enums.Commands"); qmlRegisterType("player", 1, 0, "Process"); + qmlRegisterType("player", 1, 0, "QMLDebugger"); qmlRegisterType("player", 1, 0, "ThumbnailCache"); qmlRegisterType("player", 1, 0, "Utils"); if (backendString == "mpv") { - backend = Enums::Backends::MpvBackend; + backend = Enums::Backends::MPVBackend; } else if (backendString == "direct-mpv") { - backend = Enums::Backends::DirectMpvBackend; + backend = Enums::Backends::DirectMPVBackend; } else { launcherLogger->error("Invalid backend {}.", backendString.toUtf8().constData()); @@ -176,9 +178,9 @@ main(int argc, char* argv[]) launcherLogger->info("Using backend={}", backendString.toUtf8().constData()); switch (backend) { - case Enums::Backends::MpvBackend: { + case Enums::Backends::MPVBackend: { #ifndef DISABLE_MpvPlayerBackend - qmlRegisterType("player", 1, 0, "PlayerBackend"); + qmlRegisterType("player", 1, 0, "PlayerBackend"); #else qDebug() << "Normal MPV backend not available, resetting backend option " "to blank."; @@ -187,8 +189,8 @@ main(int argc, char* argv[]) #endif break; } - case Enums::Backends::DirectMpvBackend: { - qmlRegisterType("player", 1, 0, "PlayerBackend"); + case Enums::Backends::DirectMPVBackend: { + qmlRegisterType("player", 1, 0, "PlayerBackend"); break; } } diff --git a/src/qml/main.qml b/src/qml/main.qml index 33658f9..56e79f3 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -20,6 +20,10 @@ Window { property bool onTop: false + QMLDebugger { + id: qmlDebugger + } + function getAppearanceValueForTheme(themeName, name) { if (themeName == "YouTube") { return youTubeAppearance[name] @@ -223,6 +227,7 @@ Window { } function startPlayer() { + console.info(qmlDebugger.properties(player)) console.info("OwO!") var args = Qt.application.arguments diff --git a/src/qmldebugger.cpp b/src/qmldebugger.cpp new file mode 100644 index 0000000..2da1a5e --- /dev/null +++ b/src/qmldebugger.cpp @@ -0,0 +1,33 @@ +#include "qmldebugger.h" + +#include +#include + +QString QMLDebugger::properties(QQuickItem *item, bool linebreak) +{ + const QMetaObject *meta = item->metaObject(); + + QHash list; + for (int i = 0; i < meta->propertyCount(); i++) + { + QMetaProperty property = meta->property(i); + const char* name = property.name(); + QVariant value = item->property(name); + list[name] = value; + } + + QString out; + QHashIterator i(list); + while (i.hasNext()) { + i.next(); + if (!out.isEmpty()) + { + out += ", "; + if (linebreak) out += "\n"; + } + out.append(i.key()); + out.append(": "); + out.append(i.value().toString()); + } + return out; +} diff --git a/src/qmldebugger.h b/src/qmldebugger.h new file mode 100644 index 0000000..00dac2c --- /dev/null +++ b/src/qmldebugger.h @@ -0,0 +1,16 @@ +#ifndef QMLDEBUGGER_H +#define QMLDEBUGGER_H + +#include +#include + +class QMLDebugger : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE static QString properties(QQuickItem *item, bool linebreak += true); + +}; + +#endif // QMLDEBUGGER_H