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 #define class class_name // Yeah, in 2011 ...
57 #include <xcb/xcb_icccm.h>
60 // xcb-icccm 3.8 support
61 #ifdef XCB_ICCCM_NUM_WM_SIZE_HINTS_ELEMENTS
62 #define xcb_wm_hints_t xcb_icccm_wm_hints_t
63 #define xcb_wm_hints_set_iconic xcb_icccm_wm_hints_set_iconic
64 #define xcb_wm_hints_set_normal xcb_icccm_wm_hints_set_normal
65 #define xcb_set_wm_hints xcb_icccm_set_wm_hints
68 #include <private/qguiapplication_p.h>
69 #include <private/qwindow_p.h>
71 #include <QtGui/QPlatformBackingStore>
72 #include <QtGui/QWindowSystemInterface>
78 #include <X11/Xutil.h>
81 #if defined(XCB_USE_GLX)
82 #include "qglxintegration.h"
83 #include <QtPlatformSupport/private/qglxconvenience_p.h>
84 #elif defined(XCB_USE_EGL)
85 #include "qxcbeglsurface.h"
86 #include <QtPlatformSupport/private/qeglconvenience_p.h>
87 #include <QtPlatformSupport/private/qxlibeglintegration_p.h>
90 #define XCOORD_MAX 16383
92 //#ifdef NET_WM_STATE_DEBUG
94 // Returns true if we should set WM_TRANSIENT_FOR on \a w
95 static inline bool isTransient(const QWindow *w)
97 return w->windowType() == Qt::Dialog
98 || w->windowType() == Qt::Sheet
99 || w->windowType() == Qt::Tool
100 || w->windowType() == Qt::SplashScreen
101 || w->windowType() == Qt::ToolTip
102 || w->windowType() == Qt::Drawer
103 || w->windowType() == Qt::Popup;
106 QXcbWindow::QXcbWindow(QWindow *window)
107 : QPlatformWindow(window)
111 , m_netWmUserTimeWindow(XCB_NONE)
112 #if defined(XCB_USE_EGL)
116 m_screen = static_cast<QXcbScreen *>(window->screen()->handle());
118 setConnection(m_screen->connection());
123 void QXcbWindow::create()
127 m_windowState = Qt::WindowNoState;
128 m_dirtyFrameMargins = true;
130 Qt::WindowType type = window()->windowType();
132 if (type == Qt::Desktop) {
133 m_window = m_screen->root();
134 m_depth = m_screen->screen()->root_depth;
135 m_imageFormat = (m_depth == 32) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
136 connection()->addWindow(m_window, this);
140 const quint32 mask = XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK;
141 const quint32 values[] = {
142 // XCB_CW_BACK_PIXMAP
144 // XCB_CW_OVERRIDE_REDIRECT
145 type == Qt::Popup || type == Qt::ToolTip,
147 type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
149 XCB_EVENT_MASK_EXPOSURE
150 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
151 | XCB_EVENT_MASK_KEY_PRESS
152 | XCB_EVENT_MASK_KEY_RELEASE
153 | XCB_EVENT_MASK_BUTTON_PRESS
154 | XCB_EVENT_MASK_BUTTON_RELEASE
155 | XCB_EVENT_MASK_BUTTON_MOTION
156 | XCB_EVENT_MASK_ENTER_WINDOW
157 | XCB_EVENT_MASK_LEAVE_WINDOW
158 | XCB_EVENT_MASK_POINTER_MOTION
159 | XCB_EVENT_MASK_PROPERTY_CHANGE
160 | XCB_EVENT_MASK_FOCUS_CHANGE
163 QRect rect = window()->geometry();
164 QPlatformWindow::setGeometry(rect);
166 rect.setWidth(qBound(1, rect.width(), XCOORD_MAX));
167 rect.setHeight(qBound(1, rect.height(), XCOORD_MAX));
169 xcb_window_t xcb_parent_id = m_screen->root();
171 xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
173 m_requestedFormat = window()->format();
175 #if (defined(XCB_USE_GLX) || defined(XCB_USE_EGL)) && defined(XCB_USE_XLIB)
176 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL)
177 || window()->format().hasAlpha())
179 #if defined(XCB_USE_GLX)
180 XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(m_screen),m_screen->screenNumber(), window()->format());
182 #elif defined(XCB_USE_EGL)
183 EGLDisplay eglDisplay = connection()->egl_display();
184 EGLConfig eglConfig = q_configFromGLFormat(eglDisplay, window()->format(), true);
185 VisualID id = QXlibEglIntegration::getCompatibleVisualId(DISPLAY_FROM_XCB(this), eglDisplay, eglConfig);
187 XVisualInfo visualInfoTemplate;
188 memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
189 visualInfoTemplate.visualid = id;
191 XVisualInfo *visualInfo;
192 int matchingCount = 0;
193 visualInfo = XGetVisualInfo(DISPLAY_FROM_XCB(this), VisualIDMask, &visualInfoTemplate, &matchingCount);
196 m_depth = visualInfo->depth;
197 m_imageFormat = (m_depth == 32) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
198 Colormap cmap = XCreateColormap(DISPLAY_FROM_XCB(this), xcb_parent_id, visualInfo->visual, AllocNone);
200 XSetWindowAttributes a;
201 a.background_pixel = WhitePixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
202 a.border_pixel = BlackPixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
204 m_window = XCreateWindow(DISPLAY_FROM_XCB(this), xcb_parent_id, rect.x(), rect.y(), rect.width(), rect.height(),
205 0, visualInfo->depth, InputOutput, visualInfo->visual,
206 CWBackPixel|CWBorderPixel|CWColormap, &a);
208 qFatal("no window!");
211 #endif //defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
213 m_window = xcb_generate_id(xcb_connection());
214 m_depth = m_screen->screen()->root_depth;
215 m_imageFormat = (m_depth == 32) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
217 Q_XCB_CALL(xcb_create_window(xcb_connection(),
218 XCB_COPY_FROM_PARENT, // depth -- same as root
219 m_window, // window id
220 xcb_parent_id, // parent window id
226 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
227 m_screen->screen()->root_visual, // visual
232 connection()->addWindow(m_window, this);
234 Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
236 xcb_atom_t properties[4];
237 int propertyCount = 0;
238 properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
239 properties[propertyCount++] = atom(QXcbAtom::WM_TAKE_FOCUS);
240 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_PING);
242 if (m_screen->syncRequestSupported())
243 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
245 if (window()->windowFlags() & Qt::WindowContextHelpButtonHint)
246 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
248 Q_XCB_CALL(xcb_change_property(xcb_connection(),
249 XCB_PROP_MODE_REPLACE,
251 atom(QXcbAtom::WM_PROTOCOLS),
259 if (m_screen->syncRequestSupported()) {
260 m_syncCounter = xcb_generate_id(xcb_connection());
261 Q_XCB_CALL(xcb_sync_create_counter(xcb_connection(), m_syncCounter, m_syncValue));
263 Q_XCB_CALL(xcb_change_property(xcb_connection(),
264 XCB_PROP_MODE_REPLACE,
266 atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
273 // set the PID to let the WM kill the application if unresponsive
275 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
276 atom(QXcbAtom::_NET_WM_PID), XCB_ATOM_CARDINAL, 32,
279 xcb_wm_hints_t hints;
280 memset(&hints, 0, sizeof(hints));
281 xcb_wm_hints_set_normal(&hints);
283 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
285 xcb_window_t leader = m_screen->clientLeader();
286 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
287 atom(QXcbAtom::WM_CLIENT_LEADER), XCB_ATOM_WINDOW, 32,
290 setWindowFlags(window()->windowFlags());
291 setWindowTitle(window()->windowTitle());
292 setWindowState(window()->windowState());
294 connection()->drag()->dndEnable(this, true);
297 QXcbWindow::~QXcbWindow()
302 void QXcbWindow::destroy()
304 if (m_syncCounter && m_screen->syncRequestSupported())
305 Q_XCB_CALL(xcb_sync_destroy_counter(xcb_connection(), m_syncCounter));
307 connection()->removeWindow(m_window);
308 Q_XCB_CALL(xcb_destroy_window(xcb_connection(), m_window));
312 #if defined(XCB_USE_EGL)
318 void QXcbWindow::setGeometry(const QRect &rect)
320 QPlatformWindow::setGeometry(rect);
322 const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
323 const quint32 values[] = { rect.x(),
325 qBound(1, rect.width(), XCOORD_MAX),
326 qBound(1, rect.height(), XCOORD_MAX) };
328 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
331 QMargins QXcbWindow::frameMargins() const
333 if (m_dirtyFrameMargins) {
334 xcb_window_t window = m_window;
335 xcb_window_t parent = m_window;
337 bool foundRoot = false;
339 const QVector<xcb_window_t> &virtualRoots =
340 connection()->wmSupport()->virtualRoots();
343 xcb_query_tree_cookie_t cookie = xcb_query_tree(xcb_connection(), parent);
345 xcb_generic_error_t *error;
346 xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, &error);
348 if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1) {
352 parent = reply->parent;
358 connection()->handleXcbError(error);
362 m_dirtyFrameMargins = false;
363 m_frameMargins = QMargins();
364 return m_frameMargins;
370 xcb_generic_error_t *error;
371 xcb_translate_coordinates_reply_t *reply =
372 xcb_translate_coordinates_reply(
374 xcb_translate_coordinates(xcb_connection(), window, parent, 0, 0),
378 offset = QPoint(reply->dst_x, reply->dst_y);
384 xcb_get_geometry_reply_t *geom =
385 xcb_get_geometry_reply(
387 xcb_get_geometry(xcb_connection(), parent),
392 // add the border_width for the window managers frame... some window managers
393 // do not use a border_width of zero for their frames, and if we the left and
394 // top strut, we ensure that pos() is absolutely correct. frameGeometry()
395 // will still be incorrect though... perhaps i should have foffset as well, to
396 // indicate the frame offset (equal to the border_width on X).
398 // -- copied from qwidget_x11.cpp
400 int left = offset.x() + geom->border_width;
401 int top = offset.y() + geom->border_width;
402 int right = geom->width + geom->border_width - geometry().width() - offset.x();
403 int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
405 m_frameMargins = QMargins(left, top, right, bottom);
412 m_dirtyFrameMargins = false;
415 return m_frameMargins;
418 void QXcbWindow::setVisible(bool visible)
426 void QXcbWindow::show()
428 if (window()->isTopLevel()) {
429 xcb_get_property_cookie_t cookie = xcb_get_wm_hints(xcb_connection(), m_window);
431 xcb_generic_error_t *error;
433 xcb_wm_hints_t hints;
434 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, &error);
437 connection()->handleXcbError(error);
441 m_dirtyFrameMargins = true;
443 if (window()->windowState() & Qt::WindowMinimized)
444 xcb_wm_hints_set_iconic(&hints);
446 xcb_wm_hints_set_normal(&hints);
448 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
450 // update WM_NORMAL_HINTS
451 propagateSizeHints();
453 // update WM_TRANSIENT_FOR
454 if (window()->transientParent() && isTransient(window())) {
455 QXcbWindow *transientXcbParent = static_cast<QXcbWindow *>(window()->transientParent()->handle());
456 if (transientXcbParent) {
458 xcb_window_t parentWindow = transientXcbParent->xcb_window();
460 // todo: set transient for group (wm_client_leader) if no parent, a la qwidget_x11.cpp
461 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
462 XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 32,
467 // update _MOTIF_WM_HINTS
468 updateMotifWmHintsBeforeMap();
470 // update _NET_WM_STATE
471 updateNetWmStateBeforeMap();
474 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
475 xcb_flush(xcb_connection());
477 connection()->sync();
480 void QXcbWindow::hide()
482 Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
484 // send synthetic UnmapNotify event according to icccm 4.1.4
485 xcb_unmap_notify_event_t event;
486 event.response_type = XCB_UNMAP_NOTIFY;
487 event.event = m_screen->root();
488 event.window = m_window;
489 event.from_configure = false;
490 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, m_screen->root(),
491 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
493 xcb_flush(xcb_connection());
498 struct QtMotifWmHints {
499 quint32 flags, functions, decorations;
505 MWM_HINTS_FUNCTIONS = (1L << 0),
507 MWM_FUNC_ALL = (1L << 0),
508 MWM_FUNC_RESIZE = (1L << 1),
509 MWM_FUNC_MOVE = (1L << 2),
510 MWM_FUNC_MINIMIZE = (1L << 3),
511 MWM_FUNC_MAXIMIZE = (1L << 4),
512 MWM_FUNC_CLOSE = (1L << 5),
514 MWM_HINTS_DECORATIONS = (1L << 1),
516 MWM_DECOR_ALL = (1L << 0),
517 MWM_DECOR_BORDER = (1L << 1),
518 MWM_DECOR_RESIZEH = (1L << 2),
519 MWM_DECOR_TITLE = (1L << 3),
520 MWM_DECOR_MENU = (1L << 4),
521 MWM_DECOR_MINIMIZE = (1L << 5),
522 MWM_DECOR_MAXIMIZE = (1L << 6),
524 MWM_HINTS_INPUT_MODE = (1L << 2),
526 MWM_INPUT_MODELESS = 0L,
527 MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
528 MWM_INPUT_FULL_APPLICATION_MODAL = 3L
531 static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
533 QtMotifWmHints hints;
535 xcb_get_property_cookie_t get_cookie =
536 xcb_get_property(c->xcb_connection(), 0, window, c->atom(QXcbAtom::_MOTIF_WM_HINTS),
537 c->atom(QXcbAtom::_MOTIF_WM_HINTS), 0, 20);
539 xcb_generic_error_t *error;
541 xcb_get_property_reply_t *reply =
542 xcb_get_property_reply(c->xcb_connection(), get_cookie, &error);
544 if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
545 hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
547 c->handleXcbError(error);
551 hints.functions = MWM_FUNC_ALL;
552 hints.decorations = MWM_DECOR_ALL;
553 hints.input_mode = 0L;
562 static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
564 if (hints.flags != 0l) {
565 Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
566 XCB_PROP_MODE_REPLACE,
568 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
569 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
574 Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
578 void QXcbWindow::printNetWmState(const QVector<xcb_atom_t> &state)
580 printf("_NET_WM_STATE (%d): ", state.size());
581 for (int i = 0; i < state.size(); ++i) {
582 #define CHECK_WM_STATE(state_atom) \
583 if (state.at(i) == atom(QXcbAtom::state_atom))\
584 printf(#state_atom " ");
585 CHECK_WM_STATE(_NET_WM_STATE_ABOVE)
586 CHECK_WM_STATE(_NET_WM_STATE_BELOW)
587 CHECK_WM_STATE(_NET_WM_STATE_FULLSCREEN)
588 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_HORZ)
589 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_VERT)
590 CHECK_WM_STATE(_NET_WM_STATE_MODAL)
591 CHECK_WM_STATE(_NET_WM_STATE_STAYS_ON_TOP)
592 CHECK_WM_STATE(_NET_WM_STATE_DEMANDS_ATTENTION)
593 #undef CHECK_WM_STATE
598 QVector<xcb_atom_t> QXcbWindow::getNetWmState()
600 QVector<xcb_atom_t> result;
602 xcb_get_property_cookie_t get_cookie =
603 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::_NET_WM_STATE),
604 XCB_ATOM_ATOM, 0, 1024);
606 xcb_generic_error_t *error;
608 xcb_get_property_reply_t *reply =
609 xcb_get_property_reply(xcb_connection(), get_cookie, &error);
611 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
612 result.resize(reply->length);
614 memcpy(result.data(), xcb_get_property_value(reply), reply->length * sizeof(xcb_atom_t));
616 #ifdef NET_WM_STATE_DEBUG
617 printf("getting net wm state (%x)\n", m_window);
618 printNetWmState(result);
623 connection()->handleXcbError(error);
626 #ifdef NET_WM_STATE_DEBUG
627 printf("getting net wm state (%x), empty\n", m_window);
634 void QXcbWindow::setNetWmState(const QVector<xcb_atom_t> &atoms)
636 if (atoms.isEmpty()) {
637 Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
639 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
640 atom(QXcbAtom::_NET_WM_STATE), XCB_ATOM_ATOM, 32,
641 atoms.count(), atoms.constData()));
643 xcb_flush(xcb_connection());
647 Qt::WindowFlags QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
649 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
651 if (type == Qt::ToolTip)
652 flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
653 if (type == Qt::Popup)
654 flags |= Qt::X11BypassWindowManagerHint;
656 setNetWmWindowFlags(flags);
657 setMotifWindowFlags(flags);
662 void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
664 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
666 QtMotifWmHints mwmhints;
668 mwmhints.functions = 0L;
669 mwmhints.decorations = 0;
670 mwmhints.input_mode = 0L;
671 mwmhints.status = 0L;
673 if (type != Qt::SplashScreen) {
674 mwmhints.flags |= MWM_HINTS_DECORATIONS;
676 bool customize = flags & Qt::CustomizeWindowHint;
677 if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
678 mwmhints.decorations |= MWM_DECOR_BORDER;
679 mwmhints.decorations |= MWM_DECOR_RESIZEH;
681 if (flags & Qt::WindowTitleHint)
682 mwmhints.decorations |= MWM_DECOR_TITLE;
684 if (flags & Qt::WindowSystemMenuHint)
685 mwmhints.decorations |= MWM_DECOR_MENU;
687 if (flags & Qt::WindowMinimizeButtonHint) {
688 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
689 mwmhints.functions |= MWM_FUNC_MINIMIZE;
692 if (flags & Qt::WindowMaximizeButtonHint) {
693 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
694 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
697 if (flags & Qt::WindowCloseButtonHint)
698 mwmhints.functions |= MWM_FUNC_CLOSE;
701 // if type == Qt::SplashScreen
702 mwmhints.decorations = MWM_DECOR_ALL;
705 if (mwmhints.functions != 0) {
706 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
707 mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
709 mwmhints.functions = MWM_FUNC_ALL;
712 if (!(flags & Qt::FramelessWindowHint)
713 && flags & Qt::CustomizeWindowHint
714 && flags & Qt::WindowTitleHint
716 (Qt::WindowMinimizeButtonHint
717 | Qt::WindowMaximizeButtonHint
718 | Qt::WindowCloseButtonHint)))
720 // a special case - only the titlebar without any button
721 mwmhints.flags = MWM_HINTS_FUNCTIONS;
722 mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
723 mwmhints.decorations = 0;
726 setMotifWmHints(connection(), m_window, mwmhints);
729 void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
731 xcb_client_message_event_t event;
733 event.response_type = XCB_CLIENT_MESSAGE;
735 event.window = m_window;
736 event.type = atom(QXcbAtom::_NET_WM_STATE);
737 event.data.data32[0] = set ? 1 : 0;
738 event.data.data32[1] = one;
739 event.data.data32[2] = two;
740 event.data.data32[3] = 0;
741 event.data.data32[4] = 0;
743 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));
746 Qt::WindowState QXcbWindow::setWindowState(Qt::WindowState state)
748 if (state == m_windowState)
751 m_dirtyFrameMargins = true;
754 switch (m_windowState) {
755 case Qt::WindowMinimized:
756 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
758 case Qt::WindowMaximized:
759 changeNetWmState(false,
760 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
761 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
763 case Qt::WindowFullScreen:
764 changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
772 case Qt::WindowMinimized:
774 xcb_client_message_event_t event;
776 event.response_type = XCB_CLIENT_MESSAGE;
778 event.window = m_window;
779 event.type = atom(QXcbAtom::WM_CHANGE_STATE);
780 event.data.data32[0] = XCB_WM_STATE_ICONIC;
781 event.data.data32[1] = 0;
782 event.data.data32[2] = 0;
783 event.data.data32[3] = 0;
784 event.data.data32[4] = 0;
786 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));
789 case Qt::WindowMaximized:
790 changeNetWmState(true,
791 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
792 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
794 case Qt::WindowFullScreen:
795 changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
797 case Qt::WindowNoState:
803 connection()->sync();
805 m_windowState = state;
806 return m_windowState;
809 void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
811 // in order of decreasing priority
812 QVector<uint> windowTypes;
814 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
819 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
823 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
826 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
828 case Qt::SplashScreen:
829 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
835 if (flags & Qt::FramelessWindowHint)
836 windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
838 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
840 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
841 atom(QXcbAtom::_NET_WM_WINDOW_TYPE), XCB_ATOM_ATOM, 32,
842 windowTypes.count(), windowTypes.constData()));
845 void QXcbWindow::updateMotifWmHintsBeforeMap()
847 QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
849 if (window()->windowModality() != Qt::NonModal) {
850 switch (window()->windowModality()) {
851 case Qt::WindowModal:
852 mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
854 case Qt::ApplicationModal:
856 mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
859 mwmhints.flags |= MWM_HINTS_INPUT_MODE;
861 mwmhints.input_mode = MWM_INPUT_MODELESS;
862 mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
865 if (window()->minimumSize() == window()->maximumSize()) {
866 // fixed size, remove the resize handle (since mwm/dtwm
867 // isn't smart enough to do it itself)
868 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
869 if (mwmhints.functions == MWM_FUNC_ALL) {
870 mwmhints.functions = MWM_FUNC_MOVE;
872 mwmhints.functions &= ~MWM_FUNC_RESIZE;
875 if (mwmhints.decorations == MWM_DECOR_ALL) {
876 mwmhints.flags |= MWM_HINTS_DECORATIONS;
877 mwmhints.decorations = (MWM_DECOR_BORDER
881 mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
885 if (window()->windowFlags() & Qt::WindowMinimizeButtonHint) {
886 mwmhints.flags |= MWM_HINTS_DECORATIONS;
887 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
888 mwmhints.functions |= MWM_FUNC_MINIMIZE;
890 if (window()->windowFlags() & Qt::WindowMaximizeButtonHint) {
891 mwmhints.flags |= MWM_HINTS_DECORATIONS;
892 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
893 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
895 if (window()->windowFlags() & Qt::WindowCloseButtonHint)
896 mwmhints.functions |= MWM_FUNC_CLOSE;
898 setMotifWmHints(connection(), m_window, mwmhints);
901 void QXcbWindow::updateNetWmStateBeforeMap()
903 QVector<xcb_atom_t> netWmState;
905 Qt::WindowFlags flags = window()->windowFlags();
906 if (flags & Qt::WindowStaysOnTopHint) {
907 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_ABOVE));
908 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP));
909 } else if (flags & Qt::WindowStaysOnBottomHint) {
910 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_BELOW));
913 if (window()->windowState() & Qt::WindowFullScreen) {
914 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
917 if (window()->windowState() & Qt::WindowMaximized) {
918 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ));
919 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
922 if (window()->windowModality() != Qt::NonModal) {
923 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MODAL));
926 setNetWmState(netWmState);
929 void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
931 xcb_window_t wid = m_window;
933 const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
934 if (m_netWmUserTimeWindow || isSupportedByWM) {
935 if (!m_netWmUserTimeWindow) {
936 m_netWmUserTimeWindow = xcb_generate_id(xcb_connection());
937 Q_XCB_CALL(xcb_create_window(xcb_connection(),
938 XCB_COPY_FROM_PARENT, // depth -- same as root
939 m_netWmUserTimeWindow, // window id
940 m_window, // parent window id
943 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
944 m_screen->screen()->root_visual, // visual
947 wid = m_netWmUserTimeWindow;
948 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW),
949 XCB_ATOM_WINDOW, 32, 1, &m_netWmUserTimeWindow);
950 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME));
951 } else if (!isSupportedByWM) {
952 // WM no longer supports it, then we should remove the
953 // _NET_WM_USER_TIME_WINDOW atom.
954 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
955 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
956 m_netWmUserTimeWindow = XCB_NONE;
958 wid = m_netWmUserTimeWindow;
961 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
962 XCB_ATOM_CARDINAL, 32, 1, ×tamp);
966 WId QXcbWindow::winId() const
971 void QXcbWindow::setParent(const QPlatformWindow *parent)
973 // re-create for compatibility
976 QPoint topLeft = geometry().topLeft();
978 xcb_window_t xcb_parent_id = parent ? static_cast<const QXcbWindow *>(parent)->xcb_window() : m_screen->root();
979 Q_XCB_CALL(xcb_reparent_window(xcb_connection(), xcb_window(), xcb_parent_id, topLeft.x(), topLeft.y()));
982 void QXcbWindow::setWindowTitle(const QString &title)
984 QByteArray ba = title.toUtf8();
985 Q_XCB_CALL(xcb_change_property(xcb_connection(),
986 XCB_PROP_MODE_REPLACE,
988 atom(QXcbAtom::_NET_WM_NAME),
989 atom(QXcbAtom::UTF8_STRING),
995 void QXcbWindow::raise()
997 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
998 const quint32 values[] = { XCB_STACK_MODE_ABOVE };
999 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1002 void QXcbWindow::lower()
1004 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1005 const quint32 values[] = { XCB_STACK_MODE_BELOW };
1006 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1009 void QXcbWindow::propagateSizeHints()
1011 // update WM_NORMAL_HINTS
1012 xcb_size_hints_t hints;
1013 memset(&hints, 0, sizeof(hints));
1015 QRect rect = geometry();
1017 xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
1018 xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
1019 xcb_size_hints_set_win_gravity(&hints, XCB_GRAVITY_STATIC);
1021 QWindow *win = window();
1023 QSize minimumSize = win->minimumSize();
1024 QSize maximumSize = win->maximumSize();
1025 QSize baseSize = win->baseSize();
1026 QSize sizeIncrement = win->sizeIncrement();
1028 if (minimumSize.width() > 0 || minimumSize.height() > 0)
1029 xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1031 if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1032 xcb_size_hints_set_max_size(&hints,
1033 qMin(XCOORD_MAX, maximumSize.width()),
1034 qMin(XCOORD_MAX, maximumSize.height()));
1036 if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1037 xcb_size_hints_set_base_size(&hints, baseSize.width(), baseSize.height());
1038 xcb_size_hints_set_resize_inc(&hints, sizeIncrement.width(), sizeIncrement.height());
1041 xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1044 void QXcbWindow::requestActivateWindow()
1047 updateNetWmUserTime(connection()->time());
1048 Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1050 connection()->sync();
1053 QSurfaceFormat QXcbWindow::format() const
1055 // ### return actual format
1056 return m_requestedFormat;
1059 #if defined(XCB_USE_EGL)
1060 QXcbEGLSurface *QXcbWindow::eglSurface() const
1062 if (!m_eglSurface) {
1063 EGLDisplay display = connection()->egl_display();
1064 EGLConfig config = q_configFromGLFormat(display, window()->format(), true);
1065 EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)m_window, 0);
1067 m_eglSurface = new QXcbEGLSurface(display, surface);
1070 return m_eglSurface;
1074 void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1076 QRect rect(event->x, event->y, event->width, event->height);
1077 QWindowSystemInterface::handleExposeEvent(window(), rect);
1080 void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1082 if (event->format != 32)
1085 if (event->type == atom(QXcbAtom::WM_PROTOCOLS)) {
1086 if (event->data.data32[0] == atom(QXcbAtom::WM_DELETE_WINDOW)) {
1087 QWindowSystemInterface::handleCloseEvent(window());
1088 } else if (event->data.data32[0] == atom(QXcbAtom::WM_TAKE_FOCUS)) {
1089 connection()->setTime(event->data.data32[1]);
1090 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_PING)) {
1091 xcb_client_message_event_t reply = *event;
1093 reply.response_type = XCB_CLIENT_MESSAGE;
1094 reply.window = m_screen->root();
1096 xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&reply);
1097 xcb_flush(xcb_connection());
1098 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_SYNC_REQUEST)) {
1099 connection()->setTime(event->data.data32[1]);
1100 m_syncValue.lo = event->data.data32[2];
1101 m_syncValue.hi = event->data.data32[3];
1103 qWarning() << "unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1105 } else if (event->type == atom(QXcbAtom::XdndEnter)) {
1106 connection()->drag()->handleEnter(window(), event);
1107 } else if (event->type == atom(QXcbAtom::XdndPosition)) {
1108 connection()->drag()->handlePosition(window(), event, false);
1109 } else if (event->type == atom(QXcbAtom::XdndLeave)) {
1110 connection()->drag()->handleLeave(window(), event, false);
1111 } else if (event->type == atom(QXcbAtom::XdndDrop)) {
1112 connection()->drag()->handleDrop(window(), event, false);
1114 qWarning() << "unhandled client message:" << connection()->atomName(event->type);
1118 void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1120 int xpos = geometry().x();
1121 int ypos = geometry().y();
1123 if ((event->width == geometry().width() && event->height == geometry().height()) || event->x != 0 || event->y != 0) {
1128 QRect rect(xpos, ypos, event->width, event->height);
1130 QPlatformWindow::setGeometry(rect);
1131 QWindowSystemInterface::handleGeometryChange(window(), rect);
1135 static_cast<QDri2Context *>(m_context)->resize(rect.size());
1139 void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1141 if (event->window == m_window) {
1143 QWindowSystemInterface::handleMapEvent(window());
1147 void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1149 if (event->window == m_window) {
1151 QWindowSystemInterface::handleUnmapEvent(window());
1155 static Qt::MouseButtons translateMouseButtons(int s)
1157 Qt::MouseButtons ret = 0;
1158 if (s & XCB_BUTTON_MASK_1)
1159 ret |= Qt::LeftButton;
1160 if (s & XCB_BUTTON_MASK_2)
1161 ret |= Qt::MidButton;
1162 if (s & XCB_BUTTON_MASK_3)
1163 ret |= Qt::RightButton;
1167 static Qt::MouseButton translateMouseButton(xcb_button_t s)
1171 return Qt::LeftButton;
1173 return Qt::MidButton;
1175 return Qt::RightButton;
1177 return Qt::NoButton;
1181 void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1183 updateNetWmUserTime(event->time);
1185 QPoint local(event->event_x, event->event_y);
1186 QPoint global(event->root_x, event->root_y);
1188 Qt::KeyboardModifiers modifiers = Qt::NoModifier;
1190 if (event->detail >= 4 && event->detail <= 7) {
1191 //logic borrowed from qapplication_x11.cpp
1192 int delta = 120 * ((event->detail == 4 || event->detail == 6) ? 1 : -1);
1193 bool hor = (((event->detail == 4 || event->detail == 5)
1194 && (modifiers & Qt::AltModifier))
1195 || (event->detail == 6 || event->detail == 7));
1197 QWindowSystemInterface::handleWheelEvent(window(), event->time,
1198 local, global, delta, hor ? Qt::Horizontal : Qt::Vertical);
1202 handleMouseEvent(event->detail, event->state, event->time, local, global);
1205 void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1207 QPoint local(event->event_x, event->event_y);
1208 QPoint global(event->root_x, event->root_y);
1210 handleMouseEvent(event->detail, event->state, event->time, local, global);
1213 void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
1215 QPoint local(event->event_x, event->event_y);
1216 QPoint global(event->root_x, event->root_y);
1218 handleMouseEvent(event->detail, event->state, event->time, local, global);
1221 void QXcbWindow::handleMouseEvent(xcb_button_t detail, uint16_t state, xcb_timestamp_t time, const QPoint &local, const QPoint &global)
1223 connection()->setTime(time);
1225 Qt::MouseButtons buttons = translateMouseButtons(state);
1226 Qt::MouseButton button = translateMouseButton(detail);
1228 buttons ^= button; // X event uses state *before*, Qt uses state *after*
1230 QWindowSystemInterface::handleMouseEvent(window(), time, local, global, buttons);
1233 void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1235 connection()->setTime(event->time);
1237 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1238 || event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1239 || event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1244 QWindowSystemInterface::handleEnterEvent(window());
1247 void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1249 connection()->setTime(event->time);
1251 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1252 || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)
1257 QWindowSystemInterface::handleLeaveEvent(window());
1260 void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1262 QWindowSystemInterface::handleWindowActivated(window());
1265 static bool focusInPeeker(xcb_generic_event_t *event)
1268 // FocusIn event is not in the queue, proceed with FocusOut normally.
1269 QWindowSystemInterface::handleWindowActivated(0);
1272 uint response_type = event->response_type & ~0x80;
1273 return response_type == XCB_FOCUS_IN;
1276 void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1278 // Do not set the active window to 0 if there is a FocusIn coming.
1279 // There is however no equivalent for XPutBackEvent so register a
1280 // callback for QXcbConnection instead.
1281 connection()->addPeekFunc(focusInPeeker);
1284 void QXcbWindow::updateSyncRequestCounter()
1286 if (m_screen->syncRequestSupported() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
1287 Q_XCB_CALL(xcb_sync_set_counter(xcb_connection(), m_syncCounter, m_syncValue));
1288 xcb_flush(xcb_connection());
1289 connection()->sync();
1296 bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1299 xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1302 xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
1303 m_window, XCB_TIME_CURRENT_TIME,
1304 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1305 xcb_generic_error_t *err;
1306 xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, &err);
1307 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1313 bool QXcbWindow::setMouseGrabEnabled(bool grab)
1316 xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1319 xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
1320 (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
1321 | XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW
1322 | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_POINTER_MOTION),
1323 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
1324 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
1325 XCB_TIME_CURRENT_TIME);
1326 xcb_generic_error_t *err;
1327 xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, &err);
1328 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1334 void QXcbWindow::setCursor(xcb_cursor_t cursor)
1336 xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1337 xcb_flush(xcb_connection());