1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the plugins of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qxcbwindow.h"
47 #include "qxcbconnection.h"
48 #include "qxcbscreen.h"
50 #include "qxcbkeyboard.h"
51 #include "qxcbwmsupport.h"
53 #include <qplatformintegration_qpa.h>
56 #include "qdri2context.h"
59 // FIXME This workaround can be removed for xcb-icccm > 3.8
60 #define class class_name
61 #include <xcb/xcb_icccm.h>
63 #include <xcb/xfixes.h>
65 // xcb-icccm 3.8 support
66 #ifdef XCB_ICCCM_NUM_WM_SIZE_HINTS_ELEMENTS
67 #define xcb_get_wm_hints_reply xcb_icccm_get_wm_hints_reply
68 #define xcb_get_wm_hints xcb_icccm_get_wm_hints
69 #define xcb_set_wm_hints xcb_icccm_set_wm_hints
70 #define xcb_set_wm_normal_hints xcb_icccm_set_wm_normal_hints
71 #define xcb_size_hints_set_base_size xcb_icccm_size_hints_set_base_size
72 #define xcb_size_hints_set_max_size xcb_icccm_size_hints_set_max_size
73 #define xcb_size_hints_set_min_size xcb_icccm_size_hints_set_min_size
74 #define xcb_size_hints_set_position xcb_icccm_size_hints_set_position
75 #define xcb_size_hints_set_resize_inc xcb_icccm_size_hints_set_resize_inc
76 #define xcb_size_hints_set_size xcb_icccm_size_hints_set_size
77 #define xcb_size_hints_set_win_gravity xcb_icccm_size_hints_set_win_gravity
78 #define xcb_wm_hints_set_iconic xcb_icccm_wm_hints_set_iconic
79 #define xcb_wm_hints_set_normal xcb_icccm_wm_hints_set_normal
80 #define xcb_wm_hints_set_input xcb_icccm_wm_hints_set_input
81 #define xcb_wm_hints_t xcb_icccm_wm_hints_t
82 #define XCB_WM_STATE_ICONIC XCB_ICCCM_WM_STATE_ICONIC
83 #define XCB_WM_STATE_WITHDRAWN XCB_ICCCM_WM_STATE_WITHDRAWN
86 #include <private/qguiapplication_p.h>
87 #include <private/qwindow_p.h>
89 #include <QtGui/QPlatformBackingStore>
90 #include <QtGui/QWindowSystemInterface>
96 #include <X11/Xutil.h>
99 #ifdef XCB_USE_XINPUT2_MAEMO
100 #include <X11/extensions/XInput2.h>
103 #if defined(XCB_USE_GLX)
104 #include "qglxintegration.h"
105 #include <QtPlatformSupport/private/qglxconvenience_p.h>
106 #elif defined(XCB_USE_EGL)
107 #include "qxcbeglsurface.h"
108 #include <QtPlatformSupport/private/qeglconvenience_p.h>
109 #include <QtPlatformSupport/private/qxlibeglintegration_p.h>
112 #define XCOORD_MAX 16383
114 //#ifdef NET_WM_STATE_DEBUG
118 // Returns true if we should set WM_TRANSIENT_FOR on \a w
119 static inline bool isTransient(const QWindow *w)
121 return w->windowType() == Qt::Dialog
122 || w->windowType() == Qt::Sheet
123 || w->windowType() == Qt::Tool
124 || w->windowType() == Qt::SplashScreen
125 || w->windowType() == Qt::ToolTip
126 || w->windowType() == Qt::Drawer
127 || w->windowType() == Qt::Popup;
130 static inline QImage::Format imageFormatForDepth(int depth)
133 case 32: return QImage::Format_ARGB32_Premultiplied;
134 case 24: return QImage::Format_RGB32;
135 case 16: return QImage::Format_RGB16;
136 default: return QImage::Format_Invalid;
140 QXcbWindow::QXcbWindow(QWindow *window)
141 : QPlatformWindow(window)
145 , m_transparent(false)
146 , m_deferredActivation(false)
147 , m_netWmUserTimeWindow(XCB_NONE)
148 #if defined(XCB_USE_EGL)
152 m_screen = static_cast<QXcbScreen *>(window->screen()->handle());
154 setConnection(m_screen->connection());
159 void QXcbWindow::create()
163 m_windowState = Qt::WindowNoState;
165 Qt::WindowType type = window()->windowType();
167 if (type == Qt::Desktop) {
168 m_window = m_screen->root();
169 m_depth = m_screen->screen()->root_depth;
170 m_imageFormat = imageFormatForDepth(m_depth);
171 connection()->addWindow(m_window, this);
175 const quint32 mask = XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK;
176 const quint32 values[] = {
177 // XCB_CW_BACK_PIXMAP
179 // XCB_CW_OVERRIDE_REDIRECT
180 type == Qt::Popup || type == Qt::ToolTip,
182 type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
184 XCB_EVENT_MASK_EXPOSURE
185 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
186 | XCB_EVENT_MASK_KEY_PRESS
187 | XCB_EVENT_MASK_KEY_RELEASE
188 | XCB_EVENT_MASK_BUTTON_PRESS
189 | XCB_EVENT_MASK_BUTTON_RELEASE
190 | XCB_EVENT_MASK_BUTTON_MOTION
191 | XCB_EVENT_MASK_ENTER_WINDOW
192 | XCB_EVENT_MASK_LEAVE_WINDOW
193 | XCB_EVENT_MASK_POINTER_MOTION
194 | XCB_EVENT_MASK_PROPERTY_CHANGE
195 | XCB_EVENT_MASK_FOCUS_CHANGE
198 QRect rect = window()->geometry();
199 QPlatformWindow::setGeometry(rect);
201 rect.setWidth(qBound(1, rect.width(), XCOORD_MAX));
202 rect.setHeight(qBound(1, rect.height(), XCOORD_MAX));
204 xcb_window_t xcb_parent_id = m_screen->root();
206 xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
208 m_format = window()->requestedFormat();
210 #if (defined(XCB_USE_GLX) || defined(XCB_USE_EGL)) && defined(XCB_USE_XLIB)
211 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL)
212 || m_format.hasAlpha())
214 #if defined(XCB_USE_GLX)
215 XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(m_screen), m_screen->screenNumber(), &m_format);
217 qFatal("Could not initialize GLX");
218 #elif defined(XCB_USE_EGL)
219 EGLDisplay eglDisplay = connection()->egl_display();
220 EGLConfig eglConfig = q_configFromGLFormat(eglDisplay, m_format, true);
221 m_format = q_glFormatFromConfig(eglDisplay, eglConfig);
223 VisualID id = QXlibEglIntegration::getCompatibleVisualId(DISPLAY_FROM_XCB(this), eglDisplay, eglConfig);
225 XVisualInfo visualInfoTemplate;
226 memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
227 visualInfoTemplate.visualid = id;
229 XVisualInfo *visualInfo;
230 int matchingCount = 0;
231 visualInfo = XGetVisualInfo(DISPLAY_FROM_XCB(this), VisualIDMask, &visualInfoTemplate, &matchingCount);
233 qFatal("Could not initialize EGL");
235 m_depth = visualInfo->depth;
236 m_imageFormat = imageFormatForDepth(m_depth);
237 Colormap cmap = XCreateColormap(DISPLAY_FROM_XCB(this), xcb_parent_id, visualInfo->visual, AllocNone);
239 XSetWindowAttributes a;
240 a.background_pixel = WhitePixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
241 a.border_pixel = BlackPixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
244 m_visualId = visualInfo->visualid;
246 m_window = XCreateWindow(DISPLAY_FROM_XCB(this), xcb_parent_id, rect.x(), rect.y(), rect.width(), rect.height(),
247 0, visualInfo->depth, InputOutput, visualInfo->visual,
248 CWBackPixel|CWBorderPixel|CWColormap, &a);
252 #endif //defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
254 m_window = xcb_generate_id(xcb_connection());
255 m_depth = m_screen->screen()->root_depth;
256 m_imageFormat = imageFormatForDepth(m_depth);
257 m_visualId = m_screen->screen()->root_visual;
259 Q_XCB_CALL(xcb_create_window(xcb_connection(),
260 XCB_COPY_FROM_PARENT, // depth -- same as root
261 m_window, // window id
262 xcb_parent_id, // parent window id
268 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
269 m_visualId, // visual
274 connection()->addWindow(m_window, this);
276 Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
278 propagateSizeHints();
280 xcb_atom_t properties[4];
281 int propertyCount = 0;
282 properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
283 properties[propertyCount++] = atom(QXcbAtom::WM_TAKE_FOCUS);
284 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_PING);
286 if (m_screen->syncRequestSupported())
287 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
289 if (window()->windowFlags() & Qt::WindowContextHelpButtonHint)
290 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
292 Q_XCB_CALL(xcb_change_property(xcb_connection(),
293 XCB_PROP_MODE_REPLACE,
295 atom(QXcbAtom::WM_PROTOCOLS),
303 if (m_screen->syncRequestSupported()) {
304 m_syncCounter = xcb_generate_id(xcb_connection());
305 Q_XCB_CALL(xcb_sync_create_counter(xcb_connection(), m_syncCounter, m_syncValue));
307 Q_XCB_CALL(xcb_change_property(xcb_connection(),
308 XCB_PROP_MODE_REPLACE,
310 atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
317 // set the PID to let the WM kill the application if unresponsive
319 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
320 atom(QXcbAtom::_NET_WM_PID), XCB_ATOM_CARDINAL, 32,
323 xcb_wm_hints_t hints;
324 memset(&hints, 0, sizeof(hints));
325 xcb_wm_hints_set_normal(&hints);
327 xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
329 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
331 xcb_window_t leader = m_screen->clientLeader();
332 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
333 atom(QXcbAtom::WM_CLIENT_LEADER), XCB_ATOM_WINDOW, 32,
336 #ifdef XCB_USE_XINPUT2_MAEMO
337 if (connection()->isUsingXInput2()) {
338 XIEventMask xieventmask;
339 uchar bitmask[2] = { 0, 0 };
341 xieventmask.deviceid = XIAllMasterDevices;
342 xieventmask.mask = bitmask;
343 xieventmask.mask_len = sizeof(bitmask);
345 XISetMask(bitmask, XI_ButtonPress);
346 XISetMask(bitmask, XI_ButtonRelease);
347 XISetMask(bitmask, XI_Motion);
349 XISelectEvents(DISPLAY_FROM_XCB(this), m_window, &xieventmask, 1);
353 setWindowFlags(window()->windowFlags());
354 setWindowTitle(window()->windowTitle());
355 setWindowState(window()->windowState());
357 if (window()->windowFlags() & Qt::WindowTransparentForInput)
358 setTransparentForMouseEvents(true);
360 connection()->drag()->dndEnable(this, true);
363 QXcbWindow::~QXcbWindow()
368 void QXcbWindow::destroy()
370 if (m_syncCounter && m_screen->syncRequestSupported())
371 Q_XCB_CALL(xcb_sync_destroy_counter(xcb_connection(), m_syncCounter));
373 if (m_netWmUserTimeWindow) {
374 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
375 // Some window managers, like metacity, do XSelectInput on the _NET_WM_USER_TIME_WINDOW window,
376 // without trapping BadWindow (which crashes when the user time window is destroyed).
377 connection()->sync();
378 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
379 m_netWmUserTimeWindow = XCB_NONE;
381 connection()->removeWindow(m_window);
382 Q_XCB_CALL(xcb_destroy_window(xcb_connection(), m_window));
386 #if defined(XCB_USE_EGL)
392 void QXcbWindow::setGeometry(const QRect &rect)
394 QPlatformWindow::setGeometry(rect);
396 propagateSizeHints();
398 const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
399 const quint32 values[] = { rect.x(),
401 qBound(1, rect.width(), XCOORD_MAX),
402 qBound(1, rect.height(), XCOORD_MAX) };
404 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
406 xcb_flush(xcb_connection());
409 QMargins QXcbWindow::frameMargins() const
411 if (m_dirtyFrameMargins) {
412 xcb_window_t window = m_window;
413 xcb_window_t parent = m_window;
415 bool foundRoot = false;
417 const QVector<xcb_window_t> &virtualRoots =
418 connection()->wmSupport()->virtualRoots();
421 xcb_query_tree_cookie_t cookie = xcb_query_tree(xcb_connection(), parent);
423 xcb_generic_error_t *error;
424 xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, &error);
426 if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1) {
430 parent = reply->parent;
436 connection()->handleXcbError(error);
440 m_dirtyFrameMargins = false;
441 m_frameMargins = QMargins();
442 return m_frameMargins;
448 xcb_generic_error_t *error;
449 xcb_translate_coordinates_reply_t *reply =
450 xcb_translate_coordinates_reply(
452 xcb_translate_coordinates(xcb_connection(), window, parent, 0, 0),
456 offset = QPoint(reply->dst_x, reply->dst_y);
462 xcb_get_geometry_reply_t *geom =
463 xcb_get_geometry_reply(
465 xcb_get_geometry(xcb_connection(), parent),
470 // add the border_width for the window managers frame... some window managers
471 // do not use a border_width of zero for their frames, and if we the left and
472 // top strut, we ensure that pos() is absolutely correct. frameGeometry()
473 // will still be incorrect though... perhaps i should have foffset as well, to
474 // indicate the frame offset (equal to the border_width on X).
476 // -- copied from qwidget_x11.cpp
478 int left = offset.x() + geom->border_width;
479 int top = offset.y() + geom->border_width;
480 int right = geom->width + geom->border_width - geometry().width() - offset.x();
481 int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
483 m_frameMargins = QMargins(left, top, right, bottom);
490 m_dirtyFrameMargins = false;
493 return m_frameMargins;
496 void QXcbWindow::setVisible(bool visible)
504 void QXcbWindow::show()
506 if (window()->isTopLevel()) {
507 xcb_get_property_cookie_t cookie = xcb_get_wm_hints(xcb_connection(), m_window);
509 xcb_generic_error_t *error;
511 xcb_wm_hints_t hints;
512 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, &error);
515 connection()->handleXcbError(error);
519 if (window()->windowState() & Qt::WindowMinimized)
520 xcb_wm_hints_set_iconic(&hints);
522 xcb_wm_hints_set_normal(&hints);
524 xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
526 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
528 // update WM_NORMAL_HINTS
529 propagateSizeHints();
531 // update WM_TRANSIENT_FOR
532 if (window()->transientParent() && isTransient(window())) {
533 QXcbWindow *transientXcbParent = static_cast<QXcbWindow *>(window()->transientParent()->handle());
534 if (transientXcbParent) {
536 xcb_window_t parentWindow = transientXcbParent->xcb_window();
538 // todo: set transient for group (wm_client_leader) if no parent, a la qwidget_x11.cpp
539 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
540 XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 32,
545 // update _MOTIF_WM_HINTS
546 updateMotifWmHintsBeforeMap();
548 // update _NET_WM_STATE
549 updateNetWmStateBeforeMap();
552 updateNetWmUserTime(connection()->time());
554 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
555 xcb_flush(xcb_connection());
557 connection()->sync();
560 void QXcbWindow::hide()
562 Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
564 // send synthetic UnmapNotify event according to icccm 4.1.4
565 xcb_unmap_notify_event_t event;
566 event.response_type = XCB_UNMAP_NOTIFY;
567 event.event = m_screen->root();
568 event.window = m_window;
569 event.from_configure = false;
570 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, m_screen->root(),
571 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
573 xcb_flush(xcb_connection());
578 struct QtMotifWmHints {
579 quint32 flags, functions, decorations;
585 MWM_HINTS_FUNCTIONS = (1L << 0),
587 MWM_FUNC_ALL = (1L << 0),
588 MWM_FUNC_RESIZE = (1L << 1),
589 MWM_FUNC_MOVE = (1L << 2),
590 MWM_FUNC_MINIMIZE = (1L << 3),
591 MWM_FUNC_MAXIMIZE = (1L << 4),
592 MWM_FUNC_CLOSE = (1L << 5),
594 MWM_HINTS_DECORATIONS = (1L << 1),
596 MWM_DECOR_ALL = (1L << 0),
597 MWM_DECOR_BORDER = (1L << 1),
598 MWM_DECOR_RESIZEH = (1L << 2),
599 MWM_DECOR_TITLE = (1L << 3),
600 MWM_DECOR_MENU = (1L << 4),
601 MWM_DECOR_MINIMIZE = (1L << 5),
602 MWM_DECOR_MAXIMIZE = (1L << 6),
604 MWM_HINTS_INPUT_MODE = (1L << 2),
606 MWM_INPUT_MODELESS = 0L,
607 MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
608 MWM_INPUT_FULL_APPLICATION_MODAL = 3L
611 static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
613 QtMotifWmHints hints;
615 xcb_get_property_cookie_t get_cookie =
616 xcb_get_property(c->xcb_connection(), 0, window, c->atom(QXcbAtom::_MOTIF_WM_HINTS),
617 c->atom(QXcbAtom::_MOTIF_WM_HINTS), 0, 20);
619 xcb_generic_error_t *error;
621 xcb_get_property_reply_t *reply =
622 xcb_get_property_reply(c->xcb_connection(), get_cookie, &error);
624 if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
625 hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
627 c->handleXcbError(error);
631 hints.functions = MWM_FUNC_ALL;
632 hints.decorations = MWM_DECOR_ALL;
633 hints.input_mode = 0L;
642 static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
644 if (hints.flags != 0l) {
645 Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
646 XCB_PROP_MODE_REPLACE,
648 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
649 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
654 Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
658 void QXcbWindow::printNetWmState(const QVector<xcb_atom_t> &state)
660 printf("_NET_WM_STATE (%d): ", state.size());
661 for (int i = 0; i < state.size(); ++i) {
662 #define CHECK_WM_STATE(state_atom) \
663 if (state.at(i) == atom(QXcbAtom::state_atom))\
664 printf(#state_atom " ");
665 CHECK_WM_STATE(_NET_WM_STATE_ABOVE)
666 CHECK_WM_STATE(_NET_WM_STATE_BELOW)
667 CHECK_WM_STATE(_NET_WM_STATE_FULLSCREEN)
668 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_HORZ)
669 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_VERT)
670 CHECK_WM_STATE(_NET_WM_STATE_MODAL)
671 CHECK_WM_STATE(_NET_WM_STATE_STAYS_ON_TOP)
672 CHECK_WM_STATE(_NET_WM_STATE_DEMANDS_ATTENTION)
673 #undef CHECK_WM_STATE
678 QVector<xcb_atom_t> QXcbWindow::getNetWmState()
680 QVector<xcb_atom_t> result;
682 xcb_get_property_cookie_t get_cookie =
683 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::_NET_WM_STATE),
684 XCB_ATOM_ATOM, 0, 1024);
686 xcb_generic_error_t *error;
688 xcb_get_property_reply_t *reply =
689 xcb_get_property_reply(xcb_connection(), get_cookie, &error);
691 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
692 result.resize(reply->length);
694 memcpy(result.data(), xcb_get_property_value(reply), reply->length * sizeof(xcb_atom_t));
696 #ifdef NET_WM_STATE_DEBUG
697 printf("getting net wm state (%x)\n", m_window);
698 printNetWmState(result);
703 connection()->handleXcbError(error);
706 #ifdef NET_WM_STATE_DEBUG
707 printf("getting net wm state (%x), empty\n", m_window);
714 void QXcbWindow::setNetWmState(const QVector<xcb_atom_t> &atoms)
716 if (atoms.isEmpty()) {
717 Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
719 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
720 atom(QXcbAtom::_NET_WM_STATE), XCB_ATOM_ATOM, 32,
721 atoms.count(), atoms.constData()));
723 xcb_flush(xcb_connection());
727 Qt::WindowFlags QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
729 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
731 if (type == Qt::ToolTip)
732 flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
733 if (type == Qt::Popup)
734 flags |= Qt::X11BypassWindowManagerHint;
736 if (flags & Qt::WindowTransparentForInput) {
737 uint32_t mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_VISIBILITY_CHANGE
738 | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_RESIZE_REDIRECT
739 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
740 | XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_PROPERTY_CHANGE
741 | XCB_EVENT_MASK_COLOR_MAP_CHANGE | XCB_EVENT_MASK_OWNER_GRAB_BUTTON;
742 xcb_change_window_attributes(xcb_connection(), xcb_window(), XCB_CW_EVENT_MASK, &mask);
745 setNetWmWindowFlags(flags);
746 setMotifWindowFlags(flags);
748 setTransparentForMouseEvents(flags & Qt::WindowTransparentForInput);
749 updateDoesNotAcceptFocus(flags & Qt::WindowDoesNotAcceptFocus);
754 void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
756 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
758 QtMotifWmHints mwmhints;
760 mwmhints.functions = 0L;
761 mwmhints.decorations = 0;
762 mwmhints.input_mode = 0L;
763 mwmhints.status = 0L;
765 if (type != Qt::SplashScreen) {
766 mwmhints.flags |= MWM_HINTS_DECORATIONS;
768 bool customize = flags & Qt::CustomizeWindowHint;
769 if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
770 mwmhints.decorations |= MWM_DECOR_BORDER;
771 mwmhints.decorations |= MWM_DECOR_RESIZEH;
773 if (flags & Qt::WindowTitleHint)
774 mwmhints.decorations |= MWM_DECOR_TITLE;
776 if (flags & Qt::WindowSystemMenuHint)
777 mwmhints.decorations |= MWM_DECOR_MENU;
779 if (flags & Qt::WindowMinimizeButtonHint) {
780 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
781 mwmhints.functions |= MWM_FUNC_MINIMIZE;
784 if (flags & Qt::WindowMaximizeButtonHint) {
785 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
786 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
789 if (flags & Qt::WindowCloseButtonHint)
790 mwmhints.functions |= MWM_FUNC_CLOSE;
793 // if type == Qt::SplashScreen
794 mwmhints.decorations = MWM_DECOR_ALL;
797 if (mwmhints.functions != 0) {
798 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
799 mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
801 mwmhints.functions = MWM_FUNC_ALL;
804 if (!(flags & Qt::FramelessWindowHint)
805 && flags & Qt::CustomizeWindowHint
806 && flags & Qt::WindowTitleHint
808 (Qt::WindowMinimizeButtonHint
809 | Qt::WindowMaximizeButtonHint
810 | Qt::WindowCloseButtonHint)))
812 // a special case - only the titlebar without any button
813 mwmhints.flags = MWM_HINTS_FUNCTIONS;
814 mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
815 mwmhints.decorations = 0;
818 setMotifWmHints(connection(), m_window, mwmhints);
821 void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
823 xcb_client_message_event_t event;
825 event.response_type = XCB_CLIENT_MESSAGE;
827 event.window = m_window;
828 event.type = atom(QXcbAtom::_NET_WM_STATE);
829 event.data.data32[0] = set ? 1 : 0;
830 event.data.data32[1] = one;
831 event.data.data32[2] = two;
832 event.data.data32[3] = 0;
833 event.data.data32[4] = 0;
835 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));
838 Qt::WindowState QXcbWindow::setWindowState(Qt::WindowState state)
840 if (state == m_windowState)
844 switch (m_windowState) {
845 case Qt::WindowMinimized:
846 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
848 case Qt::WindowMaximized:
849 changeNetWmState(false,
850 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
851 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
853 case Qt::WindowFullScreen:
854 changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
862 case Qt::WindowMinimized:
864 xcb_client_message_event_t event;
866 event.response_type = XCB_CLIENT_MESSAGE;
868 event.window = m_window;
869 event.type = atom(QXcbAtom::WM_CHANGE_STATE);
870 event.data.data32[0] = XCB_WM_STATE_ICONIC;
871 event.data.data32[1] = 0;
872 event.data.data32[2] = 0;
873 event.data.data32[3] = 0;
874 event.data.data32[4] = 0;
876 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));
879 case Qt::WindowMaximized:
880 changeNetWmState(true,
881 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
882 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
884 case Qt::WindowFullScreen:
885 changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
887 case Qt::WindowNoState:
893 connection()->sync();
895 m_windowState = state;
896 return m_windowState;
899 void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
901 // in order of decreasing priority
902 QVector<uint> windowTypes;
904 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
909 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
913 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
916 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
918 case Qt::SplashScreen:
919 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
925 if (flags & Qt::FramelessWindowHint)
926 windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
928 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
930 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
931 atom(QXcbAtom::_NET_WM_WINDOW_TYPE), XCB_ATOM_ATOM, 32,
932 windowTypes.count(), windowTypes.constData()));
935 void QXcbWindow::updateMotifWmHintsBeforeMap()
937 QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
939 if (window()->windowModality() != Qt::NonModal) {
940 switch (window()->windowModality()) {
941 case Qt::WindowModal:
942 mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
944 case Qt::ApplicationModal:
946 mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
949 mwmhints.flags |= MWM_HINTS_INPUT_MODE;
951 mwmhints.input_mode = MWM_INPUT_MODELESS;
952 mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
955 if (window()->minimumSize() == window()->maximumSize()) {
956 // fixed size, remove the resize handle (since mwm/dtwm
957 // isn't smart enough to do it itself)
958 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
959 if (mwmhints.functions == MWM_FUNC_ALL) {
960 mwmhints.functions = MWM_FUNC_MOVE;
962 mwmhints.functions &= ~MWM_FUNC_RESIZE;
965 if (mwmhints.decorations == MWM_DECOR_ALL) {
966 mwmhints.flags |= MWM_HINTS_DECORATIONS;
967 mwmhints.decorations = (MWM_DECOR_BORDER
971 mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
975 if (window()->windowFlags() & Qt::WindowMinimizeButtonHint) {
976 mwmhints.flags |= MWM_HINTS_DECORATIONS;
977 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
978 mwmhints.functions |= MWM_FUNC_MINIMIZE;
980 if (window()->windowFlags() & Qt::WindowMaximizeButtonHint) {
981 mwmhints.flags |= MWM_HINTS_DECORATIONS;
982 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
983 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
985 if (window()->windowFlags() & Qt::WindowCloseButtonHint)
986 mwmhints.functions |= MWM_FUNC_CLOSE;
988 setMotifWmHints(connection(), m_window, mwmhints);
991 void QXcbWindow::updateNetWmStateBeforeMap()
993 QVector<xcb_atom_t> netWmState;
995 Qt::WindowFlags flags = window()->windowFlags();
996 if (flags & Qt::WindowStaysOnTopHint) {
997 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_ABOVE));
998 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP));
999 } else if (flags & Qt::WindowStaysOnBottomHint) {
1000 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_BELOW));
1003 if (window()->windowState() & Qt::WindowFullScreen) {
1004 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
1007 if (window()->windowState() & Qt::WindowMaximized) {
1008 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ));
1009 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
1012 if (window()->windowModality() != Qt::NonModal) {
1013 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MODAL));
1016 setNetWmState(netWmState);
1019 void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
1021 xcb_window_t wid = m_window;
1023 const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1024 if (m_netWmUserTimeWindow || isSupportedByWM) {
1025 if (!m_netWmUserTimeWindow) {
1026 m_netWmUserTimeWindow = xcb_generate_id(xcb_connection());
1027 Q_XCB_CALL(xcb_create_window(xcb_connection(),
1028 XCB_COPY_FROM_PARENT, // depth -- same as root
1029 m_netWmUserTimeWindow, // window id
1030 m_window, // parent window id
1033 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
1034 m_visualId, // visual
1037 wid = m_netWmUserTimeWindow;
1038 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW),
1039 XCB_ATOM_WINDOW, 32, 1, &m_netWmUserTimeWindow);
1040 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME));
1041 } else if (!isSupportedByWM) {
1042 // WM no longer supports it, then we should remove the
1043 // _NET_WM_USER_TIME_WINDOW atom.
1044 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1045 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
1046 m_netWmUserTimeWindow = XCB_NONE;
1048 wid = m_netWmUserTimeWindow;
1051 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
1052 XCB_ATOM_CARDINAL, 32, 1, ×tamp);
1055 void QXcbWindow::setTransparentForMouseEvents(bool transparent)
1057 if (transparent == m_transparent)
1060 xcb_rectangle_t rectangle;
1062 xcb_rectangle_t *rect = 0;
1068 rectangle.width = geometry().width();
1069 rectangle.height = geometry().height();
1074 xcb_xfixes_region_t region = xcb_generate_id(xcb_connection());
1075 xcb_xfixes_create_region(xcb_connection(), region, nrect, rect);
1076 xcb_xfixes_set_window_shape_region_checked(xcb_connection(), m_window, XCB_SHAPE_SK_INPUT, 0, 0, region);
1077 xcb_xfixes_destroy_region(xcb_connection(), region);
1079 m_transparent = transparent;
1082 void QXcbWindow::updateDoesNotAcceptFocus(bool doesNotAcceptFocus)
1084 xcb_get_property_cookie_t cookie = xcb_get_wm_hints(xcb_connection(), m_window);
1086 xcb_generic_error_t *error;
1088 xcb_wm_hints_t hints;
1089 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, &error);
1092 connection()->handleXcbError(error);
1097 xcb_wm_hints_set_input(&hints, !doesNotAcceptFocus);
1098 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
1101 WId QXcbWindow::winId() const
1106 void QXcbWindow::setParent(const QPlatformWindow *parent)
1108 // re-create for compatibility
1111 QPoint topLeft = geometry().topLeft();
1113 xcb_window_t xcb_parent_id = parent ? static_cast<const QXcbWindow *>(parent)->xcb_window() : m_screen->root();
1114 Q_XCB_CALL(xcb_reparent_window(xcb_connection(), xcb_window(), xcb_parent_id, topLeft.x(), topLeft.y()));
1117 void QXcbWindow::setWindowTitle(const QString &title)
1119 QByteArray ba = title.toUtf8();
1120 Q_XCB_CALL(xcb_change_property(xcb_connection(),
1121 XCB_PROP_MODE_REPLACE,
1123 atom(QXcbAtom::_NET_WM_NAME),
1124 atom(QXcbAtom::UTF8_STRING),
1130 void QXcbWindow::raise()
1132 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1133 const quint32 values[] = { XCB_STACK_MODE_ABOVE };
1134 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1137 void QXcbWindow::lower()
1139 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1140 const quint32 values[] = { XCB_STACK_MODE_BELOW };
1141 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1144 void QXcbWindow::propagateSizeHints()
1146 // update WM_NORMAL_HINTS
1147 xcb_size_hints_t hints;
1148 memset(&hints, 0, sizeof(hints));
1150 QRect rect = geometry();
1152 QWindow *win = window();
1154 xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
1155 xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
1156 xcb_size_hints_set_win_gravity(&hints, qt_window_private(win)->positionPolicy == QWindowPrivate::WindowFrameInclusive
1157 ? XCB_GRAVITY_NORTH_WEST : XCB_GRAVITY_STATIC);
1159 QSize minimumSize = win->minimumSize();
1160 QSize maximumSize = win->maximumSize();
1161 QSize baseSize = win->baseSize();
1162 QSize sizeIncrement = win->sizeIncrement();
1164 if (minimumSize.width() > 0 || minimumSize.height() > 0)
1165 xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1167 if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1168 xcb_size_hints_set_max_size(&hints,
1169 qMin(XCOORD_MAX, maximumSize.width()),
1170 qMin(XCOORD_MAX, maximumSize.height()));
1172 if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1173 xcb_size_hints_set_base_size(&hints, baseSize.width(), baseSize.height());
1174 xcb_size_hints_set_resize_inc(&hints, sizeIncrement.width(), sizeIncrement.height());
1177 xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1180 void QXcbWindow::requestActivateWindow()
1183 m_deferredActivation = true;
1186 m_deferredActivation = false;
1188 updateNetWmUserTime(connection()->time());
1190 if (window()->isTopLevel()
1191 && connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_ACTIVE_WINDOW))) {
1192 xcb_client_message_event_t event;
1194 event.response_type = XCB_CLIENT_MESSAGE;
1196 event.window = m_window;
1197 event.type = atom(QXcbAtom::_NET_ACTIVE_WINDOW);
1198 event.data.data32[0] = 1;
1199 event.data.data32[1] = connection()->time();
1200 QWindow *focusWindow = QGuiApplication::focusWindow();
1201 event.data.data32[2] = focusWindow ? focusWindow->winId() : XCB_NONE;
1202 event.data.data32[3] = 0;
1203 event.data.data32[4] = 0;
1205 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));
1207 Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1210 connection()->sync();
1213 #if XCB_USE_MAEMO_WINDOW_PROPERTIES
1214 void QXcbWindow::setOrientation(Qt::ScreenOrientation orientation)
1217 switch (orientation) {
1218 case Qt::PortraitOrientation: angle = 270; break;
1219 case Qt::LandscapeOrientation: angle = 0; break;
1220 case Qt::InvertedPortraitOrientation: angle = 90; break;
1221 case Qt::InvertedLandscapeOrientation: angle = 180; break;
1222 case Qt::PrimaryOrientation: break;
1224 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
1225 atom(QXcbAtom::MeegoTouchOrientationAngle), XCB_ATOM_CARDINAL, 32,
1230 QSurfaceFormat QXcbWindow::format() const
1232 // ### return actual format
1236 #if defined(XCB_USE_EGL)
1237 QXcbEGLSurface *QXcbWindow::eglSurface() const
1239 if (!m_eglSurface) {
1240 EGLDisplay display = connection()->egl_display();
1241 EGLConfig config = q_configFromGLFormat(display, window()->requestedFormat(), true);
1242 EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)m_window, 0);
1244 m_eglSurface = new QXcbEGLSurface(display, surface);
1247 return m_eglSurface;
1251 void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1253 QRect rect(event->x, event->y, event->width, event->height);
1255 if (m_exposeRegion.isEmpty())
1256 m_exposeRegion = rect;
1258 m_exposeRegion |= rect;
1260 // if count is non-zero there are more expose events pending
1261 if (event->count == 0) {
1262 QWindowSystemInterface::handleSynchronousExposeEvent(window(), m_exposeRegion);
1263 m_exposeRegion = QRegion();
1267 void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1269 if (event->format != 32)
1272 if (event->type == atom(QXcbAtom::WM_PROTOCOLS)) {
1273 if (event->data.data32[0] == atom(QXcbAtom::WM_DELETE_WINDOW)) {
1274 QWindowSystemInterface::handleCloseEvent(window());
1275 } else if (event->data.data32[0] == atom(QXcbAtom::WM_TAKE_FOCUS)) {
1276 connection()->setTime(event->data.data32[1]);
1277 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_PING)) {
1278 xcb_client_message_event_t reply = *event;
1280 reply.response_type = XCB_CLIENT_MESSAGE;
1281 reply.window = m_screen->root();
1283 xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&reply);
1284 xcb_flush(xcb_connection());
1285 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_SYNC_REQUEST)) {
1286 connection()->setTime(event->data.data32[1]);
1287 m_syncValue.lo = event->data.data32[2];
1288 m_syncValue.hi = event->data.data32[3];
1290 qWarning() << "QXcbWindow: Unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1292 } else if (event->type == atom(QXcbAtom::XdndEnter)) {
1293 connection()->drag()->handleEnter(window(), event);
1294 } else if (event->type == atom(QXcbAtom::XdndPosition)) {
1295 connection()->drag()->handlePosition(window(), event);
1296 } else if (event->type == atom(QXcbAtom::XdndLeave)) {
1297 connection()->drag()->handleLeave(window(), event);
1298 } else if (event->type == atom(QXcbAtom::XdndDrop)) {
1299 connection()->drag()->handleDrop(window(), event);
1301 qWarning() << "QXcbWindow: Unhandled client message:" << connection()->atomName(event->type);
1305 void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1307 bool fromSendEvent = (event->response_type & 0x80);
1308 QPoint pos(event->x, event->y);
1309 if (!fromSendEvent) {
1310 // Do not trust the position, query it instead.
1311 xcb_translate_coordinates_cookie_t cookie = xcb_translate_coordinates(xcb_connection(), xcb_window(),
1312 m_screen->root(), 0, 0);
1313 xcb_generic_error_t *error;
1314 xcb_translate_coordinates_reply_t *reply = xcb_translate_coordinates_reply(xcb_connection(), cookie, &error);
1316 pos.setX(reply->dst_x);
1317 pos.setY(reply->dst_y);
1324 QRect rect(pos, QSize(event->width, event->height));
1326 QPlatformWindow::setGeometry(rect);
1327 QWindowSystemInterface::handleGeometryChange(window(), rect);
1329 m_dirtyFrameMargins = true;
1333 static_cast<QDri2Context *>(m_context)->resize(rect.size());
1337 void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1339 if (event->window == m_window) {
1341 if (m_deferredActivation)
1342 requestActivateWindow();
1343 QWindowSystemInterface::handleMapEvent(window());
1347 void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1349 if (event->window == m_window) {
1351 QWindowSystemInterface::handleUnmapEvent(window());
1355 static Qt::MouseButtons translateMouseButtons(int s)
1357 Qt::MouseButtons ret = 0;
1358 if (s & XCB_BUTTON_MASK_1)
1359 ret |= Qt::LeftButton;
1360 if (s & XCB_BUTTON_MASK_2)
1361 ret |= Qt::MidButton;
1362 if (s & XCB_BUTTON_MASK_3)
1363 ret |= Qt::RightButton;
1367 static Qt::MouseButton translateMouseButton(xcb_button_t s)
1370 case 1: return Qt::LeftButton;
1371 case 2: return Qt::MidButton;
1372 case 3: return Qt::RightButton;
1373 // Button values 4-7 were already handled as Wheel events, and won't occur here.
1374 case 8: return Qt::BackButton; // Also known as Qt::ExtraButton1
1375 case 9: return Qt::ForwardButton; // Also known as Qt::ExtraButton2
1376 case 10: return Qt::ExtraButton3;
1377 case 11: return Qt::ExtraButton4;
1378 case 12: return Qt::ExtraButton5;
1379 case 13: return Qt::ExtraButton6;
1380 case 14: return Qt::ExtraButton7;
1381 case 15: return Qt::ExtraButton8;
1382 case 16: return Qt::ExtraButton9;
1383 case 17: return Qt::ExtraButton10;
1384 case 18: return Qt::ExtraButton11;
1385 case 19: return Qt::ExtraButton12;
1386 case 20: return Qt::ExtraButton13;
1387 case 21: return Qt::ExtraButton14;
1388 case 22: return Qt::ExtraButton15;
1389 case 23: return Qt::ExtraButton16;
1390 case 24: return Qt::ExtraButton17;
1391 case 25: return Qt::ExtraButton18;
1392 case 26: return Qt::ExtraButton19;
1393 case 27: return Qt::ExtraButton20;
1394 case 28: return Qt::ExtraButton21;
1395 case 29: return Qt::ExtraButton22;
1396 case 30: return Qt::ExtraButton23;
1397 case 31: return Qt::ExtraButton24;
1398 default: return Qt::NoButton;
1402 void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1404 updateNetWmUserTime(event->time);
1406 QPoint local(event->event_x, event->event_y);
1407 QPoint global(event->root_x, event->root_y);
1409 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1411 if (event->detail >= 4 && event->detail <= 7) {
1412 // Logic borrowed from qapplication_x11.cpp
1413 int delta = 120 * ((event->detail == 4 || event->detail == 6) ? 1 : -1);
1414 bool hor = (((event->detail == 4 || event->detail == 5)
1415 && (modifiers & Qt::AltModifier))
1416 || (event->detail == 6 || event->detail == 7));
1418 QWindowSystemInterface::handleWheelEvent(window(), event->time,
1419 local, global, delta, hor ? Qt::Horizontal : Qt::Vertical, modifiers);
1423 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1426 void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1428 QPoint local(event->event_x, event->event_y);
1429 QPoint global(event->root_x, event->root_y);
1430 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1432 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1435 void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
1437 QPoint local(event->event_x, event->event_y);
1438 QPoint global(event->root_x, event->root_y);
1439 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1441 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1444 void QXcbWindow::handleMouseEvent(xcb_button_t detail, uint16_t state, xcb_timestamp_t time, const QPoint &local, const QPoint &global, Qt::KeyboardModifiers modifiers)
1446 connection()->setTime(time);
1448 Qt::MouseButtons buttons = translateMouseButtons(state);
1449 Qt::MouseButton button = translateMouseButton(detail);
1451 buttons ^= button; // X event uses state *before*, Qt uses state *after*
1453 QWindowSystemInterface::handleMouseEvent(window(), time, local, global, buttons, modifiers);
1456 void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1458 connection()->setTime(event->time);
1460 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1461 || event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1462 || event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1467 QWindowSystemInterface::handleEnterEvent(window());
1470 void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1472 connection()->setTime(event->time);
1474 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1475 || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)
1480 QWindowSystemInterface::handleLeaveEvent(window());
1483 void QXcbWindow::handlePropertyNotifyEvent(const xcb_property_notify_event_t *event)
1485 connection()->setTime(event->time);
1487 bool propertyDeleted = event->state == XCB_PROPERTY_DELETE;
1489 if (event->atom == atom(QXcbAtom::_NET_WM_STATE) || event->atom == atom(QXcbAtom::WM_STATE)) {
1490 if (propertyDeleted)
1493 xcb_get_property_cookie_t get_cookie =
1494 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::WM_STATE),
1495 XCB_ATOM_ANY, 0, 1024);
1497 xcb_generic_error_t *error;
1499 xcb_get_property_reply_t *reply =
1500 xcb_get_property_reply(xcb_connection(), get_cookie, &error);
1502 xcb_atom_t wm_state = XCB_WM_STATE_WITHDRAWN;
1503 if (reply && reply->format == 32 && reply->type == atom(QXcbAtom::WM_STATE)) {
1504 if (reply->length != 0)
1505 wm_state = ((long *)xcb_get_property_value(reply))[0];
1508 connection()->handleXcbError(error);
1512 QVector<xcb_atom_t> netWmState = getNetWmState();
1514 bool maximized = netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ))
1515 && netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
1516 bool fullscreen = netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
1518 Qt::WindowState state = Qt::WindowNoState;
1519 if (wm_state == XCB_WM_STATE_ICONIC)
1520 state = Qt::WindowMinimized;
1522 state = Qt::WindowMaximized;
1523 else if (fullscreen)
1524 state = Qt::WindowFullScreen;
1526 QWindowSystemInterface::handleWindowStateChanged(window(), state);
1530 void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1532 QWindowSystemInterface::handleWindowActivated(window());
1535 static bool focusInPeeker(xcb_generic_event_t *event)
1538 // FocusIn event is not in the queue, proceed with FocusOut normally.
1539 QWindowSystemInterface::handleWindowActivated(0);
1542 uint response_type = event->response_type & ~0x80;
1543 return response_type == XCB_FOCUS_IN;
1546 void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1548 // Do not set the active window to 0 if there is a FocusIn coming.
1549 // There is however no equivalent for XPutBackEvent so register a
1550 // callback for QXcbConnection instead.
1551 connection()->addPeekFunc(focusInPeeker);
1554 void QXcbWindow::updateSyncRequestCounter()
1556 if (m_screen->syncRequestSupported() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
1557 Q_XCB_CALL(xcb_sync_set_counter(xcb_connection(), m_syncCounter, m_syncValue));
1558 xcb_flush(xcb_connection());
1559 connection()->sync();
1566 bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1569 xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1572 xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
1573 m_window, XCB_TIME_CURRENT_TIME,
1574 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1575 xcb_generic_error_t *err;
1576 xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, &err);
1577 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1583 bool QXcbWindow::setMouseGrabEnabled(bool grab)
1586 xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1589 xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
1590 (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
1591 | XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW
1592 | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_POINTER_MOTION),
1593 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
1594 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
1595 XCB_TIME_CURRENT_TIME);
1596 xcb_generic_error_t *err;
1597 xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, &err);
1598 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1604 void QXcbWindow::setCursor(xcb_cursor_t cursor)
1606 xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1607 xcb_flush(xcb_connection());