diff options
author | Chris Xiong <chirs241097@gmail.com> | 2023-11-26 01:10:36 -0500 |
---|---|---|
committer | Chris Xiong <chirs241097@gmail.com> | 2023-11-26 01:10:36 -0500 |
commit | 60989e52b3f3bc0a95d3e61bd8e59fa4d9b7ab83 (patch) | |
tree | af08e5b6f7019c6f70bf3800a419ec78db140988 /mpris-plugin/qmpriswrapper.cpp | |
parent | 382d85b15ce9cc4580a2522b39f5dd4ce43a24b0 (diff) | |
download | QMidiPlayer-60989e52b3f3bc0a95d3e61bd8e59fa4d9b7ab83.tar.xz |
The 2 year constipation. (mpris plugin)
Probably buggy as hell.
Diffstat (limited to 'mpris-plugin/qmpriswrapper.cpp')
-rw-r--r-- | mpris-plugin/qmpriswrapper.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/mpris-plugin/qmpriswrapper.cpp b/mpris-plugin/qmpriswrapper.cpp new file mode 100644 index 0000000..8a3f273 --- /dev/null +++ b/mpris-plugin/qmpriswrapper.cpp @@ -0,0 +1,77 @@ +#include "qmpriswrapper.hpp" +#include "qmprisdbusinterface.hpp" +#include <QDBusMetaType> +#include <QDBusConnection> +#include <QDBusMessage> + +#include "../include/qmpcorepublic.hpp" + +QMPrisWrapper::QMPrisWrapper(QString serviceSuffix, qmpPluginAPI *_api, QObject *parent) : + api(_api), + QObject(parent), + svcsuffix(serviceSuffix) +{ + qDBusRegisterMetaType<QStringList>(); + qDBusRegisterMetaType<QVariantMap>(); + qDBusRegisterMetaType<QList<QVariantMap>>(); + qDBusRegisterMetaType<QList<QDBusObjectPath>>(); +} + +void QMPrisWrapper::post_creation() +{ + QDBusConnection sessbus = QDBusConnection::sessionBus(); + sessbus.registerService("org.mpris.MediaPlayer2." + svcsuffix); + sessbus.registerObject("/org/mpris/MediaPlayer2", this); + api->registerUIHook("main.stop", [this](const void* a, void* _) { + tracklist->TrackListReplaced({}, QDBusObjectPath("/")); + this->notifyPropertyChange(PLAYER_INTERFACE, "Metadata", player->getMetadata()); + this->notifyPropertyChange(PLAYER_INTERFACE, "PlaybackStatus", player->getPlaybackStatus()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanPause", player->getCanPause()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanPlay", player->getCanPlay()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanSeek", player->getCanSeek()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanGoNext", player->getCanGoNext()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanGoPrevious", player->getCanGoPrevious()); + }, nullptr); + api->registerUIHook("main.start", [this](const void* a, void* _) { + tracklist->TrackListReplaced(tracklist->getTracks(), QDBusObjectPath("/org/chrisoft/qmidiplayer/dummylist/0")); + this->notifyPropertyChange(PLAYER_INTERFACE, "Metadata", player->getMetadata()); + this->notifyPropertyChange(PLAYER_INTERFACE, "PlaybackStatus", player->getPlaybackStatus()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanPause", player->getCanPause()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanPlay", player->getCanPlay()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanSeek", player->getCanSeek()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanGoNext", player->getCanGoNext()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanGoPrevious", player->getCanGoPrevious()); + this->notifyPropertyChange(PLAYER_INTERFACE, "Rate", player->getRate()); + }, nullptr); + api->registerUIHook("main.pause", [this](const void* a, void* _) { + this->notifyPropertyChange(PLAYER_INTERFACE, "PlaybackStatus", player->getPlaybackStatus()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanPause", player->getCanPause()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanPlay", player->getCanPlay()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanSeek", player->getCanSeek()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanGoNext", player->getCanGoNext()); + this->notifyPropertyChange(PLAYER_INTERFACE, "CanGoPrevious", player->getCanGoPrevious()); + }, nullptr); + api->registerUIHook("main.seek", [this](const void* a, void *_) { + auto ps = static_cast<const ::PlaybackStatus*>(a); + player->Seeked(ps->curtime_ms * 1000); + }, nullptr); +} + +QMPrisWrapper::~QMPrisWrapper() +{ + QDBusConnection sessbus = QDBusConnection::sessionBus(); + sessbus.unregisterObject("/org/mpris/MediaPlayer2"); + sessbus.unregisterService("org.mpris.MediaPlayer2." + svcsuffix); +} + +void QMPrisWrapper::notifyPropertyChange(QString intf, QString prop, QVariant val) +{ + QDBusConnection sessbus = QDBusConnection::sessionBus(); + auto signal = QDBusMessage::createSignal("/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "PropertiesChanged"); + signal.setArguments({ + intf, + QVariantMap{{prop, val}}, + QStringList{} + }); + sessbus.send(signal); +} |