2019-01-01 20:07:29 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2019-01-01 20:07:29 +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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QAbstractEventDispatcher>
|
|
|
|
#include <QPair>
|
|
|
|
#include <QHash>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QFlags>
|
2020-09-04 23:00:42 +02:00
|
|
|
#include <QKeySequence>
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
# include <QKeyCombination>
|
|
|
|
#endif
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
#include "core/logging.h"
|
|
|
|
|
|
|
|
#include "globalshortcut.h"
|
|
|
|
|
|
|
|
GlobalShortcut *GlobalShortcut::initialized_ = nullptr;
|
|
|
|
QHash<QPair<quint32, quint32>, GlobalShortcut*> GlobalShortcut::internal_shortcuts_;
|
|
|
|
|
2021-07-11 07:40:57 +02:00
|
|
|
GlobalShortcut::GlobalShortcut(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
backend_(nullptr),
|
2022-06-13 00:23:42 +02:00
|
|
|
qt_key_(static_cast<Qt::Key>(0)),
|
2021-07-11 07:40:57 +02:00
|
|
|
qt_mods_(Qt::NoModifier),
|
|
|
|
native_key_(0),
|
2022-04-02 00:43:41 +02:00
|
|
|
native_key2_(0),
|
2021-07-11 07:40:57 +02:00
|
|
|
native_mods_(0) {
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
Q_ASSERT(!initialized_);
|
|
|
|
|
|
|
|
QAbstractEventDispatcher::instance()->installNativeEventFilter(this);
|
|
|
|
initialized_ = this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-11 07:40:57 +02:00
|
|
|
GlobalShortcut::GlobalShortcut(const QKeySequence &shortcut, GlobalShortcutsBackend *backend, QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
backend_(backend),
|
|
|
|
shortcut_(shortcut),
|
2022-06-13 00:23:42 +02:00
|
|
|
qt_key_(static_cast<Qt::Key>(0)),
|
2021-07-11 07:40:57 +02:00
|
|
|
qt_mods_(Qt::NoModifier),
|
|
|
|
native_key_(0),
|
2022-04-06 21:21:57 +02:00
|
|
|
native_key2_(0),
|
2021-07-11 07:40:57 +02:00
|
|
|
native_mods_(0) {
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
Q_ASSERT(initialized_);
|
|
|
|
setShortcut(shortcut);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalShortcut::~GlobalShortcut() {
|
2019-04-19 12:36:54 +02:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
if (this == initialized_) {
|
|
|
|
QAbstractEventDispatcher::instance()->removeNativeEventFilter(this);
|
|
|
|
initialized_ = nullptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unsetShortcut();
|
|
|
|
}
|
2019-04-19 12:36:54 +02:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalShortcut::setShortcut(const QKeySequence &shortcut) {
|
|
|
|
|
|
|
|
Q_ASSERT(initialized_);
|
|
|
|
|
|
|
|
if (shortcut.isEmpty()) return false;
|
|
|
|
shortcut_ = shortcut;
|
|
|
|
|
2020-09-04 23:00:42 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
QKeyCombination key_combination(shortcut[0]);
|
|
|
|
qt_key_ = key_combination.key();
|
|
|
|
qt_mods_ = key_combination.keyboardModifiers();
|
|
|
|
#else
|
2019-01-01 20:07:29 +01:00
|
|
|
Qt::KeyboardModifiers all_mods = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier;
|
|
|
|
qt_key_ = Qt::Key((shortcut[0] ^ all_mods) & shortcut[0]);
|
|
|
|
qt_mods_ = Qt::KeyboardModifiers(shortcut[0] & all_mods);
|
2020-09-04 23:00:42 +02:00
|
|
|
#endif
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
native_key_ = nativeKeycode(qt_key_);
|
|
|
|
if (native_key_ == 0) return false;
|
|
|
|
native_mods_ = nativeModifiers(qt_mods_);
|
|
|
|
|
2022-04-02 00:43:41 +02:00
|
|
|
bool success = registerShortcut(native_key_, native_mods_);
|
|
|
|
if (success) {
|
2019-01-01 20:07:29 +01:00
|
|
|
internal_shortcuts_.insert(qMakePair(native_key_, native_mods_), this);
|
2019-01-01 20:11:46 +01:00
|
|
|
qLog(Info) << "Registered shortcut" << shortcut_.toString();
|
2022-04-02 00:43:41 +02:00
|
|
|
native_key2_ = nativeKeycode2(qt_key_);
|
|
|
|
if (native_key2_ > 0 && registerShortcut(native_key2_, native_mods_)) {
|
|
|
|
internal_shortcuts_.insert(qMakePair(native_key2_, native_mods_), this);
|
|
|
|
}
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
qLog(Error) << "Failed to register shortcut" << shortcut_.toString();
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:43:41 +02:00
|
|
|
return success;
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalShortcut::unsetShortcut() {
|
|
|
|
|
|
|
|
Q_ASSERT(initialized_);
|
|
|
|
|
|
|
|
QPair<quint32, quint32> hash = qMakePair(native_key_, native_mods_);
|
|
|
|
if (internal_shortcuts_.contains(hash)) {
|
|
|
|
GlobalShortcut *gshortcut = internal_shortcuts_.value(hash);
|
|
|
|
if (gshortcut != this) return false;
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:43:41 +02:00
|
|
|
bool success = unregisterShortcut(native_key_, native_mods_);
|
|
|
|
if (success) {
|
2019-01-01 20:07:29 +01:00
|
|
|
if (internal_shortcuts_.contains(hash)) {
|
|
|
|
internal_shortcuts_.remove(hash);
|
|
|
|
}
|
|
|
|
qLog(Info) << "Unregister shortcut" << shortcut_.toString();
|
2022-04-02 00:43:41 +02:00
|
|
|
if (native_key2_ > 0) {
|
|
|
|
QPair<quint32, quint32> hash2 = qMakePair(native_key2_, native_mods_);
|
|
|
|
if (internal_shortcuts_.contains(hash2)) {
|
|
|
|
GlobalShortcut *gshortcut2 = internal_shortcuts_.value(hash);
|
|
|
|
if (gshortcut2 == this) {
|
|
|
|
unregisterShortcut(native_key2_, native_mods_);
|
|
|
|
internal_shortcuts_.remove(hash2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-01-01 20:11:46 +01:00
|
|
|
qLog(Error) << "Failed to unregister shortcut" << shortcut_.toString();
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
|
2022-06-13 00:23:42 +02:00
|
|
|
qt_key_ = static_cast<Qt::Key>(0);
|
2020-05-29 17:47:10 +02:00
|
|
|
qt_mods_ = Qt::KeyboardModifiers();
|
2019-01-01 20:07:29 +01:00
|
|
|
native_key_ = 0;
|
|
|
|
native_mods_ = 0;
|
|
|
|
|
2022-04-02 00:43:41 +02:00
|
|
|
return success;
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:43:41 +02:00
|
|
|
void GlobalShortcut::activateShortcut(const quint32 native_key, const quint32 native_mod) {
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
Q_ASSERT(initialized_);
|
|
|
|
|
|
|
|
QPair<quint32, quint32> hash = qMakePair(native_key, native_mod);
|
|
|
|
if (!internal_shortcuts_.contains(hash)) return;
|
|
|
|
|
|
|
|
GlobalShortcut *gshortcut = internal_shortcuts_.value(hash);
|
|
|
|
if (gshortcut && gshortcut != initialized_) {
|
|
|
|
emit gshortcut->activated();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|