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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/***************************************************************************
* Copyright (C) 2014-2015 by Ilya Kotov *
* forkotov02@hotmail.ru *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QDBusInterface>
#include <QDBusConnection>
#include <QDBusPendingCallWatcher>
#include <QDBusConnectionInterface>
#include <QVariantList>
#include <QDateTime>
#include <QCoreApplication>
#include <qmmpui/mediaplayer.h>
#include <qmmp/soundcore.h>
#include "mediakeys.h"
MediaKeys::MediaKeys(QObject *parent) :
QObject(parent)
{
m_isRegistered = false;
m_interface = 0;
if(!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.gnome.SettingsDaemon"))
{
qWarning("MediaKeys: gnome settings daemon is not running");
return;
}
m_interface = new QDBusInterface("org.gnome.SettingsDaemon",
"/org/gnome/SettingsDaemon/MediaKeys",
"org.gnome.SettingsDaemon.MediaKeys",
QDBusConnection::sessionBus(), this);
QDBusPendingReply<> reply = grabMediaPlayerKeys(QCoreApplication::applicationName(),
QDateTime::currentDateTime().toTime_t());
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(reply, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), SLOT(onRegisterFinished(QDBusPendingCallWatcher*)));
}
MediaKeys::~MediaKeys()
{
if(m_isRegistered && m_interface)
{
QDBusPendingReply<> reply = releaseMediaPlayerKeys(QCoreApplication::applicationName());
reply.waitForFinished();
qWarning("MediaKeys: unregistered");
}
}
QDBusPendingReply<> MediaKeys::grabMediaPlayerKeys(const QString &application, uint time)
{
QVariantList argumentList;
argumentList << QVariant::fromValue(application) << QVariant::fromValue(time);
return m_interface->asyncCallWithArgumentList(QLatin1String("GrabMediaPlayerKeys"), argumentList);
}
QDBusPendingReply<> MediaKeys::releaseMediaPlayerKeys(const QString &application)
{
QVariantList argumentList;
argumentList << QVariant::fromValue(application);
return m_interface->asyncCallWithArgumentList(QLatin1String("ReleaseMediaPlayerKeys"), argumentList);
}
void MediaKeys::onRegisterFinished(QDBusPendingCallWatcher *watcher)
{
QDBusMessage reply = watcher->reply();
watcher->deleteLater();
if (reply.type() == QDBusMessage::ErrorMessage)
{
qWarning("MediaKeys: unable to grab media keys: [%s] - %s",
qPrintable(reply.errorName()), qPrintable(reply.errorMessage()));
return;
}
m_interface->connection().connect("org.gnome.SettingsDaemon",
"/org/gnome/SettingsDaemon/MediaKeys",
"org.gnome.SettingsDaemon.MediaKeys",
"MediaPlayerKeyPressed", this,
SLOT(onKeyPressed(const QString&, const QString&)));
m_isRegistered = true;
qDebug("MediaKeys: registered");
}
void MediaKeys::onKeyPressed(const QString &in0, const QString &in1)
{
if(in0 != QCoreApplication::applicationName())
return;
MediaPlayer *player = MediaPlayer::instance();
SoundCore *core = SoundCore::instance();
qDebug("MediaKeys: [%s] pressed", qPrintable(in1));
if(in1 == "Play")
{
if (core->state() == Qmmp::Stopped)
player->play();
else if (core->state() != Qmmp::FatalError)
core->pause();
}
else if(in1 == "Pause")
core->pause();
else if(in1 == "Stop")
player->stop();
else if(in1 == "Previous")
player->previous();
else if(in1 == "Next")
player->next();
else
qWarning("MediaKeys: unknown media key pressed");
}
|