| 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 | |
| 55 | QT_BEGIN_NAMESPACE |
| 56 | |
| 57 | Q_DECLARE_LOGGING_CATEGORY(lcQpaXcb) |
| 58 | |
| 59 | class Q_XCB_EXPORT QXcbBasicConnection : public QObject |
| 60 | { |
| 61 | Q_OBJECT |
| 62 | public: |
| 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(c: 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(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(x: major, y: 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 | bool isAtLeastXI21() const { return m_xi2Enabled && m_xi2Minor >= 1; } |
| 103 | bool isAtLeastXI22() const { return m_xi2Enabled && m_xi2Minor >= 2; } |
| 104 | bool isXIEvent(xcb_generic_event_t *event) const; |
| 105 | bool isXIType(xcb_generic_event_t *event, uint16_t type) const; |
| 106 | |
| 107 | bool isXFixesType(uint responseType, int eventType) const; |
| 108 | bool isXRandrType(uint responseType, int eventType) const; |
| 109 | bool isXkbType(uint responseType) const; // https://bugs.freedesktop.org/show_bug.cgi?id=51295 |
| 110 | |
| 111 | protected: |
| 112 | void initializeShm(); |
| 113 | void initializeXFixes(); |
| 114 | void initializeXRender(); |
| 115 | void initializeXRandr(); |
| 116 | void initializeXinerama(); |
| 117 | void initializeXShape(); |
| 118 | void initializeXKB(); |
| 119 | void initializeXSync(); |
| 120 | void initializeXInput2(); |
| 121 | |
| 122 | private: |
| 123 | #if QT_CONFIG(xcb_xlib) |
| 124 | void *m_xlibDisplay = nullptr; |
| 125 | #endif |
| 126 | QByteArray m_displayName; |
| 127 | xcb_connection_t *m_xcbConnection = nullptr; |
| 128 | int m_primaryScreenNumber = 0; |
| 129 | const xcb_setup_t *m_setup = nullptr; |
| 130 | QXcbAtom m_xcbAtom; |
| 131 | |
| 132 | bool m_hasXFixes = false; |
| 133 | bool m_hasXinerama = false; |
| 134 | bool m_hasXhape = false; |
| 135 | bool m_hasInputShape; |
| 136 | bool m_hasXRandr = false; |
| 137 | bool m_hasXkb = false; |
| 138 | bool m_hasXRender = false; |
| 139 | bool m_hasShm = false; |
| 140 | bool m_hasShmFd = false; |
| 141 | bool m_hasXSync = false; |
| 142 | |
| 143 | QPair<int, int> m_xrenderVersion; |
| 144 | |
| 145 | bool m_xi2Enabled = false; |
| 146 | int m_xi2Minor = -1; |
| 147 | int m_xiOpCode = -1; |
| 148 | uint32_t m_xinputFirstEvent = 0; |
| 149 | |
| 150 | uint32_t m_xfixesFirstEvent = 0; |
| 151 | uint32_t m_xrandrFirstEvent = 0; |
| 152 | uint32_t m_xkbFirstEvent = 0; |
| 153 | |
| 154 | uint32_t m_maximumRequestLength = 0; |
| 155 | }; |
| 156 | |
| 157 | #define Q_XCB_REPLY_CONNECTION_ARG(connection, ...) connection |
| 158 | |
| 159 | struct QStdFreeDeleter { |
| 160 | void operator()(void *p) const noexcept { return std::free(ptr: p); } |
| 161 | }; |
| 162 | |
| 163 | #define Q_XCB_REPLY(call, ...) \ |
| 164 | std::unique_ptr<call##_reply_t, QStdFreeDeleter>( \ |
| 165 | call##_reply(Q_XCB_REPLY_CONNECTION_ARG(__VA_ARGS__), call(__VA_ARGS__), nullptr) \ |
| 166 | ) |
| 167 | |
| 168 | #define Q_XCB_REPLY_UNCHECKED(call, ...) \ |
| 169 | std::unique_ptr<call##_reply_t, QStdFreeDeleter>( \ |
| 170 | call##_reply(Q_XCB_REPLY_CONNECTION_ARG(__VA_ARGS__), call##_unchecked(__VA_ARGS__), nullptr) \ |
| 171 | ) |
| 172 | |
| 173 | QT_END_NAMESPACE |
| 174 | |
| 175 | #endif // QXCBBASICCONNECTION_H |
| 176 | |