1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the plugins of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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 "qxcbwmsupport.h"
53 #include "qdri2context.h"
56 #include <xcb/xcb_icccm.h>
58 #include <private/qguiapplication_p.h>
59 #include <private/qwindow_p.h>
61 #include <QtGui/QPlatformBackingStore>
62 #include <QtGui/QWindowSystemInterface>
68 #include <X11/Xutil.h>
71 #if defined(XCB_USE_GLX)
72 #include "qglxintegration.h"
73 #include <QtPlatformSupport/private/qglxconvenience_p.h>
74 #elif defined(XCB_USE_EGL)
75 #include "qxcbeglsurface.h"
76 #include <QtPlatformSupport/private/qeglconvenience_p.h>
77 #include <QtPlatformSupport/private/qxlibeglintegration_p.h>
80 #define XCOORD_MAX 16383
82 //#ifdef NET_WM_STATE_DEBUG
84 // Returns true if we should set WM_TRANSIENT_FOR on \a w
85 static inline bool isTransient(const QWindow *w)
87 return w->windowType() == Qt::Dialog
88 || w->windowType() == Qt::Sheet
89 || w->windowType() == Qt::Tool
90 || w->windowType() == Qt::SplashScreen
91 || w->windowType() == Qt::ToolTip
92 || w->windowType() == Qt::Drawer
93 || w->windowType() == Qt::Popup;
96 QXcbWindow::QXcbWindow(QWindow *window)
97 : QPlatformWindow(window)
101 , m_netWmUserTimeWindow(XCB_NONE)
102 #if defined(XCB_USE_EGL)
106 m_screen = static_cast<QXcbScreen *>(window->screen()->handle());
108 setConnection(m_screen->connection());
113 void QXcbWindow::create()
115 bool wasCreated = (m_window != 0);
118 m_windowState = Qt::WindowNoState;
119 m_hasReceivedSyncRequest = false;
120 m_dirtyFrameMargins = true;
122 Qt::WindowType type = window()->windowType();
124 if (type == Qt::Desktop) {
125 m_window = m_screen->root();
126 m_depth = m_screen->screen()->root_depth;
127 m_imageFormat = (m_depth == 32) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
128 connection()->addWindow(m_window, this);
132 const quint32 mask = XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK;
133 const quint32 values[] = {
134 // XCB_CW_BACK_PIXMAP
136 // XCB_CW_OVERRIDE_REDIRECT
137 type == Qt::Popup || type == Qt::ToolTip,
139 type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
141 XCB_EVENT_MASK_EXPOSURE
142 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
143 | XCB_EVENT_MASK_KEY_PRESS
144 | XCB_EVENT_MASK_KEY_RELEASE
145 | XCB_EVENT_MASK_BUTTON_PRESS
146 | XCB_EVENT_MASK_BUTTON_RELEASE
147 | XCB_EVENT_MASK_BUTTON_MOTION
148 | XCB_EVENT_MASK_ENTER_WINDOW
149 | XCB_EVENT_MASK_LEAVE_WINDOW
150 | XCB_EVENT_MASK_POINTER_MOTION
151 | XCB_EVENT_MASK_PROPERTY_CHANGE
152 | XCB_EVENT_MASK_FOCUS_CHANGE
155 QRect rect = window()->geometry();
156 QPlatformWindow::setGeometry(rect);
158 xcb_window_t xcb_parent_id = m_screen->root();
160 xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
162 m_requestedFormat = window()->format();
164 #if defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
165 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL)
166 || window()->format().hasAlpha())
168 #if defined(XCB_USE_GLX)
169 XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(m_screen),m_screen->screenNumber(), window()->format());
171 #elif defined(XCB_USE_EGL)
172 EGLDisplay eglDisplay = connection()->egl_display();
173 EGLConfig eglConfig = q_configFromGLFormat(eglDisplay, window()->format(), true);
174 VisualID id = QXlibEglIntegration::getCompatibleVisualId(DISPLAY_FROM_XCB(this), eglDisplay, eglConfig);
176 XVisualInfo visualInfoTemplate;
177 memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
178 visualInfoTemplate.visualid = id;
180 XVisualInfo *visualInfo;
181 int matchingCount = 0;
182 visualInfo = XGetVisualInfo(DISPLAY_FROM_XCB(this), VisualIDMask, &visualInfoTemplate, &matchingCount);
185 m_depth = visualInfo->depth;
186 m_imageFormat = (m_depth == 32) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
187 Colormap cmap = XCreateColormap(DISPLAY_FROM_XCB(this), xcb_parent_id, visualInfo->visual, AllocNone);
189 XSetWindowAttributes a;
190 a.background_pixel = WhitePixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
191 a.border_pixel = BlackPixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
193 m_window = XCreateWindow(DISPLAY_FROM_XCB(this), xcb_parent_id, rect.x(), rect.y(), rect.width(), rect.height(),
194 0, visualInfo->depth, InputOutput, visualInfo->visual,
195 CWBackPixel|CWBorderPixel|CWColormap, &a);
197 qFatal("no window!");
200 #endif //defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
202 m_window = xcb_generate_id(xcb_connection());
203 m_depth = m_screen->screen()->root_depth;
204 m_imageFormat = (m_depth == 32) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
206 Q_XCB_CALL(xcb_create_window(xcb_connection(),
207 XCB_COPY_FROM_PARENT, // depth -- same as root
208 m_window, // window id
209 xcb_parent_id, // parent window id
215 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
216 m_screen->screen()->root_visual, // visual
221 connection()->addWindow(m_window, this);
223 Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
225 xcb_atom_t properties[4];
226 int propertyCount = 0;
227 properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
228 properties[propertyCount++] = atom(QXcbAtom::WM_TAKE_FOCUS);
229 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_PING);
231 if (m_screen->syncRequestSupported())
232 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
234 if (window()->windowFlags() & Qt::WindowContextHelpButtonHint)
235 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
237 Q_XCB_CALL(xcb_change_property(xcb_connection(),
238 XCB_PROP_MODE_REPLACE,
240 atom(QXcbAtom::WM_PROTOCOLS),
248 if (m_screen->syncRequestSupported()) {
249 m_syncCounter = xcb_generate_id(xcb_connection());
250 Q_XCB_CALL(xcb_sync_create_counter(xcb_connection(), m_syncCounter, m_syncValue));
252 Q_XCB_CALL(xcb_change_property(xcb_connection(),
253 XCB_PROP_MODE_REPLACE,
255 atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
262 // set the PID to let the WM kill the application if unresponsive
264 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
265 atom(QXcbAtom::_NET_WM_PID), XCB_ATOM_CARDINAL, 32,
268 xcb_wm_hints_t hints;
269 memset(&hints, 0, sizeof(hints));
270 xcb_wm_hints_set_normal(&hints);
272 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
274 xcb_window_t leader = m_screen->clientLeader();
275 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
276 atom(QXcbAtom::WM_CLIENT_LEADER), XCB_ATOM_WINDOW, 32,
280 setWindowFlags(window()->windowFlags());
282 connection()->drag()->dndEnable(this, true);
285 QXcbWindow::~QXcbWindow()
290 void QXcbWindow::destroy()
292 if (m_syncCounter && m_screen->syncRequestSupported())
293 Q_XCB_CALL(xcb_sync_destroy_counter(xcb_connection(), m_syncCounter));
295 connection()->removeWindow(m_window);
296 Q_XCB_CALL(xcb_destroy_window(xcb_connection(), m_window));
300 #if defined(XCB_USE_EGL)
306 void QXcbWindow::setGeometry(const QRect &rect)
308 QPlatformWindow::setGeometry(rect);
310 const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
311 const quint32 values[] = { rect.x(),
313 qBound(1, rect.width(), XCOORD_MAX),
314 qBound(1, rect.height(), XCOORD_MAX) };
316 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
319 QMargins QXcbWindow::frameMargins() const
321 if (m_dirtyFrameMargins) {
322 xcb_window_t window = m_window;
323 xcb_window_t parent = m_window;
325 bool foundRoot = false;
327 const QVector<xcb_window_t> &virtualRoots =
328 connection()->wmSupport()->virtualRoots();
331 xcb_query_tree_cookie_t cookie = xcb_query_tree(xcb_connection(), parent);
333 xcb_generic_error_t *error;
334 xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, &error);
336 if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1) {
340 parent = reply->parent;
346 connection()->handleXcbError(error);
350 m_dirtyFrameMargins = false;
351 m_frameMargins = QMargins();
352 return m_frameMargins;
358 xcb_generic_error_t *error;
359 xcb_translate_coordinates_reply_t *reply =
360 xcb_translate_coordinates_reply(
362 xcb_translate_coordinates(xcb_connection(), window, parent, 0, 0),
366 offset = QPoint(reply->dst_x, reply->dst_y);
372 xcb_get_geometry_reply_t *geom =
373 xcb_get_geometry_reply(
375 xcb_get_geometry(xcb_connection(), parent),
380 // add the border_width for the window managers frame... some window managers
381 // do not use a border_width of zero for their frames, and if we the left and
382 // top strut, we ensure that pos() is absolutely correct. frameGeometry()
383 // will still be incorrect though... perhaps i should have foffset as well, to
384 // indicate the frame offset (equal to the border_width on X).
386 // -- copied from qwidget_x11.cpp
388 int left = offset.x() + geom->border_width;
389 int top = offset.y() + geom->border_width;
390 int right = geom->width + geom->border_width - geometry().width() - offset.x();
391 int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
393 m_frameMargins = QMargins(left, top, right, bottom);
400 m_dirtyFrameMargins = false;
403 return m_frameMargins;
406 void QXcbWindow::setVisible(bool visible)
414 void QXcbWindow::show()
416 if (window()->isTopLevel()) {
417 xcb_get_property_cookie_t cookie = xcb_get_wm_hints(xcb_connection(), m_window);
419 xcb_generic_error_t *error;
421 xcb_wm_hints_t hints;
422 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, &error);
425 connection()->handleXcbError(error);
429 m_dirtyFrameMargins = true;
431 if (window()->windowState() & Qt::WindowMinimized)
432 xcb_wm_hints_set_iconic(&hints);
434 xcb_wm_hints_set_normal(&hints);
436 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
438 // update WM_NORMAL_HINTS
439 propagateSizeHints();
441 // update WM_TRANSIENT_FOR
442 if (window()->transientParent() && isTransient(window())) {
443 QXcbWindow *transientXcbParent = static_cast<QXcbWindow *>(window()->transientParent()->handle());
444 if (transientXcbParent) {
446 xcb_window_t parentWindow = transientXcbParent->xcb_window();
448 // todo: set transient for group (wm_client_leader) if no parent, a la qwidget_x11.cpp
449 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
450 XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 32,
455 // update _MOTIF_WM_HINTS
456 updateMotifWmHintsBeforeMap();
458 // update _NET_WM_STATE
459 updateNetWmStateBeforeMap();
462 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
463 xcb_flush(xcb_connection());
465 connection()->sync();
468 void QXcbWindow::hide()
470 Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
472 // send synthetic UnmapNotify event according to icccm 4.1.4
473 xcb_unmap_notify_event_t event;
474 event.response_type = XCB_UNMAP_NOTIFY;
475 event.event = m_screen->root();
476 event.window = m_window;
477 event.from_configure = false;
478 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, m_screen->root(),
479 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
481 xcb_flush(xcb_connection());
486 struct QtMotifWmHints {
487 quint32 flags, functions, decorations;
493 MWM_HINTS_FUNCTIONS = (1L << 0),
495 MWM_FUNC_ALL = (1L << 0),
496 MWM_FUNC_RESIZE = (1L << 1),
497 MWM_FUNC_MOVE = (1L << 2),
498 MWM_FUNC_MINIMIZE = (1L << 3),
499 MWM_FUNC_MAXIMIZE = (1L << 4),
500 MWM_FUNC_CLOSE = (1L << 5),
502 MWM_HINTS_DECORATIONS = (1L << 1),
504 MWM_DECOR_ALL = (1L << 0),
505 MWM_DECOR_BORDER = (1L << 1),
506 MWM_DECOR_RESIZEH = (1L << 2),
507 MWM_DECOR_TITLE = (1L << 3),
508 MWM_DECOR_MENU = (1L << 4),
509 MWM_DECOR_MINIMIZE = (1L << 5),
510 MWM_DECOR_MAXIMIZE = (1L << 6),
512 MWM_HINTS_INPUT_MODE = (1L << 2),
514 MWM_INPUT_MODELESS = 0L,
515 MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
516 MWM_INPUT_FULL_APPLICATION_MODAL = 3L
519 static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
521 QtMotifWmHints hints;
523 xcb_get_property_cookie_t get_cookie =
524 xcb_get_property(c->xcb_connection(), 0, window, c->atom(QXcbAtom::_MOTIF_WM_HINTS),
525 c->atom(QXcbAtom::_MOTIF_WM_HINTS), 0, 20);
527 xcb_generic_error_t *error;
529 xcb_get_property_reply_t *reply =
530 xcb_get_property_reply(c->xcb_connection(), get_cookie, &error);
532 if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
533 hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
535 c->handleXcbError(error);
539 hints.functions = MWM_FUNC_ALL;
540 hints.decorations = MWM_DECOR_ALL;
541 hints.input_mode = 0L;
550 static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
552 if (hints.flags != 0l) {
553 Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
554 XCB_PROP_MODE_REPLACE,
556 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
557 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
562 Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
566 void QXcbWindow::printNetWmState(const QVector<xcb_atom_t> &state)
568 printf("_NET_WM_STATE (%d): ", state.size());
569 for (int i = 0; i < state.size(); ++i) {
570 #define CHECK_WM_STATE(state_atom) \
571 if (state.at(i) == atom(QXcbAtom::state_atom))\
572 printf(#state_atom " ");
573 CHECK_WM_STATE(_NET_WM_STATE_ABOVE)
574 CHECK_WM_STATE(_NET_WM_STATE_BELOW)
575 CHECK_WM_STATE(_NET_WM_STATE_FULLSCREEN)
576 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_HORZ)
577 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_VERT)
578 CHECK_WM_STATE(_NET_WM_STATE_MODAL)
579 CHECK_WM_STATE(_NET_WM_STATE_STAYS_ON_TOP)
580 CHECK_WM_STATE(_NET_WM_STATE_DEMANDS_ATTENTION)
581 #undef CHECK_WM_STATE
586 QVector<xcb_atom_t> QXcbWindow::getNetWmState()
588 QVector<xcb_atom_t> result;
590 xcb_get_property_cookie_t get_cookie =
591 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::_NET_WM_STATE),
592 XCB_ATOM_ATOM, 0, 1024);
594 xcb_generic_error_t *error;
596 xcb_get_property_reply_t *reply =
597 xcb_get_property_reply(xcb_connection(), get_cookie, &error);
599 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
600 result.resize(reply->length);
602 memcpy(result.data(), xcb_get_property_value(reply), reply->length * sizeof(xcb_atom_t));
604 #ifdef NET_WM_STATE_DEBUG
605 printf("getting net wm state (%x)\n", m_window);
606 printNetWmState(result);
611 connection()->handleXcbError(error);
614 #ifdef NET_WM_STATE_DEBUG
615 printf("getting net wm state (%x), empty\n", m_window);
622 void QXcbWindow::setNetWmState(const QVector<xcb_atom_t> &atoms)
624 if (atoms.isEmpty()) {
625 Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
627 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
628 atom(QXcbAtom::_NET_WM_STATE), XCB_ATOM_ATOM, 32,
629 atoms.count(), atoms.constData()));
631 xcb_flush(xcb_connection());
635 Qt::WindowFlags QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
637 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
639 if (type == Qt::ToolTip)
640 flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
641 if (type == Qt::Popup)
642 flags |= Qt::X11BypassWindowManagerHint;
644 setNetWmWindowFlags(flags);
645 setMotifWindowFlags(flags);
650 void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
652 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
654 QtMotifWmHints mwmhints;
656 mwmhints.functions = 0L;
657 mwmhints.decorations = 0;
658 mwmhints.input_mode = 0L;
659 mwmhints.status = 0L;
661 if (type != Qt::SplashScreen) {
662 mwmhints.flags |= MWM_HINTS_DECORATIONS;
664 bool customize = flags & Qt::CustomizeWindowHint;
665 if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
666 mwmhints.decorations |= MWM_DECOR_BORDER;
667 mwmhints.decorations |= MWM_DECOR_RESIZEH;
669 if (flags & Qt::WindowTitleHint)
670 mwmhints.decorations |= MWM_DECOR_TITLE;
672 if (flags & Qt::WindowSystemMenuHint)
673 mwmhints.decorations |= MWM_DECOR_MENU;
675 if (flags & Qt::WindowMinimizeButtonHint) {
676 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
677 mwmhints.functions |= MWM_FUNC_MINIMIZE;
680 if (flags & Qt::WindowMaximizeButtonHint) {
681 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
682 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
685 if (flags & Qt::WindowCloseButtonHint)
686 mwmhints.functions |= MWM_FUNC_CLOSE;
689 // if type == Qt::SplashScreen
690 mwmhints.decorations = MWM_DECOR_ALL;
693 if (mwmhints.functions != 0) {
694 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
695 mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
697 mwmhints.functions = MWM_FUNC_ALL;
700 if (!(flags & Qt::FramelessWindowHint)
701 && flags & Qt::CustomizeWindowHint
702 && flags & Qt::WindowTitleHint
704 (Qt::WindowMinimizeButtonHint
705 | Qt::WindowMaximizeButtonHint
706 | Qt::WindowCloseButtonHint)))
708 // a special case - only the titlebar without any button
709 mwmhints.flags = MWM_HINTS_FUNCTIONS;
710 mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
711 mwmhints.decorations = 0;
714 setMotifWmHints(connection(), m_window, mwmhints);
717 void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
719 xcb_client_message_event_t event;
721 event.response_type = XCB_CLIENT_MESSAGE;
723 event.window = m_window;
724 event.type = atom(QXcbAtom::_NET_WM_STATE);
725 event.data.data32[0] = set ? 1 : 0;
726 event.data.data32[1] = one;
727 event.data.data32[2] = two;
728 event.data.data32[3] = 0;
729 event.data.data32[4] = 0;
731 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));
734 Qt::WindowState QXcbWindow::setWindowState(Qt::WindowState state)
736 if (state == m_windowState)
739 m_dirtyFrameMargins = true;
742 switch (m_windowState) {
743 case Qt::WindowMinimized:
744 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
746 case Qt::WindowMaximized:
747 changeNetWmState(false,
748 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
749 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
751 case Qt::WindowFullScreen:
752 changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
760 case Qt::WindowMinimized:
762 xcb_client_message_event_t event;
764 event.response_type = XCB_CLIENT_MESSAGE;
766 event.window = m_window;
767 event.type = atom(QXcbAtom::WM_CHANGE_STATE);
768 event.data.data32[0] = XCB_WM_STATE_ICONIC;
769 event.data.data32[1] = 0;
770 event.data.data32[2] = 0;
771 event.data.data32[3] = 0;
772 event.data.data32[4] = 0;
774 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));
777 case Qt::WindowMaximized:
778 changeNetWmState(true,
779 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
780 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
782 case Qt::WindowFullScreen:
783 changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
785 case Qt::WindowNoState:
791 connection()->sync();
793 m_windowState = state;
794 return m_windowState;
797 void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
799 // in order of decreasing priority
800 QVector<uint> windowTypes;
802 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
807 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
811 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
814 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
816 case Qt::SplashScreen:
817 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
823 if (flags & Qt::FramelessWindowHint)
824 windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
826 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
828 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
829 atom(QXcbAtom::_NET_WM_WINDOW_TYPE), XCB_ATOM_ATOM, 32,
830 windowTypes.count(), windowTypes.constData()));
833 void QXcbWindow::updateMotifWmHintsBeforeMap()
835 QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
837 if (window()->windowModality() != Qt::NonModal) {
838 switch (window()->windowModality()) {
839 case Qt::WindowModal:
840 mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
842 case Qt::ApplicationModal:
844 mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
847 mwmhints.flags |= MWM_HINTS_INPUT_MODE;
849 mwmhints.input_mode = MWM_INPUT_MODELESS;
850 mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
853 if (window()->minimumSize() == window()->maximumSize()) {
854 // fixed size, remove the resize handle (since mwm/dtwm
855 // isn't smart enough to do it itself)
856 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
857 if (mwmhints.functions == MWM_FUNC_ALL) {
858 mwmhints.functions = MWM_FUNC_MOVE;
860 mwmhints.functions &= ~MWM_FUNC_RESIZE;
863 if (mwmhints.decorations == MWM_DECOR_ALL) {
864 mwmhints.flags |= MWM_HINTS_DECORATIONS;
865 mwmhints.decorations = (MWM_DECOR_BORDER
869 mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
873 if (window()->windowFlags() & Qt::WindowMinimizeButtonHint) {
874 mwmhints.flags |= MWM_HINTS_DECORATIONS;
875 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
876 mwmhints.functions |= MWM_FUNC_MINIMIZE;
878 if (window()->windowFlags() & Qt::WindowMaximizeButtonHint) {
879 mwmhints.flags |= MWM_HINTS_DECORATIONS;
880 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
881 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
883 if (window()->windowFlags() & Qt::WindowCloseButtonHint)
884 mwmhints.functions |= MWM_FUNC_CLOSE;
886 setMotifWmHints(connection(), m_window, mwmhints);
889 void QXcbWindow::updateNetWmStateBeforeMap()
891 QVector<xcb_atom_t> netWmState;
893 Qt::WindowFlags flags = window()->windowFlags();
894 if (flags & Qt::WindowStaysOnTopHint) {
895 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_ABOVE));
896 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP));
897 } else if (flags & Qt::WindowStaysOnBottomHint) {
898 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_BELOW));
901 if (window()->windowState() & Qt::WindowFullScreen) {
902 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
905 if (window()->windowState() & Qt::WindowMaximized) {
906 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ));
907 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
910 if (window()->windowModality() != Qt::NonModal) {
911 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MODAL));
914 setNetWmState(netWmState);
917 void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
919 xcb_window_t wid = m_window;
921 const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
922 if (m_netWmUserTimeWindow || isSupportedByWM) {
923 if (!m_netWmUserTimeWindow) {
924 m_netWmUserTimeWindow = xcb_generate_id(xcb_connection());
925 Q_XCB_CALL(xcb_create_window(xcb_connection(),
926 XCB_COPY_FROM_PARENT, // depth -- same as root
927 m_netWmUserTimeWindow, // window id
928 m_window, // parent window id
931 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
932 m_screen->screen()->root_visual, // visual
935 wid = m_netWmUserTimeWindow;
936 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW),
937 XCB_ATOM_WINDOW, 32, 1, &m_netWmUserTimeWindow);
938 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME));
939 } else if (!isSupportedByWM) {
940 // WM no longer supports it, then we should remove the
941 // _NET_WM_USER_TIME_WINDOW atom.
942 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
943 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
944 m_netWmUserTimeWindow = XCB_NONE;
946 wid = m_netWmUserTimeWindow;
949 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
950 XCB_ATOM_CARDINAL, 32, 1, ×tamp);
954 WId QXcbWindow::winId() const
959 void QXcbWindow::setParent(const QPlatformWindow *parent)
961 // re-create for compatibility
964 QPoint topLeft = geometry().topLeft();
966 xcb_window_t xcb_parent_id = parent ? static_cast<const QXcbWindow *>(parent)->xcb_window() : m_screen->root();
967 Q_XCB_CALL(xcb_reparent_window(xcb_connection(), xcb_window(), xcb_parent_id, topLeft.x(), topLeft.y()));
970 void QXcbWindow::setWindowTitle(const QString &title)
972 QByteArray ba = title.toUtf8();
973 Q_XCB_CALL(xcb_change_property(xcb_connection(),
974 XCB_PROP_MODE_REPLACE,
976 atom(QXcbAtom::_NET_WM_NAME),
977 atom(QXcbAtom::UTF8_STRING),
983 void QXcbWindow::raise()
985 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
986 const quint32 values[] = { XCB_STACK_MODE_ABOVE };
987 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
990 void QXcbWindow::lower()
992 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
993 const quint32 values[] = { XCB_STACK_MODE_BELOW };
994 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
997 void QXcbWindow::propagateSizeHints()
999 // update WM_NORMAL_HINTS
1000 xcb_size_hints_t hints;
1001 memset(&hints, 0, sizeof(hints));
1003 QRect rect = geometry();
1005 xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
1006 xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
1007 xcb_size_hints_set_win_gravity(&hints, XCB_GRAVITY_STATIC);
1009 QWindow *win = window();
1011 QSize minimumSize = win->minimumSize();
1012 QSize maximumSize = win->maximumSize();
1013 QSize baseSize = win->baseSize();
1014 QSize sizeIncrement = win->sizeIncrement();
1016 if (minimumSize.width() > 0 || minimumSize.height() > 0)
1017 xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1019 if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1020 xcb_size_hints_set_max_size(&hints,
1021 qMin(XCOORD_MAX, maximumSize.width()),
1022 qMin(XCOORD_MAX, maximumSize.height()));
1024 if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1025 xcb_size_hints_set_base_size(&hints, baseSize.width(), baseSize.height());
1026 xcb_size_hints_set_resize_inc(&hints, sizeIncrement.width(), sizeIncrement.height());
1029 xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1032 void QXcbWindow::requestActivateWindow()
1035 updateNetWmUserTime(connection()->time());
1036 Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1038 connection()->sync();
1041 QSurfaceFormat QXcbWindow::format() const
1043 // ### return actual format
1044 return m_requestedFormat;
1047 #if defined(XCB_USE_EGL)
1048 QXcbEGLSurface *QXcbWindow::eglSurface() const
1050 if (!m_eglSurface) {
1051 EGLDisplay display = connection()->egl_display();
1052 EGLConfig config = q_configFromGLFormat(display, window()->format(), true);
1053 EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)m_window, 0);
1055 m_eglSurface = new QXcbEGLSurface(display, surface);
1058 return m_eglSurface;
1062 void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1064 QRect rect(event->x, event->y, event->width, event->height);
1065 QWindowSystemInterface::handleExposeEvent(window(), rect);
1068 void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1070 if (event->format != 32)
1073 if (event->type == atom(QXcbAtom::WM_PROTOCOLS)) {
1074 if (event->data.data32[0] == atom(QXcbAtom::WM_DELETE_WINDOW)) {
1075 QWindowSystemInterface::handleCloseEvent(window());
1076 } else if (event->data.data32[0] == atom(QXcbAtom::WM_TAKE_FOCUS)) {
1077 connection()->setTime(event->data.data32[1]);
1078 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_PING)) {
1079 xcb_client_message_event_t reply = *event;
1081 reply.response_type = XCB_CLIENT_MESSAGE;
1082 reply.window = m_screen->root();
1084 xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&reply);
1085 xcb_flush(xcb_connection());
1086 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_SYNC_REQUEST)) {
1087 connection()->setTime(event->data.data32[1]);
1088 if (!m_hasReceivedSyncRequest) {
1089 m_hasReceivedSyncRequest = true;
1090 printf("Window manager supports _NET_WM_SYNC_REQUEST, syncing resizes\n");
1092 m_syncValue.lo = event->data.data32[2];
1093 m_syncValue.hi = event->data.data32[3];
1095 qWarning() << "unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1097 } else if (event->type == atom(QXcbAtom::XdndEnter)) {
1098 connection()->drag()->handleEnter(window(), event);
1099 } else if (event->type == atom(QXcbAtom::XdndPosition)) {
1100 connection()->drag()->handlePosition(window(), event, false);
1101 } else if (event->type == atom(QXcbAtom::XdndLeave)) {
1102 connection()->drag()->handleLeave(window(), event, false);
1103 } else if (event->type == atom(QXcbAtom::XdndDrop)) {
1104 connection()->drag()->handleDrop(window(), event, false);
1106 qWarning() << "unhandled client message:" << connection()->atomName(event->type);
1110 void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1112 int xpos = geometry().x();
1113 int ypos = geometry().y();
1115 if ((event->width == geometry().width() && event->height == geometry().height()) || event->x != 0 || event->y != 0) {
1120 QRect rect(xpos, ypos, event->width, event->height);
1122 if (rect == geometry())
1125 QPlatformWindow::setGeometry(rect);
1126 QWindowSystemInterface::handleGeometryChange(window(), rect);
1130 static_cast<QDri2Context *>(m_context)->resize(rect.size());
1134 void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1136 if (event->window == m_window) {
1138 QWindowSystemInterface::handleMapEvent(window());
1142 void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1144 if (event->window == m_window) {
1146 QWindowSystemInterface::handleUnmapEvent(window());
1150 static Qt::MouseButtons translateMouseButtons(int s)
1152 Qt::MouseButtons ret = 0;
1153 if (s & XCB_BUTTON_MASK_1)
1154 ret |= Qt::LeftButton;
1155 if (s & XCB_BUTTON_MASK_2)
1156 ret |= Qt::MidButton;
1157 if (s & XCB_BUTTON_MASK_3)
1158 ret |= Qt::RightButton;
1162 static Qt::MouseButton translateMouseButton(xcb_button_t s)
1166 return Qt::LeftButton;
1168 return Qt::MidButton;
1170 return Qt::RightButton;
1172 return Qt::NoButton;
1176 void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1178 updateNetWmUserTime(event->time);
1180 QPoint local(event->event_x, event->event_y);
1181 QPoint global(event->root_x, event->root_y);
1183 Qt::KeyboardModifiers modifiers = Qt::NoModifier;
1185 if (event->detail >= 4 && event->detail <= 7) {
1186 //logic borrowed from qapplication_x11.cpp
1187 int delta = 120 * ((event->detail == 4 || event->detail == 6) ? 1 : -1);
1188 bool hor = (((event->detail == 4 || event->detail == 5)
1189 && (modifiers & Qt::AltModifier))
1190 || (event->detail == 6 || event->detail == 7));
1192 QWindowSystemInterface::handleWheelEvent(window(), event->time,
1193 local, global, delta, hor ? Qt::Horizontal : Qt::Vertical);
1197 handleMouseEvent(event->detail, event->state, event->time, local, global);
1200 void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1202 QPoint local(event->event_x, event->event_y);
1203 QPoint global(event->root_x, event->root_y);
1205 handleMouseEvent(event->detail, event->state, event->time, local, global);
1208 void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
1210 QPoint local(event->event_x, event->event_y);
1211 QPoint global(event->root_x, event->root_y);
1213 handleMouseEvent(event->detail, event->state, event->time, local, global);
1216 void QXcbWindow::handleMouseEvent(xcb_button_t detail, uint16_t state, xcb_timestamp_t time, const QPoint &local, const QPoint &global)
1218 connection()->setTime(time);
1220 Qt::MouseButtons buttons = translateMouseButtons(state);
1221 Qt::MouseButton button = translateMouseButton(detail);
1223 buttons ^= button; // X event uses state *before*, Qt uses state *after*
1225 QWindowSystemInterface::handleMouseEvent(window(), time, local, global, buttons);
1228 void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1230 connection()->setTime(event->time);
1232 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1233 || event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1234 || event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1239 QWindowSystemInterface::handleEnterEvent(window());
1242 void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1244 connection()->setTime(event->time);
1246 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1247 || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)
1252 QWindowSystemInterface::handleLeaveEvent(window());
1255 void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1257 QWindowSystemInterface::handleWindowActivated(window());
1260 static bool focusInPeeker(xcb_generic_event_t *event)
1263 // FocusIn event is not in the queue, proceed with FocusOut normally.
1264 QWindowSystemInterface::handleWindowActivated(0);
1267 uint response_type = event->response_type & ~0x80;
1268 return response_type == XCB_FOCUS_IN;
1271 void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1273 // Do not set the active window to 0 if there is a FocusIn coming.
1274 // There is however no equivalent for XPutBackEvent so register a
1275 // callback for QXcbConnection instead.
1276 connection()->addPeekFunc(focusInPeeker);
1279 void QXcbWindow::updateSyncRequestCounter()
1281 if (m_screen->syncRequestSupported() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
1282 Q_XCB_CALL(xcb_sync_set_counter(xcb_connection(), m_syncCounter, m_syncValue));
1283 xcb_flush(xcb_connection());
1284 connection()->sync();
1291 bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1294 xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1297 xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
1298 m_window, XCB_TIME_CURRENT_TIME,
1299 GrabModeAsync, GrabModeAsync);
1300 xcb_generic_error_t *err;
1301 xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, &err);
1302 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1308 bool QXcbWindow::setMouseGrabEnabled(bool grab)
1311 xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1314 xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
1315 (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
1316 | EnterWindowMask | LeaveWindowMask | PointerMotionMask),
1317 GrabModeAsync, GrabModeAsync,
1318 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
1319 XCB_TIME_CURRENT_TIME);
1320 xcb_generic_error_t *err;
1321 xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, &err);
1322 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1328 void QXcbWindow::setCursor(xcb_cursor_t cursor)
1330 xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1331 xcb_flush(xcb_connection());