Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Extract KisInputEventsEater into a separate .cpp file

Dmitry Kazakova4f4408980c1
diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txt
index dc796aa485..80ea07cded 100644
--- a/libs/ui/CMakeLists.txt
+++ b/libs/ui/CMakeLists.txt
@@ -347,6 +347,7 @@ set(kritaui_LIB_SRCS
input/KisCanvasNavigationActionStrategy.cpp
input/KisCanvasNavigationActionStrategyNativeGesture.cpp
input/KisCanvasNavigationActionStrategyTouch.cpp
+ input/KisInputEventsEater.cpp
input/kis_input_manager.cpp
input/kis_input_manager_p.cpp
input/kis_extended_modifiers_mapper.cpp
diff --git a/libs/ui/input/KisInputEventsEater.cpp b/libs/ui/input/KisInputEventsEater.cpp
new file mode 100644
index 0000000000..0429d2f2f4
--- /dev/null
+++ b/libs/ui/input/KisInputEventsEater.cpp
@@ -0,0 +1,342 @@
+/*
+ * SPDX-FileCopyrightText: 2015 Michael Abrahams <[email protected]>
+ * SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "KisInputEventsEater.h"
+
+#include <kis_config.h>
+
+#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
+// for namespace Qt::StringLiterals
+#include <KisPortingUtils.h>
+#endif
+
+
+/**
+ * This hungry class EventEater encapsulates event masking logic.
+ *
+ * Its basic role is to kill synthetic mouseMove events sent by Xorg or Qt after
+ * tablet events. Those events are sent in order to allow widgets that haven't
+ * implemented tablet specific functionality to seamlessly behave as if one were
+ * using a mouse. These synthetic events are *supposed* to be optional, or at
+ * least come with a flag saying "This is a fake event!!" but neither of those
+ * methods is trustworthy. (This is correct as of Qt 5.4 + Xorg.)
+ *
+ * Qt 5.4 provides no reliable way to see if a user's tablet is being hovered
+ * over the pad, since it converts all tablethover events into mousemove, with
+ * no option to turn this off. Moreover, sometimes the MouseButtonPress event
+ * from the tapping their tablet happens BEFORE the TabletPress event. This
+ * means we have to resort to a somewhat complicated logic. What makes this
+ * truly a joke is that we are not guaranteed to observe TabletProximityEnter
+ * events when we're using a tablet, either, you may only see an Enter event.
+ *
+ * Once we see tablet events heading our way, we can say pretty confidently that
+ * every mouse event is fake. There are two painful cases to consider - a
+ * mousePress event could arrive before the tabletPress event, or it could
+ * arrive much later, e.g. after tabletRelease. The first was only seen on Linux
+ * with Qt's XInput2 code, the solution was to hold onto mousePress events
+ * temporarily and wait for tabletPress later, this is contained in git history
+ * but is now removed. The second case is currently handled by the
+ * eatOneMousePress function, which waits as long as necessary to detect and
+ * block a single mouse press event.
+ */
+
+static bool isMouseEventType(QEvent::Type t)
+{
+ return (t == QEvent::MouseMove ||
+ t == QEvent::MouseButtonPress ||
+ t == QEvent::MouseButtonRelease ||
+ t == QEvent::MouseButtonDblClick);
+}
+
+static bool isTabletEventType(QEvent::Type t)
+{
+ return (t == QEvent::TabletMove ||
+ t == QEvent::TabletPress ||
+ t == QEvent::TabletRelease);
+}
+
+static bool isTouchEventType(QEvent::Type t)
+{
+ return (t == QEvent::TouchBegin ||
+ t == QEvent::TouchEnd ||
+ t == QEvent::TouchUpdate ||
+ t == QEvent::TouchCancel);
+}
+
+static bool isNativeGestureEventType(QEvent::Type t)
+{
+ return t == QEvent::NativeGesture;
+}
+
+KisInputEventsEater::KisInputEventsEater()
+{
+ KisConfig cfg(true);
+ activateSecondaryButtonsWorkaround = cfg.useRightMiddleTabletButtonWorkaround();
+}
+
+void KisInputEventsEater::debugEaterStateTransition(const QLatin1String &stateName, bool newValue, QEvent::Type eventType, const QLatin1String &comment)
+{
+ if (comment.isEmpty()) {
+ dbgInputEater.noquote().nospace() << stateName << " <- " << newValue << " by " << eventType;
+ } else {
+ dbgInputEater.noquote().nospace() << stateName << " <- " << newValue << " by " << eventType << " (" << comment << ")";
+ }
+}
+bool KisInputEventsEater::eventFilter(QObject* target, QEvent* event )
+{
+ using namespace Qt::StringLiterals;
+
+ Q_UNUSED(target);
+
+ {
+ // handle `newTabletIsHovering` and `tabletIsPressed` state transitions
+
+ if (isTabletEventType(event->type())) {
+
+ QTabletEvent *ev = static_cast<QTabletEvent*>(event);
+ const bool newTabletIsHovering = ev->buttons() == Qt::NoButton;
+ const bool newTabletIsPressed = !newTabletIsHovering;
+
+ if (newTabletIsHovering != tabletIsHovering) {
+ tabletIsHovering = newTabletIsHovering;
+ debugEaterStateTransition("tabletIsHovering"_L1, newTabletIsHovering, event->type());
+ }
+
+ if (newTabletIsPressed != tabletIsPressed) {
+ tabletIsPressed = newTabletIsPressed;
+ debugEaterStateTransition("tabletIsPressed"_L1, newTabletIsPressed, event->type());
+ }
+ }
+
+ if (event->type() == QEvent::FocusOut ||
+ event->type() == QEvent::Leave ||
+ /**
+ * Tablet events have higher priority over the touch events, so, if a touch has been
+ * started **before** the tablet stroke, then TouchUpdate events will no cause
+ * the EventEater to perform state transitions to the tablet state (on Windows, the
+ * streams of touch and tablet event come in parallel independently).
+ *
+ * Please note that the actual cancellation of the already started touch
+ * action is performed inside KisShortcutMatcher, not in the event eater.
+ * Here we just allow all the TouchUpdate events pass firther and block synthesized
+ * mouse events only.
+ */
+ (event->type() == QEvent::TouchBegin && !tabletIsInProximity)) {
+
+ if (tabletIsHovering) {
+ tabletIsHovering = false;
+ debugEaterStateTransition("tabletIsHovering"_L1, false, event->type());
+ }
+ if (tabletIsPressed) {
+ tabletIsPressed = false;
+ debugEaterStateTransition("tabletIsPressed"_L1, false, event->type());
+ }
+ }
+
+ // also proximity-leave in a corresponding function...
+ }
+
+ {
+ // handle `touchIsActive` state transitions
+
+ bool assumeTouchStarted = event->type() == QEvent::TouchBegin;
+
+ if (!assumeTouchStarted && event->type() == QEvent::TouchUpdate) {
+ QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);
+
+#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
+ using DeviceType = QInputDevice::DeviceType;
+#else
+ using DeviceType = QTouchDevice::DeviceType;
+#endif
+
+ if (touchEvent->device()->type() == DeviceType::TouchPad) {
+#ifdef Q_OS_MACOS
+ /**
+ * On MacOS TouchUpdate events arrive for normal touchpad
+ * movements. These TouchUpdate events have lower resolution
+ * and they are not wrapped into TouchBegin/TouchEnd pair.
+ * So we just allow synthesized events for them.
+ *
+ * For all other systems TouchUpdate events arrive for
+ * touch screens only, and only inside a valid gesture, so
+ * we should block mouse events for them.
+ *
+ * Also see a comment about hover events in the handler
+ * of QEvent::TouchUpdate in KisInputManager::eventFilter.
+ */
+
+ // noop, keep assumeTouchStarted false
+#else
+ dbgInputEater << "Received a TouchUpdate for a touchpad device on a non-MacOS system!";
+#endif
+ } else {
+ assumeTouchStarted = true;
+ }
+ }
+
+ if (assumeTouchStarted) {
+ if (!touchIsActive) {
+ touchIsActive = true;
+ debugEaterStateTransition("touchIsActive"_L1, true, event->type());
+ }
+ }
+
+ if (event->type() == QEvent::TouchEnd ||
+ event->type() == QEvent::TouchCancel) {
+ if (touchIsActive) {
+ touchIsActive = false;
+ debugEaterStateTransition("touchIsActive"_L1, false, event->type());
+ }
+ }
+ }
+
+ if (eatOneMousePressEvent && event->type() == QEvent::MouseButtonPress
+ // Drop one mouse press following tabletPress or touchBegin
+ && (static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton)) {
+ eatOneMousePressEvent = false;
+ debugEvent<QMouseEvent>(event, BlockedByNextPressSuppression);
+ return true;
+ }
+
+ if (activateSecondaryButtonsWorkaround) {
+ if (event->type() == QEvent::TabletPress ||
+ event->type() == QEvent::TabletRelease) {
+
+ QTabletEvent *te = static_cast<QTabletEvent*>(event);
+ if (te->button() != Qt::LeftButton) {
+ debugEvent<QTabletEvent>(te, BlockedByButtonsWorkaround);
+ return true;
+ }
+ } else if (event->type() == QEvent::MouseButtonPress ||
+ event->type() == QEvent::MouseButtonRelease ||
+ event->type() == QEvent::MouseButtonDblClick) {
+
+ QMouseEvent *me = static_cast<QMouseEvent*>(event);
+ if (me->button() != Qt::LeftButton) {
+ return false;
+ }
+ }
+ }
+
+#ifdef Q_OS_MACOS
+ /**
+ * MacOS is the only platform where we can differentiate between
+ * wheel events generated by a mouse and by a touchpad. More than
+ * that, on MacOS touchpad generates NativeGesture events, hence
+ * we should just blocked all the synthetic wheel events.
+ */
+
+ if (event->type() == QEvent::Wheel) {
+ QWheelEvent *we = static_cast<QWheelEvent *>(event);
+ if (we->source() == Qt::MouseEventSource::MouseEventSynthesizedBySystem) {
+ debugEvent<QWheelEvent>(event, BlockedBySynthetic);
+ return true;
+ }
+ }
+#endif
+
+ const bool shouldEatMouseEvents =
+ // On MacOS Qt sends fake mouse events instead of hover events,
+ // so we should not block mouse events when the tablet just hovers
+ // the canvas. The real TabletMove events will arrive only when
+ // the user touches the surface of the tablet, i.e. after a TabletPress.
+#ifndef Q_OS_MACOS
+ tabletIsInProximity ||
+ tabletIsHovering ||
+#endif
+ tabletIsPressed ||
+ touchIsActive;
+
+ if (isMouseEventType(event->type()) &&
+ (shouldEatMouseEvents
+ // On Mac, we need mouse events when the tablet is in proximity, but not pressed down
+ // since tablet move events are not generated until after tablet press.
+ #ifndef Q_OS_MAC
+ || (static_cast<QMouseEvent*>(event)->source() != Qt::MouseEventNotSynthesized)
+ #endif
+ )) {
+
+ EventBlockingReasons reasons = NotBlocked;
+#ifndef Q_OS_MACOS
+ reasons.setFlag(BlockedByTabletProximity, tabletIsInProximity);
+ reasons.setFlag(BlockedByTabletHover, tabletIsHovering);
+ reasons.setFlag(BlockedBySynthetic, static_cast<QMouseEvent*>(event)->source() != Qt::MouseEventNotSynthesized);
+#endif
+ reasons.setFlag(BlockedByTabletPress, tabletIsPressed);
+ reasons.setFlag(BlockedByTouchPress, touchIsActive);
+
+ debugEvent<QMouseEvent>(event, reasons);
+
+ // Drop mouse events if enabled or event was synthetic & synthetic events are disabled
+ return true;
+ }
+
+ if (event->type() == QEvent::TouchBegin &&
+ (tabletIsInProximity || tabletIsHovering || tabletIsPressed)) {
+
+ EventBlockingReasons reasons = NotBlocked;
+ reasons.setFlag(BlockedByTabletProximity, tabletIsInProximity);
+ reasons.setFlag(BlockedByTabletHover, tabletIsHovering);
+ reasons.setFlag(BlockedByTabletPress, tabletIsPressed);
+
+ debugEvent<QTouchEvent>(event, reasons);
+
+ if (touchIsActive) {
+ // don't forget to reset the touch active state, since we are
+ // not going to receive the res of the events
+ touchIsActive = false;
+ debugEaterStateTransition("touchIsActive"_L1, false, event->type(), "cancelled by tablet"_L1);
+ }
+
+ // Drop touch-begin event. It will prevent KisShortcutMatcher to
+ // start any touch action, while letting it complete any existing
+ // actions. We explicitly accept the event to make sure OS will
+ // not trigger any global gestures (works on WinInk only).
+ event->accept();
+ return true;
+ }
+
+ if (isMouseEventType(event->type())) {
+ debugEvent<QMouseEvent>(event, NotBlocked);
+ } else if (isTabletEventType(event->type())) {
+ debugEvent<QTabletEvent>(event, NotBlocked);
+ } else if (isTouchEventType(event->type())) {
+ debugEvent<QTouchEvent>(event, NotBlocked);
+ } else if (isNativeGestureEventType(event->type())) {
+ debugEvent<QNativeGestureEvent>(event, NotBlocked);
+ }
+
+ return false; // All clear - let this one through!
+}
+
+void KisInputEventsEater::notifyTabletEnterProximity()
+{
+ using namespace Qt::StringLiterals;
+
+ tabletIsInProximity = true;
+ debugEaterStateTransition("tabletIsInProximity"_L1, true, QEvent::TabletEnterProximity);
+}
+
+void KisInputEventsEater::notifyTabletLeaveProximity()
+{
+ using namespace Qt::StringLiterals;
+
+ tabletIsInProximity = false;
+ debugEaterStateTransition("tabletIsInProximity"_L1, false, QEvent::TabletLeaveProximity);
+
+ if (tabletIsHovering) {
+ tabletIsHovering = false;
+ debugEaterStateTransition("tabletIsHovering"_L1, false, QEvent::TabletLeaveProximity);
+ }
+}
+
+void KisInputEventsEater::eatOneMousePress()
+{
+ // Enable on other platforms if getting full-pressure splotches
+ eatOneMousePressEvent = true;
+}
diff --git a/libs/ui/input/KisInputEventsEater.h b/libs/ui/input/KisInputEventsEater.h
new file mode 100644
index 0000000000..1bf931f3e5
--- /dev/null
+++ b/libs/ui/input/KisInputEventsEater.h
@@ -0,0 +1,93 @@
+/*
+ * SPDX-FileCopyrightText: 2015 Michael Abrahams <[email protected]>
+ * SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <kritaui_export.h>
+
+#include <kis_debug.h>
+#include "input/kis_tablet_debugger.h"
+
+class KisToolInvocationAction;
+
+enum EventBlockingReason {
+ NotBlocked = 0x0,
+ BlockedByTabletProximity = 0x01,
+ BlockedByTabletHover = 0x02,
+ BlockedByTabletPress = 0x04,
+ BlockedByTouchPress = 0x08,
+ BlockedByNextPressSuppression = 0x10,
+ BlockedByButtonsWorkaround = 0x20,
+ BlockedBySynthetic = 0x40
+};
+Q_DECLARE_FLAGS(EventBlockingReasons, EventBlockingReason)
+Q_DECLARE_OPERATORS_FOR_FLAGS(EventBlockingReasons)
+
+class KRITAUI_EXPORT KisInputEventsEater
+{
+public:
+ KisInputEventsEater();
+
+ bool eventFilter(QObject *target, QEvent *event);
+
+ void notifyTabletEnterProximity();
+ void notifyTabletLeaveProximity();
+
+ // On Windows, we sometimes receive mouse events very late, so watch & wait.
+ void eatOneMousePress();
+
+ template<class Event>
+ static void debugEvent(QEvent *event, EventBlockingReasons reasons = NotBlocked)
+ {
+ if (!KisTabletDebugger::instance()->debugEnabled())
+ return;
+
+ QString reasonsString;
+
+ if (reasons.testFlag(BlockedByTabletProximity)) {
+ reasonsString += "Prx";
+ }
+
+ if (reasons.testFlag(BlockedByTabletHover)) {
+ reasonsString += "Hov";
+ }
+
+ if (reasons.testFlag(BlockedByTabletPress)) {
+ reasonsString += "Prs";
+ }
+
+ if (reasons.testFlag(BlockedByNextPressSuppression)) {
+ reasonsString += "Nxt";
+ }
+
+ if (reasons.testFlag(BlockedByTouchPress)) {
+ reasonsString += "Tch";
+ }
+
+ if (reasons.testFlag(BlockedBySynthetic)) {
+ reasonsString += "Syn";
+ }
+
+ QString msg1 = QString("[%1] ").arg(reasonsString, 15);
+ Event *specificEvent = static_cast<Event *>(event);
+ dbgTablet << KisTabletDebugger::instance()->eventToString(*specificEvent, msg1);
+ }
+
+private:
+
+ bool eatOneMousePressEvent{false}; // Eat a single mouse press event
+ bool activateSecondaryButtonsWorkaround{false}; // Use mouse events for right- and middle-clicks
+
+ bool tabletIsInProximity{false};
+ bool tabletIsHovering{false};
+ bool tabletIsPressed{false};
+ bool touchIsActive{false};
+
+private:
+ void debugEaterStateTransition(const QLatin1String &stateName,
+ bool newValue,
+ QEvent::Type eventType,
+ const QLatin1String &comment = QLatin1String());
+};
diff --git a/libs/ui/input/kis_input_manager.cpp b/libs/ui/input/kis_input_manager.cpp
index 24719c2916..567649cad1 100644
--- a/libs/ui/input/kis_input_manager.cpp
+++ b/libs/ui/input/kis_input_manager.cpp
@@ -394,7 +394,7 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
break;
}
case QEvent::ShortcutOverride: {
- d->debugEvent<QKeyEvent>(event);
+ KisInputEventsEater::debugEvent<QKeyEvent>(event);
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
Qt::Key key = KisExtendedModifiersMapper::workaroundShiftAltMetaHell(keyEvent);
@@ -439,7 +439,7 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
break;
}
case QEvent::KeyRelease: {
- d->debugEvent<QKeyEvent>(event);
+ KisInputEventsEater::debugEvent<QKeyEvent>(event);
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (!keyEvent->isAutoRepeat()) {
@@ -489,7 +489,7 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
break;
}
case QEvent::Wheel: {
- d->debugEvent<QWheelEvent>(event);
+ KisInputEventsEater::debugEvent<QWheelEvent>(event);
QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
d->accumulatedScrollDelta += wheelEvent->angleDelta().y();
@@ -539,18 +539,18 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
}
#ifndef Q_OS_ANDROID
case QEvent::Enter:
- d->debugEvent<QEvent>(event);
+ KisInputEventsEater::debugEvent<QEvent>(event);
//Make sure the input actions know we are active.
KisAbstractInputAction::setInputManager(this);
d->matcher.enterEvent();
break;
case QEvent::Leave:
- d->debugEvent<QEvent>(event);
+ KisInputEventsEater::debugEvent<QEvent>(event);
d->matcher.leaveEvent();
break;
#endif
case QEvent::FocusIn:
- d->debugEvent<QEvent>(event);
+ KisInputEventsEater::debugEvent<QEvent>(event);
KisAbstractInputAction::setInputManager(this);
d->fixShortcutMatcherModifiersState();
@@ -558,7 +558,7 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
break;
case QEvent::FocusOut: {
- d->debugEvent<QEvent>(event);
+ KisInputEventsEater::debugEvent<QEvent>(event);
KisAbstractInputAction::setInputManager(this);
QPointF currentLocalPos =
diff --git a/libs/ui/input/kis_input_manager_p.cpp b/libs/ui/input/kis_input_manager_p.cpp
index bb45d5bc69..cb77f9c34a 100644
--- a/libs/ui/input/kis_input_manager_p.cpp
+++ b/libs/ui/input/kis_input_manager_p.cpp
@@ -24,344 +24,10 @@
#include "kis_input_profile_manager.h"
#include "kis_extended_modifiers_mapper.h"
-#include "kis_zoom_and_rotate_action.h"
-#include "kis_popup_palette.h"
#include "config-qt-patches-present.h"
#include <memory>
-#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
-// for namespace Qt::StringLiterals
-#include <KisPortingUtils.h>
-#endif
-
-
-/**
- * This hungry class EventEater encapsulates event masking logic.
- *
- * Its basic role is to kill synthetic mouseMove events sent by Xorg or Qt after
- * tablet events. Those events are sent in order to allow widgets that haven't
- * implemented tablet specific functionality to seamlessly behave as if one were
- * using a mouse. These synthetic events are *supposed* to be optional, or at
- * least come with a flag saying "This is a fake event!!" but neither of those
- * methods is trustworthy. (This is correct as of Qt 5.4 + Xorg.)
- *
- * Qt 5.4 provides no reliable way to see if a user's tablet is being hovered
- * over the pad, since it converts all tablethover events into mousemove, with
- * no option to turn this off. Moreover, sometimes the MouseButtonPress event
- * from the tapping their tablet happens BEFORE the TabletPress event. This
- * means we have to resort to a somewhat complicated logic. What makes this
- * truly a joke is that we are not guaranteed to observe TabletProximityEnter
- * events when we're using a tablet, either, you may only see an Enter event.
- *
- * Once we see tablet events heading our way, we can say pretty confidently that
- * every mouse event is fake. There are two painful cases to consider - a
- * mousePress event could arrive before the tabletPress event, or it could
- * arrive much later, e.g. after tabletRelease. The first was only seen on Linux
- * with Qt's XInput2 code, the solution was to hold onto mousePress events
- * temporarily and wait for tabletPress later, this is contained in git history
- * but is now removed. The second case is currently handled by the
- * eatOneMousePress function, which waits as long as necessary to detect and
- * block a single mouse press event.
- */
-
-static bool isMouseEventType(QEvent::Type t)
-{
- return (t == QEvent::MouseMove ||
- t == QEvent::MouseButtonPress ||
- t == QEvent::MouseButtonRelease ||
- t == QEvent::MouseButtonDblClick);
-}
-
-static bool isTabletEventType(QEvent::Type t)
-{
- return (t == QEvent::TabletMove ||
- t == QEvent::TabletPress ||
- t == QEvent::TabletRelease);
-}
-
-static bool isTouchEventType(QEvent::Type t)
-{
- return (t == QEvent::TouchBegin ||
- t == QEvent::TouchEnd ||
- t == QEvent::TouchUpdate ||
- t == QEvent::TouchCancel);
-}
-
-static bool isNativeGestureEventType(QEvent::Type t)
-{
- return t == QEvent::NativeGesture;
-}
-
-KisInputManager::Private::EventEater::EventEater()
-{
- KisConfig cfg(true);
- activateSecondaryButtonsWorkaround = cfg.useRightMiddleTabletButtonWorkaround();
-}
-
-void KisInputManager::Private::EventEater::debugEaterStateTransition(const QLatin1String &stateName, bool newValue, QEvent::Type eventType, const QLatin1String &comment)
-{
- if (comment.isEmpty()) {
- dbgInputEater.noquote().nospace() << stateName << " <- " << newValue << " by " << eventType;
- } else {
- dbgInputEater.noquote().nospace() << stateName << " <- " << newValue << " by " << eventType << " (" << comment << ")";
- }
-}
-bool KisInputManager::Private::EventEater::eventFilter(QObject* target, QEvent* event )
-{
- using namespace Qt::StringLiterals;
-
- Q_UNUSED(target);
-
- {
- // handle `newTabletIsHovering` and `tabletIsPressed` state transitions
-
- if (isTabletEventType(event->type())) {
-
- QTabletEvent *ev = static_cast<QTabletEvent*>(event);
- const bool newTabletIsHovering = ev->buttons() == Qt::NoButton;
- const bool newTabletIsPressed = !newTabletIsHovering;
-
- if (newTabletIsHovering != tabletIsHovering) {
- tabletIsHovering = newTabletIsHovering;
- debugEaterStateTransition("tabletIsHovering"_L1, newTabletIsHovering, event->type());
- }
-
- if (newTabletIsPressed != tabletIsPressed) {
- tabletIsPressed = newTabletIsPressed;
- debugEaterStateTransition("tabletIsPressed"_L1, newTabletIsPressed, event->type());
- }
- }
-
- if (event->type() == QEvent::FocusOut ||
- event->type() == QEvent::Leave ||
- /**
- * Tablet events have higher priority over the touch events, so, if a touch has been
- * started **before** the tablet stroke, then TouchUpdate events will no cause
- * the EventEater to perform state transitions to the tablet state (on Windows, the
- * streams of touch and tablet event come in parallel independently).
- *
- * Please note that the actual cancellation of the already started touch
- * action is performed inside KisShortcutMatcher, not in the event eater.
- * Here we just allow all the TouchUpdate events pass firther and block synthesized
- * mouse events only.
- */
- (event->type() == QEvent::TouchBegin && !tabletIsInProximity)) {
-
- if (tabletIsHovering) {
- tabletIsHovering = false;
- debugEaterStateTransition("tabletIsHovering"_L1, false, event->type());
- }
- if (tabletIsPressed) {
- tabletIsPressed = false;
- debugEaterStateTransition("tabletIsPressed"_L1, false, event->type());
- }
- }
-
- // also proximity-leave in a corresponding function...
- }
-
- {
- // handle `touchIsActive` state transitions
-
- bool assumeTouchStarted = event->type() == QEvent::TouchBegin;
-
- if (!assumeTouchStarted && event->type() == QEvent::TouchUpdate) {
- QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);
-
-#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
- using DeviceType = QInputDevice::DeviceType;
-#else
- using DeviceType = QTouchDevice::DeviceType;
-#endif
-
- if (touchEvent->device()->type() == DeviceType::TouchPad) {
-#ifdef Q_OS_MACOS
- /**
- * On MacOS TouchUpdate events arrive for normal touchpad
- * movements. These TouchUpdate events have lower resolution
- * and they are not wrapped into TouchBegin/TouchEnd pair.
- * So we just allow synthesized events for them.
- *
- * For all other systems TouchUpdate events arrive for
- * touch screens only, and only inside a valid gesture, so
- * we should block mouse events for them.
- *
- * Also see a comment about hover events in the handler
- * of QEvent::TouchUpdate in KisInputManager::eventFilter.
- */
-
- // noop, keep assumeTouchStarted false
-#else
- dbgInputEater << "Received a TouchUpdate for a touchpad device on a non-MacOS system!";
-#endif
- } else {
- assumeTouchStarted = true;
- }
- }
-
- if (assumeTouchStarted) {
- if (!touchIsActive) {
- touchIsActive = true;
- debugEaterStateTransition("touchIsActive"_L1, true, event->type());
- }
- }
-
- if (event->type() == QEvent::TouchEnd ||
- event->type() == QEvent::TouchCancel) {
- if (touchIsActive) {
- touchIsActive = false;
- debugEaterStateTransition("touchIsActive"_L1, false, event->type());
- }
- }
- }
-
- if (eatOneMousePressEvent && event->type() == QEvent::MouseButtonPress
- // Drop one mouse press following tabletPress or touchBegin
- && (static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton)) {
- eatOneMousePressEvent = false;
- KisInputManager::Private::debugEvent<QMouseEvent>(event, BlockedByNextPressSuppression);
- return true;
- }
-
- if (activateSecondaryButtonsWorkaround) {
- if (event->type() == QEvent::TabletPress ||
- event->type() == QEvent::TabletRelease) {
-
- QTabletEvent *te = static_cast<QTabletEvent*>(event);
- if (te->button() != Qt::LeftButton) {
- KisInputManager::Private::debugEvent<QTabletEvent>(te, BlockedByButtonsWorkaround);
- return true;
- }
- } else if (event->type() == QEvent::MouseButtonPress ||
- event->type() == QEvent::MouseButtonRelease ||
- event->type() == QEvent::MouseButtonDblClick) {
-
- QMouseEvent *me = static_cast<QMouseEvent*>(event);
- if (me->button() != Qt::LeftButton) {
- return false;
- }
- }
- }
-
-#ifdef Q_OS_MACOS
- /**
- * MacOS is the only platform where we can differentiate between
- * wheel events generated by a mouse and by a touchpad. More than
- * that, on MacOS touchpad generates NativeGesture events, hence
- * we should just blocked all the synthetic wheel events.
- */
-
- if (event->type() == QEvent::Wheel) {
- QWheelEvent *we = static_cast<QWheelEvent *>(event);
- if (we->source() == Qt::MouseEventSource::MouseEventSynthesizedBySystem) {
- KisInputManager::Private::debugEvent<QWheelEvent>(event, BlockedBySynthetic);
- return true;
- }
- }
-#endif
-
- const bool shouldEatMouseEvents =
- // On MacOS Qt sends fake mouse events instead of hover events,
- // so we should not block mouse events when the tablet just hovers
- // the canvas. The real TabletMove events will arrive only when
- // the user touches the surface of the tablet, i.e. after a TabletPress.
-#ifndef Q_OS_MACOS
- tabletIsInProximity ||
- tabletIsHovering ||
-#endif
- tabletIsPressed ||
- touchIsActive;
-
- if (isMouseEventType(event->type()) &&
- (shouldEatMouseEvents
- // On Mac, we need mouse events when the tablet is in proximity, but not pressed down
- // since tablet move events are not generated until after tablet press.
- #ifndef Q_OS_MAC
- || (static_cast<QMouseEvent*>(event)->source() != Qt::MouseEventNotSynthesized)
- #endif
- )) {
-
- EventBlockingReasons reasons = NotBlocked;
-#ifndef Q_OS_MACOS
- reasons.setFlag(BlockedByTabletProximity, tabletIsInProximity);
- reasons.setFlag(BlockedByTabletHover, tabletIsHovering);
- reasons.setFlag(BlockedBySynthetic, static_cast<QMouseEvent*>(event)->source() != Qt::MouseEventNotSynthesized);
-#endif
- reasons.setFlag(BlockedByTabletPress, tabletIsPressed);
- reasons.setFlag(BlockedByTouchPress, touchIsActive);
-
- KisInputManager::Private::debugEvent<QMouseEvent>(event, reasons);
-
- // Drop mouse events if enabled or event was synthetic & synthetic events are disabled
- return true;
- }
-
- if (event->type() == QEvent::TouchBegin &&
- (tabletIsInProximity || tabletIsHovering || tabletIsPressed)) {
-
- EventBlockingReasons reasons = NotBlocked;
- reasons.setFlag(BlockedByTabletProximity, tabletIsInProximity);
- reasons.setFlag(BlockedByTabletHover, tabletIsHovering);
- reasons.setFlag(BlockedByTabletPress, tabletIsPressed);
-
- KisInputManager::Private::debugEvent<QTouchEvent>(event, reasons);
-
- if (touchIsActive) {
- // don't forget to reset the touch active state, since we are
- // not going to receive the res of the events
- touchIsActive = false;
- debugEaterStateTransition("touchIsActive"_L1, false, event->type(), "cancelled by tablet"_L1);
- }
-
- // Drop touch-begin event. It will prevent KisShortcutMatcher to
- // start any touch action, while letting it complete any existing
- // actions. We explicitly accept the event to make sure OS will
- // not trigger any global gestures (works on WinInk only).
- event->accept();
- return true;
- }
-
- if (isMouseEventType(event->type())) {
- KisInputManager::Private::debugEvent<QMouseEvent>(event, NotBlocked);
- } else if (isTabletEventType(event->type())) {
- KisInputManager::Private::debugEvent<QTabletEvent>(event, NotBlocked);
- } else if (isTouchEventType(event->type())) {
- KisInputManager::Private::debugEvent<QTouchEvent>(event, NotBlocked);
- } else if (isNativeGestureEventType(event->type())) {
- KisInputManager::Private::debugEvent<QNativeGestureEvent>(event, NotBlocked);
- }
-
- return false; // All clear - let this one through!
-}
-
-void KisInputManager::Private::EventEater::notifyTabletEnterProximity()
-{
- using namespace Qt::StringLiterals;
-
- tabletIsInProximity = true;
- debugEaterStateTransition("tabletIsInProximity"_L1, true, QEvent::TabletEnterProximity);
-}
-
-void KisInputManager::Private::EventEater::notifyTabletLeaveProximity()
-{
- using namespace Qt::StringLiterals;
-
- tabletIsInProximity = false;
- debugEaterStateTransition("tabletIsInProximity"_L1, false, QEvent::TabletLeaveProximity);
-
- if (tabletIsHovering) {
- tabletIsHovering = false;
- debugEaterStateTransition("tabletIsHovering"_L1, false, QEvent::TabletLeaveProximity);
- }
-}
-
-void KisInputManager::Private::EventEater::eatOneMousePress()
-{
- // Enable on other platforms if getting full-pressure splotches
- eatOneMousePressEvent = true;
-}
-
void KisInputManager::Private::setMaskSyntheticEvents(bool value)
{
Q_UNUSED(value)
@@ -599,11 +265,11 @@ bool KisInputManager::Private::ProximityNotifier::eventFilter(QObject* object, Q
{
switch (event->type()) {
case QEvent::TabletEnterProximity:
- d->debugEvent<QEvent>(event);
+ KisInputEventsEater::debugEvent<QEvent>(event);
d->eventEater.notifyTabletEnterProximity();
break;
case QEvent::TabletLeaveProximity:
- d->debugEvent<QEvent>(event);
+ KisInputEventsEater::debugEvent<QEvent>(event);
d->eventEater.notifyTabletLeaveProximity();
break;
#ifdef Q_OS_WIN
diff --git a/libs/ui/input/kis_input_manager_p.h b/libs/ui/input/kis_input_manager_p.h
index 998aa3e67a..73ee65406e 100644
--- a/libs/ui/input/kis_input_manager_p.h
+++ b/libs/ui/input/kis_input_manager_p.h
@@ -8,36 +8,21 @@
#include <QList>
#include <QPointer>
#include <QEvent>
-#include <QTouchEvent>
#include <QScopedPointer>
-#include <QQueue>
+#include "KisInputEventsEater.h"
#include "kis_input_manager.h"
#include "kis_shortcut_matcher.h"
#include "kis_shortcut_configuration.h"
#include "kis_canvas2.h"
#include "kis_tool_proxy.h"
#include "kis_signal_compressor.h"
-#include "input/kis_tablet_debugger.h"
#include "kis_timed_signal_threshold.h"
#include "kis_signal_auto_connection.h"
#include "kis_latency_tracker.h"
class KisToolInvocationAction;
-enum EventBlockingReason {
- NotBlocked = 0x0,
- BlockedByTabletProximity = 0x01,
- BlockedByTabletHover = 0x02,
- BlockedByTabletPress = 0x04,
- BlockedByTouchPress = 0x08,
- BlockedByNextPressSuppression = 0x10,
- BlockedByButtonsWorkaround = 0x20,
- BlockedBySynthetic = 0x40
-};
-Q_DECLARE_FLAGS(EventBlockingReasons, EventBlockingReason)
-Q_DECLARE_OPERATORS_FOR_FLAGS(EventBlockingReasons)
-
class KisInputManager::Private
{
public:
@@ -85,42 +70,6 @@ public:
void setMaskSyntheticEvents(bool value);
void resetCompressor();
- template <class Event>
- static void debugEvent(QEvent *event, EventBlockingReasons reasons = NotBlocked)
- {
- if (!KisTabletDebugger::instance()->debugEnabled()) return;
-
- QString reasonsString;
-
- if (reasons.testFlag(BlockedByTabletProximity)) {
- reasonsString += "Prx";
- }
-
- if (reasons.testFlag(BlockedByTabletHover)) {
- reasonsString += "Hov";
- }
-
- if (reasons.testFlag(BlockedByTabletPress)) {
- reasonsString += "Prs";
- }
-
- if (reasons.testFlag(BlockedByNextPressSuppression)) {
- reasonsString += "Nxt";
- }
-
- if (reasons.testFlag(BlockedByTouchPress)) {
- reasonsString += "Tch";
- }
-
- if (reasons.testFlag(BlockedBySynthetic)) {
- reasonsString += "Syn";
- }
-
- QString msg1 = QString("[%1] ").arg(reasonsString, 15);
- Event *specificEvent = static_cast<Event*>(event);
- dbgTablet << KisTabletDebugger::instance()->eventToString(*specificEvent, msg1);
- }
-
class ProximityNotifier : public QObject
{
public:
@@ -150,29 +99,7 @@ public:
};
CanvasSwitcher canvasSwitcher;
- struct EventEater
- {
- EventEater();
-
- bool eventFilter(QObject* target, QEvent* event);
-
- void notifyTabletEnterProximity();
- void notifyTabletLeaveProximity();
-
- // On Windows, we sometimes receive mouse events very late, so watch & wait.
- void eatOneMousePress();
-
- bool eatOneMousePressEvent{false}; // Eat a single mouse press event
- bool activateSecondaryButtonsWorkaround{false}; // Use mouse events for right- and middle-clicks
-
- bool tabletIsInProximity {false};
- bool tabletIsHovering {false};
- bool tabletIsPressed {false};
- bool touchIsActive {false};
-
- void debugEaterStateTransition(const QLatin1String &stateName, bool newValue, QEvent::Type eventType, const QLatin1String &comment = QLatin1String());
- };
- EventEater eventEater;
+ KisInputEventsEater eventEater;
int accumulatedScrollDelta = 0;