summaryrefslogtreecommitdiffhomepage
path: root/qxt-mini/qxtglobalshortcut.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2017-02-25 14:05:37 +0100
committerStanislaw Halik <sthalik@misaki.pl>2017-02-25 14:19:59 +0100
commit35a00c84e56749aab323bfb664cbaeccc984c168 (patch)
tree1ec99cebc00d95f88ba65046fccd28bf946839a7 /qxt-mini/qxtglobalshortcut.cpp
parent595a9399a570e691a414aeeec69625bc82ac42db (diff)
qxt-mini: multiple Linux support fixes
- Register an event filter once and keep it there. The refcount goes out of whack. Will further investigate. - Use an iterator over all shortcuts registered for given key combination - Lessen logspam Keyboard shortcut support on Linux looks pretty good now. The issue that blocked keys from being further processed was running XGrabKey multiple times with various modifiers. XGrabKey will block unlisted modifiers from being passed to other apps. The solution was to register once with AnyModifier and care about modifiers only later.
Diffstat (limited to 'qxt-mini/qxtglobalshortcut.cpp')
-rw-r--r--qxt-mini/qxtglobalshortcut.cpp70
1 files changed, 47 insertions, 23 deletions
diff --git a/qxt-mini/qxtglobalshortcut.cpp b/qxt-mini/qxtglobalshortcut.cpp
index bf3ed33d..298472b5 100644
--- a/qxt-mini/qxtglobalshortcut.cpp
+++ b/qxt-mini/qxtglobalshortcut.cpp
@@ -30,36 +30,50 @@
*****************************************************************************/
#include "qxtglobalshortcut_p.h"
+
+#include "compat/util.hpp"
+
#include <QAbstractEventDispatcher>
-#include <QtDebug>
#include <QApplication>
+#include <QtDebug>
+#include <QtGlobal>
-#ifndef Q_OS_MAC
-int QxtGlobalShortcutPrivate::ref = 0;
-#endif // Q_OS_MAC
QHash<QPair<quint32, quint32>, QxtGlobalShortcut*> QxtGlobalShortcutPrivate::shortcuts;
-QxtGlobalShortcutPrivate::QxtGlobalShortcutPrivate() : enabled(true), key(Qt::Key(0)), mods(Qt::NoModifier)
+struct QxtGlobalShortcutPrivate::event_filter_installer
+{
+ static void ensure_event_filter();
+};
+
+void QxtGlobalShortcutPrivate::event_filter_installer::ensure_event_filter()
{
#ifndef Q_OS_MAC
- if (ref == 0) {
- QAbstractEventDispatcher::instance()->installNativeEventFilter(this);
+ QAbstractEventDispatcher* instance = QAbstractEventDispatcher::instance();
+ if (instance)
+ {
+ static QxtGlobalShortcutPrivate filter(QxtGlobalShortcutPrivate::tag {});
+ static bool installed =
+ (instance->installNativeEventFilter(&filter),
+ true);
+ Q_UNUSED(installed);
}
- ++ref;
-#endif // Q_OS_MAC
+#endif
+}
+
+QxtGlobalShortcutPrivate::QxtGlobalShortcutPrivate(QxtGlobalShortcutPrivate::tag) :
+ enabled(false), key(Qt::Key(0)), mods(Qt::NoModifier)
+{
+ qDebug() << "qxt-mini: adding event filter";
+}
+
+QxtGlobalShortcutPrivate::QxtGlobalShortcutPrivate() :
+ enabled(true), key(Qt::Key(0)), mods(Qt::NoModifier)
+{
+ QxtGlobalShortcutPrivate::event_filter_installer::ensure_event_filter();
}
QxtGlobalShortcutPrivate::~QxtGlobalShortcutPrivate()
{
-#ifndef Q_OS_MAC
- --ref;
- if (ref == 0) {
- QAbstractEventDispatcher *ed = QAbstractEventDispatcher::instance(qApp->thread());
- if (ed != 0) {
- ed->removeNativeEventFilter(this);
- }
- }
-#endif // Q_OS_MAC
unsetShortcut();
}
@@ -76,11 +90,13 @@ bool QxtGlobalShortcutPrivate::setShortcut(const QKeySequence& shortcut)
const quint32 nativeMods = nativeModifiers(mods);
const bool res = registerShortcut(nativeKey, nativeMods);
if (res)
+ {
#ifndef Q_OS_MAC
shortcuts.insertMulti(qMakePair(nativeKey, nativeMods), &qxt_p());
#else
shortcuts.insert(qMakePair(nativeKey, nativeMods), &qxt_p());
#endif
+ }
else
qWarning() << "QxtGlobalShortcut failed to register:" << QKeySequence(key + mods).toString();
return res;
@@ -102,10 +118,14 @@ bool QxtGlobalShortcutPrivate::unsetShortcut()
else
qWarning() << "QxtGlobalShortcut failed to unregister:" << QKeySequence(key + mods).toString();
#else
- auto list = shortcuts.values(qMakePair(nativeKey, nativeMods));
+ using IT = decltype(shortcuts.end());
+ const auto pair = qMakePair(nativeKey, nativeMods);
+ IT it = shortcuts.find(pair);
bool found = false;
- for (auto it = list.begin(); it != list.end(); it++)
+ for (; it != shortcuts.end(); it++)
{
+ if (it.key() != pair) // DO NOT REMOVE
+ break;
if (*it == &qxt_p())
{
found = true;
@@ -126,13 +146,17 @@ bool QxtGlobalShortcutPrivate::unsetShortcut()
void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativeMods)
{
#ifndef Q_OS_MAC
- auto list = shortcuts.values(qMakePair(nativeKey, nativeMods));
+ using IT = decltype(shortcuts.end());
+ const auto pair = qMakePair(nativeKey, nativeMods);
+ IT it = shortcuts.find(pair);
- for (auto it = list.begin(); it != list.end(); it++)
+ for (; it != shortcuts.end(); it++)
{
+ if (it.key() != pair) // DO NOT REMOVE
+ break;
auto ptr = *it;
if (ptr->isEnabled())
- ptr->activated();
+ emit ptr->activated();
}
#else
QxtGlobalShortcut* shortcut = shortcuts.value(qMakePair(nativeKey, nativeMods));