1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the plugins of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include "qxcbwindow.h"
46
#include <QtGui/QIcon>
47
#include <QtGui/QRegion>
49
#include "qxcbconnection.h"
50
#include "qxcbscreen.h"
52
#include "qxcbkeyboard.h"
53
#include "qxcbwmsupport.h"
54
#include "qxcbimage.h"
56
#include <qpa/qplatformintegration.h>
58
// FIXME This workaround can be removed for xcb-icccm > 3.8
59
#define class class_name
60
#include <xcb/xcb_icccm.h>
62
#include <xcb/xfixes.h>
64
# include <xcb/shape.h>
67
// xcb-icccm 3.8 support
68
#ifdef XCB_ICCCM_NUM_WM_SIZE_HINTS_ELEMENTS
69
#define xcb_get_wm_hints_reply xcb_icccm_get_wm_hints_reply
70
#define xcb_get_wm_hints xcb_icccm_get_wm_hints
71
#define xcb_get_wm_hints_unchecked xcb_icccm_get_wm_hints_unchecked
72
#define xcb_set_wm_hints xcb_icccm_set_wm_hints
73
#define xcb_set_wm_normal_hints xcb_icccm_set_wm_normal_hints
74
#define xcb_size_hints_set_base_size xcb_icccm_size_hints_set_base_size
75
#define xcb_size_hints_set_max_size xcb_icccm_size_hints_set_max_size
76
#define xcb_size_hints_set_min_size xcb_icccm_size_hints_set_min_size
77
#define xcb_size_hints_set_position xcb_icccm_size_hints_set_position
78
#define xcb_size_hints_set_resize_inc xcb_icccm_size_hints_set_resize_inc
79
#define xcb_size_hints_set_size xcb_icccm_size_hints_set_size
80
#define xcb_size_hints_set_win_gravity xcb_icccm_size_hints_set_win_gravity
81
#define xcb_wm_hints_set_iconic xcb_icccm_wm_hints_set_iconic
82
#define xcb_wm_hints_set_normal xcb_icccm_wm_hints_set_normal
83
#define xcb_wm_hints_set_input xcb_icccm_wm_hints_set_input
84
#define xcb_wm_hints_t xcb_icccm_wm_hints_t
85
#define XCB_WM_STATE_ICONIC XCB_ICCCM_WM_STATE_ICONIC
86
#define XCB_WM_STATE_WITHDRAWN XCB_ICCCM_WM_STATE_WITHDRAWN
89
#include <private/qguiapplication_p.h>
90
#include <private/qwindow_p.h>
92
#include <qpa/qplatformbackingstore.h>
93
#include <qpa/qwindowsysteminterface.h>
99
#include <X11/Xutil.h>
102
#if defined(XCB_USE_XINPUT2_MAEMO) || defined(XCB_USE_XINPUT2)
103
#include <X11/extensions/XInput2.h>
106
#if defined(XCB_USE_GLX)
107
#include "qglxintegration.h"
108
#include <QtPlatformSupport/private/qglxconvenience_p.h>
109
#elif defined(XCB_USE_EGL)
110
#include "qxcbeglsurface.h"
111
#include <QtPlatformSupport/private/qeglconvenience_p.h>
112
#include <QtPlatformSupport/private/qxlibeglintegration_p.h>
115
#define XCOORD_MAX 16383
117
//#ifdef NET_WM_STATE_DEBUG
121
// Returns true if we should set WM_TRANSIENT_FOR on \a w
122
static inline bool isTransient(const QWindow *w)
124
return w->type() == Qt::Dialog
125
|| w->type() == Qt::Sheet
126
|| w->type() == Qt::Tool
127
|| w->type() == Qt::SplashScreen
128
|| w->type() == Qt::ToolTip
129
|| w->type() == Qt::Drawer
130
|| w->type() == Qt::Popup;
133
static inline QImage::Format imageFormatForDepth(int depth)
136
case 32: return QImage::Format_ARGB32_Premultiplied;
137
case 24: return QImage::Format_RGB32;
138
case 16: return QImage::Format_RGB16;
139
default: return QImage::Format_Invalid;
143
static inline bool positionIncludesFrame(QWindow *w)
145
return qt_window_private(w)->positionPolicy == QWindowPrivate::WindowFrameInclusive;
148
QXcbWindow::QXcbWindow(QWindow *window)
149
: QPlatformWindow(window)
152
, m_gravity(XCB_GRAVITY_STATIC)
154
, m_transparent(false)
155
, m_deferredActivation(false)
156
, m_netWmUserTimeWindow(XCB_NONE)
157
, m_dirtyFrameMargins(false)
158
#if defined(XCB_USE_EGL)
161
, m_lastWindowStateEvent(-1)
163
m_screen = static_cast<QXcbScreen *>(window->screen()->handle());
165
setConnection(m_screen->connection());
170
void QXcbWindow::create()
174
m_deferredExpose = false;
175
m_configureNotifyPending = true;
176
m_windowState = Qt::WindowNoState;
178
Qt::WindowType type = window()->type();
180
if (type == Qt::Desktop) {
181
m_window = m_screen->root();
182
m_depth = m_screen->screen()->root_depth;
183
m_imageFormat = imageFormatForDepth(m_depth);
184
connection()->addWindow(m_window, this);
188
// Determine gravity from initial position. Do not change
189
// later as it will cause the window to move uncontrollably.
190
m_gravity = positionIncludesFrame(window()) ?
191
XCB_GRAVITY_NORTH_WEST : XCB_GRAVITY_STATIC;
193
const quint32 mask = XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK;
194
const quint32 values[] = {
195
// XCB_CW_BACK_PIXMAP
197
// XCB_CW_OVERRIDE_REDIRECT
198
type == Qt::Popup || type == Qt::ToolTip,
200
type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
202
XCB_EVENT_MASK_EXPOSURE
203
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
204
| XCB_EVENT_MASK_KEY_PRESS
205
| XCB_EVENT_MASK_KEY_RELEASE
206
| XCB_EVENT_MASK_BUTTON_PRESS
207
| XCB_EVENT_MASK_BUTTON_RELEASE
208
| XCB_EVENT_MASK_BUTTON_MOTION
209
| XCB_EVENT_MASK_ENTER_WINDOW
210
| XCB_EVENT_MASK_LEAVE_WINDOW
211
| XCB_EVENT_MASK_POINTER_MOTION
212
| XCB_EVENT_MASK_PROPERTY_CHANGE
213
| XCB_EVENT_MASK_FOCUS_CHANGE
216
// Parameters to XCreateWindow() are frame corner + inner size.
217
// This fits in case position policy is frame inclusive. There is
218
// currently no way to implement it for frame-exclusive geometries.
219
QRect rect = window()->geometry();
220
QPlatformWindow::setGeometry(rect);
222
rect.setWidth(qBound(1, rect.width(), XCOORD_MAX));
223
rect.setHeight(qBound(1, rect.height(), XCOORD_MAX));
225
xcb_window_t xcb_parent_id = m_screen->root();
227
xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
229
m_format = window()->requestedFormat();
231
#if (defined(XCB_USE_GLX) || defined(XCB_USE_EGL)) && defined(XCB_USE_XLIB)
232
if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL)) {
233
#if defined(XCB_USE_GLX)
234
XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(m_screen), m_screen->screenNumber(), &m_format);
235
if (!visualInfo && window()->surfaceType() == QSurface::OpenGLSurface)
236
qFatal("Could not initialize GLX");
237
#elif defined(XCB_USE_EGL)
238
EGLDisplay eglDisplay = connection()->egl_display();
239
EGLConfig eglConfig = q_configFromGLFormat(eglDisplay, m_format, true);
240
m_format = q_glFormatFromConfig(eglDisplay, eglConfig);
242
VisualID id = QXlibEglIntegration::getCompatibleVisualId(DISPLAY_FROM_XCB(this), eglDisplay, eglConfig);
244
XVisualInfo visualInfoTemplate;
245
memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
246
visualInfoTemplate.visualid = id;
248
XVisualInfo *visualInfo;
249
int matchingCount = 0;
250
visualInfo = XGetVisualInfo(DISPLAY_FROM_XCB(this), VisualIDMask, &visualInfoTemplate, &matchingCount);
251
if (!visualInfo && window()->surfaceType() == QSurface::OpenGLSurface)
252
qFatal("Could not initialize EGL");
255
m_depth = visualInfo->depth;
256
m_imageFormat = imageFormatForDepth(m_depth);
257
Colormap cmap = XCreateColormap(DISPLAY_FROM_XCB(this), xcb_parent_id, visualInfo->visual, AllocNone);
259
XSetWindowAttributes a;
260
a.background_pixel = WhitePixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
261
a.border_pixel = BlackPixel(DISPLAY_FROM_XCB(this), m_screen->screenNumber());
264
m_visualId = visualInfo->visualid;
266
m_window = XCreateWindow(DISPLAY_FROM_XCB(this), xcb_parent_id, rect.x(), rect.y(), rect.width(), rect.height(),
267
0, visualInfo->depth, InputOutput, visualInfo->visual,
268
CWBackPixel|CWBorderPixel|CWColormap, &a);
275
#endif //defined(XCB_USE_GLX) || defined(XCB_USE_EGL)
277
m_window = xcb_generate_id(xcb_connection());
278
m_depth = m_screen->screen()->root_depth;
279
m_imageFormat = imageFormatForDepth(m_depth);
280
m_visualId = m_screen->screen()->root_visual;
282
Q_XCB_CALL(xcb_create_window(xcb_connection(),
283
XCB_COPY_FROM_PARENT, // depth -- same as root
284
m_window, // window id
285
xcb_parent_id, // parent window id
291
XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
292
m_visualId, // visual
297
connection()->addWindow(m_window, this);
299
Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
301
propagateSizeHints();
303
xcb_atom_t properties[5];
304
int propertyCount = 0;
305
properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
306
properties[propertyCount++] = atom(QXcbAtom::WM_TAKE_FOCUS);
307
properties[propertyCount++] = atom(QXcbAtom::_NET_WM_PING);
309
if (m_screen->syncRequestSupported())
310
properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
312
if (window()->flags() & Qt::WindowContextHelpButtonHint)
313
properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
315
Q_XCB_CALL(xcb_change_property(xcb_connection(),
316
XCB_PROP_MODE_REPLACE,
318
atom(QXcbAtom::WM_PROTOCOLS),
326
if (m_screen->syncRequestSupported()) {
327
m_syncCounter = xcb_generate_id(xcb_connection());
328
Q_XCB_CALL(xcb_sync_create_counter(xcb_connection(), m_syncCounter, m_syncValue));
330
Q_XCB_CALL(xcb_change_property(xcb_connection(),
331
XCB_PROP_MODE_REPLACE,
333
atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
340
// set the PID to let the WM kill the application if unresponsive
342
Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
343
atom(QXcbAtom::_NET_WM_PID), XCB_ATOM_CARDINAL, 32,
346
xcb_wm_hints_t hints;
347
memset(&hints, 0, sizeof(hints));
348
xcb_wm_hints_set_normal(&hints);
350
xcb_wm_hints_set_input(&hints, !(window()->flags() & Qt::WindowDoesNotAcceptFocus));
352
xcb_set_wm_hints(xcb_connection(), m_window, &hints);
354
xcb_window_t leader = m_screen->clientLeader();
355
Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
356
atom(QXcbAtom::WM_CLIENT_LEADER), XCB_ATOM_WINDOW, 32,
359
#ifdef XCB_USE_XINPUT2_MAEMO
360
if (connection()->isUsingXInput2Maemo()) {
361
XIEventMask xieventmask;
362
uchar bitmask[2] = { 0, 0 };
364
xieventmask.deviceid = XIAllMasterDevices;
365
xieventmask.mask = bitmask;
366
xieventmask.mask_len = sizeof(bitmask);
368
XISetMask(bitmask, XI_ButtonPress);
369
XISetMask(bitmask, XI_ButtonRelease);
370
XISetMask(bitmask, XI_Motion);
372
XISelectEvents(DISPLAY_FROM_XCB(this), m_window, &xieventmask, 1);
374
#elif defined(XCB_USE_XINPUT2)
375
connection()->xi2Select(m_window);
378
setWindowState(window()->windowState());
379
setWindowFlags(window()->flags());
380
setWindowTitle(window()->title());
382
if (window()->flags() & Qt::WindowTransparentForInput)
383
setTransparentForMouseEvents(true);
386
// force sync to read outstanding requests - see QTBUG-29106
387
XSync(DISPLAY_FROM_XCB(m_screen), false);
390
#ifndef QT_NO_DRAGANDDROP
391
connection()->drag()->dndEnable(this, true);
394
const qreal opacity = qt_window_private(window())->opacity;
395
if (!qFuzzyCompare(opacity, qreal(1.0)))
399
QXcbWindow::~QXcbWindow()
404
void QXcbWindow::destroy()
406
if (m_syncCounter && m_screen->syncRequestSupported())
407
Q_XCB_CALL(xcb_sync_destroy_counter(xcb_connection(), m_syncCounter));
409
if (m_netWmUserTimeWindow) {
410
xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
411
// Some window managers, like metacity, do XSelectInput on the _NET_WM_USER_TIME_WINDOW window,
412
// without trapping BadWindow (which crashes when the user time window is destroyed).
413
connection()->sync();
414
xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
415
m_netWmUserTimeWindow = XCB_NONE;
417
connection()->removeWindow(m_window);
418
Q_XCB_CALL(xcb_destroy_window(xcb_connection(), m_window));
423
#if defined(XCB_USE_EGL)
429
void QXcbWindow::setGeometry(const QRect &rect)
431
QPlatformWindow::setGeometry(rect);
433
propagateSizeHints();
434
const QRect wmGeometry = windowToWmGeometry(rect);
436
const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
437
const qint32 values[] = {
438
qBound<qint32>(-XCOORD_MAX, wmGeometry.x(), XCOORD_MAX),
439
qBound<qint32>(-XCOORD_MAX, wmGeometry.y(), XCOORD_MAX),
440
qBound<qint32>(1, wmGeometry.width(), XCOORD_MAX),
441
qBound<qint32>(1, wmGeometry.height(), XCOORD_MAX),
444
Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, reinterpret_cast<const quint32*>(values)));
446
xcb_flush(xcb_connection());
449
QMargins QXcbWindow::frameMargins() const
451
if (m_dirtyFrameMargins) {
452
xcb_window_t window = m_window;
453
xcb_window_t parent = m_window;
455
bool foundRoot = false;
457
const QVector<xcb_window_t> &virtualRoots =
458
connection()->wmSupport()->virtualRoots();
461
xcb_query_tree_cookie_t cookie = xcb_query_tree_unchecked(xcb_connection(), parent);
463
xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, NULL);
465
if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1) {
469
parent = reply->parent;
474
m_dirtyFrameMargins = false;
475
m_frameMargins = QMargins();
476
return m_frameMargins;
482
xcb_translate_coordinates_reply_t *reply =
483
xcb_translate_coordinates_reply(
485
xcb_translate_coordinates(xcb_connection(), window, parent, 0, 0),
489
offset = QPoint(reply->dst_x, reply->dst_y);
493
xcb_get_geometry_reply_t *geom =
494
xcb_get_geometry_reply(
496
xcb_get_geometry(xcb_connection(), parent),
501
// add the border_width for the window managers frame... some window managers
502
// do not use a border_width of zero for their frames, and if we the left and
503
// top strut, we ensure that pos() is absolutely correct. frameGeometry()
504
// will still be incorrect though... perhaps i should have foffset as well, to
505
// indicate the frame offset (equal to the border_width on X).
507
// -- copied from qwidget_x11.cpp
509
int left = offset.x() + geom->border_width;
510
int top = offset.y() + geom->border_width;
511
int right = geom->width + geom->border_width - geometry().width() - offset.x();
512
int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
514
m_frameMargins = QMargins(left, top, right, bottom);
519
m_dirtyFrameMargins = false;
522
return m_frameMargins;
525
void QXcbWindow::setVisible(bool visible)
533
void QXcbWindow::show()
535
if (window()->isTopLevel()) {
536
xcb_get_property_cookie_t cookie = xcb_get_wm_hints_unchecked(xcb_connection(), m_window);
538
xcb_wm_hints_t hints;
539
xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, NULL);
541
if (window()->windowState() & Qt::WindowMinimized)
542
xcb_wm_hints_set_iconic(&hints);
544
xcb_wm_hints_set_normal(&hints);
546
xcb_wm_hints_set_input(&hints, !(window()->flags() & Qt::WindowDoesNotAcceptFocus));
548
xcb_set_wm_hints(xcb_connection(), m_window, &hints);
550
// update WM_NORMAL_HINTS
551
propagateSizeHints();
553
// update WM_TRANSIENT_FOR
554
if (isTransient(window())) {
555
xcb_window_t transientXcbParent = 0;
556
if (const QWindow *tp = window()->transientParent())
557
transientXcbParent = static_cast<const QXcbWindow *>(tp->handle())->winId();
558
// Default to client leader if there is no transient parent, else modal dialogs can
559
// be hidden by their parents.
560
if (!transientXcbParent)
561
transientXcbParent = static_cast<QXcbScreen *>(screen())->clientLeader();
562
if (transientXcbParent) { // ICCCM 4.1.2.6
563
Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
564
XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 32,
565
1, &transientXcbParent));
569
// update _MOTIF_WM_HINTS
570
updateMotifWmHintsBeforeMap();
572
// update _NET_WM_STATE
573
updateNetWmStateBeforeMap();
576
if (connection()->time() != XCB_TIME_CURRENT_TIME)
577
updateNetWmUserTime(connection()->time());
579
Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
580
xcb_flush(xcb_connection());
582
connection()->sync();
585
void QXcbWindow::hide()
587
Q_XCB_CALL(xcb_unmap_window(xcb_connection(), m_window));
589
// send synthetic UnmapNotify event according to icccm 4.1.4
590
xcb_unmap_notify_event_t event;
591
event.response_type = XCB_UNMAP_NOTIFY;
592
event.event = m_screen->root();
593
event.window = m_window;
594
event.from_configure = false;
595
Q_XCB_CALL(xcb_send_event(xcb_connection(), false, m_screen->root(),
596
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event));
598
xcb_flush(xcb_connection());
603
struct QtMotifWmHints {
604
quint32 flags, functions, decorations;
610
MWM_HINTS_FUNCTIONS = (1L << 0),
612
MWM_FUNC_ALL = (1L << 0),
613
MWM_FUNC_RESIZE = (1L << 1),
614
MWM_FUNC_MOVE = (1L << 2),
615
MWM_FUNC_MINIMIZE = (1L << 3),
616
MWM_FUNC_MAXIMIZE = (1L << 4),
617
MWM_FUNC_CLOSE = (1L << 5),
619
MWM_HINTS_DECORATIONS = (1L << 1),
621
MWM_DECOR_ALL = (1L << 0),
622
MWM_DECOR_BORDER = (1L << 1),
623
MWM_DECOR_RESIZEH = (1L << 2),
624
MWM_DECOR_TITLE = (1L << 3),
625
MWM_DECOR_MENU = (1L << 4),
626
MWM_DECOR_MINIMIZE = (1L << 5),
627
MWM_DECOR_MAXIMIZE = (1L << 6),
629
MWM_HINTS_INPUT_MODE = (1L << 2),
631
MWM_INPUT_MODELESS = 0L,
632
MWM_INPUT_PRIMARY_APPLICATION_MODAL = 1L,
633
MWM_INPUT_FULL_APPLICATION_MODAL = 3L
636
static QtMotifWmHints getMotifWmHints(QXcbConnection *c, xcb_window_t window)
638
QtMotifWmHints hints;
640
xcb_get_property_cookie_t get_cookie =
641
xcb_get_property_unchecked(c->xcb_connection(), 0, window, c->atom(QXcbAtom::_MOTIF_WM_HINTS),
642
c->atom(QXcbAtom::_MOTIF_WM_HINTS), 0, 20);
644
xcb_get_property_reply_t *reply =
645
xcb_get_property_reply(c->xcb_connection(), get_cookie, NULL);
647
if (reply && reply->format == 32 && reply->type == c->atom(QXcbAtom::_MOTIF_WM_HINTS)) {
648
hints = *((QtMotifWmHints *)xcb_get_property_value(reply));
651
hints.functions = MWM_FUNC_ALL;
652
hints.decorations = MWM_DECOR_ALL;
653
hints.input_mode = 0L;
662
static void setMotifWmHints(QXcbConnection *c, xcb_window_t window, const QtMotifWmHints &hints)
664
if (hints.flags != 0l) {
665
Q_XCB_CALL2(xcb_change_property(c->xcb_connection(),
666
XCB_PROP_MODE_REPLACE,
668
c->atom(QXcbAtom::_MOTIF_WM_HINTS),
669
c->atom(QXcbAtom::_MOTIF_WM_HINTS),
674
Q_XCB_CALL2(xcb_delete_property(c->xcb_connection(), window, c->atom(QXcbAtom::_MOTIF_WM_HINTS)), c);
678
QXcbWindow::NetWmStates QXcbWindow::netWmStates()
680
NetWmStates result(0);
682
xcb_get_property_cookie_t get_cookie =
683
xcb_get_property_unchecked(xcb_connection(), 0, m_window, atom(QXcbAtom::_NET_WM_STATE),
684
XCB_ATOM_ATOM, 0, 1024);
686
xcb_get_property_reply_t *reply =
687
xcb_get_property_reply(xcb_connection(), get_cookie, NULL);
689
if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
690
const xcb_atom_t *states = static_cast<const xcb_atom_t *>(xcb_get_property_value(reply));
691
const xcb_atom_t *statesEnd = states + reply->length;
692
if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_ABOVE)))
693
result |= NetWmStateAbove;
694
if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_BELOW)))
695
result |= NetWmStateBelow;
696
if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN)))
697
result |= NetWmStateFullScreen;
698
if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ)))
699
result |= NetWmStateMaximizedHorz;
700
if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT)))
701
result |= NetWmStateMaximizedVert;
702
if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_MODAL)))
703
result |= NetWmStateModal;
704
if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP)))
705
result |= NetWmStateStaysOnTop;
706
if (statesEnd != qFind(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_DEMANDS_ATTENTION)))
707
result |= NetWmStateDemandsAttention;
710
#ifdef NET_WM_STATE_DEBUG
711
printf("getting net wm state (%x), empty\n", m_window);
718
void QXcbWindow::setNetWmStates(NetWmStates states)
720
QVector<xcb_atom_t> atoms;
721
if (states & NetWmStateAbove)
722
atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_ABOVE));
723
if (states & NetWmStateBelow)
724
atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_BELOW));
725
if (states & NetWmStateFullScreen)
726
atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
727
if (states & NetWmStateMaximizedHorz)
728
atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ));
729
if (states & NetWmStateMaximizedVert)
730
atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
731
if (states & NetWmStateModal)
732
atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_MODAL));
733
if (states & NetWmStateStaysOnTop)
734
atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP));
735
if (states & NetWmStateDemandsAttention)
736
atoms.push_back(atom(QXcbAtom::_NET_WM_STATE_DEMANDS_ATTENTION));
738
if (atoms.isEmpty()) {
739
Q_XCB_CALL(xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_STATE)));
741
Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
742
atom(QXcbAtom::_NET_WM_STATE), XCB_ATOM_ATOM, 32,
743
atoms.count(), atoms.constData()));
745
xcb_flush(xcb_connection());
748
void QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
750
Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
752
if (type == Qt::ToolTip)
753
flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
754
if (type == Qt::Popup)
755
flags |= Qt::X11BypassWindowManagerHint;
757
if (flags & Qt::WindowTransparentForInput) {
758
uint32_t mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_VISIBILITY_CHANGE
759
| XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_RESIZE_REDIRECT
760
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
761
| XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_PROPERTY_CHANGE
762
| XCB_EVENT_MASK_COLOR_MAP_CHANGE | XCB_EVENT_MASK_OWNER_GRAB_BUTTON;
763
xcb_change_window_attributes(xcb_connection(), xcb_window(), XCB_CW_EVENT_MASK, &mask);
766
setNetWmWindowFlags(flags);
767
setMotifWindowFlags(flags);
769
setTransparentForMouseEvents(flags & Qt::WindowTransparentForInput);
770
updateDoesNotAcceptFocus(flags & Qt::WindowDoesNotAcceptFocus);
773
void QXcbWindow::setMotifWindowFlags(Qt::WindowFlags flags)
775
Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
777
QtMotifWmHints mwmhints;
779
mwmhints.functions = 0L;
780
mwmhints.decorations = 0;
781
mwmhints.input_mode = 0L;
782
mwmhints.status = 0L;
784
if (type != Qt::SplashScreen) {
785
mwmhints.flags |= MWM_HINTS_DECORATIONS;
787
bool customize = flags & Qt::CustomizeWindowHint;
788
if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
789
mwmhints.decorations |= MWM_DECOR_BORDER;
790
mwmhints.decorations |= MWM_DECOR_RESIZEH;
791
mwmhints.decorations |= MWM_DECOR_TITLE;
793
if (flags & Qt::WindowSystemMenuHint)
794
mwmhints.decorations |= MWM_DECOR_MENU;
796
if (flags & Qt::WindowMinimizeButtonHint) {
797
mwmhints.decorations |= MWM_DECOR_MINIMIZE;
798
mwmhints.functions |= MWM_FUNC_MINIMIZE;
801
if (flags & Qt::WindowMaximizeButtonHint) {
802
mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
803
mwmhints.functions |= MWM_FUNC_MAXIMIZE;
806
if (flags & Qt::WindowCloseButtonHint)
807
mwmhints.functions |= MWM_FUNC_CLOSE;
810
// if type == Qt::SplashScreen
811
mwmhints.decorations = MWM_DECOR_ALL;
814
if (mwmhints.functions != 0) {
815
mwmhints.flags |= MWM_HINTS_FUNCTIONS;
816
mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
818
mwmhints.functions = MWM_FUNC_ALL;
821
if (!(flags & Qt::FramelessWindowHint)
822
&& flags & Qt::CustomizeWindowHint
823
&& flags & Qt::WindowTitleHint
825
(Qt::WindowMinimizeButtonHint
826
| Qt::WindowMaximizeButtonHint
827
| Qt::WindowCloseButtonHint)))
829
// a special case - only the titlebar without any button
830
mwmhints.flags = MWM_HINTS_FUNCTIONS;
831
mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
832
mwmhints.decorations = 0;
835
setMotifWmHints(connection(), m_window, mwmhints);
838
void QXcbWindow::changeNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
840
xcb_client_message_event_t event;
842
event.response_type = XCB_CLIENT_MESSAGE;
844
event.window = m_window;
845
event.type = atom(QXcbAtom::_NET_WM_STATE);
846
event.data.data32[0] = set ? 1 : 0;
847
event.data.data32[1] = one;
848
event.data.data32[2] = two;
849
event.data.data32[3] = 0;
850
event.data.data32[4] = 0;
852
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));
855
void QXcbWindow::setWindowState(Qt::WindowState state)
857
if (state == m_windowState)
861
switch (m_windowState) {
862
case Qt::WindowMinimized:
863
Q_XCB_CALL(xcb_map_window(xcb_connection(), m_window));
865
case Qt::WindowMaximized:
866
changeNetWmState(false,
867
atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
868
atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
870
case Qt::WindowFullScreen:
871
changeNetWmState(false, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
879
case Qt::WindowMinimized:
881
xcb_client_message_event_t event;
883
event.response_type = XCB_CLIENT_MESSAGE;
885
event.window = m_window;
886
event.type = atom(QXcbAtom::WM_CHANGE_STATE);
887
event.data.data32[0] = XCB_WM_STATE_ICONIC;
888
event.data.data32[1] = 0;
889
event.data.data32[2] = 0;
890
event.data.data32[3] = 0;
891
event.data.data32[4] = 0;
893
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));
896
case Qt::WindowMaximized:
897
changeNetWmState(true,
898
atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ),
899
atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT));
901
case Qt::WindowFullScreen:
902
changeNetWmState(true, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN));
904
case Qt::WindowNoState:
910
connection()->sync();
912
m_windowState = state;
915
void QXcbWindow::setNetWmWindowFlags(Qt::WindowFlags flags)
917
// in order of decreasing priority
918
QVector<uint> windowTypes;
920
Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
925
windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_DIALOG));
929
windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_UTILITY));
932
windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_TOOLTIP));
934
case Qt::SplashScreen:
935
windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_SPLASH));
941
if (flags & Qt::FramelessWindowHint)
942
windowTypes.append(atom(QXcbAtom::_KDE_NET_WM_WINDOW_TYPE_OVERRIDE));
944
windowTypes.append(atom(QXcbAtom::_NET_WM_WINDOW_TYPE_NORMAL));
946
Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
947
atom(QXcbAtom::_NET_WM_WINDOW_TYPE), XCB_ATOM_ATOM, 32,
948
windowTypes.count(), windowTypes.constData()));
951
void QXcbWindow::updateMotifWmHintsBeforeMap()
953
QtMotifWmHints mwmhints = getMotifWmHints(connection(), m_window);
955
if (window()->modality() != Qt::NonModal) {
956
switch (window()->modality()) {
957
case Qt::WindowModal:
958
mwmhints.input_mode = MWM_INPUT_PRIMARY_APPLICATION_MODAL;
960
case Qt::ApplicationModal:
962
mwmhints.input_mode = MWM_INPUT_FULL_APPLICATION_MODAL;
965
mwmhints.flags |= MWM_HINTS_INPUT_MODE;
967
mwmhints.input_mode = MWM_INPUT_MODELESS;
968
mwmhints.flags &= ~MWM_HINTS_INPUT_MODE;
971
if (window()->minimumSize() == window()->maximumSize()) {
972
// fixed size, remove the resize handle (since mwm/dtwm
973
// isn't smart enough to do it itself)
974
mwmhints.flags |= MWM_HINTS_FUNCTIONS;
975
if (mwmhints.functions == MWM_FUNC_ALL) {
976
mwmhints.functions = MWM_FUNC_MOVE;
978
mwmhints.functions &= ~MWM_FUNC_RESIZE;
981
if (mwmhints.decorations == MWM_DECOR_ALL) {
982
mwmhints.flags |= MWM_HINTS_DECORATIONS;
983
mwmhints.decorations = (MWM_DECOR_BORDER
987
mwmhints.decorations &= ~MWM_DECOR_RESIZEH;
991
if (window()->flags() & Qt::WindowMinimizeButtonHint) {
992
mwmhints.flags |= MWM_HINTS_DECORATIONS;
993
mwmhints.decorations |= MWM_DECOR_MINIMIZE;
994
mwmhints.functions |= MWM_FUNC_MINIMIZE;
996
if (window()->flags() & Qt::WindowMaximizeButtonHint) {
997
mwmhints.flags |= MWM_HINTS_DECORATIONS;
998
mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
999
mwmhints.functions |= MWM_FUNC_MAXIMIZE;
1001
if (window()->flags() & Qt::WindowCloseButtonHint)
1002
mwmhints.functions |= MWM_FUNC_CLOSE;
1004
setMotifWmHints(connection(), m_window, mwmhints);
1007
void QXcbWindow::updateNetWmStateBeforeMap()
1009
NetWmStates states(0);
1011
const Qt::WindowFlags flags = window()->flags();
1012
if (flags & Qt::WindowStaysOnTopHint) {
1013
states |= NetWmStateAbove;
1014
states |= NetWmStateStaysOnTop;
1015
} else if (flags & Qt::WindowStaysOnBottomHint) {
1016
states |= NetWmStateBelow;
1019
if (window()->windowState() & Qt::WindowFullScreen)
1020
states |= NetWmStateFullScreen;
1022
if (window()->windowState() & Qt::WindowMaximized) {
1023
states |= NetWmStateMaximizedHorz;
1024
states |= NetWmStateMaximizedVert;
1027
if (window()->modality() != Qt::NonModal)
1028
states |= NetWmStateModal;
1030
setNetWmStates(states);
1033
void QXcbWindow::updateNetWmUserTime(xcb_timestamp_t timestamp)
1035
xcb_window_t wid = m_window;
1036
connection()->setNetWmUserTime(timestamp);
1038
const bool isSupportedByWM = connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1039
if (m_netWmUserTimeWindow || isSupportedByWM) {
1040
if (!m_netWmUserTimeWindow) {
1041
m_netWmUserTimeWindow = xcb_generate_id(xcb_connection());
1042
Q_XCB_CALL(xcb_create_window(xcb_connection(),
1043
XCB_COPY_FROM_PARENT, // depth -- same as root
1044
m_netWmUserTimeWindow, // window id
1045
m_window, // parent window id
1048
XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
1049
m_visualId, // visual
1052
wid = m_netWmUserTimeWindow;
1053
xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW),
1054
XCB_ATOM_WINDOW, 32, 1, &m_netWmUserTimeWindow);
1055
xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME));
1056
} else if (!isSupportedByWM) {
1057
// WM no longer supports it, then we should remove the
1058
// _NET_WM_USER_TIME_WINDOW atom.
1059
xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
1060
xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
1061
m_netWmUserTimeWindow = XCB_NONE;
1063
wid = m_netWmUserTimeWindow;
1066
xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, wid, atom(QXcbAtom::_NET_WM_USER_TIME),
1067
XCB_ATOM_CARDINAL, 32, 1, ×tamp);
1070
void QXcbWindow::setTransparentForMouseEvents(bool transparent)
1072
if (transparent == m_transparent)
1075
xcb_rectangle_t rectangle;
1077
xcb_rectangle_t *rect = 0;
1083
rectangle.width = geometry().width();
1084
rectangle.height = geometry().height();
1089
xcb_xfixes_region_t region = xcb_generate_id(xcb_connection());
1090
xcb_xfixes_create_region(xcb_connection(), region, nrect, rect);
1091
xcb_xfixes_set_window_shape_region_checked(xcb_connection(), m_window, XCB_SHAPE_SK_INPUT, 0, 0, region);
1092
xcb_xfixes_destroy_region(xcb_connection(), region);
1094
m_transparent = transparent;
1097
void QXcbWindow::updateDoesNotAcceptFocus(bool doesNotAcceptFocus)
1099
xcb_get_property_cookie_t cookie = xcb_get_wm_hints_unchecked(xcb_connection(), m_window);
1101
xcb_wm_hints_t hints;
1102
if (!xcb_get_wm_hints_reply(xcb_connection(), cookie, &hints, NULL)) {
1106
xcb_wm_hints_set_input(&hints, !doesNotAcceptFocus);
1107
xcb_set_wm_hints(xcb_connection(), m_window, &hints);
1110
WId QXcbWindow::winId() const
1115
void QXcbWindow::setParent(const QPlatformWindow *parent)
1117
QPoint topLeft = geometry().topLeft();
1119
xcb_window_t xcb_parent_id = parent ? static_cast<const QXcbWindow *>(parent)->xcb_window() : m_screen->root();
1120
Q_XCB_CALL(xcb_reparent_window(xcb_connection(), xcb_window(), xcb_parent_id, topLeft.x(), topLeft.y()));
1123
void QXcbWindow::setWindowTitle(const QString &title)
1125
QString fullTitle = title;
1126
if (QGuiApplicationPrivate::displayName) {
1127
// Append display name, if set.
1128
if (!fullTitle.isEmpty())
1129
fullTitle += QString::fromUtf8(" \xe2\x80\x94 "); // unicode character U+2014, EM DASH
1130
fullTitle += *QGuiApplicationPrivate::displayName;
1131
} else if (fullTitle.isEmpty()) {
1132
// Don't let the window title be completely empty, use the app name as fallback.
1133
fullTitle = QCoreApplication::applicationName();
1135
const QByteArray ba = fullTitle.toUtf8();
1137
Q_XCB_CALL(xcb_change_property(xcb_connection(),
1138
XCB_PROP_MODE_REPLACE,
1140
atom(QXcbAtom::_NET_WM_NAME),
1141
atom(QXcbAtom::UTF8_STRING),
1147
void QXcbWindow::setWindowIcon(const QIcon &icon)
1149
QVector<quint32> icon_data;
1151
if (!icon.isNull()) {
1152
QList<QSize> availableSizes = icon.availableSizes();
1153
if (availableSizes.isEmpty()) {
1154
// try to use default sizes since the icon can be a scalable image like svg.
1155
availableSizes.push_back(QSize(16,16));
1156
availableSizes.push_back(QSize(32,32));
1157
availableSizes.push_back(QSize(64,64));
1158
availableSizes.push_back(QSize(128,128));
1160
for (int i = 0; i < availableSizes.size(); ++i) {
1161
QSize size = availableSizes.at(i);
1162
QPixmap pixmap = icon.pixmap(size);
1163
if (!pixmap.isNull()) {
1164
QImage image = pixmap.toImage().convertToFormat(QImage::Format_ARGB32);
1165
int pos = icon_data.size();
1166
icon_data.resize(pos + 2 + image.width()*image.height());
1167
icon_data[pos++] = image.width();
1168
icon_data[pos++] = image.height();
1169
memcpy(icon_data.data() + pos, image.bits(), image.width()*image.height()*4);
1174
if (!icon_data.isEmpty()) {
1175
Q_XCB_CALL(xcb_change_property(xcb_connection(),
1176
XCB_PROP_MODE_REPLACE,
1178
atom(QXcbAtom::_NET_WM_ICON),
1179
atom(QXcbAtom::CARDINAL),
1182
(unsigned char *) icon_data.data()));
1184
Q_XCB_CALL(xcb_delete_property(xcb_connection(),
1186
atom(QXcbAtom::_NET_WM_ICON)));
1190
void QXcbWindow::raise()
1192
const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1193
const quint32 values[] = { XCB_STACK_MODE_ABOVE };
1194
Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1197
void QXcbWindow::lower()
1199
const quint32 mask = XCB_CONFIG_WINDOW_STACK_MODE;
1200
const quint32 values[] = { XCB_STACK_MODE_BELOW };
1201
Q_XCB_CALL(xcb_configure_window(xcb_connection(), m_window, mask, values));
1204
// Adapt the geometry to match the WM expection with regards
1206
QRect QXcbWindow::windowToWmGeometry(QRect r) const
1208
if (m_dirtyFrameMargins || m_frameMargins.isNull())
1210
const bool frameInclusive = positionIncludesFrame(window());
1211
// XCB_GRAVITY_STATIC requires the inner geometry, whereas
1212
// XCB_GRAVITY_NORTH_WEST requires the frame geometry
1213
if (frameInclusive && m_gravity == XCB_GRAVITY_STATIC) {
1214
r.translate(m_frameMargins.left(), m_frameMargins.top());
1215
} else if (!frameInclusive && m_gravity == XCB_GRAVITY_NORTH_WEST) {
1216
r.translate(-m_frameMargins.left(), -m_frameMargins.top());
1221
void QXcbWindow::propagateSizeHints()
1223
// update WM_NORMAL_HINTS
1224
xcb_size_hints_t hints;
1225
memset(&hints, 0, sizeof(hints));
1227
const QRect rect = windowToWmGeometry(geometry());
1229
QWindow *win = window();
1231
xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
1232
xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
1233
xcb_size_hints_set_win_gravity(&hints, m_gravity);
1235
QSize minimumSize = win->minimumSize();
1236
QSize maximumSize = win->maximumSize();
1237
QSize baseSize = win->baseSize();
1238
QSize sizeIncrement = win->sizeIncrement();
1240
if (minimumSize.width() > 0 || minimumSize.height() > 0)
1241
xcb_size_hints_set_min_size(&hints, minimumSize.width(), minimumSize.height());
1243
if (maximumSize.width() < QWINDOWSIZE_MAX || maximumSize.height() < QWINDOWSIZE_MAX)
1244
xcb_size_hints_set_max_size(&hints,
1245
qMin(XCOORD_MAX, maximumSize.width()),
1246
qMin(XCOORD_MAX, maximumSize.height()));
1248
if (sizeIncrement.width() > 0 || sizeIncrement.height() > 0) {
1249
xcb_size_hints_set_base_size(&hints, baseSize.width(), baseSize.height());
1250
xcb_size_hints_set_resize_inc(&hints, sizeIncrement.width(), sizeIncrement.height());
1253
xcb_set_wm_normal_hints(xcb_connection(), m_window, &hints);
1256
void QXcbWindow::requestActivateWindow()
1259
m_deferredActivation = true;
1262
m_deferredActivation = false;
1264
updateNetWmUserTime(connection()->time());
1266
if (window()->isTopLevel()
1267
&& connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_ACTIVE_WINDOW))) {
1268
xcb_client_message_event_t event;
1270
event.response_type = XCB_CLIENT_MESSAGE;
1272
event.window = m_window;
1273
event.type = atom(QXcbAtom::_NET_ACTIVE_WINDOW);
1274
event.data.data32[0] = 1;
1275
event.data.data32[1] = connection()->time();
1276
QWindow *focusWindow = QGuiApplication::focusWindow();
1277
event.data.data32[2] = focusWindow ? focusWindow->winId() : XCB_NONE;
1278
event.data.data32[3] = 0;
1279
event.data.data32[4] = 0;
1281
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));
1283
Q_XCB_CALL(xcb_set_input_focus(xcb_connection(), XCB_INPUT_FOCUS_PARENT, m_window, connection()->time()));
1286
connection()->sync();
1289
#if XCB_USE_MAEMO_WINDOW_PROPERTIES
1290
void QXcbWindow::handleContentOrientationChange(Qt::ScreenOrientation orientation)
1293
switch (orientation) {
1294
case Qt::PortraitOrientation: angle = 270; break;
1295
case Qt::LandscapeOrientation: angle = 0; break;
1296
case Qt::InvertedPortraitOrientation: angle = 90; break;
1297
case Qt::InvertedLandscapeOrientation: angle = 180; break;
1298
case Qt::PrimaryOrientation: break;
1300
Q_XCB_CALL(xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
1301
atom(QXcbAtom::MeegoTouchOrientationAngle), XCB_ATOM_CARDINAL, 32,
1306
QSurfaceFormat QXcbWindow::format() const
1308
// ### return actual format
1312
#if defined(XCB_USE_EGL)
1313
QXcbEGLSurface *QXcbWindow::eglSurface() const
1315
if (!m_eglSurface) {
1316
EGLDisplay display = connection()->egl_display();
1317
EGLConfig config = q_configFromGLFormat(display, window()->requestedFormat(), true);
1318
EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)m_window, 0);
1320
m_eglSurface = new QXcbEGLSurface(display, surface);
1323
return m_eglSurface;
1327
class ExposeCompressor
1330
ExposeCompressor(xcb_window_t window, QRegion *region)
1337
bool checkEvent(xcb_generic_event_t *event)
1341
if ((event->response_type & ~0x80) != XCB_EXPOSE)
1343
xcb_expose_event_t *expose = (xcb_expose_event_t *)event;
1344
if (expose->window != m_window)
1346
if (expose->count == 0)
1348
*m_region |= QRect(expose->x, expose->y, expose->width, expose->height);
1352
bool pending() const
1358
xcb_window_t m_window;
1363
void QXcbWindow::handleExposeEvent(const xcb_expose_event_t *event)
1365
QRect rect(event->x, event->y, event->width, event->height);
1367
if (m_exposeRegion.isEmpty())
1368
m_exposeRegion = rect;
1370
m_exposeRegion |= rect;
1372
ExposeCompressor compressor(m_window, &m_exposeRegion);
1373
xcb_generic_event_t *filter = 0;
1375
filter = connection()->checkEvent(compressor);
1379
// if count is non-zero there are more expose events pending
1380
if (event->count == 0 || !compressor.pending()) {
1381
QWindowSystemInterface::handleExposeEvent(window(), m_exposeRegion);
1382
m_exposeRegion = QRegion();
1386
void QXcbWindow::handleClientMessageEvent(const xcb_client_message_event_t *event)
1388
if (event->format != 32)
1391
if (event->type == atom(QXcbAtom::WM_PROTOCOLS)) {
1392
if (event->data.data32[0] == atom(QXcbAtom::WM_DELETE_WINDOW)) {
1393
QWindowSystemInterface::handleCloseEvent(window());
1394
} else if (event->data.data32[0] == atom(QXcbAtom::WM_TAKE_FOCUS)) {
1395
connection()->setTime(event->data.data32[1]);
1396
} else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_PING)) {
1397
xcb_client_message_event_t reply = *event;
1399
reply.response_type = XCB_CLIENT_MESSAGE;
1400
reply.window = m_screen->root();
1402
xcb_send_event(xcb_connection(), 0, m_screen->root(), XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&reply);
1403
xcb_flush(xcb_connection());
1404
} else if (event->data.data32[0] == atom(QXcbAtom::_NET_WM_SYNC_REQUEST)) {
1405
connection()->setTime(event->data.data32[1]);
1406
m_syncValue.lo = event->data.data32[2];
1407
m_syncValue.hi = event->data.data32[3];
1409
qWarning() << "QXcbWindow: Unhandled WM_PROTOCOLS message:" << connection()->atomName(event->data.data32[0]);
1411
#ifndef QT_NO_DRAGANDDROP
1412
} else if (event->type == atom(QXcbAtom::XdndEnter)) {
1413
connection()->drag()->handleEnter(window(), event);
1414
} else if (event->type == atom(QXcbAtom::XdndPosition)) {
1415
connection()->drag()->handlePosition(window(), event);
1416
} else if (event->type == atom(QXcbAtom::XdndLeave)) {
1417
connection()->drag()->handleLeave(window(), event);
1418
} else if (event->type == atom(QXcbAtom::XdndDrop)) {
1419
connection()->drag()->handleDrop(window(), event);
1421
} else if (event->type == atom(QXcbAtom::_XEMBED)) { // QSystemTrayIcon
1423
qWarning() << "QXcbWindow: Unhandled client message:" << connection()->atomName(event->type);
1427
void QXcbWindow::handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event)
1429
bool fromSendEvent = (event->response_type & 0x80);
1430
QPoint pos(event->x, event->y);
1431
if (!parent() && !fromSendEvent) {
1432
// Do not trust the position, query it instead.
1433
xcb_translate_coordinates_cookie_t cookie = xcb_translate_coordinates(xcb_connection(), xcb_window(),
1434
m_screen->root(), 0, 0);
1435
xcb_translate_coordinates_reply_t *reply = xcb_translate_coordinates_reply(xcb_connection(), cookie, NULL);
1437
pos.setX(reply->dst_x);
1438
pos.setY(reply->dst_y);
1443
QRect rect(pos, QSize(event->width, event->height));
1445
QPlatformWindow::setGeometry(rect);
1446
QWindowSystemInterface::handleGeometryChange(window(), rect);
1448
m_configureNotifyPending = false;
1450
if (m_deferredExpose) {
1451
m_deferredExpose = false;
1452
QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
1455
m_dirtyFrameMargins = true;
1458
bool QXcbWindow::isExposed() const
1463
void QXcbWindow::handleMapNotifyEvent(const xcb_map_notify_event_t *event)
1465
if (event->window == m_window) {
1467
if (m_deferredActivation)
1468
requestActivateWindow();
1469
if (m_configureNotifyPending)
1470
m_deferredExpose = true;
1472
QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), geometry().size()));
1476
void QXcbWindow::handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event)
1478
if (event->window == m_window) {
1480
QWindowSystemInterface::handleExposeEvent(window(), QRegion());
1484
void QXcbWindow::handleButtonPressEvent(const xcb_button_press_event_t *event)
1486
updateNetWmUserTime(event->time);
1488
QPoint local(event->event_x, event->event_y);
1489
QPoint global(event->root_x, event->root_y);
1491
Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1493
if (event->detail >= 4 && event->detail <= 7) {
1494
// Logic borrowed from qapplication_x11.cpp
1495
int delta = 120 * ((event->detail == 4 || event->detail == 6) ? 1 : -1);
1496
bool hor = (((event->detail == 4 || event->detail == 5)
1497
&& (modifiers & Qt::AltModifier))
1498
|| (event->detail == 6 || event->detail == 7));
1500
QWindowSystemInterface::handleWheelEvent(window(), event->time,
1501
local, global, delta, hor ? Qt::Horizontal : Qt::Vertical, modifiers);
1505
handleMouseEvent(event->time, local, global, modifiers);
1508
void QXcbWindow::handleButtonReleaseEvent(const xcb_button_release_event_t *event)
1510
QPoint local(event->event_x, event->event_y);
1511
QPoint global(event->root_x, event->root_y);
1512
Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1514
if (event->detail >= 4 && event->detail <= 7) {
1515
// mouse wheel, handled in handleButtonPressEvent()
1519
handleMouseEvent(event->time, local, global, modifiers);
1522
void QXcbWindow::handleMotionNotifyEvent(const xcb_motion_notify_event_t *event)
1524
QPoint local(event->event_x, event->event_y);
1525
QPoint global(event->root_x, event->root_y);
1526
Qt::KeyboardModifiers modifiers = connection()->keyboard()->translateModifiers(event->state);
1528
handleMouseEvent(event->time, local, global, modifiers);
1531
void QXcbWindow::handleMouseEvent(xcb_timestamp_t time, const QPoint &local, const QPoint &global, Qt::KeyboardModifiers modifiers)
1533
connection()->setTime(time);
1534
QWindowSystemInterface::handleMouseEvent(window(), time, local, global, connection()->buttons(), modifiers);
1537
class EnterEventChecker
1540
bool checkEvent(xcb_generic_event_t *event)
1544
if ((event->response_type & ~0x80) != XCB_ENTER_NOTIFY)
1547
xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *)event;
1549
if ((enter->mode != XCB_NOTIFY_MODE_NORMAL && enter->mode != XCB_NOTIFY_MODE_UNGRAB)
1550
|| enter->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1551
|| enter->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1560
void QXcbWindow::handleEnterNotifyEvent(const xcb_enter_notify_event_t *event)
1562
connection()->setTime(event->time);
1564
if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1565
|| event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1566
|| event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1571
const QPoint local(event->event_x, event->event_y);
1572
const QPoint global(event->root_x, event->root_y);
1573
QWindowSystemInterface::handleEnterEvent(window(), local, global);
1576
void QXcbWindow::handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event)
1578
connection()->setTime(event->time);
1580
if ((event->mode != XCB_NOTIFY_MODE_NORMAL && event->mode != XCB_NOTIFY_MODE_UNGRAB)
1581
|| event->detail == XCB_NOTIFY_DETAIL_VIRTUAL
1582
|| event->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL)
1587
EnterEventChecker checker;
1588
xcb_enter_notify_event_t *enter = (xcb_enter_notify_event_t *)connection()->checkEvent(checker);
1589
QXcbWindow *enterWindow = enter ? connection()->platformWindowFromId(enter->event) : 0;
1592
QPoint local(enter->event_x, enter->event_y);
1593
QPoint global(enter->root_x, enter->root_y);
1595
QWindowSystemInterface::handleEnterLeaveEvent(enterWindow->window(), window(), local, global);
1597
QWindowSystemInterface::handleLeaveEvent(window());
1603
void QXcbWindow::handlePropertyNotifyEvent(const xcb_property_notify_event_t *event)
1605
connection()->setTime(event->time);
1607
const bool propertyDeleted = event->state == XCB_PROPERTY_DELETE;
1608
const xcb_atom_t netWmStateAtom = atom(QXcbAtom::_NET_WM_STATE);
1609
const xcb_atom_t wmStateAtom = atom(QXcbAtom::WM_STATE);
1611
if (event->atom == netWmStateAtom || event->atom == wmStateAtom) {
1612
if (propertyDeleted)
1615
Qt::WindowState newState = Qt::WindowNoState;
1616
if (event->atom == wmStateAtom) { // WM_STATE: Quick check for 'Minimize'.
1617
const xcb_get_property_cookie_t get_cookie =
1618
xcb_get_property(xcb_connection(), 0, m_window, wmStateAtom,
1619
XCB_ATOM_ANY, 0, 1024);
1621
xcb_get_property_reply_t *reply =
1622
xcb_get_property_reply(xcb_connection(), get_cookie, NULL);
1624
if (reply && reply->format == 32 && reply->type == wmStateAtom) {
1625
const long *data = (const long *)xcb_get_property_value(reply);
1626
if (reply->length != 0 && XCB_WM_STATE_ICONIC == data[0])
1627
newState = Qt::WindowMinimized;
1630
} // WM_STATE: Quick check for 'Minimize'.
1631
if (newState != Qt::WindowMinimized) { // Something else changed, get _NET_WM_STATE.
1632
const NetWmStates states = netWmStates();
1633
if ((states & NetWmStateMaximizedHorz) && (states & NetWmStateMaximizedVert))
1634
newState = Qt::WindowMaximized;
1635
else if (states & NetWmStateFullScreen)
1636
newState = Qt::WindowFullScreen;
1638
// Send Window state, compress events in case other flags (modality, etc) are changed.
1639
if (m_lastWindowStateEvent != newState) {
1640
QWindowSystemInterface::handleWindowStateChanged(window(), newState);
1641
m_lastWindowStateEvent = newState;
1646
void QXcbWindow::handleFocusInEvent(const xcb_focus_in_event_t *)
1648
QWindowSystemInterface::handleWindowActivated(window());
1651
static bool focusInPeeker(xcb_generic_event_t *event)
1654
// FocusIn event is not in the queue, proceed with FocusOut normally.
1655
QWindowSystemInterface::handleWindowActivated(0);
1658
uint response_type = event->response_type & ~0x80;
1659
return response_type == XCB_FOCUS_IN;
1662
void QXcbWindow::handleFocusOutEvent(const xcb_focus_out_event_t *)
1664
// Do not set the active window to 0 if there is a FocusIn coming.
1665
// There is however no equivalent for XPutBackEvent so register a
1666
// callback for QXcbConnection instead.
1667
connection()->addPeekFunc(focusInPeeker);
1670
void QXcbWindow::updateSyncRequestCounter()
1672
if (m_screen->syncRequestSupported() && (m_syncValue.lo != 0 || m_syncValue.hi != 0)) {
1673
Q_XCB_CALL(xcb_sync_set_counter(xcb_connection(), m_syncCounter, m_syncValue));
1674
xcb_flush(xcb_connection());
1675
connection()->sync();
1682
bool QXcbWindow::setKeyboardGrabEnabled(bool grab)
1685
xcb_ungrab_keyboard(xcb_connection(), XCB_TIME_CURRENT_TIME);
1688
xcb_grab_keyboard_cookie_t cookie = xcb_grab_keyboard(xcb_connection(), false,
1689
m_window, XCB_TIME_CURRENT_TIME,
1690
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
1691
xcb_grab_keyboard_reply_t *reply = xcb_grab_keyboard_reply(xcb_connection(), cookie, NULL);
1692
bool result = !(!reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1697
bool QXcbWindow::setMouseGrabEnabled(bool grab)
1700
xcb_ungrab_pointer(xcb_connection(), XCB_TIME_CURRENT_TIME);
1703
xcb_grab_pointer_cookie_t cookie = xcb_grab_pointer(xcb_connection(), false, m_window,
1704
(XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
1705
| XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW
1706
| XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_POINTER_MOTION),
1707
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
1708
XCB_WINDOW_NONE, XCB_CURSOR_NONE,
1709
XCB_TIME_CURRENT_TIME);
1710
xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(xcb_connection(), cookie, NULL);
1711
bool result = !(!reply || reply->status != XCB_GRAB_STATUS_SUCCESS);
1716
void QXcbWindow::setCursor(xcb_cursor_t cursor)
1718
xcb_change_window_attributes(xcb_connection(), m_window, XCB_CW_CURSOR, &cursor);
1719
xcb_flush(xcb_connection());
1722
bool QXcbWindow::startSystemResize(const QPoint &pos, Qt::Corner corner)
1724
const xcb_atom_t moveResize = connection()->atom(QXcbAtom::_NET_WM_MOVERESIZE);
1725
if (!connection()->wmSupport()->isSupportedByWM(moveResize))
1727
xcb_client_message_event_t xev;
1728
xev.response_type = XCB_CLIENT_MESSAGE;
1729
xev.type = moveResize;
1730
xev.window = xcb_window();
1732
const QPoint globalPos = window()->mapToGlobal(pos);
1733
xev.data.data32[0] = globalPos.x();
1734
xev.data.data32[1] = globalPos.y();
1735
const bool bottom = corner == Qt::BottomRightCorner || corner == Qt::BottomLeftCorner;
1736
const bool left = corner == Qt::BottomLeftCorner || corner == Qt::TopLeftCorner;
1738
xev.data.data32[2] = left ? 6 : 4; // bottomleft/bottomright
1740
xev.data.data32[2] = left ? 0 : 2; // topleft/topright
1741
xev.data.data32[3] = XCB_BUTTON_INDEX_1;
1742
xev.data.data32[4] = 0;
1743
xcb_ungrab_pointer(connection()->xcb_connection(), XCB_CURRENT_TIME);
1744
xcb_send_event(connection()->xcb_connection(), false, m_screen->root(),
1745
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
1746
(const char *)&xev);
1750
#if !defined(QT_NO_SHAPE)
1752
static inline xcb_rectangle_t qRectToXCBRectangle(const QRect &r)
1754
xcb_rectangle_t result;
1755
result.x = qMax(SHRT_MIN, r.x());
1756
result.y = qMax(SHRT_MIN, r.y());
1757
result.width = qMin((int)USHRT_MAX, r.width());
1758
result.height = qMin((int)USHRT_MAX, r.height());
1762
void QXcbWindow::setOpacity(qreal level)
1767
quint32 value = qRound64(qBound(qreal(0), level, qreal(1)) * 0xffffffff);
1769
Q_XCB_CALL(xcb_change_property(xcb_connection(),
1770
XCB_PROP_MODE_REPLACE,
1772
atom(QXcbAtom::_NET_WM_WINDOW_OPACITY),
1779
void QXcbWindow::setMask(const QRegion ®ion)
1781
if (!connection()->hasXShape())
1783
if (region.isEmpty()) {
1784
xcb_shape_mask(connection()->xcb_connection(), XCB_SHAPE_SO_SET,
1785
XCB_SHAPE_SK_BOUNDING, xcb_window(), 0, 0, XCB_NONE);
1787
QVector<xcb_rectangle_t> rects;
1788
foreach (const QRect &r, region.rects())
1789
rects.push_back(qRectToXCBRectangle(r));
1790
xcb_shape_rectangles(connection()->xcb_connection(), XCB_SHAPE_SO_SET,
1791
XCB_SHAPE_SK_BOUNDING, XCB_CLIP_ORDERING_UNSORTED,
1792
xcb_window(), 0, 0, rects.size(), &rects[0]);
1796
#endif // !QT_NO_SHAPE