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()
125 bool wasCreated = (m_window != 0);
128 m_windowState = Qt::WindowNoState;
129 m_dirtyFrameMargins = true;
131 Qt::WindowType type = window()->windowType();
133 if (type == Qt::Desktop) {
134 m_window = m_screen->root();
135 m_depth = m_screen->screen()->root_depth;
136 m_imageFormat = (m_depth == 32) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
137 connection()->addWindow(m_window, this);
141 const quint32 mask = XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK;
142 const quint32 values[] = {
143 // XCB_CW_BACK_PIXMAP
145 // XCB_CW_OVERRIDE_REDIRECT
146 type == Qt::Popup || type == Qt::ToolTip,
148 type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
150 XCB_EVENT_MASK_EXPOSURE
151 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
152 | XCB_EVENT_MASK_KEY_PRESS
153 | XCB_EVENT_MASK_KEY_RELEASE
154 | XCB_EVENT_MASK_BUTTON_PRESS
155 | XCB_EVENT_MASK_BUTTON_RELEASE
156 | XCB_EVENT_MASK_BUTTON_MOTION
157 | XCB_EVENT_MASK_ENTER_WINDOW
158 | XCB_EVENT_MASK_LEAVE_WINDOW
159 | XCB_EVENT_MASK_POINTER_MOTION
160 | XCB_EVENT_MASK_PROPERTY_CHANGE
161 | XCB_EVENT_MASK_FOCUS_CHANGE
164 QRect rect = window()->geometry();
165 QPlatformWindow::setGeometry(rect);
167 rect.setWidth(qBound(1, rect.width(), XCOORD_MAX));
168 rect.setHeight(qBound(1, rect.height(), XCOORD_MAX));
170 xcb_window_t xcb_parent_id = m_screen->root();
172 xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
174 m_requestedFormat = window()->format();
176 #if defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
177 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL)
178 || window()->format().hasAlpha())
180 #if defined(XCB_USE_GLX)
181 XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(m_screen),m_screen->screenNumber(), window()->format());
183 #elif defined(XCB_USE_EGL)
184 EGLDisplay eglDisplay = connection()->egl_display();
185 EGLConfig eglConfig = q_configFromGLFormat(eglDisplay, window()->format(), true);
186 VisualID id = QXlibEglIntegration::getCompatibleVisualId(DISPLAY_FROM_XCB(this), eglDisplay, eglConfig);
188 XVisualInfo visualInfoTemplate;
189 memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
190 visualInfoTemplate.visualid = id;
192 XVisualInfo *visualInfo;
193 int matchingCount = 0;
194 visualInfo = XGetVisualInfo(DISPLAY_FROM_XCB(this), VisualIDMask, &visualInfoTemplate, &matchingCount);
197 m_depth = visualInfo->depth;
198 m_imageFormat = (m_depth == 32) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
199 Colormap cmap = XCreateColormap(DISPLAY_FROM_XCB(this), xcb_parent_id, visualInfo->visual, AllocNone);
201 XSetWindowAttributes a;
202 a.background_pixel = WhitePixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
203 a.border_pixel = BlackPixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
205 m_window = XCreateWindow(DISPLAY_FROM_XCB(this), xcb_parent_id, rect.x(), rect.y(), rect.width(), rect.height(),
206 0, visualInfo->depth, InputOutput, visualInfo->visual,
207 CWBackPixel|CWBorderPixel|CWColormap, &a);
209 qFatal("no window!");
212 #endif //defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
214 m_window = xcb_generate_id(xcb_connection());
215 m_depth = m_screen->screen()->root_depth;
216 m_imageFormat = (m_depth == 32) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
218 Q_XCB_CALL(xcb_create_window(xcb_connection(),
219 XCB_COPY_FROM_PARENT, // depth -- same as root
220 m_window, // window id
221 xcb_parent_id, // parent window id
227 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
228 m_screen->screen()->root_visual, // visual
233 connection()->addWindow(m_window, this);
235 Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
237 xcb_atom_t properties[4];
238 int propertyCount = 0;
239 properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
240 properties[propertyCount++] = atom(QXcbAtom::WM_TAKE_FOCUS);
241 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_PING);
243 if (m_screen->syncRequestSupported())
244 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
246 if (window()->windowFlags() & Qt::WindowContextHelpButtonHint)
247 properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
249 Q_XCB_CALL(xcb_change_property(xcb_connection(),
250 XCB_PROP_MODE_REPLACE,
252 atom(QXcbAtom::WM_PROTOCOLS),
260 if (m_screen->syncRequestSupported()) {
261 m_syncCounter = xcb_generate_id(xcb_connection());
262 Q_XCB_CALL(xcb_sync_create_counter(xcb_connection(), m_syncCounter, m_syncValue));
264 Q_XCB_CALL(xcb_change_property(xcb_connection(),
265 XCB_PROP_MODE_REPLACE,
267 atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
274 // set the PID to let the WM kill the application if unresponsive
276 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
277 atom(QXcbAtom::_NET_WM_PID), XCB_ATOM_CARDINAL, 32,
280 xcb_wm_hints_t hints;
281 memset(&hints, 0, sizeof(hints));
282 xcb_wm_hints_set_normal(&hints);
284 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
286 xcb_window_t leader = m_screen->clientLeader();
287 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
288 atom(QXcbAtom::WM_CLIENT_LEADER), XCB_ATOM_WINDOW, 32,
291 setWindowFlags(window()->windowFlags());
292 setWindowTitle(window()->windowTitle());
293 setWindowState(window()->windowState());
295 connection()->drag()->dndEnable(this, true);
298 QXcbWindow::~QXcbWindow()
303 void QXcbWindow::destroy()
305 if (m_syncCounter && m_screen->syncRequestSupported())
306 Q_XCB_CALL(xcb_sync_destroy_counter(xcb_connection(), m_syncCounter));
308 connection()->removeWindow(m_window);
309 Q_XCB_CALL(xcb_destroy_window(xcb_connection(), m_window));
313 #if defined(XCB_USE_EGL)
319 void QXcbWindow::setGeometry(const QRect &rect)
321 QPlatformWindow::setGeometry(rect);
323 const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
324 const quint32 values[] = { rect.x(),
326 qBound(1, rect.width(), XCOORD_MAX),
327 qBound(1, rect.height(), XCOORD_MAX) };
329 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
332 QMargins QXcbWindow::frameMargins() const
334 if (m_dirtyFrameMargins) {
335 xcb_window_t window = m_window;
336 xcb_window_t parent = m_window;
338 bool foundRoot = false;
340 const QVector<xcb_window_t> &virtualRoots =
341 connection()->wmSupport()->virtualRoots();
344 xcb_query_tree_cookie_t cookie = xcb_query_tree(xcb_connection(), parent);
346 xcb_generic_error_t *error;
347 xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, &error);
349 if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1) {
353 parent = reply->parent;
359 connection()->handleXcbError(error);
363 m_dirtyFrameMargins = false;
364 m_frameMargins = QMargins();
365 return m_frameMargins;
371 xcb_generic_error_t *error;
372 xcb_translate_coordinates_reply_t *reply =
373 xcb_translate_coordinates_reply(
375 xcb_translate_coordinates(xcb_connection(), window, parent, 0, 0),
379 offset = QPoint(reply->dst_x, reply->dst_y);
385 xcb_get_geometry_reply_t *geom =
386 xcb_get_geometry_reply(
388 xcb_get_geometry(xcb_connection(), parent),
393 // add the border_width for the window managers frame... some window managers
394 // do not use a border_width of zero for their frames, and if we the left and
395 // top strut, we ensure that pos() is absolutely correct. frameGeometry()
396 // will still be incorrect though... perhaps i should have foffset as well, to
397 // indicate the frame offset (equal to the border_width on X).
399 // -- copied from qwidget_x11.cpp
401 int left = offset.x() + geom->border_width;
402 int top = offset.y() + geom->border_width;
403 int right = geom->width + geom->border_width - geometry().width() - offset.x();
404 int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
406 m_frameMargins = QMargins(left, top, right, bottom);
413 m_dirtyFrameMargins = false;
416 return m_frameMargins;
419 void QXcbWindow::setVisible(bool visible)
427 void QXcbWindow::show()
429 if (window()->isTopLevel()) {
430 xcb_get_property_cookie_t cookie = xcb_get_wm_hints(xcb_connection(), m_window);
432 xcb_generic_error_t *error;
434 xcb_wm_hints_t hints;
435 xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, &error);
438 connection()->handleXcbError(error);
442 m_dirtyFrameMargins = true;
444 if (window()->windowState() & Qt::WindowMinimized)
445 xcb_wm_hints_set_iconic(&hints);
447 xcb_wm_hints_set_normal(&hints);
449 xcb_set_wm_hints(xcb_connection(), m_window, &hints);
451 // update WM_NORMAL_HINTS
452 propagateSizeHints();
454 // update WM_TRANSIENT_FOR
455 if (window()->transientParent() && isTransient(window())) {
456 QXcbWindow *transientXcbParent = static_cast<QXcbWindow *>(window()->transientParent()->handle());
457 if (transientXcbParent) {
459 xcb_window_t parentWindow = transientXcbParent->xcb_window();
461 // todo: set transient for group (wm_client_leader) if no parent, a la qwidget_x11.cpp
462 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
463 XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 32,
468 // update _MOTIF_WM_HINTS
469 updateMotifWmHintsBeforeMap();
471 // update _NET_WM_STATE
472 updateNetWmStateBeforeMap();
475 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
476 xcb_flush(xcb_connection());
478 connection()->sync();
481 void QXcbWindow::hide()
483 Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
485 // send synthetic UnmapNotify event according to icccm 4.1.4
486 xcb_unmap_notify_event_t event;
487 event.response_type = XCB_UNMAP_NOTIFY;
488 event.event = m_screen->root();
489 event.window = m_window;
490 event.from_configure = false;
491 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, m_screen->root(),
492 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
494 xcb_flush(xcb_connection());
499 struct QtMotifWmHints {
500 quint32 flags, functions, decorations;
506 MWM_HINTS_FUNCTIONS = (1L << 0),
508 MWM_FUNC_ALL = (1L << 0),
509 MWM_FUNC_RESIZE = (1L << 1),
510 MWM_FUNC_MOVE = (1L << 2),
511 MWM_FUNC_MINIMIZE = (1L << 3),
512 MWM_FUNC_MAXIMIZE = (1L << 4),
513 MWM_FUNC_CLOSE = (1L << 5),
515 MWM_HINTS_DECORATIONS = (1L << 1),
517 MWM_DECOR_ALL = (1L << 0),
518 MWM_DECOR_BORDER = (1L << 1),
519 MWM_DECOR_RESIZEH = (1L << 2),
520 MWM_DECOR_TITLE = (1L << 3),
521 MWM_DECOR_MENU = (1L << 4),
522 MWM_DECOR_MINIMIZE = (1L << 5),
523 MWM_DECOR_MAXIMIZE = (1L << 6),
525 MWM_HINTS_INPUT_MODE = (1L << 2),
527 MWM_INPUT_MODELESS = 0L,
528 MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
529 MWM_INPUT_FULL_APPLICATION_MODAL = 3L
532 static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
534 QtMotifWmHints hints;
536 xcb_get_property_cookie_t get_cookie =
537 xcb_get_property(c->xcb_connection(), 0, window, c->atom(QXcbAtom::_MOTIF_WM_HINTS),
538 c->atom(QXcbAtom::_MOTIF_WM_HINTS), 0, 20);
540 xcb_generic_error_t *error;
542 xcb_get_property_reply_t *reply =
543 xcb_get_property_reply(c->xcb_connection(), get_cookie, &error);
545 if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
546 hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
548 c->handleXcbError(error);
552 hints.functions = MWM_FUNC_ALL;
553 hints.decorations = MWM_DECOR_ALL;
554 hints.input_mode = 0L;
563 static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
565 if (hints.flags != 0l) {
566 Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
567 XCB_PROP_MODE_REPLACE,
569 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
570 c->atom(QXcbAtom::_MOTIF_WM_HINTS),
575 Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
579 void QXcbWindow::printNetWmState(const QVector<xcb_atom_t> &state)
581 printf("_NET_WM_STATE (%d): ", state.size());
582 for (int i = 0; i < state.size(); ++i) {
583 #define CHECK_WM_STATE(state_atom) \
584 if (state.at(i) == atom(QXcbAtom::state_atom))\
585 printf(#state_atom " ");
586 CHECK_WM_STATE(_NET_WM_STATE_ABOVE)
587 CHECK_WM_STATE(_NET_WM_STATE_BELOW)
588 CHECK_WM_STATE(_NET_WM_STATE_FULLSCREEN)
589 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_HORZ)
590 CHECK_WM_STATE(_NET_WM_STATE_MAXIMIZED_VERT)
591 CHECK_WM_STATE(_NET_WM_STATE_MODAL)
592 CHECK_WM_STATE(_NET_WM_STATE_STAYS_ON_TOP)
593 CHECK_WM_STATE(_NET_WM_STATE_DEMANDS_ATTENTION)
594 #undef CHECK_WM_STATE
599 QVector<xcb_atom_t> QXcbWindow::getNetWmState()
601 QVector<xcb_atom_t> result;
603 xcb_get_property_cookie_t get_cookie =
604 xcb_get_property(xcb_connection(), 0, m_window, atom(QXcbAtom::_NET_WM_STATE),
605 XCB_ATOM_ATOM, 0, 1024);
607 xcb_generic_error_t *error;
609 xcb_get_property_reply_t *reply =
610 xcb_get_property_reply(xcb_connection(), get_cookie, &error);
612 if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
613 result.resize(reply->length);
615 memcpy(result.data(), xcb_get_property_value(reply), reply->length * sizeof(xcb_atom_t));
617 #ifdef NET_WM_STATE_DEBUG
618 printf("getting net wm state (%x)\n", m_window);
619 printNetWmState(result);
624 connection()->handleXcbError(error);
627 #ifdef NET_WM_STATE_DEBUG
628 printf("getting net wm state (%x), empty\n", m_window);
635 void QXcbWindow::setNetWmState(const QVector<xcb_atom_t> &atoms)
637 if (atoms.isEmpty()) {
638 Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
640 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
641 atom(QXcbAtom::_NET_WM_STATE), XCB_ATOM_ATOM, 32,
642 atoms.count(), atoms.constData()));
644 xcb_flush(xcb_connection());
648 Qt::WindowFlags QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
650 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
652 if (type == Qt::ToolTip)
653 flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
654 if (type == Qt::Popup)
655 flags |= Qt::X11BypassWindowManagerHint;
657 setNetWmWindowFlags(flags);
658 setMotifWindowFlags(flags);
663 void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
665 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
667 QtMotifWmHints mwmhints;
669 mwmhints.functions = 0L;
670 mwmhints.decorations = 0;
671 mwmhints.input_mode = 0L;
672 mwmhints.status = 0L;
674 if (type != Qt::SplashScreen) {
675 mwmhints.flags |= MWM_HINTS_DECORATIONS;
677 bool customize = flags & Qt::CustomizeWindowHint;
678 if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
679 mwmhints.decorations |= MWM_DECOR_BORDER;
680 mwmhints.decorations |= MWM_DECOR_RESIZEH;
682 if (flags & Qt::WindowTitleHint)
683 mwmhints.decorations |= MWM_DECOR_TITLE;
685 if (flags & Qt::WindowSystemMenuHint)
686 mwmhints.decorations |= MWM_DECOR_MENU;
688 if (flags & Qt::WindowMinimizeButtonHint) {
689 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
690 mwmhints.functions |= MWM_FUNC_MINIMIZE;
693 if (flags & Qt::WindowMaximizeButtonHint) {
694 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
695 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
698 if (flags & Qt::WindowCloseButtonHint)
699 mwmhints.functions |= MWM_FUNC_CLOSE;
702 // if type == Qt::SplashScreen
703 mwmhints.decorations = MWM_DECOR_ALL;
706 if (mwmhints.functions != 0) {
707 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
708 mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
710 mwmhints.functions = MWM_FUNC_ALL;
713 if (!(flags & Qt::FramelessWindowHint)
714 && flags & Qt::CustomizeWindowHint
715 && flags & Qt::WindowTitleHint
717 (Qt::WindowMinimizeButtonHint
718 | Qt::WindowMaximizeButtonHint
719 | Qt::WindowCloseButtonHint)))
721 // a special case - only the titlebar without any button
722 mwmhints.flags = MWM_HINTS_FUNCTIONS;
723 mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
724 mwmhints.decorations = 0;
727 setMotifWmHints(connection(), m_window, mwmhints);
730 void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
732 xcb_client_message_event_t event;
734 event.response_type = XCB_CLIENT_MESSAGE;
736 event.window = m_window;
737 event.type = atom(QXcbAtom::_NET_WM_STATE);
738 event.data.data32[0] = set ? 1 : 0;
739 event.data.data32[1] = one;
740 event.data.data32[2] = two;
741 event.data.data32[3] = 0;
742 event.data.data32[4] = 0;
744 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));
747 Qt::WindowState QXcbWindow::setWindowState(Qt::WindowState state)
749 if (state == m_windowState)
752 m_dirtyFrameMargins = true;
755 switch (m_windowState) {
756 case Qt::WindowMinimized:
757 Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
759 case Qt::WindowMaximized:
760 changeNetWmState(false,
761 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
762 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
764 case Qt::WindowFullScreen:
765 changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
773 case Qt::WindowMinimized:
775 xcb_client_message_event_t event;
777 event.response_type = XCB_CLIENT_MESSAGE;
779 event.window = m_window;
780 event.type = atom(QXcbAtom::WM_CHANGE_STATE);
781 event.data.data32[0] = XCB_WM_STATE_ICONIC;
782 event.data.data32[1] = 0;
783 event.data.data32[2] = 0;
784 event.data.data32[3] = 0;
785 event.data.data32[4] = 0;
787 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));
790 case Qt::WindowMaximized:
791 changeNetWmState(true,
792 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
793 atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
795 case Qt::WindowFullScreen:
796 changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
798 case Qt::WindowNoState:
804 connection()->sync();
806 m_windowState = state;
807 return m_windowState;
810 void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
812 // in order of decreasing priority
813 QVector<uint> windowTypes;
815 Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
820 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
824 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
827 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
829 case Qt::SplashScreen:
830 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
836 if (flags & Qt::FramelessWindowHint)
837 windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
839 windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
841 Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
842 atom(QXcbAtom::_NET_WM_WINDOW_TYPE), XCB_ATOM_ATOM, 32,
843 windowTypes.count(), windowTypes.constData()));
846 void QXcbWindow::updateMotifWmHintsBeforeMap()
848 QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
850 if (window()->windowModality() != Qt::NonModal) {
851 switch (window()->windowModality()) {
852 case Qt::WindowModal:
853 mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
855 case Qt::ApplicationModal:
857 mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
860 mwmhints.flags |= MWM_HINTS_INPUT_MODE;
862 mwmhints.input_mode = MWM_INPUT_MODELESS;
863 mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
866 if (window()->minimumSize() == window()->maximumSize()) {
867 // fixed size, remove the resize handle (since mwm/dtwm
868 // isn't smart enough to do it itself)
869 mwmhints.flags |= MWM_HINTS_FUNCTIONS;
870 if (mwmhints.functions == MWM_FUNC_ALL) {
871 mwmhints.functions = MWM_FUNC_MOVE;
873 mwmhints.functions &= ~MWM_FUNC_RESIZE;
876 if (mwmhints.decorations == MWM_DECOR_ALL) {
877 mwmhints.flags |= MWM_HINTS_DECORATIONS;
878 mwmhints.decorations = (MWM_DECOR_BORDER
882 mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
886 if (window()->windowFlags() & Qt::WindowMinimizeButtonHint) {
887 mwmhints.flags |= MWM_HINTS_DECORATIONS;
888 mwmhints.decorations |= MWM_DECOR_MINIMIZE;
889 mwmhints.functions |= MWM_FUNC_MINIMIZE;
891 if (window()->windowFlags() & Qt::WindowMaximizeButtonHint) {
892 mwmhints.flags |= MWM_HINTS_DECORATIONS;
893 mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
894 mwmhints.functions |= MWM_FUNC_MAXIMIZE;
896 if (window()->windowFlags() & Qt::WindowCloseButtonHint)
897 mwmhints.functions |= MWM_FUNC_CLOSE;
899 setMotifWmHints(connection(), m_window, mwmhints);
902 void QXcbWindow::updateNetWmStateBeforeMap()
904 QVector<xcb_atom_t> netWmState;
906 Qt::WindowFlags flags = window()->windowFlags();
907 if (flags & Qt::WindowStaysOnTopHint) {
908 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_ABOVE));
909 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP));
910 } else if (flags & Qt::WindowStaysOnBottomHint) {
911 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_BELOW));
914 if (window()->windowState() & Qt::WindowFullScreen) {
915 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
918 if (window()->windowState() & Qt::WindowMaximized) {
919 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ));
920 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
923 if (window()->windowModality() != Qt::NonModal) {
924 netWmState.append(atom(QXcbAtom::_NET_WM_STATE_MODAL));
927 setNetWmState(netWmState);
930 void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
932 xcb_window_t wid = m_window;
934 const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
935 if (m_netWmUserTimeWindow || isSupportedByWM) {
936 if (!m_netWmUserTimeWindow) {
937 m_netWmUserTimeWindow = xcb_generate_id(xcb_connection());
938 Q_XCB_CALL(xcb_create_window(xcb_connection(),
939 XCB_COPY_FROM_PARENT, // depth -- same as root
940 m_netWmUserTimeWindow, // window id
941 m_window, // parent window id
944 XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
945 m_screen->screen()->root_visual, // visual
948 wid = m_netWmUserTimeWindow;
949 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW),
950 XCB_ATOM_WINDOW, 32, 1, &m_netWmUserTimeWindow);
951 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME));
952 } else if (!isSupportedByWM) {
953 // WM no longer supports it, then we should remove the
954 // _NET_WM_USER_TIME_WINDOW atom.
955 xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
956 xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
957 m_netWmUserTimeWindow = XCB_NONE;
959 wid = m_netWmUserTimeWindow;
962 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
963 XCB_ATOM_CARDINAL, 32, 1, ×tamp);
967 WId QXcbWindow::winId() const
972 void QXcbWindow::setParent(const QPlatformWindow *parent)
974 // re-create for compatibility
977 QPoint topLeft = geometry().topLeft();
979 xcb_window_t xcb_parent_id = parent ? static_cast<const QXcbWindow *>(parent)->xcb_window() : m_screen->root();
980 Q_XCB_CALL(xcb_reparent_window(xcb_connection(), xcb_window(), xcb_parent_id, topLeft.x(), topLeft.y()));
983 void QXcbWindow::setWindowTitle(const QString &title)
985 QByteArray ba = title.toUtf8();
986 Q_XCB_CALL(xcb_change_property(xcb_connection(),
987 XCB_PROP_MODE_REPLACE,
989 atom(QXcbAtom::_NET_WM_NAME),
990 atom(QXcbAtom::UTF8_STRING),
996 void QXcbWindow::raise()
998 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
999 const quint32 values[] = { XCB_STACK_MODE_ABOVE };
1000 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1003 void QXcbWindow::lower()
1005 const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1006 const quint32 values[] = { XCB_STACK_MODE_BELOW };
1007 Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1010 void QXcbWindow::propagateSizeHints()
1012 // update WM_NORMAL_HINTS
1013 xcb_size_hints_t hints;
1014 memset(&hints, 0, sizeof(hints));
1016 QRect rect = geometry();
1018 xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
1019 xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
1020 xcb_size_hints_set_win_gravity(&hints, XCB_GRAVITY_STATIC);
1022 QWindow *win = window();
1024 QSize minimumSize = win->minimumSize();
1025 QSize maximumSize = win->maximumSize();
1026 QSize baseSize = win->baseSize();
1027 QSize sizeIncrement = win->sizeIncrement();
1029 if (minimumSize.width() > 0 || minimumSize.height() > 0)
1030 xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1032 if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1033 xcb_size_hints_set_max_size(&hints,
1034 qMin(XCOORD_MAX, maximumSize.width()),
1035 qMin(XCOORD_MAX, maximumSize.height()));
1037 if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1038 xcb_size_hints_set_base_size(&hints, baseSize.width(), baseSize.height());
1039 xcb_size_hints_set_resize_inc(&hints, sizeIncrement.width(), sizeIncrement.height());
1042 xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1045 void QXcbWindow::requestActivateWindow()
1048 updateNetWmUserTime(connection()->time());
1049 Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1051 connection()->sync();
1054 QSurfaceFormat QXcbWindow::format() const
1056 // ### return actual format
1057 return m_requestedFormat;
1060 #if defined(XCB_USE_EGL)
1061 QXcbEGLSurface *QXcbWindow::eglSurface() const
1063 if (!m_eglSurface) {
1064 EGLDisplay display = connection()->egl_display();
1065 EGLConfig config = q_configFromGLFormat(display, window()->format(), true);
1066 EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)m_window, 0);
1068 m_eglSurface = new QXcbEGLSurface(display, surface);
1071 return m_eglSurface;
1075 void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1077 QRect rect(event->x, event->y, event->width, event->height);
1078 QWindowSystemInterface::handleExposeEvent(window(), rect);
1081 void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1083 if (event->format != 32)
1086 if (event->type == atom(QXcbAtom::WM_PROTOCOLS)) {
1087 if (event->data.data32[0] == atom(QXcbAtom::WM_DELETE_WINDOW)) {
1088 QWindowSystemInterface::handleCloseEvent(window());
1089 } else if (event->data.data32[0] == atom(QXcbAtom::WM_TAKE_FOCUS)) {
1090 connection()->setTime(event->data.data32[1]);
1091 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_PING)) {
1092 xcb_client_message_event_t reply = *event;
1094 reply.response_type = XCB_CLIENT_MESSAGE;
1095 reply.window = m_screen->root();
1097 xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&reply);
1098 xcb_flush(xcb_connection());
1099 } else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_SYNC_REQUEST)) {
1100 connection()->setTime(event->data.data32[1]);
1101 m_syncValue.lo = event->data.data32[2];
1102 m_syncValue.hi = event->data.data32[3];
1104 qWarning() << "unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1106 } else if (event->type == atom(QXcbAtom::XdndEnter)) {
1107 connection()->drag()->handleEnter(window(), event);
1108 } else if (event->type == atom(QXcbAtom::XdndPosition)) {
1109 connection()->drag()->handlePosition(window(), event, false);
1110 } else if (event->type == atom(QXcbAtom::XdndLeave)) {
1111 connection()->drag()->handleLeave(window(), event, false);
1112 } else if (event->type == atom(QXcbAtom::XdndDrop)) {
1113 connection()->drag()->handleDrop(window(), event, false);
1115 qWarning() << "unhandled client message:" << connection()->atomName(event->type);
1119 void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1121 int xpos = geometry().x();
1122 int ypos = geometry().y();
1124 if ((event->width == geometry().width() && event->height == geometry().height()) || event->x != 0 || event->y != 0) {
1129 QRect rect(xpos, ypos, event->width, event->height);
1131 if (rect == geometry())
1134 QPlatformWindow::setGeometry(rect);
1135 QWindowSystemInterface::handleGeometryChange(window(), rect);
1139 static_cast<QDri2Context *>(m_context)->resize(rect.size());
1143 void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1145 if (event->window == m_window) {
1147 QWindowSystemInterface::handleMapEvent(window());
1151 void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1153 if (event->window == m_window) {
1155 QWindowSystemInterface::handleUnmapEvent(window());
1159 static Qt::MouseButtons translateMouseButtons(int s)
1161 Qt::MouseButtons ret = 0;
1162 if (s & XCB_BUTTON_MASK_1)
1163 ret |= Qt::LeftButton;
1164 if (s & XCB_BUTTON_MASK_2)
1165 ret |= Qt::MidButton;
1166 if (s & XCB_BUTTON_MASK_3)
1167 ret |= Qt::RightButton;
1171 static Qt::MouseButton translateMouseButton(xcb_button_t s)
1175 return Qt::LeftButton;
1177 return Qt::MidButton;
1179 return Qt::RightButton;
1181 return Qt::NoButton;
1185 void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1187 updateNetWmUserTime(event->time);
1189 QPoint local(event->event_x, event->event_y);
1190 QPoint global(event->root_x, event->root_y);
1192 Qt::KeyboardModifiers modifiers = Qt::NoModifier;
1194 if (event->detail >= 4 && event->detail <= 7) {
1195 //logic borrowed from qapplication_x11.cpp
1196 int delta = 120 * ((event->detail == 4 || event->detail == 6) ? 1 : -1);
1197 bool hor = (((event->detail == 4 || event->detail == 5)
1198 && (modifiers & Qt::AltModifier))
1199 || (event->detail == 6 || event->detail == 7));
1201 QWindowSystemInterface::handleWheelEvent(window(), event->time,
1202 local, global, delta, hor ? Qt::Horizontal : Qt::Vertical);
1206 handleMouseEvent(event->detail, event->state, event->time, local, global);
1209 void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1211 QPoint local(event->event_x, event->event_y);
1212 QPoint global(event->root_x, event->root_y);
1214 handleMouseEvent(event->detail, event->state, event->time, local, global);
1217 void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
1219 QPoint local(event->event_x, event->event_y);
1220 QPoint global(event->root_x, event->root_y);
1222 handleMouseEvent(event->detail, event->state, event->time, local, global);
1225 void QXcbWindow::handleMouseEvent(xcb_button_t detail, uint16_t state, xcb_timestamp_t time, const QPoint &local, const QPoint &global)
1227 connection()->setTime(time);
1229 Qt::MouseButtons buttons = translateMouseButtons(state);
1230 Qt::MouseButton button = translateMouseButton(detail);
1232 buttons ^= button; // X event uses state *before*, Qt uses state *after*
1234 QWindowSystemInterface::handleMouseEvent(window(), time, local, global, buttons);
1237 void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1239 connection()->setTime(event->time);
1241 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1242 || event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1243 || event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1248 QWindowSystemInterface::handleEnterEvent(window());
1251 void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1253 connection()->setTime(event->time);
1255 if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1256 || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)
1261 QWindowSystemInterface::handleLeaveEvent(window());
1264 void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1266 QWindowSystemInterface::handleWindowActivated(window());
1269 static bool focusInPeeker(xcb_generic_event_t *event)
1272 // FocusIn event is not in the queue, proceed with FocusOut normally.
1273 QWindowSystemInterface::handleWindowActivated(0);
1276 uint response_type = event->response_type & ~0x80;
1277 return response_type == XCB_FOCUS_IN;
1280 void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1282 // Do not set the active window to 0 if there is a FocusIn coming.
1283 // There is however no equivalent for XPutBackEvent so register a
1284 // callback for QXcbConnection instead.
1285 connection()->addPeekFunc(focusInPeeker);
1288 void QXcbWindow::updateSyncRequestCounter()
1290 if (m_screen->syncRequestSupported() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
1291 Q_XCB_CALL(xcb_sync_set_counter(xcb_connection(), m_syncCounter, m_syncValue));
1292 xcb_flush(xcb_connection());
1293 connection()->sync();
1300 bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1303 xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1306 xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
1307 m_window, XCB_TIME_CURRENT_TIME,
1308 GrabModeAsync, GrabModeAsync);
1309 xcb_generic_error_t *err;
1310 xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, &err);
1311 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1317 bool QXcbWindow::setMouseGrabEnabled(bool grab)
1320 xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1323 xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
1324 (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
1325 | EnterWindowMask | LeaveWindowMask | PointerMotionMask),
1326 GrabModeAsync, GrabModeAsync,
1327 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
1328 XCB_TIME_CURRENT_TIME);
1329 xcb_generic_error_t *err;
1330 xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, &err);
1331 bool result = !(err || !reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1337 void QXcbWindow::setCursor(xcb_cursor_t cursor)
1339 xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1340 xcb_flush(xcb_connection());