diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2014-03-06 10:53:52 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2014-03-06 10:53:52 +0000 |
| commit | 389799c3433a17a7babaeb3365f1fb8c12ea12fe (patch) | |
| tree | 4b0092c3936016af0f01062a0a5d8475e793a4ea /src/plugins/General/gnomehotkey/mediakeys.cpp | |
| parent | bc4c331d2c1fe0060de15c999110803f41f62b29 (diff) | |
| download | qmmp-389799c3433a17a7babaeb3365f1fb8c12ea12fe.tar.gz qmmp-389799c3433a17a7babaeb3365f1fb8c12ea12fe.tar.bz2 qmmp-389799c3433a17a7babaeb3365f1fb8c12ea12fe.zip | |
added gnome hotkey plugin
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@4123 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src/plugins/General/gnomehotkey/mediakeys.cpp')
| -rw-r--r-- | src/plugins/General/gnomehotkey/mediakeys.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/plugins/General/gnomehotkey/mediakeys.cpp b/src/plugins/General/gnomehotkey/mediakeys.cpp new file mode 100644 index 000000000..e3b775df2 --- /dev/null +++ b/src/plugins/General/gnomehotkey/mediakeys.cpp @@ -0,0 +1,99 @@ +#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") + player->play(); + 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"); +} |
