Implement QXcbWindow::setWindowIcon
[profile/ivi/qtbase.git] / src / plugins / platforms / xcb / qxcbwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qxcbwindow.h"
43
44 #include <QtDebug>
45 #include <QScreen>
46 #include <QtGui/QIcon>
47
48 #include "qxcbconnection.h"
49 #include "qxcbscreen.h"
50 #include "qxcbdrag.h"
51 #include "qxcbkeyboard.h"
52 #include "qxcbwmsupport.h"
53 #include "qxcbimage.h"
54
55 #include <qpa/qplatformintegration.h>
56
57 #ifdef XCB_USE_DRI2
58 #include "qdri2context.h"
59 #endif
60
61 // FIXME This workaround can be removed for xcb-icccm > 3.8
62 #define class class_name
63 #include <xcb/xcb_icccm.h>
64 #undef class
65 #include <xcb/xfixes.h>
66
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
87 #endif
88
89 #include <private/qguiapplication_p.h>
90 #include <private/qwindow_p.h>
91
92 #include <qpa/qplatformbackingstore.h>
93 #include <QtGui/QWindowSystemInterface>
94
95 #include <stdio.h>
96
97 #ifdef XCB_USE_XLIB
98 #include <X11/Xlib.h>
99 #include <X11/Xutil.h>
100 #endif
101
102 #ifdef XCB_USE_XINPUT2_MAEMO
103 #include <X11/extensions/XInput2.h>
104 #endif
105
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>
113 #endif
114
115 #define XCOORD_MAX 16383
116
117 //#ifdef NET_WM_STATE_DEBUG
118
119 QT_BEGIN_NAMESPACE
120
121 // Returns true if we should set WM_TRANSIENT_FOR on \a w
122 static inline bool isTransient(const QWindow *w)
123 {
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;
131 }
132
133 static inline QImage::Format imageFormatForDepth(int depth)
134 {
135     switch (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;
140     }
141 }
142
143 static inline bool positionIncludesFrame(QWindow *w)
144 {
145     return qt_window_private(w)->positionPolicy == QWindowPrivate::WindowFrameInclusive;
146 }
147
148 QXcbWindow::QXcbWindow(QWindow *window)
149     : QPlatformWindow(window)
150     , m_window(0)
151     , m_syncCounter(0)
152     , m_gravity(XCB_GRAVITY_STATIC)
153     , m_mapped(false)
154     , m_transparent(false)
155     , m_deferredActivation(false)
156     , m_netWmUserTimeWindow(XCB_NONE)
157     , m_dirtyFrameMargins(false)
158 #if defined(XCB_USE_EGL)
159     , m_eglSurface(0)
160 #endif
161     , m_lastWindowStateEvent(-1)
162 {
163     m_screen = static_cast<QXcbScreen *>(window->screen()->handle());
164
165     setConnection(m_screen->connection());
166
167     create();
168 }
169
170 void QXcbWindow::create()
171 {
172     destroy();
173
174     m_deferredExpose = false;
175     m_configureNotifyPending = true;
176     m_windowState = Qt::WindowNoState;
177
178     Qt::WindowType type = window()->windowType();
179
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);
185         return;
186     }
187
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;
192
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
196         XCB_NONE,
197         // XCB_CW_OVERRIDE_REDIRECT
198         type == Qt::Popup || type == Qt::ToolTip,
199         // XCB_CW_SAVE_UNDER
200         type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
201         // XCB_CW_EVENT_MASK
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
214     };
215
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);
221
222     rect.setWidth(qBound(1, rect.width(), XCOORD_MAX));
223     rect.setHeight(qBound(1, rect.height(), XCOORD_MAX));
224
225     xcb_window_t xcb_parent_id = m_screen->root();
226     if (parent())
227         xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
228
229     m_format = window()->requestedFormat();
230
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())
234     {
235 #if defined(XCB_USE_GLX)
236         XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(m_screen), m_screen->screenNumber(), &m_format);
237         if (!visualInfo)
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);
243
244         VisualID id = QXlibEglIntegration::getCompatibleVisualId(DISPLAY_FROM_XCB(this), eglDisplay, eglConfig);
245
246         XVisualInfo visualInfoTemplate;
247         memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
248         visualInfoTemplate.visualid = id;
249
250         XVisualInfo *visualInfo;
251         int matchingCount = 0;
252         visualInfo = XGetVisualInfo(DISPLAY_FROM_XCB(this), VisualIDMask, &visualInfoTemplate, &matchingCount);
253         if (!visualInfo)
254             qFatal("Could not initialize EGL");
255 #endif //XCB_USE_GLX
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);
259
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());
263         a.colormap = cmap;
264
265         m_visualId = visualInfo->visualid;
266
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);
270
271         XFree(visualInfo);
272     } else
273 #endif //defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
274     {
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;
279
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
284                                      rect.x(),
285                                      rect.y(),
286                                      rect.width(),
287                                      rect.height(),
288                                      0,                               // border width
289                                      XCB_WINDOW_CLASS_INPUT_OUTPUT,   // window class
290                                      m_visualId,                      // visual
291                                      0,                               // value mask
292                                      0));                             // value list
293     }
294
295     connection()->addWindow(m_window, this);
296
297     Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
298
299     propagateSizeHints();
300
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);
306
307     if (m_screen->syncRequestSupported())
308         properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
309
310     if (window()->windowFlags() & Qt::WindowContextHelpButtonHint)
311         properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
312
313     Q_XCB_CALL(xcb_change_property(xcb_connection(),
314                                    XCB_PROP_MODE_REPLACE,
315                                    m_window,
316                                    atom(QXcbAtom::WM_PROTOCOLS),
317                                    XCB_ATOM_ATOM,
318                                    32,
319                                    propertyCount,
320                                    properties));
321     m_syncValue.hi = 0;
322     m_syncValue.lo = 0;
323
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));
327
328         Q_XCB_CALL(xcb_change_property(xcb_connection(),
329                                        XCB_PROP_MODE_REPLACE,
330                                        m_window,
331                                        atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
332                                        XCB_ATOM_CARDINAL,
333                                        32,
334                                        1,
335                                        &m_syncCounter));
336     }
337
338     // set the PID to let the WM kill the application if unresponsive
339     long pid = getpid();
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,
342                                    1, &pid));
343
344     xcb_wm_hints_t hints;
345     memset(&hints, 0, sizeof(hints));
346     xcb_wm_hints_set_normal(&hints);
347
348     xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
349
350     xcb_set_wm_hints(xcb_connection(), m_window, &hints);
351
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,
355                                    1, &leader));
356
357 #ifdef XCB_USE_XINPUT2_MAEMO
358     if (connection()->isUsingXInput2()) {
359         XIEventMask xieventmask;
360         uchar bitmask[2] = { 0, 0 };
361
362         xieventmask.deviceid = XIAllMasterDevices;
363         xieventmask.mask = bitmask;
364         xieventmask.mask_len = sizeof(bitmask);
365
366         XISetMask(bitmask, XI_ButtonPress);
367         XISetMask(bitmask, XI_ButtonRelease);
368         XISetMask(bitmask, XI_Motion);
369
370         XISelectEvents(DISPLAY_FROM_XCB(this), m_window, &xieventmask, 1);
371     }
372 #endif
373
374     setWindowFlags(window()->windowFlags());
375     setWindowTitle(window()->windowTitle());
376     setWindowState(window()->windowState());
377
378     if (window()->windowFlags() & Qt::WindowTransparentForInput)
379         setTransparentForMouseEvents(true);
380
381 #ifndef QT_NO_DRAGANDDROP
382     connection()->drag()->dndEnable(this, true);
383 #endif
384 }
385
386 QXcbWindow::~QXcbWindow()
387 {
388     destroy();
389 }
390
391 void QXcbWindow::destroy()
392 {
393     if (m_syncCounter && m_screen->syncRequestSupported())
394         Q_XCB_CALL(xcb_sync_destroy_counter(xcb_connection(), m_syncCounter));
395     if (m_window) {
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;
403         }
404         connection()->removeWindow(m_window);
405         Q_XCB_CALL(xcb_destroy_window(xcb_connection(), m_window));
406     }
407     m_mapped = false;
408
409 #if defined(XCB_USE_EGL)
410     delete m_eglSurface;
411     m_eglSurface = 0;
412 #endif
413 }
414
415 void QXcbWindow::setGeometry(const QRect &rect)
416 {
417     QPlatformWindow::setGeometry(rect);
418
419     propagateSizeHints();
420     const QRect wmGeometry = windowToWmGeometry(rect);
421
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),
428     };
429
430     Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, reinterpret_cast<const quint32*>(values)));
431
432     xcb_flush(xcb_connection());
433 }
434
435 QMargins QXcbWindow::frameMargins() const
436 {
437     if (m_dirtyFrameMargins) {
438         xcb_window_t window = m_window;
439         xcb_window_t parent = m_window;
440
441         bool foundRoot = false;
442
443         const QVector<xcb_window_t> &virtualRoots =
444             connection()->wmSupport()->virtualRoots();
445
446         while (!foundRoot) {
447             xcb_query_tree_cookie_t cookie = xcb_query_tree_unchecked(xcb_connection(), parent);
448
449             xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, NULL);
450             if (reply) {
451                 if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1) {
452                     foundRoot = true;
453                 } else {
454                     window = parent;
455                     parent = reply->parent;
456                 }
457
458                 free(reply);
459             } else {
460                 m_dirtyFrameMargins = false;
461                 m_frameMargins = QMargins();
462                 return m_frameMargins;
463             }
464         }
465
466         QPoint offset;
467
468         xcb_translate_coordinates_reply_t *reply =
469             xcb_translate_coordinates_reply(
470                 xcb_connection(),
471                 xcb_translate_coordinates(xcb_connection(), window, parent, 0, 0),
472                 NULL);
473
474         if (reply) {
475             offset = QPoint(reply->dst_x, reply->dst_y);
476             free(reply);
477         }
478
479         xcb_get_geometry_reply_t *geom =
480             xcb_get_geometry_reply(
481                 xcb_connection(),
482                 xcb_get_geometry(xcb_connection(), parent),
483                 NULL);
484
485         if (geom) {
486             // --
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).
492             // - Brad
493             // -- copied from qwidget_x11.cpp
494
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();
499
500             m_frameMargins = QMargins(left, top, right, bottom);
501
502             free(geom);
503         }
504
505         m_dirtyFrameMargins = false;
506     }
507
508     return m_frameMargins;
509 }
510
511 void QXcbWindow::setVisible(bool visible)
512 {
513     if (visible)
514         show();
515     else
516         hide();
517 }
518
519 void QXcbWindow::show()
520 {
521     if (window()->isTopLevel()) {
522         xcb_get_property_cookie_t cookie = xcb_get_wm_hints_unchecked(xcb_connection(), m_window);
523
524         xcb_wm_hints_t hints;
525         xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, NULL);
526
527         if (window()->windowState() & Qt::WindowMinimized)
528             xcb_wm_hints_set_iconic(&hints);
529         else
530             xcb_wm_hints_set_normal(&hints);
531
532         xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
533
534         xcb_set_wm_hints(xcb_connection(), m_window, &hints);
535
536         // update WM_NORMAL_HINTS
537         propagateSizeHints();
538
539         // update WM_TRANSIENT_FOR
540         if (window()->transientParent() && isTransient(window())) {
541             QXcbWindow *transientXcbParent = static_cast<QXcbWindow *>(window()->transientParent()->handle());
542             if (transientXcbParent) {
543                 // ICCCM 4.1.2.6
544                 xcb_window_t parentWindow = transientXcbParent->xcb_window();
545
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,
549                                                1, &parentWindow));
550             }
551         }
552
553         // update _MOTIF_WM_HINTS
554         updateMotifWmHintsBeforeMap();
555
556         // update _NET_WM_STATE
557         updateNetWmStateBeforeMap();
558     }
559
560     if (connection()->time() != XCB_TIME_CURRENT_TIME)
561         updateNetWmUserTime(connection()->time());
562
563     Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
564     xcb_flush(xcb_connection());
565
566     connection()->sync();
567 }
568
569 void QXcbWindow::hide()
570 {
571     Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
572
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));
581
582     xcb_flush(xcb_connection());
583
584     m_mapped = false;
585 }
586
587 struct QtMotifWmHints {
588     quint32 flags, functions, decorations;
589     qint32 input_mode;
590     quint32 status;
591 };
592
593 enum {
594     MWM_HINTS_FUNCTIONS   = (1L << 0),
595
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),
602
603     MWM_HINTS_DECORATIONS = (1L << 1),
604
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),
612
613     MWM_HINTS_INPUT_MODE = (1L << 2),
614
615     MWM_INPUT_MODELESS                  = 0L,
616     MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
617     MWM_INPUT_FULL_APPLICATION_MODAL    = 3L
618 };
619
620 static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
621 {
622     QtMotifWmHints hints;
623
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);
627
628     xcb_get_property_reply_t *reply =
629         xcb_get_property_reply(c->xcb_connection(), get_cookie, NULL);
630
631     if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
632         hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
633     } else {
634         hints.flags = 0L;
635         hints.functions = MWM_FUNC_ALL;
636         hints.decorations = MWM_DECOR_ALL;
637         hints.input_mode = 0L;
638         hints.status = 0L;
639     }
640
641     free(reply);
642
643     return hints;
644 }
645
646 static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
647 {
648     if (hints.flags != 0l) {
649         Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
650                                        XCB_PROP_MODE_REPLACE,
651                                        window,
652                                        c->atom(QXcbAtom::_MOTIF_WM_HINTS),
653                                        c->atom(QXcbAtom::_MOTIF_WM_HINTS),
654                                        32,
655                                        5,
656                                        &hints), c);
657     } else {
658         Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
659     }
660 }
661
662 QXcbWindow::NetWmStates QXcbWindow::netWmStates()
663 {
664     NetWmStates result(0);
665
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);
669
670     xcb_get_property_reply_t *reply =
671         xcb_get_property_reply(xcb_connection(), get_cookie, NULL);
672
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;
692         free(reply);
693     } else {
694 #ifdef NET_WM_STATE_DEBUG
695         printf("getting net wm state (%x), empty\n", m_window);
696 #endif
697     }
698
699     return result;
700 }
701
702 void QXcbWindow::setNetWmStates(NetWmStates states)
703 {
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));
721
722     if (atoms.isEmpty()) {
723         Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
724     } else {
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()));
728     }
729     xcb_flush(xcb_connection());
730 }
731
732 Qt::WindowFlags QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
733 {
734     Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
735
736     if (type == Qt::ToolTip)
737         flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
738     if (type == Qt::Popup)
739         flags |= Qt::X11BypassWindowManagerHint;
740
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);
748     }
749
750     setNetWmWindowFlags(flags);
751     setMotifWindowFlags(flags);
752
753     setTransparentForMouseEvents(flags & Qt::WindowTransparentForInput);
754     updateDoesNotAcceptFocus(flags & Qt::WindowDoesNotAcceptFocus);
755
756     return flags;
757 }
758
759 void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
760 {
761     Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
762
763     QtMotifWmHints mwmhints;
764     mwmhints.flags = 0L;
765     mwmhints.functions = 0L;
766     mwmhints.decorations = 0;
767     mwmhints.input_mode = 0L;
768     mwmhints.status = 0L;
769
770     if (type != Qt::SplashScreen) {
771         mwmhints.flags |= MWM_HINTS_DECORATIONS;
772
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;
777
778             if (flags & Qt::WindowTitleHint)
779                 mwmhints.decorations |= MWM_DECOR_TITLE;
780
781             if (flags & Qt::WindowSystemMenuHint)
782                 mwmhints.decorations |= MWM_DECOR_MENU;
783
784             if (flags & Qt::WindowMinimizeButtonHint) {
785                 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
786                 mwmhints.functions |= MWM_FUNC_MINIMIZE;
787             }
788
789             if (flags & Qt::WindowMaximizeButtonHint) {
790                 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
791                 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
792             }
793
794             if (flags & Qt::WindowCloseButtonHint)
795                 mwmhints.functions |= MWM_FUNC_CLOSE;
796         }
797     } else {
798         // if type == Qt::SplashScreen
799         mwmhints.decorations = MWM_DECOR_ALL;
800     }
801
802     if (mwmhints.functions != 0) {
803         mwmhints.flags |= MWM_HINTS_FUNCTIONS;
804         mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
805     } else {
806         mwmhints.functions = MWM_FUNC_ALL;
807     }
808
809     if (!(flags & Qt::FramelessWindowHint)
810         && flags & Qt::CustomizeWindowHint
811         && flags & Qt::WindowTitleHint
812         && !(flags &
813              (Qt::WindowMinimizeButtonHint
814               | Qt::WindowMaximizeButtonHint
815               | Qt::WindowCloseButtonHint)))
816     {
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;
821     }
822
823     setMotifWmHints(connection(), m_window, mwmhints);
824 }
825
826 void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
827 {
828     xcb_client_message_event_t event;
829
830     event.response_type = XCB_CLIENT_MESSAGE;
831     event.format = 32;
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;
839
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));
841 }
842
843 Qt::WindowState QXcbWindow::setWindowState(Qt::WindowState state)
844 {
845     if (state == m_windowState)
846         return state;
847
848     // unset old state
849     switch (m_windowState) {
850     case Qt::WindowMinimized:
851         Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
852         break;
853     case Qt::WindowMaximized:
854         changeNetWmState(false,
855                          atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
856                          atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
857         break;
858     case Qt::WindowFullScreen:
859         changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
860         break;
861     default:
862         break;
863     }
864
865     // set new state
866     switch (state) {
867     case Qt::WindowMinimized:
868         {
869             xcb_client_message_event_t event;
870
871             event.response_type = XCB_CLIENT_MESSAGE;
872             event.format = 32;
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;
880
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));
882         }
883         break;
884     case Qt::WindowMaximized:
885         changeNetWmState(true,
886                          atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
887                          atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
888         break;
889     case Qt::WindowFullScreen:
890         changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
891         break;
892     case Qt::WindowNoState:
893         break;
894     default:
895         break;
896     }
897
898     connection()->sync();
899
900     m_windowState = state;
901     return m_windowState;
902 }
903
904 void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
905 {
906     // in order of decreasing priority
907     QVector<uint> windowTypes;
908
909     Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
910
911     switch (type) {
912     case Qt::Dialog:
913     case Qt::Sheet:
914         windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
915         break;
916     case Qt::Tool:
917     case Qt::Drawer:
918         windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
919         break;
920     case Qt::ToolTip:
921         windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
922         break;
923     case Qt::SplashScreen:
924         windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
925         break;
926     default:
927         break;
928     }
929
930     if (flags & Qt::FramelessWindowHint)
931         windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
932
933     windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
934
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()));
938 }
939
940 void QXcbWindow::updateMotifWmHintsBeforeMap()
941 {
942     QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
943
944     if (window()->windowModality() != Qt::NonModal) {
945         switch (window()->windowModality()) {
946         case Qt::WindowModal:
947             mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
948             break;
949         case Qt::ApplicationModal:
950         default:
951             mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
952             break;
953         }
954         mwmhints.flags |= MWM_HINTS_INPUT_MODE;
955     } else {
956         mwmhints.input_mode = MWM_INPUT_MODELESS;
957         mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
958     }
959
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;
966         } else {
967             mwmhints.functions &= ~MWM_FUNC_RESIZE;
968         }
969
970         if (mwmhints.decorations == MWM_DECOR_ALL) {
971             mwmhints.flags |= MWM_HINTS_DECORATIONS;
972             mwmhints.decorations = (MWM_DECOR_BORDER
973                                     | MWM_DECOR_TITLE
974                                     | MWM_DECOR_MENU);
975         } else {
976             mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
977         }
978     }
979
980     if (window()->windowFlags() & Qt::WindowMinimizeButtonHint) {
981         mwmhints.flags |= MWM_HINTS_DECORATIONS;
982         mwmhints.decorations |= MWM_DECOR_MINIMIZE;
983         mwmhints.functions |= MWM_FUNC_MINIMIZE;
984     }
985     if (window()->windowFlags() & Qt::WindowMaximizeButtonHint) {
986         mwmhints.flags |= MWM_HINTS_DECORATIONS;
987         mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
988         mwmhints.functions |= MWM_FUNC_MAXIMIZE;
989     }
990     if (window()->windowFlags() & Qt::WindowCloseButtonHint)
991         mwmhints.functions |= MWM_FUNC_CLOSE;
992
993     setMotifWmHints(connection(), m_window, mwmhints);
994 }
995
996 void QXcbWindow::updateNetWmStateBeforeMap()
997 {
998     NetWmStates states(0);
999
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;
1006     }
1007
1008     if (window()->windowState() & Qt::WindowFullScreen)
1009         states |= NetWmStateFullScreen;
1010
1011     if (window()->windowState() & Qt::WindowMaximized) {
1012         states |= NetWmStateMaximizedHorz;
1013         states |= NetWmStateMaximizedVert;
1014     }
1015
1016     if (window()->windowModality() != Qt::NonModal)
1017         states |= NetWmStateModal;
1018
1019     setNetWmStates(states);
1020 }
1021
1022 void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
1023 {
1024     xcb_window_t wid = m_window;
1025
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
1034                                          -1, -1, 1, 1,
1035                                          0,                               // border width
1036                                          XCB_WINDOW_CLASS_INPUT_OUTPUT,   // window class
1037                                          m_visualId,                      // visual
1038                                          0,                               // value mask
1039                                          0));                             // value list
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;
1050         } else {
1051             wid = m_netWmUserTimeWindow;
1052         }
1053     }
1054     xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
1055                         XCB_ATOM_CARDINAL, 32, 1, &timestamp);
1056 }
1057
1058 void QXcbWindow::setTransparentForMouseEvents(bool transparent)
1059 {
1060     if (transparent == m_transparent)
1061         return;
1062
1063     xcb_rectangle_t rectangle;
1064
1065     xcb_rectangle_t *rect = 0;
1066     int nrect = 0;
1067
1068     if (!transparent) {
1069         rectangle.x = 0;
1070         rectangle.y = 0;
1071         rectangle.width = geometry().width();
1072         rectangle.height = geometry().height();
1073         rect = &rectangle;
1074         nrect = 1;
1075     }
1076
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);
1081
1082     m_transparent = transparent;
1083 }
1084
1085 void QXcbWindow::updateDoesNotAcceptFocus(bool doesNotAcceptFocus)
1086 {
1087     xcb_get_property_cookie_t cookie = xcb_get_wm_hints_unchecked(xcb_connection(), m_window);
1088
1089     xcb_wm_hints_t hints;
1090     if (!xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, NULL)) {
1091         return;
1092     }
1093
1094     xcb_wm_hints_set_input(&hints, !doesNotAcceptFocus);
1095     xcb_set_wm_hints(xcb_connection(), m_window, &hints);
1096 }
1097
1098 WId QXcbWindow::winId() const
1099 {
1100     return m_window;
1101 }
1102
1103 void QXcbWindow::setParent(const QPlatformWindow *parent)
1104 {
1105     // re-create for compatibility
1106     create();
1107
1108     QPoint topLeft = geometry().topLeft();
1109
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()));
1112 }
1113
1114 void QXcbWindow::setWindowTitle(const QString &title)
1115 {
1116     QByteArray ba = title.toUtf8();
1117     Q_XCB_CALL(xcb_change_property(xcb_connection(),
1118                                    XCB_PROP_MODE_REPLACE,
1119                                    m_window,
1120                                    atom(QXcbAtom::_NET_WM_NAME),
1121                                    atom(QXcbAtom::UTF8_STRING),
1122                                    8,
1123                                    ba.length(),
1124                                    ba.constData()));
1125 }
1126
1127 void QXcbWindow::setWindowIcon(const QIcon &icon)
1128 {
1129     QVector<quint32> icon_data;
1130
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));
1139         }
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);
1150             }
1151         }
1152     }
1153
1154     if (!icon_data.isEmpty()) {
1155         Q_XCB_CALL(xcb_change_property(xcb_connection(),
1156                                        XCB_PROP_MODE_REPLACE,
1157                                        m_window,
1158                                        atom(QXcbAtom::_NET_WM_ICON),
1159                                        atom(QXcbAtom::CARDINAL),
1160                                        32,
1161                                        icon_data.size(),
1162                                        (unsigned char *) icon_data.data()));
1163     } else {
1164         Q_XCB_CALL(xcb_delete_property(xcb_connection(),
1165                                        m_window,
1166                                        atom(QXcbAtom::_NET_WM_ICON)));
1167     }
1168 }
1169
1170 void QXcbWindow::raise()
1171 {
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));
1175 }
1176
1177 void QXcbWindow::lower()
1178 {
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));
1182 }
1183
1184 // Adapt the geometry to match the WM expection with regards
1185 // to gravity.
1186 QRect QXcbWindow::windowToWmGeometry(QRect r) const
1187 {
1188     if (m_dirtyFrameMargins || m_frameMargins.isNull())
1189         return r;
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());
1197     }
1198     return r;
1199 }
1200
1201 void QXcbWindow::propagateSizeHints()
1202 {
1203     // update WM_NORMAL_HINTS
1204     xcb_size_hints_t hints;
1205     memset(&hints, 0, sizeof(hints));
1206
1207     const QRect rect = windowToWmGeometry(geometry());
1208
1209     QWindow *win = window();
1210
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);
1214
1215     QSize minimumSize = win->minimumSize();
1216     QSize maximumSize = win->maximumSize();
1217     QSize baseSize = win->baseSize();
1218     QSize sizeIncrement = win->sizeIncrement();
1219
1220     if (minimumSize.width() > 0 || minimumSize.height() > 0)
1221         xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1222
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()));
1227
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());
1231     }
1232
1233     xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1234 }
1235
1236 void QXcbWindow::requestActivateWindow()
1237 {
1238     if (!m_mapped) {
1239         m_deferredActivation = true;
1240         return;
1241     }
1242     m_deferredActivation = false;
1243
1244     updateNetWmUserTime(connection()->time());
1245
1246     if (window()->isTopLevel()
1247         && connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_ACTIVE_WINDOW))) {
1248         xcb_client_message_event_t event;
1249
1250         event.response_type = XCB_CLIENT_MESSAGE;
1251         event.format = 32;
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;
1260
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));
1262     } else {
1263         Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1264     }
1265
1266     connection()->sync();
1267 }
1268
1269 #if XCB_USE_MAEMO_WINDOW_PROPERTIES
1270 void QXcbWindow::handleContentOrientationChange(Qt::ScreenOrientation orientation)
1271 {
1272     int angle = 0;
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;
1279     }
1280     Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
1281                                    atom(QXcbAtom::MeegoTouchOrientationAngle), XCB_ATOM_CARDINAL, 32,
1282                                    1, &angle));
1283 }
1284 #endif
1285
1286 QSurfaceFormat QXcbWindow::format() const
1287 {
1288     // ### return actual format
1289     return m_format;
1290 }
1291
1292 #if defined(XCB_USE_EGL)
1293 QXcbEGLSurface *QXcbWindow::eglSurface() const
1294 {
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);
1299
1300         m_eglSurface = new QXcbEGLSurface(display, surface);
1301     }
1302
1303     return m_eglSurface;
1304 }
1305 #endif
1306
1307 void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1308 {
1309     QRect rect(event->x, event->y, event->width, event->height);
1310
1311     if (m_exposeRegion.isEmpty())
1312         m_exposeRegion = rect;
1313     else
1314         m_exposeRegion |= rect;
1315
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();
1320     }
1321 }
1322
1323 void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1324 {
1325     if (event->format != 32)
1326         return;
1327
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;
1335
1336             reply.response_type = XCB_CLIENT_MESSAGE;
1337             reply.window = m_screen->root();
1338
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];
1345         } else {
1346             qWarning() << "QXcbWindow: Unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1347         }
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);
1357 #endif
1358     } else if (event->type == atom(QXcbAtom::_XEMBED)) { // QSystemTrayIcon
1359     } else {
1360         qWarning() << "QXcbWindow: Unhandled client message:" << connection()->atomName(event->type);
1361     }
1362 }
1363
1364 void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1365 {
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);
1373         if (reply) {
1374             pos.setX(reply->dst_x);
1375             pos.setY(reply->dst_y);
1376             free(reply);
1377         }
1378     }
1379
1380     QRect rect(pos, QSize(event->width, event->height));
1381
1382     QPlatformWindow::setGeometry(rect);
1383     QWindowSystemInterface::handleGeometryChange(window(), rect);
1384
1385     m_configureNotifyPending = false;
1386
1387     if (m_deferredExpose) {
1388         m_deferredExpose = false;
1389         QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
1390     }
1391
1392     m_dirtyFrameMargins = true;
1393
1394 #if XCB_USE_DRI2
1395     if (m_context)
1396         static_cast<QDri2Context *>(m_context)->resize(rect.size());
1397 #endif
1398 }
1399
1400 bool QXcbWindow::isExposed() const
1401 {
1402     return m_mapped;
1403 }
1404
1405 void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1406 {
1407     if (event->window == m_window) {
1408         m_mapped = true;
1409         if (m_deferredActivation)
1410             requestActivateWindow();
1411         if (m_configureNotifyPending)
1412             m_deferredExpose = true;
1413         else
1414             QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
1415     }
1416 }
1417
1418 void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1419 {
1420     if (event->window == m_window) {
1421         m_mapped = false;
1422         QWindowSystemInterface::handleExposeEvent(window(), QRegion());
1423     }
1424 }
1425
1426 static Qt::MouseButtons translateMouseButtons(int s)
1427 {
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;
1435     return ret;
1436 }
1437
1438 static Qt::MouseButton translateMouseButton(xcb_button_t s)
1439 {
1440     switch (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;
1470     }
1471 }
1472
1473 void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1474 {
1475     updateNetWmUserTime(event->time);
1476
1477     QPoint local(event->event_x, event->event_y);
1478     QPoint global(event->root_x, event->root_y);
1479
1480     Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1481
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));
1488
1489         QWindowSystemInterface::handleWheelEvent(window(), event->time,
1490                                                  local, global, delta, hor ? Qt::Horizontal : Qt::Vertical, modifiers);
1491         return;
1492     }
1493
1494     handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1495 }
1496
1497 void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1498 {
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);
1502
1503     handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1504 }
1505
1506 void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
1507 {
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);
1511
1512     handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1513 }
1514
1515 void QXcbWindow::handleMouseEvent(xcb_button_t detail, uint16_t state, xcb_timestamp_t time, const QPoint &local, const QPoint &global, Qt::KeyboardModifiers modifiers)
1516 {
1517     connection()->setTime(time);
1518
1519     Qt::MouseButtons buttons = translateMouseButtons(state);
1520     Qt::MouseButton button = translateMouseButton(detail);
1521
1522     buttons ^= button; // X event uses state *before*, Qt uses state *after*
1523
1524     QWindowSystemInterface::handleMouseEvent(window(), time, local, global, buttons, modifiers);
1525 }
1526
1527 void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1528 {
1529     connection()->setTime(event->time);
1530
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)
1534     {
1535         return;
1536     }
1537
1538     QWindowSystemInterface::handleEnterEvent(window());
1539 }
1540
1541 void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1542 {
1543     connection()->setTime(event->time);
1544
1545     if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1546         || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)
1547     {
1548         return;
1549     }
1550
1551     QWindowSystemInterface::handleLeaveEvent(window());
1552 }
1553
1554 void QXcbWindow::handlePropertyNotifyEvent(const xcb_property_notify_event_t *event)
1555 {
1556     connection()->setTime(event->time);
1557
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);
1561
1562     if (event->atom == netWmStateAtom || event->atom == wmStateAtom) {
1563         if (propertyDeleted)
1564             return;
1565
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);
1571
1572             xcb_get_property_reply_t *reply =
1573                 xcb_get_property_reply(xcb_connection(), get_cookie, NULL);
1574
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;
1579                 free(reply);
1580             }
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;
1588         }
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;
1593         }
1594     }
1595 }
1596
1597 void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1598 {
1599     QWindowSystemInterface::handleWindowActivated(window());
1600 }
1601
1602 static bool focusInPeeker(xcb_generic_event_t *event)
1603 {
1604     if (!event) {
1605         // FocusIn event is not in the queue, proceed with FocusOut normally.
1606         QWindowSystemInterface::handleWindowActivated(0);
1607         return true;
1608     }
1609     uint response_type = event->response_type & ~0x80;
1610     return response_type == XCB_FOCUS_IN;
1611 }
1612
1613 void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1614 {
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);
1619 }
1620
1621 void QXcbWindow::updateSyncRequestCounter()
1622 {
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();
1627
1628         m_syncValue.lo = 0;
1629         m_syncValue.hi = 0;
1630     }
1631 }
1632
1633 bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1634 {
1635     if (!grab) {
1636         xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1637         return true;
1638     }
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);
1644     free(reply);
1645     return result;
1646 }
1647
1648 bool QXcbWindow::setMouseGrabEnabled(bool grab)
1649 {
1650     if (!grab) {
1651         xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1652         return true;
1653     }
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);
1663     free(reply);
1664     return result;
1665 }
1666
1667 void QXcbWindow::setCursor(xcb_cursor_t cursor)
1668 {
1669     xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1670     xcb_flush(xcb_connection());
1671 }
1672
1673 QT_END_NAMESPACE