From f43793ecdc5bc7fd70571814ee5398bb5f1cd894 Mon Sep 17 00:00:00 2001 From: <> Date: Sun, 1 Aug 2010 15:04:55 +0200 Subject: [PATCH] Global shortcuts --- minitube.pro | 13 +++++- src/MainWindow.cpp | 20 +++++++++ src/MediaView.h | 2 +- src/globalshortcutbackend.cpp | 24 +++++++++++ src/globalshortcutbackend.h | 27 ++++++++++++ src/globalshortcuts.cpp | 50 ++++++++++++++++++++++ src/globalshortcuts.h | 56 ++++++++++++++++++++++++ src/gnomeglobalshortcutbackend.cpp | 68 ++++++++++++++++++++++++++++++ src/gnomeglobalshortcutbackend.h | 29 +++++++++++++ src/main.cpp | 7 +++ src/playlistwidget.h | 2 +- 11 files changed, 294 insertions(+), 4 deletions(-) create mode 100644 src/globalshortcutbackend.cpp create mode 100644 src/globalshortcutbackend.h create mode 100644 src/globalshortcuts.cpp create mode 100644 src/globalshortcuts.h create mode 100644 src/gnomeglobalshortcutbackend.cpp create mode 100644 src/gnomeglobalshortcutbackend.h diff --git a/minitube.pro b/minitube.pro index 0c00ef2..75119a7 100755 --- a/minitube.pro +++ b/minitube.pro @@ -10,6 +10,9 @@ TARGET = minitube QT += network \ xml \ phonon +unix:!mac { + QT += dbus +} include(src/qtsingleapplication/qtsingleapplication.pri) HEADERS += src/MainWindow.h \ src/SearchView.h \ @@ -41,7 +44,10 @@ HEADERS += src/MainWindow.h \ src/flickcharm.h \ src/videodefinition.h \ src/fontutils.h \ - src/thlibrary/thblackbar.h + src/thlibrary/thblackbar.h \ + src/gnomeglobalshortcutbackend.h \ + src/globalshortcuts.h \ + src/globalshortcutbackend.h SOURCES += src/main.cpp \ src/MainWindow.cpp \ src/SearchView.cpp \ @@ -71,7 +77,10 @@ SOURCES += src/main.cpp \ src/videodefinition.cpp \ src/constants.cpp \ src/fontutils.cpp \ - src/thlibrary/thblackbar.cpp + src/thlibrary/thblackbar.cpp \ + src/gnomeglobalshortcutbackend.cpp \ + src/globalshortcuts.cpp \ + src/globalshortcutbackend.cpp RESOURCES += resources.qrc DESTDIR = build/target/ OBJECTS_DIR = build/obj/ diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 19bd15f..5ab28aa 100755 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -5,6 +5,13 @@ #include "global.h" #include "videodefinition.h" #include "fontutils.h" +#include "globalshortcuts.h" +#ifdef Q_WS_X11 +#include "gnomeglobalshortcutbackend.h" +#endif +#ifdef APP_MAC +#include "local/mac/mac_startup.h" +#endif MainWindow::MainWindow() : aboutView(0), @@ -58,6 +65,19 @@ MainWindow::MainWindow() : showWidget(searchView); setCentralWidget(views); + + // Global shortcuts + GlobalShortcuts &shortcuts = GlobalShortcuts::instance(); +#ifdef Q_WS_X11 + if (GnomeGlobalShortcutBackend::IsGsdAvailable()) + shortcuts.setBackend(new GnomeGlobalShortcutBackend(&shortcuts)); +#endif +#ifdef APP_MAC + mac::MacSetup(); +#endif + connect(&shortcuts, SIGNAL(PlayPause()), pauseAct, SLOT(trigger())); + connect(&shortcuts, SIGNAL(Stop()), this, SLOT(stop())); + connect(&shortcuts, SIGNAL(Next()), skipAct, SLOT(trigger())); } MainWindow::~MainWindow() { diff --git a/src/MediaView.h b/src/MediaView.h index f3bd3dd..04dd92c 100644 --- a/src/MediaView.h +++ b/src/MediaView.h @@ -7,7 +7,7 @@ #include #include "View.h" #include "ListModel.h" -#include "thblackbar.h" +#include "thlibrary/thblackbar.h" #include "searchparams.h" #include "playlistwidget.h" #include "loadingwidget.h" diff --git a/src/globalshortcutbackend.cpp b/src/globalshortcutbackend.cpp new file mode 100644 index 0000000..8646b7a --- /dev/null +++ b/src/globalshortcutbackend.cpp @@ -0,0 +1,24 @@ +#include "globalshortcutbackend.h" +#include "globalshortcuts.h" + +GlobalShortcutBackend::GlobalShortcutBackend(GlobalShortcuts *parent) + : QObject(parent), + manager_(parent), + active_(false) { } + +bool GlobalShortcutBackend::Register() { + bool ret = DoRegister(); + if (ret) + active_ = true; + return ret; +} + +void GlobalShortcutBackend::Unregister() { + DoUnregister(); + active_ = false; +} + +void GlobalShortcutBackend::Reregister() { + Unregister(); + Register(); +} diff --git a/src/globalshortcutbackend.h b/src/globalshortcutbackend.h new file mode 100644 index 0000000..e6844bb --- /dev/null +++ b/src/globalshortcutbackend.h @@ -0,0 +1,27 @@ +#ifndef GLOBALSHORTCUTBACKEND_H +#define GLOBALSHORTCUTBACKEND_H + +#include + +class GlobalShortcuts; + +class GlobalShortcutBackend : public QObject { +public: + GlobalShortcutBackend(GlobalShortcuts* parent = 0); + virtual ~GlobalShortcutBackend() {} + + bool is_active() const { return active_; } + + bool Register(); + void Unregister(); + void Reregister(); + +protected: + virtual bool DoRegister() = 0; + virtual void DoUnregister() = 0; + + GlobalShortcuts* manager_; + bool active_; +}; + +#endif // GLOBALSHORTCUTBACKEND_H diff --git a/src/globalshortcuts.cpp b/src/globalshortcuts.cpp new file mode 100644 index 0000000..39eda6d --- /dev/null +++ b/src/globalshortcuts.cpp @@ -0,0 +1,50 @@ +#include "globalshortcuts.h" +#include "globalshortcutbackend.h" + +static GlobalShortcuts *singleton = 0; + +GlobalShortcuts& GlobalShortcuts::instance() { + if (!singleton) singleton = new GlobalShortcuts(); + return *singleton; +} + +GlobalShortcuts::GlobalShortcuts(QObject *parent) + : QObject(parent), + backend(0) { + + // Create actions + AddShortcut("play", tr("Play"), SIGNAL(Play())); + AddShortcut("pause", tr("Pause"), SIGNAL(Pause())); + AddShortcut("play_pause", tr("Play/Pause"), SIGNAL(PlayPause()), QKeySequence(Qt::Key_MediaPlay)); + AddShortcut("stop", tr("Stop"), SIGNAL(Stop()), QKeySequence(Qt::Key_MediaStop)); + AddShortcut("stop_after", tr("Stop playing after current track"), SIGNAL(StopAfter())); + AddShortcut("next_track", tr("Next track"), SIGNAL(Next()), QKeySequence(Qt::Key_MediaNext)); + AddShortcut("prev_track", tr("Previous track"), SIGNAL(Previous()), QKeySequence(Qt::Key_MediaPrevious)); + AddShortcut("inc_volume", tr("Increase volume"), SIGNAL(IncVolume())); + AddShortcut("dec_volume", tr("Decrease volume"), SIGNAL(DecVolume())); + AddShortcut("mute", tr("Mute"), SIGNAL(Mute())); + AddShortcut("seek_forward", tr("Seek forward"), SIGNAL(SeekForward())); + AddShortcut("seek_backward", tr("Seek backward"), SIGNAL(SeekBackward())); + +} + +void GlobalShortcuts::AddShortcut(const QString &id, const QString &name, + const char* signal, + const QKeySequence &default_key) { + Shortcut shortcut; + shortcut.action = new QAction(name, this); + shortcut.action->setShortcut(default_key); + shortcut.id = id; + shortcut.default_key = default_key; + + connect(shortcut.action, SIGNAL(triggered()), this, signal); + + shortcuts_[id] = shortcut; +} + +void GlobalShortcuts::reload() { + if (backend) { + backend->Unregister(); + backend->Register(); + } +} diff --git a/src/globalshortcuts.h b/src/globalshortcuts.h new file mode 100644 index 0000000..62cd935 --- /dev/null +++ b/src/globalshortcuts.h @@ -0,0 +1,56 @@ +#ifndef GLOBALSHORTCUTS_H +#define GLOBALSHORTCUTS_H + +#include +#include + +class GlobalShortcutBackend; + +class GlobalShortcuts : public QObject { + + Q_OBJECT + +public: + static GlobalShortcuts& instance(); + + struct Shortcut { + QString id; + QKeySequence default_key; + QAction* action; + }; + + QMap shortcuts() const { return shortcuts_; } + void setBackend(GlobalShortcutBackend* backend) { + this->backend = backend; + reload(); + } + +public slots: + void reload(); + +signals: + void Play(); + void Pause(); + void PlayPause(); + void Stop(); + void StopAfter(); + void Next(); + void Previous(); + void IncVolume(); + void DecVolume(); + void Mute(); + void SeekForward(); + void SeekBackward(); + +private: + GlobalShortcuts(QObject* parent = 0); + void AddShortcut(const QString& id, const QString& name, const char* signal, + const QKeySequence& default_key = QKeySequence(0)); + +private: + GlobalShortcutBackend* backend; + + QMap shortcuts_; +}; + +#endif diff --git a/src/gnomeglobalshortcutbackend.cpp b/src/gnomeglobalshortcutbackend.cpp new file mode 100644 index 0000000..f34c20d --- /dev/null +++ b/src/gnomeglobalshortcutbackend.cpp @@ -0,0 +1,68 @@ +#include "gnomeglobalshortcutbackend.h" +#include "globalshortcuts.h" + +#include +#include + +#ifdef QT_DBUS_LIB +# include +#endif + +const char* GnomeGlobalShortcutBackend::kGsdService = "org.gnome.SettingsDaemon"; +const char* GnomeGlobalShortcutBackend::kGsdPath = "/org/gnome/SettingsDaemon/MediaKeys"; +const char* GnomeGlobalShortcutBackend::kGsdInterface = "org.gnome.SettingsDaemon.MediaKeys"; + +GnomeGlobalShortcutBackend::GnomeGlobalShortcutBackend(GlobalShortcuts* parent) + : GlobalShortcutBackend(parent), + interface_(NULL) { } + +bool GnomeGlobalShortcutBackend::IsGsdAvailable() { +#ifdef QT_DBUS_LIB + return QDBusConnection::sessionBus().interface()->isServiceRegistered( + GnomeGlobalShortcutBackend::kGsdService); +#else // QT_DBUS_LIB + return false; +#endif +} + +bool GnomeGlobalShortcutBackend::DoRegister() { + // qDebug() << __PRETTY_FUNCTION__; +#ifdef QT_DBUS_LIB + // Check if the GSD service is available + if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService)) + return false; + + if (!interface_) { + interface_ = new QDBusInterface( + kGsdService, kGsdPath, kGsdInterface, QDBusConnection::sessionBus(), this); + } + + connect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)), + this, SLOT(GnomeMediaKeyPressed(QString,QString))); + + return true; +#else // QT_DBUS_LIB + return false; +#endif +} + +void GnomeGlobalShortcutBackend::DoUnregister() { + +#ifdef QT_DBUS_LIB + // Check if the GSD service is available + if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService)) + return; + if (!interface_) + return; + + disconnect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)), + this, SLOT(GnomeMediaKeyPressed(QString,QString))); +#endif +} + +void GnomeGlobalShortcutBackend::GnomeMediaKeyPressed(const QString&, const QString& key) { + if (key == "Play") manager_->shortcuts()["play_pause"].action->trigger(); + if (key == "Stop") manager_->shortcuts()["stop"].action->trigger(); + if (key == "Next") manager_->shortcuts()["next_track"].action->trigger(); + if (key == "Previous") manager_->shortcuts()["prev_track"].action->trigger(); +} diff --git a/src/gnomeglobalshortcutbackend.h b/src/gnomeglobalshortcutbackend.h new file mode 100644 index 0000000..bf6dd89 --- /dev/null +++ b/src/gnomeglobalshortcutbackend.h @@ -0,0 +1,29 @@ +#ifndef GNOMEGLOBALSHORTCUTBACKEND_H +#define GNOMEGLOBALSHORTCUTBACKEND_H + +#include "globalshortcutbackend.h" + +class QDBusInterface; + +class GnomeGlobalShortcutBackend : public GlobalShortcutBackend { + Q_OBJECT + +public: + GnomeGlobalShortcutBackend(GlobalShortcuts* parent); + static bool IsGsdAvailable(); + static const char* kGsdService; + static const char* kGsdPath; + static const char* kGsdInterface; + +protected: + bool DoRegister(); + void DoUnregister(); + +private slots: + void GnomeMediaKeyPressed(const QString& application, const QString& key); + +private: + QDBusInterface* interface_; +}; + +#endif // GNOMEGLOBALSHORTCUTBACKEND_H diff --git a/src/main.cpp b/src/main.cpp index 36f5d91..ec2d77c 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,9 +2,16 @@ #include #include "constants.h" #include "MainWindow.h" +#ifdef APP_MAC +#include "local/mac/mac_startup.h" +#endif int main(int argc, char **argv) { +#ifdef APP_MAC + mac::MacMain(); +#endif + QtSingleApplication app(argc, argv); if (app.sendMessage("Wake up!")) return 0; diff --git a/src/playlistwidget.h b/src/playlistwidget.h index 8d2bb29..71cb687 100644 --- a/src/playlistwidget.h +++ b/src/playlistwidget.h @@ -2,7 +2,7 @@ #define PLAYLISTWIDGET_H #include -#include "thblackbar.h" +#include "thlibrary/thblackbar.h" class PlaylistWidget : public QWidget { -- 2.39.5