Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwaylandinputdevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2024 Jie Liu <liujie01@kylinos.cn>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
7
13#if QT_CONFIG(wayland_datadevice)
14#include "qwaylanddatadevice_p.h"
15#include "qwaylanddatadevicemanager_p.h"
16#endif
17#if QT_CONFIG(clipboard)
18#include "qwaylanddatacontrolv1_p.h"
19#endif
20#if QT_CONFIG(wayland_client_primary_selection)
21#include "qwaylandprimaryselectionv1_p.h"
22#endif
23#if QT_CONFIG(tabletevent)
24#include "qwaylandtabletv2_p.h"
25#endif
38
39#include <QtGui/private/qpixmap_raster_p.h>
40#include <QtGui/private/qguiapplication_p.h>
41#include <qpa/qplatformwindow.h>
42#include <qpa/qplatforminputcontext.h>
43#include <qpa/qplatformtheme.h>
44#include <QDebug>
45
46#include <unistd.h>
47#include <fcntl.h>
48#include <sys/mman.h>
49
50#if QT_CONFIG(cursor)
51#include <wayland-cursor.h>
52#endif
53
54#include <QtGui/QGuiApplication>
55#include <QtGui/QPointingDevice>
56
57QT_BEGIN_NAMESPACE
58
59namespace QtWaylandClient {
60
61Q_LOGGING_CATEGORY(lcQpaWaylandInput, "qt.qpa.wayland.input");
62
63// The maximum number of concurrent touchpoints is not exposed in wayland, so we assume a
64// reasonable number of them. As of 2021 most touchscreen panels support 10 concurrent touchpoints.
65static const int MaxTouchPoints = 10;
66
68{
69 timeElapsed.start();
70 delayTimer.setSingleShot(true);
71}
72
74{
75 using namespace std::chrono_literals;
76
77 if (!QCoreApplication::testAttribute(Qt::AA_CompressHighFrequencyEvents))
78 return false;
79
80 const auto elapsed = timeElapsed.durationElapsed();
81 timeElapsed.start();
82 if (elapsed < 100us || delayTimer.isActive())
83 {
84 // The highest USB HID polling rate is 8 kHz (125 μs). Most mice use lowe polling rate [125 Hz - 1000 Hz].
85 // Reject all events faster than 100 μs, because it definitely means the application main thread is
86 // freezed by long operation and events are delivered one after another from the queue. Since now we rely
87 // on the 0 ms timer to deliver the last pending event when application main thread is no longer freezed.
88 delayTimer.start(0);
89 return true;
90 }
91
92 return false;
93}
94
96 : mParent(p)
97{
99 mRepeatTimer.callOnTimeout(this, [&]() {
100 if (!focusSurface()) {
101 // We destroyed the keyboard focus surface, but the server didn't get the message yet...
102 // or the server didn't send an enter event first.
103 return;
104 }
109 mRepeatKey.text, true);
112 mRepeatKey.text, true);
113 });
114}
115
116#if QT_CONFIG(xkbcommon)
118{
120 if (!ctx)
121 return false;
122
123 struct xkb_rule_names names;
124 names.rules = "evdev";
125 names.model = "pc105";
126 names.layout = "us";
127 names.variant = "";
128 names.options = "";
129
131 if (mXkbKeymap)
133
134 if (!mXkbKeymap || !mXkbState) {
135 qCWarning(lcQpaWayland, "failed to create default keymap");
136 return false;
137 }
138
139 return true;
140}
141#endif
142
152
157
175
177{
178 if (version() >= 3)
180 else
182}
183
188
189#if QT_CONFIG(cursor)
190
192{
193 if (seat()->mQDisplay->compositor()->version() < 3) {
194 return 1;
195 }
196
197 if (auto *s = mCursor.surface.get()) {
198 if (s->outputScale() > 0)
199 return s->outputScale();
200 }
201
203}
204
206{
209
213 }
214
216 cursorThemeName = QStringLiteral("default");
217 if (cursorSize.isEmpty())
218 cursorSize = QSize(24, 24);
219
220 int scale = idealCursorScale();
222 auto *display = seat()->mQDisplay;
224
225 if (!mCursor.theme)
226 return; // A warning has already been printed in loadCursorTheme
227
228 if (auto *arrow = mCursor.theme->cursor(Qt::ArrowCursor)) {
229 int arrowPixelSize = qMax(arrow->images[0]->width, arrow->images[0]->height); // Not all cursor themes are square
230 while (scale > 1 && arrowPixelSize / scale < cursorSize.width())
231 --scale;
232 } else {
233 qCWarning(lcQpaWayland) << "Cursor theme does not support the arrow cursor";
234 }
236}
237
239{
240 if (mEnterSerial == 0)
241 return;
242
243 auto shape = seat()->mCursor.shape;
244
245 if (shape == Qt::BlankCursor) {
246 if (mCursor.surface)
248 set_cursor(mEnterSerial, nullptr, 0, 0);
249 return;
250 }
251
252 if (shape == Qt::BitmapCursor) {
254 if (!buffer) {
255 qCWarning(lcQpaWayland) << "No buffer for bitmap cursor, can't set cursor";
256 return;
257 }
258 auto hotspot = seat()->mCursor.hotspot;
261 return;
262 }
263
264 if (mCursor.shape) {
265 if (mCursor.surface) {
267 }
269 return;
270 }
271
274
275 if (!mCursor.theme)
276 return;
277
278 // Set from shape using theme
280 const uint time = timer.isValid() ? timer.elapsed() : 0;
281
282 if (struct ::wl_cursor *waylandCursor = mCursor.theme->cursor(shape)) {
283 uint duration = 0;
286
288 if (!buffer) {
289 qCWarning(lcQpaWayland) << "Could not find buffer for cursor" << shape;
290 return;
291 }
295 bool animated = duration > 0;
296 if (animated) {
300 }
302 return;
303 }
304
305 qCWarning(lcQpaWayland) << "Unable to change to cursor" << shape;
306}
307
310{
311 if (!mCursor.surface)
313 return mCursor.surface.get();
314}
315
317{
320 updateCursor();
321 }
322}
323
325{
328 updateCursor();
329 }
330}
331
332#endif // QT_CONFIG(cursor)
333
339
341{
342 if (version() >= 3)
344 else
346}
347
352 , mId(id)
353{
354
355#if QT_CONFIG(clipboard)
358 }
359#endif
360
361#if QT_CONFIG(wayland_datadevice)
364 }
365#endif
366
367#if QT_CONFIG(wayland_client_primary_selection)
368 // TODO: Could probably decouple this more if there was a signal for new seat added
371#endif
372
377 }
378
381
384
387
388#if QT_CONFIG(tabletevent)
389 if (auto *tm = mQDisplay->tabletManager())
391#endif
392}
393
401
403{
404 mCaps = caps;
405
408 } else if (!(mCaps & WL_SEAT_CAPABILITY_KEYBOARD) && mKeyboard) {
410 }
411
414
416 if (pointerGestures) {
420 // NOTE: The name of the device and its system ID are not exposed on Wayland.
430 }
431 } else if (!(mCaps & WL_SEAT_CAPABILITY_POINTER) && mPointer) {
432 mPointer.reset();
435 }
436
438 mTouch.reset(createTouch(this));
439
440 if (!mTouchDevice) {
441 // TODO number of touchpoints, actual name and ID
447 }
448 } else if (!(mCaps & WL_SEAT_CAPABILITY_TOUCH) && mTouch) {
449 mTouch.reset();
450 }
451}
452
457
462
467
472
477
482
487
492
494{
495 return mTouch.get();
496}
497
505
511
512#if QT_CONFIG(wayland_datadevice)
514{
516}
517
519{
520 return mDataDevice;
521}
522#endif
523
524#if QT_CONFIG(clipboard)
526{
528}
529
531{
532 return mDataControlDevice.get();
533}
534#endif
535
536#if QT_CONFIG(wayland_client_primary_selection)
538{
540}
541
543{
545}
546#endif
547
552
553#if QT_CONFIG(tabletevent)
555{
557}
558
560{
561 return mTabletSeat.get();
562}
563#endif
564
569
574
579
585
587{
588 //TODO GUARD!
589 return mPointer && mPointer->focusSurface() ? mPointer->focusSurface()->waylandWindow() : nullptr;
590}
591
593{
594 //GUARD!
595
597}
598
600{
601 //GUARD!
602
603 return mTouch && mTouch->mFocus ? mTouch->mFocus->waylandWindow() : nullptr;
604}
605
610
612{
613#if QT_CONFIG(xkbcommon)
616#else
618#endif
619 return {};
620}
621
623{
624 if (!mKeyboard)
625 return Qt::NoModifier;
626
627 return mKeyboard->modifiers();
628}
629
631{
633
634#if QT_CONFIG(xkbcommon)
635 if (!mXkbState)
636 return ret;
637
639#endif
640
641 return ret;
642}
643
644#if QT_CONFIG(cursor)
646{
648 mCursor = CursorState(); // Clear any previous state
653
654 if (mCursor.shape == Qt::BitmapCursor) {
657 mCursor.bitmapScale = int(dpr); // Wayland doesn't support fractional buffer scale
658 // If there was a fractional part of the dpr, we need to scale the hotspot accordingly
659 if (mCursor.bitmapScale < dpr)
661 }
662
663 // Return early if setCursor was called redundantly (mostly happens from decorations)
668 return;
669 }
670
671 if (mPointer)
673
674#if QT_CONFIG(tabletevent)
675 if (mTabletSeat)
677#endif
678}
679#endif
680
682{
683public:
684 EnterEvent(QWaylandSurface *surface, const QPointF &local, const QPointF &global)
687 {}
688};
689
692{
693 if (!wlSurface)
694 return;
695
697
698 if (!surface)
699 return; // Ignore foreign surfaces
700
701 if (mFocus) {
702 qCWarning(lcQpaWayland) << "The compositor sent a wl_pointer.enter event before sending a"
703 << "leave event first, this is not allowed by the wayland protocol"
704 << "attempting to work around it by invalidating the current focus";
706 }
707 mFocus = surface;
709
711 if (auto *ww = surface->waylandWindow())
713 else
714 return;
715
718
721 }
722
723#if QT_CONFIG(cursor)
724 // Depends on mEnterSerial being updated
725 updateCursor();
726#endif
727
729 if (!grab)
731}
732
734{
735public:
736 LeaveEvent(QWaylandSurface *surface, const QPointF &localPos, const QPointF &globalPos)
739 {}
740};
741
743{
745
748
749 // The event may arrive after destroying the window, indicated by
750 // a null surface.
751 if (!wlSurface)
752 return;
753
755 if (!surface)
756 return; // Ignore foreign surfaces
757
760}
761
763{
764public:
765 MotionEvent(QWaylandSurface *surface, ulong timestamp, const QPointF &localPos,
766 const QPointF &globalPos, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
769 {
770 }
771};
772
774{
776 if (!surface) {
777 // We destroyed the pointer focus surface, but the server didn't get the message yet...
778 // or the server didn't send an enter event first. In either case, ignore the event.
779 return;
780 }
781
784 if (auto *ww = surface->waylandWindow())
786 else
787 return;
788
791 mParent->mTime = time;
792
794 if (grab && grab->waylandSurface() != surface) {
795 // We can't know the true position since we're getting events for another surface,
796 // so we just set it outside of the window boundaries.
797 pos = QPointF(-1, -1);
800 }
802}
803
805{
806public:
807 PressEvent(QWaylandSurface *surface, ulong timestamp, const QPointF &localPos,
808 const QPointF &globalPos, Qt::MouseButtons buttons, Qt::MouseButton button,
809 Qt::KeyboardModifiers modifiers)
812 {
813 }
814};
815
817{
818public:
819 ReleaseEvent(QWaylandSurface *surface, ulong timestamp, const QPointF &localPos,
820 const QPointF &globalPos, Qt::MouseButtons buttons, Qt::MouseButton button,
821 Qt::KeyboardModifiers modifiers)
824 {
825 }
826};
827
830{
832 if (!surface) {
833 // We destroyed the pointer focus surface, but the server didn't get the message yet...
834 // or the server didn't send an enter event first. In either case, ignore the event.
835 return;
836 }
837
839
840 // translate from kernel (input.h) 'button' to corresponding Qt:MouseButton.
841 // The range of mouse values is 0x110 <= mouse_button < 0x120, the first Joystick button.
842 switch (button) {
843 case 0x110: qt_button = Qt::LeftButton; break; // kernel BTN_LEFT
844 case 0x111: qt_button = Qt::RightButton; break;
845 case 0x112: qt_button = Qt::MiddleButton; break;
846 case 0x113: qt_button = Qt::ExtraButton1; break; // AKA Qt::BackButton
847 case 0x114: qt_button = Qt::ExtraButton2; break; // AKA Qt::ForwardButton
848 case 0x115: qt_button = Qt::ExtraButton3; break; // AKA Qt::TaskButton
849 case 0x116: qt_button = Qt::ExtraButton4; break;
850 case 0x117: qt_button = Qt::ExtraButton5; break;
851 case 0x118: qt_button = Qt::ExtraButton6; break;
852 case 0x119: qt_button = Qt::ExtraButton7; break;
853 case 0x11a: qt_button = Qt::ExtraButton8; break;
854 case 0x11b: qt_button = Qt::ExtraButton9; break;
855 case 0x11c: qt_button = Qt::ExtraButton10; break;
856 case 0x11d: qt_button = Qt::ExtraButton11; break;
857 case 0x11e: qt_button = Qt::ExtraButton12; break;
858 case 0x11f: qt_button = Qt::ExtraButton13; break;
859 default: return; // invalid button number (as far as Qt is concerned)
860 }
861
863
864 if (state)
866 else
868
869 mParent->mTime = time;
871 if (state)
873
875
878 if (grab && grab->waylandSurface() != focusSurface()) {
879 pos = QPointF(-1, -1);
881
883 }
884
885 if (state) {
886 if (auto *window = surface->waylandWindow())
889 }
890 else
892}
893
895{
896 if (mFocus) {
898 mFocus = nullptr;
899 }
900 mEnterSerial = 0;
901}
902
908
910{
911 if (auto *surface = focusSurface()) {
913 if (auto *ww = surface->waylandWindow())
915 }
916}
917
919{
920public:
921 WheelEvent(QWaylandSurface *surface, Qt::ScrollPhase phase, ulong timestamp, const QPointF &local,
922 const QPointF &global, const QPoint &pixelDelta, const QPoint &angleDelta,
923 Qt::MouseEventSource source, Qt::KeyboardModifiers modifiers, bool inverted)
928 {
929 }
930};
931
933{
934 if (!focusSurface()) {
935 // We destroyed the pointer focus surface, but the server didn't get the message yet...
936 // or the server didn't send an enter event first. In either case, ignore the event.
937 return;
938 }
939
940 // Get the delta and convert it into the expected range
941 switch (axis) {
944 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis vertical:" << mFrameData.delta.y();
945 break;
948 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis horizontal:" << mFrameData.delta.x();
949 break;
950 default:
951 //TODO: is this really needed?
952 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis: Unknown axis:" << axis;
953 return;
954 }
955
956 mParent->mTime = time;
957
959}
960
962{
964 qCDebug(lcQpaWaylandInput) << "compressed pointer_frame event";
965 return;
966 }
967
969}
970
972{
973 switch (source) {
975 qCDebug(lcQpaWaylandInput) << "Axis source wheel";
976 break;
978 qCDebug(lcQpaWaylandInput) << "Axis source finger";
979 break;
981 qCDebug(lcQpaWaylandInput) << "Axis source continuous";
982 break;
984 qCDebug(lcQpaWaylandInput) << "Axis source wheel tilt";
985 }
987}
988
990{
991 if (!focusSurface())
992 return;
993
994 mParent->mTime = time;
995 switch (axis) {
997 qCDebug(lcQpaWaylandInput) << "Received vertical wl_pointer.axis_stop";
998 break;
1000 qCDebug(lcQpaWaylandInput) << "Received horizontal wl_pointer.axis_stop";
1001 break;
1002 default:
1003 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_stop: Unknown axis: " << axis
1004 << "This is most likely a compositor bug";
1005 return;
1006 }
1007
1008 mScrollEnd = true;
1009}
1010
1012{
1013 if (!focusSurface())
1014 return;
1015
1016 const int32_t delta120 = value * 15 * 8;
1017
1018 switch (axis) {
1020 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_discrete vertical:" << value;
1022 break;
1024 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_discrete horizontal:" << value;
1026 break;
1027 default:
1028 //TODO: is this really needed?
1029 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_discrete: Unknown axis:" << axis;
1030 return;
1031 }
1032}
1033
1035{
1036 if (!focusSurface())
1037 return;
1038
1039 switch (axis) {
1041 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_value120 vertical:" << value;
1043 break;
1045 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_value120 horizontal:" << value;
1047 break;
1048 default:
1049 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_value120: Unknown axis:" << axis;
1050 return;
1051 }
1052}
1053
1055{
1057 switch (axis) {
1060 break;
1063 break;
1064 default:
1065 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_relative_direction: Unknown axis:" << axis;
1066 }
1067}
1068
1070{
1072 qCDebug(lcQpaWaylandInput) << "Flushing new event; no frame event in this version";
1073 pointer_frame();
1074 }
1075}
1076
1078{
1079 qCDebug(lcQpaWaylandInput) << "Setting frame event " << event->type;
1081 qCDebug(lcQpaWaylandInput) << "Flushing; previous was " << mFrameData.event->type;
1083 }
1084
1086
1088}
1089
1098
1100{
1101 switch (axisSource) {
1102 case axis_source_wheel_tilt: // sideways tilt of the wheel
1103 case axis_source_wheel:
1104 // In the case of wheel events, a pixel delta doesn't really make sense,
1105 // and will make Qt think this is a continuous scroll event when it isn't,
1106 // so just ignore it.
1107 return false;
1108 case axis_source_finger:
1110 return !delta.isNull();
1111 default:
1112 return false;
1113 }
1114}
1115
1117{
1118 if (!hasPixelDelta())
1119 return QPoint();
1120
1122 // Add accumulated rounding error before rounding again
1125 Q_ASSERT(qAbs(accumulatedError->x()) < 1.0);
1126 Q_ASSERT(qAbs(accumulatedError->y()) < 1.0);
1127
1128 // for continuous scroll events things should be
1129 // in the same direction
1130 // i.e converted so downwards surface co-ordinates (positive axis_value)
1131 // goes to downwards in wheel event (negative value)
1132 pixelDelta *= -1;
1133 return pixelDelta;
1134}
1135
1137{
1138 if (delta120.isNull()) {
1139 // If we didn't get any discrete events, then we need to fall back to
1140 // the continuous information.
1141 return (delta * -12).toPoint(); //TODO: why multiply by 12?
1142 }
1143
1144 // The angle delta is in eights of degrees, and our docs says most mice have
1145 // 1 click = 15 degrees, i.e. 120 is one click. It's also in the opposite
1146 // direction of surface space.
1147 return -delta120;
1148}
1149
1151{
1152 switch (axisSource) {
1153 case axis_source_wheel_tilt: // sideways tilt of the wheel
1154 case axis_source_wheel:
1156 case axis_source_finger:
1158 default: // Whatever other sources might be added are probably not mouse wheels
1160 }
1161}
1162
1164{
1166
1167 // The wayland protocol has separate horizontal and vertical axes, Qt has just the one inverted flag
1168 // Pragmatically it should't come up
1170
1171 // Angle delta is required for Qt wheel events, so don't try to send events if it's zero
1172 if (!angleDelta.isNull()) {
1174 if (!mScrollBeginSent) {
1175 if (!target && QWaylandWindow::mouseGrab())
1177 if (!target)
1178 target = focusSurface();
1179 }
1180 if (!target) {
1181 qCDebug(lcQpaWaylandInput) << "Flushing scroll event aborted - no scroll target";
1183 return;
1184 }
1185
1187 qCDebug(lcQpaWaylandInput) << "Flushing scroll event sending ScrollBegin";
1188 if (auto *ww = target->waylandWindow())
1192 mParent->modifiers(), false));
1193 mScrollBeginSent = true;
1196 }
1197
1200
1201 qCDebug(lcQpaWaylandInput) << "Flushing scroll event" << phase << pixelDelta << angleDelta;
1202 if (auto *ww = target->waylandWindow())
1205 }
1206
1207 if (mScrollEnd) {
1208 if (mScrollBeginSent) {
1209 if (auto target = mScrollTarget.get()) {
1210 qCDebug(lcQpaWaylandInput) << "Flushing scroll end event";
1211 if (auto *ww = target->waylandWindow())
1214 }
1215 mScrollBeginSent = false;
1217 } else {
1218 // May receive axis_stop for events we haven't sent a ScrollBegin for because
1219 // most axis_sources do not mandate an axis_stop event to be sent.
1220
1221 // TODO: For now, we just ignore these events, but we could perhaps take this as an
1222 // indication that this compositor will in fact send axis_stop events for these sources
1223 // and send a ScrollBegin the next time an axis_source event with this type is encountered.
1224 }
1225 mScrollEnd = false;
1227 }
1228
1230}
1231
1233{
1235
1236 if (auto *event = mFrameData.event.get()) {
1237 if (auto surface = event->surface) {
1238 if (auto *ww = surface->waylandWindow())
1240 } else if (mFrameData.event->type == QEvent::MouseButtonRelease) {
1241 // If the window has been destroyed, we still need to report an up event, but it can't
1242 // be handled by the destroyed window (obviously), so send the event here instead.
1244 nullptr, event->timestamp,
1247 event->modifiers); // , Qt::MouseEventSource source =
1248 // Qt::MouseEventNotSynthesized);
1249 }
1251 }
1252
1253 //TODO: do modifiers get passed correctly here?
1255}
1256
1258{
1259 return source == axis_source_finger;
1260}
1261
1263{
1265#if QT_CONFIG(xkbcommon)
1267 return;
1268
1270 qCWarning(lcQpaWayland) << "unknown keymap format:" << format;
1271 close(fd);
1272 return;
1273 }
1274
1275 char *map_str = static_cast<char *>(mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0));
1276 if (map_str == MAP_FAILED) {
1277 close(fd);
1278 return;
1279 }
1280
1285
1287 close(fd);
1288
1289 if (mXkbKeymap)
1291 else
1292 mXkbState.reset(nullptr);
1293#else
1294 Q_UNUSED(fd);
1295 Q_UNUSED(size);
1296#endif
1297}
1298
1300{
1302 Q_UNUSED(keys);
1303
1304 if (!surface) {
1305 // Ignoring wl_keyboard.enter event with null surface. This is either a compositor bug,
1306 // or it's a race with a wl_surface.destroy request. In either case, ignore the event.
1307 return;
1308 }
1309
1311 if (!window)
1312 return;
1313
1314 if (mFocus) {
1315 qCWarning(lcQpaWayland()) << "Unexpected wl_keyboard.enter event. Keyboard already has focus";
1317 }
1318
1321
1323}
1324
1326{
1328
1329 if (!surface) {
1330 // Either a compositor bug, or a race condition with wl_surface.destroy, ignore the event.
1331 return;
1332 }
1333
1335 if (!window)
1336 return;
1337
1338 if (window->waylandSurface() != mFocus) {
1339 qCWarning(lcQpaWayland) << "Ignoring unexpected wl_keyboard.leave event."
1340 << "wl_surface argument does not match the current focus"
1341 << "This is most likely a compositor bug";
1342 return;
1343 }
1346}
1347
1351 const QString &text, bool autorepeat, ushort count)
1352{
1354 bool filtered = false;
1355
1356 if (inputContext) {
1361 }
1362
1363 if (!filtered) {
1364 QWindow *window = nullptr;
1367 } else if (auto *surface = focusSurface()) {
1368 if (auto *ww = surface->waylandWindow())
1369 window = ww->window();
1370 }
1371 if (!window)
1372 return;
1373
1374 if (type == QEvent::KeyPress && key == Qt::Key_Menu) {
1375 auto cursor = window->screen()->handle()->cursor();
1376 if (cursor) {
1377 const QPoint globalPos = cursor->pos();
1380 }
1381 }
1382
1385 }
1386}
1387
1389{
1391 qCWarning(lcQpaWayland) << Q_FUNC_INFO << "unknown keymap format:" << mKeymapFormat;
1392 return;
1393 }
1394
1395 auto *surface = focusSurface();
1396 if (!surface) {
1397 // We destroyed the keyboard focus surface, but the server didn't get the message yet...
1398 // or the server didn't send an enter event first. In either case, ignore the event.
1399 return;
1400 }
1401
1403
1405 if (isDown)
1407
1409#if QT_CONFIG(xkbcommon)
1410 if ((!mXkbKeymap || !mXkbState) && !createDefaultKeymap())
1411 return;
1412
1413 auto code = key + 8; // map to wl_keyboard::keymap_format::keymap_format_xkb_v1
1414
1417
1420
1423 if (isAutoRepeat && mRepeatRate > 0) {
1424 qCWarning(lcQpaWayland, "received key repeat event while repeat rate is non-zero");
1425 return;
1426 }
1428 qCWarning(lcQpaWayland, "received key repeat event for a key that should not be repeated");
1429 return;
1430 }
1431 if (isAutoRepeat)
1433
1435
1444 } else if (mRepeatKey.code == code) {
1446 }
1447#else
1448 Q_UNUSED(time);
1449 Q_UNUSED(key);
1450 qCWarning(lcQpaWayland, "xkbcommon not available on this build, not performing key mapping");
1451 return;
1452#endif
1454 // raw scan code
1455 return;
1456 }
1457}
1458
1460{
1462}
1463
1465{
1466 mFocus = nullptr;
1469}
1470
1491
1497
1499 uint32_t time,
1500 struct wl_surface *surface,
1501 int32_t id,
1502 wl_fixed_t x,
1503 wl_fixed_t y)
1504{
1505 if (!surface)
1506 return;
1507
1509 if (!window)
1510 return; // Ignore foreign surfaces
1511
1512 mParent->mTime = time;
1514 mFocus = window;
1518}
1519
1521{
1523 mParent->mTime = time;
1525
1526 if (allTouchPointsReleased()) {
1527 mFocus = nullptr;
1528
1529 // As of Weston 7.0.0 there is no touch_frame after the last touch_up
1530 // (i.e. when the last finger is released). To accommodate for this, issue a
1531 // touch_frame. This cannot hurt since it is safe to call the touch_frame
1532 // handler multiple times when there are no points left.
1533 // See: https://gitlab.freedesktop.org/wayland/weston/issues/44
1534 // TODO: change logging category to lcQpaWaylandInput in newer versions.
1535 qCDebug(lcQpaWayland, "Generating fake frame event to work around Weston bug");
1536 touch_frame();
1537 }
1538}
1539
1546
1554
1556{
1558 auto it = std::find_if(mTouch->mPendingTouchPoints.begin(), end, [id](const QWindowSystemInterface::TouchPoint &tp){ return tp.id == id; });
1559 if (it == end) {
1561 it->id = id;
1562 }
1563 // If the touch points were up and down in same frame, send out frame right away
1564 else if ((it->state == QEventPoint::Pressed && state == QEventPoint::Released)
1568 it->id = id;
1569 }
1570
1572
1573 // Only moved and pressed needs to update/set position
1575 // We need a global (screen) position.
1577
1578 //is it possible that mTouchFocus is null;
1579 if (!surface && mPointer)
1581 if (!surface && mKeyboard)
1583 if (!surface)
1584 return;
1585
1586 auto *ww = surface->waylandWindow();
1587 if (!ww || !ww->window())
1588 return;
1589
1590 tp.area = QRectF(0, 0, 8, 8);
1594 }
1595
1596 // If the touch point was pressed earlier this frame, we don't want to overwrite its state.
1597 if (tp.state != QEventPoint::Pressed)
1599
1600 tp.pressure = tp.state == QEventPoint::Released ? 0 : 1;
1601}
1602
1604{
1605 for (const auto &tp : std::as_const(mPendingTouchPoints)) {
1606 if (tp.state != QEventPoint::Released)
1607 return false;
1608 }
1609 return true;
1610}
1611
1622
1624{
1625 // TODO: early return if no events?
1626
1627 QWindow *window = nullptr;
1628 if (mFocus) {
1629 if (auto *ww = mFocus->waylandWindow())
1630 window = ww->window();
1631 }
1632
1633 if (mFocus) {
1634 // Returns a reference to the last item in the list. The list must not be empty.
1635 // If the list can be empty, call isEmpty() before calling this function.
1636 // See: https://doc.qt.io/qt-5.15/qlist.html#last
1638 return;
1640 // When the touch event is received, the global pos is calculated with the margins
1641 // in mind. Now we need to adjust again to get the correct local pos back.
1642 auto *ww = mFocus->waylandWindow();
1643 if (!ww)
1644 return;
1646 QPoint p = tp.area.center().toPoint();
1649 return;
1650 }
1651
1653
1654 // Prepare state for next frame
1657 for (const auto &prevPoint: prevTouchPoints) {
1658 // All non-released touch points should be part of the next touch event
1661 tp.state = QEventPoint::Stationary; // ... as stationary (unless proven otherwise)
1663 }
1664 }
1665
1666}
1667
1668}
1669
1670QT_END_NAMESPACE
1671
1672#include "moc_qwaylandinputdevice_p.cpp"
EnterEvent(QWaylandSurface *surface, const QPointF &local, const QPointF &global)
LeaveEvent(QWaylandSurface *surface, const QPointF &localPos, const QPointF &globalPos)
MotionEvent(QWaylandSurface *surface, ulong timestamp, const QPointF &localPos, const QPointF &globalPos, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
PressEvent(QWaylandSurface *surface, ulong timestamp, const QPointF &localPos, const QPointF &globalPos, Qt::MouseButtons buttons, Qt::MouseButton button, Qt::KeyboardModifiers modifiers)
ReleaseEvent(QWaylandSurface *surface, ulong timestamp, const QPointF &localPos, const QPointF &globalPos, Qt::MouseButtons buttons, Qt::MouseButton button, Qt::KeyboardModifiers modifiers)
WheelEvent(QWaylandSurface *surface, Qt::ScrollPhase phase, ulong timestamp, const QPointF &local, const QPointF &global, const QPoint &pixelDelta, const QPoint &angleDelta, Qt::MouseEventSource source, Qt::KeyboardModifiers modifiers, bool inverted)
Q_LOGGING_CATEGORY(lcQpaWayland, "qt.qpa.wayland")
static const int MaxTouchPoints
Q_DECLARE_LOGGING_CATEGORY(lcQpaWaylandInput)