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++
Move synthesized mouse events handling into EventEater
Dmitry Kazakov7c00ef0d6c10
diff --git a/libs/global/kis_debug.cpp b/libs/global/kis_debug.cppindex 684042131e..e431fcca38 100644--- a/libs/global/kis_debug.cpp+++ b/libs/global/kis_debug.cpp@@ -91,6 +91,7 @@ Q_LOGGING_CATEGORY(_41015, "krita.opengl", QtInfoMsg)Q_LOGGING_CATEGORY(_41016, "krita.metadata", QtInfoMsg)Q_LOGGING_CATEGORY(_41017, "krita.android", QtDebugMsg)Q_LOGGING_CATEGORY(_41018, "krita.locale", QtInfoMsg)+Q_LOGGING_CATEGORY(_41019, "krita.input.eater", QtInfoMsg)Q_LOGGING_CATEGORY(_41021, "krita.performance", QtInfoMsg)QString __methodName(const char *_prettyFunction)diff --git a/libs/global/kis_debug.h b/libs/global/kis_debug.hindex 29060f61b0..211663b2f2 100644--- a/libs/global/kis_debug.h+++ b/libs/global/kis_debug.h@@ -39,6 +39,7 @@ extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41015();extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41016();extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41017();extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41018();+extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41019();extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41021();#define dbgPerformanceIsEnabled() (_41021().isDebugEnabled())@@ -64,6 +65,7 @@ extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41021();#define dbgMetaData qCDebug(_41016)#define dbgAndroid qCDebug(_41017)#define dbgLocale qCDebug(_41018)+#define dbgInputEater qCDebug(_41019)#define dbgPerformance qCDebug(_41021)#define infoResources qCInfo(_30009)@@ -81,10 +83,11 @@ extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41021();#define infoRender qCInfo(_41010)#define infoScript qCInfo(_41011)#define infoInput qCInfo(_41012)-#define infoAction qCDebug(_41013)-#define infoTablet qCDebug(_41014)-#define infoOpenGL qCDebug(_41015)-#define infoMetaData qCDebug(_41016)+#define infoAction qCInfo(_41013)+#define infoTablet qCInfo(_41014)+#define infoOpenGL qCInfo(_41015)+#define infoMetaData qCInfo(_41016)+#define infoInputEater qCInfo(_41019)#define warnResources qCWarning(_30009)#define warnDbMigration qCWarning(_30010)@@ -101,10 +104,11 @@ extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41021();#define warnRender qCWarning(_41010)#define warnScript qCWarning(_41011)#define warnInput qCWarning(_41012)-#define warnAction qCDebug(_41013)-#define warnTablet qCDebug(_41014)-#define warnOpenGL qCDebug(_41015)-#define warnMetaData qCDebug(_41016)+#define warnAction qCWarning(_41013)+#define warnTablet qCWarning(_41014)+#define warnOpenGL qCWarning(_41015)+#define warnMetaData qCWarning(_41016)+#define warnInputEater qCWarning(_41019)#define errResources qCCritical(_30009)#define errDbMigration qCCritical(_30010)@@ -121,10 +125,11 @@ extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41021();#define errRender qCCritical(_41010)#define errScript qCCritical(_41011)#define errInput qCCritical(_41012)-#define errAction qCDebug(_41013)-#define errTablet qCDebug(_41014)-#define errOpenGL qCDebug(_41015)-#define errMetaData qCDebug(_41016)+#define errAction qCCritical(_41013)+#define errTablet qCCritical(_41014)+#define errOpenGL qCCritical(_41015)+#define errMetaData qCCritical(_41016)+#define errInputEater qCCritical(_41019)// Qt does not yet define qCFatal (TODO: this is an oversight, submit a patch upstream)/*diff --git a/libs/ui/input/kis_input_manager.cpp b/libs/ui/input/kis_input_manager.cppindex 30229410a6..1cb1c7ea03 100644--- a/libs/ui/input/kis_input_manager.cpp+++ b/libs/ui/input/kis_input_manager.cpp@@ -237,8 +237,7 @@ bool KisInputManager::compressMoveEventCommon(Event *event)* has a correct type.*/static_assert(std::is_same<Event, QMouseEvent>::value ||- std::is_same<Event, QTabletEvent>::value ||- std::is_same<Event, QTouchEvent>::value,+ std::is_same<Event, QTabletEvent>::value,"event should be a mouse or a tablet event");@@ -248,8 +247,7 @@ bool KisInputManager::compressMoveEventCommon(Event *event)* Compress the events if the tool doesn't need high resolution input*/if ((event->type() == QEvent::MouseMove ||- event->type() == QEvent::TabletMove ||- event->type() == QEvent::TouchUpdate) &&+ event->type() == QEvent::TabletMove) &&(!d->matcher.supportsHiResInputEvents() ||d->testingCompressBrushEvents)) {#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))@@ -375,9 +373,6 @@ bool KisInputManager::eventFilterImpl(QEvent * event)switch (event->type()) {case QEvent::MouseButtonPress:case QEvent::MouseButtonDblClick: {- d->debugEvent<QMouseEvent, true>(event);- if (d->touchHasBlockedPressEvents) break;-QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);if (d->popupWidget) {@@ -393,16 +388,13 @@ bool KisInputManager::eventFilterImpl(QEvent * event)break;}case QEvent::MouseButtonRelease: {- d->debugEvent<QMouseEvent, true>(event);- if (d->touchHasBlockedPressEvents) break;-QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);retval = d->matcher.buttonReleased(mouseEvent->button(), mouseEvent);event->setAccepted(retval);break;}case QEvent::ShortcutOverride: {- d->debugEvent<QKeyEvent, false>(event);+ d->debugEvent<QKeyEvent>(event);QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);Qt::Key key = KisExtendedModifiersMapper::workaroundShiftAltMetaHell(keyEvent);@@ -447,7 +439,7 @@ bool KisInputManager::eventFilterImpl(QEvent * event)break;}case QEvent::KeyRelease: {- d->debugEvent<QKeyEvent, false>(event);+ d->debugEvent<QKeyEvent>(event);QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);if (!keyEvent->isAutoRepeat()) {@@ -491,15 +483,13 @@ bool KisInputManager::eventFilterImpl(QEvent * event)break;}case QEvent::MouseMove: {- d->debugEvent<QMouseEvent, true>(event);-QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);retval = compressMoveEventCommon(mouseEvent);break;}case QEvent::Wheel: {- d->debugEvent<QWheelEvent, false>(event);+ d->debugEvent<QWheelEvent>(event);QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);#ifdef Q_OS_MACOS@@ -563,43 +553,26 @@ bool KisInputManager::eventFilterImpl(QEvent * event)}#ifndef Q_OS_ANDROIDcase QEvent::Enter:- d->debugEvent<QEvent, false>(event);+ d->debugEvent<QEvent>(event);//Make sure the input actions know we are active.KisAbstractInputAction::setInputManager(this);- if (!d->containsPointer) {- d->containsPointer = true;-- d->allowMouseEvents();- d->touchHasBlockedPressEvents = false;- }d->matcher.enterEvent();break;case QEvent::Leave:- d->debugEvent<QEvent, false>(event);- d->containsPointer = false;- /**- * We won't get a TabletProximityLeave event when the tablet- * is hovering above some other widget, so restore cursor- * events processing right now.- */- d->allowMouseEvents();- d->touchHasBlockedPressEvents = false;-+ d->debugEvent<QEvent>(event);d->matcher.leaveEvent();break;#endifcase QEvent::FocusIn:- d->debugEvent<QEvent, false>(event);+ d->debugEvent<QEvent>(event);KisAbstractInputAction::setInputManager(this);d->fixShortcutMatcherModifiersState();d->matcher.reinitializeButtons();-- d->allowMouseEvents();break;case QEvent::FocusOut: {- d->debugEvent<QEvent, false>(event);+ d->debugEvent<QEvent>(event);KisAbstractInputAction::setInputManager(this);QPointF currentLocalPos =@@ -610,45 +583,36 @@ bool KisInputManager::eventFilterImpl(QEvent * event)break;}case QEvent::TabletPress: {- d->debugEvent<QTabletEvent, false>(event);QTabletEvent *tabletEvent = static_cast<QTabletEvent*>(event);{//Make sure the input actions know we are active.KisAbstractInputAction::setInputManager(this);retval = d->matcher.buttonPressed(tabletEvent->button(), tabletEvent);- if (!d->containsPointer) {- d->containsPointer = true;- d->touchHasBlockedPressEvents = false;- }}event->setAccepted(true);retval = true;- d->blockMouseEvents();- d->startBlockingTouch();+//Reset signal compressor to prevent processing events before press lated->resetCompressor();-#if defined Q_OS_LINUX && !KRITA_QT_HAS_ENTER_LEAVE_PATCH// remove this hack when this patch is integrated:// https://codereview.qt-project.org/#/c/255384/event->setAccepted(false);- d->eatOneMousePress();+ d->eventEater.eatOneMousePress();#elif defined Q_OS_WIN32/*** Windows is the only platform that synthesizes mouse events for* the tablet on OS-level, that is, even when we accept the event*/- d->eatOneMousePress();+ d->eventEater.eatOneMousePress();#endifbreak;}case QEvent::TabletMove: {- d->debugEvent<QTabletEvent, false>(event);-QTabletEvent *tabletEvent = static_cast<QTabletEvent*>(event);retval = compressMoveEventCommon(tabletEvent);@@ -656,15 +620,6 @@ bool KisInputManager::eventFilterImpl(QEvent * event)d->tabletLatencyTracker->push(tabletEvent->timestamp());}- /**- * The flow of tablet events means the tablet is in the- * proximity area, so activate it even when the- * TabletEnterProximity event was missed (may happen when- * changing focus of the window with tablet in the proximity- * area)- */- d->blockMouseEvents();-#if defined Q_OS_LINUX && !KRITA_QT_HAS_ENTER_LEAVE_PATCH// remove this hack when this patch is integrated:// https://codereview.qt-project.org/#/c/255384/@@ -674,12 +629,6 @@ bool KisInputManager::eventFilterImpl(QEvent * event)break;}case QEvent::TabletRelease: {-#if defined(Q_OS_MAC) || defined(Q_OS_ANDROID)- d->allowMouseEvents();-#endif- d->stopBlockingTouch();- d->debugEvent<QTabletEvent, false>(event);-QTabletEvent *tabletEvent = static_cast<QTabletEvent*>(event);retval = d->matcher.buttonReleased(tabletEvent->button(), tabletEvent);retval = true;@@ -696,21 +645,20 @@ bool KisInputManager::eventFilterImpl(QEvent * event)case QEvent::TouchBegin:{- d->debugEvent<QTouchEvent, false>(event);// The popup was dismissed in previous TouchBegin->TouchEnd sequence. We now have a new TouchBegin.d->popupWasActive = false;- if (startTouch(retval)) {- QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);- KisAbstractInputAction::setInputManager(this);- d->lastPointCount = touchEvent->touchPoints().size();- d->startingPos = touchEvent->touchPoints().at(0).pos();- d->previousPos = d->startingPos;+ QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);+ KisAbstractInputAction::setInputManager(this);+ d->lastPointCount = touchEvent->touchPoints().size();+ d->startingPos = touchEvent->touchPoints().at(0).pos();+ d->previousPos = d->startingPos;- retval = d->matcher.touchBeginEvent(touchEvent);+ retval = d->matcher.touchBeginEvent(touchEvent);- d->resetCompressor();- event->accept();- }+ d->resetCompressor();+ // we accept touch events unconditionally to disable+ // system-wide gesture on the canvas+ event->accept();break;}@@ -721,7 +669,6 @@ bool KisInputManager::eventFilterImpl(QEvent * event)return true;}QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);- d->debugEvent<QTouchEvent, false>(event);int eventPointCount = touchEvent->touchPoints().size();d->lastPointCount = eventPointCount;@@ -741,12 +688,13 @@ bool KisInputManager::eventFilterImpl(QEvent * event)#endifKisAbstractInputAction::setInputManager(this);retval = d->matcher.touchUpdateEvent(touchEvent);- d->touchHasBlockedPressEvents = retval;#ifdef Q_OS_MACOS}#endif+ // we accept touch events unconditionally to disable+ // system-wide gesture on the canvasevent->accept();break;}@@ -757,14 +705,12 @@ bool KisInputManager::eventFilterImpl(QEvent * event)event->setAccepted(true);return true;}- d->debugEvent<QTouchEvent, false>(event);QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);retval = d->matcher.touchEndEvent(touchEvent);- endTouch();- d->allowMouseEvents();-+ // we accept touch events unconditionally to disable+ // system-wide gesture on the canvasevent->accept();break;}@@ -786,9 +732,6 @@ bool KisInputManager::eventFilterImpl(QEvent * event)event->setAccepted(true);return true;}- d->debugEvent<QTouchEvent, false>(event);- endTouch();- d->allowMouseEvents();QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);if (ignoreCancel) {d->matcher.touchEndEvent(touchEvent);@@ -800,6 +743,9 @@ bool KisInputManager::eventFilterImpl(QEvent * event)d->startingPos = {0, 0};d->previousPos = {0, 0};retval = true;++ // we accept tablet events unconditionally to disable+ // system-wide gesture on the canvasevent->accept();break;}@@ -810,16 +756,13 @@ bool KisInputManager::eventFilterImpl(QEvent * event)switch (gevent->gestureType()) {case Qt::BeginNativeGesture:{- if (startTouch(retval)) {- KisAbstractInputAction::setInputManager(this);- retval = d->matcher.nativeGestureBeginEvent(gevent);- event->accept();- }+ KisAbstractInputAction::setInputManager(this);+ retval = d->matcher.nativeGestureBeginEvent(gevent);+ event->accept();break;}case Qt::EndNativeGesture:{- endTouch();retval = d->matcher.nativeGestureEndEvent(gevent);event->accept();break;@@ -842,36 +785,11 @@ bool KisInputManager::eventFilterImpl(QEvent * event)return !retval ? d->processUnhandledEvent(event) : true;}-bool KisInputManager::startTouch(bool &retval)-{- Q_UNUSED(retval);-- // TODO: can we remove that? It should be covered by the- // touch events stream-- // Touch rejection: if touch is disabled on canvas, no need to block mouse press events- if (KisConfig(true).disableTouchOnCanvas()) {- d->eatOneMousePress();- }-- return true;-}--void KisInputManager::endTouch()-{- d->touchHasBlockedPressEvents = false;-}-void KisInputManager::slotCompressedMoveEvent(){if (d->compressedMoveEvent) {- // d->touchHasBlockedPressEvents = false;-(void) d->handleCompressedTabletEvent(d->compressedMoveEvent.data());d->compressedMoveEvent.reset();- //dbgInput << "Compressed move event received.";- } else {- //dbgInput << "Unexpected empty move event";}}diff --git a/libs/ui/input/kis_input_manager.h b/libs/ui/input/kis_input_manager.hindex 38de484526..2ff40a16ea 100644--- a/libs/ui/input/kis_input_manager.h+++ b/libs/ui/input/kis_input_manager.h@@ -104,9 +104,6 @@ private Q_SLOTS:void slotConfigChanged();private:- bool startTouch(bool &retval);- void endTouch();-bool eventFilterImpl(QEvent * event);template <class Event>bool compressMoveEventCommon(Event *event);diff --git a/libs/ui/input/kis_input_manager_p.cpp b/libs/ui/input/kis_input_manager_p.cppindex 3b551b61eb..55fc9ce4de 100644--- a/libs/ui/input/kis_input_manager_p.cpp+++ b/libs/ui/input/kis_input_manager_p.cpp@@ -68,45 +68,115 @@ static bool isMouseEventType(QEvent::Type t)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);+}+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);- auto debugEvent = [&](int i) {- if (KisTabletDebugger::instance()->debugEnabled()) {- QString pre = QString("[BLOCKED %1:]").arg(i);- QMouseEvent *ev = static_cast<QMouseEvent*>(event);- dbgTablet << KisTabletDebugger::instance()->eventToString(*ev, pre);- }- };+ {+ // handle `newTabletIsHovering` and `tabletIsPressed` state transitions++ if (isTabletEventType(event->type())) {- auto debugTabletEvent = [&](int i) {- if (KisTabletDebugger::instance()->debugEnabled()) {- QString pre = QString("[BLOCKED %1:]").arg(i);QTabletEvent *ev = static_cast<QTabletEvent*>(event);- dbgTablet << KisTabletDebugger::instance()->eventToString(*ev, pre);+ 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++ if (event->type() == QEvent::TouchBegin ||+ event->type() == QEvent::TouchUpdate) {++ if (!touchIsActive) {+ touchIsActive = true;+ debugEaterStateTransition("touchIsActive"_L1, true, event->type());+ }}- };- auto debugTouchEvent = [&](int i) {- if (KisTabletDebugger::instance()->debugEnabled()) {- QString pre = QString("[BLOCKED %1:]").arg(i);- QTouchEvent *ev = static_cast<QTouchEvent*>(event);- dbgTablet << KisTabletDebugger::instance()->eventToString(*ev, pre);+ if (event->type() == QEvent::TouchEnd ||+ event->type() == QEvent::TouchCancel) {+ if (touchIsActive) {+ touchIsActive = false;+ debugEaterStateTransition("touchIsActive"_L1, false, event->type());+ }}- };+ }- if (peckish && event->type() == QEvent::MouseButtonPress+ if (eatOneMousePressEvent && event->type() == QEvent::MouseButtonPress// Drop one mouse press following tabletPress or touchBegin&& (static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton)) {- peckish = false;- debugEvent(1);+ eatOneMousePressEvent = false;+ KisInputManager::Private::debugEvent<QMouseEvent>(event, BlockedByNextPressSuppression);return true;}@@ -116,7 +186,7 @@ bool KisInputManager::Private::EventEater::eventFilter(QObject* target, QEvent*QTabletEvent *te = static_cast<QTabletEvent*>(event);if (te->button() != Qt::LeftButton) {- debugTabletEvent(3);+ KisInputManager::Private::debugEvent<QTabletEvent>(te, BlockedByButtonsWorkaround);return true;}} else if (event->type() == QEvent::MouseButtonPress ||@@ -130,71 +200,109 @@ bool KisInputManager::Private::EventEater::eventFilter(QObject* target, QEvent*}}+ 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()) &&- (hungry+ (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- || (eatSyntheticEvents && static_cast<QMouseEvent*>(event)->source() != Qt::MouseEventNotSynthesized)+ || (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- debugEvent(2);return true;}- if (eatTouchEvents && event->type() == QEvent::TouchBegin) {- // Drop touch events. If QEvent::TouchBegin is ignored, we won't- // receive further touch events until the next TouchBegin.- debugTouchEvent(3);- event->ignore();- return true;- }+ if (event->type() == QEvent::TouchBegin &&+ (tabletIsInProximity || tabletIsHovering || tabletIsPressed)) {- return false; // All clear - let this one through!-}+ EventBlockingReasons reasons = NotBlocked;+ reasons.setFlag(BlockedByTabletProximity, tabletIsInProximity);+ reasons.setFlag(BlockedByTabletHover, tabletIsHovering);+ reasons.setFlag(BlockedByTabletPress, tabletIsPressed);+ KisInputManager::Private::debugEvent<QTouchEvent>(event, reasons);-void KisInputManager::Private::EventEater::activate()-{- if (!hungry && (KisTabletDebugger::instance()->debugEnabled())) {- dbgTablet << "Start blocking mouse events";+ 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;}- hungry = true;-}-void KisInputManager::Private::EventEater::deactivate()-{- if (hungry && (KisTabletDebugger::instance()->debugEnabled())) {- dbgTablet << "Stop blocking mouse events";+ 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);}- hungry = false;-}-void KisInputManager::Private::EventEater::eatOneMousePress()-{- // Enable on other platforms if getting full-pressure splotches- peckish = true;+ return false; // All clear - let this one through!}-void KisInputManager::Private::EventEater::startBlockingTouch()+void KisInputManager::Private::EventEater::notifyTabletEnterProximity(){- eatTouchEvents = true;+ using namespace Qt::StringLiterals;++ tabletIsInProximity = true;+ debugEaterStateTransition("tabletIsInProximity"_L1, true, QEvent::TabletEnterProximity);}-void KisInputManager::Private::EventEater::stopBlockingTouch()+void KisInputManager::Private::EventEater::notifyTabletLeaveProximity(){- eatTouchEvents = false;+ using namespace Qt::StringLiterals;++ tabletIsInProximity = false;+ debugEaterStateTransition("tabletIsInProximity"_L1, false, QEvent::TabletLeaveProximity);++ if (tabletIsHovering) {+ tabletIsHovering = false;+ debugEaterStateTransition("tabletIsHovering"_L1, false, QEvent::TabletLeaveProximity);+ }}-bool KisInputManager::Private::ignoringQtCursorEvents()+void KisInputManager::Private::EventEater::eatOneMousePress(){- return eventEater.hungry;+ // Enable on other platforms if getting full-pressure splotches+ eatOneMousePressEvent = true;}void KisInputManager::Private::setMaskSyntheticEvents(bool value){- eventEater.eatSyntheticEvents = value;+ Q_UNUSED(value)+ // TODO: remove}KisInputManager::Private::Private(KisInputManager *qq)@@ -428,21 +536,12 @@ bool KisInputManager::Private::ProximityNotifier::eventFilter(QObject* object, Q{switch (event->type()) {case QEvent::TabletEnterProximity:- d->debugEvent<QEvent, false>(event);- // Tablet proximity events are unreliable AND fake mouse events do not- // necessarily come after tablet events, so this is insufficient.- // d->eventEater.eatOneMousePress();-- // Qt sends fake mouse events instead of hover events, so not very useful.- // Don't block mouse events on tablet since tablet move events are not generated until- // after tablet press.-#ifndef Q_OS_MACOS- d->blockMouseEvents();-#endif+ d->debugEvent<QEvent>(event);+ d->eventEater.notifyTabletEnterProximity();break;case QEvent::TabletLeaveProximity:- d->debugEvent<QEvent, false>(event);- d->allowMouseEvents();+ d->debugEvent<QEvent>(event);+ d->eventEater.notifyTabletLeaveProximity();break;#ifdef Q_OS_WINcase QEvent::KeyPress:@@ -655,74 +754,16 @@ bool KisInputManager::Private::processUnhandledEvent(QEvent *event)return retval && !forwardAllEventsToTool;}-#ifdef HAVE_X11-inline QPointF dividePoints(const QPointF &pt1, const QPointF &pt2) {- return QPointF(pt1.x() / pt2.x(), pt1.y() / pt2.y());-}--inline QPointF multiplyPoints(const QPointF &pt1, const QPointF &pt2) {- return QPointF(pt1.x() * pt2.x(), pt1.y() * pt2.y());-}-#endif--void KisInputManager::Private::blockMouseEvents()-{- eventEater.activate();-}--void KisInputManager::Private::allowMouseEvents()-{- /**- * On Windows tablet events may arrive asynchronously to the- * mouse events (in WinTab mode). The problem is that Qt- * generates Enter/Leave and FocusIn/Out events via mouse- * events only. It means that TabletPress may come much before- * Enter and FocusIn event and start the stroke. In such a case- * we shouldn't unblock mouse events.- *- * See https://bugs.kde.org/show_bug.cgi?id=417040- *- * PS:- * Ideally, we should fix Qt to generate Enter/Leave and- * FocusIn/Out events based on tablet events as well, but- * it is a lot of work.- */-#ifdef Q_OS_WIN32- if (eventEater.hungry && matcher.hasRunningShortcut()) {- return;- }-#endif-- eventEater.deactivate();-}--void KisInputManager::Private::eatOneMousePress()-{- eventEater.eatOneMousePress();-}-void KisInputManager::Private::resetCompressor() {compressedMoveEvent.reset();moveEventCompressor.stop();}-void KisInputManager::Private::startBlockingTouch()-{- eventEater.startBlockingTouch();-}--void KisInputManager::Private::stopBlockingTouch()-{- eventEater.stopBlockingTouch();-}-bool KisInputManager::Private::handleCompressedTabletEvent(QEvent *event){bool retval = false;- if (event->type() == QTouchEvent::TouchUpdate && touchHasBlockedPressEvents) {- matcher.touchUpdateEvent((QTouchEvent *)event);- } else if (!matcher.pointerMoved(event) && toolProxy && event->type() != QTouchEvent::TouchUpdate) {+ if (!matcher.pointerMoved(event) && toolProxy) {toolProxy->forwardHoverEvent(event);}retval = true;diff --git a/libs/ui/input/kis_input_manager_p.h b/libs/ui/input/kis_input_manager_p.hindex bc829c5ae8..9295c685fd 100644--- a/libs/ui/input/kis_input_manager_p.h+++ b/libs/ui/input/kis_input_manager_p.h@@ -25,6 +25,18 @@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{@@ -47,9 +59,6 @@ public:QPointer<KisToolProxy> toolProxy;bool forwardAllEventsToTool = false;- bool ignoringQtCursorEvents();-- bool touchHasBlockedPressEvents = false;KisShortcutMatcher matcher;@@ -77,24 +86,43 @@ public:KisPopupWidgetInterface *popupWidget;- QVector<QTouchEvent *> bufferedTouchEvents;-- void blockMouseEvents();- void allowMouseEvents();- void eatOneMousePress();void setMaskSyntheticEvents(bool value);void resetCompressor();- void startBlockingTouch();- void stopBlockingTouch();- template <class Event, bool useBlocking>- void debugEvent(QEvent *event)+ template <class Event>+ static void debugEvent(QEvent *event, EventBlockingReasons reasons = NotBlocked){- if (!KisTabletDebugger::instance()->debugEnabled()) return;+ 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";+ }- QString msg1 = useBlocking && ignoringQtCursorEvents() ? "[BLOCKED] " : "[ ]";- Event *specificEvent = static_cast<Event*>(event);- dbgTablet << KisTabletDebugger::instance()->eventToString(*specificEvent, msg1);+ 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@@ -132,29 +160,24 @@ public:bool eventFilter(QObject* target, QEvent* event);- // This should be called after we're certain a tablet stroke has started.- void activate();- // This should be called after a tablet stroke has ended.- void deactivate();+ void notifyTabletEnterProximity();+ void notifyTabletLeaveProximity();// On Windows, we sometimes receive mouse events very late, so watch & wait.void eatOneMousePress();- // This should be called after the tablet is pressed,- void startBlockingTouch();- // This should be called after the tablet is released.- void stopBlockingTouch();-- bool hungry{false}; // Continue eating mouse strokes- bool peckish{false}; // Eat a single mouse press event- bool eatSyntheticEvents{false}; // Mask all synthetic events+ bool eatOneMousePressEvent{false}; // Eat a single mouse press eventbool activateSecondaryButtonsWorkaround{false}; // Use mouse events for right- and middle-clicks- bool eatTouchEvents{false}; // Eat touch interactions++ 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;- bool containsPointer = false;-int accumulatedScrollDelta = 0;class TabletLatencyTracker : public KisLatencyTracker {