From 857219546f74045b875f843d1a5bd4da386beea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Thu, 19 Feb 2015 10:38:49 +0100 Subject: [PATCH] Update x11Time when a global shortcut is pressed KGlobalAccel sets the timestamp as a property and we need to set our x11Time to it otherwise following keyboard grabs might fail. Requires 61e2a156678eef033b2629f7c72530dc78d7c3ac in kglobalaccel. --- autotests/test_screen_edges.cpp | 5 +++++ autotests/test_virtual_desktops.cpp | 5 +++++ input.cpp | 13 +++++++++++++ input.h | 20 +++++++++++++++++++- tabbox/tabbox.cpp | 3 +-- useractions.cpp | 3 +-- virtualdesktops.cpp | 6 ++---- 7 files changed, 46 insertions(+), 9 deletions(-) diff --git a/autotests/test_screen_edges.cpp b/autotests/test_screen_edges.cpp index b70391e5b7..270fcfae2b 100644 --- a/autotests/test_screen_edges.cpp +++ b/autotests/test_screen_edges.cpp @@ -83,6 +83,11 @@ void InputRedirection::registerAxisShortcut(Qt::KeyboardModifiers modifiers, Poi Q_UNUSED(action) } +void InputRedirection::registerShortcutForGlobalAccelTimestamp(QAction *action) +{ + Q_UNUSED(action) +} + void updateXTime() { } diff --git a/autotests/test_virtual_desktops.cpp b/autotests/test_virtual_desktops.cpp index 277a0c853f..3ad296e0a8 100644 --- a/autotests/test_virtual_desktops.cpp +++ b/autotests/test_virtual_desktops.cpp @@ -44,6 +44,11 @@ void InputRedirection::registerAxisShortcut(Qt::KeyboardModifiers modifiers, Poi Q_UNUSED(action) } +void InputRedirection::registerShortcutForGlobalAccelTimestamp(QAction *action) +{ + Q_UNUSED(action) +} + } Q_DECLARE_METATYPE(Qt::Orientation) diff --git a/input.cpp b/input.cpp index 2732a064f9..c6489a7110 100644 --- a/input.cpp +++ b/input.cpp @@ -551,6 +551,7 @@ Qt::KeyboardModifiers InputRedirection::keyboardModifiers() const void InputRedirection::registerShortcut(const QKeySequence &shortcut, QAction *action) { m_shortcuts->registerShortcut(action, shortcut); + registerShortcutForGlobalAccelTimestamp(action); } void InputRedirection::registerPointerShortcut(Qt::KeyboardModifiers modifiers, Qt::MouseButton pointerButtons, QAction *action) @@ -563,6 +564,18 @@ void InputRedirection::registerAxisShortcut(Qt::KeyboardModifiers modifiers, Poi m_shortcuts->registerAxisShortcut(action, modifiers, axis); } +void InputRedirection::registerShortcutForGlobalAccelTimestamp(QAction *action) +{ + connect(action, &QAction::triggered, kwinApp(), [action] { + QVariant timestamp = action->property("org.kde.kglobalaccel.activationTimestamp"); + bool ok = false; + const quint32 t = timestamp.toULongLong(&ok); + if (ok) { + kwinApp()->setX11Time(t); + } + }); +} + static bool screenContainsPos(const QPointF &pos) { for (int i = 0; i < screens()->count(); ++i) { diff --git a/input.h b/input.h index 5e7a3cbf94..05512b6a17 100644 --- a/input.h +++ b/input.h @@ -20,6 +20,7 @@ along with this program. If not, see . #ifndef KWIN_INPUT_H #define KWIN_INPUT_H #include +#include #include #include #include @@ -27,7 +28,6 @@ along with this program. If not, see . #include #include -class QAction; class QKeySequence; struct xkb_context; @@ -85,6 +85,16 @@ public: Qt::KeyboardModifiers keyboardModifiers() const; void registerShortcut(const QKeySequence &shortcut, QAction *action); + /** + * @overload + * + * Like registerShortcut, but also connects QAction::triggered to the @p slot on @p receiver. + * It's recommended to use this method as it ensures that the X11 timestamp is updated prior + * to the @p slot being invoked. If not using this overload it's required to ensure that + * registerShortcut is called before connecting to QAction's triggered signal. + **/ + template + void registerShortcut(const QKeySequence &shortcut, QAction *action, T *receiver, void (T::*slot)()); void registerPointerShortcut(Qt::KeyboardModifiers modifiers, Qt::MouseButton pointerButtons, QAction *action); void registerAxisShortcut(Qt::KeyboardModifiers modifiers, PointerAxisDirection axis, QAction *action); @@ -158,6 +168,7 @@ private: void setupLibInput(); void updatePointerPosition(const QPointF &pos); void updatePointerAfterScreenChange(); + void registerShortcutForGlobalAccelTimestamp(QAction *action); QPointF m_globalPointer; QHash m_pointerButtons; #if HAVE_XKB @@ -222,6 +233,13 @@ InputRedirection::PointerButtonState InputRedirection::pointerButtonState(uint32 } } +template +inline +void InputRedirection::registerShortcut(const QKeySequence &shortcut, QAction *action, T *receiver, void (T::*slot)()) { + registerShortcut(shortcut, action); + connect(action, &QAction::triggered, receiver, slot); +} + #if HAVE_XKB inline Qt::KeyboardModifiers Xkb::modifiers() const diff --git a/tabbox/tabbox.cpp b/tabbox/tabbox.cpp index f81a40717f..4a00e4b79a 100644 --- a/tabbox/tabbox.cpp +++ b/tabbox/tabbox.cpp @@ -500,8 +500,7 @@ void TabBox::key(const char *actionName, Slot slot, const QKeySequence &shortcut a->setObjectName(QString::fromUtf8(actionName)); a->setText(i18n(actionName)); KGlobalAccel::self()->setShortcut(a, QList() << shortcut); - connect(a, &QAction::triggered, TabBox::self(), slot); - input()->registerShortcut(shortcut, a); + input()->registerShortcut(shortcut, a, TabBox::self(), slot); auto cuts = KGlobalAccel::self()->shortcut(a); globalShortcutChanged(a, cuts.isEmpty() ? QKeySequence() : cuts.first()); } diff --git a/useractions.cpp b/useractions.cpp index 3491a64024..ce1c28e13a 100644 --- a/useractions.cpp +++ b/useractions.cpp @@ -937,8 +937,7 @@ void Workspace::initShortcut(const QString &actionName, const QString &descripti } KGlobalAccel::self()->setDefaultShortcut(a, QList() << shortcut); KGlobalAccel::self()->setShortcut(a, QList() << shortcut); - connect(a, &QAction::triggered, this, slot); - input()->registerShortcut(shortcut, a); + input()->registerShortcut(shortcut, a, this, slot); } /*! diff --git a/virtualdesktops.cpp b/virtualdesktops.cpp index 36ae63a6b6..a68ebb9b84 100644 --- a/virtualdesktops.cpp +++ b/virtualdesktops.cpp @@ -458,9 +458,8 @@ void VirtualDesktopManager::addAction(const QString &name, const KLocalizedStrin a->setObjectName(name.arg(value)); a->setText(label.subs(value).toString()); a->setData(value); - connect(a, &QAction::triggered, this, slot); KGlobalAccel::setGlobalShortcut(a, key); - input()->registerShortcut(key, a); + input()->registerShortcut(key, a, this, slot); } void VirtualDesktopManager::addAction(const QString &name, const QString &label, void (VirtualDesktopManager::*slot)()) @@ -468,9 +467,8 @@ void VirtualDesktopManager::addAction(const QString &name, const QString &label, QAction *a = new QAction(this); a->setObjectName(name); a->setText(label); - connect(a, &QAction::triggered, this, slot); KGlobalAccel::setGlobalShortcut(a, QKeySequence()); - input()->registerShortcut(QKeySequence(), a); + input()->registerShortcut(QKeySequence(), a, this, slot); } void VirtualDesktopManager::slotSwitchTo()