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_netWmUserTimeWindow(XCB_NONE)
147 #if defined(XCB_USE_EGL)
151 m_screen = static_cast<QXcbScreen *>(window->screen()->handle());
153 setConnection(m_screen->connection());
158 void QXcbWindow::create()
162 m_windowState = Qt::WindowNoState;
164 Qt::WindowType type = window()->windowType();
166 if (type == Qt::Desktop) {
167 m_window = m_screen->root();
168 m_depth = m_screen->screen()->root_depth;
169 m_imageFormat = imageFormatForDepth(m_depth);
170 connection()->addWindow(m_window, this);
174 const quint32 mask = XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK;
175 const quint32 values[] = {
176 // XCB_CW_BACK_PIXMAP
178 // XCB_CW_OVERRIDE_REDIRECT
179 type == Qt::Popup || type == Qt::ToolTip,
181 type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
183 XCB_EVENT_MASK_EXPOSURE
184 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
185 | XCB_EVENT_MASK_KEY_PRESS
186 | XCB_EVENT_MASK_KEY_RELEASE
187 | XCB_EVENT_MASK_BUTTON_PRESS
188 | XCB_EVENT_MASK_BUTTON_RELEASE
189 | XCB_EVENT_MASK_BUTTON_MOTION
190 | XCB_EVENT_MASK_ENTER_WINDOW
191 | XCB_EVENT_MASK_LEAVE_WINDOW
192 | XCB_EVENT_MASK_POINTER_MOTION
193 | XCB_EVENT_MASK_PROPERTY_CHANGE
194 | XCB_EVENT_MASK_FOCUS_CHANGE
197 QRect rect = window()->geometry();
198 QPlatformWindow::setGeometry(rect);
200 rect.setWidth(qBound(1, rect.width(), XCOORD_MAX));
201 rect.setHeight(qBound(1, rect.height(), XCOORD_MAX));
203 xcb_window_t xcb_parent_id = m_screen->root();
205 xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
207 m_format = window()->requestedFormat();
209 #if (defined(XCB_USE_GLX) || defined(XCB_USE_EGL)) && defined(XCB_USE_XLIB)
210 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL)
211 || m_format.hasAlpha())
213 #if defined(XCB_USE_GLX)
214 XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(m_screen), m_screen->screenNumber(), &m_format);
216 qFatal("Could not initialize GLX");
217 #elif defined(XCB_USE_EGL)
218 EGLDisplay eglDisplay = connection()->egl_display();
219 EGLConfig eglConfig = q_configFromGLFormat(eglDisplay, m_format, true);
220 m_format = q_glFormatFromConfig(eglDisplay, eglConfig);
222 VisualID id = QXlibEglIntegration::getCompatibleVisualId(DISPLAY_FROM_XCB(this), eglDisplay, eglConfig);
224 XVisualInfo visualInfoTemplate;
225 memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
226 visualInfoTemplate.visualid = id;
228 XVisualInfo *visualInfo;
229 int matchingCount = 0;
230 visualInfo = XGetVisualInfo(DISPLAY_FROM_XCB(this), VisualIDMask, &visualInfoTemplate, &matchingCount);
232 qFatal("Could not initialize EGL");
234 m_depth = visualInfo->depth;
235 m_imageFormat = imageFormatForDepth(m_depth);
236 Colormap cmap = XCreateColormap(DISPLAY_FROM_XCB(this), xcb_parent_id, visualInfo->visual, AllocNone);
238 XSetWindowAttributes a;
239 a.background_pixel = WhitePixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
240 a.border_pixel = BlackPixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
243 m_visualId = visualInfo->visualid;
245 m_window = XCreateWindow(DISPLAY_FROM_XCB(this), xcb_parent_id, rect.x(), rect.y(), rect.width(), rect.height(),
246 0, visualInfo->depth, InputOutput, visualInfo->visual,
247 CWBackPixel|CWBorderPixel|CWColormap, &a);
251 #endif //defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
253 m_window = xcb_generate_id(xcb_connection());
254 m_depth = m_screen->screen()->root_depth;
255 m_imageFormat = imageFormatForDepth(m_depth);
256 m_visualId = m_screen->screen()->root_visual;
258 Q_XCB_CALL(xcb_create_window(xcb_connection(),
259 XCB_COPY_FROM_PARENT, // depth -- same as root
260 m_window, // window id
261 xcb_parent_id, // parent window id
267 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
268 m_visualId, // visual
273 connection()->addWindow(m_window, this);
275 Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
277 propagateSizeHints();
279 xcb_atom_t properties[4];
280 int propertyCount = 0;
281 properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
282 properties[propertyCount++] = atom(QXcbAtom::WM_TAKE_FOCUS);
283 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_PING);
285 if (m_screen->syncRequestSupported())
286 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
288 if (window()->windowFlags() & Qt::WindowContextHelpButtonHint)
289 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
291 Q_XCB_CALL(xcb_change_property(xcb_connection(),
292 XCB_PROP_MODE_REPLACE,
294 atom(QXcbAtom::WM_PROTOCOLS),
302 if (m_screen->syncRequestSupported()) {
303 m_syncCounter = xcb_generate_id(xcb_connection());
304 Q_XCB_CALL(xcb_sync_create_counter(xcb_connection(), m_syncCounter, m_syncValue));
306 Q_XCB_CALL(xcb_change_property(xcb_connection(),
307 XCB_PROP_MODE_REPLACE,
309 atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
316 // set the PID to let the WM kill the application if unresponsive
318 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
319 atom(QXcbAtom::_NET_WM_PID), XCB_ATOM_CARDINAL, 32,
322 xcb_wm_hints_t hints;
323 memset(&hints, 0, sizeof(hints));
324 xcb_wm_hints_set_normal(&hints);
326 xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
328 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
330 xcb_window_t leader = m_screen->clientLeader();
331 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
332 atom(QXcbAtom::WM_CLIENT_LEADER), XCB_ATOM_WINDOW, 32,
335 #ifdef XCB_USE_XINPUT2_MAEMO
336 if (connection()->isUsingXInput2()) {
337 XIEventMask xieventmask;
338 uchar bitmask[2] = { 0, 0 };
340 xieventmask.deviceid = XIAllMasterDevices;
341 xieventmask.mask = bitmask;
342 xieventmask.mask_len = sizeof(bitmask);
344 XISetMask(bitmask, XI_ButtonPress);
345 XISetMask(bitmask, XI_ButtonRelease);
346 XISetMask(bitmask, XI_Motion);
348 XISelectEvents(DISPLAY_FROM_XCB(this), m_window, &xieventmask, 1);
352 setWindowFlags(window()->windowFlags());
353 setWindowTitle(window()->windowTitle());
354 setWindowState(window()->windowState());
356 if (window()->windowFlags() & Qt::WindowTransparentForInput)
357 setTransparentForMouseEvents(true);
359 connection()->drag()->dndEnable(this, true);
362 QXcbWindow::~QXcbWindow()
367 void QXcbWindow::destroy()
369 if (m_syncCounter && m_screen->syncRequestSupported())
370 Q_XCB_CALL(xcb_sync_destroy_counter(xcb_connection(), m_syncCounter));
372 if (m_netWmUserTimeWindow) {
373 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
374 // Some window managers, like metacity, do XSelectInput on the _NET_WM_USER_TIME_WINDOW window,
375 // without trapping BadWindow (which crashes when the user time window is destroyed).
376 connection()->sync();
377 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
378 m_netWmUserTimeWindow = XCB_NONE;
380 connection()->removeWindow(m_window);
381 Q_XCB_CALL(xcb_destroy_window(xcb_connection(), m_window));
385 #if defined(XCB_USE_EGL)
391 void QXcbWindow::setGeometry(const QRect &rect)
393 QPlatformWindow::setGeometry(rect);
395 propagateSizeHints();
397 const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
398 const quint32 values[] = { rect.x(),
400 qBound(1, rect.width(), XCOORD_MAX),
401 qBound(1, rect.height(), XCOORD_MAX) };
403 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
405 xcb_flush(xcb_connection());
408 QMargins QXcbWindow::frameMargins() const
410 if (m_dirtyFrameMargins) {
411 xcb_window_t window = m_window;
412 xcb_window_t parent = m_window;
414 bool foundRoot = false;
416 const QVector<xcb_window_t> &virtualRoots =
417 connection()->wmSupport()->virtualRoots();
420 xcb_query_tree_cookie_t cookie = xcb_query_tree(xcb_connection(), parent);
422 xcb_generic_error_t *error;
423 xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, &error);
425 if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1) {
429 parent = reply->parent;
435 connection()->handleXcbError(error);
439 m_dirtyFrameMargins = false;
440 m_frameMargins = QMargins();
441 return m_frameMargins;
447 xcb_generic_error_t *error;
448 xcb_translate_coordinates_reply_t *reply =
449 xcb_translate_coordinates_reply(
451 xcb_translate_coordinates(xcb_connection(), window, parent, 0, 0),
455 offset = QPoint(reply->dst_x, reply->dst_y);
461 xcb_get_geometry_reply_t *geom =
462 xcb_get_geometry_reply(
464 xcb_get_geometry(xcb_connection(), parent),
469 // add the border_width for the window managers frame... some window managers
470 // do not use a border_width of zero for their frames, and if we the left and
471 // top strut, we ensure that pos() is absolutely correct. frameGeometry()
472 // will still be incorrect though... perhaps i should have foffset as well, to
473 // indicate the frame offset (equal to the border_width on X).
475 // -- copied from qwidget_x11.cpp
477 int left = offset.x() + geom->border_width;
478 int top = offset.y() + geom->border_width;
479 int right = geom->width + geom->border_width - geometry().width() - offset.x();
480 int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
482 m_frameMargins = QMargins(left, top, right, bottom);
489 m_dirtyFrameMargins = false;
492 return m_frameMargins;
495 void QXcbWindow::setVisible(bool visible)
503 void QXcbWindow::show()
505 if (window()->isTopLevel()) {
506 xcb_get_property_cookie_t cookie = xcb_get_wm_hints(xcb_connection(), m_window);
508 xcb_generic_error_t *error;
510 xcb_wm_hints_t hints;
511 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, &error);
514 connection()->handleXcbError(error);
518 if (window()->windowState() & Qt::WindowMinimized)
519 xcb_wm_hints_set_iconic(&hints);
521 xcb_wm_hints_set_normal(&hints);
523 xcb_wm_hints_set_input(&hints, !(window()->windowFlags() & Qt::WindowDoesNotAcceptFocus));
525 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
527 // update WM_NORMAL_HINTS
528 propagateSizeHints();
530 // update WM_TRANSIENT_FOR
531 if (window()->transientParent() && isTransient(window())) {
532 QXcbWindow *transientXcbParent = static_cast<QXcbWindow *>(window()->transientParent()->handle());
533 if (transientXcbParent) {
535 xcb_window_t parentWindow = transientXcbParent->xcb_window();
537 // todo: set transient for group (wm_client_leader) if no parent, a la qwidget_x11.cpp
538 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
539 XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 32,
544 // update _MOTIF_WM_HINTS
545 updateMotifWmHintsBeforeMap();
547 // update _NET_WM_STATE
548 updateNetWmStateBeforeMap();
551 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
552 xcb_flush(xcb_connection());
554 connection()->sync();
557 void QXcbWindow::hide()
559 Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
561 // send synthetic UnmapNotify event according to icccm 4.1.4
562 xcb_unmap_notify_event_t event;
563 event.response_type = XCB_UNMAP_NOTIFY;
564 event.event = m_screen->root();
565 event.window = m_window;
566 event.from_configure = false;
567 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, m_screen->root(),
568 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
570 xcb_flush(xcb_connection());
575 struct QtMotifWmHints {
576 quint32 flags, functions, decorations;
582 MWM_HINTS_FUNCTIONS = (1L << 0),
584 MWM_FUNC_ALL = (1L << 0),
585 MWM_FUNC_RESIZE = (1L << 1),
586 MWM_FUNC_MOVE = (1L << 2),
587 MWM_FUNC_MINIMIZE = (1L << 3),
588 MWM_FUNC_MAXIMIZE = (1L << 4),
589 MWM_FUNC_CLOSE = (1L << 5),
591 MWM_HINTS_DECORATIONS = (1L << 1),
593 MWM_DECOR_ALL = (1L << 0),
594 MWM_DECOR_BORDER = (1L << 1),
595 MWM_DECOR_RESIZEH = (1L << 2),
596 MWM_DECOR_TITLE = (1L << 3),
597 MWM_DECOR_MENU = (1L << 4),
598 MWM_DECOR_MINIMIZE = (1L << 5),
599 MWM_DECOR_MAXIMIZE = (1L << 6),
601 MWM_HINTS_INPUT_MODE = (1L << 2),
603 MWM_INPUT_MODELESS = 0L,
604 MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
605 MWM_INPUT_FULL_APPLICATION_MODAL = 3L
608 static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
610 QtMotifWmHints hints;
612 xcb_get_property_cookie_t get_cookie =
613 xcb_get_property(c->xcb_connection(), 0, window, c->atom(QXcbAtom::_MOTIF_WM_HINTS),
614 c->atom(QXcbAtom::_MOTIF_WM_HINTS), 0, 20);
616 xcb_generic_error_t *error;
618 xcb_get_property_reply_t *reply =
619 xcb_get_property_reply(c->xcb_connection(), get_cookie, &error);
621 if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
622 hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
624 c->handleXcbError(error);
628 hints.functions = MWM_FUNC_ALL;
629 hints.decorations = MWM_DECOR_ALL;
630 hints.input_mode = 0L;
639 static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
641 if (hints.flags != 0l) {
642 Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
643 XCB_PROP_MODE_REPLACE,
645 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
646 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
651 Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
655 void QXcbWindow::printNetWmState(const QVector<xcb_atom_t> &state)
657 printf("_NET_WM_STATE (%d): ", state.size());
658 for (int i = 0; i < state.size(); ++i) {
659 #define CHECK_WM_STATE(state_atom) \
660 if (state.at(i) == atom(QXcbAtom::state_atom))\
661 printf(#state_atom " ");
662 CHECK_WM_STATE(_NET_WM_STATE_ABOVE)
663 CHECK_WM_STATE(_NET_WM_STATE_BELOW)
664 CHECK_WM_STATE(_NET_WM_STATE_FULLSCREEN)
665 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_HORZ)
666 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_VERT)
667 CHECK_WM_STATE(_NET_WM_STATE_MODAL)
668 CHECK_WM_STATE(_NET_WM_STATE_STAYS_ON_TOP)
669 CHECK_WM_STATE(_NET_WM_STATE_DEMANDS_ATTENTION)
670 #undef CHECK_WM_STATE
675 QVector<xcb_atom_t> QXcbWindow::getNetWmState()
677 QVector<xcb_atom_t> result;
679 xcb_get_property_cookie_t get_cookie =
680 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::_NET_WM_STATE),
681 XCB_ATOM_ATOM, 0, 1024);
683 xcb_generic_error_t *error;
685 xcb_get_property_reply_t *reply =
686 xcb_get_property_reply(xcb_connection(), get_cookie, &error);
688 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
689 result.resize(reply->length);
691 memcpy(result.data(), xcb_get_property_value(reply), reply->length * sizeof(xcb_atom_t));
693 #ifdef NET_WM_STATE_DEBUG
694 printf("getting net wm state (%x)\n", m_window);
695 printNetWmState(result);
700 connection()->handleXcbError(error);
703 #ifdef NET_WM_STATE_DEBUG
704 printf("getting net wm state (%x), empty\n", m_window);
711 void QXcbWindow::setNetWmState(const QVector<xcb_atom_t> &atoms)
713 if (atoms.isEmpty()) {
714 Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
716 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
717 atom(QXcbAtom::_NET_WM_STATE), XCB_ATOM_ATOM, 32,
718 atoms.count(), atoms.constData()));
720 xcb_flush(xcb_connection());
724 Qt::WindowFlags QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
726 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
728 if (type == Qt::ToolTip)
729 flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
730 if (type == Qt::Popup)
731 flags |= Qt::X11BypassWindowManagerHint;
733 if (flags & Qt::WindowTransparentForInput) {
734 uint32_t mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_VISIBILITY_CHANGE
735 | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_RESIZE_REDIRECT
736 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
737 | XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_PROPERTY_CHANGE
738 | XCB_EVENT_MASK_COLOR_MAP_CHANGE | XCB_EVENT_MASK_OWNER_GRAB_BUTTON;
739 xcb_change_window_attributes(xcb_connection(), xcb_window(), XCB_CW_EVENT_MASK, &mask);
742 setNetWmWindowFlags(flags);
743 setMotifWindowFlags(flags);
745 setTransparentForMouseEvents(flags & Qt::WindowTransparentForInput);
746 updateDoesNotAcceptFocus(flags & Qt::WindowDoesNotAcceptFocus);
751 void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
753 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
755 QtMotifWmHints mwmhints;
757 mwmhints.functions = 0L;
758 mwmhints.decorations = 0;
759 mwmhints.input_mode = 0L;
760 mwmhints.status = 0L;
762 if (type != Qt::SplashScreen) {
763 mwmhints.flags |= MWM_HINTS_DECORATIONS;
765 bool customize = flags & Qt::CustomizeWindowHint;
766 if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
767 mwmhints.decorations |= MWM_DECOR_BORDER;
768 mwmhints.decorations |= MWM_DECOR_RESIZEH;
770 if (flags & Qt::WindowTitleHint)
771 mwmhints.decorations |= MWM_DECOR_TITLE;
773 if (flags & Qt::WindowSystemMenuHint)
774 mwmhints.decorations |= MWM_DECOR_MENU;
776 if (flags & Qt::WindowMinimizeButtonHint) {
777 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
778 mwmhints.functions |= MWM_FUNC_MINIMIZE;
781 if (flags & Qt::WindowMaximizeButtonHint) {
782 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
783 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
786 if (flags & Qt::WindowCloseButtonHint)
787 mwmhints.functions |= MWM_FUNC_CLOSE;
790 // if type == Qt::SplashScreen
791 mwmhints.decorations = MWM_DECOR_ALL;
794 if (mwmhints.functions != 0) {
795 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
796 mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
798 mwmhints.functions = MWM_FUNC_ALL;
801 if (!(flags & Qt::FramelessWindowHint)
802 && flags & Qt::CustomizeWindowHint
803 && flags & Qt::WindowTitleHint
805 (Qt::WindowMinimizeButtonHint
806 | Qt::WindowMaximizeButtonHint
807 | Qt::WindowCloseButtonHint)))
809 // a special case - only the titlebar without any button
810 mwmhints.flags = MWM_HINTS_FUNCTIONS;
811 mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
812 mwmhints.decorations = 0;
815 setMotifWmHints(connection(), m_window, mwmhints);
818 void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
820 xcb_client_message_event_t event;
822 event.response_type = XCB_CLIENT_MESSAGE;
824 event.window = m_window;
825 event.type = atom(QXcbAtom::_NET_WM_STATE);
826 event.data.data32[0] = set ? 1 : 0;
827 event.data.data32[1] = one;
828 event.data.data32[2] = two;
829 event.data.data32[3] = 0;
830 event.data.data32[4] = 0;
832 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));
835 Qt::WindowState QXcbWindow::setWindowState(Qt::WindowState state)
837 if (state == m_windowState)
841 switch (m_windowState) {
842 case Qt::WindowMinimized:
843 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
845 case Qt::WindowMaximized:
846 changeNetWmState(false,
847 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
848 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
850 case Qt::WindowFullScreen:
851 changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
859 case Qt::WindowMinimized:
861 xcb_client_message_event_t event;
863 event.response_type = XCB_CLIENT_MESSAGE;
865 event.window = m_window;
866 event.type = atom(QXcbAtom::WM_CHANGE_STATE);
867 event.data.data32[0] = XCB_WM_STATE_ICONIC;
868 event.data.data32[1] = 0;
869 event.data.data32[2] = 0;
870 event.data.data32[3] = 0;
871 event.data.data32[4] = 0;
873 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));
876 case Qt::WindowMaximized:
877 changeNetWmState(true,
878 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
879 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
881 case Qt::WindowFullScreen:
882 changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
884 case Qt::WindowNoState:
890 connection()->sync();
892 m_windowState = state;
893 return m_windowState;
896 void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
898 // in order of decreasing priority
899 QVector<uint> windowTypes;
901 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
906 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
910 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
913 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
915 case Qt::SplashScreen:
916 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
922 if (flags & Qt::FramelessWindowHint)
923 windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
925 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
927 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
928 atom(QXcbAtom::_NET_WM_WINDOW_TYPE), XCB_ATOM_ATOM, 32,
929 windowTypes.count(), windowTypes.constData()));
932 void QXcbWindow::updateMotifWmHintsBeforeMap()
934 QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
936 if (window()->windowModality() != Qt::NonModal) {
937 switch (window()->windowModality()) {
938 case Qt::WindowModal:
939 mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
941 case Qt::ApplicationModal:
943 mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
946 mwmhints.flags |= MWM_HINTS_INPUT_MODE;
948 mwmhints.input_mode = MWM_INPUT_MODELESS;
949 mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
952 if (window()->minimumSize() == window()->maximumSize()) {
953 // fixed size, remove the resize handle (since mwm/dtwm
954 // isn't smart enough to do it itself)
955 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
956 if (mwmhints.functions == MWM_FUNC_ALL) {
957 mwmhints.functions = MWM_FUNC_MOVE;
959 mwmhints.functions &= ~MWM_FUNC_RESIZE;
962 if (mwmhints.decorations == MWM_DECOR_ALL) {
963 mwmhints.flags |= MWM_HINTS_DECORATIONS;
964 mwmhints.decorations = (MWM_DECOR_BORDER
968 mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
972 if (window()->windowFlags() & Qt::WindowMinimizeButtonHint) {
973 mwmhints.flags |= MWM_HINTS_DECORATIONS;
974 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
975 mwmhints.functions |= MWM_FUNC_MINIMIZE;
977 if (window()->windowFlags() & Qt::WindowMaximizeButtonHint) {
978 mwmhints.flags |= MWM_HINTS_DECORATIONS;
979 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
980 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
982 if (window()->windowFlags() & Qt::WindowCloseButtonHint)
983 mwmhints.functions |= MWM_FUNC_CLOSE;
985 setMotifWmHints(connection(), m_window, mwmhints);
988 void QXcbWindow::updateNetWmStateBeforeMap()
990 QVector<xcb_atom_t> netWmState;
992 Qt::WindowFlags flags = window()->windowFlags();
993 if (flags & Qt::WindowStaysOnTopHint) {
994 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_ABOVE));
995 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP));
996 } else if (flags & Qt::WindowStaysOnBottomHint) {
997 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_BELOW));
1000 if (window()->windowState() & Qt::WindowFullScreen) {
1001 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
1004 if (window()->windowState() & Qt::WindowMaximized) {
1005 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ));
1006 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
1009 if (window()->windowModality() != Qt::NonModal) {
1010 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MODAL));
1013 setNetWmState(netWmState);
1016 void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
1018 xcb_window_t wid = m_window;
1020 const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1021 if (m_netWmUserTimeWindow || isSupportedByWM) {
1022 if (!m_netWmUserTimeWindow) {
1023 m_netWmUserTimeWindow = xcb_generate_id(xcb_connection());
1024 Q_XCB_CALL(xcb_create_window(xcb_connection(),
1025 XCB_COPY_FROM_PARENT, // depth -- same as root
1026 m_netWmUserTimeWindow, // window id
1027 m_window, // parent window id
1030 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
1031 m_visualId, // visual
1034 wid = m_netWmUserTimeWindow;
1035 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW),
1036 XCB_ATOM_WINDOW, 32, 1, &m_netWmUserTimeWindow);
1037 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME));
1038 } else if (!isSupportedByWM) {
1039 // WM no longer supports it, then we should remove the
1040 // _NET_WM_USER_TIME_WINDOW atom.
1041 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1042 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
1043 m_netWmUserTimeWindow = XCB_NONE;
1045 wid = m_netWmUserTimeWindow;
1048 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
1049 XCB_ATOM_CARDINAL, 32, 1, ×tamp);
1052 void QXcbWindow::setTransparentForMouseEvents(bool transparent)
1054 if (transparent == m_transparent)
1057 xcb_rectangle_t rectangle;
1059 xcb_rectangle_t *rect = 0;
1065 rectangle.width = geometry().width();
1066 rectangle.height = geometry().height();
1071 xcb_xfixes_region_t region = xcb_generate_id(xcb_connection());
1072 xcb_xfixes_create_region(xcb_connection(), region, nrect, rect);
1073 xcb_xfixes_set_window_shape_region_checked(xcb_connection(), m_window, XCB_SHAPE_SK_INPUT, 0, 0, region);
1074 xcb_xfixes_destroy_region(xcb_connection(), region);
1076 m_transparent = transparent;
1079 void QXcbWindow::updateDoesNotAcceptFocus(bool doesNotAcceptFocus)
1081 xcb_get_property_cookie_t cookie = xcb_get_wm_hints(xcb_connection(), m_window);
1083 xcb_generic_error_t *error;
1085 xcb_wm_hints_t hints;
1086 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, &error);
1089 connection()->handleXcbError(error);
1094 xcb_wm_hints_set_input(&hints, !doesNotAcceptFocus);
1095 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
1098 WId QXcbWindow::winId() const
1103 void QXcbWindow::setParent(const QPlatformWindow *parent)
1105 // re-create for compatibility
1108 QPoint topLeft = geometry().topLeft();
1110 xcb_window_t xcb_parent_id = parent ? static_cast<const QXcbWindow *>(parent)->xcb_window() : m_screen->root();
1111 Q_XCB_CALL(xcb_reparent_window(xcb_connection(), xcb_window(), xcb_parent_id, topLeft.x(), topLeft.y()));
1114 void QXcbWindow::setWindowTitle(const QString &title)
1116 QByteArray ba = title.toUtf8();
1117 Q_XCB_CALL(xcb_change_property(xcb_connection(),
1118 XCB_PROP_MODE_REPLACE,
1120 atom(QXcbAtom::_NET_WM_NAME),
1121 atom(QXcbAtom::UTF8_STRING),
1127 void QXcbWindow::raise()
1129 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1130 const quint32 values[] = { XCB_STACK_MODE_ABOVE };
1131 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1134 void QXcbWindow::lower()
1136 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1137 const quint32 values[] = { XCB_STACK_MODE_BELOW };
1138 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1141 void QXcbWindow::propagateSizeHints()
1143 // update WM_NORMAL_HINTS
1144 xcb_size_hints_t hints;
1145 memset(&hints, 0, sizeof(hints));
1147 QRect rect = geometry();
1149 QWindow *win = window();
1151 xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
1152 xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
1153 xcb_size_hints_set_win_gravity(&hints, qt_window_private(win)->positionPolicy == QWindowPrivate::WindowFrameInclusive
1154 ? XCB_GRAVITY_NORTH_WEST : XCB_GRAVITY_STATIC);
1156 QSize minimumSize = win->minimumSize();
1157 QSize maximumSize = win->maximumSize();
1158 QSize baseSize = win->baseSize();
1159 QSize sizeIncrement = win->sizeIncrement();
1161 if (minimumSize.width() > 0 || minimumSize.height() > 0)
1162 xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1164 if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1165 xcb_size_hints_set_max_size(&hints,
1166 qMin(XCOORD_MAX, maximumSize.width()),
1167 qMin(XCOORD_MAX, maximumSize.height()));
1169 if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1170 xcb_size_hints_set_base_size(&hints, baseSize.width(), baseSize.height());
1171 xcb_size_hints_set_resize_inc(&hints, sizeIncrement.width(), sizeIncrement.height());
1174 xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1177 void QXcbWindow::requestActivateWindow()
1182 updateNetWmUserTime(connection()->time());
1184 if (window()->isTopLevel()
1185 && connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_ACTIVE_WINDOW))) {
1186 xcb_client_message_event_t event;
1188 event.response_type = XCB_CLIENT_MESSAGE;
1190 event.window = m_window;
1191 event.type = atom(QXcbAtom::_NET_ACTIVE_WINDOW);
1192 event.data.data32[0] = 1;
1193 event.data.data32[1] = connection()->time();
1194 QWindow *focusWindow = QGuiApplication::focusWindow();
1195 event.data.data32[2] = focusWindow ? focusWindow->winId() : XCB_NONE;
1196 event.data.data32[3] = 0;
1197 event.data.data32[4] = 0;
1199 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));
1201 Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1204 connection()->sync();
1207 #if XCB_USE_MAEMO_WINDOW_PROPERTIES
1208 void QXcbWindow::setOrientation(Qt::ScreenOrientation orientation)
1211 switch (orientation) {
1212 case Qt::PortraitOrientation: angle = 270; break;
1213 case Qt::LandscapeOrientation: angle = 0; break;
1214 case Qt::InvertedPortraitOrientation: angle = 90; break;
1215 case Qt::InvertedLandscapeOrientation: angle = 180; break;
1216 case Qt::PrimaryOrientation: break;
1218 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
1219 atom(QXcbAtom::MeegoTouchOrientationAngle), XCB_ATOM_CARDINAL, 32,
1224 QSurfaceFormat QXcbWindow::format() const
1226 // ### return actual format
1230 #if defined(XCB_USE_EGL)
1231 QXcbEGLSurface *QXcbWindow::eglSurface() const
1233 if (!m_eglSurface) {
1234 EGLDisplay display = connection()->egl_display();
1235 EGLConfig config = q_configFromGLFormat(display, window()->requestedFormat(), true);
1236 EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)m_window, 0);
1238 m_eglSurface = new QXcbEGLSurface(display, surface);
1241 return m_eglSurface;
1245 void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1247 QRect rect(event->x, event->y, event->width, event->height);
1249 if (m_exposeRegion.isEmpty())
1250 m_exposeRegion = rect;
1252 m_exposeRegion |= rect;
1254 // if count is non-zero there are more expose events pending
1255 if (event->count == 0) {
1256 QWindowSystemInterface::handleSynchronousExposeEvent(window(), m_exposeRegion);
1257 m_exposeRegion = QRegion();
1261 void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1263 if (event->format != 32)
1266 if (event->type == atom(QXcbAtom::WM_PROTOCOLS)) {
1267 if (event->data.data32[0] == atom(QXcbAtom::WM_DELETE_WINDOW)) {
1268 QWindowSystemInterface::handleCloseEvent(window());
1269 } else if (event->data.data32[0] == atom(QXcbAtom::WM_TAKE_FOCUS)) {
1270 connection()->setTime(event->data.data32[1]);
1271 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_PING)) {
1272 xcb_client_message_event_t reply = *event;
1274 reply.response_type = XCB_CLIENT_MESSAGE;
1275 reply.window = m_screen->root();
1277 xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&reply);
1278 xcb_flush(xcb_connection());
1279 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_SYNC_REQUEST)) {
1280 connection()->setTime(event->data.data32[1]);
1281 m_syncValue.lo = event->data.data32[2];
1282 m_syncValue.hi = event->data.data32[3];
1284 qWarning() << "QXcbWindow: Unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1286 } else if (event->type == atom(QXcbAtom::XdndEnter)) {
1287 connection()->drag()->handleEnter(window(), event);
1288 } else if (event->type == atom(QXcbAtom::XdndPosition)) {
1289 connection()->drag()->handlePosition(window(), event);
1290 } else if (event->type == atom(QXcbAtom::XdndLeave)) {
1291 connection()->drag()->handleLeave(window(), event);
1292 } else if (event->type == atom(QXcbAtom::XdndDrop)) {
1293 connection()->drag()->handleDrop(window(), event);
1295 qWarning() << "QXcbWindow: Unhandled client message:" << connection()->atomName(event->type);
1299 void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1301 bool fromSendEvent = (event->response_type & 0x80);
1302 QPoint pos(event->x, event->y);
1303 if (!fromSendEvent) {
1304 // Do not trust the position, query it instead.
1305 xcb_translate_coordinates_cookie_t cookie = xcb_translate_coordinates(xcb_connection(), xcb_window(),
1306 m_screen->root(), 0, 0);
1307 xcb_generic_error_t *error;
1308 xcb_translate_coordinates_reply_t *reply = xcb_translate_coordinates_reply(xcb_connection(), cookie, &error);
1310 pos.setX(reply->dst_x);
1311 pos.setY(reply->dst_y);
1318 QRect rect(pos, QSize(event->width, event->height));
1320 QPlatformWindow::setGeometry(rect);
1321 QWindowSystemInterface::handleGeometryChange(window(), rect);
1323 m_dirtyFrameMargins = true;
1327 static_cast<QDri2Context *>(m_context)->resize(rect.size());
1331 void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1333 if (event->window == m_window) {
1335 QWindowSystemInterface::handleMapEvent(window());
1339 void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1341 if (event->window == m_window) {
1343 QWindowSystemInterface::handleUnmapEvent(window());
1347 static Qt::MouseButtons translateMouseButtons(int s)
1349 Qt::MouseButtons ret = 0;
1350 if (s & XCB_BUTTON_MASK_1)
1351 ret |= Qt::LeftButton;
1352 if (s & XCB_BUTTON_MASK_2)
1353 ret |= Qt::MidButton;
1354 if (s & XCB_BUTTON_MASK_3)
1355 ret |= Qt::RightButton;
1359 static Qt::MouseButton translateMouseButton(xcb_button_t s)
1362 case 1: return Qt::LeftButton;
1363 case 2: return Qt::MidButton;
1364 case 3: return Qt::RightButton;
1365 // Button values 4-7 were already handled as Wheel events, and won't occur here.
1366 case 8: return Qt::BackButton; // Also known as Qt::ExtraButton1
1367 case 9: return Qt::ForwardButton; // Also known as Qt::ExtraButton2
1368 case 10: return Qt::ExtraButton3;
1369 case 11: return Qt::ExtraButton4;
1370 case 12: return Qt::ExtraButton5;
1371 case 13: return Qt::ExtraButton6;
1372 case 14: return Qt::ExtraButton7;
1373 case 15: return Qt::ExtraButton8;
1374 case 16: return Qt::ExtraButton9;
1375 case 17: return Qt::ExtraButton10;
1376 case 18: return Qt::ExtraButton11;
1377 case 19: return Qt::ExtraButton12;
1378 case 20: return Qt::ExtraButton13;
1379 case 21: return Qt::ExtraButton14;
1380 case 22: return Qt::ExtraButton15;
1381 case 23: return Qt::ExtraButton16;
1382 case 24: return Qt::ExtraButton17;
1383 case 25: return Qt::ExtraButton18;
1384 case 26: return Qt::ExtraButton19;
1385 case 27: return Qt::ExtraButton20;
1386 case 28: return Qt::ExtraButton21;
1387 case 29: return Qt::ExtraButton22;
1388 case 30: return Qt::ExtraButton23;
1389 case 31: return Qt::ExtraButton24;
1390 default: return Qt::NoButton;
1394 void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1396 updateNetWmUserTime(event->time);
1398 QPoint local(event->event_x, event->event_y);
1399 QPoint global(event->root_x, event->root_y);
1401 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1403 if (event->detail >= 4 && event->detail <= 7) {
1404 // Logic borrowed from qapplication_x11.cpp
1405 int delta = 120 * ((event->detail == 4 || event->detail == 6) ? 1 : -1);
1406 bool hor = (((event->detail == 4 || event->detail == 5)
1407 && (modifiers & Qt::AltModifier))
1408 || (event->detail == 6 || event->detail == 7));
1410 QWindowSystemInterface::handleWheelEvent(window(), event->time,
1411 local, global, delta, hor ? Qt::Horizontal : Qt::Vertical, modifiers);
1415 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1418 void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1420 QPoint local(event->event_x, event->event_y);
1421 QPoint global(event->root_x, event->root_y);
1422 Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1424 handleMouseEvent(event->detail, event->state, event->time, local, global, modifiers);
1427 void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_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::handleMouseEvent(xcb_button_t detail, uint16_t state, xcb_timestamp_t time, const QPoint &local, const QPoint &global, Qt::KeyboardModifiers modifiers)
1438 connection()->setTime(time);
1440 Qt::MouseButtons buttons = translateMouseButtons(state);
1441 Qt::MouseButton button = translateMouseButton(detail);
1443 buttons ^= button; // X event uses state *before*, Qt uses state *after*
1445 QWindowSystemInterface::handleMouseEvent(window(), time, local, global, buttons, modifiers);
1448 void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1450 connection()->setTime(event->time);
1452 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1453 || event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1454 || event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1459 QWindowSystemInterface::handleEnterEvent(window());
1462 void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1464 connection()->setTime(event->time);
1466 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1467 || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)
1472 QWindowSystemInterface::handleLeaveEvent(window());
1475 void QXcbWindow::handlePropertyNotifyEvent(const xcb_property_notify_event_t *event)
1477 connection()->setTime(event->time);
1479 bool propertyDeleted = event->state == XCB_PROPERTY_DELETE;
1481 if (event->atom == atom(QXcbAtom::_NET_WM_STATE) || event->atom == atom(QXcbAtom::WM_STATE)) {
1482 if (propertyDeleted)
1485 xcb_get_property_cookie_t get_cookie =
1486 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::WM_STATE),
1487 XCB_ATOM_ANY, 0, 1024);
1489 xcb_generic_error_t *error;
1491 xcb_get_property_reply_t *reply =
1492 xcb_get_property_reply(xcb_connection(), get_cookie, &error);
1494 xcb_atom_t wm_state = XCB_WM_STATE_WITHDRAWN;
1495 if (reply && reply->format == 32 && reply->type == atom(QXcbAtom::WM_STATE)) {
1496 if (reply->length != 0)
1497 wm_state = ((long *)xcb_get_property_value(reply))[0];
1500 connection()->handleXcbError(error);
1504 QVector<xcb_atom_t> netWmState = getNetWmState();
1506 bool maximized = netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ))
1507 && netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
1508 bool fullscreen = netWmState.contains(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
1510 Qt::WindowState state = Qt::WindowNoState;
1511 if (wm_state == XCB_WM_STATE_ICONIC)
1512 state = Qt::WindowMinimized;
1514 state = Qt::WindowMaximized;
1515 else if (fullscreen)
1516 state = Qt::WindowFullScreen;
1518 QWindowSystemInterface::handleWindowStateChanged(window(), state);
1522 void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1524 QWindowSystemInterface::handleWindowActivated(window());
1527 static bool focusInPeeker(xcb_generic_event_t *event)
1530 // FocusIn event is not in the queue, proceed with FocusOut normally.
1531 QWindowSystemInterface::handleWindowActivated(0);
1534 uint response_type = event->response_type & ~0x80;
1535 return response_type == XCB_FOCUS_IN;
1538 void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1540 // Do not set the active window to 0 if there is a FocusIn coming.
1541 // There is however no equivalent for XPutBackEvent so register a
1542 // callback for QXcbConnection instead.
1543 connection()->addPeekFunc(focusInPeeker);
1546 void QXcbWindow::updateSyncRequestCounter()
1548 if (m_screen->syncRequestSupported() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
1549 Q_XCB_CALL(xcb_sync_set_counter(xcb_connection(), m_syncCounter, m_syncValue));
1550 xcb_flush(xcb_connection());
1551 connection()->sync();
1558 bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1561 xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1564 xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
1565 m_window, XCB_TIME_CURRENT_TIME,
1566 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1567 xcb_generic_error_t *err;
1568 xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, &err);
1569 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1575 bool QXcbWindow::setMouseGrabEnabled(bool grab)
1578 xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1581 xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
1582 (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
1583 | XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW
1584 | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_POINTER_MOTION),
1585 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
1586 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
1587 XCB_TIME_CURRENT_TIME);
1588 xcb_generic_error_t *err;
1589 xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, &err);
1590 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1596 void QXcbWindow::setCursor(xcb_cursor_t cursor)
1598 xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1599 xcb_flush(xcb_connection());