From 0eba3f3c18c205b4cb739b5aed31abfa7b176b43 Mon Sep 17 00:00:00 2001 From: Jesse Pullinen Date: Sun, 17 Sep 2017 14:44:10 +0300 Subject: [PATCH] Fix TouchEvent::id() returning -1, fixes a crash Summary: Libinput returns -1 when calling libinput_event_touch_get_slot on an event from a single-touch touchscreen. The returned value is used in DecorationEventFilter to determine the touch which is acting on it. The value -1 is used to signify that the decoration is not being acted on. Thus when releasing the touch, it checks whether it was being dragged, and as it thinks it isn't, it doesn't handle it and the decoration is still being dragged. Clicking on the decoration then crashes kwin. Test Plan: Move a window by dragging the decoration with a single-touch touchscreen. Reviewers: #kwin, graesslin Reviewed By: #kwin, graesslin Subscribers: graesslin, kwin, #kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D7854 --- autotests/libinput/touch_event_test.cpp | 15 +++++++++++++++ libinput/events.cpp | 5 ++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/autotests/libinput/touch_event_test.cpp b/autotests/libinput/touch_event_test.cpp index 52eefa37b3..5aa0a7c3cb 100644 --- a/autotests/libinput/touch_event_test.cpp +++ b/autotests/libinput/touch_event_test.cpp @@ -40,6 +40,7 @@ private Q_SLOTS: void testType(); void testAbsoluteMotion_data(); void testAbsoluteMotion(); + void testNoAssignedSlot(); private: libinput_device *m_nativeDevice = nullptr; @@ -126,5 +127,19 @@ void TestLibinputTouchEvent::testAbsoluteMotion() QCOMPARE(te->absolutePos(QSize(1280, 1024)), QPointF(640, 512)); } +void TestLibinputTouchEvent::testNoAssignedSlot() +{ + // this test verifies that touch events without an assigned slot get id == 0 + libinput_event_touch *touchEvent = new libinput_event_touch; + touchEvent->type = LIBINPUT_EVENT_TOUCH_UP; + touchEvent->device = m_nativeDevice; + // touch events without an assigned slot have slot == -1 + touchEvent->slot = -1; + + QScopedPointer event(Event::create(touchEvent)); + QVERIFY(dynamic_cast(event.data())); + QCOMPARE(dynamic_cast(event.data())->id(), 0); +} + QTEST_GUILESS_MAIN(TestLibinputTouchEvent) #include "touch_event_test.moc" diff --git a/libinput/events.cpp b/libinput/events.cpp index d0539c3d2e..183ab70c2b 100644 --- a/libinput/events.cpp +++ b/libinput/events.cpp @@ -225,7 +225,10 @@ QPointF TouchEvent::absolutePos(const QSize &size) const qint32 TouchEvent::id() const { Q_ASSERT(type() != LIBINPUT_EVENT_TOUCH_CANCEL && type() != LIBINPUT_EVENT_TOUCH_FRAME); - return libinput_event_touch_get_slot(m_touchEvent); + + const qint32 slot = libinput_event_touch_get_slot(m_touchEvent); + + return slot == -1 ? 0 : slot; } GestureEvent::GestureEvent(libinput_event *event, libinput_event_type type)