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"
46 #include <QtGui/QIcon>
48 #include "qxcbconnection.h"
49 #include "qxcbscreen.h"
51 #include "qxcbkeyboard.h"
52 #include "qxcbwmsupport.h"
53 #include "qxcbimage.h"
55 #include <qpa/qplatformintegration.h>
58 #include "qdri2context.h"
61 // FIXME This workaround can be removed for xcb-icccm > 3.8
62 #define class class_name
63 #include <xcb/xcb_icccm.h>
65 #include <xcb/xfixes.h>
67 // xcb-icccm 3.8 support
68 #ifdef XCB_ICCCM_NUM_WM_SIZE_HINTS_ELEMENTS
69 #define xcb_get_wm_hints_reply xcb_icccm_get_wm_hints_reply
70 #define xcb_get_wm_hints xcb_icccm_get_wm_hints
71 #define xcb_get_wm_hints_unchecked xcb_icccm_get_wm_hints_unchecked
72 #define xcb_set_wm_hints xcb_icccm_set_wm_hints
73 #define xcb_set_wm_normal_hints xcb_icccm_set_wm_normal_hints
74 #define xcb_size_hints_set_base_size xcb_icccm_size_hints_set_base_size
75 #define xcb_size_hints_set_max_size xcb_icccm_size_hints_set_max_size
76 #define xcb_size_hints_set_min_size xcb_icccm_size_hints_set_min_size
77 #define xcb_size_hints_set_position xcb_icccm_size_hints_set_position
78 #define xcb_size_hints_set_resize_inc xcb_icccm_size_hints_set_resize_inc
79 #define xcb_size_hints_set_size xcb_icccm_size_hints_set_size
80 #define xcb_size_hints_set_win_gravity xcb_icccm_size_hints_set_win_gravity
81 #define xcb_wm_hints_set_iconic xcb_icccm_wm_hints_set_iconic
82 #define xcb_wm_hints_set_normal xcb_icccm_wm_hints_set_normal
83 #define xcb_wm_hints_set_input xcb_icccm_wm_hints_set_input
84 #define xcb_wm_hints_t xcb_icccm_wm_hints_t
85 #define XCB_WM_STATE_ICONIC XCB_ICCCM_WM_STATE_ICONIC
86 #define XCB_WM_STATE_WITHDRAWN XCB_ICCCM_WM_STATE_WITHDRAWN
89 #include <private/qguiapplication_p.h>
90 #include <private/qwindow_p.h>
92 #include <qpa/qplatformbackingstore.h>
93 #include <QtGui/QWindowSystemInterface>
99 #include <X11/Xutil.h>
102 #ifdef XCB_USE_XINPUT2_MAEMO
103 #include <X11/extensions/XInput2.h>
106 #if defined(XCB_USE_GLX)
107 #include "qglxintegration.h"
108 #include <QtPlatformSupport/private/qglxconvenience_p.h>
109 #elif defined(XCB_USE_EGL)
110 #include "qxcbeglsurface.h"
111 #include <QtPlatformSupport/private/qeglconvenience_p.h>
112 #include <QtPlatformSupport/private/qxlibeglintegration_p.h>
115 #define XCOORD_MAX 16383
117 //#ifdef NET_WM_STATE_DEBUG
121 // Returns true if we should set WM_TRANSIENT_FOR on \a w
122 static inline bool isTransient(const QWindow *w)
124 return w->windowType() == Qt::Dialog
125 || w->windowType() == Qt::Sheet
126 || w->windowType() == Qt::Tool
127 || w->windowType() == Qt::SplashScreen
128 || w->windowType() == Qt::ToolTip
129 || w->windowType() == Qt::Drawer
130 || w->windowType() == Qt::Popup;
133 static inline QImage::Format imageFormatForDepth(int depth)
136 case 32: return QImage::Format_ARGB32_Premultiplied;
137 case 24: return QImage::Format_RGB32;
138 case 16: return QImage::Format_RGB16;
139 default: return QImage::Format_Invalid;
143 static inline bool positionIncludesFrame(QWindow *w)
145 return qt_window_private(w)->positionPolicy == QWindowPrivate::WindowFrameInclusive;
148 QXcbWindow::QXcbWindow(QWindow *window)
149 : QPlatformWindow(window)
152 , m_gravity(XCB_GRAVITY_STATIC)
154 , m_transparent(false)
155 , m_deferredActivation(false)
156 , m_netWmUserTimeWindow(XCB_NONE)
157 , m_dirtyFrameMargins(false)
158 #if defined(XCB_USE_EGL)
161 , m_lastWindowStateEvent(-1)
163 m_screen = static_cast<QXcbScreen *>(window->screen()->handle());
165 setConnection(m_screen->connection());
170 void QXcbWindow::create()
174 m_deferredExpose = false;
175 m_configureNotifyPending = true;
176 m_windowState = Qt::WindowNoState;
178 Qt::WindowType type = window()->windowType();
180 if (type == Qt::Desktop) {
181 m_window = m_screen->root();
182 m_depth = m_screen->screen()->root_depth;
183 m_imageFormat = imageFormatForDepth(m_depth);
184 connection()->addWindow(m_window, this);
188 // Determine gravity from initial position. Do not change
189 // later as it will cause the window to move uncontrollably.
190 m_gravity = positionIncludesFrame(window()) ?
191 XCB_GRAVITY_NORTH_WEST : XCB_GRAVITY_STATIC;
193 const quint32 mask = XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK;
194 const quint32 values[] = {
195 // XCB_CW_BACK_PIXMAP
197 // XCB_CW_OVERRIDE_REDIRECT
198 type == Qt::Popup || type == Qt::ToolTip,
200 type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
202 XCB_EVENT_MASK_EXPOSURE
203 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
204 | XCB_EVENT_MASK_KEY_PRESS
205 | XCB_EVENT_MASK_KEY_RELEASE
206 | XCB_EVENT_MASK_BUTTON_PRESS
207 | XCB_EVENT_MASK_BUTTON_RELEASE
208 | XCB_EVENT_MASK_BUTTON_MOTION
209 | XCB_EVENT_MASK_ENTER_WINDOW
210 | XCB_EVENT_MASK_LEAVE_WINDOW
211 | XCB_EVENT_MASK_POINTER_MOTION
212 | XCB_EVENT_MASK_PROPERTY_CHANGE
213 | XCB_EVENT_MASK_FOCUS_CHANGE
216 // Parameters to XCreateWindow() are frame corner + inner size.
217 // This fits in case position policy is frame inclusive. There is
218 // currently no way to implement it for frame-exclusive geometries.
219 QRect rect = window()->geometry();
220 QPlatformWindow::setGeometry(rect);
222 rect.setWidth(qBound(1, rect.width(), XCOORD_MAX));
223 rect.setHeight(qBound(1, rect.height(), XCOORD_MAX));
225 xcb_window_t xcb_parent_id = m_screen->root();
227 xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
229 m_format = window()->requestedFormat();
231 #if (defined(XCB_USE_GLX) || defined(XCB_USE_EGL)) && defined(XCB_USE_XLIB)
232 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL)
233 || m_format.hasAlpha())
235 #if defined(XCB_USE_GLX)
236 XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(m_screen), m_screen->screenNumber(), &m_format);
238 qFatal("Could not initialize GLX");
239 #elif defined(XCB_USE_EGL)
240 EGLDisplay eglDisplay = connection()->egl_display();
241 EGLConfig eglConfig = q_configFromGLFormat(eglDisplay, m_format, true);
242 m_format = q_glFormatFromConfig(eglDisplay, eglConfig);
244 VisualID id = QXlibEglIntegration::getCompatibleVisualId(DISPLAY_FROM_XCB(this), eglDisplay, eglConfig);
246 XVisualInfo visualInfoTemplate;
247 memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
248 visualInfoTemplate.visualid = id;
250 XVisualInfo *visualInfo;
251 int matchingCount = 0;
252 visualInfo = XGetVisualInfo(DISPLAY_FROM_XCB(this), VisualIDMask, &visualInfoTemplate, &matchingCount);
254 qFatal("Could not initialize EGL");
256 m_depth = visualInfo->depth;
257 m_imageFormat = imageFormatForDepth(m_depth);
258 Colormap cmap = XCreateColormap(DISPLAY_FROM_XCB(this), xcb_parent_id, visualInfo->visual, AllocNone);
260 XSetWindowAttributes a;
261 a.background_pixel = WhitePixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
262 a.border_pixel = BlackPixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
265 m_visualId = visualInfo->visualid;
267 m_window = XCreateWindow(DISPLAY_FROM_XCB(this), xcb_parent_id, rect.x(), rect.y(), rect.width(), rect.height(),
268 0, visualInfo->depth, InputOutput, visualInfo->visual,
269 CWBackPixel|CWBorderPixel|CWColormap, &a);
273 #endif //defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
275 m_window = xcb_generate_id(xcb_connection());
276 m_depth = m_screen->screen()->root_depth;
277 m_imageFormat = imageFormatForDepth(m_depth);
278 m_visualId = m_screen->screen()->root_visual;
280 Q_XCB_CALL(xcb_create_window(xcb_connection(),
281 XCB_COPY_FROM_PARENT, // depth -- same as root
282 m_window, // window id
283 xcb_parent_id, // parent window id
289 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
290 m_visualId, // visual
295 connection()->addWindow(m_window, this);
297 Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
299 propagateSizeHints();
301 xcb_atom_t properties[4];
302 int propertyCount = 0;
303 properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
304 properties[propertyCount++] = atom(QXcbAtom::WM_TAKE_FOCUS);
305 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_PING);
307 if (m_screen->syncRequestSupported())
308 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
310 if (window()->windowFlags() & Qt::WindowContextHelpButtonHint)
311 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
313 Q_XCB_CALL(xcb_change_property(xcb_connection(),
314 XCB_PROP_MODE_REPLACE,
316 atom(QXcbAtom::WM_PROTOCOLS),
324 if (m_screen->syncRequestSupported()) {
325 m_syncCounter = xcb_generate_id(xcb_connection());
326 Q_XCB_CALL(xcb_sync_create_counter(xcb_connection(), m_syncCounter, m_syncValue));
328 Q_XCB_CALL(xcb_change_property(xcb_connection(),
329 XCB_PROP_MODE_REPLACE,
331 atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
338 // set the PID to let the WM kill the application if unresponsive
340 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
341 atom(QXcbAtom::_NET_WM_PID), XCB_ATOM_CARDINAL, 32,
344 xcb_wm_hints_t hints;
345 memset(&hints, 0, sizeof(hints));
346 xcb_wm_hints_set_normal(&hints);
348 xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
350 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
352 xcb_window_t leader = m_screen->clientLeader();
353 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
354 atom(QXcbAtom::WM_CLIENT_LEADER), XCB_ATOM_WINDOW, 32,
357 #ifdef XCB_USE_XINPUT2_MAEMO
358 if (connection()->isUsingXInput2()) {
359 XIEventMask xieventmask;
360 uchar bitmask[2] = { 0, 0 };
362 xieventmask.deviceid = XIAllMasterDevices;
363 xieventmask.mask = bitmask;
364 xieventmask.mask_len = sizeof(bitmask);
366 XISetMask(bitmask, XI_ButtonPress);
367 XISetMask(bitmask, XI_ButtonRelease);
368 XISetMask(bitmask, XI_Motion);
370 XISelectEvents(DISPLAY_FROM_XCB(this), m_window, &xieventmask, 1);
374 setWindowFlags(window()->windowFlags());
375 setWindowTitle(window()->windowTitle());
376 setWindowState(window()->windowState());
378 if (window()->windowFlags() & Qt::WindowTransparentForInput)
379 setTransparentForMouseEvents(true);
381 #ifndef QT_NO_DRAGANDDROP
382 connection()->drag()->dndEnable(this, true);
386 QXcbWindow::~QXcbWindow()
391 void QXcbWindow::destroy()
393 if (m_syncCounter && m_screen->syncRequestSupported())
394 Q_XCB_CALL(xcb_sync_destroy_counter(xcb_connection(), m_syncCounter));
396 if (m_netWmUserTimeWindow) {
397 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
398 // Some window managers, like metacity, do XSelectInput on the _NET_WM_USER_TIME_WINDOW window,
399 // without trapping BadWindow (which crashes when the user time window is destroyed).
400 connection()->sync();
401 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
402 m_netWmUserTimeWindow = XCB_NONE;
404 connection()->removeWindow(m_window);
405 Q_XCB_CALL(xcb_destroy_window(xcb_connection(), m_window));
409 #if defined(XCB_USE_EGL)
415 void QXcbWindow::setGeometry(const QRect &rect)
417 QPlatformWindow::setGeometry(rect);
419 propagateSizeHints();
420 const QRect wmGeometry = windowToWmGeometry(rect);
422 const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
423 const qint32 values[] = {
424 qBound<qint32>(-XCOORD_MAX, wmGeometry.x(), XCOORD_MAX),
425 qBound<qint32>(-XCOORD_MAX, wmGeometry.y(), XCOORD_MAX),
426 qBound<qint32>(1, wmGeometry.width(), XCOORD_MAX),
427 qBound<qint32>(1, wmGeometry.height(), XCOORD_MAX),
430 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, reinterpret_cast<const quint32*>(values)));
432 xcb_flush(xcb_connection());
435 QMargins QXcbWindow::frameMargins() const
437 if (m_dirtyFrameMargins) {
438 xcb_window_t window = m_window;
439 xcb_window_t parent = m_window;
441 bool foundRoot = false;
443 const QVector<xcb_window_t> &virtualRoots =
444 connection()->wmSupport()->virtualRoots();
447 xcb_query_tree_cookie_t cookie = xcb_query_tree_unchecked(xcb_connection(), parent);
449 xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, NULL);
451 if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1) {
455 parent = reply->parent;
460 m_dirtyFrameMargins = false;
461 m_frameMargins = QMargins();
462 return m_frameMargins;
468 xcb_translate_coordinates_reply_t *reply =
469 xcb_translate_coordinates_reply(
471 xcb_translate_coordinates(xcb_connection(), window, parent, 0, 0),
475 offset = QPoint(reply->dst_x, reply->dst_y);
479 xcb_get_geometry_reply_t *geom =
480 xcb_get_geometry_reply(
482 xcb_get_geometry(xcb_connection(), parent),
487 // add the border_width for the window managers frame... some window managers
488 // do not use a border_width of zero for their frames, and if we the left and
489 // top strut, we ensure that pos() is absolutely correct. frameGeometry()
490 // will still be incorrect though... perhaps i should have foffset as well, to
491 // indicate the frame offset (equal to the border_width on X).
493 // -- copied from qwidget_x11.cpp
495 int left = offset.x() + geom->border_width;
496 int top = offset.y() + geom->border_width;
497 int right = geom->width + geom->border_width - geometry().width() - offset.x();
498 int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
500 m_frameMargins = QMargins(left, top, right, bottom);
505 m_dirtyFrameMargins = false;
508 return m_frameMargins;
511 void QXcbWindow::setVisible(bool visible)
519 void QXcbWindow::show()
521 if (window()->isTopLevel()) {
522 xcb_get_property_cookie_t cookie = xcb_get_wm_hints_unchecked(xcb_connection(), m_window);
524 xcb_wm_hints_t hints;
525 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, NULL);
527 if (window()->windowState() & Qt::WindowMinimized)
528 xcb_wm_hints_set_iconic(&hints);
530 xcb_wm_hints_set_normal(&hints);
532 xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
534 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
536 // update WM_NORMAL_HINTS
537 propagateSizeHints();
539 // update WM_TRANSIENT_FOR
540 if (window()->transientParent() && isTransient(window())) {
541 QXcbWindow *transientXcbParent = static_cast<QXcbWindow *>(window()->transientParent()->handle());
542 if (transientXcbParent) {
544 xcb_window_t parentWindow = transientXcbParent->xcb_window();
546 // todo: set transient for group (wm_client_leader) if no parent, a la qwidget_x11.cpp
547 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
548 XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 32,
553 // update _MOTIF_WM_HINTS
554 updateMotifWmHintsBeforeMap();
556 // update _NET_WM_STATE
557 updateNetWmStateBeforeMap();
560 if (connection()->time() != XCB_TIME_CURRENT_TIME)
561 updateNetWmUserTime(connection()->time());
563 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
564 xcb_flush(xcb_connection());
566 connection()->sync();
569 void QXcbWindow::hide()
571 Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
573 // send synthetic UnmapNotify event according to icccm 4.1.4
574 xcb_unmap_notify_event_t event;
575 event.response_type = XCB_UNMAP_NOTIFY;
576 event.event = m_screen->root();
577 event.window = m_window;
578 event.from_configure = false;
579 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, m_screen->root(),
580 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
582 xcb_flush(xcb_connection());
587 struct QtMotifWmHints {
588 quint32 flags, functions, decorations;
594 MWM_HINTS_FUNCTIONS = (1L << 0),
596 MWM_FUNC_ALL = (1L << 0),
597 MWM_FUNC_RESIZE = (1L << 1),
598 MWM_FUNC_MOVE = (1L << 2),
599 MWM_FUNC_MINIMIZE = (1L << 3),
600 MWM_FUNC_MAXIMIZE = (1L << 4),
601 MWM_FUNC_CLOSE = (1L << 5),
603 MWM_HINTS_DECORATIONS = (1L << 1),
605 MWM_DECOR_ALL = (1L << 0),
606 MWM_DECOR_BORDER = (1L << 1),
607 MWM_DECOR_RESIZEH = (1L << 2),
608 MWM_DECOR_TITLE = (1L << 3),
609 MWM_DECOR_MENU = (1L << 4),
610 MWM_DECOR_MINIMIZE = (1L << 5),
611 MWM_DECOR_MAXIMIZE = (1L << 6),
613 MWM_HINTS_INPUT_MODE = (1L << 2),
615 MWM_INPUT_MODELESS = 0L,
616 MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
617 MWM_INPUT_FULL_APPLICATION_MODAL = 3L
620 static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
622 QtMotifWmHints hints;
624 xcb_get_property_cookie_t get_cookie =
625 xcb_get_property_unchecked(c->xcb_connection(), 0, window, c->atom(QXcbAtom::_MOTIF_WM_HINTS),
626 c->atom(QXcbAtom::_MOTIF_WM_HINTS), 0, 20);
628 xcb_get_property_reply_t *reply =
629 xcb_get_property_reply(c->xcb_connection(), get_cookie, NULL);
631 if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
632 hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
635 hints.functions = MWM_FUNC_ALL;
636 hints.decorations = MWM_DECOR_ALL;
637 hints.input_mode = 0L;
646 static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
648 if (hints.flags != 0l) {
649 Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
650 XCB_PROP_MODE_REPLACE,
652 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
653 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
658 Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
662 QXcbWindow::NetWmStates QXcbWindow::netWmStates()
664 NetWmStates result(0);
666 xcb_get_property_cookie_t get_cookie =
667 xcb_get_property_unchecked(xcb_connection(), 0, m_window, atom(QXcbAtom::_NET_WM_STATE),
668 XCB_ATOM_ATOM, 0, 1024);
670 xcb_get_property_reply_t *reply =
671 xcb_get_property_reply(xcb_connection(), get_cookie, NULL);
673 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
674 const xcb_atom_t *states = static_cast<const xcb_atom_t *>(xcb_get_property_value(reply));
675 const xcb_atom_t *statesEnd = states + reply->length;
676 if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_ABOVE)))
677 result |= NetWmStateAbove;
678 if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_BELOW)))
679 result |= NetWmStateBelow;
680 if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN)))
681 result |= NetWmStateFullScreen;
682 if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ)))
683 result |= NetWmStateMaximizedHorz;
684 if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT)))
685 result |= NetWmStateMaximizedVert;
686 if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_MODAL)))
687 result |= NetWmStateModal;
688 if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP)))
689 result |= NetWmStateStaysOnTop;
690 if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_DEMANDS_ATTENTION)))
691 result |= NetWmStateDemandsAttention;
694 #ifdef NET_WM_STATE_DEBUG
695 printf("getting net wm state (%x), empty\n", m_window);
702 void QXcbWindow::setNetWmStates(NetWmStates states)
704 QVector<xcb_atom_t> atoms;
705 if (states & NetWmStateAbove)
706 atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_ABOVE));
707 if (states & NetWmStateBelow)
708 atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_BELOW));
709 if (states & NetWmStateFullScreen)
710 atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
711 if (states & NetWmStateMaximizedHorz)
712 atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ));
713 if (states & NetWmStateMaximizedVert)
714 atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
715 if (states & NetWmStateModal)
716 atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_MODAL));
717 if (states & NetWmStateStaysOnTop)
718 atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP));
719 if (states & NetWmStateDemandsAttention)
720 atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_DEMANDS_ATTENTION));
722 if (atoms.isEmpty()) {
723 Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
725 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
726 atom(QXcbAtom::_NET_WM_STATE), XCB_ATOM_ATOM, 32,
727 atoms.count(), atoms.constData()));
729 xcb_flush(xcb_connection());
732 Qt::WindowFlags QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
734 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
736 if (type == Qt::ToolTip)
737 flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
738 if (type == Qt::Popup)
739 flags |= Qt::X11BypassWindowManagerHint;
741 if (flags & Qt::WindowTransparentForInput) {
742 uint32_t mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_VISIBILITY_CHANGE
743 | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_RESIZE_REDIRECT
744 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
745 | XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_PROPERTY_CHANGE
746 | XCB_EVENT_MASK_COLOR_MAP_CHANGE | XCB_EVENT_MASK_OWNER_GRAB_BUTTON;
747 xcb_change_window_attributes(xcb_connection(), xcb_window(), XCB_CW_EVENT_MASK, &mask);
750 setNetWmWindowFlags(flags);
751 setMotifWindowFlags(flags);
753 setTransparentForMouseEvents(flags & Qt::WindowTransparentForInput);
754 updateDoesNotAcceptFocus(flags & Qt::WindowDoesNotAcceptFocus);
759 void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
761 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
763 QtMotifWmHints mwmhints;
765 mwmhints.functions = 0L;
766 mwmhints.decorations = 0;
767 mwmhints.input_mode = 0L;
768 mwmhints.status = 0L;
770 if (type != Qt::SplashScreen) {
771 mwmhints.flags |= MWM_HINTS_DECORATIONS;
773 bool customize = flags & Qt::CustomizeWindowHint;
774 if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
775 mwmhints.decorations |= MWM_DECOR_BORDER;
776 mwmhints.decorations |= MWM_DECOR_RESIZEH;
778 if (flags & Qt::WindowTitleHint)
779 mwmhints.decorations |= MWM_DECOR_TITLE;
781 if (flags & Qt::WindowSystemMenuHint)
782 mwmhints.decorations |= MWM_DECOR_MENU;
784 if (flags & Qt::WindowMinimizeButtonHint) {
785 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
786 mwmhints.functions |= MWM_FUNC_MINIMIZE;
789 if (flags & Qt::WindowMaximizeButtonHint) {
790 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
791 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
794 if (flags & Qt::WindowCloseButtonHint)
795 mwmhints.functions |= MWM_FUNC_CLOSE;
798 // if type == Qt::SplashScreen
799 mwmhints.decorations = MWM_DECOR_ALL;
802 if (mwmhints.functions != 0) {
803 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
804 mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
806 mwmhints.functions = MWM_FUNC_ALL;
809 if (!(flags & Qt::FramelessWindowHint)
810 && flags & Qt::CustomizeWindowHint
811 && flags & Qt::WindowTitleHint
813 (Qt::WindowMinimizeButtonHint
814 | Qt::WindowMaximizeButtonHint
815 | Qt::WindowCloseButtonHint)))
817 // a special case - only the titlebar without any button
818 mwmhints.flags = MWM_HINTS_FUNCTIONS;
819 mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
820 mwmhints.decorations = 0;
823 setMotifWmHints(connection(), m_window, mwmhints);
826 void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
828 xcb_client_message_event_t event;
830 event.response_type = XCB_CLIENT_MESSAGE;
832 event.window = m_window;
833 event.type = atom(QXcbAtom::_NET_WM_STATE);
834 event.data.data32[0] = set ? 1 : 0;
835 event.data.data32[1] = one;
836 event.data.data32[2] = two;
837 event.data.data32[3] = 0;
838 event.data.data32[4] = 0;
840 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));
843 Qt::WindowState QXcbWindow::setWindowState(Qt::WindowState state)
845 if (state == m_windowState)
849 switch (m_windowState) {
850 case Qt::WindowMinimized:
851 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
853 case Qt::WindowMaximized:
854 changeNetWmState(false,
855 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
856 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
858 case Qt::WindowFullScreen:
859 changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
867 case Qt::WindowMinimized:
869 xcb_client_message_event_t event;
871 event.response_type = XCB_CLIENT_MESSAGE;
873 event.window = m_window;
874 event.type = atom(QXcbAtom::WM_CHANGE_STATE);
875 event.data.data32[0] = XCB_WM_STATE_ICONIC;
876 event.data.data32[1] = 0;
877 event.data.data32[2] = 0;
878 event.data.data32[3] = 0;
879 event.data.data32[4] = 0;
881 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));
884 case Qt::WindowMaximized:
885 changeNetWmState(true,
886 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
887 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
889 case Qt::WindowFullScreen:
890 changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
892 case Qt::WindowNoState:
898 connection()->sync();
900 m_windowState = state;
901 return m_windowState;
904 void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
906 // in order of decreasing priority
907 QVector<uint> windowTypes;
909 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
914 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
918 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
921 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
923 case Qt::SplashScreen:
924 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
930 if (flags & Qt::FramelessWindowHint)
931 windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
933 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
935 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
936 atom(QXcbAtom::_NET_WM_WINDOW_TYPE), XCB_ATOM_ATOM, 32,
937 windowTypes.count(), windowTypes.constData()));
940 void QXcbWindow::updateMotifWmHintsBeforeMap()
942 QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
944 if (window()->windowModality() != Qt::NonModal) {
945 switch (window()->windowModality()) {
946 case Qt::WindowModal:
947 mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
949 case Qt::ApplicationModal:
951 mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
954 mwmhints.flags |= MWM_HINTS_INPUT_MODE;
956 mwmhints.input_mode = MWM_INPUT_MODELESS;
957 mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
960 if (window()->minimumSize() == window()->maximumSize()) {
961 // fixed size, remove the resize handle (since mwm/dtwm
962 // isn't smart enough to do it itself)
963 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
964 if (mwmhints.functions == MWM_FUNC_ALL) {
965 mwmhints.functions = MWM_FUNC_MOVE;
967 mwmhints.functions &= ~MWM_FUNC_RESIZE;
970 if (mwmhints.decorations == MWM_DECOR_ALL) {
971 mwmhints.flags |= MWM_HINTS_DECORATIONS;
972 mwmhints.decorations = (MWM_DECOR_BORDER
976 mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
980 if (window()->windowFlags() & Qt::WindowMinimizeButtonHint) {
981 mwmhints.flags |= MWM_HINTS_DECORATIONS;
982 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
983 mwmhints.functions |= MWM_FUNC_MINIMIZE;
985 if (window()->windowFlags() & Qt::WindowMaximizeButtonHint) {
986 mwmhints.flags |= MWM_HINTS_DECORATIONS;
987 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
988 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
990 if (window()->windowFlags() & Qt::WindowCloseButtonHint)
991 mwmhints.functions |= MWM_FUNC_CLOSE;
993 setMotifWmHints(connection(), m_window, mwmhints);
996 void QXcbWindow::updateNetWmStateBeforeMap()
998 NetWmStates states(0);
1000 const Qt::WindowFlags flags = window()->windowFlags();
1001 if (flags & Qt::WindowStaysOnTopHint) {
1002 states |= NetWmStateAbove;
1003 states |= NetWmStateStaysOnTop;
1004 } else if (flags & Qt::WindowStaysOnBottomHint) {
1005 states |= NetWmStateBelow;
1008 if (window()->windowState() & Qt::WindowFullScreen)
1009 states |= NetWmStateFullScreen;
1011 if (window()->windowState() & Qt::WindowMaximized) {
1012 states |= NetWmStateMaximizedHorz;
1013 states |= NetWmStateMaximizedVert;
1016 if (window()->windowModality() != Qt::NonModal)
1017 states |= NetWmStateModal;
1019 setNetWmStates(states);
1022 void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
1024 xcb_window_t wid = m_window;
1026 const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1027 if (m_netWmUserTimeWindow || isSupportedByWM) {
1028 if (!m_netWmUserTimeWindow) {
1029 m_netWmUserTimeWindow = xcb_generate_id(xcb_connection());
1030 Q_XCB_CALL(xcb_create_window(xcb_connection(),
1031 XCB_COPY_FROM_PARENT, // depth -- same as root
1032 m_netWmUserTimeWindow, // window id
1033 m_window, // parent window id
1036 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
1037 m_visualId, // visual
1040 wid = m_netWmUserTimeWindow;
1041 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW),
1042 XCB_ATOM_WINDOW, 32, 1, &m_netWmUserTimeWindow);
1043 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME));
1044 } else if (!isSupportedByWM) {
1045 // WM no longer supports it, then we should remove the
1046 // _NET_WM_USER_TIME_WINDOW atom.
1047 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1048 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
1049 m_netWmUserTimeWindow = XCB_NONE;
1051 wid = m_netWmUserTimeWindow;
1054 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
1055 XCB_ATOM_CARDINAL, 32, 1, ×tamp);
1058 void QXcbWindow::setTransparentForMouseEvents(bool transparent)
1060 if (transparent == m_transparent)
1063 xcb_rectangle_t rectangle;
1065 xcb_rectangle_t *rect = 0;
1071 rectangle.width = geometry().width();
1072 rectangle.height = geometry().height();
1077 xcb_xfixes_region_t region = xcb_generate_id(xcb_connection());
1078 xcb_xfixes_create_region(xcb_connection(), region, nrect, rect);
1079 xcb_xfixes_set_window_shape_region_checked(xcb_connection(), m_window, XCB_SHAPE_SK_INPUT, 0, 0, region);
1080 xcb_xfixes_destroy_region(xcb_connection(), region);
1082 m_transparent = transparent;
1085 void QXcbWindow::updateDoesNotAcceptFocus(bool doesNotAcceptFocus)
1087 xcb_get_property_cookie_t cookie = xcb_get_wm_hints_unchecked(xcb_connection(), m_window);
1089 xcb_wm_hints_t hints;
1090 if (!xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, NULL)) {
1094 xcb_wm_hints_set_input(&hints, !doesNotAcceptFocus);
1095 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
1098 WId QXcbWindow::winId() const
1103 void QXcbWindow::setParent(const QPlatformWindow *parent)
1105 // re-create for compatibility
1108 QPoint topLeft = geometry().topLeft();
1110 xcb_window_t xcb_parent_id = parent ? static_cast<const QXcbWindow *>(parent)->xcb_window() : m_screen->root();
1111 Q_XCB_CALL(xcb_reparent_window(xcb_connection(), xcb_window(), xcb_parent_id, topLeft.x(), topLeft.y()));
1114 void QXcbWindow::setWindowTitle(const QString &title)
1116 QByteArray ba = title.toUtf8();
1117 Q_XCB_CALL(xcb_change_property(xcb_connection(),
1118 XCB_PROP_MODE_REPLACE,
1120 atom(QXcbAtom::_NET_WM_NAME),
1121 atom(QXcbAtom::UTF8_STRING),
1127 void QXcbWindow::setWindowIcon(const QIcon &icon)
1129 QVector<quint32> icon_data;
1131 if (!icon.isNull()) {
1132 QList<QSize> availableSizes = icon.availableSizes();
1133 if (availableSizes.isEmpty()) {
1134 // try to use default sizes since the icon can be a scalable image like svg.
1135 availableSizes.push_back(QSize(16,16));
1136 availableSizes.push_back(QSize(32,32));
1137 availableSizes.push_back(QSize(64,64));
1138 availableSizes.push_back(QSize(128,128));
1140 for (int i = 0; i < availableSizes.size(); ++i) {
1141 QSize size = availableSizes.at(i);
1142 QPixmap pixmap = icon.pixmap(size);
1143 if (!pixmap.isNull()) {
1144 QImage image = pixmap.toImage().convertToFormat(QImage::Format_ARGB32);
1145 int pos = icon_data.size();
1146 icon_data.resize(pos + 2 + image.width()*image.height());
1147 icon_data[pos++] = image.width();
1148 icon_data[pos++] = image.height();
1149 memcpy(icon_data.data() + pos, image.bits(), image.width()*image.height()*4);
1154 if (!icon_data.isEmpty()) {
1155 Q_XCB_CALL(xcb_change_property(xcb_connection(),
1156 XCB_PROP_MODE_REPLACE,
1158 atom(QXcbAtom::_NET_WM_ICON),
1159 atom(QXcbAtom::CARDINAL),
1162 (unsigned char *) icon_data.data()));
1164 Q_XCB_CALL(xcb_delete_property(xcb_connection(),
1166 atom(QXcbAtom::_NET_WM_ICON)));
1170 void QXcbWindow::raise()
1172 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1173 const quint32 values[] = { XCB_STACK_MODE_ABOVE };
1174 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1177 void QXcbWindow::lower()
1179 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1180 const quint32 values[] = { XCB_STACK_MODE_BELOW };
1181 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1184 // Adapt the geometry to match the WM expection with regards
1186 QRect QXcbWindow::windowToWmGeometry(QRect r) const
1188 if (m_dirtyFrameMargins || m_frameMargins.isNull())
1190 const bool frameInclusive = positionIncludesFrame(window());
1191 // XCB_GRAVITY_STATIC requires the inner geometry, whereas
1192 // XCB_GRAVITY_NORTH_WEST requires the frame geometry
1193 if (frameInclusive && m_gravity == XCB_GRAVITY_STATIC) {
1194 r.translate(m_frameMargins.left(), m_frameMargins.top());
1195 } else if (!frameInclusive && m_gravity == XCB_GRAVITY_NORTH_WEST) {
1196 r.translate(-m_frameMargins.left(), -m_frameMargins.top());
1201 void QXcbWindow::propagateSizeHints()
1203 // update WM_NORMAL_HINTS
1204 xcb_size_hints_t hints;
1205 memset(&hints, 0, sizeof(hints));
1207 const QRect rect = windowToWmGeometry(geometry());
1209 QWindow *win = window();
1211 xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
1212 xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
1213 xcb_size_hints_set_win_gravity(&hints, m_gravity);
1215 QSize minimumSize = win->minimumSize();
1216 QSize maximumSize = win->maximumSize();
1217 QSize baseSize = win->baseSize();
1218 QSize sizeIncrement = win->sizeIncrement();
1220 if (minimumSize.width() > 0 || minimumSize.height() > 0)
1221 xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1223 if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1224 xcb_size_hints_set_max_size(&hints,
1225 qMin(XCOORD_MAX, maximumSize.width()),
1226 qMin(XCOORD_MAX, maximumSize.height()));
1228 if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1229 xcb_size_hints_set_base_size(&hints, baseSize.width(), baseSize.height());
1230 xcb_size_hints_set_resize_inc(&hints, sizeIncrement.width(), sizeIncrement.height());
1233 xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1236 void QXcbWindow::requestActivateWindow()
1239 m_deferredActivation = true;
1242 m_deferredActivation = false;
1244 updateNetWmUserTime(connection()->time());
1246 if (window()->isTopLevel()
1247 && connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_ACTIVE_WINDOW))) {
1248 xcb_client_message_event_t event;
1250 event.response_type = XCB_CLIENT_MESSAGE;
1252 event.window = m_window;
1253 event.type = atom(QXcbAtom::_NET_ACTIVE_WINDOW);
1254 event.data.data32[0] = 1;
1255 event.data.data32[1] = connection()->time();
1256 QWindow *focusWindow = QGuiApplication::focusWindow();
1257 event.data.data32[2] = focusWindow ? focusWindow->winId() : XCB_NONE;
1258 event.data.data32[3] = 0;
1259 event.data.data32[4] = 0;
1261 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));
1263 Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1266 connection()->sync();
1269 #if XCB_USE_MAEMO_WINDOW_PROPERTIES
1270 void QXcbWindow::handleContentOrientationChange(Qt::ScreenOrientation orientation)
1273 switch (orientation) {
1274 case Qt::PortraitOrientation: angle = 270; break;
1275 case Qt::LandscapeOrientation: angle = 0; break;
1276 case Qt::InvertedPortraitOrientation: angle = 90; break;
1277 case Qt::InvertedLandscapeOrientation: angle = 180; break;
1278 case Qt::PrimaryOrientation: break;
1280 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
1281 atom(QXcbAtom::MeegoTouchOrientationAngle), XCB_ATOM_CARDINAL, 32,
1286 QSurfaceFormat QXcbWindow::format() const
1288 // ### return actual format
1292 #if defined(XCB_USE_EGL)
1293 QXcbEGLSurface *QXcbWindow::eglSurface() const
1295 if (!m_eglSurface) {
1296 EGLDisplay display = connection()->egl_display();
1297 EGLConfig config = q_configFromGLFormat(display, window()->requestedFormat(), true);
1298 EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)m_window, 0);
1300 m_eglSurface = new QXcbEGLSurface(display, surface);
1303 return m_eglSurface;
1307 void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1309 QRect rect(event->x, event->y, event->width, event->height);
1311 if (m_exposeRegion.isEmpty())
1312 m_exposeRegion = rect;
1314 m_exposeRegion |= rect;
1316 // if count is non-zero there are more expose events pending
1317 if (event->count == 0) {
1318 QWindowSystemInterface::handleExposeEvent(window(), m_exposeRegion);
1319 m_exposeRegion = QRegion();
1323 void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1325 if (event->format != 32)
1328 if (event->type == atom(QXcbAtom::WM_PROTOCOLS)) {
1329 if (event->data.data32[0] == atom(QXcbAtom::WM_DELETE_WINDOW)) {
1330 QWindowSystemInterface::handleCloseEvent(window());
1331 } else if (event->data.data32[0] == atom(QXcbAtom::WM_TAKE_FOCUS)) {
1332 connection()->setTime(event->data.data32[1]);
1333 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_PING)) {
1334 xcb_client_message_event_t reply = *event;
1336 reply.response_type = XCB_CLIENT_MESSAGE;
1337 reply.window = m_screen->root();
1339 xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&reply);
1340 xcb_flush(xcb_connection());
1341 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_SYNC_REQUEST)) {
1342 connection()->setTime(event->data.data32[1]);
1343 m_syncValue.lo = event->data.data32[2];
1344 m_syncValue.hi = event->data.data32[3];
1346 qWarning() << "QXcbWindow: Unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1348 #ifndef QT_NO_DRAGANDDROP
1349 } else if (event->type == atom(QXcbAtom::XdndEnter)) {
1350 connection()->drag()->handleEnter(window(), event);
1351 } else if (event->type == atom(QXcbAtom::XdndPosition)) {
1352 connection()->drag()->handlePosition(window(), event);
1353 } else if (event->type == atom(QXcbAtom::XdndLeave)) {
1354 connection()->drag()->handleLeave(window(), event);
1355 } else if (event->type == atom(QXcbAtom::XdndDrop)) {
1356 connection()->drag()->handleDrop(window(), event);
1358 } else if (event->type == atom(QXcbAtom::_XEMBED)) { // QSystemTrayIcon
1360 qWarning() << "QXcbWindow: Unhandled client message:" << connection()->atomName(event->type);
1364 void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1366 bool fromSendEvent = (event->response_type & 0x80);
1367 QPoint pos(event->x, event->y);
1368 if (!parent() && !fromSendEvent) {
1369 // Do not trust the position, query it instead.
1370 xcb_translate_coordinates_cookie_t cookie = xcb_translate_coordinates(xcb_connection(), xcb_window(),
1371 m_screen->root(), 0, 0);
1372 xcb_translate_coordinates_reply_t *reply = xcb_translate_coordinates_reply(xcb_connection(), cookie, NULL);
1374 pos.setX(reply->dst_x);
1375 pos.setY(reply->dst_y);
1380 QRect rect(pos, QSize(event->width, event->height));
1382 QPlatformWindow::setGeometry(rect);
1383 QWindowSystemInterface::handleGeometryChange(window(), rect);
1385 m_configureNotifyPending = false;
1387 if (m_deferredExpose) {
1388 m_deferredExpose = false;
1389 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
1392 m_dirtyFrameMargins = true;
1396 static_cast<QDri2Context *>(m_context)->resize(rect.size());
1400 bool QXcbWindow::isExposed() const
1405 void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1407 if (event->window == m_window) {
1409 if (m_deferredActivation)
1410 requestActivateWindow();
1411 if (m_configureNotifyPending)
1412 m_deferredExpose = true;
1414 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
1418 void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1420 if (event->window == m_window) {
1422 QWindowSystemInterface::handleExposeEvent(window(), QRegion());
1426 static Qt::MouseButtons translateMouseButtons(int s)
1428 Qt::MouseButtons ret = 0;
1429 if (s & XCB_BUTTON_MASK_1)
1430 ret |= Qt::LeftButton;
1431 if (s & XCB_BUTTON_MASK_2)
1432 ret |= Qt::MidButton;
1433 if (s & XCB_BUTTON_MASK_3)
1434 ret |= Qt::RightButton;
1438 static Qt::MouseButton translateMouseButton(xcb_button_t s)
1441 case 1: return Qt::LeftButton;
1442 case 2: return Qt::MidButton;
1443 case 3: return Qt::RightButton;
1444 // Button values 4-7 were already handled as Wheel events, and won't occur here.
1445 case 8: return Qt::BackButton; // Also known as Qt::ExtraButton1
1446 case 9: return Qt::ForwardButton; // Also known as Qt::ExtraButton2
1447 case 10: return Qt::ExtraButton3;
1448 case 11: return Qt::ExtraButton4;
1449 case 12: return Qt::ExtraButton5;
1450 case 13: return Qt::ExtraButton6;
1451 case 14: return Qt::ExtraButton7;
1452 case 15: return Qt::ExtraButton8;
1453 case 16: return Qt::ExtraButton9;
1454 case 17: return Qt::ExtraButton10;
1455 case 18: return Qt::ExtraButton11;
1456 case 19: return Qt::ExtraButton12;
1457 case 20: return Qt::ExtraButton13;
1458 case 21: return Qt::ExtraButton14;
1459 case 22: return Qt::ExtraButton15;
1460 case 23: return Qt::ExtraButton16;
1461 case 24: return Qt::ExtraButton17;
1462 case 25: return Qt::ExtraButton18;
1463 case 26: return Qt::ExtraButton19;
1464 case 27: return Qt::ExtraButton20;
1465 case 28: return Qt::ExtraButton21;
1466 case 29: return Qt::ExtraButton22;
1467 case 30: return Qt::ExtraButton23;
1468 case 31: return Qt::ExtraButton24;
1469 default: return Qt::NoButton;
1473 void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1475 updateNetWmUserTime(event->time);
1477 QPoint local(event->event_x, event->event_y);
1478 QPoint global(event->root_x, event->root_y);
1480 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1482 if (event->detail >= 4 && event->detail <= 7) {
1483 // Logic borrowed from qapplication_x11.cpp
1484 int delta = 120 * ((event->detail == 4 || event->detail == 6) ? 1 : -1);
1485 bool hor = (((event->detail == 4 || event->detail == 5)
1486 && (modifiers & Qt::AltModifier))
1487 || (event->detail == 6 || event->detail == 7));
1489 QWindowSystemInterface::handleWheelEvent(window(), event->time,
1490 local, global, delta, hor ? Qt::Horizontal : Qt::Vertical, modifiers);
1494 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1497 void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1499 QPoint local(event->event_x, event->event_y);
1500 QPoint global(event->root_x, event->root_y);
1501 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1503 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1506 void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
1508 QPoint local(event->event_x, event->event_y);
1509 QPoint global(event->root_x, event->root_y);
1510 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1512 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1515 void QXcbWindow::handleMouseEvent(xcb_button_t detail, uint16_t state, xcb_timestamp_t time, const QPoint &local, const QPoint &global, Qt::KeyboardModifiers modifiers)
1517 connection()->setTime(time);
1519 Qt::MouseButtons buttons = translateMouseButtons(state);
1520 Qt::MouseButton button = translateMouseButton(detail);
1522 buttons ^= button; // X event uses state *before*, Qt uses state *after*
1524 QWindowSystemInterface::handleMouseEvent(window(), time, local, global, buttons, modifiers);
1527 void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1529 connection()->setTime(event->time);
1531 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1532 || event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1533 || event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1538 QWindowSystemInterface::handleEnterEvent(window());
1541 void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1543 connection()->setTime(event->time);
1545 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1546 || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)
1551 QWindowSystemInterface::handleLeaveEvent(window());
1554 void QXcbWindow::handlePropertyNotifyEvent(const xcb_property_notify_event_t *event)
1556 connection()->setTime(event->time);
1558 const bool propertyDeleted = event->state == XCB_PROPERTY_DELETE;
1559 const xcb_atom_t netWmStateAtom = atom(QXcbAtom::_NET_WM_STATE);
1560 const xcb_atom_t wmStateAtom = atom(QXcbAtom::WM_STATE);
1562 if (event->atom == netWmStateAtom || event->atom == wmStateAtom) {
1563 if (propertyDeleted)
1566 Qt::WindowState newState = Qt::WindowNoState;
1567 if (event->atom == wmStateAtom) { // WM_STATE: Quick check for 'Minimize'.
1568 const xcb_get_property_cookie_t get_cookie =
1569 xcb_get_property(xcb_connection(), 0, m_window, wmStateAtom,
1570 XCB_ATOM_ANY, 0, 1024);
1572 xcb_get_property_reply_t *reply =
1573 xcb_get_property_reply(xcb_connection(), get_cookie, NULL);
1575 if (reply && reply->format == 32 && reply->type == wmStateAtom) {
1576 const long *data = (const long *)xcb_get_property_value(reply);
1577 if (reply->length != 0 && XCB_WM_STATE_ICONIC == data[0])
1578 newState = Qt::WindowMinimized;
1581 } // WM_STATE: Quick check for 'Minimize'.
1582 if (newState != Qt::WindowMinimized) { // Something else changed, get _NET_WM_STATE.
1583 const NetWmStates states = netWmStates();
1584 if ((states & NetWmStateMaximizedHorz) && (states & NetWmStateMaximizedVert))
1585 newState = Qt::WindowMaximized;
1586 else if (states & NetWmStateFullScreen)
1587 newState = Qt::WindowFullScreen;
1589 // Send Window state, compress events in case other flags (modality, etc) are changed.
1590 if (m_lastWindowStateEvent != newState) {
1591 QWindowSystemInterface::handleWindowStateChanged(window(), newState);
1592 m_lastWindowStateEvent = newState;
1597 void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1599 QWindowSystemInterface::handleWindowActivated(window());
1602 static bool focusInPeeker(xcb_generic_event_t *event)
1605 // FocusIn event is not in the queue, proceed with FocusOut normally.
1606 QWindowSystemInterface::handleWindowActivated(0);
1609 uint response_type = event->response_type & ~0x80;
1610 return response_type == XCB_FOCUS_IN;
1613 void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1615 // Do not set the active window to 0 if there is a FocusIn coming.
1616 // There is however no equivalent for XPutBackEvent so register a
1617 // callback for QXcbConnection instead.
1618 connection()->addPeekFunc(focusInPeeker);
1621 void QXcbWindow::updateSyncRequestCounter()
1623 if (m_screen->syncRequestSupported() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
1624 Q_XCB_CALL(xcb_sync_set_counter(xcb_connection(), m_syncCounter, m_syncValue));
1625 xcb_flush(xcb_connection());
1626 connection()->sync();
1633 bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1636 xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1639 xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
1640 m_window, XCB_TIME_CURRENT_TIME,
1641 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1642 xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, NULL);
1643 bool result = !(!reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1648 bool QXcbWindow::setMouseGrabEnabled(bool grab)
1651 xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1654 xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
1655 (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
1656 | XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW
1657 | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_POINTER_MOTION),
1658 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
1659 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
1660 XCB_TIME_CURRENT_TIME);
1661 xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, NULL);
1662 bool result = !(!reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1667 void QXcbWindow::setCursor(xcb_cursor_t cursor)
1669 xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1670 xcb_flush(xcb_connection());
1673 bool QXcbWindow::startSystemResize(const QPoint &pos, Qt::Corner corner)
1675 const xcb_atom_t moveResize = connection()->atom(QXcbAtom::_NET_WM_MOVERESIZE);
1676 if (!connection()->wmSupport()->isSupportedByWM(moveResize))
1678 xcb_client_message_event_t xev;
1679 xev.response_type = XCB_CLIENT_MESSAGE;
1680 xev.type = moveResize;
1681 xev.window = xcb_window();
1683 const QPoint globalPos = window()->mapToGlobal(pos);
1684 xev.data.data32[0] = globalPos.x();
1685 xev.data.data32[1] = globalPos.y();
1686 const bool bottom = corner == Qt::BottomRightCorner || corner == Qt::BottomLeftCorner;
1687 const bool left = corner == Qt::BottomLeftCorner || corner == Qt::TopLeftCorner;
1689 xev.data.data32[2] = left ? 6 : 4; // bottomleft/bottomright
1691 xev.data.data32[2] = left ? 0 : 2; // topleft/topright
1692 xev.data.data32[3] = Button1;
1693 xev.data.data32[4] = 0;
1694 xcb_ungrab_pointer(connection()->xcb_connection(), XCB_CURRENT_TIME);
1695 xcb_send_event(connection()->xcb_connection(), false, m_screen->root(),
1696 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
1697 (const char *)&xev);