Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtx11extras.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2016 Richard Moore <rich@kde.org>
3// Copyright (C) 2016 David Faure <david.faure@kdab.com>
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5// Qt-Security score:significant reason:default
6
8
9#include <qpa/qplatformnativeinterface.h>
10#include <qpa/qplatformwindow.h>
11#include <qpa/qplatformscreen_p.h>
12#include <qpa/qplatformscreen.h>
13#include <qscreen.h>
14#include <qwindow.h>
15#include <qguiapplication.h>
16#include <xcb/xcb.h>
17#include <QtCore/qdebug.h>
18
20
21using namespace Qt::StringLiterals;
22
23static QScreen *findScreenForVirtualDesktop(int virtualDesktopNumber)
24{
25 const auto screens = QGuiApplication::screens();
26 for (QScreen *screen : screens) {
27 auto *qxcbScreen = dynamic_cast<QNativeInterface::Private::QXcbScreen *>(screen->handle());
28 if (qxcbScreen && qxcbScreen->virtualDesktopNumber() == virtualDesktopNumber)
29 return screen;
30 }
31 return nullptr;
32}
33
34/*!
35 \class QX11Info
36 \inmodule QtGui
37 \since 6.2
38 \internal
39
40 \brief Provides information about the X display configuration.
41
42 The class provides two APIs: a set of non-static functions that
43 provide information about a specific widget or pixmap, and a set
44 of static functions that provide the default information for the
45 application.
46
47 \warning This class is only available on X11. For querying
48 per-screen information in a portable way, use QScreen.
49*/
50
51/*!
52 Constructs an empty QX11Info object.
53*/
54QX11Info::QX11Info()
55{
56}
57
58/*!
59 Returns true if the application is currently running on X11.
60
61 \since 6.2
62 */
63bool QX11Info::isPlatformX11()
64{
65 return QGuiApplication::platformName() == "xcb"_L1;
66}
67
68/*!
69 Returns the horizontal resolution of the given \a screen in terms of the
70 number of dots per inch.
71
72 The \a screen argument is an X screen number. Be aware that if
73 the user's system uses Xinerama (as opposed to traditional X11
74 multiscreen), there is only one X screen. Use QScreen to
75 query for information about Xinerama screens.
76
77 \sa appDpiY()
78*/
79int QX11Info::appDpiX(int screen)
80{
81 if (screen == -1) {
82 const QScreen *scr = QGuiApplication::primaryScreen();
83 if (!scr)
84 return 75;
85 return qRound(scr->logicalDotsPerInchX());
86 }
87
88 QScreen *scr = findScreenForVirtualDesktop(screen);
89 if (!scr)
90 return 0;
91
92 return scr->logicalDotsPerInchX();
93}
94
95/*!
96 Returns the vertical resolution of the given \a screen in terms of the
97 number of dots per inch.
98
99 The \a screen argument is an X screen number. Be aware that if
100 the user's system uses Xinerama (as opposed to traditional X11
101 multiscreen), there is only one X screen. Use QScreen to
102 query for information about Xinerama screens.
103
104 \sa appDpiX()
105*/
106int QX11Info::appDpiY(int screen)
107{
108 if (screen == -1) {
109 const QScreen *scr = QGuiApplication::primaryScreen();
110 if (!scr)
111 return 75;
112 return qRound(scr->logicalDotsPerInchY());
113 }
114
115 QScreen *scr = findScreenForVirtualDesktop(screen);
116 if (!scr)
117 return 0;
118
119 return scr->logicalDotsPerInchY();
120}
121
122/*!
123 Returns a handle for the applications root window on the given \a screen.
124
125 The \a screen argument is an X screen number. Be aware that if
126 the user's system uses Xinerama (as opposed to traditional X11
127 multiscreen), there is only one X screen. Use QScreen to
128 query for information about Xinerama screens.
129*/
130quint32 QX11Info::appRootWindow(int screen)
131{
132 if (!qApp)
133 return 0;
134 QPlatformNativeInterface *native = qApp->platformNativeInterface();
135 if (!native)
136 return 0;
137 QScreen *scr = screen == -1 ? QGuiApplication::primaryScreen() : findScreenForVirtualDesktop(screen);
138 if (!scr)
139 return 0;
140 return static_cast<xcb_window_t>(reinterpret_cast<quintptr>(native->nativeResourceForScreen(QByteArrayLiteral("rootwindow"), scr)));
141}
142
143/*!
144 Returns the number of the screen where the application is being
145 displayed.
146
147 This method refers to screens in the original X11 meaning with a
148 different DISPLAY environment variable per screen.
149 This information is only useful if your application needs to know
150 on which X screen it is running.
151
152 In a typical multi-head configuration, multiple physical monitors
153 are combined in one X11 screen. This means this method returns the
154 same number for each of the physical monitors. In such a setup you
155 are interested in the monitor information as provided by the X11
156 RandR extension. This is available through QScreen.
157
158 \sa display()
159*/
160int QX11Info::appScreen()
161{
162 if (!qApp)
163 return 0;
164 QPlatformNativeInterface *native = qApp->platformNativeInterface();
165 if (!native)
166 return 0;
167 return reinterpret_cast<qintptr>(native->nativeResourceForIntegration(QByteArrayLiteral("x11screen")));
168}
169
170/*!
171 Returns the X11 time.
172
173 \sa setAppTime(), appUserTime()
174*/
175quint32 QX11Info::appTime()
176{
177 if (!qApp)
178 return 0;
179 QPlatformNativeInterface *native = qApp->platformNativeInterface();
180 if (!native)
181 return 0;
182 QScreen* screen = QGuiApplication::primaryScreen();
183 return static_cast<xcb_timestamp_t>(reinterpret_cast<quintptr>(native->nativeResourceForScreen("apptime", screen)));
184}
185
186/*!
187 Returns the X11 user time.
188
189 \sa setAppUserTime(), appTime()
190*/
191quint32 QX11Info::appUserTime()
192{
193 if (!qApp)
194 return 0;
195 QPlatformNativeInterface *native = qApp->platformNativeInterface();
196 if (!native)
197 return 0;
198 QScreen* screen = QGuiApplication::primaryScreen();
199 return static_cast<xcb_timestamp_t>(reinterpret_cast<quintptr>(native->nativeResourceForScreen("appusertime", screen)));
200}
201
202/*!
203 Sets the X11 time to the value specified by \a time.
204
205 \sa appTime(), setAppUserTime()
206*/
207void QX11Info::setAppTime(quint32 time)
208{
209 if (!qApp)
210 return;
211 QPlatformNativeInterface *native = qApp->platformNativeInterface();
212 if (!native)
213 return;
214 typedef void (*SetAppTimeFunc)(QScreen *, xcb_timestamp_t);
215 QScreen* screen = QGuiApplication::primaryScreen();
216 SetAppTimeFunc func = reinterpret_cast<SetAppTimeFunc>(reinterpret_cast<void *>(native->nativeResourceFunctionForScreen("setapptime")));
217 if (func)
218 func(screen, time);
219 else
220 qWarning("Internal error: QPA plugin doesn't implement setAppTime");
221}
222
223/*!
224 Sets the X11 user time as specified by \a time.
225
226 \sa appUserTime(), setAppTime()
227*/
228void QX11Info::setAppUserTime(quint32 time)
229{
230 if (!qApp)
231 return;
232 QPlatformNativeInterface *native = qApp->platformNativeInterface();
233 if (!native)
234 return;
235 typedef void (*SetAppUserTimeFunc)(QScreen *, xcb_timestamp_t);
236 QScreen* screen = QGuiApplication::primaryScreen();
237 SetAppUserTimeFunc func = reinterpret_cast<SetAppUserTimeFunc>(reinterpret_cast<void *>(native->nativeResourceFunctionForScreen("setappusertime")));
238 if (func)
239 func(screen, time);
240 else
241 qWarning("Internal error: QPA plugin doesn't implement setAppUserTime");
242}
243
244/*!
245 Fetches the current X11 time stamp from the X Server.
246
247 This method creates a property notify event and blocks till it is
248 received back from the X Server.
249*/
250quint32 QX11Info::getTimestamp()
251{
252 if (!qApp)
253 return 0;
254 QPlatformNativeInterface *native = qApp->platformNativeInterface();
255 if (!native)
256 return 0;
257 QScreen* screen = QGuiApplication::primaryScreen();
258 return static_cast<xcb_timestamp_t>(reinterpret_cast<quintptr>(native->nativeResourceForScreen("gettimestamp", screen)));
259}
260
261/*!
262 Returns the startup ID that will be used for the next window to be shown by this process.
263
264 After the next window is shown, the next startup ID will be empty.
265
266 http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt
267
268 \sa setNextStartupId()
269*/
270QByteArray QX11Info::nextStartupId()
271{
272 if (!qApp)
273 return QByteArray();
274 QPlatformNativeInterface *native = qApp->platformNativeInterface();
275 if (!native)
276 return QByteArray();
277 return static_cast<char *>(native->nativeResourceForIntegration("startupid"));
278}
279
280/*!
281 Sets the next startup ID to \a id.
282
283 This is the startup ID that will be used for the next window to be shown by this process.
284
285 The startup ID of the first window comes from the environment variable DESKTOP_STARTUP_ID.
286 This method is useful for subsequent windows, when the request comes from another process
287 (e.g. via DBus).
288
289 \sa nextStartupId()
290*/
291void QX11Info::setNextStartupId(const QByteArray &id)
292{
293 if (!qApp)
294 return;
295 QPlatformNativeInterface *native = qApp->platformNativeInterface();
296 if (!native)
297 return;
298 typedef void (*SetStartupIdFunc)(const char*);
299 SetStartupIdFunc func = reinterpret_cast<SetStartupIdFunc>(reinterpret_cast<void *>(native->nativeResourceFunctionForIntegration("setstartupid")));
300 if (func)
301 func(id.constData());
302 else
303 qWarning("Internal error: QPA plugin doesn't implement setStartupId");
304}
305
306/*!
307 Returns the default display for the application.
308
309 \sa appScreen()
310*/
311Display *QX11Info::display()
312{
313 if (!qApp)
314 return nullptr;
315 QPlatformNativeInterface *native = qApp->platformNativeInterface();
316 if (!native)
317 return nullptr;
318
319 void *display = native->nativeResourceForIntegration(QByteArray("display"));
320 return reinterpret_cast<Display *>(display);
321}
322
323/*!
324 Returns the default XCB connection for the application.
325
326 \sa display()
327*/
328xcb_connection_t *QX11Info::connection()
329{
330 if (!qApp)
331 return nullptr;
332 QPlatformNativeInterface *native = qApp->platformNativeInterface();
333 if (!native)
334 return nullptr;
335
336 void *connection = native->nativeResourceForIntegration(QByteArray("connection"));
337 return reinterpret_cast<xcb_connection_t *>(connection);
338}
339
340/*!
341 Returns true if there is a compositing manager running for the connection
342 attached to \a screen.
343
344 If \a screen equals -1, the application's primary screen is used.
345*/
346bool QX11Info::isCompositingManagerRunning(int screen)
347{
348 if (!qApp)
349 return false;
350 QPlatformNativeInterface *native = qApp->platformNativeInterface();
351 if (!native)
352 return false;
353
354 QScreen *scr = screen == -1 ? QGuiApplication::primaryScreen() : findScreenForVirtualDesktop(screen);
355 if (!scr) {
356 qWarning() << "isCompositingManagerRunning: Could not find screen number" << screen;
357 return false;
358 }
359
360 return native->nativeResourceForScreen(QByteArray("compositingEnabled"), scr);
361}
362
363/*!
364 Returns a new peeker id or -1 if some internal error has occurred.
365 Each peeker id is associated with an index in the buffered native
366 event queue.
367
368 For more details see QX11Info::PeekOption and peekEventQueue().
369
370 \sa peekEventQueue(), removePeekerId()
371*/
372qint32 QX11Info::generatePeekerId()
373{
374 if (!qApp)
375 return -1;
376 QPlatformNativeInterface *native = qApp->platformNativeInterface();
377 if (!native)
378 return -1;
379
380 typedef qint32 (*GeneratePeekerIdFunc)(void);
381 GeneratePeekerIdFunc generatepeekerid = reinterpret_cast<GeneratePeekerIdFunc>(
382 reinterpret_cast<void *>(native->nativeResourceFunctionForIntegration("generatepeekerid")));
383 if (!generatepeekerid) {
384 qWarning("Internal error: QPA plugin doesn't implement generatePeekerId");
385 return -1;
386 }
387
388 return generatepeekerid();
389}
390
391/*!
392 Removes \a peekerId, which was earlier obtained via generatePeekerId().
393
394 Returns \c true on success or \c false if unknown peeker id was
395 provided or some internal error has occurred.
396
397 \sa generatePeekerId()
398*/
399bool QX11Info::removePeekerId(qint32 peekerId)
400{
401 if (!qApp)
402 return false;
403 QPlatformNativeInterface *native = qApp->platformNativeInterface();
404 if (!native)
405 return false;
406
407 typedef bool (*RemovePeekerIdFunc)(qint32);
408 RemovePeekerIdFunc removePeekerId = reinterpret_cast<RemovePeekerIdFunc>(
409 reinterpret_cast<void *>(native->nativeResourceFunctionForIntegration("removepeekerid")));
410 if (!removePeekerId) {
411 qWarning("Internal error: QPA plugin doesn't implement removePeekerId");
412 return false;
413 }
414
415 return removePeekerId(peekerId);
416}
417
418/*!
419 \enum QX11Info::PeekOption
420 \brief An enum to tune the behavior of QX11Info::peekEventQueue().
421
422 \value PeekDefault
423 Peek from the beginning of the buffered native event queue. A peeker
424 id is optional with PeekDefault. If a peeker id is provided to
425 peekEventQueue() when using PeekDefault, then peeking starts from
426 the beginning of the queue, not from the cached index; thus, this
427 can be used to manually reset a cached index to peek from the start
428 of the queue. When this operation completes, the associated index
429 will be updated to the new position in the queue.
430
431 \value PeekFromCachedIndex
432 QX11Info::peekEventQueue() can optimize the peeking algorithm by
433 skipping events that it already has seen in earlier calls to
434 peekEventQueue(). When control returns to the main event loop,
435 which causes the buffered native event queue to be flushed to Qt's
436 event queue, the cached indices are marked invalid and will be
437 reset on the next access. The same is true if the program
438 explicitly flushes the buffered native event queue by
439 QCoreApplication::processEvents().
440*/
441
442/*!
443 \typedef QX11Info::PeekerCallback
444 Typedef for a pointer to a function with the following signature:
445
446 \code
447 bool (*PeekerCallback)(xcb_generic_event_t *event, void *peekerData);
448 \endcode
449
450 The \a event is a native XCB event.
451 The \a peekerData is a pointer to data, passed in via peekEventQueue().
452
453 Return \c true from this function to stop examining the buffered
454 native event queue or \c false to continue.
455
456 \note A non-capturing lambda can serve as a PeekerCallback.
457*/
458
459/*!
460 \brief Peek into the buffered XCB event queue.
461
462 You can call peekEventQueue() periodically, when your program is busy
463 performing a long-running operation, to peek into the buffered native
464 event queue. The more time the long-running operation blocks the
465 program from returning control to the main event loop, the more
466 events will accumulate in the buffered XCB event queue. Once control
467 returns to the main event loop these events will be flushed to Qt's
468 event queue, which is a separate event queue from the queue this
469 function is peeking into.
470
471 \note It is usually better to run CPU-intensive operations in a
472 non-GUI thread, instead of blocking the main event loop.
473
474 The buffered XCB event queue is populated from a non-GUI thread and
475 therefore might be ahead of the current GUI state. To handle native
476 events as they are processed by the GUI thread, see
477 QAbstractNativeEventFilter::nativeEventFilter().
478
479 The \a peeker is a callback function as documented in PeekerCallback.
480 The \a peekerData can be used to pass in arbitrary data to the \a
481 peeker callback.
482 The \a option is an enum that tunes the behavior of peekEventQueue().
483 The \a peekerId is used to track an index in the queue, for more
484 details see QX11Info::PeekOption. There can be several indices,
485 each tracked individually by a peeker id obtained via generatePeekerId().
486
487 This function returns \c true when the peeker has stopped the event
488 proccesing by returning \c true from the callback. If there were no
489 events in the buffered native event queue to peek at or all the
490 events have been processed by the peeker, this function returns \c
491 false.
492
493 \sa generatePeekerId(), QAbstractNativeEventFilter::nativeEventFilter()
494*/
495bool QX11Info::peekEventQueue(PeekerCallback peeker, void *peekerData, PeekOptions option,
496 qint32 peekerId)
497{
498 if (!peeker || !qApp)
499 return false;
500 QPlatformNativeInterface *native = qApp->platformNativeInterface();
501 if (!native)
502 return false;
503
504 typedef bool (*PeekEventQueueFunc)(PeekerCallback, void *, PeekOptions, qint32);
505 PeekEventQueueFunc peekeventqueue = reinterpret_cast<PeekEventQueueFunc>(
506 reinterpret_cast<void *>(native->nativeResourceFunctionForIntegration("peekeventqueue")));
507 if (!peekeventqueue) {
508 qWarning("Internal error: QPA plugin doesn't implement peekEventQueue");
509 return false;
510 }
511
512 return peekeventqueue(peeker, peekerData, option, peekerId);
513}
514
515QT_END_NAMESPACE
#define qApp
static QScreen * findScreenForVirtualDesktop(int virtualDesktopNumber)