From 22c91df2ec6c7dbe4cbb8410cd0e00147246fa6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Sat, 18 Mar 2017 11:00:30 +0100 Subject: [PATCH] Add support for global touchpad swipe gestures Summary: This change adds global touchpad swipe gestures to the GlobalShortcutsManager and hooks up the swipe gestures as defined at the Plasma Affenfels sprint: * swipe up: Desktop Grid * swipe down: Present Windows * swipe left: previous virtual desktop * swipe right: next virtual desktop The main work is handled by two new classes: SwipeGesture and GestureRecognizer. This is implemented in a way that it can be extended to also recognize touch screen gestures and pinch gestures. The SwipeGesture defines what is required for the gesture to trigger. Currently this includes the minimum and maximum number of fingers participating in the gesture and the direction. The gesture gets registered in the GestureRecognizer. The events for the gesture are fed into the GestureRecognizer. It evaluates which gestures could trigger and tracks them for every update of the gesture. In the process of the gesture tracking the GestureRecognizer emits signals on the Gesture: * started: when the Gesture gets considered for a sequence * cancelled: the Gesture no longer matches the sequence * triggered: the sequence ended and the Gesture still matches The remaining changes are related to hook up the existing shortcut framework with the new touchpad gestures. The GlobalShortcutManager gained support for it, InputRedirection and EffectsHandler offer methods to register a QAction. VirtualDesktopManager, PresentWindows and DesktopGrid are adjusted to support the gesture. Reviewers: #kwin, #plasma_on_wayland Subscribers: plasma-devel Tags: #plasma_on_wayland Differential Revision: https://phabricator.kde.org/D5097 --- CMakeLists.txt | 1 + autotests/CMakeLists.txt | 16 + autotests/mock_effectshandler.h | 1 + autotests/test_gestures.cpp | 382 ++++++++++++++++++++++ autotests/test_screen_edges.cpp | 4 + autotests/test_virtual_desktops.cpp | 4 + effects.cpp | 5 + effects.h | 1 + effects/desktopgrid/desktopgrid.cpp | 1 + effects/presentwindows/presentwindows.cpp | 1 + gestures.cpp | 145 ++++++++ gestures.h | 133 ++++++++ globalshortcuts.cpp | 78 ++++- globalshortcuts.h | 22 ++ input.cpp | 25 ++ input.h | 1 + libkwineffects/CMakeLists.txt | 2 +- libkwineffects/kwineffects.h | 9 + libkwineffects/kwinglobals.h | 12 + virtualdesktops.cpp | 12 +- virtualdesktops.h | 5 +- 21 files changed, 852 insertions(+), 8 deletions(-) create mode 100644 autotests/test_gestures.cpp create mode 100644 gestures.cpp create mode 100644 gestures.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d2d1bf170..9f6205d954 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -436,6 +436,7 @@ set(kwin_KDEINIT_SRCS appmenu.cpp modifier_only_shortcuts.cpp xkb.cpp + gestures.cpp ) if(KWIN_BUILD_TABBOX) diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 174653dc67..bffe6c3841 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -333,3 +333,19 @@ target_link_libraries(testOnScreenNotification add_test(kwin-testOnScreenNotification testOnScreenNotification) ecm_mark_as_test(testOnScreenNotification) + +######################################################## +# Test Gestures +######################################################## +set( testGestures_SRCS + test_gestures.cpp + ../gestures.cpp +) +add_executable( testGestures ${testGestures_SRCS}) + +target_link_libraries(testGestures + Qt5::Test +) + +add_test(kwin-testGestures testGestures) +ecm_mark_as_test(testGestures) diff --git a/autotests/mock_effectshandler.h b/autotests/mock_effectshandler.h index 7c01b18792..1f6bb5bd1a 100644 --- a/autotests/mock_effectshandler.h +++ b/autotests/mock_effectshandler.h @@ -177,6 +177,7 @@ public: void registerAxisShortcut(Qt::KeyboardModifiers, KWin::PointerAxisDirection, QAction *) override {} void registerGlobalShortcut(const QKeySequence &, QAction *) override {} void registerPointerShortcut(Qt::KeyboardModifiers, Qt::MouseButton, QAction *) override {} + void registerTouchpadSwipeShortcut(KWin::SwipeDirection, QAction *) override {} void reloadEffect(KWin::Effect *) override {} void removeSupportProperty(const QByteArray &, KWin::Effect *) override {} void reserveElectricBorder(KWin::ElectricBorder, KWin::Effect *) override {} diff --git a/autotests/test_gestures.cpp b/autotests/test_gestures.cpp new file mode 100644 index 0000000000..da32e72ab2 --- /dev/null +++ b/autotests/test_gestures.cpp @@ -0,0 +1,382 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2017 Martin Gräßlin + +This program 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 2 of the License, or +(at your option) any later version. + +This program 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 this program. If not, see . +*********************************************************************/ +#include "../gestures.h" + +#include +#include + +using namespace KWin; + +class GestureTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void testSwipeMinFinger_data(); + void testSwipeMinFinger(); + void testSwipeMaxFinger_data(); + void testSwipeMaxFinger(); + void testDirection_data(); + void testDirection(); + void testUnregisterSwipeCancels(); + void testDeleteSwipeCancels(); + void testSwipeCancel_data(); + void testSwipeCancel(); + void testSwipeUpdateCancel(); + void testSwipeUpdateTrigger_data(); + void testSwipeUpdateTrigger(); + void testSwipeMinFingerStart_data(); + void testSwipeMinFingerStart(); + void testSwipeMaxFingerStart_data(); + void testSwipeMaxFingerStart(); + void testSwipeDiagonalCancels_data(); + void testSwipeDiagonalCancels(); +}; + +void GestureTest::testSwipeMinFinger_data() +{ + QTest::addColumn("count"); + QTest::addColumn("expectedCount"); + + QTest::newRow("0") << 0u << 0u; + QTest::newRow("1") << 1u << 1u; + QTest::newRow("10") << 10u << 10u; +} + +void GestureTest::testSwipeMinFinger() +{ + SwipeGesture gesture; + QCOMPARE(gesture.minimumFingerCountIsRelevant(), false); + QCOMPARE(gesture.minimumFingerCount(), 0u); + QFETCH(uint, count); + gesture.setMinimumFingerCount(count); + QCOMPARE(gesture.minimumFingerCountIsRelevant(), true); + QTEST(gesture.minimumFingerCount(), "expectedCount"); + gesture.setMinimumFingerCount(0); + QCOMPARE(gesture.minimumFingerCountIsRelevant(), true); + QCOMPARE(gesture.minimumFingerCount(), 0u); +} + +void GestureTest::testSwipeMaxFinger_data() +{ + QTest::addColumn("count"); + QTest::addColumn("expectedCount"); + + QTest::newRow("0") << 0u << 0u; + QTest::newRow("1") << 1u << 1u; + QTest::newRow("10") << 10u << 10u; +} + +void GestureTest::testSwipeMaxFinger() +{ + SwipeGesture gesture; + QCOMPARE(gesture.maximumFingerCountIsRelevant(), false); + QCOMPARE(gesture.maximumFingerCount(), 0u); + QFETCH(uint, count); + gesture.setMaximumFingerCount(count); + QCOMPARE(gesture.maximumFingerCountIsRelevant(), true); + QTEST(gesture.maximumFingerCount(), "expectedCount"); + gesture.setMaximumFingerCount(0); + QCOMPARE(gesture.maximumFingerCountIsRelevant(), true); + QCOMPARE(gesture.maximumFingerCount(), 0u); +} + +void GestureTest::testDirection_data() +{ + QTest::addColumn("direction"); + + QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up; + QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left; + QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right; + QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down; +} + +void GestureTest::testDirection() +{ + SwipeGesture gesture; + QCOMPARE(gesture.direction(), SwipeGesture::Direction::Down); + QFETCH(KWin::SwipeGesture::Direction, direction); + gesture.setDirection(direction); + QCOMPARE(gesture.direction(), direction); + // back to down + gesture.setDirection(SwipeGesture::Direction::Down); + QCOMPARE(gesture.direction(), SwipeGesture::Direction::Down); +} + +void GestureTest::testUnregisterSwipeCancels() +{ + GestureRecognizer recognizer; + QScopedPointer gesture(new SwipeGesture); + QSignalSpy startedSpy(gesture.data(), &SwipeGesture::started); + QVERIFY(startedSpy.isValid()); + QSignalSpy cancelledSpy(gesture.data(), &SwipeGesture::cancelled); + QVERIFY(cancelledSpy.isValid()); + + recognizer.registerGesture(gesture.data()); + recognizer.startSwipeGesture(1); + QCOMPARE(startedSpy.count(), 1); + QCOMPARE(cancelledSpy.count(), 0); + recognizer.unregisterGesture(gesture.data()); + QCOMPARE(cancelledSpy.count(), 1); + + // delete the gesture should not trigger cancel + gesture.reset(); + QCOMPARE(cancelledSpy.count(), 1); +} + +void GestureTest::testDeleteSwipeCancels() +{ + GestureRecognizer recognizer; + QScopedPointer gesture(new SwipeGesture); + QSignalSpy startedSpy(gesture.data(), &SwipeGesture::started); + QVERIFY(startedSpy.isValid()); + QSignalSpy cancelledSpy(gesture.data(), &SwipeGesture::cancelled); + QVERIFY(cancelledSpy.isValid()); + + recognizer.registerGesture(gesture.data()); + recognizer.startSwipeGesture(1); + QCOMPARE(startedSpy.count(), 1); + QCOMPARE(cancelledSpy.count(), 0); + gesture.reset(); + QCOMPARE(cancelledSpy.count(), 1); +} + +void GestureTest::testSwipeCancel_data() +{ + QTest::addColumn("direction"); + + QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up; + QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left; + QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right; + QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down; +} + +void GestureTest::testSwipeCancel() +{ + GestureRecognizer recognizer; + QScopedPointer gesture(new SwipeGesture); + QFETCH(SwipeGesture::Direction, direction); + gesture->setDirection(direction); + QSignalSpy startedSpy(gesture.data(), &SwipeGesture::started); + QVERIFY(startedSpy.isValid()); + QSignalSpy cancelledSpy(gesture.data(), &SwipeGesture::cancelled); + QVERIFY(cancelledSpy.isValid()); + QSignalSpy triggeredSpy(gesture.data(), &SwipeGesture::triggered); + QVERIFY(triggeredSpy.isValid()); + + recognizer.registerGesture(gesture.data()); + recognizer.startSwipeGesture(1); + QCOMPARE(startedSpy.count(), 1); + QCOMPARE(cancelledSpy.count(), 0); + recognizer.cancelSwipeGesture(); + QCOMPARE(cancelledSpy.count(), 1); + QCOMPARE(triggeredSpy.count(), 0); +} + +void GestureTest::testSwipeUpdateCancel() +{ + GestureRecognizer recognizer; + SwipeGesture upGesture; + upGesture.setDirection(SwipeGesture::Direction::Up); + SwipeGesture downGesture; + downGesture.setDirection(SwipeGesture::Direction::Down); + SwipeGesture rightGesture; + rightGesture.setDirection(SwipeGesture::Direction::Right); + SwipeGesture leftGesture; + leftGesture.setDirection(SwipeGesture::Direction::Left); + + QSignalSpy upCancelledSpy(&upGesture, &SwipeGesture::cancelled); + QVERIFY(upCancelledSpy.isValid()); + QSignalSpy downCancelledSpy(&downGesture, &SwipeGesture::cancelled); + QVERIFY(downCancelledSpy.isValid()); + QSignalSpy rightCancelledSpy(&rightGesture, &SwipeGesture::cancelled); + QVERIFY(rightCancelledSpy.isValid()); + QSignalSpy leftCancelledSpy(&leftGesture, &SwipeGesture::cancelled); + QVERIFY(leftCancelledSpy.isValid()); + + QSignalSpy upTriggeredSpy(&upGesture, &SwipeGesture::triggered); + QVERIFY(upTriggeredSpy.isValid()); + QSignalSpy downTriggeredSpy(&downGesture, &SwipeGesture::triggered); + QVERIFY(downTriggeredSpy.isValid()); + QSignalSpy rightTriggeredSpy(&rightGesture, &SwipeGesture::triggered); + QVERIFY(rightTriggeredSpy.isValid()); + QSignalSpy leftTriggeredSpy(&leftGesture, &SwipeGesture::triggered); + QVERIFY(leftTriggeredSpy.isValid()); + + recognizer.registerGesture(&upGesture); + recognizer.registerGesture(&downGesture); + recognizer.registerGesture(&rightGesture); + recognizer.registerGesture(&leftGesture); + + recognizer.startSwipeGesture(4); + + // first a down gesture + recognizer.updateSwipeGesture(QSizeF(1, 20)); + QCOMPARE(upCancelledSpy.count(), 1); + QCOMPARE(downCancelledSpy.count(), 0); + QCOMPARE(leftCancelledSpy.count(), 1); + QCOMPARE(rightCancelledSpy.count(), 1); + // another down gesture + recognizer.updateSwipeGesture(QSizeF(-2, 10)); + QCOMPARE(downCancelledSpy.count(), 0); + // and an up gesture + recognizer.updateSwipeGesture(QSizeF(-2, -10)); + QCOMPARE(upCancelledSpy.count(), 1); + QCOMPARE(downCancelledSpy.count(), 1); + QCOMPARE(leftCancelledSpy.count(), 1); + QCOMPARE(rightCancelledSpy.count(), 1); + + recognizer.endSwipeGesture(); + QCOMPARE(upCancelledSpy.count(), 1); + QCOMPARE(downCancelledSpy.count(), 1); + QCOMPARE(leftCancelledSpy.count(), 1); + QCOMPARE(rightCancelledSpy.count(), 1); + QCOMPARE(upTriggeredSpy.count(), 0); + QCOMPARE(downTriggeredSpy.count(), 0); + QCOMPARE(leftTriggeredSpy.count(), 0); + QCOMPARE(rightTriggeredSpy.count(), 0); +} + +void GestureTest::testSwipeUpdateTrigger_data() +{ + QTest::addColumn("direction"); + QTest::addColumn("delta"); + + QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up << QSizeF(2, -3); + QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left << QSizeF(-3, 1); + QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right << QSizeF(20, -19); + QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down << QSizeF(0, 50); +} + +void GestureTest::testSwipeUpdateTrigger() +{ + GestureRecognizer recognizer; + SwipeGesture gesture; + QFETCH(SwipeGesture::Direction, direction); + gesture.setDirection(direction); + + QSignalSpy triggeredSpy(&gesture, &SwipeGesture::triggered); + QVERIFY(triggeredSpy.isValid()); + QSignalSpy cancelledSpy(&gesture, &SwipeGesture::cancelled); + QVERIFY(cancelledSpy.isValid()); + + recognizer.registerGesture(&gesture); + + recognizer.startSwipeGesture(1); + QFETCH(QSizeF, delta); + recognizer.updateSwipeGesture(delta); + QCOMPARE(cancelledSpy.count(), 0); + QCOMPARE(triggeredSpy.count(), 0); + + recognizer.endSwipeGesture(); + QCOMPARE(cancelledSpy.count(), 0); + QCOMPARE(triggeredSpy.count(), 1); +} + +void GestureTest::testSwipeMinFingerStart_data() +{ + QTest::addColumn("min"); + QTest::addColumn("count"); + QTest::addColumn("started"); + + QTest::newRow("same") << 1u << 1u << true; + QTest::newRow("less") << 2u << 1u << false; + QTest::newRow("more") << 1u << 2u << true; +} + +void GestureTest::testSwipeMinFingerStart() +{ + GestureRecognizer recognizer; + SwipeGesture gesture; + QFETCH(uint, min); + gesture.setMinimumFingerCount(min); + + QSignalSpy startedSpy(&gesture, &SwipeGesture::started); + QVERIFY(startedSpy.isValid()); + + recognizer.registerGesture(&gesture); + QFETCH(uint, count); + recognizer.startSwipeGesture(count); + QTEST(!startedSpy.isEmpty(), "started"); +} + +void GestureTest::testSwipeMaxFingerStart_data() +{ + QTest::addColumn("max"); + QTest::addColumn("count"); + QTest::addColumn("started"); + + QTest::newRow("same") << 1u << 1u << true; + QTest::newRow("less") << 2u << 1u << true; + QTest::newRow("more") << 1u << 2u << false; +} + +void GestureTest::testSwipeMaxFingerStart() +{ + GestureRecognizer recognizer; + SwipeGesture gesture; + QFETCH(uint, max); + gesture.setMaximumFingerCount(max); + + QSignalSpy startedSpy(&gesture, &SwipeGesture::started); + QVERIFY(startedSpy.isValid()); + + recognizer.registerGesture(&gesture); + QFETCH(uint, count); + recognizer.startSwipeGesture(count); + QTEST(!startedSpy.isEmpty(), "started"); +} + +void GestureTest::testSwipeDiagonalCancels_data() +{ + QTest::addColumn("direction"); + + QTest::newRow("Up") << KWin::SwipeGesture::Direction::Up; + QTest::newRow("Left") << KWin::SwipeGesture::Direction::Left; + QTest::newRow("Right") << KWin::SwipeGesture::Direction::Right; + QTest::newRow("Down") << KWin::SwipeGesture::Direction::Down; +} + +void GestureTest::testSwipeDiagonalCancels() +{ + GestureRecognizer recognizer; + SwipeGesture gesture; + QFETCH(SwipeGesture::Direction, direction); + gesture.setDirection(direction); + + QSignalSpy triggeredSpy(&gesture, &SwipeGesture::triggered); + QVERIFY(triggeredSpy.isValid()); + QSignalSpy cancelledSpy(&gesture, &SwipeGesture::cancelled); + QVERIFY(cancelledSpy.isValid()); + + recognizer.registerGesture(&gesture); + + recognizer.startSwipeGesture(1); + recognizer.updateSwipeGesture(QSizeF(1, 1)); + QCOMPARE(cancelledSpy.count(), 1); + QCOMPARE(triggeredSpy.count(), 0); + + recognizer.endSwipeGesture(); + QCOMPARE(cancelledSpy.count(), 1); + QCOMPARE(triggeredSpy.count(), 0); + +} + +QTEST_MAIN(GestureTest) +#include "test_gestures.moc" diff --git a/autotests/test_screen_edges.cpp b/autotests/test_screen_edges.cpp index 0ab99844f7..e60856f8da 100644 --- a/autotests/test_screen_edges.cpp +++ b/autotests/test_screen_edges.cpp @@ -87,6 +87,10 @@ void InputRedirection::registerAxisShortcut(Qt::KeyboardModifiers modifiers, Poi Q_UNUSED(action) } +void InputRedirection::registerTouchpadSwipeShortcut(SwipeDirection, QAction*) +{ +} + void updateXTime() { } diff --git a/autotests/test_virtual_desktops.cpp b/autotests/test_virtual_desktops.cpp index 03fcb5a752..6b7a59b1eb 100644 --- a/autotests/test_virtual_desktops.cpp +++ b/autotests/test_virtual_desktops.cpp @@ -44,6 +44,10 @@ void InputRedirection::registerAxisShortcut(Qt::KeyboardModifiers modifiers, Poi Q_UNUSED(action) } +void InputRedirection::registerTouchpadSwipeShortcut(SwipeDirection, QAction*) +{ +} + } Q_DECLARE_METATYPE(Qt::Orientation) diff --git a/effects.cpp b/effects.cpp index 354b083d94..7bc454345f 100644 --- a/effects.cpp +++ b/effects.cpp @@ -723,6 +723,11 @@ void EffectsHandlerImpl::registerAxisShortcut(Qt::KeyboardModifiers modifiers, P input()->registerAxisShortcut(modifiers, axis, action); } +void EffectsHandlerImpl::registerTouchpadSwipeShortcut(SwipeDirection direction, QAction *action) +{ + input()->registerTouchpadSwipeShortcut(direction, action); +} + void* EffectsHandlerImpl::getProxy(QString name) { for (QVector< EffectPair >::const_iterator it = loaded_effects.constBegin(); it != loaded_effects.constEnd(); ++it) diff --git a/effects.h b/effects.h index b1c96635ad..0720313931 100644 --- a/effects.h +++ b/effects.h @@ -125,6 +125,7 @@ public: void registerGlobalShortcut(const QKeySequence &shortcut, QAction *action) override; void registerPointerShortcut(Qt::KeyboardModifiers modifiers, Qt::MouseButton pointerButtons, QAction *action) override; void registerAxisShortcut(Qt::KeyboardModifiers modifiers, PointerAxisDirection axis, QAction *action) override; + void registerTouchpadSwipeShortcut(SwipeDirection direction, QAction *action) override; void* getProxy(QString name) override; void startMousePolling() override; void stopMousePolling() override; diff --git a/effects/desktopgrid/desktopgrid.cpp b/effects/desktopgrid/desktopgrid.cpp index 6e3c372033..2d2a888f92 100644 --- a/effects/desktopgrid/desktopgrid.cpp +++ b/effects/desktopgrid/desktopgrid.cpp @@ -73,6 +73,7 @@ DesktopGridEffect::DesktopGridEffect() KGlobalAccel::self()->setShortcut(a, QList() << Qt::CTRL + Qt::Key_F8); shortcut = KGlobalAccel::self()->shortcut(a); effects->registerGlobalShortcut(Qt::CTRL + Qt::Key_F8, a); + effects->registerTouchpadSwipeShortcut(SwipeDirection::Up, a); connect(a, SIGNAL(triggered(bool)), this, SLOT(toggle())); connect(KGlobalAccel::self(), &KGlobalAccel::globalShortcutChanged, this, &DesktopGridEffect::globalShortcutChanged); connect(effects, SIGNAL(windowAdded(KWin::EffectWindow*)), this, SLOT(slotWindowAdded(KWin::EffectWindow*))); diff --git a/effects/presentwindows/presentwindows.cpp b/effects/presentwindows/presentwindows.cpp index 135fbfb654..3f757008d8 100755 --- a/effects/presentwindows/presentwindows.cpp +++ b/effects/presentwindows/presentwindows.cpp @@ -80,6 +80,7 @@ PresentWindowsEffect::PresentWindowsEffect() KGlobalAccel::self()->setShortcut(exposeAllAction, QList() << Qt::CTRL + Qt::Key_F10 << Qt::Key_LaunchC); shortcutAll = KGlobalAccel::self()->shortcut(exposeAllAction); effects->registerGlobalShortcut(Qt::CTRL + Qt::Key_F10, exposeAllAction); + effects->registerTouchpadSwipeShortcut(SwipeDirection::Down, exposeAllAction); connect(exposeAllAction, SIGNAL(triggered(bool)), this, SLOT(toggleActiveAllDesktops())); QAction* exposeClassAction = new QAction(this); exposeClassAction->setObjectName(QStringLiteral("ExposeClass")); diff --git a/gestures.cpp b/gestures.cpp new file mode 100644 index 0000000000..08ebd8fdc4 --- /dev/null +++ b/gestures.cpp @@ -0,0 +1,145 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2017 Martin Gräßlin + +This program 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 2 of the License, or +(at your option) any later version. + +This program 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 this program. If not, see . +*********************************************************************/ +#include "gestures.h" + +#include +#include + +namespace KWin +{ + +Gesture::Gesture(QObject *parent) + : QObject(parent) +{ +} + +Gesture::~Gesture() = default; + +SwipeGesture::SwipeGesture(QObject *parent) + : Gesture(parent) +{ +} + +SwipeGesture::~SwipeGesture() = default; + +GestureRecognizer::GestureRecognizer(QObject *parent) + : QObject(parent) +{ +} + +GestureRecognizer::~GestureRecognizer() = default; + +void GestureRecognizer::registerGesture(KWin::Gesture* gesture) +{ + Q_ASSERT(!m_gestures.contains(gesture)); + auto connection = connect(gesture, &QObject::destroyed, this, std::bind(&GestureRecognizer::unregisterGesture, this, gesture)); + m_destroyConnections.insert(gesture, connection); + m_gestures << gesture; +} + +void GestureRecognizer::unregisterGesture(KWin::Gesture* gesture) +{ + auto it = m_destroyConnections.find(gesture); + if (it != m_destroyConnections.end()) { + disconnect(it.value()); + m_destroyConnections.erase(it); + } + m_gestures.removeAll(gesture); + if (m_activeSwipeGestures.removeOne(gesture)) { + emit gesture->cancelled(); + } +} + +void GestureRecognizer::startSwipeGesture(uint fingerCount) +{ + // TODO: verify that no gesture is running + for (Gesture *gesture : qAsConst(m_gestures)) { + SwipeGesture *swipeGesture = qobject_cast(gesture); + if (!gesture) { + continue; + } + if (swipeGesture->minimumFingerCountIsRelevant()) { + if (swipeGesture->minimumFingerCount() > fingerCount) { + continue; + } + } + if (swipeGesture->maximumFingerCountIsRelevant()) { + if (swipeGesture->maximumFingerCount() < fingerCount) { + continue; + } + } + // direction doesn't matter yet + m_activeSwipeGestures << swipeGesture; + emit swipeGesture->started(); + } +} + +void GestureRecognizer::updateSwipeGesture(const QSizeF &delta) +{ + m_swipeUpdates << delta; + // determine the direction of the swipe + if (delta.width() == delta.height()) { + // special case of diagonal, this is not yet supported, thus cancel all gestures + cancelActiveSwipeGestures(); + return; + } + SwipeGesture::Direction direction; + if (std::abs(delta.width()) > std::abs(delta.height())) { + // horizontal + direction = delta.width() < 0 ? SwipeGesture::Direction::Left : SwipeGesture::Direction::Right; + } else { + // vertical + direction = delta.height() < 0 ? SwipeGesture::Direction::Up : SwipeGesture::Direction::Down; + } + for (auto it = m_activeSwipeGestures.begin(); it != m_activeSwipeGestures.end();) { + auto g = qobject_cast(*it); + if (g->direction() == direction) { + it++; + } else { + emit g->cancelled(); + it = m_activeSwipeGestures.erase(it); + } + } +} + +void GestureRecognizer::cancelActiveSwipeGestures() +{ + for (auto g : qAsConst(m_activeSwipeGestures)) { + emit g->cancelled(); + } + m_activeSwipeGestures.clear(); +} + +void GestureRecognizer::cancelSwipeGesture() +{ + cancelActiveSwipeGestures(); + m_swipeUpdates.clear(); +} + +void GestureRecognizer::endSwipeGesture() +{ + for (auto g : qAsConst(m_activeSwipeGestures)) { + emit g->triggered(); + } + m_activeSwipeGestures.clear(); + m_swipeUpdates.clear(); +} + +} diff --git a/gestures.h b/gestures.h new file mode 100644 index 0000000000..802b515102 --- /dev/null +++ b/gestures.h @@ -0,0 +1,133 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2017 Martin Gräßlin + +This program 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 2 of the License, or +(at your option) any later version. + +This program 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 this program. If not, see . +*********************************************************************/ +#ifndef KWIN_GESTURES_H +#define KWIN_GESTURES_H + +#include +#include +#include + +namespace KWin +{ + +class Gesture : public QObject +{ + Q_OBJECT +public: + ~Gesture() override; +protected: + explicit Gesture(QObject *parent); + +Q_SIGNALS: + /** + * Matching of a gesture started and this Gesture might match. + * On further evaluation either the signal @link{triggered} or + * @link{cancelled} will get emitted. + **/ + void started(); + /** + * Gesture matching ended and this Gesture matched. + **/ + void triggered(); + /** + * This Gesture no longer matches. + **/ + void cancelled(); +}; + +class SwipeGesture : public Gesture +{ + Q_OBJECT +public: + enum class Direction { + Down, + Left, + Up, + Right + }; + + explicit SwipeGesture(QObject *parent = nullptr); + ~SwipeGesture() override; + + bool minimumFingerCountIsRelevant() const { + return m_minimumFingerCountRelevant; + } + void setMinimumFingerCount(uint count) { + m_minimumFingerCount = count; + m_minimumFingerCountRelevant = true; + } + uint minimumFingerCount() const { + return m_minimumFingerCount; + } + + bool maximumFingerCountIsRelevant() const { + return m_maximumFingerCountRelevant; + } + void setMaximumFingerCount(uint count) { + m_maximumFingerCount = count; + m_maximumFingerCountRelevant = true; + } + uint maximumFingerCount() const { + return m_maximumFingerCount; + } + + Direction direction() const { + return m_direction; + } + void setDirection(Direction direction) { + m_direction = direction; + } + +private: + bool m_minimumFingerCountRelevant = false; + uint m_minimumFingerCount = 0; + bool m_maximumFingerCountRelevant = false; + uint m_maximumFingerCount = 0; + Direction m_direction = Direction::Down; +}; + +class GestureRecognizer : public QObject +{ + Q_OBJECT +public: + GestureRecognizer(QObject *parent = nullptr); + ~GestureRecognizer() override; + + void registerGesture(Gesture *gesture); + void unregisterGesture(Gesture *gesture); + + void startSwipeGesture(uint fingerCount); + void updateSwipeGesture(const QSizeF &delta); + void cancelSwipeGesture(); + void endSwipeGesture(); + +private: + void cancelActiveSwipeGestures(); + QVector m_gestures; + QVector m_activeSwipeGestures; + QMap m_destroyConnections; + QVector m_swipeUpdates; +}; + +} + +Q_DECLARE_METATYPE(KWin::SwipeGesture::Direction) + +#endif diff --git a/globalshortcuts.cpp b/globalshortcuts.cpp index 160b7ab96c..5747de3942 100644 --- a/globalshortcuts.cpp +++ b/globalshortcuts.cpp @@ -22,6 +22,7 @@ along with this program. If not, see . // kwin #include #include "main.h" +#include "gestures.h" #include "utils.h" // KDE #include @@ -32,6 +33,11 @@ along with this program. If not, see . namespace KWin { +uint qHash(SwipeDirection direction) +{ + return uint(direction); +} + GlobalShortcut::GlobalShortcut(const QKeySequence &shortcut) : m_shortcut(shortcut) , m_pointerModifiers(Qt::NoModifier) @@ -56,6 +62,15 @@ GlobalShortcut::GlobalShortcut(Qt::KeyboardModifiers modifiers, PointerAxisDirec { } +GlobalShortcut::GlobalShortcut(SwipeDirection direction) + : m_shortcut(QKeySequence()) + , m_pointerModifiers(Qt::NoModifier) + , m_pointerButtons(Qt::NoButton) + , m_axis(PointerAxisUp) + , m_swipeDirection(direction) +{ +} + GlobalShortcut::~GlobalShortcut() { } @@ -79,6 +94,35 @@ InternalGlobalShortcut::InternalGlobalShortcut(Qt::KeyboardModifiers axisModifie { } +static SwipeGesture::Direction toSwipeDirection(SwipeDirection direction) +{ + switch (direction) { + case SwipeDirection::Up: + return SwipeGesture::Direction::Up; + case SwipeDirection::Down: + return SwipeGesture::Direction::Down; + case SwipeDirection::Left: + return SwipeGesture::Direction::Left; + case SwipeDirection::Right: + return SwipeGesture::Direction::Right; + case SwipeDirection::Invalid: + default: + Q_UNREACHABLE(); + } +} + +InternalGlobalShortcut::InternalGlobalShortcut(Qt::KeyboardModifiers swipeModifier, SwipeDirection direction, QAction *action) + : GlobalShortcut(direction) + , m_action(action) + , m_swipe(new SwipeGesture) +{ + Q_UNUSED(swipeModifier) + m_swipe->setDirection(toSwipeDirection(direction)); + m_swipe->setMinimumFingerCount(4); + m_swipe->setMaximumFingerCount(4); + QObject::connect(m_swipe.data(), &SwipeGesture::triggered, m_action, &QAction::trigger, Qt::QueuedConnection); +} + InternalGlobalShortcut::~InternalGlobalShortcut() { } @@ -91,6 +135,7 @@ void InternalGlobalShortcut::invoke() GlobalShortcutsManager::GlobalShortcutsManager(QObject *parent) : QObject(parent) + , m_gestureRecognizer(new GestureRecognizer(this)) { } @@ -106,6 +151,7 @@ GlobalShortcutsManager::~GlobalShortcutsManager() { clearShortcuts(m_pointerShortcuts); clearShortcuts(m_axisShortcuts); + clearShortcuts(m_swipeShortcuts); } void GlobalShortcutsManager::init() @@ -146,10 +192,11 @@ void GlobalShortcutsManager::objectDeleted(QObject *object) { handleDestroyedAction(object, m_pointerShortcuts); handleDestroyedAction(object, m_axisShortcuts); + handleDestroyedAction(object, m_swipeShortcuts); } template -void addShortcut(T &shortcuts, QAction *action, Qt::KeyboardModifiers modifiers, R value) +GlobalShortcut *addShortcut(T &shortcuts, QAction *action, Qt::KeyboardModifiers modifiers, R value) { GlobalShortcut *cut = new InternalGlobalShortcut(modifiers, value, action); auto it = shortcuts.find(modifiers); @@ -161,6 +208,7 @@ void addShortcut(T &shortcuts, QAction *action, Qt::KeyboardModifiers modifiers, s.insert(value, cut); shortcuts.insert(modifiers, s); } + return cut; } void GlobalShortcutsManager::registerPointerShortcut(QAction *action, Qt::KeyboardModifiers modifiers, Qt::MouseButtons pointerButtons) @@ -175,6 +223,13 @@ void GlobalShortcutsManager::registerAxisShortcut(QAction *action, Qt::KeyboardM connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); } +void GlobalShortcutsManager::registerTouchpadSwipe(QAction *action, SwipeDirection direction) +{ + auto shortcut = addShortcut(m_swipeShortcuts, action, Qt::NoModifier, direction); + connect(action, &QAction::destroyed, this, &GlobalShortcutsManager::objectDeleted); + m_gestureRecognizer->registerGesture(static_cast(shortcut)->swipeGesture()); +} + template bool processShortcut(Qt::KeyboardModifiers mods, T key, U &shortcuts) { @@ -233,4 +288,25 @@ bool GlobalShortcutsManager::processAxis(Qt::KeyboardModifiers mods, PointerAxis return processShortcut(mods, axis, m_axisShortcuts); } +void GlobalShortcutsManager::processSwipeStart(uint fingerCount) +{ + m_gestureRecognizer->startSwipeGesture(fingerCount); +} + +void GlobalShortcutsManager::processSwipeUpdate(const QSizeF &delta) +{ + m_gestureRecognizer->updateSwipeGesture(delta); +} + +void GlobalShortcutsManager::processSwipeCancel() +{ + m_gestureRecognizer->cancelSwipeGesture(); +} + +void GlobalShortcutsManager::processSwipeEnd() +{ + m_gestureRecognizer->endSwipeGesture(); + // TODO: cancel on Wayland Seat if one triggered +} + } // namespace diff --git a/globalshortcuts.h b/globalshortcuts.h index ff8152761d..e213e5104f 100644 --- a/globalshortcuts.h +++ b/globalshortcuts.h @@ -32,6 +32,8 @@ namespace KWin { class GlobalShortcut; +class SwipeGesture; +class GestureRecognizer; /** * @brief Manager for the global shortcut system inside KWin. @@ -67,6 +69,8 @@ public: */ void registerAxisShortcut(QAction *action, Qt::KeyboardModifiers modifiers, PointerAxisDirection axis); + void registerTouchpadSwipe(QAction *action, SwipeDirection direction); + /** * @brief Processes a key event to decide whether a shortcut needs to be triggered. * @@ -94,6 +98,11 @@ public: */ bool processAxis(Qt::KeyboardModifiers modifiers, PointerAxisDirection axis); + void processSwipeStart(uint fingerCount); + void processSwipeUpdate(const QSizeF &delta); + void processSwipeCancel(); + void processSwipeEnd(); + void setKGlobalAccelInterface(KGlobalAccelInterface *interface) { m_kglobalAccelInterface = interface; } @@ -102,8 +111,10 @@ private: void objectDeleted(QObject *object); QHash > m_pointerShortcuts; QHash > m_axisShortcuts; + QHash > m_swipeShortcuts; KGlobalAccelD *m_kglobalAccel = nullptr; KGlobalAccelInterface *m_kglobalAccelInterface = nullptr; + GestureRecognizer *m_gestureRecognizer; }; class GlobalShortcut @@ -114,18 +125,23 @@ public: const QKeySequence &shortcut() const; Qt::KeyboardModifiers pointerButtonModifiers() const; Qt::MouseButtons pointerButtons() const; + SwipeDirection swipeDirection() const { + return m_swipeDirection; + } virtual void invoke() = 0; protected: GlobalShortcut(const QKeySequence &shortcut); GlobalShortcut(Qt::KeyboardModifiers pointerButtonModifiers, Qt::MouseButtons pointerButtons); GlobalShortcut(Qt::KeyboardModifiers axisModifiers, PointerAxisDirection axis); + GlobalShortcut(SwipeDirection direction); private: QKeySequence m_shortcut; Qt::KeyboardModifiers m_pointerModifiers; Qt::MouseButtons m_pointerButtons; PointerAxisDirection m_axis; + SwipeDirection m_swipeDirection = SwipeDirection::Invalid;; }; class InternalGlobalShortcut : public GlobalShortcut @@ -134,13 +150,19 @@ public: InternalGlobalShortcut(Qt::KeyboardModifiers modifiers, const QKeySequence &shortcut, QAction *action); InternalGlobalShortcut(Qt::KeyboardModifiers pointerButtonModifiers, Qt::MouseButtons pointerButtons, QAction *action); InternalGlobalShortcut(Qt::KeyboardModifiers axisModifiers, PointerAxisDirection axis, QAction *action); + InternalGlobalShortcut(Qt::KeyboardModifiers swipeModifier, SwipeDirection direction, QAction *action); virtual ~InternalGlobalShortcut(); void invoke() override; QAction *action() const; + + SwipeGesture *swipeGesture() const { + return m_swipe.data(); + } private: QAction *m_action; + QScopedPointer m_swipe; }; inline diff --git a/input.cpp b/input.cpp index 310129d69c..59b422e0ad 100644 --- a/input.cpp +++ b/input.cpp @@ -691,6 +691,26 @@ public: } return false; } + bool swipeGestureBegin(int fingerCount, quint32 time) override { + Q_UNUSED(time) + input()->shortcuts()->processSwipeStart(fingerCount); + return false; + } + bool swipeGestureUpdate(const QSizeF &delta, quint32 time) override { + Q_UNUSED(time) + input()->shortcuts()->processSwipeUpdate(delta); + return false; + } + bool swipeGestureCancelled(quint32 time) override { + Q_UNUSED(time) + input()->shortcuts()->processSwipeCancel(); + return false; + } + bool swipeGestureEnd(quint32 time) override { + Q_UNUSED(time) + input()->shortcuts()->processSwipeEnd(); + return false; + } }; class InternalWindowEventFilter : public InputEventFilter { @@ -1819,6 +1839,11 @@ void InputRedirection::registerAxisShortcut(Qt::KeyboardModifiers modifiers, Poi m_shortcuts->registerAxisShortcut(action, modifiers, axis); } +void InputRedirection::registerTouchpadSwipeShortcut(SwipeDirection direction, QAction *action) +{ + m_shortcuts->registerTouchpadSwipe(action, direction); +} + void InputRedirection::registerGlobalAccel(KGlobalAccelInterface *interface) { m_shortcuts->setKGlobalAccelInterface(interface); diff --git a/input.h b/input.h index 88eacb4f43..3d16b97324 100644 --- a/input.h +++ b/input.h @@ -108,6 +108,7 @@ public: 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); + void registerTouchpadSwipeShortcut(SwipeDirection direction, QAction *action); void registerGlobalAccel(KGlobalAccelInterface *interface); /** diff --git a/libkwineffects/CMakeLists.txt b/libkwineffects/CMakeLists.txt index e5fcd786c0..2c3162a030 100644 --- a/libkwineffects/CMakeLists.txt +++ b/libkwineffects/CMakeLists.txt @@ -5,7 +5,7 @@ ecm_setup_version(${PROJECT_VERSION} VARIABLE_PREFIX KWINEFFECTS VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kwineffects_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KWinEffectsConfigVersion.cmake" - SOVERSION 10 + SOVERSION 11 ) ### xrenderutils lib ### diff --git a/libkwineffects/kwineffects.h b/libkwineffects/kwineffects.h index 92f32a242c..cd55516a9e 100644 --- a/libkwineffects/kwineffects.h +++ b/libkwineffects/kwineffects.h @@ -875,6 +875,15 @@ public: **/ virtual void registerAxisShortcut(Qt::KeyboardModifiers modifiers, PointerAxisDirection axis, QAction *action) = 0; + /** + * @brief Registers a global touchpad swipe gesture shortcut with the provided @p action. + * + * @param direction The direction for the swipe + * @param action The action which gets triggered when the gesture triggers + * @since 5.10 + **/ + virtual void registerTouchpadSwipeShortcut(SwipeDirection direction, QAction *action) = 0; + /** * Retrieve the proxy class for an effect if it has one. Will return NULL if * the effect isn't loaded or doesn't have a proxy class. diff --git a/libkwineffects/kwinglobals.h b/libkwineffects/kwinglobals.h index ba57d2790e..d74dd7da16 100644 --- a/libkwineffects/kwinglobals.h +++ b/libkwineffects/kwinglobals.h @@ -129,6 +129,18 @@ enum PointerAxisDirection { PointerAxisRight }; +/** + * @brief Directions for swipe gestures + * @since 5.10 + **/ +enum class SwipeDirection { + Invalid, + Down, + Left, + Up, + Right +}; + inline KWIN_EXPORT xcb_connection_t *connection() { diff --git a/virtualdesktops.cpp b/virtualdesktops.cpp index 45f719b89c..53cdaaab59 100644 --- a/virtualdesktops.cpp +++ b/virtualdesktops.cpp @@ -534,8 +534,10 @@ void VirtualDesktopManager::initShortcuts() { initSwitchToShortcuts(); - addAction(QStringLiteral("Switch to Next Desktop"), i18n("Switch to Next Desktop"), &VirtualDesktopManager::slotNext); - addAction(QStringLiteral("Switch to Previous Desktop"), i18n("Switch to Previous Desktop"), &VirtualDesktopManager::slotPrevious); + QAction *nextAction = addAction(QStringLiteral("Switch to Next Desktop"), i18n("Switch to Next Desktop"), &VirtualDesktopManager::slotNext); + input()->registerTouchpadSwipeShortcut(SwipeDirection::Right, nextAction); + QAction *previousAction = addAction(QStringLiteral("Switch to Previous Desktop"), i18n("Switch to Previous Desktop"), &VirtualDesktopManager::slotPrevious); + input()->registerTouchpadSwipeShortcut(SwipeDirection::Left, previousAction); addAction(QStringLiteral("Switch One Desktop to the Right"), i18n("Switch One Desktop to the Right"), &VirtualDesktopManager::slotRight); addAction(QStringLiteral("Switch One Desktop to the Left"), i18n("Switch One Desktop to the Left"), &VirtualDesktopManager::slotLeft); addAction(QStringLiteral("Switch One Desktop Up"), i18n("Switch One Desktop Up"), &VirtualDesktopManager::slotUp); @@ -562,7 +564,7 @@ void VirtualDesktopManager::initSwitchToShortcuts() } } -void VirtualDesktopManager::addAction(const QString &name, const KLocalizedString &label, uint value, const QKeySequence &key, void (VirtualDesktopManager::*slot)()) +QAction *VirtualDesktopManager::addAction(const QString &name, const KLocalizedString &label, uint value, const QKeySequence &key, void (VirtualDesktopManager::*slot)()) { QAction *a = new QAction(this); a->setProperty("componentName", QStringLiteral(KWIN_NAME)); @@ -571,9 +573,10 @@ void VirtualDesktopManager::addAction(const QString &name, const KLocalizedStrin a->setData(value); KGlobalAccel::setGlobalShortcut(a, key); input()->registerShortcut(key, a, this, slot); + return a; } -void VirtualDesktopManager::addAction(const QString &name, const QString &label, void (VirtualDesktopManager::*slot)()) +QAction *VirtualDesktopManager::addAction(const QString &name, const QString &label, void (VirtualDesktopManager::*slot)()) { QAction *a = new QAction(this); a->setProperty("componentName", QStringLiteral(KWIN_NAME)); @@ -581,6 +584,7 @@ void VirtualDesktopManager::addAction(const QString &name, const QString &label, a->setText(label); KGlobalAccel::setGlobalShortcut(a, QKeySequence()); input()->registerShortcut(QKeySequence(), a, this, slot); + return a; } void VirtualDesktopManager::slotSwitchTo() diff --git a/virtualdesktops.h b/virtualdesktops.h index c8afd97343..da80e007f6 100644 --- a/virtualdesktops.h +++ b/virtualdesktops.h @@ -32,6 +32,7 @@ along with this program. If not, see . class KLocalizedString; class NETRootInfo; +class QAction; namespace KWin { @@ -420,7 +421,7 @@ private: * @param key The global shortcut for the action * @param slot The slot to invoke when the action is triggered **/ - void addAction(const QString &name, const KLocalizedString &label, uint value, const QKeySequence &key, void (VirtualDesktopManager::*slot)()); + QAction *addAction(const QString &name, const KLocalizedString &label, uint value, const QKeySequence &key, void (VirtualDesktopManager::*slot)()); /** * Creates an action and connects it to the @p slot in this Manager. * Overloaded method for the case that no additional value needs to be passed to the action and @@ -429,7 +430,7 @@ private: * @param label The localized name for the action to be created * @param slot The slot to invoke when the action is triggered **/ - void addAction(const QString &name, const QString &label, void (VirtualDesktopManager::*slot)()); + QAction *addAction(const QString &name, const QString &label, void (VirtualDesktopManager::*slot)()); QVector m_desktops; QPointer m_current;