0026-Fetch-mapped-screen-size-from-the-Wintab-driver.patch 6.92 KB
Newer Older
Boudewijn Rempt's avatar
Boudewijn Rempt committed
1 2 3 4
diff --git a/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp b/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp
index 3e35b1464..11e6769c8 100644
--- a/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp
+++ b/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.cpp
5 6 7 8 9 10 11 12 13
@@ -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)
14 15 16 17 18 19 20 21 22 23
     // 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()
24
@@ -394,6 +399,84 @@ QWindowsTabletDeviceData QWindowsTabletSupport::tabletInit(qint64 uniqueId, UINT
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
     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);
57
+    m_wintabScreenGeometry = QRect(lc.lcSysOrgX, lc.lcSysOrgY, lc.lcSysExtX, lc.lcSysExtY);
58
+
59
+    qCDebug(lcQpaTablet) << "Updated tablet mapping: " << m_wintabScreenGeometry;
60 61 62 63
+    if (QGuiApplication::primaryScreen()) {
+        qCDebug(lcQpaTablet) << "   real desktop geometry: " << QWindowsScreen::virtualGeometry(QGuiApplication::primaryScreen()->handle());
+    }
+}
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
+
+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;
+}
105 106 107 108
+
 bool QWindowsTabletSupport::translateTabletProximityEvent(WPARAM /* wParam */, LPARAM lParam)
 {
     PACKET proximityBuffer[1]; // we are only interested in the first packet in this case
109 110 111
@@ -421,6 +504,8 @@ bool QWindowsTabletSupport::translateTabletProximityEvent(WPARAM /* wParam */, L
     if (!totalPacks)
         return false;
112
 
113
+    updateEffectiveScreenGeometry();
114
+
115 116 117
     const UINT currentCursor = proximityBuffer[0].pkCursor;
     UINT physicalCursorId;
     QWindowsTabletSupport::m_winTab32DLL.wTInfo(WTI_CURSORS + currentCursor, CSR_PHYSID, &physicalCursorId);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
118
@@ -535,8 +620,8 @@ bool QWindowsTabletSupport::translateTabletPacketEvent()
119 120 121 122 123 124 125
     //    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;
126 127
 
     if (QWindowsContext::verbose > 1)  {
128
         qCDebug(lcQpaTablet) << __FUNCTION__ << "processing" << packetCount
Boudewijn Rempt's avatar
Boudewijn Rempt committed
129 130 131 132 133
diff --git a/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.h b/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.h
index 5b1ddb529..59c996cd3 100644
--- a/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.h
+++ b/qtbase/src/plugins/platforms/windows/qwindowstabletsupport.h
@@ -45,7 +45,9 @@
134 135 136 137
 
 #include <QtCore/qvector.h>
 #include <QtCore/qpoint.h>
+#include <QtCore/qrect.h>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
138
 #include <QtCore/qmap.h>
139 140 141 142
+#include <QtCore/qobject.h>
 
 #include <wintab.h>
 
Boudewijn Rempt's avatar
Boudewijn Rempt committed
143 144
@@ -55,7 +57,7 @@ QT_BEGIN_NAMESPACE
 
145 146
 class QDebug;
 class QWindow;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
147
-class QRect;
148 149 150 151
+class QScreen;
 
 struct QWindowsWinTab32DLL
 {
Boudewijn Rempt's avatar
Boudewijn Rempt committed
152
@@ -108,7 +110,7 @@ struct QWindowsTabletDeviceData
153 154 155 156 157 158 159 160
 QDebug operator<<(QDebug d, const QWindowsTabletDeviceData &t);
 #endif
 
-class QWindowsTabletSupport
+class QWindowsTabletSupport : public QObject
 {
     Q_DISABLE_COPY(QWindowsTabletSupport)
 
Boudewijn Rempt's avatar
Boudewijn Rempt committed
161
@@ -141,9 +143,14 @@ public:
162 163 164 165 166 167 168 169 170 171
     int absoluteRange() const { return m_absoluteRange; }
     void setAbsoluteRange(int a) { m_absoluteRange = a; }
 
+private Q_SLOTS:
+    void slotPrimaryScreenChanged(QScreen *screen);
+    void slotScreenGeometryChanged();
+
 private:
     unsigned options() const;
     QWindowsTabletDeviceData tabletInit(qint64 uniqueId, UINT cursorType) const;
172 173 174 175
+    void updateEffectiveScreenGeometry();
 
     static QWindowsWinTab32DLL m_winTab32DLL;
     const HWND m_window;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
176
@@ -154,6 +161,9 @@ private:
177 178 179 180
     int m_currentDevice = -1;
     Mode m_mode = PenMode;
     State m_state = PenUp;
+    QScreen *m_connectedScreen = 0;
181 182
+    QRect m_wintabScreenGeometry;
+    QRect m_effectiveScreenGeometry;
183 184 185
 };
 
 QT_END_NAMESPACE