From 070b46f455667d0f9ddbccffc1a43f6a44a25f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Fl=C3=B6ser?= Date: Sat, 1 Dec 2018 14:52:47 +0100 Subject: [PATCH] Hide mouse cursor when interacting with touch screen Summary: On touch events the mouse cursor gets hidden, on next mouse event the mouse cursor is shown again. This IMHO significantly improves the system interaction if touch is the primary user interface. Test Plan: Test case added and ctest passes Reviewers: #kwin Subscribers: kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D17280 --- CMakeLists.txt | 1 + autotests/integration/touch_input_test.cpp | 25 +++++++++ input.cpp | 2 + touch_hide_cursor_spy.cpp | 65 ++++++++++++++++++++++ touch_hide_cursor_spy.h | 40 +++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 touch_hide_cursor_spy.cpp create mode 100644 touch_hide_cursor_spy.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 63b515746c..fd5246ed9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -483,6 +483,7 @@ set(kwin_KDEINIT_SRCS libinput/events.cpp libinput/libinput_logging.cpp udev.cpp + touch_hide_cursor_spy.cpp ) include(ECMQtDeclareLoggingCategory) diff --git a/autotests/integration/touch_input_test.cpp b/autotests/integration/touch_input_test.cpp index aee34c30e8..89aef841cd 100644 --- a/autotests/integration/touch_input_test.cpp +++ b/autotests/integration/touch_input_test.cpp @@ -45,6 +45,7 @@ private Q_SLOTS: void initTestCase(); void init(); void cleanup(); + void testTouchHidesCursor(); void testMultipleTouchPoints_data(); void testMultipleTouchPoints(); void testCancel(); @@ -128,6 +129,30 @@ AbstractClient *TouchInputTest::showWindow(bool decorated) return c; } +void TouchInputTest::testTouchHidesCursor() +{ + QCOMPARE(kwinApp()->platform()->isCursorHidden(), false); + quint32 timestamp = 1; + kwinApp()->platform()->touchDown(1, QPointF(125, 125), timestamp++); + QCOMPARE(kwinApp()->platform()->isCursorHidden(), true); + kwinApp()->platform()->touchDown(2, QPointF(130, 125), timestamp++); + kwinApp()->platform()->touchUp(2, timestamp++); + kwinApp()->platform()->touchUp(1, timestamp++); + + // now a mouse event should show the cursor again + kwinApp()->platform()->pointerMotion(QPointF(0, 0), timestamp++); + QCOMPARE(kwinApp()->platform()->isCursorHidden(), false); + + // touch should hide again + kwinApp()->platform()->touchDown(1, QPointF(125, 125), timestamp++); + kwinApp()->platform()->touchUp(1, timestamp++); + QCOMPARE(kwinApp()->platform()->isCursorHidden(), true); + + // wheel should also show + kwinApp()->platform()->pointerAxisVertical(1.0, timestamp++); + QCOMPARE(kwinApp()->platform()->isCursorHidden(), false); +} + void TouchInputTest::testMultipleTouchPoints_data() { QTest::addColumn("decorated"); diff --git a/input.cpp b/input.cpp index 6f5334380c..d35333f235 100644 --- a/input.cpp +++ b/input.cpp @@ -24,6 +24,7 @@ along with this program. If not, see . #include "keyboard_input.h" #include "pointer_input.h" #include "touch_input.h" +#include "touch_hide_cursor_spy.h" #include "client.h" #include "effects.h" #include "gestures.h" @@ -1754,6 +1755,7 @@ void InputRedirection::setupInputFilters() installInputEventFilter(new VirtualTerminalFilter); } if (waylandServer()) { + installInputEventSpy(new TouchHideCursorSpy); installInputEventFilter(new TerminateServerFilter); installInputEventFilter(new DragAndDropInputFilter); installInputEventFilter(new LockScreenFilter); diff --git a/touch_hide_cursor_spy.cpp b/touch_hide_cursor_spy.cpp new file mode 100644 index 0000000000..0cc13ab1c5 --- /dev/null +++ b/touch_hide_cursor_spy.cpp @@ -0,0 +1,65 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2018 Martin Flöser + +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 "touch_hide_cursor_spy.h" +#include "main.h" +#include "platform.h" + +namespace KWin +{ + +void TouchHideCursorSpy::pointerEvent(MouseEvent *event) +{ + Q_UNUSED(event) + showCursor(); +} + +void TouchHideCursorSpy::wheelEvent(KWin::WheelEvent *event) +{ + Q_UNUSED(event) + showCursor(); +} + +void TouchHideCursorSpy::touchDown(quint32 id, const QPointF &pos, quint32 time) +{ + Q_UNUSED(id) + Q_UNUSED(pos) + Q_UNUSED(time) + hideCursor(); +} + +void TouchHideCursorSpy::showCursor() +{ + if (!m_cursorHidden) { + return; + } + m_cursorHidden = false; + kwinApp()->platform()->showCursor(); +} + +void TouchHideCursorSpy::hideCursor() +{ + if (m_cursorHidden) { + return; + } + m_cursorHidden = true; + kwinApp()->platform()->hideCursor(); +} + +} diff --git a/touch_hide_cursor_spy.h b/touch_hide_cursor_spy.h new file mode 100644 index 0000000000..08029dd2bc --- /dev/null +++ b/touch_hide_cursor_spy.h @@ -0,0 +1,40 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2018 Martin Flöser + +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 . +*********************************************************************/ +#pragma once +#include "input_event_spy.h" + +namespace KWin +{ + +class TouchHideCursorSpy : public InputEventSpy +{ +public: + void pointerEvent(KWin::MouseEvent *event) override; + void wheelEvent(KWin::WheelEvent *event) override; + void touchDown(quint32 id, const QPointF &pos, quint32 time) override; + +private: + void showCursor(); + void hideCursor(); + + bool m_cursorHidden = false; +}; + +}