1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 #include "qxcbconnection_basic.h"
40 #include "qxcbbackingstore.h" // for createSystemVShmSegment()
41 
42 #include <xcb/randr.h>
43 #include <xcb/shm.h>
44 #include <xcb/sync.h>
45 #include <xcb/xfixes.h>
46 #include <xcb/xinerama.h>
47 #include <xcb/render.h>
48 #include <xcb/xinput.h>
49 #define explicit dont_use_cxx_explicit
50 #include <xcb/xkb.h>
51 #undef explicit
52 
53 #if QT_CONFIG(xcb_xlib)
54 #define register        /* C++17 deprecated register */
55 #include <X11/Xlib.h>
56 #include <X11/Xlib-xcb.h>
57 #include <X11/Xlibint.h>
58 #include <X11/Xutil.h>
59 #undef register
60 #endif
61 
62 QT_BEGIN_NAMESPACE
63 
64 Q_LOGGING_CATEGORY(lcQpaXcb, "qt.qpa.xcb")
65 
66 #if QT_CONFIG(xcb_xlib)
67 static const char * const xcbConnectionErrors[] = {
68     "No error", /* Error 0 */
69     "I/O error", /* XCB_CONN_ERROR */
70     "Unsupported extension used", /* XCB_CONN_CLOSED_EXT_NOTSUPPORTED */
71     "Out of memory", /* XCB_CONN_CLOSED_MEM_INSUFFICIENT */
72     "Maximum allowed requested length exceeded", /* XCB_CONN_CLOSED_REQ_LEN_EXCEED */
73     "Failed to parse display string", /* XCB_CONN_CLOSED_PARSE_ERR */
74     "No such screen on display", /* XCB_CONN_CLOSED_INVALID_SCREEN */
75     "Error during FD passing" /* XCB_CONN_CLOSED_FDPASSING_FAILED */
76 };
77 
nullErrorHandler(Display * dpy,XErrorEvent * err)78 static int nullErrorHandler(Display *dpy, XErrorEvent *err)
79 {
80 #ifndef Q_XCB_DEBUG
81     Q_UNUSED(dpy);
82     Q_UNUSED(err);
83 #else
84     const int buflen = 1024;
85     char buf[buflen];
86 
87     XGetErrorText(dpy, err->error_code, buf, buflen);
88     fprintf(stderr, "X Error: serial %lu error %d %s\n", err->serial, (int) err->error_code, buf);
89 #endif
90     return 0;
91 }
92 
ioErrorHandler(Display * dpy)93 static int ioErrorHandler(Display *dpy)
94 {
95     xcb_connection_t *conn = XGetXCBConnection(dpy);
96     if (conn != nullptr) {
97         /* Print a message with a textual description of the error */
98         int code = xcb_connection_has_error(conn);
99         const char *str = "Unknown error";
100         int arrayLength = sizeof(xcbConnectionErrors) / sizeof(xcbConnectionErrors[0]);
101         if (code >= 0 && code < arrayLength)
102             str = xcbConnectionErrors[code];
103 
104         qWarning("The X11 connection broke: %s (code %d)", str, code);
105     }
106     return _XDefaultIOError(dpy);
107 }
108 #endif
109 
QXcbBasicConnection(const char * displayName)110 QXcbBasicConnection::QXcbBasicConnection(const char *displayName)
111     : m_displayName(displayName ? QByteArray(displayName) : qgetenv("DISPLAY"))
112 {
113 #if QT_CONFIG(xcb_xlib)
114     Display *dpy = XOpenDisplay(m_displayName.constData());
115     if (dpy) {
116         m_primaryScreenNumber = DefaultScreen(dpy);
117         m_xcbConnection = XGetXCBConnection(dpy);
118         XSetEventQueueOwner(dpy, XCBOwnsEventQueue);
119         XSetErrorHandler(nullErrorHandler);
120         XSetIOErrorHandler(ioErrorHandler);
121         m_xlibDisplay = dpy;
122     }
123 #else
124     m_xcbConnection = xcb_connect(m_displayName.constData(), &m_primaryScreenNumber);
125 #endif
126     if (Q_UNLIKELY(!isConnected())) {
127         qCWarning(lcQpaXcb, "could not connect to display %s", m_displayName.constData());
128         return;
129     }
130 
131     m_setup = xcb_get_setup(m_xcbConnection);
132     m_xcbAtom.initialize(m_xcbConnection);
133     m_maximumRequestLength = xcb_get_maximum_request_length(m_xcbConnection);
134 
135     xcb_extension_t *extensions[] = {
136         &xcb_shm_id, &xcb_xfixes_id, &xcb_randr_id, &xcb_shape_id, &xcb_sync_id,
137         &xcb_render_id, &xcb_xkb_id, &xcb_input_id, nullptr
138     };
139 
140     for (xcb_extension_t **ext_it = extensions; *ext_it; ++ext_it)
141         xcb_prefetch_extension_data (m_xcbConnection, *ext_it);
142 
143     initializeXSync();
144     if (!qEnvironmentVariableIsSet("QT_XCB_NO_MITSHM"))
145         initializeShm();
146     if (!qEnvironmentVariableIsSet("QT_XCB_NO_XRANDR"))
147         initializeXRandr();
148     if (!m_hasXRandr)
149         initializeXinerama();
150     initializeXFixes();
151     initializeXRender();
152     if (!qEnvironmentVariableIsSet("QT_XCB_NO_XI2"))
153         initializeXInput2();
154     initializeXShape();
155     initializeXKB();
156 }
157 
~QXcbBasicConnection()158 QXcbBasicConnection::~QXcbBasicConnection()
159 {
160     if (isConnected()) {
161 #if QT_CONFIG(xcb_xlib)
162         XCloseDisplay(static_cast<Display *>(m_xlibDisplay));
163 #else
164         xcb_disconnect(m_xcbConnection);
165 #endif
166     }
167 }
168 
maxRequestDataBytes(size_t requestSize) const169 size_t QXcbBasicConnection::maxRequestDataBytes(size_t requestSize) const
170 {
171     if (hasBigRequest())
172         requestSize += 4; // big-request encoding adds 4 bytes
173 
174     return m_maximumRequestLength * 4 - requestSize;
175 }
176 
internAtom(const char * name)177 xcb_atom_t QXcbBasicConnection::internAtom(const char *name)
178 {
179     if (!name || *name == 0)
180         return XCB_NONE;
181 
182     return Q_XCB_REPLY(xcb_intern_atom, m_xcbConnection, false, strlen(name), name)->atom;
183 }
184 
atomName(xcb_atom_t atom)185 QByteArray QXcbBasicConnection::atomName(xcb_atom_t atom)
186 {
187     if (!atom)
188         return QByteArray();
189 
190     auto reply = Q_XCB_REPLY(xcb_get_atom_name, m_xcbConnection, atom);
191     if (reply)
192         return QByteArray(xcb_get_atom_name_name(reply.get()), xcb_get_atom_name_name_length(reply.get()));
193 
194     qCWarning(lcQpaXcb) << "atomName: bad atom" << atom;
195     return QByteArray();
196 }
197 
hasBigRequest() const198 bool QXcbBasicConnection::hasBigRequest() const
199 {
200     return m_maximumRequestLength > m_setup->maximum_request_length;
201 }
202 
203 // Starting from the xcb version 1.9.3 struct xcb_ge_event_t has changed:
204 // - "pad0" became "extension"
205 // - "pad1" and "pad" became "pad0"
206 // New and old version of this struct share the following fields:
207 typedef struct qt_xcb_ge_event_t {
208     uint8_t  response_type;
209     uint8_t  extension;
210     uint16_t sequence;
211     uint32_t length;
212     uint16_t event_type;
213 } qt_xcb_ge_event_t;
214 
isXIEvent(xcb_generic_event_t * event) const215 bool QXcbBasicConnection::isXIEvent(xcb_generic_event_t *event) const
216 {
217     qt_xcb_ge_event_t *e = reinterpret_cast<qt_xcb_ge_event_t *>(event);
218     return e->extension == m_xiOpCode;
219 }
220 
isXIType(xcb_generic_event_t * event,uint16_t type) const221 bool QXcbBasicConnection::isXIType(xcb_generic_event_t *event, uint16_t type) const
222 {
223     if (!isXIEvent(event))
224         return false;
225 
226     auto *e = reinterpret_cast<qt_xcb_ge_event_t *>(event);
227     return e->event_type == type;
228 }
229 
isXFixesType(uint responseType,int eventType) const230 bool QXcbBasicConnection::isXFixesType(uint responseType, int eventType) const
231 {
232     return m_hasXFixes && responseType == m_xfixesFirstEvent + eventType;
233 }
234 
isXRandrType(uint responseType,int eventType) const235 bool QXcbBasicConnection::isXRandrType(uint responseType, int eventType) const
236 {
237     return m_hasXRandr && responseType == m_xrandrFirstEvent + eventType;
238 }
239 
isXkbType(uint responseType) const240 bool QXcbBasicConnection::isXkbType(uint responseType) const
241 {
242     return m_hasXkb && responseType == m_xkbFirstEvent;
243 }
244 
initializeXSync()245 void QXcbBasicConnection::initializeXSync()
246 {
247     const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_xcbConnection, &xcb_sync_id);
248     if (!reply || !reply->present)
249         return;
250 
251     m_hasXSync = true;
252 }
253 
initializeShm()254 void QXcbBasicConnection::initializeShm()
255 {
256     const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_xcbConnection, &xcb_shm_id);
257     if (!reply || !reply->present) {
258         qCDebug(lcQpaXcb, "MIT-SHM extension is not present on the X server");
259         return;
260     }
261 
262     auto shmQuery = Q_XCB_REPLY(xcb_shm_query_version, m_xcbConnection);
263     if (!shmQuery) {
264         qCWarning(lcQpaXcb, "failed to request MIT-SHM version");
265         return;
266     }
267 
268     m_hasShm = true;
269     m_hasShmFd = (shmQuery->major_version == 1 && shmQuery->minor_version >= 2) ||
270                   shmQuery->major_version > 1;
271 
272     qCDebug(lcQpaXcb) << "Has MIT-SHM     :" << m_hasShm;
273     qCDebug(lcQpaXcb) << "Has MIT-SHM FD  :" << m_hasShmFd;
274 
275     // Temporary disable warnings (unless running in debug mode).
276     auto logging = const_cast<QLoggingCategory*>(&lcQpaXcb());
277     bool wasEnabled = logging->isEnabled(QtMsgType::QtWarningMsg);
278     if (!logging->isEnabled(QtMsgType::QtDebugMsg))
279         logging->setEnabled(QtMsgType::QtWarningMsg, false);
280     if (!QXcbBackingStore::createSystemVShmSegment(m_xcbConnection)) {
281         qCDebug(lcQpaXcb, "failed to create System V shared memory segment (remote "
282                           "X11 connection?), disabling SHM");
283         m_hasShm = m_hasShmFd = false;
284     }
285     if (wasEnabled)
286         logging->setEnabled(QtMsgType::QtWarningMsg, true);
287 }
288 
initializeXRender()289 void QXcbBasicConnection::initializeXRender()
290 {
291     const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_xcbConnection, &xcb_render_id);
292     if (!reply || !reply->present) {
293         qCDebug(lcQpaXcb, "XRender extension not present on the X server");
294         return;
295     }
296 
297     auto xrenderQuery = Q_XCB_REPLY(xcb_render_query_version, m_xcbConnection,
298                                      XCB_RENDER_MAJOR_VERSION,
299                                      XCB_RENDER_MINOR_VERSION);
300     if (!xrenderQuery) {
301         qCWarning(lcQpaXcb, "xcb_render_query_version failed");
302         return;
303     }
304 
305     m_hasXRender = true;
306     m_xrenderVersion.first = xrenderQuery->major_version;
307     m_xrenderVersion.second = xrenderQuery->minor_version;
308 }
309 
initializeXinerama()310 void QXcbBasicConnection::initializeXinerama()
311 {
312     const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_xcbConnection, &xcb_xinerama_id);
313     if (!reply || !reply->present)
314         return;
315 
316     auto xineramaActive = Q_XCB_REPLY(xcb_xinerama_is_active, m_xcbConnection);
317     if (xineramaActive && xineramaActive->state)
318         m_hasXinerama = true;
319 }
320 
initializeXFixes()321 void QXcbBasicConnection::initializeXFixes()
322 {
323     const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_xcbConnection, &xcb_xfixes_id);
324     if (!reply || !reply->present)
325         return;
326 
327     auto xfixesQuery = Q_XCB_REPLY(xcb_xfixes_query_version, m_xcbConnection,
328                                     XCB_XFIXES_MAJOR_VERSION,
329                                     XCB_XFIXES_MINOR_VERSION);
330     if (!xfixesQuery || xfixesQuery->major_version < 2) {
331         qCWarning(lcQpaXcb, "failed to initialize XFixes");
332         return;
333     }
334 
335     m_hasXFixes = true;
336     m_xfixesFirstEvent = reply->first_event;
337 }
338 
initializeXRandr()339 void QXcbBasicConnection::initializeXRandr()
340 {
341     const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_xcbConnection, &xcb_randr_id);
342     if (!reply || !reply->present)
343         return;
344 
345     auto xrandrQuery = Q_XCB_REPLY(xcb_randr_query_version, m_xcbConnection,
346                                     XCB_RANDR_MAJOR_VERSION,
347                                     XCB_RANDR_MINOR_VERSION);
348     if (!xrandrQuery || (xrandrQuery->major_version < 1 ||
349                         (xrandrQuery->major_version == 1 && xrandrQuery->minor_version < 2))) {
350         qCWarning(lcQpaXcb, "failed to initialize XRandr");
351         return;
352     }
353 
354     m_hasXRandr = true;
355     m_xrandrFirstEvent = reply->first_event;
356 }
357 
initializeXInput2()358 void QXcbBasicConnection::initializeXInput2()
359 {
360     const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_xcbConnection, &xcb_input_id);
361     if (!reply || !reply->present) {
362         qCDebug(lcQpaXcb, "XInput extension is not present on the X server");
363         return;
364     }
365 
366     auto xinputQuery = Q_XCB_REPLY(xcb_input_xi_query_version, m_xcbConnection, 2, 2);
367     if (!xinputQuery || xinputQuery->major_version != 2) {
368         qCWarning(lcQpaXcb, "X server does not support XInput 2");
369         return;
370     }
371 
372     qCDebug(lcQpaXcb, "Using XInput version %d.%d",
373             xinputQuery->major_version, xinputQuery->minor_version);
374 
375     m_xi2Enabled = true;
376     m_xiOpCode = reply->major_opcode;
377     m_xinputFirstEvent = reply->first_event;
378     m_xi2Minor = xinputQuery->minor_version;
379 }
380 
initializeXShape()381 void QXcbBasicConnection::initializeXShape()
382 {
383     const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_xcbConnection, &xcb_shape_id);
384     if (!reply || !reply->present)
385         return;
386 
387     m_hasXhape = true;
388 
389     auto shapeQuery = Q_XCB_REPLY(xcb_shape_query_version, m_xcbConnection);
390     if (!shapeQuery) {
391         qCWarning(lcQpaXcb, "failed to initialize XShape extension");
392         return;
393     }
394 
395     if (shapeQuery->major_version > 1 || (shapeQuery->major_version == 1 && shapeQuery->minor_version >= 1)) {
396         // The input shape is the only thing added in SHAPE 1.1
397         m_hasInputShape = true;
398     }
399 }
400 
initializeXKB()401 void QXcbBasicConnection::initializeXKB()
402 {
403     const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_xcbConnection, &xcb_xkb_id);
404     if (!reply || !reply->present) {
405         qCWarning(lcQpaXcb, "XKeyboard extension not present on the X server");
406         return;
407     }
408 
409     int wantMajor = 1;
410     int wantMinor = 0;
411     auto xkbQuery = Q_XCB_REPLY(xcb_xkb_use_extension, m_xcbConnection, wantMajor, wantMinor);
412     if (!xkbQuery) {
413         qCWarning(lcQpaXcb, "failed to initialize XKeyboard extension");
414         return;
415     }
416     if (!xkbQuery->supported) {
417         qCWarning(lcQpaXcb, "unsupported XKB version (we want %d.%d, but X server has %d.%d)",
418                   wantMajor, wantMinor, xkbQuery->serverMajor, xkbQuery->serverMinor);
419         return;
420     }
421 
422     m_hasXkb = true;
423     m_xkbFirstEvent = reply->first_event;
424 }
425 
426 QT_END_NAMESPACE
427