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 const bool firstPress = state && mButtons == Qt::NoButton;
864
865 if (state)
867 else
869
870 mParent->mTime = time;
872 if (state)
874
876
879 if (grab && grab->waylandSurface() != focusSurface()) {
880 pos = QPointF(-1, -1);
882
884 }
885
886 if (firstPress)
888
889 if (state) {
890 if (auto *window = surface->waylandWindow())
893 } else {
895 if (mButtons == Qt::NoButton)
897 }
898}
899
901{
902 if (mFocus) {
904 mFocus = nullptr;
905 }
906 mEnterSerial = 0;
907}
908
919
921{
922 if (auto *surface = focusSurface()) {
924 if (auto *ww = surface->waylandWindow())
926 }
927}
928
930{
931public:
932 WheelEvent(QWaylandSurface *surface, Qt::ScrollPhase phase, ulong timestamp, const QPointF &local,
933 const QPointF &global, const QPoint &pixelDelta, const QPoint &angleDelta,
934 Qt::MouseEventSource source, Qt::KeyboardModifiers modifiers, bool inverted)
939 {
940 }
941};
942
944{
945 if (!focusSurface()) {
946 // We destroyed the pointer focus surface, but the server didn't get the message yet...
947 // or the server didn't send an enter event first. In either case, ignore the event.
948 return;
949 }
950
951 // Get the delta and convert it into the expected range
952 switch (axis) {
955 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis vertical:" << mFrameData.delta.y();
956 break;
959 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis horizontal:" << mFrameData.delta.x();
960 break;
961 default:
962 //TODO: is this really needed?
963 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis: Unknown axis:" << axis;
964 return;
965 }
966
967 mParent->mTime = time;
968
970}
971
973{
975 qCDebug(lcQpaWaylandInput) << "compressed pointer_frame event";
976 return;
977 }
978
980}
981
983{
984 switch (source) {
986 qCDebug(lcQpaWaylandInput) << "Axis source wheel";
987 break;
989 qCDebug(lcQpaWaylandInput) << "Axis source finger";
990 break;
992 qCDebug(lcQpaWaylandInput) << "Axis source continuous";
993 break;
995 qCDebug(lcQpaWaylandInput) << "Axis source wheel tilt";
996 }
998}
999
1001{
1002 if (!focusSurface())
1003 return;
1004
1005 mParent->mTime = time;
1006 switch (axis) {
1008 qCDebug(lcQpaWaylandInput) << "Received vertical wl_pointer.axis_stop";
1009 break;
1011 qCDebug(lcQpaWaylandInput) << "Received horizontal wl_pointer.axis_stop";
1012 break;
1013 default:
1014 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_stop: Unknown axis: " << axis
1015 << "This is most likely a compositor bug";
1016 return;
1017 }
1018
1019 mScrollEnd = true;
1020}
1021
1023{
1024 if (!focusSurface())
1025 return;
1026
1027 const int32_t delta120 = value * 15 * 8;
1028
1029 switch (axis) {
1031 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_discrete vertical:" << value;
1033 break;
1035 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_discrete horizontal:" << value;
1037 break;
1038 default:
1039 //TODO: is this really needed?
1040 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_discrete: Unknown axis:" << axis;
1041 return;
1042 }
1043}
1044
1046{
1047 if (!focusSurface())
1048 return;
1049
1050 switch (axis) {
1052 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_value120 vertical:" << value;
1054 break;
1056 qCDebug(lcQpaWaylandInput) << "wl_pointer.axis_value120 horizontal:" << value;
1058 break;
1059 default:
1060 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_value120: Unknown axis:" << axis;
1061 return;
1062 }
1063}
1064
1066{
1068 switch (axis) {
1071 break;
1074 break;
1075 default:
1076 qCWarning(lcQpaWaylandInput) << "wl_pointer.axis_relative_direction: Unknown axis:" << axis;
1077 }
1078}
1079
1081{
1083 qCDebug(lcQpaWaylandInput) << "Flushing new event; no frame event in this version";
1084 pointer_frame();
1085 }
1086}
1087
1089{
1090 qCDebug(lcQpaWaylandInput) << "Setting frame event " << event->type;
1092 qCDebug(lcQpaWaylandInput) << "Flushing; previous was " << mFrameData.event->type;
1094 }
1095
1097
1099}
1100
1109
1111{
1112 switch (axisSource) {
1113 case axis_source_wheel_tilt: // sideways tilt of the wheel
1114 case axis_source_wheel:
1115 // In the case of wheel events, a pixel delta doesn't really make sense,
1116 // and will make Qt think this is a continuous scroll event when it isn't,
1117 // so just ignore it.
1118 return false;
1119 case axis_source_finger:
1121 return !delta.isNull();
1122 default:
1123 return false;
1124 }
1125}
1126
1128{
1129 if (!hasPixelDelta())
1130 return QPoint();
1131
1133 // Add accumulated rounding error before rounding again
1136 Q_ASSERT(qAbs(accumulatedError->x()) < 1.0);
1137 Q_ASSERT(qAbs(accumulatedError->y()) < 1.0);
1138
1139 // for continuous scroll events things should be
1140 // in the same direction
1141 // i.e converted so downwards surface co-ordinates (positive axis_value)
1142 // goes to downwards in wheel event (negative value)
1143 pixelDelta *= -1;
1144 return pixelDelta;
1145}
1146
1148{
1149 if (delta120.isNull()) {
1150 // If we didn't get any discrete events, then we need to fall back to
1151 // the continuous information.
1152 return (delta * -12).toPoint(); //TODO: why multiply by 12?
1153 }
1154
1155 // The angle delta is in eights of degrees, and our docs says most mice have
1156 // 1 click = 15 degrees, i.e. 120 is one click. It's also in the opposite
1157 // direction of surface space.
1158 return -delta120;
1159}
1160
1162{
1163 switch (axisSource) {
1164 case axis_source_wheel_tilt: // sideways tilt of the wheel
1165 case axis_source_wheel:
1167 case axis_source_finger:
1169 default: // Whatever other sources might be added are probably not mouse wheels
1171 }
1172}
1173
1175{
1177
1178 // The wayland protocol has separate horizontal and vertical axes, Qt has just the one inverted flag
1179 // Pragmatically it should't come up
1181
1182 // Angle delta is required for Qt wheel events, so don't try to send events if it's zero
1183 if (!angleDelta.isNull()) {
1185 if (!mScrollBeginSent) {
1186 if (!target && QWaylandWindow::mouseGrab())
1188 if (!target)
1189 target = focusSurface();
1190 }
1191 if (!target) {
1192 qCDebug(lcQpaWaylandInput) << "Flushing scroll event aborted - no scroll target";
1194 return;
1195 }
1196
1198 qCDebug(lcQpaWaylandInput) << "Flushing scroll event sending ScrollBegin";
1199 if (auto *ww = target->waylandWindow())
1203 mParent->modifiers(), false));
1204 mScrollBeginSent = true;
1207 }
1208
1211
1212 qCDebug(lcQpaWaylandInput) << "Flushing scroll event" << phase << pixelDelta << angleDelta;
1213 if (auto *ww = target->waylandWindow())
1216 }
1217
1218 if (mScrollEnd) {
1219 if (mScrollBeginSent) {
1220 if (auto target = mScrollTarget.get()) {
1221 qCDebug(lcQpaWaylandInput) << "Flushing scroll end event";
1222 if (auto *ww = target->waylandWindow())
1225 }
1226 mScrollBeginSent = false;
1228 } else {
1229 // May receive axis_stop for events we haven't sent a ScrollBegin for because
1230 // most axis_sources do not mandate an axis_stop event to be sent.
1231
1232 // TODO: For now, we just ignore these events, but we could perhaps take this as an
1233 // indication that this compositor will in fact send axis_stop events for these sources
1234 // and send a ScrollBegin the next time an axis_source event with this type is encountered.
1235 }
1236 mScrollEnd = false;
1238 }
1239
1241}
1242
1244{
1246
1247 if (auto *event = mFrameData.event.get()) {
1248 if (auto surface = event->surface) {
1249 if (auto *ww = surface->waylandWindow())
1251 } else if (mFrameData.event->type == QEvent::MouseButtonRelease) {
1252 // If the window has been destroyed, we still need to report an up event, but it can't
1253 // be handled by the destroyed window (obviously), so send the event here instead.
1255 nullptr, event->timestamp,
1258 event->modifiers); // , Qt::MouseEventSource source =
1259 // Qt::MouseEventNotSynthesized);
1260 }
1262 }
1263
1264 //TODO: do modifiers get passed correctly here?
1266}
1267
1269{
1270 return source == axis_source_finger;
1271}
1272
1274{
1276#if QT_CONFIG(xkbcommon)
1278 return;
1279
1281 qCWarning(lcQpaWayland) << "unknown keymap format:" << format;
1282 close(fd);
1283 return;
1284 }
1285
1286 char *map_str = static_cast<char *>(mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0));
1287 if (map_str == MAP_FAILED) {
1288 close(fd);
1289 return;
1290 }
1291
1296
1298 close(fd);
1299
1300 if (mXkbKeymap)
1302 else
1303 mXkbState.reset(nullptr);
1304#else
1305 Q_UNUSED(fd);
1306 Q_UNUSED(size);
1307#endif
1308}
1309
1311{
1313 Q_UNUSED(keys);
1314
1315 if (!surface) {
1316 // Ignoring wl_keyboard.enter event with null surface. This is either a compositor bug,
1317 // or it's a race with a wl_surface.destroy request. In either case, ignore the event.
1318 return;
1319 }
1320
1322 if (!window)
1323 return;
1324
1325 if (mFocus) {
1326 qCWarning(lcQpaWayland()) << "Unexpected wl_keyboard.enter event. Keyboard already has focus";
1328 }
1329
1332
1334}
1335
1337{
1339
1340 if (!surface) {
1341 // Either a compositor bug, or a race condition with wl_surface.destroy, ignore the event.
1342 return;
1343 }
1344
1346 if (!window)
1347 return;
1348
1349 if (window->waylandSurface() != mFocus) {
1350 qCWarning(lcQpaWayland) << "Ignoring unexpected wl_keyboard.leave event."
1351 << "wl_surface argument does not match the current focus"
1352 << "This is most likely a compositor bug";
1353 return;
1354 }
1357}
1358
1362 const QString &text, bool autorepeat, ushort count)
1363{
1365 bool filtered = false;
1366
1367 if (inputContext) {
1372 }
1373
1374 if (!filtered) {
1375 QWindow *window = nullptr;
1378 } else if (auto *surface = focusSurface()) {
1379 if (auto *ww = surface->waylandWindow())
1380 window = ww->window();
1381 }
1382 if (!window)
1383 return;
1384
1385 if (type == QEvent::KeyPress && key == Qt::Key_Menu) {
1386 auto cursor = window->screen()->handle()->cursor();
1387 if (cursor) {
1388 const QPoint globalPos = cursor->pos();
1391 }
1392 }
1393
1396 }
1397}
1398
1400{
1402 qCWarning(lcQpaWayland) << Q_FUNC_INFO << "unknown keymap format:" << mKeymapFormat;
1403 return;
1404 }
1405
1406 auto *surface = focusSurface();
1407 if (!surface) {
1408 // We destroyed the keyboard focus surface, but the server didn't get the message yet...
1409 // or the server didn't send an enter event first. In either case, ignore the event.
1410 return;
1411 }
1412
1414
1416 if (isDown)
1418
1420#if QT_CONFIG(xkbcommon)
1421 if ((!mXkbKeymap || !mXkbState) && !createDefaultKeymap())
1422 return;
1423
1424 auto code = key + 8; // map to wl_keyboard::keymap_format::keymap_format_xkb_v1
1425
1428
1431
1434 if (isAutoRepeat && mRepeatRate > 0) {
1435 qCWarning(lcQpaWayland, "received key repeat event while repeat rate is non-zero");
1436 return;
1437 }
1439 qCWarning(lcQpaWayland, "received key repeat event for a key that should not be repeated");
1440 return;
1441 }
1442 if (isAutoRepeat)
1444
1446
1455 } else if (mRepeatKey.code == code) {
1457 }
1458#else
1459 Q_UNUSED(time);
1460 Q_UNUSED(key);
1461 qCWarning(lcQpaWayland, "xkbcommon not available on this build, not performing key mapping");
1462 return;
1463#endif
1465 // raw scan code
1466 return;
1467 }
1468}
1469
1471{
1473}
1474
1476{
1477 mFocus = nullptr;
1480}
1481
1502
1508
1510 uint32_t time,
1511 struct wl_surface *surface,
1512 int32_t id,
1513 wl_fixed_t x,
1514 wl_fixed_t y)
1515{
1516 if (!surface)
1517 return;
1518
1520 if (!window)
1521 return; // Ignore foreign surfaces
1522
1523 mParent->mTime = time;
1525 mFocus = window;
1529}
1530
1532{
1534 mParent->mTime = time;
1536
1537 if (allTouchPointsReleased()) {
1538 mFocus = nullptr;
1539
1540 // As of Weston 7.0.0 there is no touch_frame after the last touch_up
1541 // (i.e. when the last finger is released). To accommodate for this, issue a
1542 // touch_frame. This cannot hurt since it is safe to call the touch_frame
1543 // handler multiple times when there are no points left.
1544 // See: https://gitlab.freedesktop.org/wayland/weston/issues/44
1545 // TODO: change logging category to lcQpaWaylandInput in newer versions.
1546 qCDebug(lcQpaWayland, "Generating fake frame event to work around Weston bug");
1547 touch_frame();
1548 }
1549}
1550
1557
1565
1567{
1569 auto it = std::find_if(mTouch->mPendingTouchPoints.begin(), end, [id](const QWindowSystemInterface::TouchPoint &tp){ return tp.id == id; });
1570 if (it == end) {
1572 it->id = id;
1573 }
1574 // If the touch points were up and down in same frame, send out frame right away
1575 else if ((it->state == QEventPoint::Pressed && state == QEventPoint::Released)
1579 it->id = id;
1580 }
1581
1583
1584 // Only moved and pressed needs to update/set position
1586 // We need a global (screen) position.
1588
1589 //is it possible that mTouchFocus is null;
1590 if (!surface && mPointer)
1592 if (!surface && mKeyboard)
1594 if (!surface)
1595 return;
1596
1597 auto *ww = surface->waylandWindow();
1598 if (!ww || !ww->window())
1599 return;
1600
1601 tp.area = QRectF(0, 0, 8, 8);
1605 }
1606
1607 // If the touch point was pressed earlier this frame, we don't want to overwrite its state.
1608 if (tp.state != QEventPoint::Pressed)
1610
1611 tp.pressure = tp.state == QEventPoint::Released ? 0 : 1;
1612}
1613
1615{
1616 for (const auto &tp : std::as_const(mPendingTouchPoints)) {
1617 if (tp.state != QEventPoint::Released)
1618 return false;
1619 }
1620 return true;
1621}
1622
1633
1635{
1636 // TODO: early return if no events?
1637
1638 QWindow *window = nullptr;
1639 if (mFocus) {
1640 if (auto *ww = mFocus->waylandWindow())
1641 window = ww->window();
1642 }
1643
1644 if (mFocus) {
1645 // Returns a reference to the last item in the list. The list must not be empty.
1646 // If the list can be empty, call isEmpty() before calling this function.
1647 // See: https://doc.qt.io/qt-5.15/qlist.html#last
1649 return;
1651 // When the touch event is received, the global pos is calculated with the margins
1652 // in mind. Now we need to adjust again to get the correct local pos back.
1653 auto *ww = mFocus->waylandWindow();
1654 if (!ww)
1655 return;
1657 QPoint p = tp.area.center().toPoint();
1660 return;
1661 }
1662
1664
1665 // Prepare state for next frame
1668 for (const auto &prevPoint: prevTouchPoints) {
1669 // All non-released touch points should be part of the next touch event
1672 tp.state = QEventPoint::Stationary; // ... as stationary (unless proven otherwise)
1674 }
1675 }
1676
1677}
1678
1679}
1680
1681QT_END_NAMESPACE
1682
1683#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)