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.
428 }
429 } else if (!(mCaps & WL_SEAT_CAPABILITY_POINTER) && mPointer) {
430 mPointer.reset();
433 }
434
436 mTouch.reset(createTouch(this));
437
438 if (!mTouchDevice) {
439 // TODO number of touchpoints, actual name and ID
445 }
446 } else if (!(mCaps & WL_SEAT_CAPABILITY_TOUCH) && mTouch) {
447 mTouch.reset();
448 }
449}
450
455
460
465
470
475
480
485
490
492{
493 return mTouch.get();
494}
495
503
509
510#if QT_CONFIG(wayland_datadevice)
512{
514}
515
517{
518 return mDataDevice;
519}
520#endif
521
522#if QT_CONFIG(clipboard)
524{
526}
527
529{
530 return mDataControlDevice.get();
531}
532#endif
533
534#if QT_CONFIG(wayland_client_primary_selection)
536{
538}
539
541{
543}
544#endif
545
550
551#if QT_CONFIG(tabletevent)
553{
555}
556
558{
559 return mTabletSeat.get();
560}
561#endif
562
567
572
577
583
585{
586 //TODO GUARD!
587 return mPointer && mPointer->focusSurface() ? mPointer->focusSurface()->waylandWindow() : nullptr;
588}
589
591{
592 //GUARD!
593
595}
596
598{
599 //GUARD!
600
601 return mTouch && mTouch->mFocus ? mTouch->mFocus->waylandWindow() : nullptr;
602}
603
608
610{
611#if QT_CONFIG(xkbcommon)
614#else
616#endif
617 return {};
618}
619
621{
622 if (!mKeyboard)
623 return Qt::NoModifier;
624
625 return mKeyboard->modifiers();
626}
627
629{
631
632#if QT_CONFIG(xkbcommon)
633 if (!mXkbState)
634 return ret;
635
637#endif
638
639 return ret;
640}
641
642#if QT_CONFIG(cursor)
644{
646 mCursor = CursorState(); // Clear any previous state
651
652 if (mCursor.shape == Qt::BitmapCursor) {
655 mCursor.bitmapScale = int(dpr); // Wayland doesn't support fractional buffer scale
656 // If there was a fractional part of the dpr, we need to scale the hotspot accordingly
657 if (mCursor.bitmapScale < dpr)
659 }
660
661 // Return early if setCursor was called redundantly (mostly happens from decorations)
666 return;
667 }
668
669 if (mPointer)
671
672#if QT_CONFIG(tabletevent)
673 if (mTabletSeat)
675#endif
676}
677#endif
678
680{
681public:
682 EnterEvent(QWaylandSurface *surface, const QPointF &local, const QPointF &global)
685 {}
686};
687
690{
691 if (!wlSurface)
692 return;
693
695
696 if (!surface)
697 return; // Ignore foreign surfaces
698
699 if (mFocus) {
700 qCWarning(lcQpaWayland) << "The compositor sent a wl_pointer.enter event before sending a"
701 << "leave event first, this is not allowed by the wayland protocol"
702 << "attempting to work around it by invalidating the current focus";
704 }
705 mFocus = surface;
707
709 if (auto *ww = surface->waylandWindow())
711 else
712 return;
713
716
719 }
720
721#if QT_CONFIG(cursor)
722 // Depends on mEnterSerial being updated
723 updateCursor();
724#endif
725
727 if (!grab)
729}
730
732{
733public:
734 LeaveEvent(QWaylandSurface *surface, const QPointF &localPos, const QPointF &globalPos)
737 {}
738};
739
741{
743
746
747 // The event may arrive after destroying the window, indicated by
748 // a null surface.
749 if (!wlSurface)
750 return;
751
753 if (!surface)
754 return; // Ignore foreign surfaces
755
758}
759
761{
762public:
763 MotionEvent(QWaylandSurface *surface, ulong timestamp, const QPointF &localPos,
764 const QPointF &globalPos, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
767 {
768 }
769};
770
772{
774 if (!surface) {
775 // We destroyed the pointer focus surface, but the server didn't get the message yet...
776 // or the server didn't send an enter event first. In either case, ignore the event.
777 return;
778 }
779
782 if (auto *ww = surface->waylandWindow())
784 else
785 return;
786
789 mParent->mTime = time;
790
792 if (grab && grab->waylandSurface() != surface) {
793 // We can't know the true position since we're getting events for another surface,
794 // so we just set it outside of the window boundaries.
795 pos = QPointF(-1, -1);
798 }
800}
801
803{
804public:
805 PressEvent(QWaylandSurface *surface, ulong timestamp, const QPointF &localPos,
806 const QPointF &globalPos, Qt::MouseButtons buttons, Qt::MouseButton button,
807 Qt::KeyboardModifiers modifiers)
810 {
811 }
812};
813
815{
816public:
817 ReleaseEvent(QWaylandSurface *surface, ulong timestamp, const QPointF &localPos,
818 const QPointF &globalPos, Qt::MouseButtons buttons, Qt::MouseButton button,
819 Qt::KeyboardModifiers modifiers)
822 {
823 }
824};
825
828{
830 if (!surface) {
831 // We destroyed the pointer focus surface, but the server didn't get the message yet...
832 // or the server didn't send an enter event first. In either case, ignore the event.
833 return;
834 }
835
837
838 // translate from kernel (input.h) 'button' to corresponding Qt:MouseButton.
839 // The range of mouse values is 0x110 <= mouse_button < 0x120, the first Joystick button.
840 switch (button) {
841 case 0x110: qt_button = Qt::LeftButton; break; // kernel BTN_LEFT
842 case 0x111: qt_button = Qt::RightButton; break;
843 case 0x112: qt_button = Qt::MiddleButton; break;
844 case 0x113: qt_button = Qt::ExtraButton1; break; // AKA Qt::BackButton
845 case 0x114: qt_button = Qt::ExtraButton2; break; // AKA Qt::ForwardButton
846 case 0x115: qt_button = Qt::ExtraButton3; break; // AKA Qt::TaskButton
847 case 0x116: qt_button = Qt::ExtraButton4; break;
848 case 0x117: qt_button = Qt::ExtraButton5; break;
849 case 0x118: qt_button = Qt::ExtraButton6; break;
850 case 0x119: qt_button = Qt::ExtraButton7; break;
851 case 0x11a: qt_button = Qt::ExtraButton8; break;
852 case 0x11b: qt_button = Qt::ExtraButton9; break;
853 case 0x11c: qt_button = Qt::ExtraButton10; break;
854 case 0x11d: qt_button = Qt::ExtraButton11; break;
855 case 0x11e: qt_button = Qt::ExtraButton12; break;
856 case 0x11f: qt_button = Qt::ExtraButton13; break;
857 default: return; // invalid button number (as far as Qt is concerned)
858 }
859
861 const bool firstPress = state && mButtons == Qt::NoButton;
862
863 if (state)
865 else
867
868 mParent->mTime = time;
870 if (state)
872
874
877 if (grab && grab->waylandSurface() != focusSurface()) {
878 pos = QPointF(-1, -1);
880
882 }
883
884 if (firstPress)
886
887 if (state) {
888 if (auto *window = surface->waylandWindow())
891 } else {
893 if (mButtons == Qt::NoButton)
895 }
896}
897
899{
900 if (mFocus) {
902 mFocus = nullptr;
903 }
904 mEnterSerial = 0;
905}
906
917
919{
920 if (auto *surface = focusSurface()) {
922 if (auto *ww = surface->waylandWindow())
924 }
925}
926
928{
929public:
930 WheelEvent(QWaylandSurface *surface, Qt::ScrollPhase phase, ulong timestamp, const QPointF &local,
931 const QPointF &global, const QPoint &pixelDelta, const QPoint &angleDelta,
932 Qt::MouseEventSource source, Qt::KeyboardModifiers modifiers, bool inverted)
937 {
938 }
939};
940
942{
943 if (!focusSurface()) {
944 // We destroyed the pointer focus surface, but the server didn't get the message yet...
945 // or the server didn't send an enter event first. In either case, ignore the event.
946 return;
947 }
948
949 // Get the delta and convert it into the expected range
950 switch (axis) {
953 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis vertical:" << mFrameData.delta.y();
954 break;
957 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis horizontal:" << mFrameData.delta.x();
958 break;
959 default:
960 //TODO: is this really needed?
961 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis: Unknown axis:" << axis;
962 return;
963 }
964
965 mParent->mTime = time;
966
968}
969
971{
973 qCDebug(lcQpaWaylandInput) << "compressed pointer_frame event";
974 return;
975 }
976
978}
979
981{
982 switch (source) {
984 qCDebug(lcQpaWaylandInput) << "Axis source wheel";
985 break;
987 qCDebug(lcQpaWaylandInput) << "Axis source finger";
988 break;
990 qCDebug(lcQpaWaylandInput) << "Axis source continuous";
991 break;
993 qCDebug(lcQpaWaylandInput) << "Axis source wheel tilt";
994 }
996}
997
999{
1000 if (!focusSurface())
1001 return;
1002
1003 mParent->mTime = time;
1004 switch (axis) {
1006 qCDebug(lcQpaWaylandInput) << "Received vertical wl_pointer.axis_stop";
1007 break;
1009 qCDebug(lcQpaWaylandInput) << "Received horizontal wl_pointer.axis_stop";
1010 break;
1011 default:
1012 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_stop: Unknown axis: " << axis
1013 << "This is most likely a compositor bug";
1014 return;
1015 }
1016
1017 mScrollEnd = true;
1018}
1019
1021{
1022 if (!focusSurface())
1023 return;
1024
1025 const int32_t delta120 = value * 15 * 8;
1026
1027 switch (axis) {
1029 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_discrete vertical:" << value;
1031 break;
1033 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_discrete horizontal:" << value;
1035 break;
1036 default:
1037 //TODO: is this really needed?
1038 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_discrete: Unknown axis:" << axis;
1039 return;
1040 }
1041}
1042
1044{
1045 if (!focusSurface())
1046 return;
1047
1048 switch (axis) {
1050 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_value120 vertical:" << value;
1052 break;
1054 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_value120 horizontal:" << value;
1056 break;
1057 default:
1058 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_value120: Unknown axis:" << axis;
1059 return;
1060 }
1061}
1062
1064{
1066 switch (axis) {
1069 break;
1072 break;
1073 default:
1074 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_relative_direction: Unknown axis:" << axis;
1075 }
1076}
1077
1079{
1081 qCDebug(lcQpaWaylandInput) << "Flushing new event; no frame event in this version";
1082 pointer_frame();
1083 }
1084}
1085
1087{
1088 qCDebug(lcQpaWaylandInput) << "Setting frame event " << event->type;
1090 qCDebug(lcQpaWaylandInput) << "Flushing; previous was " << mFrameData.event->type;
1092 }
1093
1095
1097}
1098
1107
1109{
1111 // Add accumulated rounding error before rounding again
1114 Q_ASSERT(qAbs(accumulatedError->x()) < 1.0);
1115 Q_ASSERT(qAbs(accumulatedError->y()) < 1.0);
1116
1117 // for continuous scroll events things should be
1118 // in the same direction
1119 // i.e converted so downwards surface co-ordinates (positive axis_value)
1120 // goes to downwards in wheel event (negative value)
1121 pixelDelta *= -1;
1122 return pixelDelta;
1123}
1124
1126{
1127 if (delta120.isNull()) {
1128 // If we didn't get any discrete events, then we need to fall back to
1129 // the continuous information.
1130 return (delta * -12).toPoint(); //TODO: why multiply by 12?
1131 }
1132
1133 // The angle delta is in eights of degrees, and our docs says most mice have
1134 // 1 click = 15 degrees, i.e. 120 is one click. It's also in the opposite
1135 // direction of surface space.
1136 return -delta120;
1137}
1138
1140{
1141 switch (axisSource) {
1142 case axis_source_wheel_tilt: // sideways tilt of the wheel
1143 case axis_source_wheel:
1145 case axis_source_finger:
1147 default: // Whatever other sources might be added are probably not mouse wheels
1149 }
1150}
1151
1153{
1155
1156 // The wayland protocol has separate horizontal and vertical axes, Qt has just the one inverted flag
1157 // Pragmatically it should't come up
1159
1160 // Angle delta is required for Qt wheel events, so don't try to send events if it's zero
1161 if (!angleDelta.isNull()) {
1163 if (!mScrollBeginSent) {
1164 if (!target && QWaylandWindow::mouseGrab())
1166 if (!target)
1167 target = focusSurface();
1168 }
1169 if (!target) {
1170 qCDebug(lcQpaWaylandInput) << "Flushing scroll event aborted - no scroll target";
1172 return;
1173 }
1174
1176 qCDebug(lcQpaWaylandInput) << "Flushing scroll event sending ScrollBegin";
1177 if (auto *ww = target->waylandWindow())
1181 mParent->modifiers(), false));
1182 mScrollBeginSent = true;
1185 }
1186
1189
1190 qCDebug(lcQpaWaylandInput) << "Flushing scroll event" << phase << pixelDelta << angleDelta;
1191 if (auto *ww = target->waylandWindow())
1194 }
1195
1196 if (mScrollEnd) {
1197 if (mScrollBeginSent) {
1198 if (auto target = mScrollTarget.get()) {
1199 qCDebug(lcQpaWaylandInput) << "Flushing scroll end event";
1200 if (auto *ww = target->waylandWindow())
1203 }
1204 mScrollBeginSent = false;
1206 } else {
1207 // May receive axis_stop for events we haven't sent a ScrollBegin for because
1208 // most axis_sources do not mandate an axis_stop event to be sent.
1209
1210 // TODO: For now, we just ignore these events, but we could perhaps take this as an
1211 // indication that this compositor will in fact send axis_stop events for these sources
1212 // and send a ScrollBegin the next time an axis_source event with this type is encountered.
1213 }
1214 mScrollEnd = false;
1216 }
1217
1219}
1220
1222{
1224
1225 if (auto *event = mFrameData.event.get()) {
1226 if (auto surface = event->surface) {
1227 if (auto *ww = surface->waylandWindow())
1229 } else if (mFrameData.event->type == QEvent::MouseButtonRelease) {
1230 // If the window has been destroyed, we still need to report an up event, but it can't
1231 // be handled by the destroyed window (obviously), so send the event here instead.
1233 nullptr, event->timestamp,
1236 event->modifiers); // , Qt::MouseEventSource source =
1237 // Qt::MouseEventNotSynthesized);
1238 }
1240 }
1241
1242 //TODO: do modifiers get passed correctly here?
1244}
1245
1247{
1248 return source == axis_source_finger;
1249}
1250
1252{
1254#if QT_CONFIG(xkbcommon)
1256 return;
1257
1259 qCWarning(lcQpaWayland) << "unknown keymap format:" << format;
1260 close(fd);
1261 return;
1262 }
1263
1264 char *map_str = static_cast<char *>(mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0));
1265 if (map_str == MAP_FAILED) {
1266 close(fd);
1267 return;
1268 }
1269
1274
1276 close(fd);
1277
1278 if (mXkbKeymap)
1280 else
1281 mXkbState.reset(nullptr);
1282#else
1283 Q_UNUSED(fd);
1284 Q_UNUSED(size);
1285#endif
1286}
1287
1289{
1291 Q_UNUSED(keys);
1292
1293 if (!surface) {
1294 // Ignoring wl_keyboard.enter event with null surface. This is either a compositor bug,
1295 // or it's a race with a wl_surface.destroy request. In either case, ignore the event.
1296 return;
1297 }
1298
1300 if (!window)
1301 return;
1302
1303 if (mFocus) {
1304 qCWarning(lcQpaWayland()) << "Unexpected wl_keyboard.enter event. Keyboard already has focus";
1306 }
1307
1310
1312}
1313
1315{
1317
1318 if (!surface) {
1319 // Either a compositor bug, or a race condition with wl_surface.destroy, ignore the event.
1320 return;
1321 }
1322
1324 if (!window)
1325 return;
1326
1327 if (window->waylandSurface() != mFocus) {
1328 qCWarning(lcQpaWayland) << "Ignoring unexpected wl_keyboard.leave event."
1329 << "wl_surface argument does not match the current focus"
1330 << "This is most likely a compositor bug";
1331 return;
1332 }
1335}
1336
1340 const QString &text, bool autorepeat, ushort count)
1341{
1343 bool filtered = false;
1344
1345 if (inputContext) {
1350 }
1351
1352 if (!filtered) {
1353 QWindow *window = nullptr;
1356 } else if (auto *surface = focusSurface()) {
1357 if (auto *ww = surface->waylandWindow())
1358 window = ww->window();
1359 }
1360 if (!window)
1361 return;
1362
1363 if (type == QEvent::KeyPress && key == Qt::Key_Menu) {
1364 auto cursor = window->screen()->handle()->cursor();
1365 if (cursor) {
1366 const QPoint globalPos = cursor->pos();
1369 }
1370 }
1371
1374 }
1375}
1376
1378{
1380 qCWarning(lcQpaWayland) << Q_FUNC_INFO << "unknown keymap format:" << mKeymapFormat;
1381 return;
1382 }
1383
1384 auto *surface = focusSurface();
1385 if (!surface) {
1386 // We destroyed the keyboard focus surface, but the server didn't get the message yet...
1387 // or the server didn't send an enter event first. In either case, ignore the event.
1388 return;
1389 }
1390
1392
1394 if (isDown)
1396
1398#if QT_CONFIG(xkbcommon)
1399 if ((!mXkbKeymap || !mXkbState) && !createDefaultKeymap())
1400 return;
1401
1402 auto code = key + 8; // map to wl_keyboard::keymap_format::keymap_format_xkb_v1
1403
1406
1409
1412 if (isAutoRepeat && mRepeatRate > 0) {
1413 qCWarning(lcQpaWayland, "received key repeat event while repeat rate is non-zero");
1414 return;
1415 }
1417 qCWarning(lcQpaWayland, "received key repeat event for a key that should not be repeated");
1418 return;
1419 }
1420 if (isAutoRepeat)
1422
1424
1433 } else if (mRepeatKey.code == code) {
1435 }
1436#else
1437 Q_UNUSED(time);
1438 Q_UNUSED(key);
1439 qCWarning(lcQpaWayland, "xkbcommon not available on this build, not performing key mapping");
1440 return;
1441#endif
1443 // raw scan code
1444 return;
1445 }
1446}
1447
1449{
1451}
1452
1454{
1455 mFocus = nullptr;
1458}
1459
1480
1486
1488 uint32_t time,
1489 struct wl_surface *surface,
1490 int32_t id,
1491 wl_fixed_t x,
1492 wl_fixed_t y)
1493{
1494 if (!surface)
1495 return;
1496
1498 if (!window)
1499 return; // Ignore foreign surfaces
1500
1501 mParent->mTime = time;
1503 mFocus = window;
1507}
1508
1510{
1512 mParent->mTime = time;
1514
1515 if (allTouchPointsReleased()) {
1516 mFocus = nullptr;
1517
1518 // As of Weston 7.0.0 there is no touch_frame after the last touch_up
1519 // (i.e. when the last finger is released). To accommodate for this, issue a
1520 // touch_frame. This cannot hurt since it is safe to call the touch_frame
1521 // handler multiple times when there are no points left.
1522 // See: https://gitlab.freedesktop.org/wayland/weston/issues/44
1523 // TODO: change logging category to lcQpaWaylandInput in newer versions.
1524 qCDebug(lcQpaWayland, "Generating fake frame event to work around Weston bug");
1525 touch_frame();
1526 }
1527}
1528
1535
1543
1545{
1547 auto it = std::find_if(mTouch->mPendingTouchPoints.begin(), end, [id](const QWindowSystemInterface::TouchPoint &tp){ return tp.id == id; });
1548 if (it == end) {
1550 it->id = id;
1551 }
1552 // If the touch points were up and down in same frame, send out frame right away
1553 else if ((it->state == QEventPoint::Pressed && state == QEventPoint::Released)
1557 it->id = id;
1558 }
1559
1561
1562 // Only moved and pressed needs to update/set position
1564 // We need a global (screen) position.
1566
1567 //is it possible that mTouchFocus is null;
1568 if (!surface && mPointer)
1570 if (!surface && mKeyboard)
1572 if (!surface)
1573 return;
1574
1575 auto *ww = surface->waylandWindow();
1576 if (!ww || !ww->window())
1577 return;
1578
1579 tp.area = QRectF(0, 0, 8, 8);
1583 }
1584
1585 // If the touch point was pressed earlier this frame, we don't want to overwrite its state.
1586 if (tp.state != QEventPoint::Pressed)
1588
1589 tp.pressure = tp.state == QEventPoint::Released ? 0 : 1;
1590}
1591
1593{
1594 for (const auto &tp : std::as_const(mPendingTouchPoints)) {
1595 if (tp.state != QEventPoint::Released)
1596 return false;
1597 }
1598 return true;
1599}
1600
1611
1613{
1614 // TODO: early return if no events?
1615
1616 QWindow *window = nullptr;
1617 if (mFocus) {
1618 if (auto *ww = mFocus->waylandWindow())
1619 window = ww->window();
1620 }
1621
1622 if (mFocus) {
1623 // Returns a reference to the last item in the list. The list must not be empty.
1624 // If the list can be empty, call isEmpty() before calling this function.
1625 // See: https://doc.qt.io/qt-5.15/qlist.html#last
1627 return;
1629 // When the touch event is received, the global pos is calculated with the margins
1630 // in mind. Now we need to adjust again to get the correct local pos back.
1631 auto *ww = mFocus->waylandWindow();
1632 if (!ww)
1633 return;
1635 QPoint p = tp.area.center().toPoint();
1638 return;
1639 }
1640
1642
1643 // Prepare state for next frame
1646 for (const auto &prevPoint: prevTouchPoints) {
1647 // All non-released touch points should be part of the next touch event
1650 tp.state = QEventPoint::Stationary; // ... as stationary (unless proven otherwise)
1652 }
1653 }
1654
1655}
1656
1657}
1658
1659QT_END_NAMESPACE
1660
1661#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)