summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/xcb/qxcbconnection_xi2.cpp
blob: 5ddf545d515d186edf9f1b52a044f7cbf9fac793 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qxcbconnection.h"
#include "qxcbkeyboard.h"
#include "qxcbscrollingdevice_p.h"
#include "qxcbscreen.h"
#include "qxcbwindow.h"
#include "QtCore/qmetaobject.h"
#include "QtCore/qmath.h"
#include <QtGui/qpointingdevice.h>
#include <QtGui/private/qpointingdevice_p.h>
#include <qpa/qwindowsysteminterface_p.h>
#include <QDebug>

#include <xcb/xinput.h>

#define QT_XCB_HAS_TOUCHPAD_GESTURES (XCB_INPUT_MINOR_VERSION >= 4)

using qt_xcb_input_device_event_t = xcb_input_button_press_event_t;
#if QT_XCB_HAS_TOUCHPAD_GESTURES
using qt_xcb_input_pinch_event_t = xcb_input_gesture_pinch_begin_event_t;
using qt_xcb_input_swipe_event_t = xcb_input_gesture_swipe_begin_event_t;
#endif

struct qt_xcb_input_event_mask_t {
    xcb_input_event_mask_t header;
    alignas(4) uint8_t     mask[8] = {}; // up to 2 units of 4 bytes
};

static inline void setXcbMask(uint8_t* mask, int bit)
{
    // note that XI protocol always uses little endian for masks over the wire
    mask[bit >> 3] |= 1 << (bit & 7);
}

void QXcbConnection::xi2SelectStateEvents()
{
    // These state events do not depend on a specific X window, but are global
    // for the X client's (application's) state.
    qt_xcb_input_event_mask_t xiEventMask;
    xiEventMask.header.deviceid = XCB_INPUT_DEVICE_ALL;
    xiEventMask.header.mask_len = 1;
    setXcbMask(xiEventMask.mask, XCB_INPUT_HIERARCHY);
    setXcbMask(xiEventMask.mask, XCB_INPUT_DEVICE_CHANGED);
    setXcbMask(xiEventMask.mask, XCB_INPUT_PROPERTY);
    xcb_input_xi_select_events(xcb_connection(), rootWindow(), 1, &xiEventMask.header);
}

void QXcbConnection::xi2SelectDeviceEvents(xcb_window_t window)
{
    if (window == rootWindow())
        return;

    qt_xcb_input_event_mask_t mask;

    setXcbMask(mask.mask, XCB_INPUT_BUTTON_PRESS);
    setXcbMask(mask.mask, XCB_INPUT_BUTTON_RELEASE);
    setXcbMask(mask.mask, XCB_INPUT_MOTION);
    // There is a check for enter/leave events in plain xcb enter/leave event handler,
    // core enter/leave events will be ignored in this case.
    setXcbMask(mask.mask, XCB_INPUT_ENTER);
    setXcbMask(mask.mask, XCB_INPUT_LEAVE);
    if (isAtLeastXI22()) {
        setXcbMask(mask.mask, XCB_INPUT_TOUCH_BEGIN);
        setXcbMask(mask.mask, XCB_INPUT_TOUCH_UPDATE);
        setXcbMask(mask.mask, XCB_INPUT_TOUCH_END);
    }
#if QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES
    if (isAtLeastXI24()) {
        setXcbMask(mask.mask, XCB_INPUT_GESTURE_PINCH_BEGIN);
        setXcbMask(mask.mask, XCB_INPUT_GESTURE_PINCH_UPDATE);
        setXcbMask(mask.mask, XCB_INPUT_GESTURE_PINCH_END);
        setXcbMask(mask.mask, XCB_INPUT_GESTURE_SWIPE_BEGIN);
        setXcbMask(mask.mask, XCB_INPUT_GESTURE_SWIPE_UPDATE);
        setXcbMask(mask.mask, XCB_INPUT_GESTURE_SWIPE_END);
    }
#endif // QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES

    mask.header.deviceid = XCB_INPUT_DEVICE_ALL;
    mask.header.mask_len = 2;
    xcb_void_cookie_t cookie =
            xcb_input_xi_select_events_checked(xcb_connection(), window, 1, &mask.header);
    xcb_generic_error_t *error = xcb_request_check(xcb_connection(), cookie);
    if (error) {
        qCDebug(lcQpaXInput, "failed to select events, window %x, error code %d", window, error->error_code);
        free(error);
    } else {
        QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(false);
    }
}

static inline qreal fixed3232ToReal(xcb_input_fp3232_t val)
{
    return qreal(val.integral) + qreal(val.frac) / (1ULL << 32);
}

#if QT_CONFIG(tabletevent)
/*!
    \internal
    Find the existing QPointingDevice instance representing a particular tablet or stylus;
    or create and register a new instance if it was not found.

    An instance can be uniquely identified by its \a devType, \a pointerType and \a uniqueId.
    The rest of the arguments are necessary to create a new instance.

    If the instance represents a stylus, the instance representing the tablet
    itself must be given as \a master. Otherwise, \a master must be the xinput
    master device (core pointer) to which the tablet belongs.  It should not be
    null, because \a master is also the QObject::parent() for memory management.

    Proximity events have incomplete information. So as a side effect, if an
    existing instance is found, it is updated with the given \a usbId and
    \a toolId, and the seat ID of \a master, in case those values were only
    now discovered, or the seat assignment changed (?).
*/
static const QPointingDevice *tabletToolInstance(QPointingDevice *master, const QString &tabletName,
                                                 qint64 id, quint32 usbId, quint32 toolId, qint64 uniqueId,
                                                 QPointingDevice::PointerType pointerTypeOverride = QPointingDevice::PointerType::Unknown,
                                                 QPointingDevice::Capabilities capsOverride = QInputDevice::Capability::None)
{
    QInputDevice::DeviceType devType = QInputDevice::DeviceType::Stylus;
    QPointingDevice::PointerType pointerType = QPointingDevice::PointerType::Pen;
    QPointingDevice::Capabilities caps = QInputDevice::Capability::Position |
            QInputDevice::Capability::Pressure |
            QInputDevice::Capability::MouseEmulation |
            QInputDevice::Capability::Hover |
            capsOverride;
    int buttonCount = 3; // the tip, plus two barrel buttons
    // keep in sync with wacom_intuos_inout() in Linux kernel driver wacom_wac.c
    // TODO yeah really, there are many more now so this needs updating
    switch (toolId) {
    case 0xd12:
    case 0x912:
    case 0x112:
    case 0x913: /* Intuos3 Airbrush */
    case 0x902: /* Intuos4/5 13HD/24HD Airbrush */
    case 0x100902: /* Intuos4/5 13HD/24HD Airbrush */
        devType = QInputDevice::DeviceType::Airbrush;
        caps.setFlag(QInputDevice::Capability::XTilt);
        caps.setFlag(QInputDevice::Capability::YTilt);
        caps.setFlag(QInputDevice::Capability::TangentialPressure);
        buttonCount = 2;
        break;
    case 0x91b: /* Intuos3 Airbrush Eraser */
    case 0x90a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
    case 0x10090a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
        devType = QInputDevice::DeviceType::Airbrush;
        pointerType = QPointingDevice::PointerType::Eraser;
        caps.setFlag(QInputDevice::Capability::XTilt);
        caps.setFlag(QInputDevice::Capability::YTilt);
        caps.setFlag(QInputDevice::Capability::TangentialPressure);
        buttonCount = 2;
        break;
    case 0x007: /* Mouse 4D and 2D */
    case 0x09c:
    case 0x094:
        // TODO set something to indicate a multi-dimensional capability:
        // Capability::3D or 4D or QPointingDevice::setMaximumDimensions()?
        devType = QInputDevice::DeviceType::Mouse;
        buttonCount = 5; // TODO only if it's a 4D Mouse
        break;
    case 0x017: /* Intuos3 2D Mouse */
    case 0x806: /* Intuos4 Mouse */
        devType = QInputDevice::DeviceType::Mouse;
        break;
    case 0x096: /* Lens cursor */
    case 0x097: /* Intuos3 Lens cursor */
    case 0x006: /* Intuos4 Lens cursor */
        devType = QInputDevice::DeviceType::Puck;
        break;
    case 0x885:    /* Intuos3 Art Pen (Marker Pen) */
    case 0x100804: /* Intuos4/5 13HD/24HD Art Pen */
        caps.setFlag(QInputDevice::Capability::XTilt);
        caps.setFlag(QInputDevice::Capability::YTilt);
        caps.setFlag(QInputDevice::Capability::Rotation);
        buttonCount = 1;
        break;
    case 0x10080c: /* Intuos4/5 13HD/24HD Art Pen Eraser */
        pointerType = QPointingDevice::PointerType::Eraser;
        caps.setFlag(QInputDevice::Capability::XTilt);
        caps.setFlag(QInputDevice::Capability::YTilt);
        caps.setFlag(QInputDevice::Capability::Rotation);
        buttonCount = 1;
        break;
    case 0:
        pointerType = QPointingDevice::PointerType::Unknown;
        break;
    }
    if (pointerTypeOverride != QPointingDevice::PointerType::Unknown)
        pointerType = pointerTypeOverride;
    const QPointingDevice *ret = QPointingDevicePrivate::queryTabletDevice(devType, pointerType,
                                                                           QPointingDeviceUniqueId::fromNumericId(uniqueId),
                                                                           caps, id);
    if (!ret) {
        ret = new QPointingDevice(tabletName, id, devType, pointerType, caps, 1, buttonCount,
                                  master ? master->seatName() : QString(),
                                  QPointingDeviceUniqueId::fromNumericId(uniqueId), master);
        QWindowSystemInterface::registerInputDevice(ret);
    }
    QPointingDevicePrivate *devPriv = QPointingDevicePrivate::get(const_cast<QPointingDevice *>(ret));
    devPriv->busId = QString::number(usbId, 16);
    devPriv->toolId = toolId;
    if (master)
        devPriv->seatName = master->seatName();
    return ret;
}

static const char *toolName(QInputDevice::DeviceType tool) {
    static const QMetaObject *metaObject = qt_getEnumMetaObject(tool);
    static const QMetaEnum me = metaObject->enumerator(metaObject->indexOfEnumerator(qt_getEnumName(tool)));
    return me.valueToKey(int(tool));
}

static const char *pointerTypeName(QPointingDevice::PointerType ptype) {
    static const QMetaObject *metaObject = qt_getEnumMetaObject(ptype);
    static const QMetaEnum me = metaObject->enumerator(metaObject->indexOfEnumerator(qt_getEnumName(ptype)));
    return me.valueToKey(int(ptype));
}
#endif

void QXcbConnection::xi2SetupSlavePointerDevice(void *info, bool removeExisting, QPointingDevice *master)
{
    auto *deviceInfo = reinterpret_cast<xcb_input_xi_device_info_t *>(info);
    if (removeExisting) {
#if QT_CONFIG(tabletevent)
        for (int i = 0; i < m_tabletData.count(); ++i) {
            if (m_tabletData.at(i).deviceId == deviceInfo->deviceid) {
                m_tabletData.remove(i);
                break;
            }
        }
#endif
        m_touchDevices.remove(deviceInfo->deviceid);
    }

    const QByteArray nameRaw = QByteArray(xcb_input_xi_device_info_name(deviceInfo),
                                    xcb_input_xi_device_info_name_length(deviceInfo));
    const QString name = QString::fromUtf8(nameRaw);
    qCDebug(lcQpaXInputDevices) << "input device " << name << "ID" << deviceInfo->deviceid;
#if QT_CONFIG(tabletevent)
    TabletData tabletData;
#endif
    QXcbScrollingDevicePrivate *scrollingDeviceP = nullptr;
    auto scrollingDevice = [&]() {
        if (!scrollingDeviceP)
            scrollingDeviceP = new QXcbScrollingDevicePrivate(name, deviceInfo->deviceid,
                                                              QInputDevice::Capability::Scroll);
        return scrollingDeviceP;
    };

    int buttonCount = 32;
    auto classes_it = xcb_input_xi_device_info_classes_iterator(deviceInfo);
    for (; classes_it.rem; xcb_input_device_class_next(&classes_it)) {
        xcb_input_device_class_t *classinfo = classes_it.data;
        switch (classinfo->type) {
        case XCB_INPUT_DEVICE_CLASS_TYPE_VALUATOR: {
            auto *vci = reinterpret_cast<xcb_input_valuator_class_t *>(classinfo);
            const int valuatorAtom = qatom(vci->label);
            qCDebug(lcQpaXInputDevices) << "   has valuator" << atomName(vci->label) << "recognized?" << (valuatorAtom < QXcbAtom::NAtoms);
#if QT_CONFIG(tabletevent)
            if (valuatorAtom < QXcbAtom::NAtoms) {
                TabletData::ValuatorClassInfo info;
                info.minVal = fixed3232ToReal(vci->min);
                info.maxVal = fixed3232ToReal(vci->max);
                info.number = vci->number;
                tabletData.valuatorInfo[valuatorAtom] = info;
            }
#endif // QT_CONFIG(tabletevent)
            if (valuatorAtom == QXcbAtom::RelHorizScroll || valuatorAtom == QXcbAtom::RelHorizWheel)
                scrollingDevice()->lastScrollPosition.setX(fixed3232ToReal(vci->value));
            else if (valuatorAtom == QXcbAtom::RelVertScroll || valuatorAtom == QXcbAtom::RelVertWheel)
                scrollingDevice()->lastScrollPosition.setY(fixed3232ToReal(vci->value));
            break;
        }
        case XCB_INPUT_DEVICE_CLASS_TYPE_SCROLL: {
            auto *sci = reinterpret_cast<xcb_input_scroll_class_t *>(classinfo);
            if (sci->scroll_type == XCB_INPUT_SCROLL_TYPE_VERTICAL) {
                auto dev = scrollingDevice();
                dev->orientations.setFlag(Qt::Vertical);
                dev->verticalIndex = sci->number;
                dev->verticalIncrement = fixed3232ToReal(sci->increment);
            } else if (sci->scroll_type == XCB_INPUT_SCROLL_TYPE_HORIZONTAL) {
                auto dev = scrollingDevice();
                dev->orientations.setFlag(Qt::Horizontal);
                dev->horizontalIndex = sci->number;
                dev->horizontalIncrement = fixed3232ToReal(sci->increment);
            }
            break;
        }
        case XCB_INPUT_DEVICE_CLASS_TYPE_BUTTON: {
            auto *bci = reinterpret_cast<xcb_input_button_class_t *>(classinfo);
            xcb_atom_t *labels = nullptr;
            if (bci->num_buttons >= 5) {
                labels = xcb_input_button_class_labels(bci);
                xcb_atom_t label4 = labels[3];
                xcb_atom_t label5 = labels[4];
                // Some drivers have no labels on the wheel buttons, some have no label on just one and some have no label on
                // button 4 and the wrong one on button 5. So we just check that they are not labelled with unrelated buttons.
                if ((!label4 || qatom(label4) == QXcbAtom::ButtonWheelUp || qatom(label4) == QXcbAtom::ButtonWheelDown) &&
                    (!label5 || qatom(label5) == QXcbAtom::ButtonWheelUp || qatom(label5) == QXcbAtom::ButtonWheelDown))
                    scrollingDevice()->legacyOrientations |= Qt::Vertical;
            }
            if (bci->num_buttons >= 7) {
                xcb_atom_t label6 = labels[5];
                xcb_atom_t label7 = labels[6];
                if ((!label6 || qatom(label6) == QXcbAtom::ButtonHorizWheelLeft) && (!label7 || qatom(label7) == QXcbAtom::ButtonHorizWheelRight))
                    scrollingDevice()->legacyOrientations |= Qt::Horizontal;
            }
            buttonCount = bci->num_buttons;
            qCDebug(lcQpaXInputDevices, "   has %d buttons", bci->num_buttons);
            break;
        }
        case XCB_INPUT_DEVICE_CLASS_TYPE_KEY:
            qCDebug(lcQpaXInputDevices) << "   it's a keyboard";
            break;
        case XCB_INPUT_DEVICE_CLASS_TYPE_TOUCH:
#if QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES
        case XCB_INPUT_DEVICE_CLASS_TYPE_GESTURE:
#endif // QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES
            // will be handled in populateTouchDevices()
            break;
        default:
            qCDebug(lcQpaXInputDevices) << "   has class" << classinfo->type;
            break;
        }
    }
    bool isTablet = false;
#if QT_CONFIG(tabletevent)
    // If we have found the valuators which we expect a tablet to have, it might be a tablet.
    if (tabletData.valuatorInfo.contains(QXcbAtom::AbsX) &&
            tabletData.valuatorInfo.contains(QXcbAtom::AbsY) &&
            tabletData.valuatorInfo.contains(QXcbAtom::AbsPressure))
        isTablet = true;

    // But we need to be careful not to take the touch and tablet-button devices as tablets.
    QByteArray nameLower = nameRaw.toLower();
    QString dbgType = QLatin1String("UNKNOWN");
    if (nameLower.contains("eraser")) {
        isTablet = true;
        tabletData.pointerType = QPointingDevice::PointerType::Eraser;
        dbgType = QLatin1String("eraser");
    } else if (nameLower.contains("cursor") && !(nameLower.contains("cursor controls") && nameLower.contains("trackball"))) {
        isTablet = true;
        tabletData.pointerType = QPointingDevice::PointerType::Cursor;
        dbgType = QLatin1String("cursor");
    } else if (nameLower.contains("wacom") && nameLower.contains("finger touch")) {
        isTablet = false;
    } else if ((nameLower.contains("pen") || nameLower.contains("stylus")) && isTablet) {
        tabletData.pointerType = QPointingDevice::PointerType::Pen;
        dbgType = QLatin1String("pen");
    } else if (nameLower.contains("wacom") && isTablet && !nameLower.contains("touch")) {
        // combined device (evdev) rather than separate pen/eraser (wacom driver)
        tabletData.pointerType = QPointingDevice::PointerType::Pen;
        dbgType = QLatin1String("pen");
    } else if (nameLower.contains("aiptek") /* && device == QXcbAtom::KEYBOARD */) {
        // some "Genius" tablets
        isTablet = true;
        tabletData.pointerType = QPointingDevice::PointerType::Pen;
        dbgType = QLatin1String("pen");
    } else if (nameLower.contains("waltop") && nameLower.contains("tablet")) {
        // other "Genius" tablets
        // WALTOP International Corp. Slim Tablet
        isTablet = true;
        tabletData.pointerType = QPointingDevice::PointerType::Pen;
        dbgType = QLatin1String("pen");
    } else if (nameLower.contains("uc-logic") && isTablet) {
        tabletData.pointerType = QPointingDevice::PointerType::Pen;
        dbgType = QLatin1String("pen");
    } else if (nameLower.contains("ugee")) {
        isTablet = true;
        tabletData.pointerType = QPointingDevice::PointerType::Pen;
        dbgType = QLatin1String("pen");
    } else {
        isTablet = false;
    }

    if (isTablet) {
        tabletData.deviceId = deviceInfo->deviceid;
        tabletData.name = name;
        m_tabletData.append(tabletData);
        qCDebug(lcQpaXInputDevices) << "   it's a tablet with pointer type" << dbgType;
        QPointingDevice::Capabilities capsOverride = QInputDevice::Capability::None;
        if (tabletData.valuatorInfo.contains(QXcbAtom::AbsTiltX))
            capsOverride.setFlag(QInputDevice::Capability::XTilt);
        if (tabletData.valuatorInfo.contains(QXcbAtom::AbsTiltY))
            capsOverride.setFlag(QInputDevice::Capability::YTilt);
        // TODO can we get USB ID?
        Q_ASSERT(deviceInfo->deviceid == tabletData.deviceId);
        const QPointingDevice *dev = tabletToolInstance(master,
                tabletData.name, deviceInfo->deviceid, 0, 0, tabletData.serialId,
                tabletData.pointerType, capsOverride);
        Q_ASSERT(dev);
    }
#endif // QT_CONFIG(tabletevent)

    if (scrollingDeviceP) {
        // Only use legacy wheel button events when we don't have real scroll valuators.
        scrollingDeviceP->legacyOrientations &= ~scrollingDeviceP->orientations;
        qCDebug(lcQpaXInputDevices) << "   it's a scrolling device";
    }

    if (!isTablet) {
        TouchDeviceData *dev = populateTouchDevices(deviceInfo, scrollingDeviceP);
        if (dev && lcQpaXInputDevices().isDebugEnabled()) {
            if (dev->qtTouchDevice->type() == QInputDevice::DeviceType::TouchScreen)
                qCDebug(lcQpaXInputDevices, "   it's a touchscreen with type %d capabilities 0x%X max touch points %d",
                        int(dev->qtTouchDevice->type()), qint32(dev->qtTouchDevice->capabilities()),
                        dev->qtTouchDevice->maximumPoints());
            else if (dev->qtTouchDevice->type() == QInputDevice::DeviceType::TouchPad)
                qCDebug(lcQpaXInputDevices, "   it's a touchpad with type %d capabilities 0x%X max touch points %d size %f x %f",
                        int(dev->qtTouchDevice->type()), qint32(dev->qtTouchDevice->capabilities()),
                        dev->qtTouchDevice->maximumPoints(),
                        dev->size.width(), dev->size.height());
        }
    }

    if (!QInputDevicePrivate::fromId(deviceInfo->deviceid)) {
        qCDebug(lcQpaXInputDevices) << "   it's a mouse";
        QInputDevice::Capabilities caps = QInputDevice::Capability::Position | QInputDevice::Capability::Hover;
        if (scrollingDeviceP) {
            scrollingDeviceP->capabilities |= caps;
            scrollingDeviceP->buttonCount = buttonCount;
            if (master)
                scrollingDeviceP->seatName = master->seatName();
            QWindowSystemInterface::registerInputDevice(new QXcbScrollingDevice(*scrollingDeviceP, master));
        } else {
            QWindowSystemInterface::registerInputDevice(new QPointingDevice(
                    name, deviceInfo->deviceid,
                    QInputDevice::DeviceType::Mouse, QPointingDevice::PointerType::Generic,
                    caps, 1, buttonCount, (master ? master->seatName() : QString()), QPointingDeviceUniqueId(), master));
        }
    }
}

void QXcbConnection::xi2SetupDevices()
{
#if QT_CONFIG(tabletevent)
    m_tabletData.clear();
#endif
    m_touchDevices.clear();
    m_xiMasterPointerIds.clear();

    auto reply = Q_XCB_REPLY(xcb_input_xi_query_device, xcb_connection(), XCB_INPUT_DEVICE_ALL);
    if (!reply) {
        qCDebug(lcQpaXInputDevices) << "failed to query devices";
        return;
    }

    // XInput doesn't provide a way to identify "seats"; but each device has an attachment to another device.
    // So we make up a seatId: master-keyboard-id << 16 | master-pointer-id.

    auto it = xcb_input_xi_query_device_infos_iterator(reply.get());
    for (; it.rem; xcb_input_xi_device_info_next(&it)) {
        xcb_input_xi_device_info_t *deviceInfo = it.data;
        switch (deviceInfo->type) {
        case XCB_INPUT_DEVICE_TYPE_MASTER_KEYBOARD: {
            auto dev = new QInputDevice(QString::fromUtf8(xcb_input_xi_device_info_name(deviceInfo)),
                                        deviceInfo->deviceid, QInputDevice::DeviceType::Keyboard,
                                        QString::number(deviceInfo->deviceid << 16 | deviceInfo->attachment, 16), this);
            QWindowSystemInterface::registerInputDevice(dev);
        } break;
        case XCB_INPUT_DEVICE_TYPE_MASTER_POINTER: {
            m_xiMasterPointerIds.append(deviceInfo->deviceid);
            auto dev = new QXcbScrollingDevice(QString::fromUtf8(xcb_input_xi_device_info_name(deviceInfo)), deviceInfo->deviceid,
                               QInputDevice::Capability::Position | QInputDevice::Capability::Scroll | QInputDevice::Capability::Hover,
                               32, QString::number(deviceInfo->attachment << 16 | deviceInfo->deviceid, 16), this);
            QWindowSystemInterface::registerInputDevice(dev);
            continue;
        } break;
        default:
            break;
        }
    }

    it = xcb_input_xi_query_device_infos_iterator(reply.get());
    for (; it.rem; xcb_input_xi_device_info_next(&it)) {
        xcb_input_xi_device_info_t *deviceInfo = it.data;
        switch (deviceInfo->type) {
        case XCB_INPUT_DEVICE_TYPE_MASTER_KEYBOARD:
        case XCB_INPUT_DEVICE_TYPE_MASTER_POINTER:
            // already registered
            break;
        case XCB_INPUT_DEVICE_TYPE_SLAVE_POINTER: {
            m_xiSlavePointerIds.append(deviceInfo->deviceid);
            QInputDevice *master = const_cast<QInputDevice *>(QInputDevicePrivate::fromId(deviceInfo->attachment));
            Q_ASSERT(master);
            xi2SetupSlavePointerDevice(deviceInfo, false, qobject_cast<QPointingDevice *>(master));
        } break;
        case XCB_INPUT_DEVICE_TYPE_SLAVE_KEYBOARD: {
            QInputDevice *master = const_cast<QInputDevice *>(QInputDevicePrivate::fromId(deviceInfo->attachment));
            Q_ASSERT(master);
            QWindowSystemInterface::registerInputDevice(new QInputDevice(
                QString::fromUtf8(xcb_input_xi_device_info_name(deviceInfo)), deviceInfo->deviceid,
                QInputDevice::DeviceType::Keyboard, master->seatName(), master));
        } break;
        case XCB_INPUT_DEVICE_TYPE_FLOATING_SLAVE:
            break;
        }
    }

    if (m_xiMasterPointerIds.size() > 1)
        qCDebug(lcQpaXInputDevices) << "multi-pointer X detected";
}

QXcbConnection::TouchDeviceData *QXcbConnection::touchDeviceForId(int id)
{
    TouchDeviceData *dev = nullptr;
    if (m_touchDevices.contains(id))
        dev = &m_touchDevices[id];
    return dev;
}

QXcbConnection::TouchDeviceData *QXcbConnection::populateTouchDevices(void *info, QXcbScrollingDevicePrivate *scrollingDeviceP)
{
    auto *deviceInfo = reinterpret_cast<xcb_input_xi_device_info_t *>(info);
    QPointingDevice::Capabilities caps;
    QInputDevice::DeviceType type = QInputDevice::DeviceType::Unknown;
    int maxTouchPoints = 1;
    bool isTouchDevice = false;
    bool hasRelativeCoords = false;
    TouchDeviceData dev;
    auto classes_it = xcb_input_xi_device_info_classes_iterator(deviceInfo);
    for (; classes_it.rem; xcb_input_device_class_next(&classes_it)) {
        xcb_input_device_class_t *classinfo = classes_it.data;
        switch (classinfo->type) {
        case XCB_INPUT_DEVICE_CLASS_TYPE_TOUCH: {
            auto *tci = reinterpret_cast<xcb_input_touch_class_t *>(classinfo);
            maxTouchPoints = tci->num_touches;
            qCDebug(lcQpaXInputDevices, "   has touch class with mode %d", tci->mode);
            switch (tci->mode) {
            case XCB_INPUT_TOUCH_MODE_DEPENDENT:
                type = QInputDevice::DeviceType::TouchPad;
                break;
            case XCB_INPUT_TOUCH_MODE_DIRECT:
                type = QInputDevice::DeviceType::TouchScreen;
                break;
            }
            break;
        }
#if QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES
        case XCB_INPUT_DEVICE_CLASS_TYPE_GESTURE: {
            // Note that gesture devices can only be touchpads (i.e. dependent devices in XInput
            // naming convention). According to XI 2.4, the same device can't have touch and
            // gesture device classes.
            auto *gci = reinterpret_cast<xcb_input_gesture_class_t *>(classinfo);
            maxTouchPoints = gci->num_touches;
            qCDebug(lcQpaXInputDevices, "   has gesture class");
            type = QInputDevice::DeviceType::TouchPad;
            break;
        }
#endif // QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES
        case XCB_INPUT_DEVICE_CLASS_TYPE_VALUATOR: {
            auto *vci = reinterpret_cast<xcb_input_valuator_class_t *>(classinfo);
            const QXcbAtom::Atom valuatorAtom = qatom(vci->label);
            if (valuatorAtom < QXcbAtom::NAtoms) {
                TouchDeviceData::ValuatorClassInfo info;
                info.min = fixed3232ToReal(vci->min);
                info.max = fixed3232ToReal(vci->max);
                info.number = vci->number;
                info.label = valuatorAtom;
                dev.valuatorInfo.append(info);
            }
            // Some devices (mice) report a resolution of 0; they will be excluded later,
            // for now just prevent a division by zero
            const int vciResolution = vci->resolution ? vci->resolution : 1;
            if (valuatorAtom == QXcbAtom::AbsMTPositionX)
                caps |= QInputDevice::Capability::Position | QInputDevice::Capability::NormalizedPosition;
            else if (valuatorAtom == QXcbAtom::AbsMTTouchMajor)
                caps |= QInputDevice::Capability::Area;
            else if (valuatorAtom == QXcbAtom::AbsMTOrientation)
                dev.providesTouchOrientation = true;
            else if (valuatorAtom == QXcbAtom::AbsMTPressure || valuatorAtom == QXcbAtom::AbsPressure)
                caps |= QInputDevice::Capability::Pressure;
            else if (valuatorAtom == QXcbAtom::RelX) {
                hasRelativeCoords = true;
                dev.size.setWidth((fixed3232ToReal(vci->max) - fixed3232ToReal(vci->min)) * 1000.0 / vciResolution);
            } else if (valuatorAtom == QXcbAtom::RelY) {
                hasRelativeCoords = true;
                dev.size.setHeight((fixed3232ToReal(vci->max) - fixed3232ToReal(vci->min)) * 1000.0 / vciResolution);
            } else if (valuatorAtom == QXcbAtom::AbsX) {
                caps |= QInputDevice::Capability::Position;
                dev.size.setWidth((fixed3232ToReal(vci->max) - fixed3232ToReal(vci->min)) * 1000.0 / vciResolution);
            } else if (valuatorAtom == QXcbAtom::AbsY) {
                caps |= QInputDevice::Capability::Position;
                dev.size.setHeight((fixed3232ToReal(vci->max) - fixed3232ToReal(vci->min)) * 1000.0 / vciResolution);
            } else if (valuatorAtom == QXcbAtom::RelVertWheel || valuatorAtom == QXcbAtom::RelHorizWheel) {
                caps |= QInputDevice::Capability::Scroll;
            }
            break;
        }
        default:
            break;
        }
    }
    if (type == QInputDevice::DeviceType::Unknown && caps && hasRelativeCoords) {
        type = QInputDevice::DeviceType::TouchPad;
        if (dev.size.width() < 10 || dev.size.height() < 10 ||
                dev.size.width() > 10000 || dev.size.height() > 10000)
            dev.size = QSizeF(130, 110);
    }
    if (!isAtLeastXI22() || type == QInputDevice::DeviceType::TouchPad)
        caps |= QInputDevice::Capability::MouseEmulation;

    if (type == QInputDevice::DeviceType::TouchScreen || type == QInputDevice::DeviceType::TouchPad) {
        QInputDevice *master = const_cast<QInputDevice *>(QInputDevicePrivate::fromId(deviceInfo->attachment));
        Q_ASSERT(master);
        if (scrollingDeviceP) {
            // valuators were already discovered in QXcbConnection::xi2SetupSlavePointerDevice, so just finish initialization
            scrollingDeviceP->deviceType = type;
            scrollingDeviceP->pointerType = QPointingDevice::PointerType::Finger;
            scrollingDeviceP->capabilities |= caps;
            scrollingDeviceP->maximumTouchPoints = maxTouchPoints;
            scrollingDeviceP->buttonCount = 3;
            scrollingDeviceP->seatName = master->seatName();
            dev.qtTouchDevice = new QXcbScrollingDevice(*scrollingDeviceP, master);
            if (Q_UNLIKELY(!caps.testFlag(QInputDevice::Capability::Scroll)))
                qCDebug(lcQpaXInputDevices) << "unexpectedly missing RelVert/HorizWheel atoms for touchpad with scroll capability" << dev.qtTouchDevice;
        } else {
            dev.qtTouchDevice = new QPointingDevice(QString::fromUtf8(xcb_input_xi_device_info_name(deviceInfo),
                                                                      xcb_input_xi_device_info_name_length(deviceInfo)),
                                                    deviceInfo->deviceid,
                                                    type, QPointingDevice::PointerType::Finger, caps, maxTouchPoints, 0,
                                                    master->seatName(), QPointingDeviceUniqueId(), master);
        }
        if (caps != 0)
            QWindowSystemInterface::registerInputDevice(dev.qtTouchDevice);
        m_touchDevices[deviceInfo->deviceid] = dev;
        isTouchDevice = true;
    }

    return isTouchDevice ? &m_touchDevices[deviceInfo->deviceid] : nullptr;
}

static inline qreal fixed1616ToReal(xcb_input_fp1616_t val)
{
    return qreal(val) / 0x10000;
}

void QXcbConnection::xi2HandleEvent(xcb_ge_event_t *event)
{
    auto *xiEvent = reinterpret_cast<qt_xcb_input_device_event_t *>(event);
    setTime(xiEvent->time);
    if (m_xiSlavePointerIds.contains(xiEvent->deviceid)) {
        if (!m_duringSystemMoveResize)
            return;
        if (xiEvent->event == XCB_NONE)
            return;

        if (xiEvent->event_type == XCB_INPUT_BUTTON_RELEASE
            && xiEvent->detail == XCB_BUTTON_INDEX_1 ) {
            abortSystemMoveResize(xiEvent->event);
        } else if (xiEvent->event_type == XCB_INPUT_TOUCH_END) {
            abortSystemMoveResize(xiEvent->event);
            return;
        } else {
            return;
        }
    }
    int sourceDeviceId = xiEvent->deviceid; // may be the master id
    qt_xcb_input_device_event_t *xiDeviceEvent = nullptr;
    xcb_input_enter_event_t *xiEnterEvent = nullptr;
    QXcbWindowEventListener *eventListener = nullptr;

    switch (xiEvent->event_type) {
    case XCB_INPUT_BUTTON_PRESS:
    case XCB_INPUT_BUTTON_RELEASE:
    case XCB_INPUT_MOTION:
    case XCB_INPUT_TOUCH_BEGIN:
    case XCB_INPUT_TOUCH_UPDATE:
    case XCB_INPUT_TOUCH_END:
    {
        xiDeviceEvent = xiEvent;
        eventListener = windowEventListenerFromId(xiDeviceEvent->event);
        sourceDeviceId = xiDeviceEvent->sourceid; // use the actual device id instead of the master
        break;
    }
#if QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES
    case XCB_INPUT_GESTURE_PINCH_BEGIN:
    case XCB_INPUT_GESTURE_PINCH_UPDATE:
    case XCB_INPUT_GESTURE_PINCH_END:
        xi2HandleGesturePinchEvent(event);
        return;
    case XCB_INPUT_GESTURE_SWIPE_BEGIN:
    case XCB_INPUT_GESTURE_SWIPE_UPDATE:
    case XCB_INPUT_GESTURE_SWIPE_END:
        xi2HandleGestureSwipeEvent(event);
        return;
#endif // QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES
    case XCB_INPUT_ENTER:
    case XCB_INPUT_LEAVE: {
        xiEnterEvent = reinterpret_cast<xcb_input_enter_event_t *>(event);
        eventListener = windowEventListenerFromId(xiEnterEvent->event);
        sourceDeviceId = xiEnterEvent->sourceid; // use the actual device id instead of the master
        break;
    }
    case XCB_INPUT_HIERARCHY:
        xi2HandleHierarchyEvent(event);
        return;
    case XCB_INPUT_DEVICE_CHANGED:
        xi2HandleDeviceChangedEvent(event);
        return;
    default:
        break;
    }

    if (eventListener) {
        if (eventListener->handleNativeEvent(reinterpret_cast<xcb_generic_event_t *>(event)))
            return;
    }

#if QT_CONFIG(tabletevent)
    if (!xiEnterEvent) {
        // TODO we need the UID here; tabletDataForDevice doesn't have enough to go on (?)
        QXcbConnection::TabletData *tablet = tabletDataForDevice(sourceDeviceId);
        if (tablet && xi2HandleTabletEvent(event, tablet))
            return;
    }
#endif // QT_CONFIG(tabletevent)

    if (auto device = QPointingDevicePrivate::pointingDeviceById(sourceDeviceId))
        xi2HandleScrollEvent(event, device);

    if (xiDeviceEvent) {
        switch (xiDeviceEvent->event_type) {
        case XCB_INPUT_BUTTON_PRESS:
        case XCB_INPUT_BUTTON_RELEASE:
        case XCB_INPUT_MOTION:
            if (eventListener && !(xiDeviceEvent->flags & XCB_INPUT_POINTER_EVENT_FLAGS_POINTER_EMULATED))
                eventListener->handleXIMouseEvent(event);
            break;

        case XCB_INPUT_TOUCH_BEGIN:
        case XCB_INPUT_TOUCH_UPDATE:
        case XCB_INPUT_TOUCH_END:
            if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled()))
                qCDebug(lcQpaXInputEvents, "XI2 touch event type %d seq %d detail %d pos %6.1f, %6.1f root pos %6.1f, %6.1f on window %x",
                        event->event_type, xiDeviceEvent->sequence, xiDeviceEvent->detail,
                        fixed1616ToReal(xiDeviceEvent->event_x), fixed1616ToReal(xiDeviceEvent->event_y),
                        fixed1616ToReal(xiDeviceEvent->root_x), fixed1616ToReal(xiDeviceEvent->root_y),xiDeviceEvent->event);
            if (QXcbWindow *platformWindow = platformWindowFromId(xiDeviceEvent->event))
                xi2ProcessTouch(xiDeviceEvent, platformWindow);
            break;
        }
    } else if (xiEnterEvent && eventListener) {
        switch (xiEnterEvent->event_type) {
        case XCB_INPUT_ENTER:
        case XCB_INPUT_LEAVE:
            eventListener->handleXIEnterLeave(event);
            break;
        }
    }
}

bool QXcbConnection::isTouchScreen(int id)
{
    auto device = touchDeviceForId(id);
    return device && device->qtTouchDevice->type() == QInputDevice::DeviceType::TouchScreen;
}

void QXcbConnection::xi2ProcessTouch(void *xiDevEvent, QXcbWindow *platformWindow)
{
    auto *xiDeviceEvent = reinterpret_cast<xcb_input_touch_begin_event_t *>(xiDevEvent);
    TouchDeviceData *dev = touchDeviceForId(xiDeviceEvent->sourceid);
    Q_ASSERT(dev);
    const bool firstTouch = dev->touchPoints.isEmpty();
    if (xiDeviceEvent->event_type == XCB_INPUT_TOUCH_BEGIN) {
        QWindowSystemInterface::TouchPoint tp;
        tp.id = xiDeviceEvent->detail % INT_MAX;
        tp.state = QEventPoint::State::Pressed;
        tp.pressure = -1.0;
        dev->touchPoints[tp.id] = tp;
    }
    QWindowSystemInterface::TouchPoint &touchPoint = dev->touchPoints[xiDeviceEvent->detail];
    QXcbScreen* screen = platformWindow->xcbScreen();
    qreal x = fixed1616ToReal(xiDeviceEvent->root_x);
    qreal y = fixed1616ToReal(xiDeviceEvent->root_y);
    qreal nx = -1.0, ny = -1.0;
    qreal w = 0.0, h = 0.0;
    bool majorAxisIsY = touchPoint.area.height() > touchPoint.area.width();
    for (const TouchDeviceData::ValuatorClassInfo &vci : qAsConst(dev->valuatorInfo)) {
        double value;
        if (!xi2GetValuatorValueIfSet(xiDeviceEvent, vci.number, &value))
            continue;
        if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled()))
            qCDebug(lcQpaXInputEvents, "   valuator %20s value %lf from range %lf -> %lf",
                    atomName(vci.label).constData(), value, vci.min, vci.max);
        if (value > vci.max)
            value = vci.max;
        if (value < vci.min)
            value = vci.min;
        qreal valuatorNormalized = (value - vci.min) / (vci.max - vci.min);
        if (vci.label == QXcbAtom::RelX) {
            nx = valuatorNormalized;
        } else if (vci.label == QXcbAtom::RelY) {
            ny = valuatorNormalized;
        } else if (vci.label == QXcbAtom::AbsX) {
            nx = valuatorNormalized;
        } else if (vci.label == QXcbAtom::AbsY) {
            ny = valuatorNormalized;
        } else if (vci.label == QXcbAtom::AbsMTPositionX) {
            nx = valuatorNormalized;
        } else if (vci.label == QXcbAtom::AbsMTPositionY) {
            ny = valuatorNormalized;
        } else if (vci.label == QXcbAtom::AbsMTTouchMajor) {
            const qreal sw = screen->geometry().width();
            const qreal sh = screen->geometry().height();
            w = valuatorNormalized * qHypot(sw, sh);
        } else if (vci.label == QXcbAtom::AbsMTTouchMinor) {
            const qreal sw = screen->geometry().width();
            const qreal sh = screen->geometry().height();
            h = valuatorNormalized * qHypot(sw, sh);
        } else if (vci.label == QXcbAtom::AbsMTOrientation) {
            // Find the closest axis.
            // 0 corresponds to the Y axis, vci.max to the X axis.
            // Flipping over the Y axis and rotating by 180 degrees
            // don't change the result, so normalize value to range
            // [0, vci.max] first.
            value = qAbs(value);
            while (value > vci.max)
                value -= 2 * vci.max;
            value = qAbs(value);
            majorAxisIsY = value < vci.max - value;
        } else if (vci.label == QXcbAtom::AbsMTPressure || vci.label == QXcbAtom::AbsPressure) {
            touchPoint.pressure = valuatorNormalized;
        }

    }
    // If any value was not updated, use the last-known value.
    if (nx == -1.0) {
        x = touchPoint.area.center().x();
        nx = x / screen->geometry().width();
    }
    if (ny == -1.0) {
        y = touchPoint.area.center().y();
        ny = y / screen->geometry().height();
    }
    if (xiDeviceEvent->event_type != XCB_INPUT_TOUCH_END) {
        if (!dev->providesTouchOrientation) {
            if (w == 0.0)
                w = touchPoint.area.width();
            h = w;
        } else {
            if (w == 0.0)
                w = qMax(touchPoint.area.width(), touchPoint.area.height());
            if (h == 0.0)
                h = qMin(touchPoint.area.width(), touchPoint.area.height());
            if (majorAxisIsY)
                qSwap(w, h);
        }
    }

    switch (xiDeviceEvent->event_type) {
    case XCB_INPUT_TOUCH_BEGIN:
        if (firstTouch) {
            dev->firstPressedPosition = QPointF(x, y);
            dev->firstPressedNormalPosition = QPointF(nx, ny);
        }
        dev->pointPressedPosition.insert(touchPoint.id, QPointF(x, y));

        // Touches must be accepted when we are grabbing touch events. Otherwise the entire sequence
        // will get replayed when the grab ends.
        if (m_xiGrab) {
            xcb_input_xi_allow_events(xcb_connection(), XCB_CURRENT_TIME, xiDeviceEvent->deviceid,
                                      XCB_INPUT_EVENT_MODE_ACCEPT_TOUCH,
                                      xiDeviceEvent->detail, xiDeviceEvent->event);
        }
        break;
    case XCB_INPUT_TOUCH_UPDATE:
        if (dev->qtTouchDevice->type() == QInputDevice::DeviceType::TouchPad && dev->pointPressedPosition.value(touchPoint.id) == QPointF(x, y)) {
            qreal dx = (nx - dev->firstPressedNormalPosition.x()) *
                dev->size.width() * screen->geometry().width() / screen->physicalSize().width();
            qreal dy = (ny - dev->firstPressedNormalPosition.y()) *
                dev->size.height() * screen->geometry().height() / screen->physicalSize().height();
            x = dev->firstPressedPosition.x() + dx;
            y = dev->firstPressedPosition.y() + dy;
            touchPoint.state = QEventPoint::State::Updated;
        } else if (touchPoint.area.center() != QPoint(x, y)) {
            touchPoint.state = QEventPoint::State::Updated;
            if (dev->qtTouchDevice->type() == QInputDevice::DeviceType::TouchPad)
                dev->pointPressedPosition[touchPoint.id] = QPointF(x, y);
        }

        if (dev->qtTouchDevice->type() == QInputDevice::DeviceType::TouchScreen &&
            xiDeviceEvent->event == m_startSystemMoveResizeInfo.window &&
            xiDeviceEvent->sourceid == m_startSystemMoveResizeInfo.deviceid &&
            xiDeviceEvent->detail == m_startSystemMoveResizeInfo.pointid) {
            QXcbWindow *window = platformWindowFromId(m_startSystemMoveResizeInfo.window);
            if (window) {
                xcb_input_xi_allow_events(xcb_connection(), XCB_CURRENT_TIME, xiDeviceEvent->deviceid,
                                          XCB_INPUT_EVENT_MODE_REJECT_TOUCH,
                                          xiDeviceEvent->detail, xiDeviceEvent->event);
                window->doStartSystemMoveResize(QPoint(x, y), m_startSystemMoveResizeInfo.edges);
                m_startSystemMoveResizeInfo.window = XCB_NONE;
            }
        }
        break;
    case XCB_INPUT_TOUCH_END:
        touchPoint.state = QEventPoint::State::Released;
        if (dev->qtTouchDevice->type() == QInputDevice::DeviceType::TouchPad && dev->pointPressedPosition.value(touchPoint.id) == QPointF(x, y)) {
            qreal dx = (nx - dev->firstPressedNormalPosition.x()) *
                dev->size.width() * screen->geometry().width() / screen->physicalSize().width();
            qreal dy = (ny - dev->firstPressedNormalPosition.y()) *
                dev->size.width() * screen->geometry().width() / screen->physicalSize().width();
            x = dev->firstPressedPosition.x() + dx;
            y = dev->firstPressedPosition.y() + dy;
        }
        dev->pointPressedPosition.remove(touchPoint.id);
    }
    touchPoint.area = QRectF(x - w/2, y - h/2, w, h);
    touchPoint.normalPosition = QPointF(nx, ny);

    if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled()))
        qCDebug(lcQpaXInputEvents) << "   touchpoint "  << touchPoint.id << " state " << touchPoint.state << " pos norm " << touchPoint.normalPosition <<
            " area " << touchPoint.area << " pressure " << touchPoint.pressure;
    Qt::KeyboardModifiers modifiers = keyboard()->translateModifiers(xiDeviceEvent->mods.effective);
    QWindowSystemInterface::handleTouchEvent(platformWindow->window(), xiDeviceEvent->time, dev->qtTouchDevice, dev->touchPoints.values(), modifiers);
    if (touchPoint.state == QEventPoint::State::Released)
        // If a touchpoint was released, we can forget it, because the ID won't be reused.
        dev->touchPoints.remove(touchPoint.id);
    else
        // Make sure that we don't send TouchPointPressed/Moved in more than one QTouchEvent
        // with this touch point if the next XI2 event is about a different touch point.
        touchPoint.state = QEventPoint::State::Stationary;
}

bool QXcbConnection::startSystemMoveResizeForTouch(xcb_window_t window, int edges)
{
    QHash<int, TouchDeviceData>::const_iterator devIt = m_touchDevices.constBegin();
    for (; devIt != m_touchDevices.constEnd(); ++devIt) {
        TouchDeviceData deviceData = devIt.value();
        if (deviceData.qtTouchDevice->type() == QInputDevice::DeviceType::TouchScreen) {
            auto pointIt = deviceData.touchPoints.constBegin();
            for (; pointIt != deviceData.touchPoints.constEnd(); ++pointIt) {
                QEventPoint::State state = pointIt.value().state;
                if (state == QEventPoint::State::Updated || state == QEventPoint::State::Pressed || state == QEventPoint::State::Stationary) {
                    m_startSystemMoveResizeInfo.window = window;
                    m_startSystemMoveResizeInfo.deviceid = devIt.key();
                    m_startSystemMoveResizeInfo.pointid = pointIt.key();
                    m_startSystemMoveResizeInfo.edges = edges;
                    setDuringSystemMoveResize(true);
                    qCDebug(lcQpaXInputDevices) << "triggered system move or resize from touch";
                    return true;
                }
            }
        }
    }
    return false;
}

void QXcbConnection::abortSystemMoveResize(xcb_window_t window)
{
    qCDebug(lcQpaXInputDevices) << "sending client message NET_WM_MOVERESIZE_CANCEL to window: " << window;
    m_startSystemMoveResizeInfo.window = XCB_NONE;

    const xcb_atom_t moveResize = connection()->atom(QXcbAtom::_NET_WM_MOVERESIZE);
    xcb_client_message_event_t xev;
    xev.response_type = XCB_CLIENT_MESSAGE;
    xev.type = moveResize;
    xev.sequence = 0;
    xev.window = window;
    xev.format = 32;
    xev.data.data32[0] = 0;
    xev.data.data32[1] = 0;
    xev.data.data32[2] = 11; // _NET_WM_MOVERESIZE_CANCEL
    xev.data.data32[3] = 0;
    xev.data.data32[4] = 0;
    xcb_send_event(xcb_connection(), false, primaryScreen()->root(),
                   XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
                   (const char *)&xev);

    m_duringSystemMoveResize = false;
}

bool QXcbConnection::isDuringSystemMoveResize() const
{
    return m_duringSystemMoveResize;
}

void QXcbConnection::setDuringSystemMoveResize(bool during)
{
    m_duringSystemMoveResize = during;
}

bool QXcbConnection::xi2SetMouseGrabEnabled(xcb_window_t w, bool grab)
{
    bool ok = false;

    if (grab) { // grab
        uint8_t mask[8] = {};
        setXcbMask(mask, XCB_INPUT_BUTTON_PRESS);
        setXcbMask(mask, XCB_INPUT_BUTTON_RELEASE);
        setXcbMask(mask, XCB_INPUT_MOTION);
        setXcbMask(mask, XCB_INPUT_ENTER);
        setXcbMask(mask, XCB_INPUT_LEAVE);
        if (isAtLeastXI22()) {
            setXcbMask(mask, XCB_INPUT_TOUCH_BEGIN);
            setXcbMask(mask, XCB_INPUT_TOUCH_UPDATE);
            setXcbMask(mask, XCB_INPUT_TOUCH_END);
        }
#if QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES
        if (isAtLeastXI24()) {
            setXcbMask(mask, XCB_INPUT_GESTURE_PINCH_BEGIN);
            setXcbMask(mask, XCB_INPUT_GESTURE_PINCH_UPDATE);
            setXcbMask(mask, XCB_INPUT_GESTURE_PINCH_END);
            setXcbMask(mask, XCB_INPUT_GESTURE_SWIPE_BEGIN);
            setXcbMask(mask, XCB_INPUT_GESTURE_SWIPE_UPDATE);
            setXcbMask(mask, XCB_INPUT_GESTURE_SWIPE_END);
        }
#endif // QT_CONFIG(gestures) && QT_XCB_HAS_TOUCHPAD_GESTURES

        for (int id : qAsConst(m_xiMasterPointerIds)) {
            xcb_generic_error_t *error = nullptr;
            auto cookie = xcb_input_xi_grab_device(xcb_connection(), w, XCB_CURRENT_TIME, XCB_CURSOR_NONE, id,
                                                   XCB_INPUT_GRAB_MODE_22_ASYNC, XCB_INPUT_GRAB_MODE_22_ASYNC,
                                                   false, 2, reinterpret_cast<uint32_t *>(mask));
            auto *reply = xcb_input_xi_grab_device_reply(xcb_connection(), cookie, &error);
            if (error) {
                qCDebug(lcQpaXInput, "failed to grab events for device %d on window %x"
                                     "(error code %d)", id, w, error->error_code);
                free(error);
            } else {
                // Managed to grab at least one of master pointers, that should be enough
                // to properly dismiss windows that rely on mouse grabbing.
                ok = true;
            }
            free(reply);
        }
    } else { // ungrab
        for (int id : qAsConst(m_xiMasterPointerIds)) {
            auto cookie = xcb_input_xi_ungrab_device_checked(xcb_connection(), XCB_CURRENT_TIME, id);
            xcb_generic_error_t *error = xcb_request_check(xcb_connection(), cookie);
            if (error) {
                qCDebug(lcQpaXInput, "XIUngrabDevice failed - id: %d (error code %d)", id, error->error_code);
                free(error);
            }
        }
        // XIUngrabDevice does not seem to wait for a reply from X server (similar to
        // xcb_ungrab_pointer). Ungrabbing won't fail, unless NoSuchExtension error
        // has occurred due to a programming error somewhere else in the stack. That
        // would mean that things will crash soon anyway.
        ok = true;
    }

    if (ok)
        m_xiGrab = grab;

    return ok;
}

void QXcbConnection::xi2HandleHierarchyEvent(void *event)
{
    auto *xiEvent = reinterpret_cast<xcb_input_hierarchy_event_t *>(event);
    // We only care about hotplugged devices
    if (!(xiEvent->flags & (XCB_INPUT_HIERARCHY_MASK_SLAVE_REMOVED | XCB_INPUT_HIERARCHY_MASK_SLAVE_ADDED)))
        return;

    xi2SetupDevices();
}

#if QT_XCB_HAS_TOUCHPAD_GESTURES
void QXcbConnection::xi2HandleGesturePinchEvent(void *event)
{
    auto *xiEvent = reinterpret_cast<qt_xcb_input_pinch_event_t *>(event);

    if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled())) {
        qCDebug(lcQpaXInputEvents, "XI2 gesture event type %d seq %d fingers %d pos %6.1f, "
                "%6.1f root pos %6.1f, %6.1f delta_angle %6.1f scale %6.1f on window %x",
                xiEvent->event_type, xiEvent->sequence, xiEvent->detail,
                fixed1616ToReal(xiEvent->event_x), fixed1616ToReal(xiEvent->event_y),
                fixed1616ToReal(xiEvent->root_x), fixed1616ToReal(xiEvent->root_y),
                fixed1616ToReal(xiEvent->delta_angle), fixed1616ToReal(xiEvent->scale),
                xiEvent->event);
    }
    QXcbWindow *platformWindow = platformWindowFromId(xiEvent->event);
    if (!platformWindow)
        return;

    setTime(xiEvent->time);

    TouchDeviceData *dev = touchDeviceForId(xiEvent->sourceid);
    Q_ASSERT(dev);

    uint32_t fingerCount = xiEvent->detail;

    switch (xiEvent->event_type) {
    case XCB_INPUT_GESTURE_PINCH_BEGIN:
        // Gestures must be accepted when we are grabbing gesture events. Otherwise the entire
        // sequence will get replayed when the grab ends.
        if (m_xiGrab) {
            xcb_input_xi_allow_events(xcb_connection(), XCB_CURRENT_TIME, xiEvent->deviceid,
                                      XCB_INPUT_EVENT_MODE_ASYNC_DEVICE, 0, xiEvent->event);
        }
        m_lastPinchScale = 1.0;
        QWindowSystemInterface::handleGestureEvent(platformWindow->window(), xiEvent->time,
                                                   dev->qtTouchDevice,
                                                   Qt::BeginNativeGesture,
                                                   platformWindow->lastPointerPosition(),
                                                   platformWindow->lastPointerGlobalPosition(),
                                                   fingerCount);
        break;

    case XCB_INPUT_GESTURE_PINCH_UPDATE: {
        qreal rotationDelta = fixed1616ToReal(xiEvent->delta_angle);
        qreal scale = fixed1616ToReal(xiEvent->scale);
        qreal scaleDelta = scale - m_lastPinchScale;
        m_lastPinchScale = scale;

        QPointF delta = QPointF(fixed1616ToReal(xiEvent->delta_x),
                                fixed1616ToReal(xiEvent->delta_y));

        if (!delta.isNull()) {
            QWindowSystemInterface::handleGestureEventWithValueAndDelta(
                        platformWindow->window(), xiEvent->time, dev->qtTouchDevice,
                        Qt::PanNativeGesture, 0, delta,
                        platformWindow->lastPointerPosition(),
                        platformWindow->lastPointerGlobalPosition(),
                        fingerCount);
        }
        if (rotationDelta != 0) {
            QWindowSystemInterface::handleGestureEventWithRealValue(
                        platformWindow->window(), xiEvent->time, dev->qtTouchDevice,
                        Qt::RotateNativeGesture,
                        rotationDelta,
                        platformWindow->lastPointerPosition(),
                        platformWindow->lastPointerGlobalPosition(),
                        fingerCount);
        }
        if (scaleDelta != 0) {
            QWindowSystemInterface::handleGestureEventWithRealValue(
                        platformWindow->window(), xiEvent->time, dev->qtTouchDevice,
                        Qt::ZoomNativeGesture,
                        scaleDelta,
                        platformWindow->lastPointerPosition(),
                        platformWindow->lastPointerGlobalPosition(),
                        fingerCount);
        }
        break;
    }
    case XCB_INPUT_GESTURE_PINCH_END:
        QWindowSystemInterface::handleGestureEvent(platformWindow->window(), xiEvent->time,
                                                   dev->qtTouchDevice,
                                                   Qt::EndNativeGesture,
                                                   platformWindow->lastPointerPosition(),
                                                   platformWindow->lastPointerGlobalPosition(),
                                                   fingerCount);
        break;
    }
}

void QXcbConnection::xi2HandleGestureSwipeEvent(void *event)
{
    auto *xiEvent = reinterpret_cast<qt_xcb_input_swipe_event_t *>(event);

    if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled())) {
        qCDebug(lcQpaXInputEvents, "XI2 gesture event type %d seq %d detail %d pos %6.1f, %6.1f root pos %6.1f, %6.1f on window %x",
                xiEvent->event_type, xiEvent->sequence, xiEvent->detail,
                fixed1616ToReal(xiEvent->event_x), fixed1616ToReal(xiEvent->event_y),
                fixed1616ToReal(xiEvent->root_x), fixed1616ToReal(xiEvent->root_y),
                xiEvent->event);
    }
    QXcbWindow *platformWindow = platformWindowFromId(xiEvent->event);
    if (!platformWindow)
        return;

    setTime(xiEvent->time);

    TouchDeviceData *dev = touchDeviceForId(xiEvent->sourceid);
    Q_ASSERT(dev);

    uint32_t fingerCount = xiEvent->detail;

    switch (xiEvent->event_type) {
    case XCB_INPUT_GESTURE_SWIPE_BEGIN:
        // Gestures must be accepted when we are grabbing gesture events. Otherwise the entire
        // sequence will get replayed when the grab ends.
        if (m_xiGrab) {
            xcb_input_xi_allow_events(xcb_connection(), XCB_CURRENT_TIME, xiEvent->deviceid,
                                      XCB_INPUT_EVENT_MODE_ASYNC_DEVICE, 0, xiEvent->event);
        }
        QWindowSystemInterface::handleGestureEvent(platformWindow->window(), xiEvent->time,
                                                   dev->qtTouchDevice,
                                                   Qt::BeginNativeGesture,
                                                   platformWindow->lastPointerPosition(),
                                                   platformWindow->lastPointerGlobalPosition(),
                                                   fingerCount);
        break;
    case XCB_INPUT_GESTURE_SWIPE_UPDATE: {
        QPointF delta = QPointF(fixed1616ToReal(xiEvent->delta_x),
                                fixed1616ToReal(xiEvent->delta_y));

        if (xiEvent->delta_x != 0 || xiEvent->delta_y != 0) {
            QWindowSystemInterface::handleGestureEventWithValueAndDelta(
                        platformWindow->window(), xiEvent->time, dev->qtTouchDevice,
                        Qt::PanNativeGesture, 0, delta,
                        platformWindow->lastPointerPosition(),
                        platformWindow->lastPointerGlobalPosition(),
                        fingerCount);
        }
        break;
    }
    case XCB_INPUT_GESTURE_SWIPE_END:
        QWindowSystemInterface::handleGestureEvent(platformWindow->window(), xiEvent->time,
                                                   dev->qtTouchDevice,
                                                   Qt::EndNativeGesture,
                                                   platformWindow->lastPointerPosition(),
                                                   platformWindow->lastPointerGlobalPosition(),
                                                   fingerCount);
        break;
    }
}

#else // QT_XCB_HAS_TOUCHPAD_GESTURES
void QXcbConnection::xi2HandleGesturePinchEvent(void*) {}
void QXcbConnection::xi2HandleGestureSwipeEvent(void*) {}
#endif

void QXcbConnection::xi2HandleDeviceChangedEvent(void *event)
{
    auto *xiEvent = reinterpret_cast<xcb_input_device_changed_event_t *>(event);
    switch (xiEvent->reason) {
    case XCB_INPUT_CHANGE_REASON_DEVICE_CHANGE: {
        auto reply = Q_XCB_REPLY(xcb_input_xi_query_device, xcb_connection(), xiEvent->sourceid);
        if (!reply || reply->num_infos <= 0)
            return;
        auto it = xcb_input_xi_query_device_infos_iterator(reply.get());
        xi2SetupSlavePointerDevice(it.data);
        break;
    }
    case XCB_INPUT_CHANGE_REASON_SLAVE_SWITCH: {
        if (auto *scrollingDevice = scrollingDeviceForId(xiEvent->sourceid))
            xi2UpdateScrollingDevice(scrollingDevice);
        break;
    }
    default:
        qCDebug(lcQpaXInputEvents, "unknown device-changed-event (device %d)", xiEvent->sourceid);
        break;
    }
}

void QXcbConnection::xi2UpdateScrollingDevice(QInputDevice *dev)
{
    QXcbScrollingDevice *scrollDev = qobject_cast<QXcbScrollingDevice *>(dev);
    if (!scrollDev || !scrollDev->capabilities().testFlag(QInputDevice::Capability::Scroll))
        return;
    QXcbScrollingDevicePrivate *scrollingDevice = QXcbScrollingDevice::get(scrollDev);

    auto reply = Q_XCB_REPLY(xcb_input_xi_query_device, xcb_connection(), scrollingDevice->systemId);
    if (!reply || reply->num_infos <= 0) {
        qCDebug(lcQpaXInputDevices, "scrolling device %lld no longer present", scrollingDevice->systemId);
        return;
    }
    QPointF lastScrollPosition;
    if (lcQpaXInputEvents().isDebugEnabled())
        lastScrollPosition = scrollingDevice->lastScrollPosition;

    xcb_input_xi_device_info_t *deviceInfo = xcb_input_xi_query_device_infos_iterator(reply.get()).data;
    auto classes_it = xcb_input_xi_device_info_classes_iterator(deviceInfo);
    for (; classes_it.rem; xcb_input_device_class_next(&classes_it)) {
        xcb_input_device_class_t *classInfo = classes_it.data;
        if (classInfo->type == XCB_INPUT_DEVICE_CLASS_TYPE_VALUATOR) {
            auto *vci = reinterpret_cast<xcb_input_valuator_class_t *>(classInfo);
            const int valuatorAtom = qatom(vci->label);
            if (valuatorAtom == QXcbAtom::RelHorizScroll || valuatorAtom == QXcbAtom::RelHorizWheel)
                scrollingDevice->lastScrollPosition.setX(fixed3232ToReal(vci->value));
            else if (valuatorAtom == QXcbAtom::RelVertScroll || valuatorAtom == QXcbAtom::RelVertWheel)
                scrollingDevice->lastScrollPosition.setY(fixed3232ToReal(vci->value));
        }
    }
    if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled() && lastScrollPosition != scrollingDevice->lastScrollPosition))
        qCDebug(lcQpaXInputEvents, "scrolling device %lld moved from (%f, %f) to (%f, %f)", scrollingDevice->systemId,
                lastScrollPosition.x(), lastScrollPosition.y(),
                scrollingDevice->lastScrollPosition.x(),
                scrollingDevice->lastScrollPosition.y());
}

void QXcbConnection::xi2UpdateScrollingDevices()
{
    const auto &devices = QInputDevice::devices();
    for (const QInputDevice *dev : devices) {
        if (dev->capabilities().testFlag(QInputDevice::Capability::Scroll))
            xi2UpdateScrollingDevice(const_cast<QInputDevice *>(dev));
    }
}

QXcbScrollingDevice *QXcbConnection::scrollingDeviceForId(int id)
{
    const QPointingDevice *dev = QPointingDevicePrivate::pointingDeviceById(id);
    if (!dev|| !dev->capabilities().testFlag(QInputDevice::Capability::Scroll))
        return nullptr;
    return qobject_cast<QXcbScrollingDevice *>(const_cast<QPointingDevice *>(dev));
}

void QXcbConnection::xi2HandleScrollEvent(void *event, const QPointingDevice *dev)
{
    auto *xiDeviceEvent = reinterpret_cast<qt_xcb_input_device_event_t *>(event);

    const QXcbScrollingDevice *scrollDev = qobject_cast<const QXcbScrollingDevice *>(dev);
    if (!scrollDev || !scrollDev->capabilities().testFlag(QInputDevice::Capability::Scroll))
        return;
    const QXcbScrollingDevicePrivate *scrollingDevice = QXcbScrollingDevice::get(scrollDev);

    if (xiDeviceEvent->event_type == XCB_INPUT_MOTION && scrollingDevice->orientations) {
        if (QXcbWindow *platformWindow = platformWindowFromId(xiDeviceEvent->event)) {
            QPoint rawDelta;
            QPoint angleDelta;
            double value;
            if (scrollingDevice->orientations & Qt::Vertical) {
                if (xi2GetValuatorValueIfSet(xiDeviceEvent, scrollingDevice->verticalIndex, &value)) {
                    double delta = scrollingDevice->lastScrollPosition.y() - value;
                    scrollingDevice->lastScrollPosition.setY(value);
                    angleDelta.setY((delta / scrollingDevice->verticalIncrement) * 120);
                    // With most drivers the increment is 1 for wheels.
                    // For libinput it is hardcoded to a useless 15.
                    // For a proper touchpad driver it should be in the same order of magnitude as 120
                    if (scrollingDevice->verticalIncrement > 15)
                        rawDelta.setY(delta);
                    else if (scrollingDevice->verticalIncrement < -15)
                        rawDelta.setY(-delta);
                }
            }
            if (scrollingDevice->orientations & Qt::Horizontal) {
                if (xi2GetValuatorValueIfSet(xiDeviceEvent, scrollingDevice->horizontalIndex, &value)) {
                    double delta = scrollingDevice->lastScrollPosition.x() - value;
                    scrollingDevice->lastScrollPosition.setX(value);
                    angleDelta.setX((delta / scrollingDevice->horizontalIncrement) * 120);
                    // See comment under vertical
                    if (scrollingDevice->horizontalIncrement > 15)
                        rawDelta.setX(delta);
                    else if (scrollingDevice->horizontalIncrement < -15)
                        rawDelta.setX(-delta);
                }
            }
            if (!angleDelta.isNull()) {
                QPoint local(fixed1616ToReal(xiDeviceEvent->event_x), fixed1616ToReal(xiDeviceEvent->event_y));
                QPoint global(fixed1616ToReal(xiDeviceEvent->root_x), fixed1616ToReal(xiDeviceEvent->root_y));
                Qt::KeyboardModifiers modifiers = keyboard()->translateModifiers(xiDeviceEvent->mods.effective);
                if (modifiers & Qt::AltModifier) {
                    angleDelta = angleDelta.transposed();
                    rawDelta = rawDelta.transposed();
                }
                qCDebug(lcQpaXInputEvents) << "scroll wheel from device" << scrollingDevice->systemId
                                           << "@ window pos" << local << "delta px" << rawDelta << "angle" << angleDelta;
                QWindowSystemInterface::handleWheelEvent(platformWindow->window(), xiDeviceEvent->time, dev,
                                                         local, global, rawDelta, angleDelta, modifiers);
            }
        }
    } else if (xiDeviceEvent->event_type == XCB_INPUT_BUTTON_RELEASE && scrollingDevice->legacyOrientations) {
        if (QXcbWindow *platformWindow = platformWindowFromId(xiDeviceEvent->event)) {
            QPoint angleDelta;
            if (scrollingDevice->legacyOrientations & Qt::Vertical) {
                if (xiDeviceEvent->detail == 4)
                    angleDelta.setY(120);
                else if (xiDeviceEvent->detail == 5)
                    angleDelta.setY(-120);
            }
            if (scrollingDevice->legacyOrientations & Qt::Horizontal) {
                if (xiDeviceEvent->detail == 6)
                    angleDelta.setX(120);
                else if (xiDeviceEvent->detail == 7)
                    angleDelta.setX(-120);
            }
            if (!angleDelta.isNull()) {
                QPoint local(fixed1616ToReal(xiDeviceEvent->event_x), fixed1616ToReal(xiDeviceEvent->event_y));
                QPoint global(fixed1616ToReal(xiDeviceEvent->root_x), fixed1616ToReal(xiDeviceEvent->root_y));
                Qt::KeyboardModifiers modifiers = keyboard()->translateModifiers(xiDeviceEvent->mods.effective);
                if (modifiers & Qt::AltModifier)
                    angleDelta = angleDelta.transposed();
                qCDebug(lcQpaXInputEvents) << "scroll wheel (button" << xiDeviceEvent->detail << ") @ window pos" << local << "delta angle" << angleDelta;
                QWindowSystemInterface::handleWheelEvent(platformWindow->window(), xiDeviceEvent->time, dev,
                                                         local, global, QPoint(), angleDelta, modifiers);
            }
        }
    }
}

static int xi2ValuatorOffset(const unsigned char *maskPtr, int maskLen, int number)
{
    int offset = 0;
    for (int i = 0; i < maskLen; i++) {
        if (number < 8) {
            if ((maskPtr[i] & (1 << number)) == 0)
                return -1;
        }
        for (int j = 0; j < 8; j++) {
            if (j == number)
                return offset;
            if (maskPtr[i] & (1 << j))
                offset++;
        }
        number -= 8;
    }
    return -1;
}

bool QXcbConnection::xi2GetValuatorValueIfSet(const void *event, int valuatorNum, double *value)
{
    auto *xideviceevent = static_cast<const qt_xcb_input_device_event_t *>(event);
    auto *buttonsMaskAddr = reinterpret_cast<const unsigned char *>(&xideviceevent[1]);
    auto *valuatorsMaskAddr = buttonsMaskAddr + xideviceevent->buttons_len * 4;
    auto *valuatorsValuesAddr = reinterpret_cast<const xcb_input_fp3232_t *>(valuatorsMaskAddr + xideviceevent->valuators_len * 4);

    int valuatorOffset = xi2ValuatorOffset(valuatorsMaskAddr, xideviceevent->valuators_len, valuatorNum);
    if (valuatorOffset < 0)
        return false;

    *value = valuatorsValuesAddr[valuatorOffset].integral;
    *value += ((double)valuatorsValuesAddr[valuatorOffset].frac / (1 << 16) / (1 << 16));
    return true;
}

Qt::MouseButton QXcbConnection::xiToQtMouseButton(uint32_t b)
{
    switch (b) {
    case 1: return Qt::LeftButton;
    case 2: return Qt::MiddleButton;
    case 3: return Qt::RightButton;
    // 4-7 are for scrolling
    default: break;
    }
    if (b >= 8 && b <= Qt::MaxMouseButton)
        return static_cast<Qt::MouseButton>(Qt::BackButton << (b - 8));
    return Qt::NoButton;
}

#if QT_CONFIG(tabletevent)
bool QXcbConnection::xi2HandleTabletEvent(const void *event, TabletData *tabletData)
{
    bool handled = true;
    const auto *xiDeviceEvent = reinterpret_cast<const qt_xcb_input_device_event_t *>(event);

    switch (xiDeviceEvent->event_type) {
    case XCB_INPUT_BUTTON_PRESS: {
        Qt::MouseButton b = xiToQtMouseButton(xiDeviceEvent->detail);
        tabletData->buttons |= b;
        xi2ReportTabletEvent(event, tabletData);
        break;
    }
    case XCB_INPUT_BUTTON_RELEASE: {
        Qt::MouseButton b = xiToQtMouseButton(xiDeviceEvent->detail);
        tabletData->buttons ^= b;
        xi2ReportTabletEvent(event, tabletData);
        break;
    }
    case XCB_INPUT_MOTION:
        xi2ReportTabletEvent(event, tabletData);
        break;
    case XCB_INPUT_PROPERTY: {
        // This is the wacom driver's way of reporting tool proximity.
        // The evdev driver doesn't do it this way.
        const auto *ev = reinterpret_cast<const xcb_input_property_event_t *>(event);
        if (ev->what == XCB_INPUT_PROPERTY_FLAG_MODIFIED) {
            if (ev->property == atom(QXcbAtom::WacomSerialIDs)) {
                enum WacomSerialIndex {
                    _WACSER_USB_ID = 0,
                    _WACSER_LAST_TOOL_SERIAL,
                    _WACSER_LAST_TOOL_ID,
                    _WACSER_TOOL_SERIAL,
                    _WACSER_TOOL_ID,
                    _WACSER_COUNT
                };

                auto reply = Q_XCB_REPLY(xcb_input_xi_get_property, xcb_connection(), tabletData->deviceId, 0,
                                         ev->property, XCB_GET_PROPERTY_TYPE_ANY, 0, 100);
                if (reply) {
                    if (reply->type == atom(QXcbAtom::INTEGER) && reply->format == 32 && reply->num_items == _WACSER_COUNT) {
                        quint32 *ptr = reinterpret_cast<quint32 *>(xcb_input_xi_get_property_items(reply.get()));
                        quint32 tool = ptr[_WACSER_TOOL_ID];
                        // Workaround for http://sourceforge.net/p/linuxwacom/bugs/246/
                        // e.g. on Thinkpad Helix, tool ID will be 0 and serial will be 1
                        if (!tool && ptr[_WACSER_TOOL_SERIAL])
                            tool = ptr[_WACSER_TOOL_SERIAL];

                        // The property change event informs us which tool is in proximity or which one left proximity.
                        if (tool) {
                            const QPointingDevice *dev = tabletToolInstance(nullptr, tabletData->name,
                                    tabletData->deviceId, ptr[_WACSER_USB_ID], tool,
                                    qint64(ptr[_WACSER_TOOL_SERIAL])); // TODO look up the master
                            tabletData->inProximity = true;
                            tabletData->tool = dev->type();
                            tabletData->serialId = qint64(ptr[_WACSER_TOOL_SERIAL]);
                            QWindowSystemInterface::handleTabletEnterProximityEvent(ev->time,
                                int(tabletData->tool), int(tabletData->pointerType), tabletData->serialId);
                        } else {
                            tool = ptr[_WACSER_LAST_TOOL_ID];
                            // Workaround for http://sourceforge.net/p/linuxwacom/bugs/246/
                            // e.g. on Thinkpad Helix, tool ID will be 0 and serial will be 1
                            if (!tool)
                                tool = ptr[_WACSER_LAST_TOOL_SERIAL];
                            const QInputDevice *dev = QInputDevicePrivate::fromId(tabletData->deviceId);
                            Q_ASSERT(dev);
                            tabletData->tool = dev->type();
                            tabletData->inProximity = false;
                            tabletData->serialId = qint64(ptr[_WACSER_LAST_TOOL_SERIAL]);
                            // TODO why doesn't it just take QPointingDevice*
                            QWindowSystemInterface::handleTabletLeaveProximityEvent(ev->time,
                                int(tabletData->tool), int(tabletData->pointerType), tabletData->serialId);
                        }
                        // TODO maybe have a hash of tabletData->deviceId to device data so we can
                        // look up the tablet name here, and distinguish multiple tablets
                        if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled()))
                            qCDebug(lcQpaXInputDevices, "XI2 proximity change on tablet %d %s (USB %x): last tool: %x id %x current tool: %x id %x %s",
                                    tabletData->deviceId, qPrintable(tabletData->name), ptr[_WACSER_USB_ID],
                                    ptr[_WACSER_LAST_TOOL_SERIAL], ptr[_WACSER_LAST_TOOL_ID],
                                    ptr[_WACSER_TOOL_SERIAL], ptr[_WACSER_TOOL_ID], toolName(tabletData->tool));
                    }
                }
            }
        }
        break;
    }
    default:
        handled = false;
        break;
    }

    return handled;
}

inline qreal scaleOneValuator(qreal normValue, qreal screenMin, qreal screenSize)
{
    return screenMin + normValue * screenSize;
}

// TODO QPointingDevice not TabletData
void QXcbConnection::xi2ReportTabletEvent(const void *event, TabletData *tabletData)
{
    auto *ev = reinterpret_cast<const qt_xcb_input_device_event_t *>(event);
    QXcbWindow *xcbWindow = platformWindowFromId(ev->event);
    if (!xcbWindow)
        return;
    QWindow *window = xcbWindow->window();
    const Qt::KeyboardModifiers modifiers = keyboard()->translateModifiers(ev->mods.effective);
    QPointF local(fixed1616ToReal(ev->event_x), fixed1616ToReal(ev->event_y));
    QPointF global(fixed1616ToReal(ev->root_x), fixed1616ToReal(ev->root_y));
    double pressure = 0, rotation = 0, tangentialPressure = 0;
    int xTilt = 0, yTilt = 0;
    static const bool useValuators = !qEnvironmentVariableIsSet("QT_XCB_TABLET_LEGACY_COORDINATES");

    // Valuators' values are relative to the physical size of the current virtual
    // screen. Therefore we cannot use QScreen/QWindow geometry and should use
    // QPlatformWindow/QPlatformScreen instead.
    QRect physicalScreenArea;
    if (Q_LIKELY(useValuators)) {
        const QList<QPlatformScreen *> siblings = window->screen()->handle()->virtualSiblings();
        for (const QPlatformScreen *screen : siblings)
            physicalScreenArea |= screen->geometry();
    }

    for (QHash<int, TabletData::ValuatorClassInfo>::iterator it = tabletData->valuatorInfo.begin(),
            ite = tabletData->valuatorInfo.end(); it != ite; ++it) {
        int valuator = it.key();
        TabletData::ValuatorClassInfo &classInfo(it.value());
        xi2GetValuatorValueIfSet(event, classInfo.number, &classInfo.curVal);
        double normalizedValue = (classInfo.curVal - classInfo.minVal) / (classInfo.maxVal - classInfo.minVal);
        switch (valuator) {
        case QXcbAtom::AbsX:
            if (Q_LIKELY(useValuators)) {
                const qreal value = scaleOneValuator(normalizedValue, physicalScreenArea.x(), physicalScreenArea.width());
                global.setX(value);
                local.setX(xcbWindow->mapFromGlobalF(global).x());
            }
            break;
        case QXcbAtom::AbsY:
            if (Q_LIKELY(useValuators)) {
                qreal value = scaleOneValuator(normalizedValue, physicalScreenArea.y(), physicalScreenArea.height());
                global.setY(value);
                local.setY(xcbWindow->mapFromGlobalF(global).y());
            }
            break;
        case QXcbAtom::AbsPressure:
            pressure = normalizedValue;
            break;
        case QXcbAtom::AbsTiltX:
            xTilt = classInfo.curVal;
            break;
        case QXcbAtom::AbsTiltY:
            yTilt = classInfo.curVal;
            break;
        case QXcbAtom::AbsWheel:
            switch (tabletData->tool) {
            case QInputDevice::DeviceType::Airbrush:
                tangentialPressure = normalizedValue * 2.0 - 1.0; // Convert 0..1 range to -1..+1 range
                break;
            case QInputDevice::DeviceType::Stylus:
                rotation = normalizedValue * 360.0 - 180.0; // Convert 0..1 range to -180..+180 degrees
                break;
            default:    // Other types of styli do not use this valuator
                break;
            }
            break;
        default:
            break;
        }
    }

    if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled()))
        qCDebug(lcQpaXInputEvents, "XI2 event on tablet %d with tool %s %llx type %s seq %d detail %d time %d "
            "pos %6.1f, %6.1f root pos %6.1f, %6.1f buttons 0x%x pressure %4.2lf tilt %d, %d rotation %6.2lf modifiers 0x%x",
            tabletData->deviceId, toolName(tabletData->tool), tabletData->serialId, pointerTypeName(tabletData->pointerType),
            ev->sequence, ev->detail, ev->time,
            local.x(), local.y(), global.x(), global.y(),
            (int)tabletData->buttons, pressure, xTilt, yTilt, rotation, (int)modifiers);

    QWindowSystemInterface::handleTabletEvent(window, ev->time, local, global,
                                              int(tabletData->tool), int(tabletData->pointerType),
                                              tabletData->buttons, pressure,
                                              xTilt, yTilt, tangentialPressure,
                                              rotation, 0, tabletData->serialId, modifiers);
}

QXcbConnection::TabletData *QXcbConnection::tabletDataForDevice(int id)
{
    for (int i = 0; i < m_tabletData.count(); ++i) {
        if (m_tabletData.at(i).deviceId == id)
            return &m_tabletData[i];
    }
    return nullptr;
}

#endif // QT_CONFIG(tabletevent)