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++
/*
* 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;
#ifdef Q_OS_LINUX
#define KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND
#endif
#ifdef KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND
#include <optional>
#endif /* KRITA_ENABLE_MISSING_PROXIMITY_ENTER_WORKAROUND */
class KRITAUI_EXPORT KisInputEventsEater
{
public:
enum EventBlockingReason {
NotBlocked = 0x0,
BlockedByTabletProximity = 0x01,
BlockedByTabletHover = 0x02,
BlockedByTabletPress = 0x04,
BlockedByTouchPress = 0x08,
BlockedByNextPressSuppression = 0x10,
BlockedByButtonsWorkaround = 0x20,
BlockedBySynthetic = 0x40
};
Q_DECLARE_FLAGS(EventBlockingReasons, EventBlockingReason)
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};
#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,
QEvent::Type eventType,
const QLatin1String &comment = QLatin1String());
};
Q_DECLARE_OPERATORS_FOR_FLAGS(KisInputEventsEater::EventBlockingReasons)