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