From 82432968e9656b83e87d7ce12c460e218c5e3ab1 Mon Sep 17 00:00:00 2001 From: Florian Richter Date: Sat, 25 May 2013 13:28:26 +0200 Subject: [PATCH] listen to media key events of gnome settings daemon * add shortcuthandler, which listens to the media key event provided by the gnome settings daemon via dbus (https://github.com/GNOME/gnome-settings-daemon/blob/master/plugins/media-keys/README.media-keys-API) * fixes TWK-983 --- CMakeLists.txt | 3 + src/tomahawk/CMakeLists.txt | 5 + src/tomahawk/Config.h.in | 1 + src/tomahawk/GnomeSettingsDaemonMediaKeys.xml | 17 +++ src/tomahawk/GnomeShortcutHandler.cpp | 102 ++++++++++++++++++ src/tomahawk/GnomeShortcutHandler.h | 54 ++++++++++ src/tomahawk/TomahawkApp.cpp | 10 ++ 7 files changed, 192 insertions(+) create mode 100644 src/tomahawk/GnomeSettingsDaemonMediaKeys.xml create mode 100644 src/tomahawk/GnomeShortcutHandler.cpp create mode 100644 src/tomahawk/GnomeShortcutHandler.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a09f41220..b1703b7ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ option(WITH_CRASHREPORTER "Build with CrashReporter" ON) option(WITH_BINARY_ATTICA "Enable support for downloading binary resolvers automatically" ON) option(LEGACY_KDE_INTEGRATION "Install tomahawk.protocol file, deprecated since 4.6.0" OFF) OPTION(WITH_UPOWER "Build with support for UPower events" OFF) +OPTION(WITH_GNOMESHORTCUTHANDLER "Build with shortcut handler for GNOME" OFF) IF( CMAKE_SYSTEM_PROCESSOR MATCHES "arm" ) message(STATUS "Build of breakpad library disabled on this platform.") @@ -189,8 +190,10 @@ endif() IF( UNIX AND NOT APPLE AND QT_QTDBUS_FOUND ) SET( WITH_UPOWER ON ) + SET( WITH_GNOMESHORTCUTHANDLER ON ) ENDIF( UNIX AND NOT APPLE AND QT_QTDBUS_FOUND ) + macro_optional_find_package(Phonon 4.5.0) macro_log_feature(PHONON_FOUND "Phonon" "The Phonon multimedia library" "http://phonon.kde.org" TRUE "" "") if(PHONON_FOUND) diff --git a/src/tomahawk/CMakeLists.txt b/src/tomahawk/CMakeLists.txt index 636ff2c3c..64fb365f9 100644 --- a/src/tomahawk/CMakeLists.txt +++ b/src/tomahawk/CMakeLists.txt @@ -27,6 +27,11 @@ SET( tomahawkSources ${tomahawkSources} main.cpp ) +IF( WITH_GNOMESHORTCUTHANDLER ) + SET( tomahawkSources ${tomahawkSources} GnomeShortcutHandler.cpp ) + qt4_add_dbus_interface(tomahawkSources GnomeSettingsDaemonMediaKeys.xml GnomeSettingsDaemonMediaKeysProxy) +ENDIF( WITH_GNOMESHORTCUTHANDLER ) + IF(LIBLASTFM_FOUND) SET(tomahawkSources ${tomahawkSources} Scrobbler.cpp diff --git a/src/tomahawk/Config.h.in b/src/tomahawk/Config.h.in index 33effc143..d552b9785 100644 --- a/src/tomahawk/Config.h.in +++ b/src/tomahawk/Config.h.in @@ -19,6 +19,7 @@ #cmakedefine WITH_BINARY_ATTICA #cmakedefine WITH_QtSparkle #cmakedefine WITH_UPOWER +#cmakedefine WITH_GNOMESHORTCUTHANDLER #cmakedefine LIBLASTFM_FOUND diff --git a/src/tomahawk/GnomeSettingsDaemonMediaKeys.xml b/src/tomahawk/GnomeSettingsDaemonMediaKeys.xml new file mode 100644 index 000000000..9caf240f5 --- /dev/null +++ b/src/tomahawk/GnomeSettingsDaemonMediaKeys.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + diff --git a/src/tomahawk/GnomeShortcutHandler.cpp b/src/tomahawk/GnomeShortcutHandler.cpp new file mode 100644 index 000000000..56fe30a90 --- /dev/null +++ b/src/tomahawk/GnomeShortcutHandler.cpp @@ -0,0 +1,102 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Florian Richter + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +// implement listen on media keys events provided by the gnome settings daemon +// as documented here: +// https://github.com/GNOME/gnome-settings-daemon/blob/master/plugins/media-keys/README.media-keys-API + +#include "GnomeShortcutHandler.h" +#include "utils/Logger.h" + + +# include + +#include + +using namespace Tomahawk; + +const char* GnomeShortcutHandler::kGsdService = "org.gnome.SettingsDaemon"; +const char* GnomeShortcutHandler::kGsdPath = "/org/gnome/SettingsDaemon/MediaKeys"; +const char* GnomeShortcutHandler::kGsdInterface = "org.gnome.SettingsDaemon.MediaKeys"; + + +GnomeShortcutHandler::GnomeShortcutHandler(QObject *parent) : + Tomahawk::ShortcutHandler(parent), + interface_(NULL) +{ + +} + +bool GnomeShortcutHandler::DoRegister() { + tLog(LOGVERBOSE) << "registering for gnome media keys"; + + // Check if the GSD service is available + if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService)) { + tLog(LOGVERBOSE) << "gnome settings daemon not registered"; + return false; + } + + if (!interface_) { + interface_ = new org::gnome::SettingsDaemon::MediaKeys( + kGsdService, kGsdPath, QDBusConnection::sessionBus(), this->parent()); + } + + QDBusPendingReply<> reply = interface_->GrabMediaPlayerKeys( + QCoreApplication::applicationName(), 0); + + QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(reply, this); + connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), + this, SLOT(RegisterFinished(QDBusPendingCallWatcher*))); + + return true; +} + +void GnomeShortcutHandler::RegisterFinished(QDBusPendingCallWatcher* watcher) { + QDBusMessage reply = watcher->reply(); + watcher->deleteLater(); + + if (reply.type() == QDBusMessage::ErrorMessage) { + tLog(LOGVERBOSE) << "Failed to grab media keys" + << reply.errorName() < === + * + * Copyright 2013, Florian Richter + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#ifndef GNOMESHORTCUTHANDLER_H +#define GNOMESHORTCUTHANDLER_H + +#include "ShortcutHandler.h" +#include "GnomeSettingsDaemonMediaKeysProxy.h" + +#include + +namespace Tomahawk { + + +class GnomeShortcutHandler : public ShortcutHandler +{ + Q_OBJECT +public: + explicit GnomeShortcutHandler(QObject *parent = 0); + bool DoRegister(); + + static const char* kGsdService; + static const char* kGsdPath; + static const char* kGsdInterface; + + +public slots: + void RegisterFinished(QDBusPendingCallWatcher* watcher); + void GnomeMediaKeyPressed( const QString& application, const QString& key ); + +private: + org::gnome::SettingsDaemon::MediaKeys* interface_; + +}; + +} + +#endif // GNOMESHORTCUTHANDLER_H + diff --git a/src/tomahawk/TomahawkApp.cpp b/src/tomahawk/TomahawkApp.cpp index 927334f17..e957cff88 100644 --- a/src/tomahawk/TomahawkApp.cpp +++ b/src/tomahawk/TomahawkApp.cpp @@ -94,6 +94,10 @@ #include #endif +#ifdef WITH_GNOMESHORTCUTHANDLER +#include "GnomeShortcutHandler.h" +#endif + #include #include #include @@ -237,6 +241,12 @@ TomahawkApp::init() increaseMaxFileDescriptors(); #endif +#ifdef WITH_GNOMESHORTCUTHANDLER + GnomeShortcutHandler *gnomeShortcutHandler = new GnomeShortcutHandler( this ); + gnomeShortcutHandler->DoRegister(); + m_shortcutHandler = QPointer( gnomeShortcutHandler ); +#endif + // Connect up shortcuts if ( !m_shortcutHandler.isNull() ) {