Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

0051-Add-workaround-for-handling-table-press-correctly-in.patch 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
From 9a2a150476d51d786e011e1ddde4bb77c7b9d146 Mon Sep 17 00:00:00 2001
From: Dmitry Kazakov <dimula73@gmail.com>
Date: Wed, 15 May 2019 19:54:52 +0300
Subject: [PATCH 2/2] Add workaround for handling table press correctly in
 WinInk mode

Original problem: widgets do not get synthesized mouse-down and
mouse-press events until the stylus is released

Reason: if the app accepts the event, WndProc should report
that to the system (by returning true). This is the only way to
prevent Windows from starting some system-wide gestures, like
click+hold -> right button click. If we ignore the event, then
OS postpones all synthesized mouse events until the entire gesture
is completed.

The patch implements a "hackish" workaround for the original problem
by using the following rules:

1) All tablet-move events are ignored (without synthesized mouse events
   OS doesn't generate any Enter/Leave events)

2) All not-accepted tablet press- and release-events and also reported as
   ignored (without it D&D doesn't work).

3) All accepted tablet press- and release-events are reported as "accepted",
   **but** we artificially synthesize mouse events for them.

TODO: there are still two problems:

1) Perhaps this synthesizeMouseEvent() is not needed at all. But we should
   first check if Qt relies on these synthesized messages anywhere in the
   code or not.

2) If we still keep synthesizeMouseEvent(), then it should fetch actual
   tablet buttons from QGuiApplicationPrivate::tabletDevicePoint(). Right
   now it always synthesizes left-click, whatever the button was pressed/
   released.

See bug: https://bugs.kde.org/show_bug.cgi?id=406668
---
 src/gui/kernel/qguiapplication.cpp            |  1 +
 src/gui/kernel/qwindowsysteminterface.cpp     | 20 ++---
 src/gui/kernel/qwindowsysteminterface.h       |  8 +-
 .../windows/qwindowspointerhandler.cpp        | 75 ++++++++++++++++++-
 4 files changed, 86 insertions(+), 18 deletions(-)

diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp
index 3541c1ae..681cbd85 100644
--- a/src/gui/kernel/qguiapplication.cpp
+++ b/src/gui/kernel/qguiapplication.cpp
@@ -2536,6 +2536,7 @@ void QGuiApplicationPrivate::processTabletEvent(QWindowSystemInterfacePrivate::T
     tabletEvent.setTimestamp(e->timestamp);
     QGuiApplication::sendSpontaneousEvent(window, &tabletEvent);
     pointData.state = e->buttons;
+    e->eventAccepted = tabletEvent.isAccepted();
     if (!tabletEvent.isAccepted()
         && !QWindowSystemInterfacePrivate::TabletEvent::platformSynthesizesMouse
         && qApp->testAttribute(Qt::AA_SynthesizeMouseForUnhandledTabletEvents)) {
diff --git a/src/gui/kernel/qwindowsysteminterface.cpp b/src/gui/kernel/qwindowsysteminterface.cpp
index 0bedae1b..779a9ee4 100644
--- a/src/gui/kernel/qwindowsysteminterface.cpp
+++ b/src/gui/kernel/qwindowsysteminterface.cpp
@@ -869,7 +869,7 @@ void QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(boo
     platformSynthesizesMouse = v;
 }
 
-void QWindowSystemInterface::handleTabletEvent(QWindow *window, ulong timestamp, const QPointF &local, const QPointF &global,
+bool QWindowSystemInterface::handleTabletEvent(QWindow *window, ulong timestamp, const QPointF &local, const QPointF &global,
                                                int device, int pointerType, Qt::MouseButtons buttons, qreal pressure, int xTilt, int yTilt,
                                                qreal tangentialPressure, qreal rotation, int z, qint64 uid,
                                                Qt::KeyboardModifiers modifiers)
@@ -880,36 +880,36 @@ void QWindowSystemInterface::handleTabletEvent(QWindow *window, ulong timestamp,
                                                        QHighDpi::fromNativePixels(global, window),
                                                        device, pointerType, buttons, pressure,
                                                        xTilt, yTilt, tangentialPressure, rotation, z, uid, modifiers);
-    QWindowSystemInterfacePrivate::handleWindowSystemEvent(e);
+    return QWindowSystemInterfacePrivate::handleWindowSystemEvent<QWindowSystemInterface::SynchronousDelivery>(e);
 }
 
-void QWindowSystemInterface::handleTabletEvent(QWindow *window, const QPointF &local, const QPointF &global,
+bool QWindowSystemInterface::handleTabletEvent(QWindow *window, const QPointF &local, const QPointF &global,
                                                int device, int pointerType, Qt::MouseButtons buttons, qreal pressure, int xTilt, int yTilt,
                                                qreal tangentialPressure, qreal rotation, int z, qint64 uid,
                                                Qt::KeyboardModifiers modifiers)
 {
     ulong time = QWindowSystemInterfacePrivate::eventTime.elapsed();
-    handleTabletEvent(window, time, local, global, device, pointerType, buttons, pressure,
+    return handleTabletEvent(window, time, local, global, device, pointerType, buttons, pressure,
                       xTilt, yTilt, tangentialPressure, rotation, z, uid, modifiers);
 }
 
 #if QT_DEPRECATED_SINCE(5, 10)
-void QWindowSystemInterface::handleTabletEvent(QWindow *window, ulong timestamp, bool down, const QPointF &local, const QPointF &global,
+bool QWindowSystemInterface::handleTabletEvent(QWindow *window, ulong timestamp, bool down, const QPointF &local, const QPointF &global,
                                                int device, int pointerType, qreal pressure, int xTilt, int yTilt,
                                                qreal tangentialPressure, qreal rotation, int z, qint64 uid,
                                                Qt::KeyboardModifiers modifiers)
 {
-    handleTabletEvent(window, timestamp, local, global, device, pointerType, (down ? Qt::LeftButton : Qt::NoButton), pressure,
-                      xTilt, yTilt, tangentialPressure, rotation, z, uid, modifiers);
+    return handleTabletEvent(window, timestamp, local, global, device, pointerType, (down ? Qt::LeftButton : Qt::NoButton), pressure,
+                             xTilt, yTilt, tangentialPressure, rotation, z, uid, modifiers);
 }
 
-void QWindowSystemInterface::handleTabletEvent(QWindow *window, bool down, const QPointF &local, const QPointF &global,
+bool QWindowSystemInterface::handleTabletEvent(QWindow *window, bool down, const QPointF &local, const QPointF &global,
                                                int device, int pointerType, qreal pressure, int xTilt, int yTilt,
                                                qreal tangentialPressure, qreal rotation, int z, qint64 uid,
                                                Qt::KeyboardModifiers modifiers)
 {
-    handleTabletEvent(window, local, global, device, pointerType, (down ? Qt::LeftButton : Qt::NoButton), pressure,
-                      xTilt, yTilt, tangentialPressure, rotation, z, uid, modifiers);
+    return handleTabletEvent(window, local, global, device, pointerType, (down ? Qt::LeftButton : Qt::NoButton), pressure,
+                             xTilt, yTilt, tangentialPressure, rotation, z, uid, modifiers);
 }
 #endif // QT_DEPRECATED_SINCE(5, 10)
 
diff --git a/src/gui/kernel/qwindowsysteminterface.h b/src/gui/kernel/qwindowsysteminterface.h
index 1dde9130..0ad89547 100644
--- a/src/gui/kernel/qwindowsysteminterface.h
+++ b/src/gui/kernel/qwindowsysteminterface.h
@@ -243,20 +243,20 @@ public:
     static void handleFileOpenEvent(const QString& fileName);
     static void handleFileOpenEvent(const QUrl &url);
 
-    static void handleTabletEvent(QWindow *window, ulong timestamp, const QPointF &local, const QPointF &global,
+    static bool handleTabletEvent(QWindow *window, ulong timestamp, const QPointF &local, const QPointF &global,
                                   int device, int pointerType, Qt::MouseButtons buttons, qreal pressure, int xTilt, int yTilt,
                                   qreal tangentialPressure, qreal rotation, int z, qint64 uid,
                                   Qt::KeyboardModifiers modifiers = Qt::NoModifier);
-    static void handleTabletEvent(QWindow *window, const QPointF &local, const QPointF &global,
+    static bool handleTabletEvent(QWindow *window, const QPointF &local, const QPointF &global,
                                   int device, int pointerType, Qt::MouseButtons buttons, qreal pressure, int xTilt, int yTilt,
                                   qreal tangentialPressure, qreal rotation, int z, qint64 uid,
                                   Qt::KeyboardModifiers modifiers = Qt::NoModifier);
 #if QT_DEPRECATED_SINCE(5, 10)
-    QT_DEPRECATED static void handleTabletEvent(QWindow *window, ulong timestamp, bool down, const QPointF &local, const QPointF &global,
+    QT_DEPRECATED static bool handleTabletEvent(QWindow *window, ulong timestamp, bool down, const QPointF &local, const QPointF &global,
                                                 int device, int pointerType, qreal pressure, int xTilt, int yTilt,
                                                 qreal tangentialPressure, qreal rotation, int z, qint64 uid,
                                                 Qt::KeyboardModifiers modifiers = Qt::NoModifier);
-    QT_DEPRECATED static void handleTabletEvent(QWindow *window, bool down, const QPointF &local, const QPointF &global,
+    QT_DEPRECATED static bool handleTabletEvent(QWindow *window, bool down, const QPointF &local, const QPointF &global,
                                                 int device, int pointerType, qreal pressure, int xTilt, int yTilt,
                                                 qreal tangentialPressure, qreal rotation, int z, qint64 uid,
                                                 Qt::KeyboardModifiers modifiers = Qt::NoModifier);
diff --git a/src/plugins/platforms/windows/qwindowspointerhandler.cpp b/src/plugins/platforms/windows/qwindowspointerhandler.cpp
index 07f4d41e..58a5d19f 100644
--- a/src/plugins/platforms/windows/qwindowspointerhandler.cpp
+++ b/src/plugins/platforms/windows/qwindowspointerhandler.cpp
@@ -518,6 +518,58 @@ bool QWindowsPointerHandler::translateTouchEvent(QWindow *window, HWND hwnd,
     return false; // Allow mouse messages to be generated.
 }
 
+void synthesizeMouseEvent(QEvent::Type type, Qt::MouseButton button, const POINTER_PEN_INFO &penInfo)
+{
+    // Update the cursor position
+    BOOL result = SetCursorPos(penInfo.pointerInfo.ptPixelLocationRaw.x, penInfo.pointerInfo.ptPixelLocationRaw.y);
+    if (!result) {
+        qCDebug(lcQpaEvents).noquote().nospace() << showbase
+            << __FUNCTION__ << "SetCursorPos failed, err" << GetLastError();
+        return;
+    }
+    // Send mousebutton down/up events. Windows stores the button state.
+    DWORD inputDataFlags = 0;
+    switch (type) {
+    case QEvent::TabletPress:
+        switch (button) {
+        case Qt::LeftButton:
+            inputDataFlags = MOUSEEVENTF_LEFTDOWN;
+            break;
+        case Qt::RightButton:
+            inputDataFlags = MOUSEEVENTF_RIGHTDOWN;
+            break;
+        default:
+            return;
+        }
+        break;
+    case QEvent::TabletRelease:
+        switch (button) {
+        case Qt::LeftButton:
+            inputDataFlags = MOUSEEVENTF_LEFTUP;
+            break;
+        case Qt::RightButton:
+            inputDataFlags = MOUSEEVENTF_RIGHTUP;
+            break;
+        default:
+            return;
+        }
+        break;
+    case QEvent::TabletMove:
+    default:
+        return;
+    }
+    INPUT inputData = {};
+    inputData.type = INPUT_MOUSE;
+    inputData.mi.dwFlags = inputDataFlags;
+    inputData.mi.dwExtraInfo = 0xFF515700 | 0x01; // https://msdn.microsoft.com/en-us/library/windows/desktop/ms703320%28v=vs.85%29.aspx
+    UINT result2 = SendInput(1, &inputData, sizeof(inputData));
+    if (result2 != 1) {
+        qCDebug(lcQpaEvents).noquote().nospace() << showbase
+            << __FUNCTION__ << "SendInput failed, err" << GetLastError();
+        return;
+    }
+}
+
 bool QWindowsPointerHandler::translatePenEvent(QWindow *window, HWND hwnd, QtWindows::WindowsEventType et,
                                                MSG msg, PVOID vPenInfo)
 {
@@ -610,10 +662,25 @@ bool QWindowsPointerHandler::translatePenEvent(QWindow *window, HWND hwnd, QtWin
         }
         const Qt::KeyboardModifiers keyModifiers = QWindowsKeyMapper::queryKeyboardModifiers();
 
-        QWindowSystemInterface::handleTabletEvent(target, localPos, hiResGlobalPos, device, type, mouseButtons,
-                                                  pressure, xTilt, yTilt, tangentialPressure, rotation, z,
-                                                  sourceDevice, keyModifiers);
-        return false;  // Allow mouse messages to be generated.
+        const bool accepted =
+            QWindowSystemInterface::handleTabletEvent(target, localPos, hiResGlobalPos, device, type, mouseButtons,
+                                                      pressure, xTilt, yTilt, tangentialPressure, rotation, z,
+                                                      sourceDevice, keyModifiers);
+
+        /**
+         * TODO: right now we can fake-synthesize only left button clicks. For
+         *       synthesizing right button clicks we should fetch tablet state
+         *       from QGuiApplicationPrivate::tabletDevicePoint()
+         */
+        if (accepted && (msg.message == WM_POINTERDOWN || msg.message == WM_POINTERUP)) {
+            QEvent::Type type = (msg.message == WM_POINTERDOWN) ? QEvent::TabletPress : QEvent::TabletRelease;
+            Qt::MouseButton button = Qt::LeftButton;
+
+            synthesizeMouseEvent(type, button, *penInfo);
+            return true;
+        } else {
+            return false; // Allow mouse messages to be generated by OS
+        }
     }
     }
     return true;
-- 
2.20.1.windows.1