2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
#include <QObject>
|
|
|
|
#include <QCoreApplication>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QDateTime>
|
2019-01-01 20:07:29 +01:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusMessage>
|
|
|
|
#include <QDBusPendingCallWatcher>
|
|
|
|
#include <QDBusPendingReply>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QAction>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/logging.h"
|
2021-01-26 19:13:29 +01:00
|
|
|
#include "globalshortcutsmanager.h"
|
|
|
|
#include "globalshortcutsbackend.h"
|
2021-05-29 20:35:55 +02:00
|
|
|
#include "globalshortcutsbackend-gnome.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2022-09-13 17:53:57 +02:00
|
|
|
#include "gnomesettingsdaemon.h"
|
|
|
|
|
2024-09-07 04:24:14 +02:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2024-04-11 02:56:01 +02:00
|
|
|
namespace {
|
|
|
|
constexpr char kService1[] = "org.gnome.SettingsDaemon.MediaKeys";
|
|
|
|
constexpr char kService2[] = "org.gnome.SettingsDaemon";
|
|
|
|
constexpr char kPath[] = "/org/gnome/SettingsDaemon/MediaKeys";
|
|
|
|
} // namespace
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
GlobalShortcutsBackendGnome::GlobalShortcutsBackendGnome(GlobalShortcutsManager *manager, QObject *parent)
|
2023-02-18 14:09:27 +01:00
|
|
|
: GlobalShortcutsBackend(manager, GlobalShortcutsBackend::Type::Gnome, parent),
|
2018-02-27 18:06:05 +01:00
|
|
|
interface_(nullptr),
|
|
|
|
is_connected_(false) {}
|
|
|
|
|
2021-09-01 21:37:11 +02:00
|
|
|
bool GlobalShortcutsBackendGnome::IsAvailable() const {
|
|
|
|
return IsGnomeAvailable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalShortcutsBackendGnome::IsGnomeAvailable() {
|
2021-05-29 20:35:55 +02:00
|
|
|
|
2024-04-11 02:56:01 +02:00
|
|
|
return QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String(kService1)) || QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String(kService2));
|
2021-05-29 20:35:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalShortcutsBackendGnome::DoRegister() {
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
qLog(Debug) << "Registering";
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2019-01-08 23:25:50 +01:00
|
|
|
if (!interface_) {
|
2024-04-11 02:56:01 +02:00
|
|
|
if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String(kService1))) {
|
|
|
|
interface_ = new OrgGnomeSettingsDaemonMediaKeysInterface(QLatin1String(kService1), QLatin1String(kPath), QDBusConnection::sessionBus(), this);
|
2019-01-08 23:25:50 +01:00
|
|
|
}
|
2024-04-11 02:56:01 +02:00
|
|
|
else if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String(kService2))) {
|
|
|
|
interface_ = new OrgGnomeSettingsDaemonMediaKeysInterface(QLatin1String(kService2), QLatin1String(kPath), QDBusConnection::sessionBus(), this);
|
2019-01-08 23:25:50 +01:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!interface_) {
|
2019-01-08 23:25:50 +01:00
|
|
|
qLog(Warning) << "Gnome settings daemon not registered";
|
|
|
|
return false;
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2024-08-24 17:28:29 +02:00
|
|
|
QDBusPendingReply<> reply = interface_->GrabMediaPlayerKeys(QCoreApplication::applicationName(), QDateTime::currentSecsSinceEpoch());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
|
2021-05-29 20:35:55 +02:00
|
|
|
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &GlobalShortcutsBackendGnome::RegisterFinished);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
return true;
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-05-29 20:35:55 +02:00
|
|
|
void GlobalShortcutsBackendGnome::RegisterFinished(QDBusPendingCallWatcher *watcher) {
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
QDBusMessage reply = watcher->reply();
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
|
|
|
if (reply.type() == QDBusMessage::ErrorMessage) {
|
2021-07-11 09:49:38 +02:00
|
|
|
qLog(Warning) << "Failed to grab media keys" << reply.errorName() << reply.errorMessage();
|
2018-02-27 18:06:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-29 20:35:55 +02:00
|
|
|
QObject::connect(interface_, &OrgGnomeSettingsDaemonMediaKeysInterface::MediaPlayerKeyPressed, this, &GlobalShortcutsBackendGnome::GnomeMediaKeyPressed);
|
2018-02-27 18:06:05 +01:00
|
|
|
is_connected_ = true;
|
|
|
|
|
2021-05-29 20:35:55 +02:00
|
|
|
qLog(Debug) << "Registered.";
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-05-29 20:35:55 +02:00
|
|
|
void GlobalShortcutsBackendGnome::DoUnregister() {
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
qLog(Debug) << "Unregister";
|
2018-07-03 19:48:08 +02:00
|
|
|
|
2021-05-29 20:35:55 +02:00
|
|
|
if (!IsAvailable() || !interface_ || !is_connected_) return;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
is_connected_ = false;
|
|
|
|
|
|
|
|
interface_->ReleaseMediaPlayerKeys(QCoreApplication::applicationName());
|
2021-05-29 20:35:55 +02:00
|
|
|
QObject::disconnect(interface_, &OrgGnomeSettingsDaemonMediaKeysInterface::MediaPlayerKeyPressed, this, &GlobalShortcutsBackendGnome::GnomeMediaKeyPressed);
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-05-29 20:35:55 +02:00
|
|
|
void GlobalShortcutsBackendGnome::GnomeMediaKeyPressed(const QString&, const QString &key) {
|
2021-03-21 04:47:11 +01:00
|
|
|
|
|
|
|
auto shortcuts = manager_->shortcuts();
|
2024-09-07 04:24:14 +02:00
|
|
|
if (key == "Play"_L1) shortcuts[QStringLiteral("play_pause")].action->trigger();
|
|
|
|
if (key == "Stop"_L1) shortcuts[QStringLiteral("stop")].action->trigger();
|
|
|
|
if (key == "Next"_L1) shortcuts[QStringLiteral("next_track")].action->trigger();
|
|
|
|
if (key == "Previous"_L1) shortcuts[QStringLiteral("prev_track")].action->trigger();
|
2021-03-21 04:47:11 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|