KWin
Loading...
Searching...
No Matches
xwayland_input_test.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9#include "kwin_wayland_test.h"
10
11#include "core/output.h"
12#include "pointer_input.h"
13#include "wayland/seat.h"
14#include "wayland_server.h"
15#include "workspace.h"
16#include "x11window.h"
17
18#include <QAbstractEventDispatcher>
19#include <QSocketNotifier>
20
21#include <netwm.h>
22#include <xcb/xcb_icccm.h>
23
24namespace KWin
25{
26
27static const QString s_socketName = QStringLiteral("wayland_test_kwin_xwayland_input-0");
28
29class XWaylandInputTest : public QObject
30{
31 Q_OBJECT
32private Q_SLOTS:
33 void initTestCase();
34 void init();
35 void testPointerEnterLeaveSsd();
36 void testPointerEventLeaveCsd();
37};
38
39void XWaylandInputTest::initTestCase()
40{
41 qRegisterMetaType<KWin::Window *>();
42 QSignalSpy applicationStartedSpy(kwinApp(), &Application::started);
43 QVERIFY(waylandServer()->init(s_socketName));
45 QRect(0, 0, 1280, 1024),
46 QRect(1280, 0, 1280, 1024),
47 });
48
49 kwinApp()->start();
50 QVERIFY(applicationStartedSpy.wait());
51 const auto outputs = workspace()->outputs();
52 QCOMPARE(outputs.count(), 2);
53 QCOMPARE(outputs[0]->geometry(), QRect(0, 0, 1280, 1024));
54 QCOMPARE(outputs[1]->geometry(), QRect(1280, 0, 1280, 1024));
55 setenv("QT_QPA_PLATFORM", "wayland", true);
56}
57
58void XWaylandInputTest::init()
59{
60 workspace()->setActiveOutput(QPoint(640, 512));
61 input()->pointer()->warp(QPoint(640, 512));
62
63 QVERIFY(waylandServer()->windows().isEmpty());
64}
65
66class X11EventReaderHelper : public QObject
67{
68 Q_OBJECT
69public:
70 X11EventReaderHelper(xcb_connection_t *c);
71
72Q_SIGNALS:
73 void entered(const QPoint &localPoint);
74 void left(const QPoint &localPoint);
75
76private:
77 void processXcbEvents();
78 xcb_connection_t *m_connection;
79 QSocketNotifier *m_notifier;
80};
81
83 : QObject()
84 , m_connection(c)
85 , m_notifier(new QSocketNotifier(xcb_get_file_descriptor(m_connection), QSocketNotifier::Read, this))
86{
87 connect(m_notifier, &QSocketNotifier::activated, this, &X11EventReaderHelper::processXcbEvents);
88 connect(QCoreApplication::eventDispatcher(), &QAbstractEventDispatcher::aboutToBlock, this, &X11EventReaderHelper::processXcbEvents);
89 connect(QCoreApplication::eventDispatcher(), &QAbstractEventDispatcher::awake, this, &X11EventReaderHelper::processXcbEvents);
90}
91
92void X11EventReaderHelper::processXcbEvents()
93{
94 while (auto event = xcb_poll_for_event(m_connection)) {
95 const uint8_t eventType = event->response_type & ~0x80;
96 switch (eventType) {
97 case XCB_ENTER_NOTIFY: {
98 auto enterEvent = reinterpret_cast<xcb_enter_notify_event_t *>(event);
99 Q_EMIT entered(QPoint(enterEvent->event_x, enterEvent->event_y));
100 break;
101 }
102 case XCB_LEAVE_NOTIFY: {
103 auto leaveEvent = reinterpret_cast<xcb_leave_notify_event_t *>(event);
104 Q_EMIT left(QPoint(leaveEvent->event_x, leaveEvent->event_y));
105 break;
106 }
107 }
108 free(event);
109 }
110 xcb_flush(m_connection);
111}
112
113void XWaylandInputTest::testPointerEnterLeaveSsd()
114{
115 // this test simulates a pointer enter and pointer leave on a server-side decorated X11 window
116
117 // create the test window
119 QVERIFY(!xcb_connection_has_error(c.get()));
120 if (xcb_get_setup(c.get())->release_number < 11800000) {
121 QSKIP("XWayland 1.18 required");
122 }
123
124 xcb_warp_pointer(connection(), XCB_WINDOW_NONE, kwinApp()->x11RootWindow(), 0, 0, 0, 0, 640, 512);
125 xcb_flush(connection());
126
127 X11EventReaderHelper eventReader(c.get());
128 QSignalSpy enteredSpy(&eventReader, &X11EventReaderHelper::entered);
129 QSignalSpy leftSpy(&eventReader, &X11EventReaderHelper::left);
130
131 xcb_window_t windowId = xcb_generate_id(c.get());
132 const QRect windowGeometry = QRect(0, 0, 100, 200);
133 const uint32_t values[] = {
134 XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW};
135 xcb_create_window(c.get(), XCB_COPY_FROM_PARENT, windowId, rootWindow(),
136 windowGeometry.x(),
137 windowGeometry.y(),
138 windowGeometry.width(),
139 windowGeometry.height(),
140 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, values);
141 xcb_size_hints_t hints;
142 memset(&hints, 0, sizeof(hints));
143 xcb_icccm_size_hints_set_position(&hints, 1, windowGeometry.x(), windowGeometry.y());
144 xcb_icccm_size_hints_set_size(&hints, 1, windowGeometry.width(), windowGeometry.height());
145 xcb_icccm_set_wm_normal_hints(c.get(), windowId, &hints);
146 NETWinInfo info(c.get(), windowId, rootWindow(), NET::WMAllProperties, NET::WM2AllProperties);
147 info.setWindowType(NET::Normal);
148 xcb_map_window(c.get(), windowId);
149 xcb_flush(c.get());
150
151 QSignalSpy windowCreatedSpy(workspace(), &Workspace::windowAdded);
152 QVERIFY(windowCreatedSpy.wait());
153 X11Window *window = windowCreatedSpy.last().first().value<X11Window *>();
154 QVERIFY(window);
155 QVERIFY(window->isDecorated());
156 QVERIFY(!window->hasStrut());
157 QVERIFY(!window->readyForPainting());
158
159 QMetaObject::invokeMethod(window, "setReadyForPainting");
160 QVERIFY(window->readyForPainting());
161 QVERIFY(Test::waitForWaylandSurface(window));
162
163 // move pointer into the window, should trigger an enter
164 QVERIFY(!exclusiveContains(window->frameGeometry(), Cursors::self()->mouse()->pos()));
165 QVERIFY(enteredSpy.isEmpty());
166 input()->pointer()->warp(window->frameGeometry().center().toPoint());
167 QCOMPARE(waylandServer()->seat()->focusedPointerSurface(), window->surface());
168 QVERIFY(enteredSpy.wait());
169 QCOMPARE(enteredSpy.last().first().toPoint(), (window->frameGeometry().center() - QPointF(window->frameMargins().left(), window->frameMargins().top())).toPoint());
170
171 // move out of window
172 input()->pointer()->warp(window->frameGeometry().bottomRight() + QPointF(10, 10));
173 QVERIFY(leftSpy.wait());
174 QCOMPARE(leftSpy.last().first().toPoint(), (window->frameGeometry().center() - QPointF(window->frameMargins().left(), window->frameMargins().top())).toPoint());
175
176 // destroy window again
177 QSignalSpy windowClosedSpy(window, &X11Window::closed);
178 xcb_unmap_window(c.get(), windowId);
179 xcb_destroy_window(c.get(), windowId);
180 xcb_flush(c.get());
181 QVERIFY(windowClosedSpy.wait());
182}
183
184void XWaylandInputTest::testPointerEventLeaveCsd()
185{
186 // this test simulates a pointer enter and pointer leave on a client-side decorated X11 window
187
189 QVERIFY(!xcb_connection_has_error(c.get()));
190
191 xcb_warp_pointer(connection(), XCB_WINDOW_NONE, kwinApp()->x11RootWindow(), 0, 0, 0, 0, 640, 512);
192 xcb_flush(connection());
193
194 if (xcb_get_setup(c.get())->release_number < 11800000) {
195 QSKIP("XWayland 1.18 required");
196 }
197 if (!Xcb::Extensions::self()->isShapeAvailable()) {
198 QSKIP("SHAPE extension is required");
199 }
200
201 X11EventReaderHelper eventReader(c.get());
202 QSignalSpy enteredSpy(&eventReader, &X11EventReaderHelper::entered);
203 QSignalSpy leftSpy(&eventReader, &X11EventReaderHelper::left);
204
205 // Extents of the client-side drop-shadow.
206 NETStrut clientFrameExtent;
207 clientFrameExtent.left = 10;
208 clientFrameExtent.right = 10;
209 clientFrameExtent.top = 5;
210 clientFrameExtent.bottom = 20;
211
212 // Need to set the bounding shape in order to create a window without decoration.
213 xcb_rectangle_t boundingRect;
214 boundingRect.x = 0;
215 boundingRect.y = 0;
216 boundingRect.width = 100 + clientFrameExtent.left + clientFrameExtent.right;
217 boundingRect.height = 200 + clientFrameExtent.top + clientFrameExtent.bottom;
218
219 xcb_window_t windowId = xcb_generate_id(c.get());
220 const uint32_t values[] = {
221 XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW};
222 xcb_create_window(c.get(), XCB_COPY_FROM_PARENT, windowId, rootWindow(),
223 boundingRect.x, boundingRect.y, boundingRect.width, boundingRect.height,
224 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, values);
225 xcb_size_hints_t hints;
226 memset(&hints, 0, sizeof(hints));
227 xcb_icccm_size_hints_set_position(&hints, 1, boundingRect.x, boundingRect.y);
228 xcb_icccm_size_hints_set_size(&hints, 1, boundingRect.width, boundingRect.height);
229 xcb_icccm_set_wm_normal_hints(c.get(), windowId, &hints);
230 xcb_shape_rectangles(c.get(), XCB_SHAPE_SO_SET, XCB_SHAPE_SK_BOUNDING,
231 XCB_CLIP_ORDERING_UNSORTED, windowId, 0, 0, 1, &boundingRect);
232 NETWinInfo info(c.get(), windowId, rootWindow(), NET::WMAllProperties, NET::WM2AllProperties);
233 info.setWindowType(NET::Normal);
234 info.setGtkFrameExtents(clientFrameExtent);
235 xcb_map_window(c.get(), windowId);
236 xcb_flush(c.get());
237
238 QSignalSpy windowCreatedSpy(workspace(), &Workspace::windowAdded);
239 QVERIFY(windowCreatedSpy.wait());
240 X11Window *window = windowCreatedSpy.last().first().value<X11Window *>();
241 QVERIFY(window);
242 QVERIFY(!window->isDecorated());
243 QVERIFY(window->isClientSideDecorated());
244 QCOMPARE(window->bufferGeometry(), QRectF(0, 0, 120, 225));
245 QCOMPARE(window->frameGeometry(), QRectF(10, 5, 100, 200));
246
247 QMetaObject::invokeMethod(window, "setReadyForPainting");
248 QVERIFY(window->readyForPainting());
249 QVERIFY(Test::waitForWaylandSurface(window));
250
251 // Move pointer into the window, should trigger an enter.
252 QVERIFY(!exclusiveContains(window->frameGeometry(), Cursors::self()->mouse()->pos()));
253 QVERIFY(enteredSpy.isEmpty());
254 input()->pointer()->warp(window->frameGeometry().center().toPoint());
255 QCOMPARE(waylandServer()->seat()->focusedPointerSurface(), window->surface());
256 QVERIFY(enteredSpy.wait());
257 QCOMPARE(enteredSpy.last().first().toPoint(), QPoint(60, 105));
258
259 // Move out of the window, should trigger a leave.
260 QVERIFY(leftSpy.isEmpty());
261 input()->pointer()->warp(window->frameGeometry().bottomRight() + QPoint(100, 100));
262 QVERIFY(leftSpy.wait());
263 QCOMPARE(leftSpy.last().first().toPoint(), QPoint(60, 105));
264
265 // Destroy the window.
266 QSignalSpy windowClosedSpy(window, &X11Window::closed);
267 xcb_unmap_window(c.get(), windowId);
268 xcb_destroy_window(c.get(), windowId);
269 xcb_flush(c.get());
270 QVERIFY(windowClosedSpy.wait());
271}
272
273}
274
276#include "xwayland_input_test.moc"
QPointF pos()
Definition cursor.cpp:204
static Cursors * self()
Definition cursor.cpp:35
Cursor * mouse() const
Definition cursor.h:266
PointerInputRedirection * pointer() const
Definition input.h:220
void warp(const QPointF &pos)
void windowAdded(KWin::Window *)
QList< Output * > outputs() const
Definition workspace.h:762
void setActiveOutput(Output *output)
void entered(const QPoint &localPoint)
X11EventReaderHelper(xcb_connection_t *c)
void left(const QPoint &localPoint)
static Extensions * self()
Definition xcbutils.cpp:346
#define WAYLANDTEST_MAIN(TestObject)
void setOutputConfig(const QList< QRect > &geometries)
KWayland::Client::Seat * seat
bool waitForWaylandSurface(Window *window)
XcbConnectionPtr createX11Connection()
std::unique_ptr< xcb_connection_t, XcbConnectionDeleter > XcbConnectionPtr
KWIN_EXPORT xcb_window_t rootWindow()
Definition xcb.h:24
WaylandServer * waylandServer()
Workspace * workspace()
Definition workspace.h:830
KWIN_EXPORT xcb_connection_t * connection()
Definition xcb.h:19
InputRedirection * input()
Definition input.h:549