/* * Strawberry Music Player * This file was part of Clementine. * Copyright 2010, David Sansome * Copyright 2019-2021, Jonas Kvinge * * 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 . * */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include "core/logging.h" #include "globalshortcutsmanager.h" #include "globalshortcutsbackend.h" #include "globalshortcutsbackend-gsd.h" const char *GlobalShortcutsBackendGSD::kGsdService = "org.gnome.SettingsDaemon.MediaKeys"; const char *GlobalShortcutsBackendGSD::kGsdService2 = "org.gnome.SettingsDaemon"; const char *GlobalShortcutsBackendGSD::kGsdPath = "/org/gnome/SettingsDaemon/MediaKeys"; GlobalShortcutsBackendGSD::GlobalShortcutsBackendGSD(GlobalShortcutsManager *parent) : GlobalShortcutsBackend(parent), interface_(nullptr), is_connected_(false) {} bool GlobalShortcutsBackendGSD::DoRegister() { qLog(Debug) << "Registering"; if (!interface_) { if (QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService)) { interface_ = new OrgGnomeSettingsDaemonMediaKeysInterface(kGsdService, kGsdPath, QDBusConnection::sessionBus(), this); } else if (QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService2)) { interface_ = new OrgGnomeSettingsDaemonMediaKeysInterface(kGsdService2, kGsdPath, QDBusConnection::sessionBus(), this); } } if (!interface_) { qLog(Warning) << "Gnome settings daemon not registered"; return false; } QDBusPendingReply<> reply = interface_->GrabMediaPlayerKeys(QCoreApplication::applicationName(), QDateTime::currentDateTime().toSecsSinceEpoch()); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &GlobalShortcutsBackendGSD::RegisterFinished); return true; } void GlobalShortcutsBackendGSD::RegisterFinished(QDBusPendingCallWatcher *watcher) { QDBusMessage reply = watcher->reply(); watcher->deleteLater(); if (reply.type() == QDBusMessage::ErrorMessage) { qLog(Warning) << "Failed to grab media keys" << reply.errorName() <isServiceRegistered(kGsdService)) return; if (!interface_ || !is_connected_) return; is_connected_ = false; interface_->ReleaseMediaPlayerKeys(QCoreApplication::applicationName()); QObject::disconnect(interface_, &OrgGnomeSettingsDaemonMediaKeysInterface::MediaPlayerKeyPressed, this, &GlobalShortcutsBackendGSD::GnomeMediaKeyPressed); } void GlobalShortcutsBackendGSD::GnomeMediaKeyPressed(const QString&, const QString &key) { auto shortcuts = manager_->shortcuts(); if (key == "Play") shortcuts["play_pause"].action->trigger(); if (key == "Stop") shortcuts["stop"].action->trigger(); if (key == "Next") shortcuts["next_track"].action->trigger(); if (key == "Previous") shortcuts["prev_track"].action->trigger(); }