-
Notifications
You must be signed in to change notification settings - Fork 0
/
qxcbintegration.cpp
460 lines (402 loc) · 14.4 KB
/
qxcbintegration.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
/****************************************************************************
**
** 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 "qxcbintegration.h"
#include "qxcbconnection.h"
#include "qxcbscreen.h"
#include "qxcbwindow.h"
#include "qxcbcursor.h"
#include "qxcbkeyboard.h"
#include "qxcbbackingstore.h"
#include "qxcbnativeinterface.h"
#include "qxcbclipboard.h"
#include "qxcbeventqueue.h"
#include "qxcbeventdispatcher.h"
#ifndef QT_NO_SESSIONMANAGER
#include "qxcbsessionmanager.h"
#endif
#include <xcb/xcb.h>
#include <QtFontDatabaseSupport/private/qgenericunixfontdatabase_p.h>
#include <QtServiceSupport/private/qgenericunixservices_p.h>
#include <stdio.h>
#include <QtGui/private/qguiapplication_p.h>
#if QT_CONFIG(xcb_xlib)
#define register /* C++17 deprecated register */
#include <X11/Xlib.h>
#undef register
#endif
#include <qpa/qplatforminputcontextfactory_p.h>
#include <private/qgenericunixthemes_p.h>
#include <qpa/qplatforminputcontext.h>
#include <QtGui/QOpenGLContext>
#include <QtGui/QScreen>
#include <QtGui/QOffscreenSurface>
#ifndef QT_NO_ACCESSIBILITY
#include <qpa/qplatformaccessibility.h>
#ifndef QT_NO_ACCESSIBILITY_ATSPI_BRIDGE
#include <QtLinuxAccessibilitySupport/private/bridge_p.h>
#endif
#endif
#include <QtCore/QFileInfo>
QT_BEGIN_NAMESPACE
// Find out if our parent process is gdb by looking at the 'exe' symlink under /proc,.
// or, for older Linuxes, read out 'cmdline'.
static bool runningUnderDebugger()
{
#if defined(QT_DEBUG) && defined(Q_OS_LINUX)
const QString parentProc = QLatin1String("/proc/") + QString::number(getppid());
const QFileInfo parentProcExe(parentProc + QLatin1String("/exe"));
if (parentProcExe.isSymLink())
return parentProcExe.symLinkTarget().endsWith(QLatin1String("/gdb"));
QFile f(parentProc + QLatin1String("/cmdline"));
if (!f.open(QIODevice::ReadOnly))
return false;
QByteArray s;
char c;
while (f.getChar(&c) && c) {
if (c == '/')
s.clear();
else
s += c;
}
return s == "gdb";
#else
return false;
#endif
}
QXcbIntegration *QXcbIntegration::m_instance = nullptr;
QXcbIntegration::QXcbIntegration(const QStringList ¶meters, int &argc, char **argv)
: m_services(new QGenericUnixServices)
, m_instanceName(nullptr)
, m_canGrab(true)
, m_defaultVisualId(UINT_MAX)
{
m_instance = this;
qApp->setAttribute(Qt::AA_CompressHighFrequencyEvents, true);
QWindowSystemInterface::setPlatformFiltersEvents(true);
qRegisterMetaType<QXcbWindow*>();
#if QT_CONFIG(xcb_xlib)
XInitThreads();
#endif
m_nativeInterface.reset(new QXcbNativeInterface);
// Parse arguments
const char *displayName = nullptr;
bool noGrabArg = false;
bool doGrabArg = false;
if (argc) {
int j = 1;
for (int i = 1; i < argc; i++) {
QByteArray arg(argv[i]);
if (arg.startsWith("--"))
arg.remove(0, 1);
if (arg == "-display" && i < argc - 1)
displayName = argv[++i];
else if (arg == "-name" && i < argc - 1)
m_instanceName = argv[++i];
else if (arg == "-nograb")
noGrabArg = true;
else if (arg == "-dograb")
doGrabArg = true;
else if (arg == "-visual" && i < argc - 1) {
bool ok = false;
m_defaultVisualId = QByteArray(argv[++i]).toUInt(&ok, 0);
if (!ok)
m_defaultVisualId = UINT_MAX;
}
else
argv[j++] = argv[i];
}
argc = j;
} // argc
bool underDebugger = runningUnderDebugger();
if (noGrabArg && doGrabArg && underDebugger) {
qWarning("Both -nograb and -dograb command line arguments specified. Please pick one. -nograb takes prcedence");
doGrabArg = false;
}
#if defined(QT_DEBUG)
if (!noGrabArg && !doGrabArg && underDebugger) {
qCDebug(lcQpaXcb, "Qt: gdb: -nograb added to command-line options.\n"
"\t Use the -dograb option to enforce grabbing.");
}
#endif
m_canGrab = (!underDebugger && !noGrabArg) || (underDebugger && doGrabArg);
static bool canNotGrabEnv = qEnvironmentVariableIsSet("QT_XCB_NO_GRAB_SERVER");
if (canNotGrabEnv)
m_canGrab = false;
const int numParameters = parameters.size();
m_connections.reserve(1 + numParameters / 2);
auto conn = new QXcbConnection(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, displayName);
if (!conn->isConnected()) {
delete conn;
return;
}
m_connections << conn;
// ### Qt 6 (QTBUG-52408) remove this multi-connection code path
for (int i = 0; i < numParameters - 1; i += 2) {
qCDebug(lcQpaXcb) << "connecting to additional display: " << parameters.at(i) << parameters.at(i+1);
QString display = parameters.at(i) + QLatin1Char(':') + parameters.at(i+1);
conn = new QXcbConnection(m_nativeInterface.data(), m_canGrab, m_defaultVisualId, display.toLatin1().constData());
if (conn->isConnected())
m_connections << conn;
else
delete conn;
}
m_fontDatabase.reset(new QGenericUnixFontDatabase());
}
QXcbIntegration::~QXcbIntegration()
{
qDeleteAll(m_connections);
m_instance = nullptr;
}
QPlatformPixmap *QXcbIntegration::createPlatformPixmap(QPlatformPixmap::PixelType type) const
{
return QPlatformIntegration::createPlatformPixmap(type);
}
QPlatformWindow *QXcbIntegration::createPlatformWindow(QWindow *window) const
{
Q_ASSERT(window->type() == Qt::Desktop); // for VNC
QXcbWindow *xcbWindow = new QXcbWindow(window);
xcbWindow->create();
return xcbWindow;
}
QPlatformWindow *QXcbIntegration::createForeignWindow(QWindow *window, WId nativeHandle) const
{
return new QXcbForeignWindow(window, nativeHandle);
}
#ifndef QT_NO_OPENGL
QPlatformOpenGLContext *QXcbIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
{
return nullptr;
}
#endif
QPlatformBackingStore *QXcbIntegration::createPlatformBackingStore(QWindow *window) const
{
return new QXcbBackingStore(window);
}
QPlatformOffscreenSurface *QXcbIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const
{
return nullptr;
}
bool QXcbIntegration::hasCapability(QPlatformIntegration::Capability cap) const
{
switch (cap) {
case OpenGL:
case ThreadedOpenGL:
case RasterGLSurface:
return false;
case ThreadedPixmaps:
case WindowMasks:
case MultipleWindows:
case ForeignWindows:
case SyncState:
case SwitchableWidgetComposition:
return true;
default: return QPlatformIntegration::hasCapability(cap);
}
}
QAbstractEventDispatcher *QXcbIntegration::createEventDispatcher() const
{
return QXcbEventDispatcher::createEventDispatcher(defaultConnection());
}
void QXcbIntegration::initialize()
{
const QLatin1String defaultInputContext("compose");
// Perform everything that may potentially need the event dispatcher (timers, socket
// notifiers) here instead of the constructor.
QString icStr = QPlatformInputContextFactory::requested();
if (icStr.isNull())
icStr = defaultInputContext;
m_inputContext.reset(QPlatformInputContextFactory::create(icStr));
if (!m_inputContext && icStr != defaultInputContext && icStr != QLatin1String("none"))
m_inputContext.reset(QPlatformInputContextFactory::create(defaultInputContext));
defaultConnection()->keyboard()->initialize();
}
void QXcbIntegration::moveToScreen(QWindow *window, int screen)
{
Q_UNUSED(window);
Q_UNUSED(screen);
}
QPlatformFontDatabase *QXcbIntegration::fontDatabase() const
{
return m_fontDatabase.data();
}
QPlatformNativeInterface * QXcbIntegration::nativeInterface() const
{
return m_nativeInterface.data();
}
#ifndef QT_NO_CLIPBOARD
QPlatformClipboard *QXcbIntegration::clipboard() const
{
return m_connections.at(0)->clipboard();
}
#endif
QPlatformInputContext *QXcbIntegration::inputContext() const
{
return m_inputContext.data();
}
#ifndef QT_NO_ACCESSIBILITY
QPlatformAccessibility *QXcbIntegration::accessibility() const
{
#if !defined(QT_NO_ACCESSIBILITY_ATSPI_BRIDGE)
if (!m_accessibility) {
Q_ASSERT_X(QCoreApplication::eventDispatcher(), "QXcbIntegration",
"Initializing accessibility without event-dispatcher!");
m_accessibility.reset(new QSpiAccessibleBridge());
}
#endif
return m_accessibility.data();
}
#endif
QPlatformServices *QXcbIntegration::services() const
{
return m_services.data();
}
QList<int> QXcbIntegration::possibleKeys(const QKeyEvent *e) const
{
return m_connections.at(0)->keyboard()->possibleKeys(e);
}
QStringList QXcbIntegration::themeNames() const
{
return QGenericUnixTheme::themeNames();
}
QPlatformTheme *QXcbIntegration::createPlatformTheme(const QString &name) const
{
return QGenericUnixTheme::createUnixTheme(name);
}
QVariant QXcbIntegration::styleHint(QPlatformIntegration::StyleHint hint) const
{
switch (hint) {
case QPlatformIntegration::CursorFlashTime:
case QPlatformIntegration::KeyboardInputInterval:
case QPlatformIntegration::MouseDoubleClickInterval:
case QPlatformIntegration::StartDragTime:
case QPlatformIntegration::KeyboardAutoRepeatRate:
case QPlatformIntegration::PasswordMaskDelay:
case QPlatformIntegration::StartDragVelocity:
case QPlatformIntegration::UseRtlExtensions:
case QPlatformIntegration::PasswordMaskCharacter:
// TODO using various xcb, gnome or KDE settings
break; // Not implemented, use defaults
case QPlatformIntegration::StartDragDistance: {
// The default (in QPlatformTheme::defaultThemeHint) is 10 pixels, but
// on a high-resolution screen it makes sense to increase it.
qreal dpi = 100.0;
if (const QXcbScreen *screen = defaultConnection()->primaryScreen()) {
if (screen->logicalDpi().first > dpi)
dpi = screen->logicalDpi().first;
if (screen->logicalDpi().second > dpi)
dpi = screen->logicalDpi().second;
}
return 10.0 * dpi / 100.0;
}
case QPlatformIntegration::ShowIsFullScreen:
// X11 always has support for windows, but the
// window manager could prevent it (e.g. matchbox)
return false;
case QPlatformIntegration::ReplayMousePressOutsidePopup:
return false;
default:
break;
}
return QPlatformIntegration::styleHint(hint);
}
static QString argv0BaseName()
{
QString result;
const QStringList arguments = QCoreApplication::arguments();
if (!arguments.isEmpty() && !arguments.front().isEmpty()) {
result = arguments.front();
const int lastSlashPos = result.lastIndexOf(QLatin1Char('/'));
if (lastSlashPos != -1)
result.remove(0, lastSlashPos + 1);
}
return result;
}
static const char resourceNameVar[] = "RESOURCE_NAME";
QByteArray QXcbIntegration::wmClass() const
{
if (m_wmClass.isEmpty()) {
// Instance name according to ICCCM 4.1.2.5
QString name;
if (m_instanceName)
name = QString::fromLocal8Bit(m_instanceName);
if (name.isEmpty() && qEnvironmentVariableIsSet(resourceNameVar))
name = QString::fromLocal8Bit(qgetenv(resourceNameVar));
if (name.isEmpty())
name = argv0BaseName();
// Note: QCoreApplication::applicationName() cannot be called from the QGuiApplication constructor,
// hence this delayed initialization.
QString className = QCoreApplication::applicationName();
if (className.isEmpty()) {
className = argv0BaseName();
if (!className.isEmpty() && className.at(0).isLower())
className[0] = className.at(0).toUpper();
}
if (!name.isEmpty() && !className.isEmpty())
m_wmClass = std::move(name).toLocal8Bit() + '\0' + std::move(className).toLocal8Bit() + '\0';
}
return m_wmClass;
}
QPlatformSessionManager *QXcbIntegration::createPlatformSessionManager(const QString &id, const QString &key) const
{
return new QXcbSessionManager(id, key);
}
void QXcbIntegration::sync()
{
for (int i = 0; i < m_connections.size(); i++) {
m_connections.at(i)->sync();
}
}
// For QApplication::beep()
void QXcbIntegration::beep() const
{
QScreen *priScreen = QGuiApplication::primaryScreen();
if (!priScreen)
return;
QPlatformScreen *screen = priScreen->handle();
if (!screen)
return;
xcb_connection_t *connection = static_cast<QXcbScreen *>(screen)->xcb_connection();
xcb_bell(connection, 0);
xcb_flush(connection);
}
bool QXcbIntegration::nativePaintingEnabled() const
{
return false;
}
QT_END_NAMESPACE