0027-Switch-stylus-pointer-type-when-the-tablet-is-in-the.patch 8.09 KB
Newer Older
Boudewijn Rempt's avatar
Boudewijn Rempt committed
1 2 3 4 5
diff --git a/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp b/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp
index 11e6769c8..b04312769 100644
--- a/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp
+++ b/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp
@@ -604,7 +604,6 @@ bool QWindowsTabletSupport::translateTabletPacketEvent()
6 7 8 9 10 11 12
         return false;
 
     const int currentDevice = m_devices.at(m_currentDevice).currentDevice;
-    const int currentPointer = m_devices.at(m_currentDevice).currentPointerType;
     const qint64 uniqueId = m_devices.at(m_currentDevice).uniqueId;
 
     // The tablet can be used in 2 different modes (reflected in enum Mode),
Boudewijn Rempt's avatar
Boudewijn Rempt committed
13
@@ -634,6 +633,28 @@ bool QWindowsTabletSupport::translateTabletPacketEvent()
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
     for (int i = 0; i < packetCount ; ++i) {
         const PACKET &packet = localPacketBuf[i];
 
+        int currentPointer = m_devices.at(m_currentDevice).currentPointerType;
+
+        const int packetPointerType = pointerType(packet.pkCursor);
+        if (!packet.pkButtons && packetPointerType != currentPointer) {
+
+            QWindowSystemInterface::handleTabletLeaveProximityEvent(packet.pkTime,
+                                                                    m_devices.at(m_currentDevice).currentDevice,
+                                                                    m_devices.at(m_currentDevice).currentPointerType,
+                                                                    m_devices.at(m_currentDevice).uniqueId);
+
+
+
+            m_devices[m_currentDevice].currentPointerType = packetPointerType;
+
+            QWindowSystemInterface::handleTabletEnterProximityEvent(packet.pkTime,
+                                                                    m_devices.at(m_currentDevice).currentDevice,
+                                                                    m_devices.at(m_currentDevice).currentPointerType,
+                                                                    m_devices.at(m_currentDevice).uniqueId);
+
+            currentPointer = packetPointerType;
+        }
+
         const int z = currentDevice == QTabletEvent::FourDMouse ? int(packet.pkZ) : 0;
 
         QPointF globalPosF =
Boudewijn Rempt's avatar
Boudewijn Rempt committed
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
diff --git a/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp.orig b/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp.orig
index 316551014..11e6769c8 100644
--- a/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp.orig
+++ b/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp.orig
@@ -53,6 +53,7 @@
 #include <QtCore/qdebug.h>
 #include <QtCore/qvarlengtharray.h>
 #include <QtCore/qmath.h>
+#include <QtCore/qregularexpression.h>
 
 #include <private/qguiapplication_p.h>
 #include <QtCore/private/qsystemlibrary_p.h>
@@ -216,6 +217,10 @@ QWindowsTabletSupport::QWindowsTabletSupport(HWND window, HCTX context)
     // Some tablets don't support tilt, check if it is possible,
     if (QWindowsTabletSupport::m_winTab32DLL.wTInfo(WTI_DEVICES, DVC_ORIENTATION, &orientation))
         m_tiltSupport = orientation[0].axResolution && orientation[1].axResolution;
+
+    connect(qGuiApp, &QGuiApplication::primaryScreenChanged,
+            this, QWindowsTabletSupport::slotPrimaryScreenChanged);
+    slotScreenGeometryChanged();
 }
 
 QWindowsTabletSupport::~QWindowsTabletSupport()
@@ -394,6 +399,84 @@ QWindowsTabletDeviceData QWindowsTabletSupport::tabletInit(qint64 uniqueId, UINT
     return result;
 }
 
+void QWindowsTabletSupport::slotPrimaryScreenChanged(QScreen *screen)
+{
+    if (m_connectedScreen)
+        disconnect(m_connectedScreen, 0, this, 0);
+
+    m_connectedScreen = screen;
+
+    if (m_connectedScreen)
+        connect(m_connectedScreen, &QScreen::virtualGeometryChanged,
+                this, &QWindowsTabletSupport::slotScreenGeometryChanged);
+
+    slotScreenGeometryChanged();
+}
+
+void QWindowsTabletSupport::slotScreenGeometryChanged()
+{
+    /**
+     * Some Wintab implementations map the tablet area to the entire
+     * virtual screen, but others (e.g. Microsoft SP5) don't. They
+     * may input range to a single (built-in) screen. The logic is
+     * quite obvious: when the screen has integrated tablet device,
+     * one cannot map this tablet device to another display.
+     *
+     * For such devices, we should always request mapped area from
+     * lcSys{Org,Ext}{X,Y} fields and use it accordingly.
+     */
+
+    LOGCONTEXT lc;
+    QWindowsTabletSupport::m_winTab32DLL.wTInfo(WTI_DEFSYSCTX, 0, &lc);
+    m_wintabScreenGeometry = QRect(lc.lcSysOrgX, lc.lcSysOrgY, lc.lcSysExtX, lc.lcSysExtY);
+
+    qCDebug(lcQpaTablet) << "Updated tablet mapping: " << m_wintabScreenGeometry;
+    if (QGuiApplication::primaryScreen()) {
+        qCDebug(lcQpaTablet) << "   real desktop geometry: " << QWindowsScreen::virtualGeometry(QGuiApplication::primaryScreen()->handle());
+    }
+}
+
+void QWindowsTabletSupport::updateEffectiveScreenGeometry()
+{
+    QRect customGeometry;
+    bool dontUseWintabDesktopRect = false;
+
+    const QString geometry = qEnvironmentVariable("QT_WINTAB_DESKTOP_RECT");
+    if (!geometry.isEmpty()) {
+        QString tmp = QString::fromLatin1("([+-]?\\d+);([+-]?\\d+);(\\d+);(\\d+)");
+
+        QRegularExpression rex(tmp);
+        QRegularExpressionMatch match = rex.match(geometry);
+
+        if (match.hasMatch()) {
+            customGeometry.setRect(match.captured(1).toInt(),
+                                   match.captured(2).toInt(),
+                                   match.captured(3).toInt(),
+                                   match.captured(4).toInt());
+
+            qCDebug(lcQpaTablet) << "apply QT_WINTAB_DESKTOP_RECT:" << customGeometry;
+        } else {
+            qCWarning(lcQpaTablet) << "failed to parse QT_WINTAB_DESKTOP_RECT:" << geometry;
+        }
+    }
+
+    if (qEnvironmentVariableIsSet("QT_IGNORE_WINTAB_MAPPING")) {
+        if (!customGeometry.isValid()) {
+            qCDebug(lcQpaTablet) << "fallback mapping is requested via QT_IGNORE_WINTAB_MAPPING";
+        } else {
+            qCWarning(lcQpaTablet) << "ignoring QT_IGNORE_WINTAB_MAPPING, because QT_WINTAB_DESKTOP_RECT is set";
+        }
+        dontUseWintabDesktopRect = true;
+    }
+
+    m_effectiveScreenGeometry =
+        !customGeometry.isValid() ?
+        (dontUseWintabDesktopRect ?
+             QWindowsScreen::virtualGeometry(QGuiApplication::primaryScreen()->handle()) :
+             m_wintabScreenGeometry) :
+        customGeometry;
+}
+
 bool QWindowsTabletSupport::translateTabletProximityEvent(WPARAM /* wParam */, LPARAM lParam)
 {
     PACKET proximityBuffer[1]; // we are only interested in the first packet in this case
@@ -421,6 +504,8 @@ bool QWindowsTabletSupport::translateTabletProximityEvent(WPARAM /* wParam */, L
     if (!totalPacks)
         return false;
 
+    updateEffectiveScreenGeometry();
+
     const UINT currentCursor = proximityBuffer[0].pkCursor;
     UINT physicalCursorId;
     QWindowsTabletSupport::m_winTab32DLL.wTInfo(WTI_CURSORS + currentCursor, CSR_PHYSID, &physicalCursorId);
@@ -535,8 +620,8 @@ bool QWindowsTabletSupport::translateTabletPacketEvent()
     //    in which case we snap the position to the mouse position.
     // It seems there is no way to find out the mode programmatically, the LOGCONTEXT orgX/Y/Ext
     // area is always the virtual desktop.
-    const QRect virtualDesktopArea =
-        QWindowsScreen::virtualGeometry(QGuiApplication::primaryScreen()->handle());
+
+    const QRect virtualDesktopArea = m_effectiveScreenGeometry;
 
     if (QWindowsContext::verbose > 1)  {
         qCDebug(lcQpaTablet) << __FUNCTION__ << "processing" << packetCount
@@ -562,6 +647,11 @@ bool QWindowsTabletSupport::translateTabletPacketEvent()
             m_state = PenDown;
             m_mode = (mouseLocation - globalPosF).manhattanLength() > m_absoluteRange
                 ? MouseMode : PenMode;
+
+            // Krita doesn't support mouse mode. And this code may break
+            // normal painting, so we just disable it.
+            m_mode = PenMode;
+
             qCDebug(lcQpaTablet) << __FUNCTION__ << "mode=" << m_mode << "pen:"
                 << globalPosF << "mouse:" << mouseLocation;
         }