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 if (connection()->time() != CurrentTime)
553 updateNetWmUserTime(connection()->time());
555 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
556 xcb_flush(xcb_connection());
558 connection()->sync();
561 void QXcbWindow::hide()
563 Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
565 // send synthetic UnmapNotify event according to icccm 4.1.4
566 xcb_unmap_notify_event_t event;
567 event.response_type = XCB_UNMAP_NOTIFY;
568 event.event = m_screen->root();
569 event.window = m_window;
570 event.from_configure = false;
571 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, m_screen->root(),
572 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
574 xcb_flush(xcb_connection());
579 struct QtMotifWmHints {
580 quint32 flags, functions, decorations;
586 MWM_HINTS_FUNCTIONS = (1L << 0),
588 MWM_FUNC_ALL = (1L << 0),
589 MWM_FUNC_RESIZE = (1L << 1),
590 MWM_FUNC_MOVE = (1L << 2),
591 MWM_FUNC_MINIMIZE = (1L << 3),
592 MWM_FUNC_MAXIMIZE = (1L << 4),
593 MWM_FUNC_CLOSE = (1L << 5),
595 MWM_HINTS_DECORATIONS = (1L << 1),
597 MWM_DECOR_ALL = (1L << 0),
598 MWM_DECOR_BORDER = (1L << 1),
599 MWM_DECOR_RESIZEH = (1L << 2),
600 MWM_DECOR_TITLE = (1L << 3),
601 MWM_DECOR_MENU = (1L << 4),
602 MWM_DECOR_MINIMIZE = (1L << 5),
603 MWM_DECOR_MAXIMIZE = (1L << 6),
605 MWM_HINTS_INPUT_MODE = (1L << 2),
607 MWM_INPUT_MODELESS = 0L,
608 MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
609 MWM_INPUT_FULL_APPLICATION_MODAL = 3L
612 static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
614 QtMotifWmHints hints;
616 xcb_get_property_cookie_t get_cookie =
617 xcb_get_property(c->xcb_connection(), 0, window, c->atom(QXcbAtom::_MOTIF_WM_HINTS),
618 c->atom(QXcbAtom::_MOTIF_WM_HINTS), 0, 20);
620 xcb_generic_error_t *error;
622 xcb_get_property_reply_t *reply =
623 xcb_get_property_reply(c->xcb_connection(), get_cookie, &error);
625 if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
626 hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
628 c->handleXcbError(error);
632 hints.functions = MWM_FUNC_ALL;
633 hints.decorations = MWM_DECOR_ALL;
634 hints.input_mode = 0L;
643 static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
645 if (hints.flags != 0l) {
646 Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
647 XCB_PROP_MODE_REPLACE,
649 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
650 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
655 Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
659 void QXcbWindow::printNetWmState(const QVector<xcb_atom_t> &state)
661 printf("_NET_WM_STATE (%d): ", state.size());
662 for (int i = 0; i < state.size(); ++i) {
663 #define CHECK_WM_STATE(state_atom) \
664 if (state.at(i) == atom(QXcbAtom::state_atom))\
665 printf(#state_atom " ");
666 CHECK_WM_STATE(_NET_WM_STATE_ABOVE)
667 CHECK_WM_STATE(_NET_WM_STATE_BELOW)
668 CHECK_WM_STATE(_NET_WM_STATE_FULLSCREEN)
669 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_HORZ)
670 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_VERT)
671 CHECK_WM_STATE(_NET_WM_STATE_MODAL)
672 CHECK_WM_STATE(_NET_WM_STATE_STAYS_ON_TOP)
673 CHECK_WM_STATE(_NET_WM_STATE_DEMANDS_ATTENTION)
674 #undef CHECK_WM_STATE
679 QVector<xcb_atom_t> QXcbWindow::getNetWmState()
681 QVector<xcb_atom_t> result;
683 xcb_get_property_cookie_t get_cookie =
684 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::_NET_WM_STATE),
685 XCB_ATOM_ATOM, 0, 1024);
687 xcb_generic_error_t *error;
689 xcb_get_property_reply_t *reply =
690 xcb_get_property_reply(xcb_connection(), get_cookie, &error);
692 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
693 result.resize(reply->length);
695 memcpy(result.data(), xcb_get_property_value(reply), reply->length * sizeof(xcb_atom_t));
697 #ifdef NET_WM_STATE_DEBUG
698 printf("getting net wm state (%x)\n", m_window);
699 printNetWmState(result);
704 connection()->handleXcbError(error);
707 #ifdef NET_WM_STATE_DEBUG
708 printf("getting net wm state (%x), empty\n", m_window);
715 void QXcbWindow::setNetWmState(const QVector<xcb_atom_t> &atoms)
717 if (atoms.isEmpty()) {
718 Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
720 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
721 atom(QXcbAtom::_NET_WM_STATE), XCB_ATOM_ATOM, 32,
722 atoms.count(), atoms.constData()));
724 xcb_flush(xcb_connection());
728 Qt::WindowFlags QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
730 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
732 if (type == Qt::ToolTip)
733 flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
734 if (type == Qt::Popup)
735 flags |= Qt::X11BypassWindowManagerHint;
737 if (flags & Qt::WindowTransparentForInput) {
738 uint32_t mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_VISIBILITY_CHANGE
739 | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_RESIZE_REDIRECT
740 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
741 | XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_PROPERTY_CHANGE
742 | XCB_EVENT_MASK_COLOR_MAP_CHANGE | XCB_EVENT_MASK_OWNER_GRAB_BUTTON;
743 xcb_change_window_attributes(xcb_connection(), xcb_window(), XCB_CW_EVENT_MASK, &mask);
746 setNetWmWindowFlags(flags);
747 setMotifWindowFlags(flags);
749 setTransparentForMouseEvents(flags & Qt::WindowTransparentForInput);
750 updateDoesNotAcceptFocus(flags & Qt::WindowDoesNotAcceptFocus);
755 void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
757 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
759 QtMotifWmHints mwmhints;
761 mwmhints.functions = 0L;
762 mwmhints.decorations = 0;
763 mwmhints.input_mode = 0L;
764 mwmhints.status = 0L;
766 if (type != Qt::SplashScreen) {
767 mwmhints.flags |= MWM_HINTS_DECORATIONS;
769 bool customize = flags & Qt::CustomizeWindowHint;
770 if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
771 mwmhints.decorations |= MWM_DECOR_BORDER;
772 mwmhints.decorations |= MWM_DECOR_RESIZEH;
774 if (flags & Qt::WindowTitleHint)
775 mwmhints.decorations |= MWM_DECOR_TITLE;
777 if (flags & Qt::WindowSystemMenuHint)
778 mwmhints.decorations |= MWM_DECOR_MENU;
780 if (flags & Qt::WindowMinimizeButtonHint) {
781 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
782 mwmhints.functions |= MWM_FUNC_MINIMIZE;
785 if (flags & Qt::WindowMaximizeButtonHint) {
786 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
787 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
790 if (flags & Qt::WindowCloseButtonHint)
791 mwmhints.functions |= MWM_FUNC_CLOSE;
794 // if type == Qt::SplashScreen
795 mwmhints.decorations = MWM_DECOR_ALL;
798 if (mwmhints.functions != 0) {
799 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
800 mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
802 mwmhints.functions = MWM_FUNC_ALL;
805 if (!(flags & Qt::FramelessWindowHint)
806 && flags & Qt::CustomizeWindowHint
807 && flags & Qt::WindowTitleHint
809 (Qt::WindowMinimizeButtonHint
810 | Qt::WindowMaximizeButtonHint
811 | Qt::WindowCloseButtonHint)))
813 // a special case - only the titlebar without any button
814 mwmhints.flags = MWM_HINTS_FUNCTIONS;
815 mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
816 mwmhints.decorations = 0;
819 setMotifWmHints(connection(), m_window, mwmhints);
822 void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
824 xcb_client_message_event_t event;
826 event.response_type = XCB_CLIENT_MESSAGE;
828 event.window = m_window;
829 event.type = atom(QXcbAtom::_NET_WM_STATE);
830 event.data.data32[0] = set ? 1 : 0;
831 event.data.data32[1] = one;
832 event.data.data32[2] = two;
833 event.data.data32[3] = 0;
834 event.data.data32[4] = 0;
836 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));
839 Qt::WindowState QXcbWindow::setWindowState(Qt::WindowState state)
841 if (state == m_windowState)
845 switch (m_windowState) {
846 case Qt::WindowMinimized:
847 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
849 case Qt::WindowMaximized:
850 changeNetWmState(false,
851 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
852 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
854 case Qt::WindowFullScreen:
855 changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
863 case Qt::WindowMinimized:
865 xcb_client_message_event_t event;
867 event.response_type = XCB_CLIENT_MESSAGE;
869 event.window = m_window;
870 event.type = atom(QXcbAtom::WM_CHANGE_STATE);
871 event.data.data32[0] = XCB_WM_STATE_ICONIC;
872 event.data.data32[1] = 0;
873 event.data.data32[2] = 0;
874 event.data.data32[3] = 0;
875 event.data.data32[4] = 0;
877 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));
880 case Qt::WindowMaximized:
881 changeNetWmState(true,
882 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
883 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
885 case Qt::WindowFullScreen:
886 changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
888 case Qt::WindowNoState:
894 connection()->sync();
896 m_windowState = state;
897 return m_windowState;
900 void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
902 // in order of decreasing priority
903 QVector<uint> windowTypes;
905 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
910 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
914 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
917 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
919 case Qt::SplashScreen:
920 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
926 if (flags & Qt::FramelessWindowHint)
927 windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
929 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
931 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
932 atom(QXcbAtom::_NET_WM_WINDOW_TYPE), XCB_ATOM_ATOM, 32,
933 windowTypes.count(), windowTypes.constData()));
936 void QXcbWindow::updateMotifWmHintsBeforeMap()
938 QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
940 if (window()->windowModality() != Qt::NonModal) {
941 switch (window()->windowModality()) {
942 case Qt::WindowModal:
943 mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
945 case Qt::ApplicationModal:
947 mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
950 mwmhints.flags |= MWM_HINTS_INPUT_MODE;
952 mwmhints.input_mode = MWM_INPUT_MODELESS;
953 mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
956 if (window()->minimumSize() == window()->maximumSize()) {
957 // fixed size, remove the resize handle (since mwm/dtwm
958 // isn't smart enough to do it itself)
959 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
960 if (mwmhints.functions == MWM_FUNC_ALL) {
961 mwmhints.functions = MWM_FUNC_MOVE;
963 mwmhints.functions &= ~MWM_FUNC_RESIZE;
966 if (mwmhints.decorations == MWM_DECOR_ALL) {
967 mwmhints.flags |= MWM_HINTS_DECORATIONS;
968 mwmhints.decorations = (MWM_DECOR_BORDER
972 mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
976 if (window()->windowFlags() & Qt::WindowMinimizeButtonHint) {
977 mwmhints.flags |= MWM_HINTS_DECORATIONS;
978 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
979 mwmhints.functions |= MWM_FUNC_MINIMIZE;
981 if (window()->windowFlags() & Qt::WindowMaximizeButtonHint) {
982 mwmhints.flags |= MWM_HINTS_DECORATIONS;
983 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
984 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
986 if (window()->windowFlags() & Qt::WindowCloseButtonHint)
987 mwmhints.functions |= MWM_FUNC_CLOSE;
989 setMotifWmHints(connection(), m_window, mwmhints);
992 void QXcbWindow::updateNetWmStateBeforeMap()
994 QVector<xcb_atom_t> netWmState;
996 Qt::WindowFlags flags = window()->windowFlags();
997 if (flags & Qt::WindowStaysOnTopHint) {
998 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_ABOVE));
999 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP));
1000 } else if (flags & Qt::WindowStaysOnBottomHint) {
1001 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_BELOW));
1004 if (window()->windowState() & Qt::WindowFullScreen) {
1005 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
1008 if (window()->windowState() & Qt::WindowMaximized) {
1009 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ));
1010 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
1013 if (window()->windowModality() != Qt::NonModal) {
1014 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MODAL));
1017 setNetWmState(netWmState);
1020 void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
1022 xcb_window_t wid = m_window;
1024 const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1025 if (m_netWmUserTimeWindow || isSupportedByWM) {
1026 if (!m_netWmUserTimeWindow) {
1027 m_netWmUserTimeWindow = xcb_generate_id(xcb_connection());
1028 Q_XCB_CALL(xcb_create_window(xcb_connection(),
1029 XCB_COPY_FROM_PARENT, // depth -- same as root
1030 m_netWmUserTimeWindow, // window id
1031 m_window, // parent window id
1034 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
1035 m_visualId, // visual
1038 wid = m_netWmUserTimeWindow;
1039 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW),
1040 XCB_ATOM_WINDOW, 32, 1, &m_netWmUserTimeWindow);
1041 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME));
1042 } else if (!isSupportedByWM) {
1043 // WM no longer supports it, then we should remove the
1044 // _NET_WM_USER_TIME_WINDOW atom.
1045 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1046 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
1047 m_netWmUserTimeWindow = XCB_NONE;
1049 wid = m_netWmUserTimeWindow;
1052 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
1053 XCB_ATOM_CARDINAL, 32, 1, ×tamp);
1056 void QXcbWindow::setTransparentForMouseEvents(bool transparent)
1058 if (transparent == m_transparent)
1061 xcb_rectangle_t rectangle;
1063 xcb_rectangle_t *rect = 0;
1069 rectangle.width = geometry().width();
1070 rectangle.height = geometry().height();
1075 xcb_xfixes_region_t region = xcb_generate_id(xcb_connection());
1076 xcb_xfixes_create_region(xcb_connection(), region, nrect, rect);
1077 xcb_xfixes_set_window_shape_region_checked(xcb_connection(), m_window, XCB_SHAPE_SK_INPUT, 0, 0, region);
1078 xcb_xfixes_destroy_region(xcb_connection(), region);
1080 m_transparent = transparent;
1083 void QXcbWindow::updateDoesNotAcceptFocus(bool doesNotAcceptFocus)
1085 xcb_get_property_cookie_t cookie = xcb_get_wm_hints(xcb_connection(), m_window);
1087 xcb_generic_error_t *error;
1089 xcb_wm_hints_t hints;
1090 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, &error);
1093 connection()->handleXcbError(error);
1098 xcb_wm_hints_set_input(&hints, !doesNotAcceptFocus);
1099 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
1102 WId QXcbWindow::winId() const
1107 void QXcbWindow::setParent(const QPlatformWindow *parent)
1109 // re-create for compatibility
1112 QPoint topLeft = geometry().topLeft();
1114 xcb_window_t xcb_parent_id = parent ? static_cast<const QXcbWindow *>(parent)->xcb_window() : m_screen->root();
1115 Q_XCB_CALL(xcb_reparent_window(xcb_connection(), xcb_window(), xcb_parent_id, topLeft.x(), topLeft.y()));
1118 void QXcbWindow::setWindowTitle(const QString &title)
1120 QByteArray ba = title.toUtf8();
1121 Q_XCB_CALL(xcb_change_property(xcb_connection(),
1122 XCB_PROP_MODE_REPLACE,
1124 atom(QXcbAtom::_NET_WM_NAME),
1125 atom(QXcbAtom::UTF8_STRING),
1131 void QXcbWindow::raise()
1133 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1134 const quint32 values[] = { XCB_STACK_MODE_ABOVE };
1135 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1138 void QXcbWindow::lower()
1140 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1141 const quint32 values[] = { XCB_STACK_MODE_BELOW };
1142 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1145 void QXcbWindow::propagateSizeHints()
1147 // update WM_NORMAL_HINTS
1148 xcb_size_hints_t hints;
1149 memset(&hints, 0, sizeof(hints));
1151 QRect rect = geometry();
1153 QWindow *win = window();
1155 xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
1156 xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
1157 xcb_size_hints_set_win_gravity(&hints, qt_window_private(win)->positionPolicy == QWindowPrivate::WindowFrameInclusive
1158 ? XCB_GRAVITY_NORTH_WEST : XCB_GRAVITY_STATIC);
1160 QSize minimumSize = win->minimumSize();
1161 QSize maximumSize = win->maximumSize();
1162 QSize baseSize = win->baseSize();
1163 QSize sizeIncrement = win->sizeIncrement();
1165 if (minimumSize.width() > 0 || minimumSize.height() > 0)
1166 xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1168 if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1169 xcb_size_hints_set_max_size(&hints,
1170 qMin(XCOORD_MAX, maximumSize.width()),
1171 qMin(XCOORD_MAX, maximumSize.height()));
1173 if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1174 xcb_size_hints_set_base_size(&hints, baseSize.width(), baseSize.height());
1175 xcb_size_hints_set_resize_inc(&hints, sizeIncrement.width(), sizeIncrement.height());
1178 xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1181 void QXcbWindow::requestActivateWindow()
1184 m_deferredActivation = true;
1187 m_deferredActivation = false;
1189 updateNetWmUserTime(connection()->time());
1191 if (window()->isTopLevel()
1192 && connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_ACTIVE_WINDOW))) {
1193 xcb_client_message_event_t event;
1195 event.response_type = XCB_CLIENT_MESSAGE;
1197 event.window = m_window;
1198 event.type = atom(QXcbAtom::_NET_ACTIVE_WINDOW);
1199 event.data.data32[0] = 1;
1200 event.data.data32[1] = connection()->time();
1201 QWindow *focusWindow = QGuiApplication::focusWindow();
1202 event.data.data32[2] = focusWindow ? focusWindow->winId() : XCB_NONE;
1203 event.data.data32[3] = 0;
1204 event.data.data32[4] = 0;
1206 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));
1208 Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1211 connection()->sync();
1214 #if XCB_USE_MAEMO_WINDOW_PROPERTIES
1215 void QXcbWindow::setOrientation(Qt::ScreenOrientation orientation)
1218 switch (orientation) {
1219 case Qt::PortraitOrientation: angle = 270; break;
1220 case Qt::LandscapeOrientation: angle = 0; break;
1221 case Qt::InvertedPortraitOrientation: angle = 90; break;
1222 case Qt::InvertedLandscapeOrientation: angle = 180; break;
1223 case Qt::PrimaryOrientation: break;
1225 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
1226 atom(QXcbAtom::MeegoTouchOrientationAngle), XCB_ATOM_CARDINAL, 32,
1231 QSurfaceFormat QXcbWindow::format() const
1233 // ### return actual format
1237 #if defined(XCB_USE_EGL)
1238 QXcbEGLSurface *QXcbWindow::eglSurface() const
1240 if (!m_eglSurface) {
1241 EGLDisplay display = connection()->egl_display();
1242 EGLConfig config = q_configFromGLFormat(display, window()->requestedFormat(), true);
1243 EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)m_window, 0);
1245 m_eglSurface = new QXcbEGLSurface(display, surface);
1248 return m_eglSurface;
1252 void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1254 QRect rect(event->x, event->y, event->width, event->height);
1256 if (m_exposeRegion.isEmpty())
1257 m_exposeRegion = rect;
1259 m_exposeRegion |= rect;
1261 // if count is non-zero there are more expose events pending
1262 if (event->count == 0) {
1263 QWindowSystemInterface::handleSynchronousExposeEvent(window(), m_exposeRegion);
1264 m_exposeRegion = QRegion();
1268 void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1270 if (event->format != 32)
1273 if (event->type == atom(QXcbAtom::WM_PROTOCOLS)) {
1274 if (event->data.data32[0] == atom(QXcbAtom::WM_DELETE_WINDOW)) {
1275 QWindowSystemInterface::handleCloseEvent(window());
1276 } else if (event->data.data32[0] == atom(QXcbAtom::WM_TAKE_FOCUS)) {
1277 connection()->setTime(event->data.data32[1]);
1278 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_PING)) {
1279 xcb_client_message_event_t reply = *event;
1281 reply.response_type = XCB_CLIENT_MESSAGE;
1282 reply.window = m_screen->root();
1284 xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&reply);
1285 xcb_flush(xcb_connection());
1286 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_SYNC_REQUEST)) {
1287 connection()->setTime(event->data.data32[1]);
1288 m_syncValue.lo = event->data.data32[2];
1289 m_syncValue.hi = event->data.data32[3];
1291 qWarning() << "QXcbWindow: Unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1293 } else if (event->type == atom(QXcbAtom::XdndEnter)) {
1294 connection()->drag()->handleEnter(window(), event);
1295 } else if (event->type == atom(QXcbAtom::XdndPosition)) {
1296 connection()->drag()->handlePosition(window(), event);
1297 } else if (event->type == atom(QXcbAtom::XdndLeave)) {
1298 connection()->drag()->handleLeave(window(), event);
1299 } else if (event->type == atom(QXcbAtom::XdndDrop)) {
1300 connection()->drag()->handleDrop(window(), event);
1302 qWarning() << "QXcbWindow: Unhandled client message:" << connection()->atomName(event->type);
1306 void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1308 bool fromSendEvent = (event->response_type & 0x80);
1309 QPoint pos(event->x, event->y);
1310 if (!parent() && !fromSendEvent) {
1311 // Do not trust the position, query it instead.
1312 xcb_translate_coordinates_cookie_t cookie = xcb_translate_coordinates(xcb_connection(), xcb_window(),
1313 m_screen->root(), 0, 0);
1314 xcb_generic_error_t *error;
1315 xcb_translate_coordinates_reply_t *reply = xcb_translate_coordinates_reply(xcb_connection(), cookie, &error);
1317 pos.setX(reply->dst_x);
1318 pos.setY(reply->dst_y);
1325 QRect rect(pos, QSize(event->width, event->height));
1327 QPlatformWindow::setGeometry(rect);
1328 QWindowSystemInterface::handleGeometryChange(window(), rect);
1330 m_dirtyFrameMargins = true;
1334 static_cast<QDri2Context *>(m_context)->resize(rect.size());
1338 void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1340 if (event->window == m_window) {
1342 if (m_deferredActivation)
1343 requestActivateWindow();
1344 QWindowSystemInterface::handleMapEvent(window());
1348 void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1350 if (event->window == m_window) {
1352 QWindowSystemInterface::handleUnmapEvent(window());
1356 static Qt::MouseButtons translateMouseButtons(int s)
1358 Qt::MouseButtons ret = 0;
1359 if (s & XCB_BUTTON_MASK_1)
1360 ret |= Qt::LeftButton;
1361 if (s & XCB_BUTTON_MASK_2)
1362 ret |= Qt::MidButton;
1363 if (s & XCB_BUTTON_MASK_3)
1364 ret |= Qt::RightButton;
1368 static Qt::MouseButton translateMouseButton(xcb_button_t s)
1371 case 1: return Qt::LeftButton;
1372 case 2: return Qt::MidButton;
1373 case 3: return Qt::RightButton;
1374 // Button values 4-7 were already handled as Wheel events, and won't occur here.
1375 case 8: return Qt::BackButton; // Also known as Qt::ExtraButton1
1376 case 9: return Qt::ForwardButton; // Also known as Qt::ExtraButton2
1377 case 10: return Qt::ExtraButton3;
1378 case 11: return Qt::ExtraButton4;
1379 case 12: return Qt::ExtraButton5;
1380 case 13: return Qt::ExtraButton6;
1381 case 14: return Qt::ExtraButton7;
1382 case 15: return Qt::ExtraButton8;
1383 case 16: return Qt::ExtraButton9;
1384 case 17: return Qt::ExtraButton10;
1385 case 18: return Qt::ExtraButton11;
1386 case 19: return Qt::ExtraButton12;
1387 case 20: return Qt::ExtraButton13;
1388 case 21: return Qt::ExtraButton14;
1389 case 22: return Qt::ExtraButton15;
1390 case 23: return Qt::ExtraButton16;
1391 case 24: return Qt::ExtraButton17;
1392 case 25: return Qt::ExtraButton18;
1393 case 26: return Qt::ExtraButton19;
1394 case 27: return Qt::ExtraButton20;
1395 case 28: return Qt::ExtraButton21;
1396 case 29: return Qt::ExtraButton22;
1397 case 30: return Qt::ExtraButton23;
1398 case 31: return Qt::ExtraButton24;
1399 default: return Qt::NoButton;
1403 void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1405 updateNetWmUserTime(event->time);
1407 QPoint local(event->event_x, event->event_y);
1408 QPoint global(event->root_x, event->root_y);
1410 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1412 if (event->detail >= 4 && event->detail <= 7) {
1413 // Logic borrowed from qapplication_x11.cpp
1414 int delta = 120 * ((event->detail == 4 || event->detail == 6) ? 1 : -1);
1415 bool hor = (((event->detail == 4 || event->detail == 5)
1416 && (modifiers & Qt::AltModifier))
1417 || (event->detail == 6 || event->detail == 7));
1419 QWindowSystemInterface::handleWheelEvent(window(), event->time,
1420 local, global, delta, hor ? Qt::Horizontal : Qt::Vertical, modifiers);
1424 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1427 void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1429 QPoint local(event->event_x, event->event_y);
1430 QPoint global(event->root_x, event->root_y);
1431 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1433 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1436 void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
1438 QPoint local(event->event_x, event->event_y);
1439 QPoint global(event->root_x, event->root_y);
1440 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1442 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1445 void QXcbWindow::handleMouseEvent(xcb_button_t detail, uint16_t state, xcb_timestamp_t time, const QPoint &local, const QPoint &global, Qt::KeyboardModifiers modifiers)
1447 connection()->setTime(time);
1449 Qt::MouseButtons buttons = translateMouseButtons(state);
1450 Qt::MouseButton button = translateMouseButton(detail);
1452 buttons ^= button; // X event uses state *before*, Qt uses state *after*
1454 QWindowSystemInterface::handleMouseEvent(window(), time, local, global, buttons, modifiers);
1457 void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1459 connection()->setTime(event->time);
1461 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1462 || event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1463 || event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1468 QWindowSystemInterface::handleEnterEvent(window());
1471 void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1473 connection()->setTime(event->time);
1475 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1476 || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)
1481 QWindowSystemInterface::handleLeaveEvent(window());
1484 void QXcbWindow::handlePropertyNotifyEvent(const xcb_property_notify_event_t *event)
1486 connection()->setTime(event->time);
1488 bool propertyDeleted = event->state == XCB_PROPERTY_DELETE;
1490 if (event->atom == atom(QXcbAtom::_NET_WM_STATE) || event->atom == atom(QXcbAtom::WM_STATE)) {
1491 if (propertyDeleted)
1494 xcb_get_property_cookie_t get_cookie =
1495 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::WM_STATE),
1496 XCB_ATOM_ANY, 0, 1024);
1498 xcb_generic_error_t *error;
1500 xcb_get_property_reply_t *reply =
1501 xcb_get_property_reply(xcb_connection(), get_cookie, &error);
1503 xcb_atom_t wm_state = XCB_WM_STATE_WITHDRAWN;
1504 if (reply && reply->format == 32 && reply->type == atom(QXcbAtom::WM_STATE)) {
1505 if (reply->length != 0)
1506 wm_state = ((long *)xcb_get_property_value(reply))[0];
1509 connection()->handleXcbError(error);
1513 QVector<xcb_atom_t> netWmState = getNetWmState();
1515 bool maximized = netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ))
1516 && netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
1517 bool fullscreen = netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
1519 Qt::WindowState state = Qt::WindowNoState;
1520 if (wm_state == XCB_WM_STATE_ICONIC)
1521 state = Qt::WindowMinimized;
1523 state = Qt::WindowMaximized;
1524 else if (fullscreen)
1525 state = Qt::WindowFullScreen;
1527 QWindowSystemInterface::handleWindowStateChanged(window(), state);
1531 void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1533 QWindowSystemInterface::handleWindowActivated(window());
1536 static bool focusInPeeker(xcb_generic_event_t *event)
1539 // FocusIn event is not in the queue, proceed with FocusOut normally.
1540 QWindowSystemInterface::handleWindowActivated(0);
1543 uint response_type = event->response_type & ~0x80;
1544 return response_type == XCB_FOCUS_IN;
1547 void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1549 // Do not set the active window to 0 if there is a FocusIn coming.
1550 // There is however no equivalent for XPutBackEvent so register a
1551 // callback for QXcbConnection instead.
1552 connection()->addPeekFunc(focusInPeeker);
1555 void QXcbWindow::updateSyncRequestCounter()
1557 if (m_screen->syncRequestSupported() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
1558 Q_XCB_CALL(xcb_sync_set_counter(xcb_connection(), m_syncCounter, m_syncValue));
1559 xcb_flush(xcb_connection());
1560 connection()->sync();
1567 bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1570 xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1573 xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
1574 m_window, XCB_TIME_CURRENT_TIME,
1575 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1576 xcb_generic_error_t *err;
1577 xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, &err);
1578 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1584 bool QXcbWindow::setMouseGrabEnabled(bool grab)
1587 xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1590 xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
1591 (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
1592 | XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW
1593 | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_POINTER_MOTION),
1594 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
1595 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
1596 XCB_TIME_CURRENT_TIME);
1597 xcb_generic_error_t *err;
1598 xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, &err);
1599 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1605 void QXcbWindow::setCursor(xcb_cursor_t cursor)
1607 xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1608 xcb_flush(xcb_connection());