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#ifndef QXCBBASICCONNECTION_H
40#define QXCBBASICCONNECTION_H
41
42#include "qxcbatom.h"
43#include "qxcbexport.h"
44
45#include <QtCore/QPair>
46#include <QtCore/QObject>
47#include <QtCore/QByteArray>
48#include <QtCore/QLoggingCategory>
49#include <QtGui/private/qtguiglobal_p.h>
50
51#include <xcb/xcb.h>
52
53#include <memory>
54
55QT_BEGIN_NAMESPACE
56
57Q_DECLARE_LOGGING_CATEGORY(lcQpaXcb)
58
59class Q_XCB_EXPORT QXcbBasicConnection : public QObject
60{
61 Q_OBJECT
62public:
63 QXcbBasicConnection(const char *displayName);
64 ~QXcbBasicConnection();
65
66#if QT_CONFIG(xcb_xlib)
67 void *xlib_display() const { return m_xlibDisplay; }
68#endif
69 const char *displayName() const { return m_displayName.constData(); }
70 int primaryScreenNumber() const { return m_primaryScreenNumber; }
71 xcb_connection_t *xcb_connection() const { return m_xcbConnection; }
72 bool isConnected() const {
73 return m_xcbConnection && !xcb_connection_has_error(m_xcbConnection);
74 }
75 const xcb_setup_t *setup() const { return m_setup; }
76
77 size_t maxRequestDataBytes(size_t requestSize) const;
78
79 inline xcb_atom_t atom(QXcbAtom::Atom qatom) const { return m_xcbAtom.atom(qatom); }
80 QXcbAtom::Atom qatom(xcb_atom_t atom) const { return m_xcbAtom.qatom(atom); }
81 xcb_atom_t internAtom(const char *name);
82 QByteArray atomName(xcb_atom_t atom);
83
84 bool hasXFixes() const { return m_hasXFixes; }
85 bool hasXShape() const { return m_hasXhape; }
86 bool hasXRandr() const { return m_hasXRandr; }
87 bool hasInputShape() const { return m_hasInputShape; }
88 bool hasXKB() const { return m_hasXkb; }
89 bool hasXRender(int major = -1, int minor = -1) const {
90 if (m_hasXRender && major != -1 && minor != -1)
91 return m_xrenderVersion >= qMakePair(major, minor);
92
93 return m_hasXRender;
94 }
95 bool hasXInput2() const { return m_xi2Enabled; }
96 bool hasShm() const { return m_hasShm; }
97 bool hasShmFd() const { return m_hasShmFd; }
98 bool hasXSync() const { return m_hasXSync; }
99 bool hasXinerama() const { return m_hasXinerama; }
100 bool hasBigRequest() const;
101
102#if QT_CONFIG(xcb_xinput)
103 bool isAtLeastXI21() const { return m_xi2Enabled && m_xi2Minor >= 1; }
104 bool isAtLeastXI22() const { return m_xi2Enabled && m_xi2Minor >= 2; }
105 bool isXIEvent(xcb_generic_event_t *event) const;
106 bool isXIType(xcb_generic_event_t *event, uint16_t type) const;
107#endif
108
109 bool isXFixesType(uint responseType, int eventType) const;
110 bool isXRandrType(uint responseType, int eventType) const;
111 bool isXkbType(uint responseType) const; // https://bugs.freedesktop.org/show_bug.cgi?id=51295
112
113protected:
114 void initializeShm();
115 void initializeXFixes();
116 void initializeXRender();
117 void initializeXRandr();
118 void initializeXinerama();
119 void initializeXShape();
120 void initializeXKB();
121 void initializeXSync();
122#if QT_CONFIG(xcb_xinput)
123 void initializeXInput2();
124#endif
125
126private:
127#if QT_CONFIG(xcb_xlib)
128 void *m_xlibDisplay = nullptr;
129#endif
130 QByteArray m_displayName;
131 xcb_connection_t *m_xcbConnection = nullptr;
132 int m_primaryScreenNumber = 0;
133 const xcb_setup_t *m_setup = nullptr;
134 QXcbAtom m_xcbAtom;
135
136 bool m_hasXFixes = false;
137 bool m_hasXinerama = false;
138 bool m_hasXhape = false;
139 bool m_hasInputShape;
140 bool m_hasXRandr = false;
141 bool m_hasXkb = false;
142 bool m_hasXRender = false;
143 bool m_hasShm = false;
144 bool m_hasShmFd = false;
145 bool m_hasXSync = false;
146
147 QPair<int, int> m_xrenderVersion;
148
149 bool m_xi2Enabled = false;
150#if QT_CONFIG(xcb_xinput)
151 int m_xi2Minor = -1;
152 int m_xiOpCode = -1;
153 uint32_t m_xinputFirstEvent = 0;
154#endif
155
156 uint32_t m_xfixesFirstEvent = 0;
157 uint32_t m_xrandrFirstEvent = 0;
158 uint32_t m_xkbFirstEvent = 0;
159
160 uint32_t m_maximumRequestLength = 0;
161};
162
163#define Q_XCB_REPLY_CONNECTION_ARG(connection, ...) connection
164
165struct QStdFreeDeleter {
166 void operator()(void *p) const noexcept { return std::free(p); }
167};
168
169#define Q_XCB_REPLY(call, ...) \
170 std::unique_ptr<call##_reply_t, QStdFreeDeleter>( \
171 call##_reply(Q_XCB_REPLY_CONNECTION_ARG(__VA_ARGS__), call(__VA_ARGS__), nullptr) \
172 )
173
174#define Q_XCB_REPLY_UNCHECKED(call, ...) \
175 std::unique_ptr<call##_reply_t, QStdFreeDeleter>( \
176 call##_reply(Q_XCB_REPLY_CONNECTION_ARG(__VA_ARGS__), call##_unchecked(__VA_ARGS__), nullptr) \
177 )
178
179QT_END_NAMESPACE
180
181#endif // QXCBBASICCONNECTION_H
182