1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#pragma once
5
6#include "qxcbatom.h"
7#include "qxcbexport.h"
8
9#include <QtCore/QObject>
10#include <QtCore/QByteArray>
11#include <QtCore/QLoggingCategory>
12#include <QtGui/private/qtguiglobal_p.h>
13
14#include <xcb/xcb.h>
15
16#include <memory>
17
18QT_BEGIN_NAMESPACE
19
20Q_DECLARE_LOGGING_CATEGORY(lcQpaXcb)
21
22class Q_XCB_EXPORT QXcbBasicConnection : public QObject
23{
24 Q_OBJECT
25public:
26 QXcbBasicConnection(const char *displayName);
27 ~QXcbBasicConnection();
28
29#if QT_CONFIG(xcb_xlib)
30 void *xlib_display() const { return m_xlibDisplay; }
31#endif
32 const char *displayName() const { return m_displayName.constData(); }
33 int primaryScreenNumber() const { return m_primaryScreenNumber; }
34 xcb_connection_t *xcb_connection() const { return m_xcbConnection; }
35 bool isConnected() const {
36 return m_xcbConnection && !xcb_connection_has_error(c: m_xcbConnection);
37 }
38 const xcb_setup_t *setup() const { return m_setup; }
39
40 size_t maxRequestDataBytes(size_t requestSize) const;
41
42 inline xcb_atom_t atom(QXcbAtom::Atom qatom) const { return m_xcbAtom.atom(atom: qatom); }
43 QXcbAtom::Atom qatom(xcb_atom_t atom) const { return m_xcbAtom.qatom(atom); }
44 xcb_atom_t internAtom(const char *name);
45 QByteArray atomName(xcb_atom_t atom);
46
47 bool hasXFixes() const { return m_hasXFixes; }
48 bool hasXShape() const { return m_hasXhape; }
49 bool hasXRandr() const { return m_hasXRandr; }
50 bool hasInputShape() const { return m_hasInputShape; }
51 bool hasXKB() const { return m_hasXkb; }
52 bool hasXRender(int major = -1, int minor = -1) const {
53 if (m_hasXRender && major != -1 && minor != -1)
54 return m_xrenderVersion >= std::pair(major, minor);
55
56 return m_hasXRender;
57 }
58 bool hasXInput2() const { return m_xi2Enabled; }
59 bool hasShm() const { return m_hasShm; }
60 bool hasShmFd() const { return m_hasShmFd; }
61 bool hasXSync() const { return m_hasXSync; }
62 bool hasBigRequest() const;
63
64 bool isAtLeastXRandR12() const { return m_hasXRandr && m_xrandr1Minor >= 2; }
65 bool isAtLeastXRandR15() const { return m_hasXRandr && m_xrandr1Minor >= 5; }
66
67 bool isAtLeastXI21() const { return m_xi2Enabled && m_xi2Minor >= 1; }
68 bool isAtLeastXI22() const { return m_xi2Enabled && m_xi2Minor >= 2; }
69 bool isAtLeastXI24() const { return m_xi2Enabled && m_xi2Minor >= 4; }
70 bool isXIEvent(xcb_generic_event_t *event) const;
71 bool isXIType(xcb_generic_event_t *event, uint16_t type) const;
72
73 bool isXFixesType(uint responseType, int eventType) const;
74 bool isXRandrType(uint responseType, int eventType) const;
75 bool isXkbType(uint responseType) const; // https://bugs.freedesktop.org/show_bug.cgi?id=51295
76
77protected:
78 void initializeShm();
79 void initializeXFixes();
80 void initializeXRender();
81 void initializeXRandr();
82 void initializeXShape();
83 void initializeXKB();
84 void initializeXSync();
85 void initializeXInput2();
86
87private:
88#if QT_CONFIG(xcb_xlib)
89 void *m_xlibDisplay = nullptr;
90#endif
91 QByteArray m_displayName;
92 xcb_connection_t *m_xcbConnection = nullptr;
93 int m_primaryScreenNumber = 0;
94 const xcb_setup_t *m_setup = nullptr;
95 QXcbAtom m_xcbAtom;
96
97 bool m_hasXFixes = false;
98 bool m_hasXhape = false;
99 bool m_hasInputShape;
100 bool m_hasXRandr = false;
101 bool m_hasXkb = false;
102 bool m_hasXRender = false;
103 bool m_hasShm = false;
104 bool m_hasShmFd = false;
105 bool m_hasXSync = false;
106
107 std::pair<int, int> m_xrenderVersion;
108
109 bool m_xi2Enabled = false;
110 int m_xi2Minor = -1;
111 int m_xiOpCode = -1;
112 uint32_t m_xinputFirstEvent = 0;
113
114 int m_xrandr1Minor = -1;
115
116 uint32_t m_xfixesFirstEvent = 0;
117 uint32_t m_xrandrFirstEvent = 0;
118 uint32_t m_xkbFirstEvent = 0;
119
120 uint32_t m_maximumRequestLength = 0;
121};
122
123#define Q_XCB_REPLY_CONNECTION_ARG(connection, ...) connection
124
125struct QStdFreeDeleter {
126 void operator()(void *p) const noexcept { return std::free(ptr: p); }
127};
128
129#define Q_XCB_REPLY(call, ...) \
130 std::unique_ptr<call##_reply_t, QStdFreeDeleter>( \
131 call##_reply(Q_XCB_REPLY_CONNECTION_ARG(__VA_ARGS__), call(__VA_ARGS__), nullptr) \
132 )
133
134#define Q_XCB_REPLY_UNCHECKED(call, ...) \
135 std::unique_ptr<call##_reply_t, QStdFreeDeleter>( \
136 call##_reply(Q_XCB_REPLY_CONNECTION_ARG(__VA_ARGS__), call##_unchecked(__VA_ARGS__), nullptr) \
137 )
138
139QT_END_NAMESPACE
140

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtbase/src/plugins/platforms/xcb/qxcbconnection_basic.h