-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqxcbwindow.cpp
2162 lines (1851 loc) · 80 KB
/
qxcbwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qxcbwindow.h"
#include <QtDebug>
#include <QMetaEnum>
#include <QScreen>
#include <QtGui/QRegion>
#include <QtGui/private/qhighdpiscaling_p.h>
#include "qxcbintegration.h"
#include "qxcbconnection.h"
#include "qxcbscreen.h"
#include "qxcbkeyboard.h"
#include "qxcbimage.h"
#include "qxcbwmsupport.h"
#include "qxcbimage.h"
#include "qxcbnativeinterface.h"
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformcursor.h>
#include <algorithm>
#include <xcb/xcb_icccm.h>
#include <xcb/xfixes.h>
#include <xcb/shape.h>
#include <xcb/xinput.h>
#include <private/qguiapplication_p.h>
#include <private/qwindow_p.h>
#include <qpa/qplatformbackingstore.h>
#include <qpa/qwindowsysteminterface.h>
#include <QTextCodec>
#include <stdio.h>
#if QT_CONFIG(xcb_xlib)
#define register /* C++17 deprecated register */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#undef register
#endif
#define XCOORD_MAX 16383
enum {
defaultWindowWidth = 160,
defaultWindowHeight = 160
};
QT_BEGIN_NAMESPACE
Q_DECLARE_TYPEINFO(xcb_rectangle_t, Q_PRIMITIVE_TYPE);
#undef FocusIn
enum QX11EmbedFocusInDetail {
XEMBED_FOCUS_CURRENT = 0,
XEMBED_FOCUS_FIRST = 1,
XEMBED_FOCUS_LAST = 2
};
enum QX11EmbedInfoFlags {
XEMBED_MAPPED = (1 << 0),
};
enum QX11EmbedMessageType {
XEMBED_EMBEDDED_NOTIFY = 0,
XEMBED_WINDOW_ACTIVATE = 1,
XEMBED_WINDOW_DEACTIVATE = 2,
XEMBED_REQUEST_FOCUS = 3,
XEMBED_FOCUS_IN = 4,
XEMBED_FOCUS_OUT = 5,
XEMBED_FOCUS_NEXT = 6,
XEMBED_FOCUS_PREV = 7,
XEMBED_MODALITY_ON = 10,
XEMBED_MODALITY_OFF = 11,
XEMBED_REGISTER_ACCELERATOR = 12,
XEMBED_UNREGISTER_ACCELERATOR = 13,
XEMBED_ACTIVATE_ACCELERATOR = 14
};
const quint32 XEMBED_VERSION = 0;
QXcbScreen *QXcbWindow::parentScreen()
{
return parent() ? static_cast<QXcbWindow*>(parent())->parentScreen() : xcbScreen();
}
//QPlatformWindow::screenForGeometry version that uses deviceIndependentGeometry
QXcbScreen *QXcbWindow::initialScreen() const
{
QWindowPrivate *windowPrivate = qt_window_private(window());
QScreen *screen = windowPrivate->screenForGeometry(window()->geometry());
return static_cast<QXcbScreen*>(screen->handle());
}
// Returns \c true if we should set WM_TRANSIENT_FOR on \a w
static inline bool isTransient(const QWindow *w)
{
return w->type() == Qt::Dialog
|| w->type() == Qt::Sheet
|| w->type() == Qt::Tool
|| w->type() == Qt::SplashScreen
|| w->type() == Qt::ToolTip
|| w->type() == Qt::Drawer
|| w->type() == Qt::Popup;
}
void QXcbWindow::setImageFormatForVisual(const xcb_visualtype_t *visual)
{
if (qt_xcb_imageFormatForVisual(connection(), m_depth, visual, &m_imageFormat, &m_imageRgbSwap))
return;
switch (m_depth) {
case 32:
case 24:
qWarning("Using RGB32 fallback, if this works your X11 server is reporting a bad screen format.");
m_imageFormat = QImage::Format_RGB32;
break;
case 16:
qWarning("Using RGB16 fallback, if this works your X11 server is reporting a bad screen format.");
m_imageFormat = QImage::Format_RGB16;
default:
break;
}
}
#if QT_CONFIG(xcb_xlib)
static inline XTextProperty* qstringToXTP(Display *dpy, const QString& s)
{
#include <X11/Xatom.h>
static XTextProperty tp = { nullptr, 0, 0, 0 };
static bool free_prop = true; // we can't free tp.value in case it references
// the data of the static QByteArray below.
if (tp.value) {
if (free_prop)
XFree(tp.value);
tp.value = nullptr;
free_prop = true;
}
#if QT_CONFIG(textcodec)
static const QTextCodec* mapper = QTextCodec::codecForLocale();
int errCode = 0;
if (mapper) {
QByteArray mapped = mapper->fromUnicode(s);
char* tl[2];
tl[0] = mapped.data();
tl[1] = nullptr;
errCode = XmbTextListToTextProperty(dpy, tl, 1, XStdICCTextStyle, &tp);
if (errCode < 0)
qCDebug(lcQpaXcb, "XmbTextListToTextProperty result code %d", errCode);
}
if (!mapper || errCode < 0) {
mapper = QTextCodec::codecForName("latin1");
if (!mapper || !mapper->canEncode(s))
return nullptr;
#endif
static QByteArray qcs;
qcs = s.toLatin1();
tp.value = (uchar*)qcs.data();
tp.encoding = XA_STRING;
tp.format = 8;
tp.nitems = qcs.length();
free_prop = false;
#if QT_CONFIG(textcodec)
}
#else
Q_UNUSED(dpy);
#endif
return &tp;
}
#endif // QT_CONFIG(xcb_xlib)
// TODO move this into a utility function in QWindow or QGuiApplication
static QWindow *childWindowAt(QWindow *win, const QPoint &p)
{
for (QObject *obj : win->children()) {
if (obj->isWindowType()) {
QWindow *childWin = static_cast<QWindow *>(obj);
if (childWin->isVisible()) {
if (QWindow *recurse = childWindowAt(childWin, p))
return recurse;
}
}
}
if (!win->isTopLevel()
&& !(win->flags() & Qt::WindowTransparentForInput)
&& win->geometry().contains(win->parent()->mapFromGlobal(p))) {
return win;
}
return nullptr;
}
static const char *wm_window_type_property_id = "_q_xcb_wm_window_type";
static const char *wm_window_role_property_id = "_q_xcb_wm_window_role";
QXcbWindow::QXcbWindow(QWindow *window)
: QPlatformWindow(window)
{
setConnection(xcbScreen()->connection());
}
enum : quint32 {
baseEventMask
= XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY
| XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_FOCUS_CHANGE,
defaultEventMask = baseEventMask
| XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE
| XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
| XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_POINTER_MOTION,
transparentForInputEventMask = baseEventMask
| XCB_EVENT_MASK_VISIBILITY_CHANGE | XCB_EVENT_MASK_RESIZE_REDIRECT
| XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
| XCB_EVENT_MASK_COLOR_MAP_CHANGE | XCB_EVENT_MASK_OWNER_GRAB_BUTTON
};
void QXcbWindow::create()
{
destroy();
m_windowState = Qt::WindowNoState;
Qt::WindowType type = window()->type();
QXcbScreen *currentScreen = xcbScreen();
QXcbScreen *platformScreen = parent() ? parentScreen() : initialScreen();
QRect rect = parent()
? QHighDpi::toNativeLocalPosition(window()->geometry(), platformScreen)
: QHighDpi::toNativePixels(window()->geometry(), platformScreen);
if (type == Qt::Desktop) {
m_window = platformScreen->root();
m_depth = platformScreen->screen()->root_depth;
m_visualId = platformScreen->screen()->root_visual;
const xcb_visualtype_t *visual = nullptr;
if (connection()->hasDefaultVisualId()) {
visual = platformScreen->visualForId(connection()->defaultVisualId());
if (visual)
m_visualId = connection()->defaultVisualId();
if (!visual)
qWarning("Could not use default visual id. Falling back to root_visual for screen.");
}
if (!visual)
visual = platformScreen->visualForId(m_visualId);
setImageFormatForVisual(visual);
connection()->addWindowEventListener(m_window, this);
return;
}
QPlatformWindow::setGeometry(rect);
if (platformScreen != currentScreen)
QWindowSystemInterface::handleWindowScreenChanged(window(), platformScreen->QPlatformScreen::screen());
const QSize minimumSize = windowMinimumSize();
if (rect.width() > 0 || rect.height() > 0) {
rect.setWidth(qBound(1, rect.width(), XCOORD_MAX));
rect.setHeight(qBound(1, rect.height(), XCOORD_MAX));
} else if (minimumSize.width() > 0 || minimumSize.height() > 0) {
rect.setSize(minimumSize);
} else {
rect.setWidth(QHighDpi::toNativePixels(int(defaultWindowWidth), platformScreen->QPlatformScreen::screen()));
rect.setHeight(QHighDpi::toNativePixels(int(defaultWindowHeight), platformScreen->QPlatformScreen::screen()));
}
xcb_window_t xcb_parent_id = platformScreen->root();
if (parent()) {
xcb_parent_id = static_cast<QXcbWindow *>(parent())->xcb_window();
m_embedded = parent()->isForeignWindow();
QSurfaceFormat parentFormat = parent()->window()->requestedFormat();
if (window()->surfaceType() != QSurface::OpenGLSurface && parentFormat.hasAlpha()) {
window()->setFormat(parentFormat);
}
}
resolveFormat(platformScreen->surfaceFormatFor(window()->requestedFormat()));
const xcb_visualtype_t *visual = nullptr;
if (connection()->hasDefaultVisualId()) {
visual = platformScreen->visualForId(connection()->defaultVisualId());
if (!visual)
qWarning() << "Failed to use requested visual id.";
}
if (!visual)
visual = createVisual();
if (!visual) {
qWarning() << "Falling back to using screens root_visual.";
visual = platformScreen->visualForId(platformScreen->screen()->root_visual);
}
Q_ASSERT(visual);
m_visualId = visual->visual_id;
m_depth = platformScreen->depthOfVisual(m_visualId);
setImageFormatForVisual(visual);
quint32 mask = XCB_CW_BACK_PIXMAP
| XCB_CW_BORDER_PIXEL
| XCB_CW_BIT_GRAVITY
| XCB_CW_OVERRIDE_REDIRECT
| XCB_CW_SAVE_UNDER
| XCB_CW_EVENT_MASK;
static auto haveOpenGL = []() {
static const bool result = QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::OpenGL);
return result;
};
if ((window()->supportsOpenGL() && haveOpenGL()) || m_format.hasAlpha()) {
m_cmap = xcb_generate_id(xcb_connection());
xcb_create_colormap(xcb_connection(),
XCB_COLORMAP_ALLOC_NONE,
m_cmap,
xcb_parent_id,
m_visualId);
mask |= XCB_CW_COLORMAP;
}
quint32 values[] = {
XCB_BACK_PIXMAP_NONE,
platformScreen->screen()->black_pixel,
XCB_GRAVITY_NORTH_WEST,
type == Qt::Popup || type == Qt::ToolTip || (window()->flags() & Qt::BypassWindowManagerHint),
type == Qt::Popup || type == Qt::Tool || type == Qt::SplashScreen || type == Qt::ToolTip || type == Qt::Drawer,
defaultEventMask,
m_cmap
};
m_window = xcb_generate_id(xcb_connection());
xcb_create_window(xcb_connection(),
m_depth,
m_window, // window id
xcb_parent_id, // parent window id
rect.x(),
rect.y(),
rect.width(),
rect.height(),
0, // border width
XCB_WINDOW_CLASS_INPUT_OUTPUT, // window class
m_visualId, // visual
mask,
values);
connection()->addWindowEventListener(m_window, this);
propagateSizeHints();
xcb_atom_t properties[5];
int propertyCount = 0;
properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
properties[propertyCount++] = atom(QXcbAtom::WM_TAKE_FOCUS);
properties[propertyCount++] = atom(QXcbAtom::_NET_WM_PING);
if (connection()->hasXSync())
properties[propertyCount++] = atom(QXcbAtom::_NET_WM_SYNC_REQUEST);
if (window()->flags() & Qt::WindowContextHelpButtonHint)
properties[propertyCount++] = atom(QXcbAtom::_NET_WM_CONTEXT_HELP);
xcb_change_property(xcb_connection(),
XCB_PROP_MODE_REPLACE,
m_window,
atom(QXcbAtom::WM_PROTOCOLS),
XCB_ATOM_ATOM,
32,
propertyCount,
properties);
m_syncValue.hi = 0;
m_syncValue.lo = 0;
const QByteArray wmClass = QXcbIntegration::instance()->wmClass();
if (!wmClass.isEmpty()) {
xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE,
m_window, atom(QXcbAtom::WM_CLASS),
XCB_ATOM_STRING, 8, wmClass.size(), wmClass.constData());
}
if (connection()->hasXSync()) {
m_syncCounter = xcb_generate_id(xcb_connection());
xcb_sync_create_counter(xcb_connection(), m_syncCounter, m_syncValue);
xcb_change_property(xcb_connection(),
XCB_PROP_MODE_REPLACE,
m_window,
atom(QXcbAtom::_NET_WM_SYNC_REQUEST_COUNTER),
XCB_ATOM_CARDINAL,
32,
1,
&m_syncCounter);
}
// set the PID to let the WM kill the application if unresponsive
quint32 pid = getpid();
xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
atom(QXcbAtom::_NET_WM_PID), XCB_ATOM_CARDINAL, 32,
1, &pid);
const QByteArray clientMachine = QSysInfo::machineHostName().toLocal8Bit();
if (!clientMachine.isEmpty()) {
xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
atom(QXcbAtom::WM_CLIENT_MACHINE), XCB_ATOM_STRING, 8,
clientMachine.size(), clientMachine.constData());
}
// Create WM_HINTS property on the window, so we can xcb_icccm_get_wm_hints*()
// from various setter functions for adjusting the hints.
xcb_icccm_wm_hints_t hints;
memset(&hints, 0, sizeof(hints));
hints.flags = XCB_ICCCM_WM_HINT_WINDOW_GROUP;
hints.window_group = connection()->clientLeader();
xcb_icccm_set_wm_hints(xcb_connection(), m_window, &hints);
xcb_window_t leader = connection()->clientLeader();
xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
atom(QXcbAtom::WM_CLIENT_LEADER), XCB_ATOM_WINDOW, 32,
1, &leader);
/* Add XEMBED info; this operation doesn't initiate the embedding. */
quint32 data[] = { XEMBED_VERSION, XEMBED_MAPPED };
xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
atom(QXcbAtom::_XEMBED_INFO),
atom(QXcbAtom::_XEMBED_INFO),
32, 2, (void *)data);
if (connection()->hasXInput2()) {
connection()->xi2SelectDeviceEvents(m_window);
}
setWindowState(window()->windowStates());
setWindowFlags(window()->flags());
setWindowTitle(window()->title());
#if QT_CONFIG(xcb_xlib)
// force sync to read outstanding requests - see QTBUG-29106
XSync(static_cast<Display*>(platformScreen->connection()->xlib_display()), false);
#endif
const qreal opacity = qt_window_private(window())->opacity;
if (!qFuzzyCompare(opacity, qreal(1.0)))
setOpacity(opacity);
setMask(QHighDpi::toNativeLocalRegion(window()->mask(), window()));
if (window()->isTopLevel())
setWindowIcon(window()->icon());
if (window()->dynamicPropertyNames().contains(wm_window_role_property_id)) {
QByteArray wmWindowRole = window()->property(wm_window_role_property_id).toByteArray();
setWmWindowRole(wmWindowRole);
}
}
QXcbWindow::~QXcbWindow()
{
destroy();
}
QXcbForeignWindow::~QXcbForeignWindow()
{
// Clear window so that destroy() does not affect it
m_window = 0;
if (connection()->mouseGrabber() == this)
connection()->setMouseGrabber(nullptr);
}
void QXcbWindow::destroy()
{
if (connection()->focusWindow() == this)
doFocusOut();
if (connection()->mouseGrabber() == this)
connection()->setMouseGrabber(nullptr);
if (m_syncCounter && connection()->hasXSync())
xcb_sync_destroy_counter(xcb_connection(), m_syncCounter);
if (m_window) {
if (m_netWmUserTimeWindow) {
xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_NET_WM_USER_TIME_WINDOW));
// Some window managers, like metacity, do XSelectInput on the _NET_WM_USER_TIME_WINDOW window,
// without trapping BadWindow (which crashes when the user time window is destroyed).
connection()->sync();
xcb_destroy_window(xcb_connection(), m_netWmUserTimeWindow);
m_netWmUserTimeWindow = XCB_NONE;
}
connection()->removeWindowEventListener(m_window);
xcb_destroy_window(xcb_connection(), m_window);
m_window = 0;
}
if (m_cmap) {
xcb_free_colormap(xcb_connection(), m_cmap);
}
m_mapped = false;
if (m_pendingSyncRequest)
m_pendingSyncRequest->invalidate();
}
void QXcbWindow::setGeometry(const QRect &rect)
{
QPlatformWindow::setGeometry(rect);
propagateSizeHints();
QXcbScreen *currentScreen = xcbScreen();
QXcbScreen *newScreen = parent() ? parentScreen() : static_cast<QXcbScreen*>(screenForGeometry(rect));
if (!newScreen)
newScreen = xcbScreen();
if (newScreen != currentScreen)
QWindowSystemInterface::handleWindowScreenChanged(window(), newScreen->QPlatformScreen::screen());
if (qt_window_private(window())->positionAutomatic) {
const quint32 mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
const qint32 values[] = {
qBound<qint32>(1, rect.width(), XCOORD_MAX),
qBound<qint32>(1, rect.height(), XCOORD_MAX),
};
xcb_configure_window(xcb_connection(), m_window, mask, reinterpret_cast<const quint32*>(values));
} else {
const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
const qint32 values[] = {
qBound<qint32>(-XCOORD_MAX, rect.x(), XCOORD_MAX),
qBound<qint32>(-XCOORD_MAX, rect.y(), XCOORD_MAX),
qBound<qint32>(1, rect.width(), XCOORD_MAX),
qBound<qint32>(1, rect.height(), XCOORD_MAX),
};
xcb_configure_window(xcb_connection(), m_window, mask, reinterpret_cast<const quint32*>(values));
if (window()->parent() && !window()->transientParent()) {
// Wait for server reply for parented windows to ensure that a few window
// moves will come as a one event. This is important when native widget is
// moved a few times in X and Y directions causing native scroll. Widget
// must get single event to not trigger unwanted widget position changes
// and then expose events causing backingstore flushes with incorrect
// offset causing image crruption.
connection()->sync();
}
}
xcb_flush(xcb_connection());
}
QMargins QXcbWindow::frameMargins() const
{
if (m_dirtyFrameMargins) {
if (connection()->wmSupport()->isSupportedByWM(atom(QXcbAtom::_NET_FRAME_EXTENTS))) {
auto reply = Q_XCB_REPLY(xcb_get_property, xcb_connection(), false, m_window,
atom(QXcbAtom::_NET_FRAME_EXTENTS), XCB_ATOM_CARDINAL, 0, 4);
if (reply && reply->type == XCB_ATOM_CARDINAL && reply->format == 32 && reply->value_len == 4) {
quint32 *data = (quint32 *)xcb_get_property_value(reply.get());
// _NET_FRAME_EXTENTS format is left, right, top, bottom
m_frameMargins = QMargins(data[0], data[2], data[1], data[3]);
m_dirtyFrameMargins = false;
return m_frameMargins;
}
}
// _NET_FRAME_EXTENTS property is not available, so
// walk up the window tree to get the frame parent
xcb_window_t window = m_window;
xcb_window_t parent = m_window;
bool foundRoot = false;
const QVector<xcb_window_t> &virtualRoots =
connection()->wmSupport()->virtualRoots();
while (!foundRoot) {
auto reply = Q_XCB_REPLY_UNCHECKED(xcb_query_tree, xcb_connection(), parent);
if (reply) {
if (reply->root == reply->parent || virtualRoots.indexOf(reply->parent) != -1 || reply->parent == XCB_WINDOW_NONE) {
foundRoot = true;
} else {
window = parent;
parent = reply->parent;
}
} else {
m_dirtyFrameMargins = false;
m_frameMargins = QMargins();
return m_frameMargins;
}
}
QPoint offset;
auto reply = Q_XCB_REPLY(xcb_translate_coordinates, xcb_connection(), window, parent, 0, 0);
if (reply) {
offset = QPoint(reply->dst_x, reply->dst_y);
}
auto geom = Q_XCB_REPLY(xcb_get_geometry, xcb_connection(), parent);
if (geom) {
// --
// add the border_width for the window managers frame... some window managers
// do not use a border_width of zero for their frames, and if we the left and
// top strut, we ensure that pos() is absolutely correct. frameGeometry()
// will still be incorrect though... perhaps i should have foffset as well, to
// indicate the frame offset (equal to the border_width on X).
// - Brad
// -- copied from qwidget_x11.cpp
int left = offset.x() + geom->border_width;
int top = offset.y() + geom->border_width;
int right = geom->width + geom->border_width - geometry().width() - offset.x();
int bottom = geom->height + geom->border_width - geometry().height() - offset.y();
m_frameMargins = QMargins(left, top, right, bottom);
}
m_dirtyFrameMargins = false;
}
return m_frameMargins;
}
void QXcbWindow::setVisible(bool visible)
{
if (visible)
show();
else
hide();
}
void QXcbWindow::show()
{
if (window()->isTopLevel()) {
// update WM_NORMAL_HINTS
propagateSizeHints();
// update WM_TRANSIENT_FOR
xcb_window_t transientXcbParent = 0;
if (isTransient(window())) {
const QWindow *tp = window()->transientParent();
if (tp && tp->handle())
transientXcbParent = static_cast<const QXcbWindow *>(tp->handle())->winId();
// Default to client leader if there is no transient parent, else modal dialogs can
// be hidden by their parents.
if (!transientXcbParent)
transientXcbParent = connection()->clientLeader();
if (transientXcbParent) { // ICCCM 4.1.2.6
xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, m_window,
XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 32,
1, &transientXcbParent);
}
}
if (!transientXcbParent)
xcb_delete_property(xcb_connection(), m_window, XCB_ATOM_WM_TRANSIENT_FOR);
// update _NET_WM_STATE
setNetWmStateOnUnmappedWindow();
}
// QWidget-attribute Qt::WA_ShowWithoutActivating.
const auto showWithoutActivating = window()->property("_q_showWithoutActivating");
if (showWithoutActivating.isValid() && showWithoutActivating.toBool())
updateNetWmUserTime(0);
else if (connection()->time() != XCB_TIME_CURRENT_TIME)
updateNetWmUserTime(connection()->time());
xcb_map_window(xcb_connection(), m_window);
if (QGuiApplication::modalWindow() == window())
requestActivateWindow();
xcbScreen()->windowShown(this);
connection()->sync();
}
void QXcbWindow::hide()
{
xcb_unmap_window(xcb_connection(), m_window);
// send synthetic UnmapNotify event according to icccm 4.1.4
q_padded_xcb_event<xcb_unmap_notify_event_t> event = {};
event.response_type = XCB_UNMAP_NOTIFY;
event.event = xcbScreen()->root();
event.window = m_window;
event.from_configure = false;
xcb_send_event(xcb_connection(), false, xcbScreen()->root(),
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&event);
xcb_flush(xcb_connection());
if (connection()->mouseGrabber() == this)
connection()->setMouseGrabber(nullptr);
m_mapped = false;
// Hiding a modal window doesn't send an enter event to its transient parent when the
// mouse is already over the parent window, so the enter event must be emulated.
if (window()->isModal()) {
// Get the cursor position at modal window screen
const QPoint nativePos = xcbScreen()->cursor()->pos();
const QPoint cursorPos = QHighDpi::fromNativePixels(nativePos, xcbScreen()->screenForPosition(nativePos)->screen());
// Find the top level window at cursor position.
// Don't use QGuiApplication::topLevelAt(): search only the virtual siblings of this window's screen
QWindow *enterWindow = nullptr;
const auto screens = xcbScreen()->virtualSiblings();
for (QPlatformScreen *screen : screens) {
if (screen->geometry().contains(cursorPos)) {
const QPoint devicePosition = QHighDpi::toNativePixels(cursorPos, screen->screen());
enterWindow = screen->topLevelAt(devicePosition);
break;
}
}
if (enterWindow && enterWindow != window()) {
// Find the child window at cursor position, otherwise use the top level window
if (QWindow *childWindow = childWindowAt(enterWindow, cursorPos))
enterWindow = childWindow;
const QPoint localPos = enterWindow->mapFromGlobal(cursorPos);
QWindowSystemInterface::handleEnterEvent(enterWindow,
localPos * QHighDpiScaling::factor(enterWindow),
nativePos);
}
}
}
bool QXcbWindow::relayFocusToModalWindow() const
{
QWindow *w = static_cast<QWindowPrivate *>(QObjectPrivate::get(window()))->eventReceiver();
// get top-level window
while (w && w->parent())
w = w->parent();
QWindow *modalWindow = nullptr;
const bool blocked = QGuiApplicationPrivate::instance()->isWindowBlocked(w, &modalWindow);
if (blocked && modalWindow != w) {
modalWindow->requestActivate();
connection()->flush();
return true;
}
return false;
}
void QXcbWindow::doFocusIn()
{
if (relayFocusToModalWindow())
return;
QWindow *w = static_cast<QWindowPrivate *>(QObjectPrivate::get(window()))->eventReceiver();
connection()->setFocusWindow(w);
QWindowSystemInterface::handleWindowActivated(w, Qt::ActiveWindowFocusReason);
}
void QXcbWindow::doFocusOut()
{
connection()->setFocusWindow(nullptr);
relayFocusToModalWindow();
// Do not set the active window to nullptr if there is a FocusIn coming.
connection()->focusInTimer().start();
}
struct QtMotifWmHints {
quint32 flags, functions, decorations;
qint32 input_mode; // unused
quint32 status; // unused
};
enum {
MWM_HINTS_FUNCTIONS = (1L << 0),
MWM_FUNC_ALL = (1L << 0),
MWM_FUNC_RESIZE = (1L << 1),
MWM_FUNC_MOVE = (1L << 2),
MWM_FUNC_MINIMIZE = (1L << 3),
MWM_FUNC_MAXIMIZE = (1L << 4),
MWM_FUNC_CLOSE = (1L << 5),
MWM_HINTS_DECORATIONS = (1L << 1),
MWM_DECOR_ALL = (1L << 0),
MWM_DECOR_BORDER = (1L << 1),
MWM_DECOR_RESIZEH = (1L << 2),
MWM_DECOR_TITLE = (1L << 3),
MWM_DECOR_MENU = (1L << 4),
MWM_DECOR_MINIMIZE = (1L << 5),
MWM_DECOR_MAXIMIZE = (1L << 6),
};
QXcbWindow::NetWmStates QXcbWindow::netWmStates()
{
NetWmStates result;
auto reply = Q_XCB_REPLY_UNCHECKED(xcb_get_property, xcb_connection(),
0, m_window, atom(QXcbAtom::_NET_WM_STATE),
XCB_ATOM_ATOM, 0, 1024);
if (reply && reply->format == 32 && reply->type == XCB_ATOM_ATOM) {
const xcb_atom_t *states = static_cast<const xcb_atom_t *>(xcb_get_property_value(reply.get()));
const xcb_atom_t *statesEnd = states + reply->length;
if (statesEnd != std::find(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_ABOVE)))
result |= NetWmStateAbove;
if (statesEnd != std::find(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_BELOW)))
result |= NetWmStateBelow;
if (statesEnd != std::find(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_FULLSCREEN)))
result |= NetWmStateFullScreen;
if (statesEnd != std::find(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_HORZ)))
result |= NetWmStateMaximizedHorz;
if (statesEnd != std::find(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_MAXIMIZED_VERT)))
result |= NetWmStateMaximizedVert;
if (statesEnd != std::find(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_MODAL)))
result |= NetWmStateModal;
if (statesEnd != std::find(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_STAYS_ON_TOP)))
result |= NetWmStateStaysOnTop;
if (statesEnd != std::find(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_DEMANDS_ATTENTION)))
result |= NetWmStateDemandsAttention;
if (statesEnd != std::find(states, statesEnd, atom(QXcbAtom::_NET_WM_STATE_HIDDEN)))
result |= NetWmStateHidden;
} else {
qCDebug(lcQpaXcb, "getting net wm state (%x), empty\n", m_window);
}
return result;
}
void QXcbWindow::setWindowFlags(Qt::WindowFlags flags)
{
Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
if (type == Qt::ToolTip)
flags |= Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint;
if (type == Qt::Popup)
flags |= Qt::X11BypassWindowManagerHint;
const quint32 mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
const quint32 values[] = {
// XCB_CW_OVERRIDE_REDIRECT
(flags & Qt::BypassWindowManagerHint) ? 1u : 0,
// XCB_CW_EVENT_MASK
(flags & Qt::WindowTransparentForInput) ? transparentForInputEventMask : defaultEventMask
};
xcb_change_window_attributes(xcb_connection(), xcb_window(), mask, values);
QXcbWindowFunctions::WmWindowTypes wmWindowTypes;
if (window()->dynamicPropertyNames().contains(wm_window_type_property_id)) {
wmWindowTypes = static_cast<QXcbWindowFunctions::WmWindowTypes>(
qvariant_cast<int>(window()->property(wm_window_type_property_id)));
}
setWmWindowType(wmWindowTypes, flags);
setNetWmState(flags);
setMotifWmHints(flags);
updateDoesNotAcceptFocus(flags & Qt::WindowDoesNotAcceptFocus);
}
void QXcbWindow::setMotifWmHints(Qt::WindowFlags flags)
{
Qt::WindowType type = static_cast<Qt::WindowType>(int(flags & Qt::WindowType_Mask));
QtMotifWmHints mwmhints;
memset(&mwmhints, 0, sizeof(mwmhints));
if (type != Qt::SplashScreen) {
mwmhints.flags |= MWM_HINTS_DECORATIONS;
bool customize = flags & Qt::CustomizeWindowHint;
if (type == Qt::Window && !customize) {
const Qt::WindowFlags defaultFlags = Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint;
if (!(flags & defaultFlags))
flags |= defaultFlags;
}
if (!(flags & Qt::FramelessWindowHint) && !(customize && !(flags & Qt::WindowTitleHint))) {
mwmhints.decorations |= MWM_DECOR_BORDER;
mwmhints.decorations |= MWM_DECOR_RESIZEH;
mwmhints.decorations |= MWM_DECOR_TITLE;
if (flags & Qt::WindowSystemMenuHint)
mwmhints.decorations |= MWM_DECOR_MENU;
if (flags & Qt::WindowMinimizeButtonHint) {
mwmhints.decorations |= MWM_DECOR_MINIMIZE;
mwmhints.functions |= MWM_FUNC_MINIMIZE;
}
if (flags & Qt::WindowMaximizeButtonHint) {
mwmhints.decorations |= MWM_DECOR_MAXIMIZE;
mwmhints.functions |= MWM_FUNC_MAXIMIZE;
}
if (flags & Qt::WindowCloseButtonHint)
mwmhints.functions |= MWM_FUNC_CLOSE;
}
} else {
// if type == Qt::SplashScreen
mwmhints.decorations = MWM_DECOR_ALL;
}
if (mwmhints.functions != 0) {
mwmhints.flags |= MWM_HINTS_FUNCTIONS;
mwmhints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
} else {
mwmhints.functions = MWM_FUNC_ALL;
}
if (!(flags & Qt::FramelessWindowHint)
&& flags & Qt::CustomizeWindowHint
&& flags & Qt::WindowTitleHint
&& !(flags &
(Qt::WindowMinimizeButtonHint
| Qt::WindowMaximizeButtonHint
| Qt::WindowCloseButtonHint)))
{
// a special case - only the titlebar without any button
mwmhints.flags = MWM_HINTS_FUNCTIONS;
mwmhints.functions = MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
mwmhints.decorations = 0;
}
if (mwmhints.flags) {
xcb_change_property(xcb_connection(),
XCB_PROP_MODE_REPLACE,
m_window,
atom(QXcbAtom::_MOTIF_WM_HINTS),
atom(QXcbAtom::_MOTIF_WM_HINTS),
32,
5,
&mwmhints);
} else {
xcb_delete_property(xcb_connection(), m_window, atom(QXcbAtom::_MOTIF_WM_HINTS));
}
}
void QXcbWindow::setNetWmState(bool set, xcb_atom_t one, xcb_atom_t two)
{
xcb_client_message_event_t event;
event.response_type = XCB_CLIENT_MESSAGE;
event.format = 32;
event.sequence = 0;
event.window = m_window;
event.type = atom(QXcbAtom::_NET_WM_STATE);
event.data.data32[0] = set ? 1 : 0;
event.data.data32[1] = one;
event.data.data32[2] = two;
event.data.data32[3] = 0;
event.data.data32[4] = 0;
xcb_send_event(xcb_connection(), 0, xcbScreen()->root(),
XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
(const char *)&event);
}
void QXcbWindow::setNetWmState(Qt::WindowStates state)
{
if ((m_windowState ^ state) & Qt::WindowMaximized) {