aboutsummaryrefslogtreecommitdiff
path: root/mpris-plugin/qmpriswrapper.cpp
blob: 8a3f273542e64d770dac357dddcccc0c1829b067 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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);
}