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++

Implement a missing tablet proximity event workaround

Dmitry Kazakov30fe859c36b6
diff --git a/libs/ui/input/KisInputEventsEater.cpp b/libs/ui/input/KisInputEventsEater.cpp
index 0429d2f2f4..da1de20b80 100644
--- a/libs/ui/input/KisInputEventsEater.cpp
+++ b/libs/ui/input/KisInputEventsEater.cpp
@@ -110,7 +110,32 @@ bool KisInputEventsEater::eventFilter(QObject* target, QEvent* event )
tabletIsPressed = newTabletIsPressed;
debugEaterStateTransition("tabletIsPressed"_L1, newTabletIsPressed, event->type());
}
+
+#ifdef KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND
+ if (!tabletIsInProximity && !missingTabletProximityEventsWorkaround) {
+ warnInputEater << "WARNING: received a tablet event while the tablet is not in proximity! Activating a workaround...";
+ missingTabletProximityEventsWorkaround = MissingTabletProximityEnterWorkaround();
+ } else if (tabletIsInProximity && missingTabletProximityEventsWorkaround) {
+ warnInputEater << "WARNING: received a delayed tablet proximity event, disabling the workaround...";
+ missingTabletProximityEventsWorkaround = std::nullopt;
+ } else if (missingTabletProximityEventsWorkaround) {
+ missingTabletProximityEventsWorkaround->notifyTabletEventArrived();
+ }
+#endif /* KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND */
+ }
+
+#ifdef KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND
+ if (missingTabletProximityEventsWorkaround && !tabletIsPressed && isMouseEventType(event->type())) {
+ missingTabletProximityEventsWorkaround->notifyMouseEventArrived();
+ if (missingTabletProximityEventsWorkaround->shouldForgetAboutTabletInProximity()) {
+ if (tabletIsHovering) {
+ tabletIsHovering = false;
+ debugEaterStateTransition("tabletIsHovering"_L1, false, event->type(), "missing proximity workaround"_L1);
+ }
+ missingTabletProximityEventsWorkaround = std::nullopt;
+ }
}
+#endif /* KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND */
if (event->type() == QEvent::FocusOut ||
event->type() == QEvent::Leave ||
@@ -135,6 +160,10 @@ bool KisInputEventsEater::eventFilter(QObject* target, QEvent* event )
tabletIsPressed = false;
debugEaterStateTransition("tabletIsPressed"_L1, false, event->type());
}
+
+#ifdef KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND
+ missingTabletProximityEventsWorkaround = std::nullopt;
+#endif /* KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND */
}
// also proximity-leave in a corresponding function...
diff --git a/libs/ui/input/KisInputEventsEater.h b/libs/ui/input/KisInputEventsEater.h
index 9feac92e8d..68c07ca82c 100644
--- a/libs/ui/input/KisInputEventsEater.h
+++ b/libs/ui/input/KisInputEventsEater.h
@@ -12,6 +12,10 @@
class KisToolInvocationAction;
+#ifdef Q_OS_LINUX
+#define KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND
+#endif
+
class KRITAUI_EXPORT KisInputEventsEater
{
@@ -85,6 +89,45 @@ private:
bool tabletIsPressed{false};
bool touchIsActive{false};
+#ifdef KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND
+ /**
+ * On some versions of XWayland (namely 23.2.6 and 24.1.8)
+ * the proximity events may be missing due to a bug
+ * in XWayland. This may cause the event eater to never
+ * leave the `tabletIsHovering` state.
+ *
+ * To workaround this issue we just count received mouse
+ * events. If we got 10 mouse events without a single tablet
+ * event in between, then the tablet device has probably
+ * left proximity and we should allow mouse events again.
+ *
+ * This workaround relies of the fact that on X11 the
+ * synthesized mouse events come in a synchronous way,
+ * i.e. one mouse event follows one tablet event.
+ *
+ * This workaround will **not** work with WinTab
+ * protocol, since in WinTab the mouse events are
+ * generated by the driver and delivered asyn-
+ * chronouslly in batches of 10-20 events.
+ */
+ struct MissingTabletProximityEnterWorkaround {
+ int numSequentialMouseEvents = 0;
+
+ void notifyTabletEventArrived() {
+ numSequentialMouseEvents = 0;
+ }
+
+ void notifyMouseEventArrived() {
+ numSequentialMouseEvents++;
+ }
+
+ bool shouldForgetAboutTabletInProximity() const {
+ return numSequentialMouseEvents > 10;
+ }
+ };
+ std::optional<MissingTabletProximityEnterWorkaround> missingTabletProximityEventsWorkaround;
+#endif /* KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND */
+
private:
void debugEaterStateTransition(const QLatin1String &stateName,
bool newValue,