1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the plugins of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qxcbwindow.h"
47 #include "qxcbconnection.h"
48 #include "qxcbscreen.h"
50 #include "qxcbkeyboard.h"
51 #include "qxcbwmsupport.h"
53 #include <qplatformintegration_qpa.h>
56 #include "qdri2context.h"
59 // FIXME This workaround can be removed for xcb-icccm > 3.8
60 #define class class_name
61 #include <xcb/xcb_icccm.h>
63 #include <xcb/xfixes.h>
65 // xcb-icccm 3.8 support
66 #ifdef XCB_ICCCM_NUM_WM_SIZE_HINTS_ELEMENTS
67 #define xcb_get_wm_hints_reply xcb_icccm_get_wm_hints_reply
68 #define xcb_get_wm_hints xcb_icccm_get_wm_hints
69 #define xcb_get_wm_hints_unchecked xcb_icccm_get_wm_hints_unchecked
70 #define xcb_set_wm_hints xcb_icccm_set_wm_hints
71 #define xcb_set_wm_normal_hints xcb_icccm_set_wm_normal_hints
72 #define xcb_size_hints_set_base_size xcb_icccm_size_hints_set_base_size
73 #define xcb_size_hints_set_max_size xcb_icccm_size_hints_set_max_size
74 #define xcb_size_hints_set_min_size xcb_icccm_size_hints_set_min_size
75 #define xcb_size_hints_set_position xcb_icccm_size_hints_set_position
76 #define xcb_size_hints_set_resize_inc xcb_icccm_size_hints_set_resize_inc
77 #define xcb_size_hints_set_size xcb_icccm_size_hints_set_size
78 #define xcb_size_hints_set_win_gravity xcb_icccm_size_hints_set_win_gravity
79 #define xcb_wm_hints_set_iconic xcb_icccm_wm_hints_set_iconic
80 #define xcb_wm_hints_set_normal xcb_icccm_wm_hints_set_normal
81 #define xcb_wm_hints_set_input xcb_icccm_wm_hints_set_input
82 #define xcb_wm_hints_t xcb_icccm_wm_hints_t
83 #define XCB_WM_STATE_ICONIC XCB_ICCCM_WM_STATE_ICONIC
84 #define XCB_WM_STATE_WITHDRAWN XCB_ICCCM_WM_STATE_WITHDRAWN
87 #include <private/qguiapplication_p.h>
88 #include <private/qwindow_p.h>
90 #include <QtGui/QPlatformBackingStore>
91 #include <QtGui/QWindowSystemInterface>
97 #include <X11/Xutil.h>
100 #ifdef XCB_USE_XINPUT2_MAEMO
101 #include <X11/extensions/XInput2.h>
104 #if defined(XCB_USE_GLX)
105 #include "qglxintegration.h"
106 #include <QtPlatformSupport/private/qglxconvenience_p.h>
107 #elif defined(XCB_USE_EGL)
108 #include "qxcbeglsurface.h"
109 #include <QtPlatformSupport/private/qeglconvenience_p.h>
110 #include <QtPlatformSupport/private/qxlibeglintegration_p.h>
113 #define XCOORD_MAX 16383
115 //#ifdef NET_WM_STATE_DEBUG
119 // Returns true if we should set WM_TRANSIENT_FOR on \a w
120 static inline bool isTransient(const QWindow *w)
122 return w->windowType() == Qt::Dialog
123 || w->windowType() == Qt::Sheet
124 || w->windowType() == Qt::Tool
125 || w->windowType() == Qt::SplashScreen
126 || w->windowType() == Qt::ToolTip
127 || w->windowType() == Qt::Drawer
128 || w->windowType() == Qt::Popup;
131 static inline QImage::Format imageFormatForDepth(int depth)
134 case 32: return QImage::Format_ARGB32_Premultiplied;
135 case 24: return QImage::Format_RGB32;
136 case 16: return QImage::Format_RGB16;
137 default: return QImage::Format_Invalid;
141 QXcbWindow::QXcbWindow(QWindow *window)
142 : QPlatformWindow(window)
146 , m_transparent(false)
147 , m_deferredActivation(false)
148 , m_netWmUserTimeWindow(XCB_NONE)
149 #if defined(XCB_USE_EGL)
153 m_screen = static_cast<QXcbScreen *>(window->screen()->handle());
155 setConnection(m_screen->connection());
160 void QXcbWindow::create()
164 m_deferredExpose = false;
165 m_configureNotifyPending = true;
166 m_windowState = Qt::WindowNoState;
168 Qt::WindowType type = window()->windowType();
170 if (type == Qt::Desktop) {
171 m_window = m_screen->root();
172 m_depth = m_screen->screen()->root_depth;
173 m_imageFormat = imageFormatForDepth(m_depth);
174 connection()->addWindow(m_window, this);
178 const quint32 mask = XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK;
179 const quint32 values[] = {
180 // XCB_CW_BACK_PIXMAP
182 // XCB_CW_OVERRIDE_REDIRECT
183 type == Qt::Popup || type == Qt::ToolTip,
185 type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
187 XCB_EVENT_MASK_EXPOSURE
188 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
189 | XCB_EVENT_MASK_KEY_PRESS
190 | XCB_EVENT_MASK_KEY_RELEASE
191 | XCB_EVENT_MASK_BUTTON_PRESS
192 | XCB_EVENT_MASK_BUTTON_RELEASE
193 | XCB_EVENT_MASK_BUTTON_MOTION
194 | XCB_EVENT_MASK_ENTER_WINDOW
195 | XCB_EVENT_MASK_LEAVE_WINDOW
196 | XCB_EVENT_MASK_POINTER_MOTION
197 | XCB_EVENT_MASK_PROPERTY_CHANGE
198 | XCB_EVENT_MASK_FOCUS_CHANGE
201 QRect rect = window()->geometry();
202 QPlatformWindow::setGeometry(rect);
204 rect.setWidth(qBound(1, rect.width(), XCOORD_MAX));
205 rect.setHeight(qBound(1, rect.height(), XCOORD_MAX));
207 xcb_window_t xcb_parent_id = m_screen->root();
209 xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
211 m_format = window()->requestedFormat();
213 #if (defined(XCB_USE_GLX) || defined(XCB_USE_EGL)) && defined(XCB_USE_XLIB)
214 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL)
215 || m_format.hasAlpha())
217 #if defined(XCB_USE_GLX)
218 XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(m_screen), m_screen->screenNumber(), &m_format);
220 qFatal("Could not initialize GLX");
221 #elif defined(XCB_USE_EGL)
222 EGLDisplay eglDisplay = connection()->egl_display();
223 EGLConfig eglConfig = q_configFromGLFormat(eglDisplay, m_format, true);
224 m_format = q_glFormatFromConfig(eglDisplay, eglConfig);
226 VisualID id = QXlibEglIntegration::getCompatibleVisualId(DISPLAY_FROM_XCB(this), eglDisplay, eglConfig);
228 XVisualInfo visualInfoTemplate;
229 memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
230 visualInfoTemplate.visualid = id;
232 XVisualInfo *visualInfo;
233 int matchingCount = 0;
234 visualInfo = XGetVisualInfo(DISPLAY_FROM_XCB(this), VisualIDMask, &visualInfoTemplate, &matchingCount);
236 qFatal("Could not initialize EGL");
238 m_depth = visualInfo->depth;
239 m_imageFormat = imageFormatForDepth(m_depth);
240 Colormap cmap = XCreateColormap(DISPLAY_FROM_XCB(this), xcb_parent_id, visualInfo->visual, AllocNone);
242 XSetWindowAttributes a;
243 a.background_pixel = WhitePixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
244 a.border_pixel = BlackPixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
247 m_visualId = visualInfo->visualid;
249 m_window = XCreateWindow(DISPLAY_FROM_XCB(this), xcb_parent_id, rect.x(), rect.y(), rect.width(), rect.height(),
250 0, visualInfo->depth, InputOutput, visualInfo->visual,
251 CWBackPixel|CWBorderPixel|CWColormap, &a);
255 #endif //defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
257 m_window = xcb_generate_id(xcb_connection());
258 m_depth = m_screen->screen()->root_depth;
259 m_imageFormat = imageFormatForDepth(m_depth);
260 m_visualId = m_screen->screen()->root_visual;
262 Q_XCB_CALL(xcb_create_window(xcb_connection(),
263 XCB_COPY_FROM_PARENT, // depth -- same as root
264 m_window, // window id
265 xcb_parent_id, // parent window id
271 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
272 m_visualId, // visual
277 connection()->addWindow(m_window, this);
279 Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
281 propagateSizeHints();
283 xcb_atom_t properties[4];
284 int propertyCount = 0;
285 properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
286 properties[propertyCount++] = atom(QXcbAtom::WM_TAKE_FOCUS);
287 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_PING);
289 if (m_screen->syncRequestSupported())
290 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
292 if (window()->windowFlags() & Qt::WindowContextHelpButtonHint)
293 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
295 Q_XCB_CALL(xcb_change_property(xcb_connection(),
296 XCB_PROP_MODE_REPLACE,
298 atom(QXcbAtom::WM_PROTOCOLS),
306 if (m_screen->syncRequestSupported()) {
307 m_syncCounter = xcb_generate_id(xcb_connection());
308 Q_XCB_CALL(xcb_sync_create_counter(xcb_connection(), m_syncCounter, m_syncValue));
310 Q_XCB_CALL(xcb_change_property(xcb_connection(),
311 XCB_PROP_MODE_REPLACE,
313 atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
320 // set the PID to let the WM kill the application if unresponsive
322 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
323 atom(QXcbAtom::_NET_WM_PID), XCB_ATOM_CARDINAL, 32,
326 xcb_wm_hints_t hints;
327 memset(&hints, 0, sizeof(hints));
328 xcb_wm_hints_set_normal(&hints);
330 xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
332 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
334 xcb_window_t leader = m_screen->clientLeader();
335 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
336 atom(QXcbAtom::WM_CLIENT_LEADER), XCB_ATOM_WINDOW, 32,
339 #ifdef XCB_USE_XINPUT2_MAEMO
340 if (connection()->isUsingXInput2()) {
341 XIEventMask xieventmask;
342 uchar bitmask[2] = { 0, 0 };
344 xieventmask.deviceid = XIAllMasterDevices;
345 xieventmask.mask = bitmask;
346 xieventmask.mask_len = sizeof(bitmask);
348 XISetMask(bitmask, XI_ButtonPress);
349 XISetMask(bitmask, XI_ButtonRelease);
350 XISetMask(bitmask, XI_Motion);
352 XISelectEvents(DISPLAY_FROM_XCB(this), m_window, &xieventmask, 1);
356 setWindowFlags(window()->windowFlags());
357 setWindowTitle(window()->windowTitle());
358 setWindowState(window()->windowState());
360 if (window()->windowFlags() & Qt::WindowTransparentForInput)
361 setTransparentForMouseEvents(true);
363 connection()->drag()->dndEnable(this, true);
366 QXcbWindow::~QXcbWindow()
371 void QXcbWindow::destroy()
373 if (m_syncCounter && m_screen->syncRequestSupported())
374 Q_XCB_CALL(xcb_sync_destroy_counter(xcb_connection(), m_syncCounter));
376 if (m_netWmUserTimeWindow) {
377 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
378 // Some window managers, like metacity, do XSelectInput on the _NET_WM_USER_TIME_WINDOW window,
379 // without trapping BadWindow (which crashes when the user time window is destroyed).
380 connection()->sync();
381 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
382 m_netWmUserTimeWindow = XCB_NONE;
384 connection()->removeWindow(m_window);
385 Q_XCB_CALL(xcb_destroy_window(xcb_connection(), m_window));
389 #if defined(XCB_USE_EGL)
395 void QXcbWindow::setGeometry(const QRect &rect)
397 QPlatformWindow::setGeometry(rect);
399 propagateSizeHints();
401 const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
402 const qint32 values[] = {
403 qBound<qint32>(-XCOORD_MAX, rect.x(), XCOORD_MAX),
404 qBound<qint32>(-XCOORD_MAX, rect.y(), XCOORD_MAX),
405 qBound<qint32>(1, rect.width(), XCOORD_MAX),
406 qBound<qint32>(1, rect.height(), XCOORD_MAX),
409 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, reinterpret_cast<const quint32*>(values)));
411 xcb_flush(xcb_connection());
414 QMargins QXcbWindow::frameMargins() const
416 if (m_dirtyFrameMargins) {
417 xcb_window_t window = m_window;
418 xcb_window_t parent = m_window;
420 bool foundRoot = false;
422 const QVector<xcb_window_t> &virtualRoots =
423 connection()->wmSupport()->virtualRoots();
426 xcb_query_tree_cookie_t cookie = xcb_query_tree_unchecked(xcb_connection(), parent);
428 xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, NULL);
430 if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1) {
434 parent = reply->parent;
439 m_dirtyFrameMargins = false;
440 m_frameMargins = QMargins();
441 return m_frameMargins;
447 xcb_translate_coordinates_reply_t *reply =
448 xcb_translate_coordinates_reply(
450 xcb_translate_coordinates(xcb_connection(), window, parent, 0, 0),
454 offset = QPoint(reply->dst_x, reply->dst_y);
458 xcb_get_geometry_reply_t *geom =
459 xcb_get_geometry_reply(
461 xcb_get_geometry(xcb_connection(), parent),
466 // add the border_width for the window managers frame... some window managers
467 // do not use a border_width of zero for their frames, and if we the left and
468 // top strut, we ensure that pos() is absolutely correct. frameGeometry()
469 // will still be incorrect though... perhaps i should have foffset as well, to
470 // indicate the frame offset (equal to the border_width on X).
472 // -- copied from qwidget_x11.cpp
474 int left = offset.x() + geom->border_width;
475 int top = offset.y() + geom->border_width;
476 int right = geom->width + geom->border_width - geometry().width() - offset.x();
477 int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
479 m_frameMargins = QMargins(left, top, right, bottom);
484 m_dirtyFrameMargins = false;
487 return m_frameMargins;
490 void QXcbWindow::setVisible(bool visible)
498 void QXcbWindow::show()
500 if (window()->isTopLevel()) {
501 xcb_get_property_cookie_t cookie = xcb_get_wm_hints_unchecked(xcb_connection(), m_window);
503 xcb_wm_hints_t hints;
504 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, NULL);
506 if (window()->windowState() & Qt::WindowMinimized)
507 xcb_wm_hints_set_iconic(&hints);
509 xcb_wm_hints_set_normal(&hints);
511 xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
513 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
515 // update WM_NORMAL_HINTS
516 propagateSizeHints();
518 // update WM_TRANSIENT_FOR
519 if (window()->transientParent() && isTransient(window())) {
520 QXcbWindow *transientXcbParent = static_cast<QXcbWindow *>(window()->transientParent()->handle());
521 if (transientXcbParent) {
523 xcb_window_t parentWindow = transientXcbParent->xcb_window();
525 // todo: set transient for group (wm_client_leader) if no parent, a la qwidget_x11.cpp
526 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
527 XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 32,
532 // update _MOTIF_WM_HINTS
533 updateMotifWmHintsBeforeMap();
535 // update _NET_WM_STATE
536 updateNetWmStateBeforeMap();
539 if (connection()->time() != XCB_TIME_CURRENT_TIME)
540 updateNetWmUserTime(connection()->time());
542 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
543 xcb_flush(xcb_connection());
545 connection()->sync();
548 void QXcbWindow::hide()
550 Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
552 // send synthetic UnmapNotify event according to icccm 4.1.4
553 xcb_unmap_notify_event_t event;
554 event.response_type = XCB_UNMAP_NOTIFY;
555 event.event = m_screen->root();
556 event.window = m_window;
557 event.from_configure = false;
558 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, m_screen->root(),
559 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
561 xcb_flush(xcb_connection());
566 struct QtMotifWmHints {
567 quint32 flags, functions, decorations;
573 MWM_HINTS_FUNCTIONS = (1L << 0),
575 MWM_FUNC_ALL = (1L << 0),
576 MWM_FUNC_RESIZE = (1L << 1),
577 MWM_FUNC_MOVE = (1L << 2),
578 MWM_FUNC_MINIMIZE = (1L << 3),
579 MWM_FUNC_MAXIMIZE = (1L << 4),
580 MWM_FUNC_CLOSE = (1L << 5),
582 MWM_HINTS_DECORATIONS = (1L << 1),
584 MWM_DECOR_ALL = (1L << 0),
585 MWM_DECOR_BORDER = (1L << 1),
586 MWM_DECOR_RESIZEH = (1L << 2),
587 MWM_DECOR_TITLE = (1L << 3),
588 MWM_DECOR_MENU = (1L << 4),
589 MWM_DECOR_MINIMIZE = (1L << 5),
590 MWM_DECOR_MAXIMIZE = (1L << 6),
592 MWM_HINTS_INPUT_MODE = (1L << 2),
594 MWM_INPUT_MODELESS = 0L,
595 MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
596 MWM_INPUT_FULL_APPLICATION_MODAL = 3L
599 static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
601 QtMotifWmHints hints;
603 xcb_get_property_cookie_t get_cookie =
604 xcb_get_property_unchecked(c->xcb_connection(), 0, window, c->atom(QXcbAtom::_MOTIF_WM_HINTS),
605 c->atom(QXcbAtom::_MOTIF_WM_HINTS), 0, 20);
607 xcb_get_property_reply_t *reply =
608 xcb_get_property_reply(c->xcb_connection(), get_cookie, NULL);
610 if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
611 hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
614 hints.functions = MWM_FUNC_ALL;
615 hints.decorations = MWM_DECOR_ALL;
616 hints.input_mode = 0L;
625 static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
627 if (hints.flags != 0l) {
628 Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
629 XCB_PROP_MODE_REPLACE,
631 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
632 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
637 Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
641 void QXcbWindow::printNetWmState(const QVector<xcb_atom_t> &state)
643 printf("_NET_WM_STATE (%d): ", state.size());
644 for (int i = 0; i < state.size(); ++i) {
645 #define CHECK_WM_STATE(state_atom) \
646 if (state.at(i) == atom(QXcbAtom::state_atom))\
647 printf(#state_atom " ");
648 CHECK_WM_STATE(_NET_WM_STATE_ABOVE)
649 CHECK_WM_STATE(_NET_WM_STATE_BELOW)
650 CHECK_WM_STATE(_NET_WM_STATE_FULLSCREEN)
651 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_HORZ)
652 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_VERT)
653 CHECK_WM_STATE(_NET_WM_STATE_MODAL)
654 CHECK_WM_STATE(_NET_WM_STATE_STAYS_ON_TOP)
655 CHECK_WM_STATE(_NET_WM_STATE_DEMANDS_ATTENTION)
656 #undef CHECK_WM_STATE
661 QVector<xcb_atom_t> QXcbWindow::getNetWmState()
663 QVector<xcb_atom_t> result;
665 xcb_get_property_cookie_t get_cookie =
666 xcb_get_property_unchecked(xcb_connection(), 0, m_window, atom(QXcbAtom::_NET_WM_STATE),
667 XCB_ATOM_ATOM, 0, 1024);
669 xcb_get_property_reply_t *reply =
670 xcb_get_property_reply(xcb_connection(), get_cookie, NULL);
672 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
673 result.resize(reply->length);
675 memcpy(result.data(), xcb_get_property_value(reply), reply->length * sizeof(xcb_atom_t));
677 #ifdef NET_WM_STATE_DEBUG
678 printf("getting net wm state (%x)\n", m_window);
679 printNetWmState(result);
684 #ifdef NET_WM_STATE_DEBUG
685 printf("getting net wm state (%x), empty\n", m_window);
692 void QXcbWindow::setNetWmState(const QVector<xcb_atom_t> &atoms)
694 if (atoms.isEmpty()) {
695 Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
697 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
698 atom(QXcbAtom::_NET_WM_STATE), XCB_ATOM_ATOM, 32,
699 atoms.count(), atoms.constData()));
701 xcb_flush(xcb_connection());
705 Qt::WindowFlags QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
707 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
709 if (type == Qt::ToolTip)
710 flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
711 if (type == Qt::Popup)
712 flags |= Qt::X11BypassWindowManagerHint;
714 if (flags & Qt::WindowTransparentForInput) {
715 uint32_t mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_VISIBILITY_CHANGE
716 | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_RESIZE_REDIRECT
717 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
718 | XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_PROPERTY_CHANGE
719 | XCB_EVENT_MASK_COLOR_MAP_CHANGE | XCB_EVENT_MASK_OWNER_GRAB_BUTTON;
720 xcb_change_window_attributes(xcb_connection(), xcb_window(), XCB_CW_EVENT_MASK, &mask);
723 setNetWmWindowFlags(flags);
724 setMotifWindowFlags(flags);
726 setTransparentForMouseEvents(flags & Qt::WindowTransparentForInput);
727 updateDoesNotAcceptFocus(flags & Qt::WindowDoesNotAcceptFocus);
732 void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
734 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
736 QtMotifWmHints mwmhints;
738 mwmhints.functions = 0L;
739 mwmhints.decorations = 0;
740 mwmhints.input_mode = 0L;
741 mwmhints.status = 0L;
743 if (type != Qt::SplashScreen) {
744 mwmhints.flags |= MWM_HINTS_DECORATIONS;
746 bool customize = flags & Qt::CustomizeWindowHint;
747 if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
748 mwmhints.decorations |= MWM_DECOR_BORDER;
749 mwmhints.decorations |= MWM_DECOR_RESIZEH;
751 if (flags & Qt::WindowTitleHint)
752 mwmhints.decorations |= MWM_DECOR_TITLE;
754 if (flags & Qt::WindowSystemMenuHint)
755 mwmhints.decorations |= MWM_DECOR_MENU;
757 if (flags & Qt::WindowMinimizeButtonHint) {
758 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
759 mwmhints.functions |= MWM_FUNC_MINIMIZE;
762 if (flags & Qt::WindowMaximizeButtonHint) {
763 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
764 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
767 if (flags & Qt::WindowCloseButtonHint)
768 mwmhints.functions |= MWM_FUNC_CLOSE;
771 // if type == Qt::SplashScreen
772 mwmhints.decorations = MWM_DECOR_ALL;
775 if (mwmhints.functions != 0) {
776 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
777 mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
779 mwmhints.functions = MWM_FUNC_ALL;
782 if (!(flags & Qt::FramelessWindowHint)
783 && flags & Qt::CustomizeWindowHint
784 && flags & Qt::WindowTitleHint
786 (Qt::WindowMinimizeButtonHint
787 | Qt::WindowMaximizeButtonHint
788 | Qt::WindowCloseButtonHint)))
790 // a special case - only the titlebar without any button
791 mwmhints.flags = MWM_HINTS_FUNCTIONS;
792 mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
793 mwmhints.decorations = 0;
796 setMotifWmHints(connection(), m_window, mwmhints);
799 void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
801 xcb_client_message_event_t event;
803 event.response_type = XCB_CLIENT_MESSAGE;
805 event.window = m_window;
806 event.type = atom(QXcbAtom::_NET_WM_STATE);
807 event.data.data32[0] = set ? 1 : 0;
808 event.data.data32[1] = one;
809 event.data.data32[2] = two;
810 event.data.data32[3] = 0;
811 event.data.data32[4] = 0;
813 Q_XCB_CALL(xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
816 Qt::WindowState QXcbWindow::setWindowState(Qt::WindowState state)
818 if (state == m_windowState)
822 switch (m_windowState) {
823 case Qt::WindowMinimized:
824 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
826 case Qt::WindowMaximized:
827 changeNetWmState(false,
828 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
829 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
831 case Qt::WindowFullScreen:
832 changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
840 case Qt::WindowMinimized:
842 xcb_client_message_event_t event;
844 event.response_type = XCB_CLIENT_MESSAGE;
846 event.window = m_window;
847 event.type = atom(QXcbAtom::WM_CHANGE_STATE);
848 event.data.data32[0] = XCB_WM_STATE_ICONIC;
849 event.data.data32[1] = 0;
850 event.data.data32[2] = 0;
851 event.data.data32[3] = 0;
852 event.data.data32[4] = 0;
854 Q_XCB_CALL(xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
857 case Qt::WindowMaximized:
858 changeNetWmState(true,
859 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
860 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
862 case Qt::WindowFullScreen:
863 changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
865 case Qt::WindowNoState:
871 connection()->sync();
873 m_windowState = state;
874 return m_windowState;
877 void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
879 // in order of decreasing priority
880 QVector<uint> windowTypes;
882 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
887 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
891 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
894 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
896 case Qt::SplashScreen:
897 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
903 if (flags & Qt::FramelessWindowHint)
904 windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
906 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
908 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
909 atom(QXcbAtom::_NET_WM_WINDOW_TYPE), XCB_ATOM_ATOM, 32,
910 windowTypes.count(), windowTypes.constData()));
913 void QXcbWindow::updateMotifWmHintsBeforeMap()
915 QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
917 if (window()->windowModality() != Qt::NonModal) {
918 switch (window()->windowModality()) {
919 case Qt::WindowModal:
920 mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
922 case Qt::ApplicationModal:
924 mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
927 mwmhints.flags |= MWM_HINTS_INPUT_MODE;
929 mwmhints.input_mode = MWM_INPUT_MODELESS;
930 mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
933 if (window()->minimumSize() == window()->maximumSize()) {
934 // fixed size, remove the resize handle (since mwm/dtwm
935 // isn't smart enough to do it itself)
936 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
937 if (mwmhints.functions == MWM_FUNC_ALL) {
938 mwmhints.functions = MWM_FUNC_MOVE;
940 mwmhints.functions &= ~MWM_FUNC_RESIZE;
943 if (mwmhints.decorations == MWM_DECOR_ALL) {
944 mwmhints.flags |= MWM_HINTS_DECORATIONS;
945 mwmhints.decorations = (MWM_DECOR_BORDER
949 mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
953 if (window()->windowFlags() & Qt::WindowMinimizeButtonHint) {
954 mwmhints.flags |= MWM_HINTS_DECORATIONS;
955 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
956 mwmhints.functions |= MWM_FUNC_MINIMIZE;
958 if (window()->windowFlags() & Qt::WindowMaximizeButtonHint) {
959 mwmhints.flags |= MWM_HINTS_DECORATIONS;
960 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
961 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
963 if (window()->windowFlags() & Qt::WindowCloseButtonHint)
964 mwmhints.functions |= MWM_FUNC_CLOSE;
966 setMotifWmHints(connection(), m_window, mwmhints);
969 void QXcbWindow::updateNetWmStateBeforeMap()
971 QVector<xcb_atom_t> netWmState;
973 Qt::WindowFlags flags = window()->windowFlags();
974 if (flags & Qt::WindowStaysOnTopHint) {
975 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_ABOVE));
976 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP));
977 } else if (flags & Qt::WindowStaysOnBottomHint) {
978 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_BELOW));
981 if (window()->windowState() & Qt::WindowFullScreen) {
982 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
985 if (window()->windowState() & Qt::WindowMaximized) {
986 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ));
987 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
990 if (window()->windowModality() != Qt::NonModal) {
991 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MODAL));
994 setNetWmState(netWmState);
997 void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
999 xcb_window_t wid = m_window;
1001 const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1002 if (m_netWmUserTimeWindow || isSupportedByWM) {
1003 if (!m_netWmUserTimeWindow) {
1004 m_netWmUserTimeWindow = xcb_generate_id(xcb_connection());
1005 Q_XCB_CALL(xcb_create_window(xcb_connection(),
1006 XCB_COPY_FROM_PARENT, // depth -- same as root
1007 m_netWmUserTimeWindow, // window id
1008 m_window, // parent window id
1011 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
1012 m_visualId, // visual
1015 wid = m_netWmUserTimeWindow;
1016 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW),
1017 XCB_ATOM_WINDOW, 32, 1, &m_netWmUserTimeWindow);
1018 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME));
1019 } else if (!isSupportedByWM) {
1020 // WM no longer supports it, then we should remove the
1021 // _NET_WM_USER_TIME_WINDOW atom.
1022 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1023 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
1024 m_netWmUserTimeWindow = XCB_NONE;
1026 wid = m_netWmUserTimeWindow;
1029 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
1030 XCB_ATOM_CARDINAL, 32, 1, ×tamp);
1033 void QXcbWindow::setTransparentForMouseEvents(bool transparent)
1035 if (transparent == m_transparent)
1038 xcb_rectangle_t rectangle;
1040 xcb_rectangle_t *rect = 0;
1046 rectangle.width = geometry().width();
1047 rectangle.height = geometry().height();
1052 xcb_xfixes_region_t region = xcb_generate_id(xcb_connection());
1053 xcb_xfixes_create_region(xcb_connection(), region, nrect, rect);
1054 xcb_xfixes_set_window_shape_region_checked(xcb_connection(), m_window, XCB_SHAPE_SK_INPUT, 0, 0, region);
1055 xcb_xfixes_destroy_region(xcb_connection(), region);
1057 m_transparent = transparent;
1060 void QXcbWindow::updateDoesNotAcceptFocus(bool doesNotAcceptFocus)
1062 xcb_get_property_cookie_t cookie = xcb_get_wm_hints_unchecked(xcb_connection(), m_window);
1064 xcb_wm_hints_t hints;
1065 if (!xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, NULL)) {
1069 xcb_wm_hints_set_input(&hints, !doesNotAcceptFocus);
1070 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
1073 WId QXcbWindow::winId() const
1078 void QXcbWindow::setParent(const QPlatformWindow *parent)
1080 // re-create for compatibility
1083 QPoint topLeft = geometry().topLeft();
1085 xcb_window_t xcb_parent_id = parent ? static_cast<const QXcbWindow *>(parent)->xcb_window() : m_screen->root();
1086 Q_XCB_CALL(xcb_reparent_window(xcb_connection(), xcb_window(), xcb_parent_id, topLeft.x(), topLeft.y()));
1089 void QXcbWindow::setWindowTitle(const QString &title)
1091 QByteArray ba = title.toUtf8();
1092 Q_XCB_CALL(xcb_change_property(xcb_connection(),
1093 XCB_PROP_MODE_REPLACE,
1095 atom(QXcbAtom::_NET_WM_NAME),
1096 atom(QXcbAtom::UTF8_STRING),
1102 void QXcbWindow::raise()
1104 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1105 const quint32 values[] = { XCB_STACK_MODE_ABOVE };
1106 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1109 void QXcbWindow::lower()
1111 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1112 const quint32 values[] = { XCB_STACK_MODE_BELOW };
1113 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1116 void QXcbWindow::propagateSizeHints()
1118 // update WM_NORMAL_HINTS
1119 xcb_size_hints_t hints;
1120 memset(&hints, 0, sizeof(hints));
1122 QRect rect = geometry();
1124 QWindow *win = window();
1126 xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
1127 xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
1128 xcb_size_hints_set_win_gravity(&hints, qt_window_private(win)->positionPolicy == QWindowPrivate::WindowFrameInclusive
1129 ? XCB_GRAVITY_NORTH_WEST : XCB_GRAVITY_STATIC);
1131 QSize minimumSize = win->minimumSize();
1132 QSize maximumSize = win->maximumSize();
1133 QSize baseSize = win->baseSize();
1134 QSize sizeIncrement = win->sizeIncrement();
1136 if (minimumSize.width() > 0 || minimumSize.height() > 0)
1137 xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1139 if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1140 xcb_size_hints_set_max_size(&hints,
1141 qMin(XCOORD_MAX, maximumSize.width()),
1142 qMin(XCOORD_MAX, maximumSize.height()));
1144 if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1145 xcb_size_hints_set_base_size(&hints, baseSize.width(), baseSize.height());
1146 xcb_size_hints_set_resize_inc(&hints, sizeIncrement.width(), sizeIncrement.height());
1149 xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1152 void QXcbWindow::requestActivateWindow()
1155 m_deferredActivation = true;
1158 m_deferredActivation = false;
1160 updateNetWmUserTime(connection()->time());
1162 if (window()->isTopLevel()
1163 && connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_ACTIVE_WINDOW))) {
1164 xcb_client_message_event_t event;
1166 event.response_type = XCB_CLIENT_MESSAGE;
1168 event.window = m_window;
1169 event.type = atom(QXcbAtom::_NET_ACTIVE_WINDOW);
1170 event.data.data32[0] = 1;
1171 event.data.data32[1] = connection()->time();
1172 QWindow *focusWindow = QGuiApplication::focusWindow();
1173 event.data.data32[2] = focusWindow ? focusWindow->winId() : XCB_NONE;
1174 event.data.data32[3] = 0;
1175 event.data.data32[4] = 0;
1177 Q_XCB_CALL(xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
1179 Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1182 connection()->sync();
1185 #if XCB_USE_MAEMO_WINDOW_PROPERTIES
1186 void QXcbWindow::setOrientation(Qt::ScreenOrientation orientation)
1189 switch (orientation) {
1190 case Qt::PortraitOrientation: angle = 270; break;
1191 case Qt::LandscapeOrientation: angle = 0; break;
1192 case Qt::InvertedPortraitOrientation: angle = 90; break;
1193 case Qt::InvertedLandscapeOrientation: angle = 180; break;
1194 case Qt::PrimaryOrientation: break;
1196 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
1197 atom(QXcbAtom::MeegoTouchOrientationAngle), XCB_ATOM_CARDINAL, 32,
1202 QSurfaceFormat QXcbWindow::format() const
1204 // ### return actual format
1208 #if defined(XCB_USE_EGL)
1209 QXcbEGLSurface *QXcbWindow::eglSurface() const
1211 if (!m_eglSurface) {
1212 EGLDisplay display = connection()->egl_display();
1213 EGLConfig config = q_configFromGLFormat(display, window()->requestedFormat(), true);
1214 EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)m_window, 0);
1216 m_eglSurface = new QXcbEGLSurface(display, surface);
1219 return m_eglSurface;
1223 void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1225 QRect rect(event->x, event->y, event->width, event->height);
1227 if (m_exposeRegion.isEmpty())
1228 m_exposeRegion = rect;
1230 m_exposeRegion |= rect;
1232 // if count is non-zero there are more expose events pending
1233 if (event->count == 0) {
1234 QWindowSystemInterface::handleExposeEvent(window(), m_exposeRegion);
1235 m_exposeRegion = QRegion();
1239 void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1241 if (event->format != 32)
1244 if (event->type == atom(QXcbAtom::WM_PROTOCOLS)) {
1245 if (event->data.data32[0] == atom(QXcbAtom::WM_DELETE_WINDOW)) {
1246 QWindowSystemInterface::handleCloseEvent(window());
1247 } else if (event->data.data32[0] == atom(QXcbAtom::WM_TAKE_FOCUS)) {
1248 connection()->setTime(event->data.data32[1]);
1249 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_PING)) {
1250 xcb_client_message_event_t reply = *event;
1252 reply.response_type = XCB_CLIENT_MESSAGE;
1253 reply.window = m_screen->root();
1255 xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&reply);
1256 xcb_flush(xcb_connection());
1257 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_SYNC_REQUEST)) {
1258 connection()->setTime(event->data.data32[1]);
1259 m_syncValue.lo = event->data.data32[2];
1260 m_syncValue.hi = event->data.data32[3];
1262 qWarning() << "QXcbWindow: Unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1264 } else if (event->type == atom(QXcbAtom::XdndEnter)) {
1265 connection()->drag()->handleEnter(window(), event);
1266 } else if (event->type == atom(QXcbAtom::XdndPosition)) {
1267 connection()->drag()->handlePosition(window(), event);
1268 } else if (event->type == atom(QXcbAtom::XdndLeave)) {
1269 connection()->drag()->handleLeave(window(), event);
1270 } else if (event->type == atom(QXcbAtom::XdndDrop)) {
1271 connection()->drag()->handleDrop(window(), event);
1272 } else if (event->type == atom(QXcbAtom::_XEMBED)) { // QSystemTrayIcon
1274 qWarning() << "QXcbWindow: Unhandled client message:" << connection()->atomName(event->type);
1278 void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1280 bool fromSendEvent = (event->response_type & 0x80);
1281 QPoint pos(event->x, event->y);
1282 if (!parent() && !fromSendEvent) {
1283 // Do not trust the position, query it instead.
1284 xcb_translate_coordinates_cookie_t cookie = xcb_translate_coordinates(xcb_connection(), xcb_window(),
1285 m_screen->root(), 0, 0);
1286 xcb_translate_coordinates_reply_t *reply = xcb_translate_coordinates_reply(xcb_connection(), cookie, NULL);
1288 pos.setX(reply->dst_x);
1289 pos.setY(reply->dst_y);
1294 QRect rect(pos, QSize(event->width, event->height));
1296 QPlatformWindow::setGeometry(rect);
1297 QWindowSystemInterface::handleGeometryChange(window(), rect);
1299 m_configureNotifyPending = false;
1301 if (m_deferredExpose) {
1302 m_deferredExpose = false;
1303 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
1306 m_dirtyFrameMargins = true;
1310 static_cast<QDri2Context *>(m_context)->resize(rect.size());
1314 bool QXcbWindow::isExposed() const
1319 void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1321 if (event->window == m_window) {
1323 if (m_deferredActivation)
1324 requestActivateWindow();
1325 if (m_configureNotifyPending)
1326 m_deferredExpose = true;
1328 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
1332 void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1334 if (event->window == m_window) {
1336 QWindowSystemInterface::handleExposeEvent(window(), QRegion());
1340 static Qt::MouseButtons translateMouseButtons(int s)
1342 Qt::MouseButtons ret = 0;
1343 if (s & XCB_BUTTON_MASK_1)
1344 ret |= Qt::LeftButton;
1345 if (s & XCB_BUTTON_MASK_2)
1346 ret |= Qt::MidButton;
1347 if (s & XCB_BUTTON_MASK_3)
1348 ret |= Qt::RightButton;
1352 static Qt::MouseButton translateMouseButton(xcb_button_t s)
1355 case 1: return Qt::LeftButton;
1356 case 2: return Qt::MidButton;
1357 case 3: return Qt::RightButton;
1358 // Button values 4-7 were already handled as Wheel events, and won't occur here.
1359 case 8: return Qt::BackButton; // Also known as Qt::ExtraButton1
1360 case 9: return Qt::ForwardButton; // Also known as Qt::ExtraButton2
1361 case 10: return Qt::ExtraButton3;
1362 case 11: return Qt::ExtraButton4;
1363 case 12: return Qt::ExtraButton5;
1364 case 13: return Qt::ExtraButton6;
1365 case 14: return Qt::ExtraButton7;
1366 case 15: return Qt::ExtraButton8;
1367 case 16: return Qt::ExtraButton9;
1368 case 17: return Qt::ExtraButton10;
1369 case 18: return Qt::ExtraButton11;
1370 case 19: return Qt::ExtraButton12;
1371 case 20: return Qt::ExtraButton13;
1372 case 21: return Qt::ExtraButton14;
1373 case 22: return Qt::ExtraButton15;
1374 case 23: return Qt::ExtraButton16;
1375 case 24: return Qt::ExtraButton17;
1376 case 25: return Qt::ExtraButton18;
1377 case 26: return Qt::ExtraButton19;
1378 case 27: return Qt::ExtraButton20;
1379 case 28: return Qt::ExtraButton21;
1380 case 29: return Qt::ExtraButton22;
1381 case 30: return Qt::ExtraButton23;
1382 case 31: return Qt::ExtraButton24;
1383 default: return Qt::NoButton;
1387 void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1389 updateNetWmUserTime(event->time);
1391 QPoint local(event->event_x, event->event_y);
1392 QPoint global(event->root_x, event->root_y);
1394 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1396 if (event->detail >= 4 && event->detail <= 7) {
1397 // Logic borrowed from qapplication_x11.cpp
1398 int delta = 120 * ((event->detail == 4 || event->detail == 6) ? 1 : -1);
1399 bool hor = (((event->detail == 4 || event->detail == 5)
1400 && (modifiers & Qt::AltModifier))
1401 || (event->detail == 6 || event->detail == 7));
1403 QWindowSystemInterface::handleWheelEvent(window(), event->time,
1404 local, global, delta, hor ? Qt::Horizontal : Qt::Vertical, modifiers);
1408 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1411 void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1413 QPoint local(event->event_x, event->event_y);
1414 QPoint global(event->root_x, event->root_y);
1415 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1417 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1420 void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
1422 QPoint local(event->event_x, event->event_y);
1423 QPoint global(event->root_x, event->root_y);
1424 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1426 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1429 void QXcbWindow::handleMouseEvent(xcb_button_t detail, uint16_t state, xcb_timestamp_t time, const QPoint &local, const QPoint &global, Qt::KeyboardModifiers modifiers)
1431 connection()->setTime(time);
1433 Qt::MouseButtons buttons = translateMouseButtons(state);
1434 Qt::MouseButton button = translateMouseButton(detail);
1436 buttons ^= button; // X event uses state *before*, Qt uses state *after*
1438 QWindowSystemInterface::handleMouseEvent(window(), time, local, global, buttons, modifiers);
1441 void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1443 connection()->setTime(event->time);
1445 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1446 || event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1447 || event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1452 QWindowSystemInterface::handleEnterEvent(window());
1455 void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1457 connection()->setTime(event->time);
1459 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1460 || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)
1465 QWindowSystemInterface::handleLeaveEvent(window());
1468 void QXcbWindow::handlePropertyNotifyEvent(const xcb_property_notify_event_t *event)
1470 connection()->setTime(event->time);
1472 bool propertyDeleted = event->state == XCB_PROPERTY_DELETE;
1474 if (event->atom == atom(QXcbAtom::_NET_WM_STATE) || event->atom == atom(QXcbAtom::WM_STATE)) {
1475 if (propertyDeleted)
1478 xcb_get_property_cookie_t get_cookie =
1479 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::WM_STATE),
1480 XCB_ATOM_ANY, 0, 1024);
1482 xcb_get_property_reply_t *reply =
1483 xcb_get_property_reply(xcb_connection(), get_cookie, NULL);
1485 xcb_atom_t wm_state = XCB_WM_STATE_WITHDRAWN;
1486 if (reply && reply->format == 32 && reply->type == atom(QXcbAtom::WM_STATE)) {
1487 if (reply->length != 0)
1488 wm_state = ((long *)xcb_get_property_value(reply))[0];
1492 QVector<xcb_atom_t> netWmState = getNetWmState();
1494 bool maximized = netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ))
1495 && netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
1496 bool fullscreen = netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
1498 Qt::WindowState state = Qt::WindowNoState;
1499 if (wm_state == XCB_WM_STATE_ICONIC)
1500 state = Qt::WindowMinimized;
1502 state = Qt::WindowMaximized;
1503 else if (fullscreen)
1504 state = Qt::WindowFullScreen;
1506 QWindowSystemInterface::handleWindowStateChanged(window(), state);
1510 void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1512 QWindowSystemInterface::handleWindowActivated(window());
1515 static bool focusInPeeker(xcb_generic_event_t *event)
1518 // FocusIn event is not in the queue, proceed with FocusOut normally.
1519 QWindowSystemInterface::handleWindowActivated(0);
1522 uint response_type = event->response_type & ~0x80;
1523 return response_type == XCB_FOCUS_IN;
1526 void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1528 // Do not set the active window to 0 if there is a FocusIn coming.
1529 // There is however no equivalent for XPutBackEvent so register a
1530 // callback for QXcbConnection instead.
1531 connection()->addPeekFunc(focusInPeeker);
1534 void QXcbWindow::updateSyncRequestCounter()
1536 if (m_screen->syncRequestSupported() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
1537 Q_XCB_CALL(xcb_sync_set_counter(xcb_connection(), m_syncCounter, m_syncValue));
1538 xcb_flush(xcb_connection());
1539 connection()->sync();
1546 bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1549 xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1552 xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
1553 m_window, XCB_TIME_CURRENT_TIME,
1554 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1555 xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, NULL);
1556 bool result = !(!reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1561 bool QXcbWindow::setMouseGrabEnabled(bool grab)
1564 xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1567 xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
1568 (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
1569 | XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW
1570 | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_POINTER_MOTION),
1571 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
1572 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
1573 XCB_TIME_CURRENT_TIME);
1574 xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, NULL);
1575 bool result = !(!reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1580 void QXcbWindow::setCursor(xcb_cursor_t cursor)
1582 xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1583 xcb_flush(xcb_connection());