1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qxcbwindow.h"
5
6#include <QtDebug>
7#include <QMetaEnum>
8#include <QScreen>
9#include <QtCore/QFileInfo>
10#include <QtGui/QIcon>
11#include <QtGui/QRegion>
12#include <QtGui/private/qhighdpiscaling_p.h>
13
14#include "qxcbintegration.h"
15#include "qxcbconnection.h"
16#include "qxcbscreen.h"
17#if QT_CONFIG(draganddrop)
18#include "qxcbdrag.h"
19#endif
20#include "qxcbkeyboard.h"
21#include "qxcbimage.h"
22#include "qxcbwmsupport.h"
23#include "qxcbimage.h"
24#include "qxcbnativeinterface.h"
25#include "qxcbsystemtraytracker.h"
26
27#include <qpa/qplatformintegration.h>
28#include <qpa/qplatformcursor.h>
29
30#include <algorithm>
31
32#include <xcb/xcb_icccm.h>
33#include <xcb/xfixes.h>
34#include <xcb/shape.h>
35#include <xcb/xinput.h>
36
37#include <private/qguiapplication_p.h>
38#include <private/qwindow_p.h>
39
40#include <qpa/qplatformbackingstore.h>
41#include <qpa/qwindowsysteminterface.h>
42
43#include <stdio.h>
44#include <unistd.h>
45
46#if QT_CONFIG(xcb_xlib)
47#define register /* C++17 deprecated register */
48#include <X11/Xlib.h>
49#include <X11/Xutil.h>
50#undef register
51#endif
52
53#define XCOORD_MAX 32767
54enum {
55 defaultWindowWidth = 160,
56 defaultWindowHeight = 160
57};
58
59QT_BEGIN_NAMESPACE
60
61using namespace Qt::StringLiterals;
62
63Q_STATIC_LOGGING_CATEGORY(lcQpaWindow, "qt.qpa.window");
64Q_STATIC_LOGGING_CATEGORY(lcQpaXcbWindow, "qt.qpa.xcb.window");
65
66Q_DECLARE_TYPEINFO(xcb_rectangle_t, Q_PRIMITIVE_TYPE);
67
68#undef FocusIn
69
70enum QX11EmbedFocusInDetail {
71 XEMBED_FOCUS_CURRENT = 0,
72 XEMBED_FOCUS_FIRST = 1,
73 XEMBED_FOCUS_LAST = 2
74};
75
76enum QX11EmbedInfoFlags {
77 XEMBED_MAPPED = (1 << 0),
78};
79
80enum QX11EmbedMessageType {
81 XEMBED_EMBEDDED_NOTIFY = 0,
82 XEMBED_WINDOW_ACTIVATE = 1,
83 XEMBED_WINDOW_DEACTIVATE = 2,
84 XEMBED_REQUEST_FOCUS = 3,
85 XEMBED_FOCUS_IN = 4,
86 XEMBED_FOCUS_OUT = 5,
87 XEMBED_FOCUS_NEXT = 6,
88 XEMBED_FOCUS_PREV = 7,
89 XEMBED_MODALITY_ON = 10,
90 XEMBED_MODALITY_OFF = 11,
91 XEMBED_REGISTER_ACCELERATOR = 12,
92 XEMBED_UNREGISTER_ACCELERATOR = 13,
93 XEMBED_ACTIVATE_ACCELERATOR = 14
94};
95
96const quint32 XEMBED_VERSION = 0;
97
98QXcbScreen *QXcbWindow::parentScreen()
99{
100 return QPlatformWindow::parent() ? static_cast<QXcbWindow*>(QPlatformWindow::parent())->parentScreen() : xcbScreen();
101}
102
103QXcbScreen *QXcbWindow::initialScreen() const
104{
105 // Resolve initial screen via QWindowPrivate::screenForGeometry(),
106 // which works in platform independent coordinates, as opposed to
107 // QPlatformWindow::screenForGeometry() that uses native coordinates.
108 QWindowPrivate *windowPrivate = qt_window_private(window: window());
109 QScreen *screen = windowPrivate->screenForGeometry(rect: window()->geometry());
110 return static_cast<QXcbScreen*>(screen->handle());
111}
112
113// Returns \c true if we should set WM_TRANSIENT_FOR on \a w
114static inline bool isTransient(const QWindow *w)
115{
116 return w->type() == Qt::Dialog
117 || w->type() == Qt::Sheet
118 || w->type() == Qt::Tool
119 || w->type() == Qt::SplashScreen
120 || w->type() == Qt::ToolTip
121 || w->type() == Qt::Drawer
122 || w->type() == Qt::Popup;
123}
124
125void QXcbWindow::setImageFormatForVisual(const xcb_visualtype_t *visual)
126{
127 if (qt_xcb_imageFormatForVisual(connection: connection(), depth: m_depth, visual, imageFormat: &m_imageFormat, needsRgbSwap: &m_imageRgbSwap))
128 return;
129
130 switch (m_depth) {
131 case 32:
132 case 24:
133 qWarning(msg: "Using RGB32 fallback, if this works your X11 server is reporting a bad screen format.");
134 m_imageFormat = QImage::Format_RGB32;
135 break;
136 case 16:
137 qWarning(msg: "Using RGB16 fallback, if this works your X11 server is reporting a bad screen format.");
138 m_imageFormat = QImage::Format_RGB16;
139 break;
140 default:
141 break;
142 }
143}
144
145#if QT_CONFIG(xcb_xlib)
146static inline XTextProperty* qstringToXTP(Display *dpy, const QString& s)
147{
148 #include <X11/Xatom.h>
149
150 static XTextProperty tp = { .value: nullptr, .encoding: 0, .format: 0, .nitems: 0 };
151 static bool free_prop = true; // we can't free tp.value in case it references
152 // the data of the static QByteArray below.
153 if (tp.value) {
154 if (free_prop)
155 XFree(tp.value);
156 tp.value = nullptr;
157 free_prop = true;
158 }
159
160 int errCode = 0;
161 QByteArray mapped = s.toLocal8Bit(); // should always be utf-8
162 char* tl[2];
163 tl[0] = mapped.data();
164 tl[1] = nullptr;
165 errCode = XmbTextListToTextProperty(display: dpy, list: tl, count: 1, style: XStdICCTextStyle, text_prop_return: &tp);
166 if (errCode < 0)
167 qCDebug(lcQpaXcb, "XmbTextListToTextProperty result code %d", errCode);
168
169 if (errCode < 0) {
170 static QByteArray qcs;
171 qcs = s.toLatin1();
172 tp.value = (uchar*)qcs.data();
173 tp.encoding = XA_STRING;
174 tp.format = 8;
175 tp.nitems = qcs.size();
176 free_prop = false;
177 }
178 return &tp;
179}
180#endif // QT_CONFIG(xcb_xlib)
181
182// TODO move this into a utility function in QWindow or QGuiApplication
183static QWindow *childWindowAt(QWindow *win, const QPoint &p)
184{
185 for (QObject *obj : win->children()) {
186 if (obj->isWindowType()) {
187 QWindow *childWin = static_cast<QWindow *>(obj);
188 if (childWin->isVisible()) {
189 if (QWindow *recurse = childWindowAt(win: childWin, p))
190 return recurse;
191 }
192 }
193 }
194 if (!win->isTopLevel()
195 && !(win->flags() & Qt::WindowTransparentForInput)
196 && win->geometry().contains(p: win->parent()->mapFromGlobal(pos: p))) {
197 return win;
198 }
199 return nullptr;
200}
201
202static const char *wm_window_type_property_id = "_q_xcb_wm_window_type";
203static const char *wm_window_role_property_id = "_q_xcb_wm_window_role";
204
205QXcbWindow::QXcbWindow(QWindow *window)
206 : QPlatformWindow(window)
207{
208 setConnection(xcbScreen()->connection());
209}
210
211enum : quint32 {
212 baseEventMask
213 = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY
214 | XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_FOCUS_CHANGE,
215
216 defaultEventMask = baseEventMask
217 | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE
218 | XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
219 | XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
220 | XCB_EVENT_MASK_POINTER_MOTION,
221
222 transparentForInputEventMask = baseEventMask
223 | XCB_EVENT_MASK_VISIBILITY_CHANGE
224 | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
225 | XCB_EVENT_MASK_COLOR_MAP_CHANGE | XCB_EVENT_MASK_OWNER_GRAB_BUTTON
226};
227
228void QXcbWindow::create()
229{
230 xcb_window_t old_m_window = m_window;
231 destroy();
232
233 m_windowState = Qt::WindowNoState;
234 m_trayIconWindow = isTrayIconWindow(window: window());
235
236 Qt::WindowType type = window()->type();
237
238 QXcbScreen *currentScreen = xcbScreen();
239 QXcbScreen *platformScreen = QPlatformWindow::parent() ? parentScreen() : initialScreen();
240 QRect rect = QPlatformWindow::parent()
241 ? QHighDpi::toNativeLocalPosition(value: window()->geometry(), context: platformScreen)
242 : QHighDpi::toNativePixels(value: window()->geometry(), context: platformScreen);
243
244 if (type == Qt::Desktop) {
245 m_window = platformScreen->root();
246 m_depth = platformScreen->screen()->root_depth;
247 m_visualId = platformScreen->screen()->root_visual;
248 const xcb_visualtype_t *visual = nullptr;
249 if (connection()->hasDefaultVisualId()) {
250 visual = platformScreen->visualForId(visualid: connection()->defaultVisualId());
251 if (visual)
252 m_visualId = connection()->defaultVisualId();
253 if (!visual)
254 qWarning(msg: "Could not use default visual id. Falling back to root_visual for screen.");
255 }
256 if (!visual)
257 visual = platformScreen->visualForId(visualid: m_visualId);
258 setImageFormatForVisual(visual);
259 connection()->addWindowEventListener(id: m_window, eventListener: this);
260 return;
261 }
262
263 const QSize minimumSize = windowMinimumSize();
264 if (rect.width() > 0 || rect.height() > 0) {
265 rect.setWidth(qBound(min: 1, val: rect.width(), XCOORD_MAX));
266 rect.setHeight(qBound(min: 1, val: rect.height(), XCOORD_MAX));
267 } else if (minimumSize.width() > 0 || minimumSize.height() > 0) {
268 rect.setSize(minimumSize);
269 } else {
270 rect.setWidth(QHighDpi::toNativePixels(value: int(defaultWindowWidth), context: platformScreen->QPlatformScreen::screen()));
271 rect.setHeight(QHighDpi::toNativePixels(value: int(defaultWindowHeight), context: platformScreen->QPlatformScreen::screen()));
272 }
273
274 QPlatformWindow::setGeometry(rect);
275
276 if (platformScreen != currentScreen)
277 QWindowSystemInterface::handleWindowScreenChanged(window: window(), newScreen: platformScreen->QPlatformScreen::screen());
278
279 xcb_window_t xcb_parent_id = platformScreen->root();
280 if (QPlatformWindow::parent()) {
281 xcb_parent_id = static_cast<QXcbWindow *>(QPlatformWindow::parent())->xcb_window();
282 m_embedded = QPlatformWindow::parent()->isForeignWindow();
283
284 QSurfaceFormat parentFormat = QPlatformWindow::parent()->window()->requestedFormat();
285 if (window()->surfaceType() != QSurface::OpenGLSurface && parentFormat.hasAlpha()) {
286 window()->setFormat(parentFormat);
287 }
288 }
289
290 resolveFormat(format: platformScreen->surfaceFormatFor(format: window()->requestedFormat()));
291
292 const xcb_visualtype_t *visual = nullptr;
293
294 if (m_trayIconWindow && connection()->systemTrayTracker()) {
295 visual = platformScreen->visualForId(visualid: connection()->systemTrayTracker()->visualId());
296 } else if (connection()->hasDefaultVisualId()) {
297 visual = platformScreen->visualForId(visualid: connection()->defaultVisualId());
298 if (!visual)
299 qWarning() << "Failed to use requested visual id.";
300 }
301
302 if (QPlatformWindow::parent()) {
303 // When using a Vulkan QWindow via QWidget::createWindowContainer() we
304 // must make sure the visuals are compatible. Now, the parent will be
305 // of RasterGLSurface which typically chooses a GLX/EGL compatible
306 // visual which may not be what the Vulkan window would choose.
307 // Therefore, take the parent's visual.
308 if (window()->surfaceType() == QSurface::VulkanSurface
309 && QPlatformWindow::parent()->window()->surfaceType() != QSurface::VulkanSurface)
310 {
311 visual = platformScreen->visualForId(visualid: static_cast<QXcbWindow *>(QPlatformWindow::parent())->visualId());
312 }
313 }
314
315 if (!visual)
316 visual = createVisual();
317
318 if (!visual) {
319 qWarning() << "Falling back to using screens root_visual.";
320 visual = platformScreen->visualForId(visualid: platformScreen->screen()->root_visual);
321 }
322
323 Q_ASSERT(visual);
324
325 m_visualId = visual->visual_id;
326 m_depth = platformScreen->depthOfVisual(visualid: m_visualId);
327 setImageFormatForVisual(visual);
328
329 quint32 mask = XCB_CW_BACK_PIXMAP
330 | XCB_CW_BORDER_PIXEL
331 | XCB_CW_BIT_GRAVITY
332 | XCB_CW_OVERRIDE_REDIRECT
333 | XCB_CW_SAVE_UNDER
334 | XCB_CW_EVENT_MASK
335 | XCB_CW_COLORMAP;
336
337 quint32 values[] = {
338 XCB_BACK_PIXMAP_NONE,
339 platformScreen->screen()->black_pixel,
340 XCB_GRAVITY_NORTH_WEST,
341 type == Qt::Popup || type == Qt::ToolTip || (window()->flags() & Qt::BypassWindowManagerHint),
342 type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
343 defaultEventMask,
344 platformScreen->colormapForVisual(visualid: m_visualId)
345 };
346
347 m_window = xcb_generate_id(c: xcb_connection());
348 xcb_create_window(c: xcb_connection(),
349 depth: m_depth,
350 wid: m_window, // window id
351 parent: xcb_parent_id, // parent window id
352 x: rect.x(),
353 y: rect.y(),
354 width: rect.width(),
355 height: rect.height(),
356 border_width: 0, // border width
357 class: XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
358 visual: m_visualId, // visual
359 value_mask: mask,
360 value_list: values);
361
362 connection()->addWindowEventListener(id: m_window, eventListener: this);
363
364 propagateSizeHints();
365
366 xcb_atom_t properties[5];
367 int propertyCount = 0;
368 properties[propertyCount++] = atom(atom: QXcbAtom::AtomWM_DELETE_WINDOW);
369 properties[propertyCount++] = atom(atom: QXcbAtom::AtomWM_TAKE_FOCUS);
370 properties[propertyCount++] = atom(atom: QXcbAtom::Atom_NET_WM_PING);
371
372 if (connection()->hasXSync())
373 properties[propertyCount++] = atom(atom: QXcbAtom::Atom_NET_WM_SYNC_REQUEST);
374
375 if (window()->flags() & Qt::WindowContextHelpButtonHint)
376 properties[propertyCount++] = atom(atom: QXcbAtom::Atom_NET_WM_CONTEXT_HELP);
377
378 xcb_change_property(c: xcb_connection(),
379 mode: XCB_PROP_MODE_REPLACE,
380 window: m_window,
381 property: atom(atom: QXcbAtom::AtomWM_PROTOCOLS),
382 type: XCB_ATOM_ATOM,
383 format: 32,
384 data_len: propertyCount,
385 data: properties);
386 m_syncValue.hi = 0;
387 m_syncValue.lo = 0;
388
389 const QByteArray wmClass = QXcbIntegration::instance()->wmClass();
390 if (!wmClass.isEmpty()) {
391 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE,
392 window: m_window, property: atom(atom: QXcbAtom::AtomWM_CLASS),
393 type: XCB_ATOM_STRING, format: 8, data_len: wmClass.size(), data: wmClass.constData());
394 }
395
396 QString desktopFileName = QGuiApplication::desktopFileName();
397 if (QGuiApplication::desktopFileName().isEmpty()) {
398 QFileInfo fi = QFileInfo(QCoreApplication::instance()->applicationFilePath());
399 QStringList domainName =
400 QCoreApplication::instance()->organizationDomain().split(sep: QLatin1Char('.'),
401 behavior: Qt::SkipEmptyParts);
402
403 if (domainName.isEmpty()) {
404 desktopFileName = fi.baseName();
405 } else {
406 for (int i = 0; i < domainName.size(); ++i)
407 desktopFileName.prepend(c: QLatin1Char('.')).prepend(s: domainName.at(i));
408 desktopFileName.append(s: fi.baseName());
409 }
410 }
411 if (!desktopFileName.isEmpty()) {
412 const QByteArray dfName = desktopFileName.toUtf8();
413 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE,
414 window: m_window, property: atom(atom: QXcbAtom::Atom_KDE_NET_WM_DESKTOP_FILE),
415 type: atom(atom: QXcbAtom::AtomUTF8_STRING), format: 8, data_len: dfName.size(), data: dfName.constData());
416 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE,
417 window: m_window, property: atom(atom: QXcbAtom::Atom_GTK_APPLICATION_ID),
418 type: atom(atom: QXcbAtom::AtomUTF8_STRING), format: 8, data_len: dfName.size(), data: dfName.constData());
419 }
420
421 if (connection()->hasXSync()) {
422 m_syncCounter = xcb_generate_id(c: xcb_connection());
423 xcb_sync_create_counter(c: xcb_connection(), id: m_syncCounter, initial_value: m_syncValue);
424
425 xcb_change_property(c: xcb_connection(),
426 mode: XCB_PROP_MODE_REPLACE,
427 window: m_window,
428 property: atom(atom: QXcbAtom::Atom_NET_WM_SYNC_REQUEST_COUNTER),
429 type: XCB_ATOM_CARDINAL,
430 format: 32,
431 data_len: 1,
432 data: &m_syncCounter);
433 }
434
435 // set the PID to let the WM kill the application if unresponsive
436 quint32 pid = getpid();
437 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: m_window,
438 property: atom(atom: QXcbAtom::Atom_NET_WM_PID), type: XCB_ATOM_CARDINAL, format: 32,
439 data_len: 1, data: &pid);
440
441 const QByteArray clientMachine = QSysInfo::machineHostName().toLocal8Bit();
442 if (!clientMachine.isEmpty()) {
443 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: m_window,
444 property: atom(atom: QXcbAtom::AtomWM_CLIENT_MACHINE), type: XCB_ATOM_STRING, format: 8,
445 data_len: clientMachine.size(), data: clientMachine.constData());
446 }
447
448 // Create WM_HINTS property on the window, so we can xcb_icccm_get_wm_hints*()
449 // from various setter functions for adjusting the hints.
450 xcb_icccm_wm_hints_t hints;
451 memset(s: &hints, c: 0, n: sizeof(hints));
452 hints.flags = XCB_ICCCM_WM_HINT_WINDOW_GROUP;
453 hints.window_group = connection()->clientLeader();
454 xcb_icccm_set_wm_hints(c: xcb_connection(), window: m_window, hints: &hints);
455
456 xcb_window_t leader = connection()->clientLeader();
457 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: m_window,
458 property: atom(atom: QXcbAtom::AtomWM_CLIENT_LEADER), type: XCB_ATOM_WINDOW, format: 32,
459 data_len: 1, data: &leader);
460
461 /* Add XEMBED info; this operation doesn't initiate the embedding. */
462 quint32 data[] = { XEMBED_VERSION, XEMBED_MAPPED };
463 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: m_window,
464 property: atom(atom: QXcbAtom::Atom_XEMBED_INFO),
465 type: atom(atom: QXcbAtom::Atom_XEMBED_INFO),
466 format: 32, data_len: 2, data: (void *)data);
467
468 if (connection()->hasXInput2())
469 connection()->xi2SelectDeviceEvents(window: m_window);
470
471 setWindowState(window()->windowStates());
472 setWindowFlags(window()->flags());
473 setWindowTitle(window()->title());
474
475 // force sync to read outstanding requests - see QTBUG-29106
476 connection()->sync();
477
478#if QT_CONFIG(draganddrop)
479 connection()->drag()->dndEnable(win: this, on: true);
480#endif
481
482 const qreal opacity = qt_window_private(window: window())->opacity;
483 if (!qFuzzyCompare(p1: opacity, p2: qreal(1.0)))
484 setOpacity(opacity);
485
486 setMask(QHighDpi::toNativeLocalRegion(pointRegion: window()->mask(), window: window()));
487
488 if (window()->isTopLevel())
489 setWindowIcon(window()->icon());
490
491 if (window()->dynamicPropertyNames().contains(t: wm_window_role_property_id)) {
492 QByteArray wmWindowRole = window()->property(name: wm_window_role_property_id).toByteArray();
493 setWindowRole(QString::fromLatin1(ba: wmWindowRole));
494 }
495
496 if (m_trayIconWindow)
497 m_embedded = requestSystemTrayWindowDock();
498
499 if (m_window != old_m_window) {
500 if (!m_wmTransientForChildren.isEmpty()) {
501 QList<QPointer<QXcbWindow>> transientChildren = m_wmTransientForChildren;
502 m_wmTransientForChildren.clear();
503 for (auto transientChild : transientChildren) {
504 if (transientChild)
505 transientChild->updateWmTransientFor();
506 }
507 }
508 }
509}
510
511QXcbWindow::~QXcbWindow()
512{
513 destroy();
514}
515
516QXcbForeignWindow::QXcbForeignWindow(QWindow *window, WId nativeHandle)
517 : QXcbWindow(window)
518{
519 m_window = nativeHandle;
520
521 // Reflect the foreign window's geometry as our own
522 if (auto geometry = Q_XCB_REPLY(xcb_get_geometry, xcb_connection(), m_window)) {
523 QRect nativeGeometry(geometry->x, geometry->y, geometry->width, geometry->height);
524 QPlatformWindow::setGeometry(nativeGeometry);
525 }
526
527 // And reparent, if we have a parent already
528 if (QPlatformWindow::parent())
529 setParent(QPlatformWindow::parent());
530}
531
532QXcbForeignWindow::~QXcbForeignWindow()
533{
534 if (QPlatformWindow::parent())
535 setParent(nullptr);
536
537 // Clear window so that destroy() does not affect it
538 m_window = 0;
539
540 if (connection()->mouseGrabber() == this)
541 connection()->setMouseGrabber(nullptr);
542 if (connection()->mousePressWindow() == this)
543 connection()->setMousePressWindow(nullptr);
544}
545
546void QXcbWindow::destroy()
547{
548 if (connection()->focusWindow() == this)
549 doFocusOut();
550 if (connection()->mouseGrabber() == this)
551 connection()->setMouseGrabber(nullptr);
552 if (connection()->mousePressWindow() == this)
553 connection()->setMousePressWindow(nullptr);
554
555 if (m_syncCounter && connection()->hasXSync())
556 xcb_sync_destroy_counter(c: xcb_connection(), counter: m_syncCounter);
557 if (m_window) {
558 if (m_netWmUserTimeWindow) {
559 xcb_delete_property(c: xcb_connection(), window: m_window, property: atom(atom: QXcbAtom::Atom_NET_WM_USER_TIME_WINDOW));
560 // Some window managers, like metacity, do XSelectInput on the _NET_WM_USER_TIME_WINDOW window,
561 // without trapping BadWindow (which crashes when the user time window is destroyed).
562 connection()->sync();
563 xcb_destroy_window(c: xcb_connection(), window: m_netWmUserTimeWindow);
564 m_netWmUserTimeWindow = XCB_NONE;
565 }
566 connection()->removeWindowEventListener(id: m_window);
567 xcb_destroy_window(c: xcb_connection(), window: m_window);
568 m_window = 0;
569 }
570
571 m_mapped = false;
572 m_recreationReasons = RecreationNotNeeded;
573
574 if (m_pendingSyncRequest)
575 m_pendingSyncRequest->invalidate();
576}
577
578void QXcbWindow::setGeometry(const QRect &rect)
579{
580 QPlatformWindow::setGeometry(rect);
581
582 propagateSizeHints();
583
584 QXcbScreen *currentScreen = xcbScreen();
585 QXcbScreen *newScreen = QPlatformWindow::parent() ? parentScreen() : static_cast<QXcbScreen*>(screenForGeometry(newGeometry: rect));
586
587 if (!newScreen)
588 newScreen = xcbScreen();
589
590 if (newScreen != currentScreen)
591 QWindowSystemInterface::handleWindowScreenChanged(window: window(), newScreen: newScreen->QPlatformScreen::screen());
592
593 if (qt_window_private(window: window())->positionAutomatic) {
594 const quint32 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
595 const qint32 values[] = {
596 qBound<qint32>(min: 1, val: rect.width(), XCOORD_MAX),
597 qBound<qint32>(min: 1, val: rect.height(), XCOORD_MAX),
598 };
599 xcb_configure_window(c: xcb_connection(), window: m_window, value_mask: mask, value_list: reinterpret_cast<const quint32*>(values));
600 } else {
601 const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
602 const qint32 values[] = {
603 qBound<qint32>(min: -XCOORD_MAX, val: rect.x(), XCOORD_MAX),
604 qBound<qint32>(min: -XCOORD_MAX, val: rect.y(), XCOORD_MAX),
605 qBound<qint32>(min: 1, val: rect.width(), XCOORD_MAX),
606 qBound<qint32>(min: 1, val: rect.height(), XCOORD_MAX),
607 };
608 xcb_configure_window(c: xcb_connection(), window: m_window, value_mask: mask, value_list: reinterpret_cast<const quint32*>(values));
609 if (window()->parent() && !window()->transientParent()) {
610 // Wait for server reply for parented windows to ensure that a few window
611 // moves will come as a one event. This is important when native widget is
612 // moved a few times in X and Y directions causing native scroll. Widget
613 // must get single event to not trigger unwanted widget position changes
614 // and then expose events causing backingstore flushes with incorrect
615 // offset causing image crruption.
616 connection()->sync();
617 }
618 }
619
620 xcb_flush(c: xcb_connection());
621}
622
623QMargins QXcbWindow::frameMargins() const
624{
625 if (m_dirtyFrameMargins) {
626 if (connection()->wmSupport()->isSupportedByWM(atom: atom(atom: QXcbAtom::Atom_NET_FRAME_EXTENTS))) {
627 auto reply = Q_XCB_REPLY(xcb_get_property, xcb_connection(), false, m_window,
628 atom(QXcbAtom::Atom_NET_FRAME_EXTENTS), XCB_ATOM_CARDINAL, 0, 4);
629 if (reply && reply->type == XCB_ATOM_CARDINAL && reply->format == 32 && reply->value_len == 4) {
630 quint32 *data = (quint32 *)xcb_get_property_value(R: reply.get());
631 // _NET_FRAME_EXTENTS format is left, right, top, bottom
632 m_frameMargins = QMargins(data[0], data[2], data[1], data[3]);
633 m_dirtyFrameMargins = false;
634 return m_frameMargins;
635 }
636 }
637
638 // _NET_FRAME_EXTENTS property is not available, so
639 // walk up the window tree to get the frame parent
640 xcb_window_t window = m_window;
641 xcb_window_t parent = m_window;
642
643 bool foundRoot = false;
644
645 const QList<xcb_window_t> &virtualRoots =
646 connection()->wmSupport()->virtualRoots();
647
648 while (!foundRoot) {
649 auto reply = Q_XCB_REPLY_UNCHECKED(xcb_query_tree, xcb_connection(), parent);
650 if (reply) {
651 if (reply->root == reply->parent || virtualRoots.indexOf(t: reply->parent) != -1 || reply->parent == XCB_WINDOW_NONE) {
652 foundRoot = true;
653 } else {
654 window = parent;
655 parent = reply->parent;
656 }
657 } else {
658 m_dirtyFrameMargins = false;
659 m_frameMargins = QMargins();
660 return m_frameMargins;
661 }
662 }
663
664 QPoint offset;
665
666 auto reply = Q_XCB_REPLY(xcb_translate_coordinates, xcb_connection(), window, parent, 0, 0);
667 if (reply) {
668 offset = QPoint(reply->dst_x, reply->dst_y);
669 }
670
671 auto geom = Q_XCB_REPLY(xcb_get_geometry, xcb_connection(), parent);
672 if (geom) {
673 // --
674 // add the border_width for the window managers frame... some window managers
675 // do not use a border_width of zero for their frames, and if we the left and
676 // top strut, we ensure that pos() is absolutely correct. frameGeometry()
677 // will still be incorrect though... perhaps i should have foffset as well, to
678 // indicate the frame offset (equal to the border_width on X).
679 // - Brad
680 // -- copied from qwidget_x11.cpp
681
682 int left = offset.x() + geom->border_width;
683 int top = offset.y() + geom->border_width;
684 int right = geom->width + geom->border_width - geometry().width() - offset.x();
685 int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
686
687 m_frameMargins = QMargins(left, top, right, bottom);
688 }
689
690 m_dirtyFrameMargins = false;
691 }
692
693 return m_frameMargins;
694}
695
696void QXcbWindow::setVisible(bool visible)
697{
698 if (visible)
699 show();
700 else
701 hide();
702}
703
704void QXcbWindow::updateWmTransientFor()
705{
706 xcb_window_t transientXcbParent = XCB_NONE;
707 if (isTransient(w: window())) {
708 QWindow *tp = window()->transientParent();
709 if (tp && tp->handle()) {
710 QXcbWindow *handle = static_cast<QXcbWindow *>(tp->handle());
711 transientXcbParent = tp->handle()->winId();
712 if (transientXcbParent) {
713 handle->registerWmTransientForChild(this);
714 qCDebug(lcQpaXcbWindow) << Q_FUNC_INFO << static_cast<QPlatformWindow *>(handle)
715 << " registerWmTransientForChild " << static_cast<QPlatformWindow *>(this);
716 }
717 }
718 // Default to client leader if there is no transient parent, else modal dialogs can
719 // be hidden by their parents.
720 if (!transientXcbParent)
721 transientXcbParent = connection()->clientLeader();
722 if (transientXcbParent) { // ICCCM 4.1.2.6
723 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: m_window,
724 property: XCB_ATOM_WM_TRANSIENT_FOR, type: XCB_ATOM_WINDOW, format: 32,
725 data_len: 1, data: &transientXcbParent);
726 qCDebug(lcQpaXcbWindow, "0x%x added XCB_ATOM_WM_TRANSIENT_FOR 0x%x", m_window, transientXcbParent);
727 }
728 }
729 if (!transientXcbParent)
730 xcb_delete_property(c: xcb_connection(), window: m_window, property: XCB_ATOM_WM_TRANSIENT_FOR);
731}
732
733void QXcbWindow::registerWmTransientForChild(QXcbWindow *child)
734{
735 if (!child)
736 return;
737
738 if (!m_wmTransientForChildren.contains(t: child))
739 m_wmTransientForChildren.append(t: child);
740}
741
742void QXcbWindow::show()
743{
744 if (window()->isTopLevel()) {
745 if (m_recreationReasons != RecreationNotNeeded) {
746 qCDebug(lcQpaWindow) << "QXcbWindow: need to recreate window" << window() << m_recreationReasons;
747 create();
748 m_recreationReasons = RecreationNotNeeded;
749 }
750
751 // update WM_NORMAL_HINTS
752 propagateSizeHints();
753
754 // update WM_TRANSIENT_FOR
755 updateWmTransientFor();
756
757 // update _NET_WM_STATE
758 setNetWmStateOnUnmappedWindow();
759 }
760
761 // QWidget-attribute Qt::WA_ShowWithoutActivating.
762 const auto showWithoutActivating = window()->property(name: "_q_showWithoutActivating");
763 if (showWithoutActivating.isValid() && showWithoutActivating.toBool())
764 updateNetWmUserTime(timestamp: 0);
765 else if (connection()->time() != XCB_TIME_CURRENT_TIME)
766 updateNetWmUserTime(timestamp: connection()->time());
767
768 if (m_trayIconWindow)
769 return; // defer showing until XEMBED_EMBEDDED_NOTIFY
770
771 xcb_map_window(c: xcb_connection(), window: m_window);
772
773 if (QGuiApplication::modalWindow() == window())
774 requestActivateWindow();
775
776 xcbScreen()->windowShown(window: this);
777
778 connection()->sync();
779}
780
781void QXcbWindow::hide()
782{
783 xcb_unmap_window(c: xcb_connection(), window: m_window);
784
785 // send synthetic UnmapNotify event according to icccm 4.1.4
786 q_padded_xcb_event<xcb_unmap_notify_event_t> event = {};
787 event.response_type = XCB_UNMAP_NOTIFY;
788 event.event = xcbScreen()->root();
789 event.window = m_window;
790 event.from_configure = false;
791 xcb_send_event(c: xcb_connection(), propagate: false, destination: xcbScreen()->root(),
792 event_mask: XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, event: (const char *)&event);
793
794 xcb_flush(c: xcb_connection());
795
796 if (connection()->mouseGrabber() == this)
797 connection()->setMouseGrabber(nullptr);
798 if (QPlatformWindow *w = connection()->mousePressWindow()) {
799 // Unset mousePressWindow when it (or one of its parents) is unmapped
800 while (w) {
801 if (w == this) {
802 connection()->setMousePressWindow(nullptr);
803 break;
804 }
805 w = w->parent();
806 }
807 }
808
809 // Avoid race with requestActivate()
810 QMutexLocker locker(&m_mappedMutex);
811 m_mapped = false;
812 m_deferredActivation = false;
813
814 // Hiding a modal window doesn't send an enter event to its transient parent when the
815 // mouse is already over the parent window, so the enter event must be emulated.
816 if (window()->isModal()) {
817 // Get the cursor position at modal window screen
818 const QPoint nativePos = xcbScreen()->cursor()->pos();
819 const QPoint cursorPos = QHighDpi::fromNativePixels(value: nativePos, context: xcbScreen()->screenForPosition(point: nativePos)->screen());
820
821 // Find the top level window at cursor position.
822 // Don't use QGuiApplication::topLevelAt(): search only the virtual siblings of this window's screen
823 QWindow *enterWindow = nullptr;
824 const auto screens = xcbScreen()->virtualSiblings();
825 for (QPlatformScreen *screen : screens) {
826 if (screen->geometry().contains(p: cursorPos)) {
827 const QPoint devicePosition = QHighDpi::toNativePixels(value: cursorPos, context: screen->screen());
828 enterWindow = screen->topLevelAt(point: devicePosition);
829 break;
830 }
831 }
832
833 if (enterWindow && enterWindow != window()) {
834 // Find the child window at cursor position, otherwise use the top level window
835 if (QWindow *childWindow = childWindowAt(win: enterWindow, p: cursorPos))
836 enterWindow = childWindow;
837 const QPoint localPos = enterWindow->mapFromGlobal(pos: cursorPos);
838 QWindowSystemInterface::handleEnterEvent(window: enterWindow,
839 local: localPos * QHighDpiScaling::factor(context: enterWindow),
840 global: nativePos);
841 }
842 }
843}
844
845bool QXcbWindow::relayFocusToModalWindow() const
846{
847 QWindow *w = static_cast<QWindowPrivate *>(QObjectPrivate::get(o: window()))->eventReceiver();
848 // get top-level window
849 while (w && w->parent())
850 w = w->parent();
851
852 QWindow *modalWindow = nullptr;
853 const bool blocked = QGuiApplicationPrivate::instance()->isWindowBlocked(window: w, blockingWindow: &modalWindow);
854 if (blocked && modalWindow != w) {
855 modalWindow->requestActivate();
856 connection()->flush();
857 return true;
858 }
859
860 return false;
861}
862
863void QXcbWindow::doFocusIn()
864{
865 if (relayFocusToModalWindow())
866 return;
867 QWindow *w = static_cast<QWindowPrivate *>(QObjectPrivate::get(o: window()))->eventReceiver();
868 connection()->setFocusWindow(w);
869 QWindowSystemInterface::handleFocusWindowChanged(window: w, r: Qt::ActiveWindowFocusReason);
870}
871
872void QXcbWindow::doFocusOut()
873{
874 connection()->setFocusWindow(nullptr);
875 relayFocusToModalWindow();
876 // Do not set the active window to nullptr if there is a FocusIn coming.
877 connection()->focusInTimer().start();
878}
879
880struct QtMotifWmHints {
881 quint32 flags, functions, decorations;
882 qint32 input_mode; // unused
883 quint32 status; // unused
884};
885
886enum {
887 MWM_HINTS_FUNCTIONS = (1L << 0),
888
889 MWM_FUNC_ALL = (1L << 0),
890 MWM_FUNC_RESIZE = (1L << 1),
891 MWM_FUNC_MOVE = (1L << 2),
892 MWM_FUNC_MINIMIZE = (1L << 3),
893 MWM_FUNC_MAXIMIZE = (1L << 4),
894 MWM_FUNC_CLOSE = (1L << 5),
895
896 MWM_HINTS_DECORATIONS = (1L << 1),
897
898 MWM_DECOR_ALL = (1L << 0),
899 MWM_DECOR_BORDER = (1L << 1),
900 MWM_DECOR_RESIZEH = (1L << 2),
901 MWM_DECOR_TITLE = (1L << 3),
902 MWM_DECOR_MENU = (1L << 4),
903 MWM_DECOR_MINIMIZE = (1L << 5),
904 MWM_DECOR_MAXIMIZE = (1L << 6),
905};
906
907QXcbWindow::NetWmStates QXcbWindow::netWmStates()
908{
909 NetWmStates result;
910
911 auto reply = Q_XCB_REPLY_UNCHECKED(xcb_get_property, xcb_connection(),
912 0, m_window, atom(QXcbAtom::Atom_NET_WM_STATE),
913 XCB_ATOM_ATOM, 0, 1024);
914
915 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
916 const xcb_atom_t *states = static_cast<const xcb_atom_t *>(xcb_get_property_value(R: reply.get()));
917 const xcb_atom_t *statesEnd = states + reply->length;
918 if (statesEnd != std::find(first: states, last: statesEnd, val: atom(atom: QXcbAtom::Atom_NET_WM_STATE_ABOVE)))
919 result |= NetWmStateAbove;
920 if (statesEnd != std::find(first: states, last: statesEnd, val: atom(atom: QXcbAtom::Atom_NET_WM_STATE_BELOW)))
921 result |= NetWmStateBelow;
922 if (statesEnd != std::find(first: states, last: statesEnd, val: atom(atom: QXcbAtom::Atom_NET_WM_STATE_FULLSCREEN)))
923 result |= NetWmStateFullScreen;
924 if (statesEnd != std::find(first: states, last: statesEnd, val: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_HORZ)))
925 result |= NetWmStateMaximizedHorz;
926 if (statesEnd != std::find(first: states, last: statesEnd, val: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_VERT)))
927 result |= NetWmStateMaximizedVert;
928 if (statesEnd != std::find(first: states, last: statesEnd, val: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MODAL)))
929 result |= NetWmStateModal;
930 if (statesEnd != std::find(first: states, last: statesEnd, val: atom(atom: QXcbAtom::Atom_NET_WM_STATE_STAYS_ON_TOP)))
931 result |= NetWmStateStaysOnTop;
932 if (statesEnd != std::find(first: states, last: statesEnd, val: atom(atom: QXcbAtom::Atom_NET_WM_STATE_DEMANDS_ATTENTION)))
933 result |= NetWmStateDemandsAttention;
934 if (statesEnd != std::find(first: states, last: statesEnd, val: atom(atom: QXcbAtom::Atom_NET_WM_STATE_HIDDEN)))
935 result |= NetWmStateHidden;
936 } else {
937 qCDebug(lcQpaXcb, "getting net wm state (%x), empty\n", m_window);
938 }
939
940 return result;
941}
942
943void QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
944{
945 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
946
947 if (type == Qt::ToolTip)
948 flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
949 if (type == Qt::Popup)
950 flags |= Qt::X11BypassWindowManagerHint;
951
952 Qt::WindowFlags oldflags = window()->flags();
953 if ((oldflags & Qt::WindowStaysOnTopHint) != (flags & Qt::WindowStaysOnTopHint))
954 m_recreationReasons |= WindowStaysOnTopHintChanged;
955 if ((oldflags & Qt::WindowStaysOnBottomHint) != (flags & Qt::WindowStaysOnBottomHint))
956 m_recreationReasons |= WindowStaysOnBottomHintChanged;
957
958 const quint32 mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
959 const quint32 values[] = {
960 // XCB_CW_OVERRIDE_REDIRECT
961 (flags & Qt::BypassWindowManagerHint) ? 1u : 0,
962 // XCB_CW_EVENT_MASK
963 (flags & Qt::WindowTransparentForInput) ? transparentForInputEventMask : defaultEventMask
964 };
965
966 xcb_change_window_attributes(c: xcb_connection(), window: xcb_window(), value_mask: mask, value_list: values);
967
968 WindowTypes wmWindowTypes;
969 if (window()->dynamicPropertyNames().contains(t: wm_window_type_property_id)) {
970 wmWindowTypes = static_cast<WindowTypes>(
971 qvariant_cast<int>(v: window()->property(name: wm_window_type_property_id)));
972 }
973
974 setWmWindowType(types: wmWindowTypes, flags);
975 setNetWmState(flags);
976 setMotifWmHints(flags);
977
978 setTransparentForMouseEvents(flags & Qt::WindowTransparentForInput);
979 updateDoesNotAcceptFocus(doesNotAcceptFocus: flags & Qt::WindowDoesNotAcceptFocus);
980}
981
982void QXcbWindow::setMotifWmHints(Qt::WindowFlags flags)
983{
984 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
985
986 QtMotifWmHints mwmhints;
987 memset(s: &mwmhints, c: 0, n: sizeof(mwmhints));
988
989 if (type != Qt::SplashScreen) {
990 mwmhints.flags |= MWM_HINTS_DECORATIONS;
991
992 bool customize = flags & Qt::CustomizeWindowHint;
993 if (type == Qt::Window && !customize) {
994 const Qt::WindowFlags defaultFlags = Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint;
995 if (!(flags & defaultFlags))
996 flags |= defaultFlags;
997 }
998 if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
999 mwmhints.decorations |= MWM_DECOR_BORDER;
1000 mwmhints.decorations |= MWM_DECOR_RESIZEH;
1001 mwmhints.decorations |= MWM_DECOR_TITLE;
1002
1003 if (flags & Qt::WindowSystemMenuHint)
1004 mwmhints.decorations |= MWM_DECOR_MENU;
1005
1006 if (flags & Qt::WindowMinimizeButtonHint) {
1007 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
1008 mwmhints.functions |= MWM_FUNC_MINIMIZE;
1009 }
1010
1011 if (flags & Qt::WindowMaximizeButtonHint) {
1012 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
1013 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
1014 }
1015
1016 if (flags & Qt::WindowCloseButtonHint)
1017 mwmhints.functions |= MWM_FUNC_CLOSE;
1018 }
1019 } else {
1020 // if type == Qt::SplashScreen
1021 mwmhints.decorations = MWM_DECOR_ALL;
1022 }
1023
1024 if (mwmhints.functions != 0) {
1025 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
1026 mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
1027 } else {
1028 mwmhints.functions = MWM_FUNC_ALL;
1029 }
1030
1031 if (!(flags & Qt::FramelessWindowHint)
1032 && flags & Qt::CustomizeWindowHint
1033 && flags & Qt::WindowTitleHint
1034 && !(flags &
1035 (Qt::WindowMinimizeButtonHint
1036 | Qt::WindowMaximizeButtonHint
1037 | Qt::WindowCloseButtonHint)))
1038 {
1039 // a special case - only the titlebar without any button
1040 mwmhints.flags = MWM_HINTS_FUNCTIONS;
1041 mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
1042 mwmhints.decorations = 0;
1043 }
1044
1045 if (mwmhints.flags) {
1046 xcb_change_property(c: xcb_connection(),
1047 mode: XCB_PROP_MODE_REPLACE,
1048 window: m_window,
1049 property: atom(atom: QXcbAtom::Atom_MOTIF_WM_HINTS),
1050 type: atom(atom: QXcbAtom::Atom_MOTIF_WM_HINTS),
1051 format: 32,
1052 data_len: 5,
1053 data: &mwmhints);
1054 } else {
1055 xcb_delete_property(c: xcb_connection(), window: m_window, property: atom(atom: QXcbAtom::Atom_MOTIF_WM_HINTS));
1056 }
1057}
1058
1059void QXcbWindow::setNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
1060{
1061 xcb_client_message_event_t event;
1062
1063 event.response_type = XCB_CLIENT_MESSAGE;
1064 event.format = 32;
1065 event.sequence = 0;
1066 event.window = m_window;
1067 event.type = atom(atom: QXcbAtom::Atom_NET_WM_STATE);
1068 event.data.data32[0] = set ? 1 : 0;
1069 event.data.data32[1] = one;
1070 event.data.data32[2] = two;
1071 event.data.data32[3] = 0;
1072 event.data.data32[4] = 0;
1073
1074 xcb_send_event(c: xcb_connection(), propagate: 0, destination: xcbScreen()->root(),
1075 event_mask: XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
1076 event: (const char *)&event);
1077}
1078
1079void QXcbWindow::setNetWmState(Qt::WindowStates state)
1080{
1081 if ((m_windowState ^ state) & Qt::WindowMaximized) {
1082 setNetWmState(set: state & Qt::WindowMaximized,
1083 one: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_HORZ),
1084 two: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_VERT));
1085 }
1086
1087 if ((m_windowState ^ state) & Qt::WindowFullScreen)
1088 setNetWmState(set: state & Qt::WindowFullScreen, one: atom(atom: QXcbAtom::Atom_NET_WM_STATE_FULLSCREEN));
1089}
1090
1091void QXcbWindow::setNetWmState(Qt::WindowFlags flags)
1092{
1093 setNetWmState(set: flags & Qt::WindowStaysOnTopHint,
1094 one: atom(atom: QXcbAtom::Atom_NET_WM_STATE_ABOVE),
1095 two: atom(atom: QXcbAtom::Atom_NET_WM_STATE_STAYS_ON_TOP));
1096 setNetWmState(set: flags & Qt::WindowStaysOnBottomHint, one: atom(atom: QXcbAtom::Atom_NET_WM_STATE_BELOW));
1097}
1098
1099void QXcbWindow::setNetWmStateOnUnmappedWindow()
1100{
1101 QMutexLocker locker(&m_mappedMutex);
1102 if (Q_UNLIKELY(m_mapped))
1103 qCDebug(lcQpaXcb()) << "internal info: " << Q_FUNC_INFO << "called on mapped window";
1104
1105 NetWmStates states;
1106 const Qt::WindowFlags flags = window()->flags();
1107 if (flags & Qt::WindowStaysOnTopHint) {
1108 states |= NetWmStateAbove;
1109 states |= NetWmStateStaysOnTop;
1110 } else if (flags & Qt::WindowStaysOnBottomHint) {
1111 states |= NetWmStateBelow;
1112 }
1113
1114 if (window()->windowStates() & Qt::WindowMinimized)
1115 states |= NetWmStateHidden;
1116
1117 if (window()->windowStates() & Qt::WindowFullScreen)
1118 states |= NetWmStateFullScreen;
1119
1120 if (window()->windowStates() & Qt::WindowMaximized) {
1121 states |= NetWmStateMaximizedHorz;
1122 states |= NetWmStateMaximizedVert;
1123 }
1124
1125 if (window()->modality() != Qt::NonModal)
1126 states |= NetWmStateModal;
1127
1128 // According to EWMH:
1129 // "The Window Manager should remove _NET_WM_STATE whenever a window is withdrawn".
1130 // Which means that we don't have to read this property before changing it on a withdrawn
1131 // window. But there are situations where users want to adjust this property as well
1132 // (e4cea305ed2ba3c9f580bf9d16c59a1048af0e8a), so instead of overwriting the property
1133 // we first read it and then merge our hints with the existing values, allowing a user
1134 // to set custom hints.
1135
1136 QList<xcb_atom_t> atoms;
1137 auto reply = Q_XCB_REPLY_UNCHECKED(xcb_get_property, xcb_connection(),
1138 0, m_window, atom(QXcbAtom::Atom_NET_WM_STATE),
1139 XCB_ATOM_ATOM, 0, 1024);
1140 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM && reply->value_len > 0) {
1141 const xcb_atom_t *data = static_cast<const xcb_atom_t *>(xcb_get_property_value(R: reply.get()));
1142 atoms.resize(size: reply->value_len);
1143 memcpy(dest: (void *)&atoms.first(), src: (void *)data, n: reply->value_len * sizeof(xcb_atom_t));
1144 }
1145
1146 if (states & NetWmStateAbove && !atoms.contains(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_ABOVE)))
1147 atoms.push_back(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_ABOVE));
1148 if (states & NetWmStateBelow && !atoms.contains(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_BELOW)))
1149 atoms.push_back(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_BELOW));
1150 if (states & NetWmStateHidden && !atoms.contains(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_HIDDEN)))
1151 atoms.push_back(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_HIDDEN));
1152 if (states & NetWmStateFullScreen && !atoms.contains(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_FULLSCREEN)))
1153 atoms.push_back(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_FULLSCREEN));
1154 if (states & NetWmStateMaximizedHorz && !atoms.contains(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_HORZ)))
1155 atoms.push_back(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_HORZ));
1156 if (states & NetWmStateMaximizedVert && !atoms.contains(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_VERT)))
1157 atoms.push_back(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_VERT));
1158 if (states & NetWmStateModal && !atoms.contains(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MODAL)))
1159 atoms.push_back(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MODAL));
1160 if (states & NetWmStateStaysOnTop && !atoms.contains(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_STAYS_ON_TOP)))
1161 atoms.push_back(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_STAYS_ON_TOP));
1162 if (states & NetWmStateDemandsAttention && !atoms.contains(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_DEMANDS_ATTENTION)))
1163 atoms.push_back(t: atom(atom: QXcbAtom::Atom_NET_WM_STATE_DEMANDS_ATTENTION));
1164
1165 if (atoms.isEmpty()) {
1166 xcb_delete_property(c: xcb_connection(), window: m_window, property: atom(atom: QXcbAtom::Atom_NET_WM_STATE));
1167 } else {
1168 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: m_window,
1169 property: atom(atom: QXcbAtom::Atom_NET_WM_STATE), type: XCB_ATOM_ATOM, format: 32,
1170 data_len: atoms.size(), data: atoms.constData());
1171 }
1172 xcb_flush(c: xcb_connection());
1173}
1174
1175void QXcbWindow::setWindowState(Qt::WindowStates state)
1176{
1177 if (state == m_windowState)
1178 return;
1179
1180 Qt::WindowStates unsetState = m_windowState & ~state;
1181 Qt::WindowStates newState = state & ~m_windowState;
1182
1183 // unset old state
1184 if (unsetState & Qt::WindowMinimized)
1185 xcb_map_window(c: xcb_connection(), window: m_window);
1186 if (unsetState & Qt::WindowMaximized)
1187 setNetWmState(set: false,
1188 one: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_HORZ),
1189 two: atom(atom: QXcbAtom::Atom_NET_WM_STATE_MAXIMIZED_VERT));
1190 if (unsetState & Qt::WindowFullScreen)
1191 setNetWmState(set: false, one: atom(atom: QXcbAtom::Atom_NET_WM_STATE_FULLSCREEN));
1192
1193 // set new state
1194 if (newState & Qt::WindowMinimized) {
1195 {
1196 xcb_client_message_event_t event;
1197
1198 event.response_type = XCB_CLIENT_MESSAGE;
1199 event.format = 32;
1200 event.sequence = 0;
1201 event.window = m_window;
1202 event.type = atom(atom: QXcbAtom::AtomWM_CHANGE_STATE);
1203 event.data.data32[0] = XCB_ICCCM_WM_STATE_ICONIC;
1204 event.data.data32[1] = 0;
1205 event.data.data32[2] = 0;
1206 event.data.data32[3] = 0;
1207 event.data.data32[4] = 0;
1208
1209 xcb_send_event(c: xcb_connection(), propagate: 0, destination: xcbScreen()->root(),
1210 event_mask: XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
1211 event: (const char *)&event);
1212 }
1213 m_minimized = true;
1214 }
1215
1216 // set Maximized && FullScreen state if need
1217 setNetWmState(state);
1218
1219 xcb_get_property_cookie_t cookie = xcb_icccm_get_wm_hints_unchecked(c: xcb_connection(), window: m_window);
1220 xcb_icccm_wm_hints_t hints;
1221 if (xcb_icccm_get_wm_hints_reply(c: xcb_connection(), cookie, hints: &hints, e: nullptr)) {
1222 if (state & Qt::WindowMinimized)
1223 xcb_icccm_wm_hints_set_iconic(hints: &hints);
1224 else
1225 xcb_icccm_wm_hints_set_normal(hints: &hints);
1226 xcb_icccm_set_wm_hints(c: xcb_connection(), window: m_window, hints: &hints);
1227 }
1228
1229 connection()->sync();
1230 m_windowState = state;
1231}
1232
1233void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
1234{
1235 xcb_window_t wid = m_window;
1236 // If timestamp == 0, then it means that the window should not be
1237 // initially activated. Don't update global user time for this
1238 // special case.
1239 if (timestamp != 0)
1240 connection()->setNetWmUserTime(timestamp);
1241
1242 const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom: atom(atom: QXcbAtom::Atom_NET_WM_USER_TIME_WINDOW));
1243 if (m_netWmUserTimeWindow || isSupportedByWM) {
1244 if (!m_netWmUserTimeWindow) {
1245 m_netWmUserTimeWindow = xcb_generate_id(c: xcb_connection());
1246 xcb_create_window(c: xcb_connection(),
1247 XCB_COPY_FROM_PARENT, // depth -- same as root
1248 wid: m_netWmUserTimeWindow, // window id
1249 parent: m_window, // parent window id
1250 x: -1, y: -1, width: 1, height: 1,
1251 border_width: 0, // border width
1252 class: XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
1253 visual: m_visualId, // visual
1254 value_mask: 0, // value mask
1255 value_list: nullptr); // value list
1256 wid = m_netWmUserTimeWindow;
1257 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: m_window, property: atom(atom: QXcbAtom::Atom_NET_WM_USER_TIME_WINDOW),
1258 type: XCB_ATOM_WINDOW, format: 32, data_len: 1, data: &m_netWmUserTimeWindow);
1259 xcb_delete_property(c: xcb_connection(), window: m_window, property: atom(atom: QXcbAtom::Atom_NET_WM_USER_TIME));
1260
1261 QXcbWindow::setWindowTitle(conn: connection(), window: m_netWmUserTimeWindow,
1262 QStringLiteral("Qt NET_WM User Time Window"));
1263
1264 } else if (!isSupportedByWM) {
1265 // WM no longer supports it, then we should remove the
1266 // _NET_WM_USER_TIME_WINDOW atom.
1267 xcb_delete_property(c: xcb_connection(), window: m_window, property: atom(atom: QXcbAtom::Atom_NET_WM_USER_TIME_WINDOW));
1268 xcb_destroy_window(c: xcb_connection(), window: m_netWmUserTimeWindow);
1269 m_netWmUserTimeWindow = XCB_NONE;
1270 } else {
1271 wid = m_netWmUserTimeWindow;
1272 }
1273 }
1274 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: wid, property: atom(atom: QXcbAtom::Atom_NET_WM_USER_TIME),
1275 type: XCB_ATOM_CARDINAL, format: 32, data_len: 1, data: &timestamp);
1276}
1277
1278void QXcbWindow::setTransparentForMouseEvents(bool transparent)
1279{
1280 if (!connection()->hasXFixes() || transparent == m_transparent)
1281 return;
1282
1283 xcb_rectangle_t rectangle;
1284
1285 xcb_rectangle_t *rect = nullptr;
1286 int nrect = 0;
1287
1288 if (!transparent) {
1289 rectangle.x = 0;
1290 rectangle.y = 0;
1291 rectangle.width = geometry().width();
1292 rectangle.height = geometry().height();
1293 rect = &rectangle;
1294 nrect = 1;
1295 }
1296
1297 xcb_xfixes_region_t region = xcb_generate_id(c: xcb_connection());
1298 xcb_xfixes_create_region(c: xcb_connection(), region, rectangles_len: nrect, rectangles: rect);
1299 xcb_xfixes_set_window_shape_region_checked(c: xcb_connection(), dest: m_window, dest_kind: XCB_SHAPE_SK_INPUT, x_offset: 0, y_offset: 0, region);
1300 xcb_xfixes_destroy_region(c: xcb_connection(), region);
1301
1302 m_transparent = transparent;
1303}
1304
1305void QXcbWindow::updateDoesNotAcceptFocus(bool doesNotAcceptFocus)
1306{
1307 xcb_get_property_cookie_t cookie = xcb_icccm_get_wm_hints_unchecked(c: xcb_connection(), window: m_window);
1308
1309 xcb_icccm_wm_hints_t hints;
1310 if (!xcb_icccm_get_wm_hints_reply(c: xcb_connection(), cookie, hints: &hints, e: nullptr))
1311 return;
1312
1313 xcb_icccm_wm_hints_set_input(hints: &hints, input: !doesNotAcceptFocus);
1314 xcb_icccm_set_wm_hints(c: xcb_connection(), window: m_window, hints: &hints);
1315}
1316
1317WId QXcbWindow::winId() const
1318{
1319 return m_window;
1320}
1321
1322void QXcbWindow::setParent(const QPlatformWindow *parent)
1323{
1324 QPoint topLeft = geometry().topLeft();
1325
1326 xcb_window_t xcb_parent_id;
1327 if (parent) {
1328 const QXcbWindow *qXcbParent = static_cast<const QXcbWindow *>(parent);
1329 xcb_parent_id = qXcbParent->xcb_window();
1330 m_embedded = qXcbParent->isForeignWindow();
1331 } else {
1332 xcb_parent_id = xcbScreen()->root();
1333 m_embedded = false;
1334 }
1335 xcb_reparent_window(c: xcb_connection(), window: xcb_window(), parent: xcb_parent_id, x: topLeft.x(), y: topLeft.y());
1336 connection()->sync();
1337}
1338
1339void QXcbWindow::setWindowTitle(const QString &title)
1340{
1341 setWindowTitle(conn: connection(), window: m_window, title);
1342}
1343
1344QString QXcbWindow::windowTitle() const
1345{
1346 return windowTitle(conn: connection(), window: m_window);
1347}
1348
1349void QXcbWindow::setWindowIconText(const QString &title)
1350{
1351 const QByteArray ba = title.toUtf8();
1352 xcb_change_property(c: xcb_connection(),
1353 mode: XCB_PROP_MODE_REPLACE,
1354 window: m_window,
1355 property: atom(atom: QXcbAtom::Atom_NET_WM_ICON_NAME),
1356 type: atom(atom: QXcbAtom::AtomUTF8_STRING),
1357 format: 8,
1358 data_len: ba.size(),
1359 data: ba.constData());
1360}
1361
1362void QXcbWindow::setWindowIcon(const QIcon &icon)
1363{
1364 QList<quint32> icon_data;
1365 const uint32_t sizeLimit = xcb_get_maximum_request_length(c: xcb_connection());
1366 if (!icon.isNull()) {
1367 QList<QSize> availableSizes = icon.availableSizes();
1368 if (availableSizes.isEmpty()) {
1369 // try to use default sizes since the icon can be a scalable image like svg.
1370 availableSizes.push_back(t: QSize(16,16));
1371 availableSizes.push_back(t: QSize(32,32));
1372 availableSizes.push_back(t: QSize(64,64));
1373 availableSizes.push_back(t: QSize(128,128));
1374 }
1375 for (int i = 0; i < availableSizes.size(); ++i) {
1376 QSize size = availableSizes.at(i);
1377 QPixmap pixmap = icon.pixmap(size);
1378 if (!pixmap.isNull()) {
1379 QImage image = pixmap.toImage().convertToFormat(f: QImage::Format_ARGB32);
1380 int pos = icon_data.size();
1381 int newSize = pos + 2 + image.width()*image.height();
1382 // In the absence of the BIG-REQUESTS extension, or with too big DPR,
1383 // the size of icon data is too big for the xcb request very easily.
1384 if (quint64(newSize) > quint64(sizeLimit))
1385 break;
1386 icon_data.resize(size: newSize);
1387 icon_data[pos++] = image.width();
1388 icon_data[pos++] = image.height();
1389 memcpy(dest: icon_data.data() + pos, src: image.bits(), n: image.width()*image.height()*4);
1390 }
1391 }
1392 }
1393
1394 if (!icon_data.isEmpty()) {
1395 // Ignore icon exceeding maximum xcb request length
1396 if (quint64(icon_data.size()) > quint64(sizeLimit)) {
1397 qWarning() << "Ignoring window icon" << icon_data.size()
1398 << "exceeds maximum xcb request length"
1399 << sizeLimit;
1400 return;
1401 }
1402 xcb_change_property(c: xcb_connection(),
1403 mode: XCB_PROP_MODE_REPLACE,
1404 window: m_window,
1405 property: atom(atom: QXcbAtom::Atom_NET_WM_ICON),
1406 type: atom(atom: QXcbAtom::AtomCARDINAL),
1407 format: 32,
1408 data_len: icon_data.size(),
1409 data: (unsigned char *) icon_data.data());
1410 } else {
1411 xcb_delete_property(c: xcb_connection(),
1412 window: m_window,
1413 property: atom(atom: QXcbAtom::Atom_NET_WM_ICON));
1414 }
1415}
1416
1417void QXcbWindow::raise()
1418{
1419 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1420 const quint32 values[] = { XCB_STACK_MODE_ABOVE };
1421 xcb_configure_window(c: xcb_connection(), window: m_window, value_mask: mask, value_list: values);
1422}
1423
1424void QXcbWindow::lower()
1425{
1426 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1427 const quint32 values[] = { XCB_STACK_MODE_BELOW };
1428 xcb_configure_window(c: xcb_connection(), window: m_window, value_mask: mask, value_list: values);
1429}
1430
1431void QXcbWindow::propagateSizeHints()
1432{
1433 // update WM_NORMAL_HINTS
1434 xcb_size_hints_t hints;
1435 memset(s: &hints, c: 0, n: sizeof(hints));
1436
1437 const QRect rect = geometry();
1438 QWindowPrivate *win = qt_window_private(window: window());
1439
1440 if (!win->positionAutomatic)
1441 xcb_icccm_size_hints_set_position(hints: &hints, user_specified: true, x: rect.x(), y: rect.y());
1442 if (rect.width() < QWINDOWSIZE_MAX || rect.height() < QWINDOWSIZE_MAX)
1443 xcb_icccm_size_hints_set_size(hints: &hints, user_specified: true, width: rect.width(), height: rect.height());
1444
1445 /* Gravity describes how to interpret x and y values the next time
1446 window needs to be positioned on a screen.
1447 XCB_GRAVITY_STATIC : the left top corner of the client window
1448 XCB_GRAVITY_NORTH_WEST : the left top corner of the frame window */
1449 auto gravity = win->positionPolicy == QWindowPrivate::WindowFrameInclusive
1450 ? XCB_GRAVITY_NORTH_WEST : XCB_GRAVITY_STATIC;
1451
1452 xcb_icccm_size_hints_set_win_gravity(hints: &hints, win_gravity: gravity);
1453
1454 QSize minimumSize = windowMinimumSize();
1455 QSize maximumSize = windowMaximumSize();
1456 QSize baseSize = windowBaseSize();
1457 QSize sizeIncrement = windowSizeIncrement();
1458
1459 if (minimumSize.width() > 0 || minimumSize.height() > 0)
1460 xcb_icccm_size_hints_set_min_size(hints: &hints,
1461 min_width: qMin(XCOORD_MAX,b: minimumSize.width()),
1462 min_height: qMin(XCOORD_MAX,b: minimumSize.height()));
1463
1464 if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1465 xcb_icccm_size_hints_set_max_size(hints: &hints,
1466 max_width: qMin(XCOORD_MAX, b: maximumSize.width()),
1467 max_height: qMin(XCOORD_MAX, b: maximumSize.height()));
1468
1469 if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1470 if (!baseSize.isNull() && baseSize.isValid())
1471 xcb_icccm_size_hints_set_base_size(hints: &hints, base_width: baseSize.width(), base_height: baseSize.height());
1472 xcb_icccm_size_hints_set_resize_inc(hints: &hints, width_inc: sizeIncrement.width(), height_inc: sizeIncrement.height());
1473 }
1474
1475 xcb_icccm_set_wm_normal_hints(c: xcb_connection(), window: m_window, hints: &hints);
1476
1477 m_sizeHintsScaleFactor = QHighDpiScaling::factor(context: screen());
1478}
1479
1480void QXcbWindow::requestActivateWindow()
1481{
1482 /* Never activate embedded windows; doing that would prevent the container
1483 * to re-gain the keyboard focus later. */
1484 if (m_embedded) {
1485 QPlatformWindow::requestActivateWindow();
1486 return;
1487 }
1488
1489 {
1490 QMutexLocker locker(&m_mappedMutex);
1491 if (!m_mapped) {
1492 m_deferredActivation = true;
1493 return;
1494 }
1495 m_deferredActivation = false;
1496 }
1497
1498 updateNetWmUserTime(timestamp: connection()->time());
1499 QWindow *focusWindow = QGuiApplication::focusWindow();
1500 xcb_window_t current = XCB_NONE;
1501 if (focusWindow) {
1502 if (QPlatformWindow *pw = focusWindow->handle())
1503 current = pw->winId();
1504 }
1505
1506 if (window()->isTopLevel()
1507 && !(window()->flags() & Qt::X11BypassWindowManagerHint)
1508 && (!focusWindow || !window()->isAncestorOf(child: focusWindow))
1509 && connection()->wmSupport()->isSupportedByWM(atom: atom(atom: QXcbAtom::Atom_NET_ACTIVE_WINDOW))) {
1510 xcb_client_message_event_t event;
1511
1512 event.response_type = XCB_CLIENT_MESSAGE;
1513 event.format = 32;
1514 event.sequence = 0;
1515 event.window = m_window;
1516 event.type = atom(atom: QXcbAtom::Atom_NET_ACTIVE_WINDOW);
1517 event.data.data32[0] = 1;
1518 event.data.data32[1] = connection()->time();
1519 event.data.data32[2] = current;
1520 event.data.data32[3] = 0;
1521 event.data.data32[4] = 0;
1522
1523 xcb_send_event(c: xcb_connection(), propagate: 0, destination: xcbScreen()->root(),
1524 event_mask: XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
1525 event: (const char *)&event);
1526 } else {
1527 xcb_set_input_focus(c: xcb_connection(), revert_to: XCB_INPUT_FOCUS_PARENT, focus: m_window, time: connection()->time());
1528 }
1529
1530 connection()->sync();
1531}
1532
1533QSurfaceFormat QXcbWindow::format() const
1534{
1535 return m_format;
1536}
1537
1538QXcbWindow::WindowTypes QXcbWindow::wmWindowTypes() const
1539{
1540 WindowTypes result;
1541
1542 auto reply = Q_XCB_REPLY_UNCHECKED(xcb_get_property, xcb_connection(),
1543 0, m_window, atom(QXcbAtom::Atom_NET_WM_WINDOW_TYPE),
1544 XCB_ATOM_ATOM, 0, 1024);
1545 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
1546 const xcb_atom_t *types = static_cast<const xcb_atom_t *>(xcb_get_property_value(R: reply.get()));
1547 const xcb_atom_t *types_end = types + reply->length;
1548 for (; types != types_end; types++) {
1549 QXcbAtom::Atom type = connection()->qatom(atom: *types);
1550 switch (type) {
1551 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_NORMAL:
1552 result |= WindowType::Normal;
1553 break;
1554 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DESKTOP:
1555 result |= WindowType::Desktop;
1556 break;
1557 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DOCK:
1558 result |= WindowType::Dock;
1559 break;
1560 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_TOOLBAR:
1561 result |= WindowType::Toolbar;
1562 break;
1563 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_MENU:
1564 result |= WindowType::Menu;
1565 break;
1566 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_UTILITY:
1567 result |= WindowType::Utility;
1568 break;
1569 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_SPLASH:
1570 result |= WindowType::Splash;
1571 break;
1572 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DIALOG:
1573 result |= WindowType::Dialog;
1574 break;
1575 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DROPDOWN_MENU:
1576 result |= WindowType::DropDownMenu;
1577 break;
1578 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_POPUP_MENU:
1579 result |= WindowType::PopupMenu;
1580 break;
1581 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_TOOLTIP:
1582 result |= WindowType::Tooltip;
1583 break;
1584 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_NOTIFICATION:
1585 result |= WindowType::Notification;
1586 break;
1587 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_COMBO:
1588 result |= WindowType::Combo;
1589 break;
1590 case QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DND:
1591 result |= WindowType::Dnd;
1592 break;
1593 case QXcbAtom::Atom_KDE_NET_WM_WINDOW_TYPE_OVERRIDE:
1594 result |= WindowType::KdeOverride;
1595 break;
1596 default:
1597 break;
1598 }
1599 }
1600 }
1601 return result;
1602}
1603
1604void QXcbWindow::setWmWindowType(WindowTypes types, Qt::WindowFlags flags)
1605{
1606 QList<xcb_atom_t> atoms;
1607
1608 // manual selection 1 (these are never set by Qt and take precedence)
1609 if (types & WindowType::Normal)
1610 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_NORMAL));
1611 if (types & WindowType::Desktop)
1612 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DESKTOP));
1613 if (types & WindowType::Dock)
1614 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DOCK));
1615 if (types & WindowType::Notification)
1616 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_NOTIFICATION));
1617
1618 // manual selection 2 (Qt uses these during auto selection);
1619 if (types & WindowType::Utility)
1620 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_UTILITY));
1621 if (types & WindowType::Splash)
1622 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_SPLASH));
1623 if (types & WindowType::Dialog)
1624 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DIALOG));
1625 if (types & WindowType::Tooltip)
1626 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_TOOLTIP));
1627 if (types & WindowType::KdeOverride)
1628 atoms.append(t: atom(atom: QXcbAtom::Atom_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
1629
1630 // manual selection 3 (these can be set by Qt, but don't have a
1631 // corresponding Qt::WindowType). note that order of the *MENU
1632 // atoms is important
1633 if (types & WindowType::Menu)
1634 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_MENU));
1635 if (types & WindowType::DropDownMenu)
1636 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DROPDOWN_MENU));
1637 if (types & WindowType::PopupMenu)
1638 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_POPUP_MENU));
1639 if (types & WindowType::Toolbar)
1640 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_TOOLBAR));
1641 if (types & WindowType::Combo)
1642 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_COMBO));
1643 if (types & WindowType::Dnd)
1644 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DND));
1645
1646 // automatic selection
1647 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
1648 switch (type) {
1649 case Qt::Dialog:
1650 case Qt::Sheet:
1651 if (!(types & WindowType::Dialog))
1652 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_DIALOG));
1653 break;
1654 case Qt::Tool:
1655 case Qt::Drawer:
1656 if (!(types & WindowType::Utility))
1657 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_UTILITY));
1658 break;
1659 case Qt::ToolTip:
1660 if (!(types & WindowType::Tooltip))
1661 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_TOOLTIP));
1662 break;
1663 case Qt::SplashScreen:
1664 if (!(types & WindowType::Splash))
1665 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_SPLASH));
1666 break;
1667 default:
1668 break;
1669 }
1670
1671 if ((flags & Qt::FramelessWindowHint) && !(types & WindowType::KdeOverride)) {
1672 // override netwm type - quick and easy for KDE noborder
1673 atoms.append(t: atom(atom: QXcbAtom::Atom_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
1674 }
1675
1676 if (atoms.size() == 1 && atoms.first() == atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_NORMAL))
1677 atoms.clear();
1678 else
1679 atoms.append(t: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE_NORMAL));
1680
1681 if (atoms.isEmpty()) {
1682 xcb_delete_property(c: xcb_connection(), window: m_window, property: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE));
1683 } else {
1684 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: m_window,
1685 property: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_TYPE), type: XCB_ATOM_ATOM, format: 32,
1686 data_len: atoms.size(), data: atoms.constData());
1687 }
1688 xcb_flush(c: xcb_connection());
1689}
1690
1691void QXcbWindow::setWindowRole(const QString &role)
1692{
1693 QByteArray roleData = role.toLatin1();
1694 xcb_change_property(c: xcb_connection(), mode: XCB_PROP_MODE_REPLACE, window: m_window,
1695 property: atom(atom: QXcbAtom::AtomWM_WINDOW_ROLE), type: XCB_ATOM_STRING, format: 8,
1696 data_len: roleData.size(), data: roleData.constData());
1697}
1698
1699void QXcbWindow::setParentRelativeBackPixmap()
1700{
1701 const quint32 mask = XCB_CW_BACK_PIXMAP;
1702 const quint32 values[] = { XCB_BACK_PIXMAP_PARENT_RELATIVE };
1703 xcb_change_window_attributes(c: xcb_connection(), window: m_window, value_mask: mask, value_list: values);
1704}
1705
1706bool QXcbWindow::requestSystemTrayWindowDock()
1707{
1708 if (!connection()->systemTrayTracker())
1709 return false;
1710 connection()->systemTrayTracker()->requestSystemTrayWindowDock(window: m_window);
1711 return true;
1712}
1713
1714bool QXcbWindow::handleNativeEvent(xcb_generic_event_t *event)
1715{
1716 auto eventType = connection()->nativeInterface()->nativeEventType();
1717 qintptr result = 0; // Used only by MS Windows
1718 return QWindowSystemInterface::handleNativeEvent(window: window(), eventType, message: event, result: &result);
1719}
1720
1721void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1722{
1723 QRect rect(event->x, event->y, event->width, event->height);
1724 m_exposeRegion |= rect;
1725
1726 bool pending = true;
1727
1728 connection()->eventQueue()->peek(option: QXcbEventQueue::PeekConsumeMatchAndContinue,
1729 peeker: [this, &pending](xcb_generic_event_t *event, int type) {
1730 if (type != XCB_EXPOSE)
1731 return false;
1732 auto expose = reinterpret_cast<xcb_expose_event_t *>(event);
1733 if (expose->window != m_window)
1734 return false;
1735 if (expose->count == 0)
1736 pending = false;
1737 m_exposeRegion |= QRect(expose->x, expose->y, expose->width, expose->height);
1738 free(ptr: expose);
1739 return true;
1740 });
1741
1742 // if count is non-zero there are more expose events pending
1743 if (event->count == 0 || !pending) {
1744 QWindowSystemInterface::handleExposeEvent(window: window(), region: m_exposeRegion);
1745 m_exposeRegion = QRegion();
1746 }
1747}
1748
1749void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1750{
1751 if (event->format != 32)
1752 return;
1753
1754 if (event->type == atom(atom: QXcbAtom::AtomWM_PROTOCOLS)) {
1755 xcb_atom_t protocolAtom = event->data.data32[0];
1756 if (protocolAtom == atom(atom: QXcbAtom::AtomWM_DELETE_WINDOW)) {
1757 QWindowSystemInterface::handleCloseEvent(window: window());
1758 } else if (protocolAtom == atom(atom: QXcbAtom::AtomWM_TAKE_FOCUS)) {
1759 connection()->setTime(event->data.data32[1]);
1760 relayFocusToModalWindow();
1761 return;
1762 } else if (protocolAtom == atom(atom: QXcbAtom::Atom_NET_WM_PING)) {
1763 if (event->window == xcbScreen()->root())
1764 return;
1765
1766 xcb_client_message_event_t reply = *event;
1767
1768 reply.response_type = XCB_CLIENT_MESSAGE;
1769 reply.window = xcbScreen()->root();
1770
1771 xcb_send_event(c: xcb_connection(), propagate: 0, destination: xcbScreen()->root(),
1772 event_mask: XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
1773 event: (const char *)&reply);
1774 xcb_flush(c: xcb_connection());
1775 } else if (protocolAtom == atom(atom: QXcbAtom::Atom_NET_WM_SYNC_REQUEST)) {
1776 connection()->setTime(event->data.data32[1]);
1777 m_syncValue.lo = event->data.data32[2];
1778 m_syncValue.hi = event->data.data32[3];
1779 if (connection()->hasXSync())
1780 m_syncState = SyncReceived;
1781#ifndef QT_NO_WHATSTHIS
1782 } else if (protocolAtom == atom(atom: QXcbAtom::Atom_NET_WM_CONTEXT_HELP)) {
1783 QWindowSystemInterface::handleEnterWhatsThisEvent();
1784#endif
1785 } else {
1786 qCWarning(lcQpaXcb, "Unhandled WM_PROTOCOLS (%s)",
1787 connection()->atomName(protocolAtom).constData());
1788 }
1789#if QT_CONFIG(draganddrop)
1790 } else if (event->type == atom(atom: QXcbAtom::AtomXdndEnter)) {
1791 connection()->drag()->handleEnter(window: this, event);
1792 } else if (event->type == atom(atom: QXcbAtom::AtomXdndPosition)) {
1793 connection()->drag()->handlePosition(w: this, event);
1794 } else if (event->type == atom(atom: QXcbAtom::AtomXdndLeave)) {
1795 connection()->drag()->handleLeave(w: this, event);
1796 } else if (event->type == atom(atom: QXcbAtom::AtomXdndDrop)) {
1797 connection()->drag()->handleDrop(this, event);
1798#endif
1799 } else if (event->type == atom(atom: QXcbAtom::Atom_XEMBED)) {
1800 handleXEmbedMessage(event);
1801 } else if (event->type == atom(atom: QXcbAtom::Atom_NET_ACTIVE_WINDOW)) {
1802 doFocusIn();
1803 } else if (event->type == atom(atom: QXcbAtom::AtomMANAGER)
1804 || event->type == atom(atom: QXcbAtom::Atom_NET_WM_STATE)
1805 || event->type == atom(atom: QXcbAtom::AtomWM_CHANGE_STATE)) {
1806 // Ignore _NET_WM_STATE, MANAGER which are relate to tray icons
1807 // and other messages.
1808 } else if (event->type == atom(atom: QXcbAtom::Atom_COMPIZ_DECOR_PENDING)
1809 || event->type == atom(atom: QXcbAtom::Atom_COMPIZ_DECOR_REQUEST)
1810 || event->type == atom(atom: QXcbAtom::Atom_COMPIZ_DECOR_DELETE_PIXMAP)
1811 || event->type == atom(atom: QXcbAtom::Atom_COMPIZ_TOOLKIT_ACTION)
1812 || event->type == atom(atom: QXcbAtom::Atom_GTK_LOAD_ICONTHEMES)) {
1813 //silence the _COMPIZ and _GTK messages for now
1814 } else {
1815 qCWarning(lcQpaXcb) << "Unhandled client message: " << connection()->atomName(atom: event->type);
1816 }
1817}
1818
1819void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1820{
1821 bool fromSendEvent = (event->response_type & 0x80);
1822 QPoint pos(event->x, event->y);
1823 if (!QPlatformWindow::parent() && !fromSendEvent) {
1824 // Do not trust the position, query it instead.
1825 auto reply = Q_XCB_REPLY(xcb_translate_coordinates, xcb_connection(),
1826 xcb_window(), xcbScreen()->root(), 0, 0);
1827 if (reply) {
1828 pos.setX(reply->dst_x);
1829 pos.setY(reply->dst_y);
1830 }
1831 }
1832
1833 const QRect actualGeometry = QRect(pos, QSize(event->width, event->height));
1834 QPlatformScreen *newScreen = QPlatformWindow::parent() ? QPlatformWindow::parent()->screen() : screenForGeometry(newGeometry: actualGeometry);
1835 if (!newScreen)
1836 return;
1837
1838 QWindowSystemInterface::handleGeometryChange(window: window(), newRect: actualGeometry);
1839
1840 // QPlatformScreen::screen() is updated asynchronously, so we can't compare it
1841 // with the newScreen. Just send the WindowScreenChanged event and QGuiApplication
1842 // will make the comparison later.
1843 QWindowSystemInterface::handleWindowScreenChanged(window: window(), newScreen: newScreen->screen());
1844
1845 if (!qFuzzyCompare(p1: QHighDpiScaling::factor(context: newScreen), p2: m_sizeHintsScaleFactor))
1846 propagateSizeHints();
1847
1848 // Send the synthetic expose event on resize only when the window is shrunk,
1849 // because the "XCB_GRAVITY_NORTH_WEST" flag doesn't send it automatically.
1850 if (!m_oldWindowSize.isEmpty()
1851 && (actualGeometry.width() < m_oldWindowSize.width()
1852 || actualGeometry.height() < m_oldWindowSize.height())) {
1853 QWindowSystemInterface::handleExposeEvent(window: window(), region: QRegion(0, 0, actualGeometry.width(), actualGeometry.height()));
1854 }
1855 m_oldWindowSize = actualGeometry.size();
1856
1857 if (connection()->hasXSync() && m_syncState == SyncReceived)
1858 m_syncState = SyncAndConfigureReceived;
1859
1860 m_dirtyFrameMargins = true;
1861}
1862
1863bool QXcbWindow::isExposed() const
1864{
1865 return m_mapped;
1866}
1867
1868bool QXcbWindow::isEmbedded() const
1869{
1870 return m_embedded;
1871}
1872
1873QPoint QXcbWindow::mapToGlobal(const QPoint &pos) const
1874{
1875 if (!m_embedded)
1876 return QPlatformWindow::mapToGlobal(pos);
1877
1878 QPoint ret;
1879 auto reply = Q_XCB_REPLY(xcb_translate_coordinates, xcb_connection(),
1880 xcb_window(), xcbScreen()->root(),
1881 pos.x(), pos.y());
1882 if (reply) {
1883 ret.setX(reply->dst_x);
1884 ret.setY(reply->dst_y);
1885 }
1886
1887 return ret;
1888}
1889
1890QPoint QXcbWindow::mapFromGlobal(const QPoint &pos) const
1891{
1892 if (!m_embedded)
1893 return QPlatformWindow::mapFromGlobal(pos);
1894
1895 QPoint ret;
1896 auto reply = Q_XCB_REPLY(xcb_translate_coordinates, xcb_connection(),
1897 xcbScreen()->root(), xcb_window(),
1898 pos.x(), pos.y());
1899 if (reply) {
1900 ret.setX(reply->dst_x);
1901 ret.setY(reply->dst_y);
1902 }
1903
1904 return ret;
1905}
1906
1907void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1908{
1909 if (event->window == m_window) {
1910 m_mappedMutex.lock();
1911 m_mapped = true;
1912 const bool deferredActivation = m_deferredActivation;
1913 m_mappedMutex.unlock();
1914 if (deferredActivation)
1915 requestActivateWindow();
1916
1917 QWindowSystemInterface::handleExposeEvent(window: window(), region: QRect(QPoint(), geometry().size()));
1918 }
1919}
1920
1921void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1922{
1923 if (event->window == m_window) {
1924 m_mappedMutex.lock();
1925 m_mapped = false;
1926 m_mappedMutex.unlock();
1927 QWindowSystemInterface::handleExposeEvent(window: window(), region: QRegion());
1928 }
1929}
1930
1931void QXcbWindow::handleButtonPressEvent(int event_x, int event_y, int root_x, int root_y,
1932 int detail, Qt::KeyboardModifiers modifiers, xcb_timestamp_t timestamp,
1933 QEvent::Type type, Qt::MouseEventSource source)
1934{
1935 const bool isWheel = detail >= 4 && detail <= 7;
1936 if (!isWheel && window() != QGuiApplication::focusWindow()) {
1937 QWindow *w = static_cast<QWindowPrivate *>(QObjectPrivate::get(o: window()))->eventReceiver();
1938 if (!(w->flags() & (Qt::WindowDoesNotAcceptFocus | Qt::BypassWindowManagerHint))
1939 && w->type() != Qt::ToolTip
1940 && w->type() != Qt::Popup) {
1941 w->requestActivate();
1942 }
1943 }
1944
1945 updateNetWmUserTime(timestamp);
1946
1947 if (m_embedded && !m_trayIconWindow) {
1948 if (window() != QGuiApplication::focusWindow()) {
1949 const QXcbWindow *container = static_cast<const QXcbWindow *>(QPlatformWindow::parent());
1950 Q_ASSERT(container != nullptr);
1951
1952 sendXEmbedMessage(window: container->xcb_window(), message: XEMBED_REQUEST_FOCUS);
1953 }
1954 }
1955 QPoint local(event_x, event_y);
1956 QPoint global(root_x, root_y);
1957
1958 if (isWheel) {
1959 if (!connection()->isAtLeastXI21()) {
1960 QPoint angleDelta;
1961 if (detail == 4)
1962 angleDelta.setY(120);
1963 else if (detail == 5)
1964 angleDelta.setY(-120);
1965 else if (detail == 6)
1966 angleDelta.setX(120);
1967 else if (detail == 7)
1968 angleDelta.setX(-120);
1969 if (modifiers & Qt::AltModifier)
1970 angleDelta = angleDelta.transposed();
1971 QWindowSystemInterface::handleWheelEvent(window: window(), timestamp, local, global, pixelDelta: QPoint(), angleDelta, mods: modifiers);
1972 }
1973 return;
1974 }
1975
1976 connection()->setMousePressWindow(this);
1977
1978 handleMouseEvent(time: timestamp, local, global, modifiers, type, source);
1979}
1980
1981void QXcbWindow::handleButtonReleaseEvent(int event_x, int event_y, int root_x, int root_y,
1982 int detail, Qt::KeyboardModifiers modifiers, xcb_timestamp_t timestamp,
1983 QEvent::Type type, Qt::MouseEventSource source)
1984{
1985 QPoint local(event_x, event_y);
1986 QPoint global(root_x, root_y);
1987
1988 if (detail >= 4 && detail <= 7) {
1989 // mouse wheel, handled in handleButtonPressEvent()
1990 return;
1991 }
1992
1993 if (connection()->buttonState() == Qt::NoButton) {
1994 connection()->setMousePressWindow(nullptr);
1995 m_ignorePressedWindowOnMouseLeave = false;
1996 }
1997
1998 handleMouseEvent(time: timestamp, local, global, modifiers, type, source);
1999}
2000
2001static inline bool doCheckUnGrabAncestor(QXcbConnection *conn)
2002{
2003 /* Checking for XCB_NOTIFY_MODE_GRAB and XCB_NOTIFY_DETAIL_ANCESTOR prevents unwanted
2004 * enter/leave events on AwesomeWM on mouse button press. It also ignores duplicated
2005 * enter/leave events on Alt+Tab switching on some WMs with XInput2 events.
2006 * Without XInput2 events the (Un)grabAncestor cannot be checked when mouse button is
2007 * not pressed, otherwise (e.g. on Alt+Tab) it can igonre important enter/leave events.
2008 */
2009 if (conn) {
2010 const bool mouseButtonsPressed = (conn->buttonState() != Qt::NoButton);
2011 return mouseButtonsPressed || conn->hasXInput2();
2012 }
2013 return true;
2014}
2015
2016static bool ignoreLeaveEvent(quint8 mode, quint8 detail, QXcbConnection *conn)
2017{
2018 return ((doCheckUnGrabAncestor(conn)
2019 && mode == XCB_NOTIFY_MODE_GRAB && detail == XCB_NOTIFY_DETAIL_ANCESTOR)
2020 || (mode == XCB_NOTIFY_MODE_UNGRAB && detail == XCB_NOTIFY_DETAIL_INFERIOR)
2021 || detail == XCB_NOTIFY_DETAIL_VIRTUAL
2022 || detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL);
2023}
2024
2025static bool ignoreEnterEvent(quint8 mode, quint8 detail, QXcbConnection *conn = nullptr)
2026{
2027 return ((doCheckUnGrabAncestor(conn)
2028 && mode == XCB_NOTIFY_MODE_UNGRAB && detail == XCB_NOTIFY_DETAIL_ANCESTOR)
2029 || (mode != XCB_NOTIFY_MODE_NORMAL && mode != XCB_NOTIFY_MODE_UNGRAB)
2030 || detail == XCB_NOTIFY_DETAIL_VIRTUAL
2031 || detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL);
2032}
2033
2034void QXcbWindow::handleEnterNotifyEvent(int event_x, int event_y, int root_x, int root_y,
2035 quint8 mode, quint8 detail, xcb_timestamp_t timestamp)
2036{
2037 connection()->setTime(timestamp);
2038
2039 if (ignoreEnterEvent(mode, detail, conn: connection())
2040 || (connection()->mousePressWindow() && !m_ignorePressedWindowOnMouseLeave)) {
2041 return;
2042 }
2043
2044 // Updates scroll valuators, as user might have done some scrolling outside our X client.
2045 connection()->xi2UpdateScrollingDevices();
2046
2047 if (mode == XCB_NOTIFY_MODE_UNGRAB && connection()->queryMouseButtons() != Qt::NoButton)
2048 m_ignorePressedWindowOnMouseLeave = true;
2049
2050 const QPoint global = QPoint(root_x, root_y);
2051 const QPoint local(event_x, event_y);
2052 QWindowSystemInterface::handleEnterEvent(window: window(), local, global);
2053}
2054
2055void QXcbWindow::handleLeaveNotifyEvent(int root_x, int root_y,
2056 quint8 mode, quint8 detail, xcb_timestamp_t timestamp)
2057{
2058 connection()->setTime(timestamp);
2059
2060 QXcbWindow *mousePressWindow = connection()->mousePressWindow();
2061 if (ignoreLeaveEvent(mode, detail, conn: connection())
2062 || (mousePressWindow && !m_ignorePressedWindowOnMouseLeave)) {
2063 return;
2064 }
2065
2066 // check if enter event is buffered
2067 auto event = connection()->eventQueue()->peek(peeker: [](xcb_generic_event_t *event, int type) {
2068 if (type != XCB_ENTER_NOTIFY)
2069 return false;
2070 auto enter = reinterpret_cast<xcb_enter_notify_event_t *>(event);
2071 return !ignoreEnterEvent(mode: enter->mode, detail: enter->detail);
2072 });
2073 auto enter = reinterpret_cast<xcb_enter_notify_event_t *>(event);
2074 QXcbWindow *enterWindow = enter ? connection()->platformWindowFromId(id: enter->event) : nullptr;
2075
2076 if (enterWindow) {
2077 QPoint local(enter->event_x, enter->event_y);
2078 QPoint global = QPoint(root_x, root_y);
2079 QWindowSystemInterface::handleEnterLeaveEvent(enter: enterWindow->window(), leave: window(), local, global);
2080 } else {
2081 QWindowSystemInterface::handleLeaveEvent(window: window());
2082 if (m_ignorePressedWindowOnMouseLeave)
2083 connection()->setMousePressWindow(nullptr);
2084 }
2085
2086 free(ptr: enter);
2087}
2088
2089void QXcbWindow::handleMotionNotifyEvent(int event_x, int event_y, int root_x, int root_y,
2090 Qt::KeyboardModifiers modifiers, xcb_timestamp_t timestamp,
2091 QEvent::Type type, Qt::MouseEventSource source)
2092{
2093 QPoint local(event_x, event_y);
2094 QPoint global(root_x, root_y);
2095
2096 // "mousePressWindow" can be NULL i.e. if a window will be grabbed or unmapped, so set it again here.
2097 // Unset "mousePressWindow" when mouse button isn't pressed - in some cases the release event won't arrive.
2098 const bool isMouseButtonPressed = (connection()->buttonState() != Qt::NoButton);
2099 const bool hasMousePressWindow = (connection()->mousePressWindow() != nullptr);
2100 if (isMouseButtonPressed && !hasMousePressWindow)
2101 connection()->setMousePressWindow(this);
2102 else if (hasMousePressWindow && !isMouseButtonPressed)
2103 connection()->setMousePressWindow(nullptr);
2104
2105 handleMouseEvent(time: timestamp, local, global, modifiers, type, source);
2106}
2107
2108void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
2109{
2110 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(s: event->state);
2111 handleButtonPressEvent(event_x: event->event_x, event_y: event->event_y, root_x: event->root_x, root_y: event->root_y, detail: event->detail,
2112 modifiers, timestamp: event->time, type: QEvent::MouseButtonPress);
2113}
2114
2115void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
2116{
2117 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(s: event->state);
2118 handleButtonReleaseEvent(event_x: event->event_x, event_y: event->event_y, root_x: event->root_x, root_y: event->root_y, detail: event->detail,
2119 modifiers, timestamp: event->time, type: QEvent::MouseButtonRelease);
2120}
2121
2122void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
2123{
2124 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(s: event->state);
2125 handleMotionNotifyEvent(event_x: event->event_x, event_y: event->event_y, root_x: event->root_x, root_y: event->root_y, modifiers,
2126 timestamp: event->time, type: QEvent::MouseMove);
2127}
2128
2129static inline int fixed1616ToInt(xcb_input_fp1616_t val)
2130{
2131 return int(qreal(val) / 0x10000);
2132}
2133
2134#define qt_xcb_mask_is_set(ptr, event) (((unsigned char*)(ptr))[(event)>>3] & (1 << ((event) & 7)))
2135
2136void QXcbWindow::handleXIMouseEvent(xcb_ge_event_t *event, Qt::MouseEventSource source)
2137{
2138 QXcbConnection *conn = connection();
2139 auto *ev = reinterpret_cast<xcb_input_button_press_event_t *>(event);
2140
2141 if (ev->buttons_len > 0) {
2142 unsigned char *buttonMask = (unsigned char *) &ev[1];
2143 // There is a bug in the evdev driver which leads to receiving mouse events without
2144 // XIPointerEmulated being set: https://bugs.freedesktop.org/show_bug.cgi?id=98188
2145 // Filter them out by other attributes: when their source device is a touch screen
2146 // and the LMB is pressed.
2147 if (qt_xcb_mask_is_set(buttonMask, 1) && conn->isTouchScreen(id: ev->sourceid)) {
2148 if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled()))
2149 qCDebug(lcQpaXInput, "XI2 mouse event from touch device %d was ignored", ev->sourceid);
2150 return;
2151 }
2152 for (int i = 1; i <= 15; ++i)
2153 conn->setButtonState(button: conn->translateMouseButton(s: i), qt_xcb_mask_is_set(buttonMask, i));
2154 }
2155
2156 const Qt::KeyboardModifiers modifiers = conn->keyboard()->translateModifiers(s: ev->mods.effective);
2157 const int event_x = fixed1616ToInt(val: ev->event_x);
2158 const int event_y = fixed1616ToInt(val: ev->event_y);
2159 const int root_x = fixed1616ToInt(val: ev->root_x);
2160 const int root_y = fixed1616ToInt(val: ev->root_y);
2161
2162 conn->keyboard()->updateXKBStateFromXI(modInfo: &ev->mods, groupInfo: &ev->group);
2163
2164 const Qt::MouseButton button = conn->xiToQtMouseButton(b: ev->detail);
2165
2166 const char *sourceName = nullptr;
2167 if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled())) {
2168 const QMetaObject *metaObject = qt_getEnumMetaObject(source);
2169 const QMetaEnum me = metaObject->enumerator(index: metaObject->indexOfEnumerator(name: qt_getEnumName(source)));
2170 sourceName = me.valueToKey(value: source);
2171 }
2172
2173 switch (ev->event_type) {
2174 case XCB_INPUT_BUTTON_PRESS:
2175 if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled()))
2176 qCDebug(lcQpaXInputEvents, "XI2 mouse press, button %d, time %d, source %s", button, ev->time, sourceName);
2177 conn->setButtonState(button, down: true);
2178 handleButtonPressEvent(event_x, event_y, root_x, root_y, detail: ev->detail, modifiers, timestamp: ev->time, type: QEvent::MouseButtonPress, source);
2179 break;
2180 case XCB_INPUT_BUTTON_RELEASE:
2181 if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled()))
2182 qCDebug(lcQpaXInputEvents, "XI2 mouse release, button %d, time %d, source %s", button, ev->time, sourceName);
2183 conn->setButtonState(button, down: false);
2184 handleButtonReleaseEvent(event_x, event_y, root_x, root_y, detail: ev->detail, modifiers, timestamp: ev->time, type: QEvent::MouseButtonRelease, source);
2185 break;
2186 case XCB_INPUT_MOTION:
2187 if (Q_UNLIKELY(lcQpaXInputEvents().isDebugEnabled()))
2188 qCDebug(lcQpaXInputEvents, "XI2 mouse motion %d,%d, time %d, source %s", event_x, event_y, ev->time, sourceName);
2189 handleMotionNotifyEvent(event_x, event_y, root_x, root_y, modifiers, timestamp: ev->time, type: QEvent::MouseMove, source);
2190 break;
2191 default:
2192 qWarning() << "Unrecognized XI2 mouse event" << ev->event_type;
2193 break;
2194 }
2195}
2196
2197void QXcbWindow::handleXIEnterLeave(xcb_ge_event_t *event)
2198{
2199 auto *ev = reinterpret_cast<xcb_input_enter_event_t *>(event);
2200
2201 // Compare the window with current mouse grabber to prevent deliver events to any other windows.
2202 // If leave event occurs and the window is under mouse - allow to deliver the leave event.
2203 QXcbWindow *mouseGrabber = connection()->mouseGrabber();
2204 if (mouseGrabber && mouseGrabber != this
2205 && (ev->event_type != XCB_INPUT_LEAVE || QGuiApplicationPrivate::currentMouseWindow != window())) {
2206 return;
2207 }
2208
2209 const int root_x = fixed1616ToInt(val: ev->root_x);
2210 const int root_y = fixed1616ToInt(val: ev->root_y);
2211
2212 switch (ev->event_type) {
2213 case XCB_INPUT_ENTER: {
2214 const int event_x = fixed1616ToInt(val: ev->event_x);
2215 const int event_y = fixed1616ToInt(val: ev->event_y);
2216 qCDebug(lcQpaXInputEvents, "XI2 mouse enter %d,%d, mode %d, detail %d, time %d",
2217 event_x, event_y, ev->mode, ev->detail, ev->time);
2218 handleEnterNotifyEvent(event_x, event_y, root_x, root_y, mode: ev->mode, detail: ev->detail, timestamp: ev->time);
2219 break;
2220 }
2221 case XCB_INPUT_LEAVE:
2222 qCDebug(lcQpaXInputEvents, "XI2 mouse leave, mode %d, detail %d, time %d",
2223 ev->mode, ev->detail, ev->time);
2224 connection()->keyboard()->updateXKBStateFromXI(modInfo: &ev->mods, groupInfo: &ev->group);
2225 handleLeaveNotifyEvent(root_x, root_y, mode: ev->mode, detail: ev->detail, timestamp: ev->time);
2226 break;
2227 }
2228}
2229
2230QXcbWindow *QXcbWindow::toWindow() { return this; }
2231
2232void QXcbWindow::handleMouseEvent(xcb_timestamp_t time, const QPoint &local, const QPoint &global,
2233 Qt::KeyboardModifiers modifiers, QEvent::Type type, Qt::MouseEventSource source)
2234{
2235 m_lastPointerPosition = local;
2236 m_lastPointerGlobalPosition = global;
2237 connection()->setTime(time);
2238 Qt::MouseButton button = type == QEvent::MouseMove ? Qt::NoButton : connection()->button();
2239 QWindowSystemInterface::handleMouseEvent(window: window(), timestamp: time, local, global,
2240 state: connection()->buttonState(), button,
2241 type, mods: modifiers, source);
2242}
2243
2244void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
2245{
2246 handleEnterNotifyEvent(event_x: event->event_x, event_y: event->event_y, root_x: event->root_x, root_y: event->root_y, mode: event->mode, detail: event->detail, timestamp: event->time);
2247}
2248
2249void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
2250{
2251 handleLeaveNotifyEvent(root_x: event->root_x, root_y: event->root_y, mode: event->mode, detail: event->detail, timestamp: event->time);
2252}
2253
2254void QXcbWindow::handlePropertyNotifyEvent(const xcb_property_notify_event_t *event)
2255{
2256 connection()->setTime(event->time);
2257
2258 const bool propertyDeleted = event->state == XCB_PROPERTY_DELETE;
2259
2260 if (event->atom == atom(atom: QXcbAtom::Atom_NET_WM_STATE) || event->atom == atom(atom: QXcbAtom::AtomWM_STATE)) {
2261 if (propertyDeleted)
2262 return;
2263
2264 Qt::WindowStates newState = Qt::WindowNoState;
2265
2266 if (event->atom == atom(atom: QXcbAtom::AtomWM_STATE)) { // WM_STATE: Quick check for 'Minimize'.
2267 auto reply = Q_XCB_REPLY(xcb_get_property, xcb_connection(),
2268 0, m_window, atom(QXcbAtom::AtomWM_STATE),
2269 XCB_ATOM_ANY, 0, 1024);
2270 if (reply && reply->format == 32 && reply->type == atom(atom: QXcbAtom::AtomWM_STATE)) {
2271 const quint32 *data = (const quint32 *)xcb_get_property_value(R: reply.get());
2272 if (reply->length != 0)
2273 m_minimized = (data[0] == XCB_ICCCM_WM_STATE_ICONIC
2274 || (data[0] == XCB_ICCCM_WM_STATE_WITHDRAWN && m_minimized));
2275 }
2276 }
2277
2278 const NetWmStates states = netWmStates();
2279 // _NET_WM_STATE_HIDDEN should be set by the Window Manager to indicate that a window would
2280 // not be visible on the screen if its desktop/viewport were active and its coordinates were
2281 // within the screen bounds. The canonical example is that minimized windows should be in
2282 // the _NET_WM_STATE_HIDDEN state.
2283 if (m_minimized && (!connection()->wmSupport()->isSupportedByWM(atom: NetWmStateHidden)
2284 || states.testFlag(flag: NetWmStateHidden)))
2285 newState = Qt::WindowMinimized;
2286
2287 if (states & NetWmStateFullScreen)
2288 newState |= Qt::WindowFullScreen;
2289 if ((states & NetWmStateMaximizedHorz) && (states & NetWmStateMaximizedVert))
2290 newState |= Qt::WindowMaximized;
2291 // Send Window state, compress events in case other flags (modality, etc) are changed.
2292 if (m_lastWindowStateEvent != newState) {
2293 QWindowSystemInterface::handleWindowStateChanged(window: window(), newState);
2294 m_lastWindowStateEvent = newState;
2295 m_windowState = newState;
2296 if ((m_windowState & Qt::WindowMinimized) && connection()->mouseGrabber() == this)
2297 connection()->setMouseGrabber(nullptr);
2298 }
2299 return;
2300 } else if (event->atom == atom(atom: QXcbAtom::Atom_NET_FRAME_EXTENTS)) {
2301 m_dirtyFrameMargins = true;
2302 }
2303}
2304
2305void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *event)
2306{
2307 // Ignore focus events that are being sent only because the pointer is over
2308 // our window, even if the input focus is in a different window.
2309 if (event->detail == XCB_NOTIFY_DETAIL_POINTER)
2310 return;
2311
2312 connection()->focusInTimer().stop();
2313 doFocusIn();
2314}
2315
2316
2317void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *event)
2318{
2319 // Ignore focus events that are being sent only because the pointer is over
2320 // our window, even if the input focus is in a different window.
2321 if (event->detail == XCB_NOTIFY_DETAIL_POINTER)
2322 return;
2323 doFocusOut();
2324}
2325
2326void QXcbWindow::updateSyncRequestCounter()
2327{
2328 if (m_syncState != SyncAndConfigureReceived) {
2329 // window manager does not expect a sync event yet.
2330 return;
2331 }
2332 if (connection()->hasXSync() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
2333 xcb_sync_set_counter(c: xcb_connection(), counter: m_syncCounter, value: m_syncValue);
2334 xcb_flush(c: xcb_connection());
2335
2336 m_syncValue.lo = 0;
2337 m_syncValue.hi = 0;
2338 m_syncState = NoSyncNeeded;
2339 }
2340}
2341
2342const xcb_visualtype_t *QXcbWindow::createVisual()
2343{
2344 return xcbScreen() ? xcbScreen()->visualForFormat(format: m_format)
2345 : nullptr;
2346}
2347
2348bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
2349{
2350 if (grab && !connection()->canGrab())
2351 return false;
2352
2353 if (!grab) {
2354 xcb_ungrab_keyboard(c: xcb_connection(), time: XCB_TIME_CURRENT_TIME);
2355 return true;
2356 }
2357
2358 auto reply = Q_XCB_REPLY(xcb_grab_keyboard, xcb_connection(), false,
2359 m_window, XCB_TIME_CURRENT_TIME,
2360 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
2361 return reply && reply->status == XCB_GRAB_STATUS_SUCCESS;
2362}
2363
2364bool QXcbWindow::setMouseGrabEnabled(bool grab)
2365{
2366 if (!grab && connection()->mouseGrabber() == this)
2367 connection()->setMouseGrabber(nullptr);
2368
2369 if (grab && !connection()->canGrab())
2370 return false;
2371
2372 if (connection()->hasXInput2()) {
2373 bool result = connection()->xi2SetMouseGrabEnabled(w: m_window, grab);
2374 if (grab && result)
2375 connection()->setMouseGrabber(this);
2376 return result;
2377 }
2378
2379 if (!grab) {
2380 xcb_ungrab_pointer(c: xcb_connection(), time: XCB_TIME_CURRENT_TIME);
2381 return true;
2382 }
2383
2384 auto reply = Q_XCB_REPLY(xcb_grab_pointer, xcb_connection(),
2385 false, m_window,
2386 (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
2387 | XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW
2388 | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_POINTER_MOTION),
2389 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
2390 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
2391 XCB_TIME_CURRENT_TIME);
2392 bool result = reply && reply->status == XCB_GRAB_STATUS_SUCCESS;
2393 if (result)
2394 connection()->setMouseGrabber(this);
2395 return result;
2396}
2397
2398bool QXcbWindow::windowEvent(QEvent *event)
2399{
2400 switch (event->type()) {
2401 case QEvent::FocusIn:
2402 if (m_embedded && !m_trayIconWindow && !event->spontaneous()) {
2403 QFocusEvent *focusEvent = static_cast<QFocusEvent *>(event);
2404 switch (focusEvent->reason()) {
2405 case Qt::TabFocusReason:
2406 case Qt::BacktabFocusReason:
2407 {
2408 const QXcbWindow *container =
2409 static_cast<const QXcbWindow *>(QPlatformWindow::parent());
2410 sendXEmbedMessage(window: container->xcb_window(),
2411 message: focusEvent->reason() == Qt::TabFocusReason ?
2412 XEMBED_FOCUS_NEXT : XEMBED_FOCUS_PREV);
2413 event->accept();
2414 }
2415 break;
2416 default:
2417 break;
2418 }
2419 }
2420 break;
2421 default:
2422 break;
2423 }
2424 return QPlatformWindow::windowEvent(event);
2425}
2426
2427bool QXcbWindow::startSystemResize(Qt::Edges edges)
2428{
2429 return startSystemMoveResize(pos: m_lastPointerPosition, edges);
2430}
2431
2432bool QXcbWindow::startSystemMove()
2433{
2434 return startSystemMoveResize(pos: m_lastPointerPosition, edges: 16);
2435}
2436
2437bool QXcbWindow::startSystemMoveResize(const QPoint &pos, int edges)
2438{
2439 const xcb_atom_t moveResize = connection()->atom(qatom: QXcbAtom::Atom_NET_WM_MOVERESIZE);
2440 if (!connection()->wmSupport()->isSupportedByWM(atom: moveResize))
2441 return false;
2442
2443 // ### FIXME QTBUG-53389
2444 bool startedByTouch = connection()->startSystemMoveResizeForTouch(window: m_window, edges);
2445 if (startedByTouch) {
2446 const QString wmname = connection()->windowManagerName();
2447 if (wmname != "kwin"_L1 && wmname != "openbox"_L1) {
2448 qCDebug(lcQpaInputDevices) << "only KDE and OpenBox support startSystemMove/Resize which is triggered from touch events: XDG_CURRENT_DESKTOP="
2449 << qgetenv(varName: "XDG_CURRENT_DESKTOP");
2450 connection()->abortSystemMoveResize(window: m_window);
2451 return false;
2452 }
2453 // KWin, Openbox, AwesomeWM and Gnome have been tested to work with _NET_WM_MOVERESIZE.
2454 } else { // Started by mouse press.
2455 doStartSystemMoveResize(globalPos: mapToGlobal(pos), edges);
2456 }
2457
2458 return true;
2459}
2460
2461static uint qtEdgesToXcbMoveResizeDirection(Qt::Edges edges)
2462{
2463 if (edges == (Qt::TopEdge | Qt::LeftEdge))
2464 return 0;
2465 if (edges == Qt::TopEdge)
2466 return 1;
2467 if (edges == (Qt::TopEdge | Qt::RightEdge))
2468 return 2;
2469 if (edges == Qt::RightEdge)
2470 return 3;
2471 if (edges == (Qt::RightEdge | Qt::BottomEdge))
2472 return 4;
2473 if (edges == Qt::BottomEdge)
2474 return 5;
2475 if (edges == (Qt::BottomEdge | Qt::LeftEdge))
2476 return 6;
2477 if (edges == Qt::LeftEdge)
2478 return 7;
2479
2480 qWarning() << "Cannot convert " << edges << "to _NET_WM_MOVERESIZE direction.";
2481 return 0;
2482}
2483
2484void QXcbWindow::doStartSystemMoveResize(const QPoint &globalPos, int edges)
2485{
2486 qCDebug(lcQpaInputDevices) << "triggered system move or resize via sending _NET_WM_MOVERESIZE client message";
2487 const xcb_atom_t moveResize = connection()->atom(qatom: QXcbAtom::Atom_NET_WM_MOVERESIZE);
2488 xcb_client_message_event_t xev;
2489 xev.response_type = XCB_CLIENT_MESSAGE;
2490 xev.type = moveResize;
2491 xev.sequence = 0;
2492 xev.window = xcb_window();
2493 xev.format = 32;
2494 xev.data.data32[0] = globalPos.x();
2495 xev.data.data32[1] = globalPos.y();
2496 if (edges == 16)
2497 xev.data.data32[2] = 8; // move
2498 else
2499 xev.data.data32[2] = qtEdgesToXcbMoveResizeDirection(edges: Qt::Edges(edges));
2500 xev.data.data32[3] = XCB_BUTTON_INDEX_1;
2501 xev.data.data32[4] = 0;
2502 xcb_ungrab_pointer(c: connection()->xcb_connection(), XCB_CURRENT_TIME);
2503 xcb_send_event(c: connection()->xcb_connection(), propagate: false, destination: xcbScreen()->root(),
2504 event_mask: XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
2505 event: (const char *)&xev);
2506
2507 connection()->setDuringSystemMoveResize(true);
2508}
2509
2510// Sends an XEmbed message.
2511void QXcbWindow::sendXEmbedMessage(xcb_window_t window, quint32 message,
2512 quint32 detail, quint32 data1, quint32 data2)
2513{
2514 xcb_client_message_event_t event;
2515
2516 event.response_type = XCB_CLIENT_MESSAGE;
2517 event.format = 32;
2518 event.sequence = 0;
2519 event.window = window;
2520 event.type = atom(atom: QXcbAtom::Atom_XEMBED);
2521 event.data.data32[0] = connection()->time();
2522 event.data.data32[1] = message;
2523 event.data.data32[2] = detail;
2524 event.data.data32[3] = data1;
2525 event.data.data32[4] = data2;
2526 xcb_send_event(c: xcb_connection(), propagate: false, destination: window, event_mask: XCB_EVENT_MASK_NO_EVENT, event: (const char *)&event);
2527}
2528
2529static bool focusWindowChangeQueued(const QWindow *window)
2530{
2531 /* Check from window system event queue if the next queued activation
2532 * targets a window other than @window.
2533 */
2534 QWindowSystemInterfacePrivate::FocusWindowEvent *systemEvent =
2535 static_cast<QWindowSystemInterfacePrivate::FocusWindowEvent *>
2536 (QWindowSystemInterfacePrivate::peekWindowSystemEvent(t: QWindowSystemInterfacePrivate::FocusWindow));
2537 return systemEvent && systemEvent->focused != window;
2538}
2539
2540void QXcbWindow::handleXEmbedMessage(const xcb_client_message_event_t *event)
2541{
2542 connection()->setTime(event->data.data32[0]);
2543 switch (event->data.data32[1]) {
2544 case XEMBED_WINDOW_ACTIVATE:
2545 case XEMBED_WINDOW_DEACTIVATE:
2546 break;
2547 case XEMBED_EMBEDDED_NOTIFY:
2548 xcb_map_window(c: xcb_connection(), window: m_window);
2549 xcbScreen()->windowShown(window: this);
2550 break;
2551 case XEMBED_FOCUS_IN:
2552 connection()->focusInTimer().stop();
2553 Qt::FocusReason reason;
2554 switch (event->data.data32[2]) {
2555 case XEMBED_FOCUS_FIRST:
2556 reason = Qt::TabFocusReason;
2557 break;
2558 case XEMBED_FOCUS_LAST:
2559 reason = Qt::BacktabFocusReason;
2560 break;
2561 case XEMBED_FOCUS_CURRENT:
2562 default:
2563 reason = Qt::OtherFocusReason;
2564 break;
2565 }
2566 connection()->setFocusWindow(window());
2567 QWindowSystemInterface::handleFocusWindowChanged(window: window(), r: reason);
2568 break;
2569 case XEMBED_FOCUS_OUT:
2570 if (window() == QGuiApplication::focusWindow()
2571 && !focusWindowChangeQueued(window: window())) {
2572 connection()->setFocusWindow(nullptr);
2573 QWindowSystemInterface::handleFocusWindowChanged(window: nullptr);
2574 }
2575 break;
2576 }
2577}
2578
2579static inline xcb_rectangle_t qRectToXCBRectangle(const QRect &r)
2580{
2581 xcb_rectangle_t result;
2582 result.x = qMax(SHRT_MIN, b: r.x());
2583 result.y = qMax(SHRT_MIN, b: r.y());
2584 result.width = qMin(a: (int)USHRT_MAX, b: r.width());
2585 result.height = qMin(a: (int)USHRT_MAX, b: r.height());
2586 return result;
2587}
2588
2589void QXcbWindow::setOpacity(qreal level)
2590{
2591 if (!m_window)
2592 return;
2593
2594 quint32 value = qRound64(d: qBound(min: qreal(0), val: level, max: qreal(1)) * 0xffffffff);
2595
2596 xcb_change_property(c: xcb_connection(),
2597 mode: XCB_PROP_MODE_REPLACE,
2598 window: m_window,
2599 property: atom(atom: QXcbAtom::Atom_NET_WM_WINDOW_OPACITY),
2600 type: XCB_ATOM_CARDINAL,
2601 format: 32,
2602 data_len: 1,
2603 data: (uchar *)&value);
2604}
2605
2606QList<xcb_rectangle_t> qRegionToXcbRectangleList(const QRegion &region)
2607{
2608 QList<xcb_rectangle_t> rects;
2609 rects.reserve(asize: region.rectCount());
2610 for (const QRect &r : region)
2611 rects.push_back(t: qRectToXCBRectangle(r));
2612 return rects;
2613}
2614
2615void QXcbWindow::setMask(const QRegion &region)
2616{
2617 if (!connection()->hasXShape())
2618 return;
2619 if (region.isEmpty()) {
2620 xcb_shape_mask(c: connection()->xcb_connection(), operation: XCB_SHAPE_SO_SET,
2621 destination_kind: XCB_SHAPE_SK_BOUNDING, destination_window: xcb_window(), x_offset: 0, y_offset: 0, XCB_NONE);
2622 } else {
2623 const auto rects = qRegionToXcbRectangleList(region);
2624 xcb_shape_rectangles(c: connection()->xcb_connection(), operation: XCB_SHAPE_SO_SET,
2625 destination_kind: XCB_SHAPE_SK_BOUNDING, ordering: XCB_CLIP_ORDERING_UNSORTED,
2626 destination_window: xcb_window(), x_offset: 0, y_offset: 0, rectangles_len: rects.size(), rectangles: &rects[0]);
2627 }
2628}
2629
2630void QXcbWindow::setAlertState(bool enabled)
2631{
2632 if (m_alertState == enabled)
2633 return;
2634
2635 m_alertState = enabled;
2636
2637 setNetWmState(set: enabled, one: atom(atom: QXcbAtom::Atom_NET_WM_STATE_DEMANDS_ATTENTION));
2638}
2639
2640uint QXcbWindow::visualId() const
2641{
2642 return m_visualId;
2643}
2644
2645bool QXcbWindow::needsSync() const
2646{
2647 return m_syncState == SyncAndConfigureReceived;
2648}
2649
2650void QXcbWindow::postSyncWindowRequest()
2651{
2652 if (!m_pendingSyncRequest) {
2653 QXcbSyncWindowRequest *e = new QXcbSyncWindowRequest(this);
2654 m_pendingSyncRequest = e;
2655 QCoreApplication::postEvent(receiver: xcbScreen()->connection(), event: e);
2656 }
2657}
2658
2659QXcbScreen *QXcbWindow::xcbScreen() const
2660{
2661 return static_cast<QXcbScreen *>(screen());
2662}
2663
2664void QXcbWindow::setWindowTitle(const QXcbConnection *conn, xcb_window_t window, const QString &title)
2665{
2666 QString fullTitle = formatWindowTitle(title, separator: QString::fromUtf8(utf8: " \xe2\x80\x94 ")); // unicode character U+2014, EM DASH
2667 const QByteArray ba = std::move(fullTitle).toUtf8();
2668 xcb_change_property(c: conn->xcb_connection(),
2669 mode: XCB_PROP_MODE_REPLACE,
2670 window,
2671 property: conn->atom(qatom: QXcbAtom::Atom_NET_WM_NAME),
2672 type: conn->atom(qatom: QXcbAtom::AtomUTF8_STRING),
2673 format: 8,
2674 data_len: ba.size(),
2675 data: ba.constData());
2676
2677#if QT_CONFIG(xcb_xlib)
2678 Display *dpy = static_cast<Display *>(conn->xlib_display());
2679 XTextProperty *text = qstringToXTP(dpy, s: title);
2680 if (text)
2681 XSetWMName(dpy, window, text);
2682#endif
2683 xcb_flush(c: conn->xcb_connection());
2684}
2685
2686QString QXcbWindow::windowTitle(const QXcbConnection *conn, xcb_window_t window)
2687{
2688 const xcb_atom_t utf8Atom = conn->atom(qatom: QXcbAtom::AtomUTF8_STRING);
2689 auto reply = Q_XCB_REPLY_UNCHECKED(xcb_get_property, conn->xcb_connection(),
2690 false, window, conn->atom(QXcbAtom::Atom_NET_WM_NAME),
2691 utf8Atom, 0, 1024);
2692 if (reply && reply->format == 8 && reply->type == utf8Atom) {
2693 const char *name = reinterpret_cast<const char *>(xcb_get_property_value(R: reply.get()));
2694 return QString::fromUtf8(utf8: name, size: xcb_get_property_value_length(R: reply.get()));
2695 }
2696
2697 reply = Q_XCB_REPLY_UNCHECKED(xcb_get_property, conn->xcb_connection(),
2698 false, window, conn->atom(QXcbAtom::AtomWM_NAME),
2699 XCB_ATOM_STRING, 0, 1024);
2700 if (reply && reply->format == 8 && reply->type == XCB_ATOM_STRING) {
2701 const char *name = reinterpret_cast<const char *>(xcb_get_property_value(R: reply.get()));
2702 return QString::fromLatin1(str: name, size: xcb_get_property_value_length(R: reply.get()));
2703 }
2704
2705 return QString();
2706}
2707
2708QT_END_NAMESPACE
2709
2710

source code of qtbase/src/plugins/platforms/xcb/qxcbwindow.cpp