Debian Codegraph @

xcb/libqt6xcbqpa-dev/6.2.4/qxcbeventqueue.h

raw
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 QXCBEVENTQUEUE_H
40 #define QXCBEVENTQUEUE_H
41
42 #include <QtCore/QThread>
43 #include <QtCore/QHash>
44 #include <QtCore/QEventLoop>
45 #include <QtCore/QList>
46 #include <QtCore/QMutex>
47 #include <QtCore/QWaitCondition>
48
49 #include <xcb/xcb.h>
50
51 #include <atomic>
52 #include <limits>
53
54 QT_BEGIN_NAMESPACE
55
56 struct QXcbEventNode {
57 QXcbEventNode(xcb_generic_event_t *e = nullptr)
58 : event(e) { }
59
60 xcb_generic_event_t *event;
61 QXcbEventNode *next = nullptr;
62 bool fromHeap = false;
63 };
64
65 class QXcbConnection;
66 class QAbstractEventDispatcher;
67
68 class QXcbEventQueue : public QThread
69 {
70 Q_OBJECT
71 public:
72 QXcbEventQueue(QXcbConnection *connection);
73 ~QXcbEventQueue();
74
75 enum { PoolSize = 100 }; // 2.4 kB with 100 nodes
76
77 enum PeekOption {
78 // See qx11info_x11.cpp in X11 Extras module.
79 PeekDefault = 0,
80 // See qx11info_x11.cpp in X11 Extras module.
81 PeekFromCachedIndex = 1,
82 // Used by the event compression algorithms to determine if
83 // the currently processed event (which has been already dequeued)
84 // can be compressed. Returns from the QXcbEventQueue::peek()
85 // on the first match.
86 PeekRetainMatch = 2,
87 // Marks the event in the node as "nullptr". The actual
88 // node remains in the queue. The nodes are unlinked only
89 // by dequeueNode(). Returns from the QXcbEventQueue::peek()
90 // on the first match.
91 PeekConsumeMatch = 3,
92 // Same as above, but continues to the next node in the
93 // queue. Repeats this until the flushed tailed node has
94 // been reached.
95 PeekConsumeMatchAndContinue = 4
96 };
97 Q_DECLARE_FLAGS(PeekOptions, PeekOption)
98
99 void run() override;
100
101 bool isEmpty() const { return m_head == m_flushedTail && !m_head->event; }
102 xcb_generic_event_t *takeFirst(QEventLoop::ProcessEventsFlags flags);
103 xcb_generic_event_t *takeFirst();
104 void flushBufferedEvents();
105 void wakeUpDispatcher();
106
107 // ### peek() and peekEventQueue() could be unified. Note that peekEventQueue()
108 // is public API exposed via QX11Extras/QX11Info. PeekOption could be reworked to
109 // have values that can be OR-ed together.
110 template<typename Peeker>
111 xcb_generic_event_t *peek(Peeker &&peeker) {
112 return peek(PeekConsumeMatch, std::forward<Peeker>(peeker));
113 }
114 template<typename Peeker>
115 inline xcb_generic_event_t *peek(PeekOption config, Peeker &&peeker);
116
117 qint32 generatePeekerId();
118 bool removePeekerId(qint32 peekerId);
119
120 using PeekerCallback = bool (*)(xcb_generic_event_t *event, void *peekerData);
121 bool peekEventQueue(PeekerCallback peeker, void *peekerData = nullptr,
122 PeekOptions option = PeekDefault, qint32 peekerId = -1);
123
124 const QXcbEventNode *flushedTail() const { return m_flushedTail; }
125 void waitForNewEvents(const QXcbEventNode *sinceFlushedTail,
126 unsigned long time = std::numeric_limits<unsigned long>::max());
127
128 private:
129 QXcbEventNode *qXcbEventNodeFactory(xcb_generic_event_t *event);
130 void dequeueNode();
131
132 void sendCloseConnectionEvent() const;
133 bool isCloseConnectionEvent(const xcb_generic_event_t *event);
134
135 QXcbEventNode *m_head = nullptr;
136 QXcbEventNode *m_flushedTail = nullptr;
137 std::atomic<QXcbEventNode *> m_tail { nullptr };
138 std::atomic_uint m_nodesRestored { 0 };
139
140 QXcbConnection *m_connection = nullptr;
141 bool m_closeConnectionDetected = false;
142
143 uint m_freeNodes = PoolSize;
144 uint m_poolIndex = 0;
145
146 qint32 m_peekerIdSource = 0;
147 bool m_queueModified = false;
148 bool m_peekerIndexCacheDirty = false;
149 QHash<qint32, QXcbEventNode *> m_peekerToNode;
150
151 QList<xcb_generic_event_t *> m_inputEvents;
152
153 // debug stats
154 quint64 m_nodesOnHeap = 0;
155
156 QMutex m_newEventsMutex;
157 QWaitCondition m_newEventsCondition;
158 };
159
160 template<typename Peeker>
161 xcb_generic_event_t *QXcbEventQueue::peek(PeekOption option, Peeker &&peeker)
162 {
163 flushBufferedEvents();
164 if (isEmpty())
165 return nullptr;
166
167 QXcbEventNode *node = m_head;
168 do {
169 xcb_generic_event_t *event = node->event;
170 if (event && peeker(event, event->response_type & ~0x80)) {
171 if (option == PeekConsumeMatch || option == PeekConsumeMatchAndContinue)
172 node->event = nullptr;
173
174 if (option != PeekConsumeMatchAndContinue)
175 return event;
176 }
177 if (node == m_flushedTail)
178 break;
179 node = node->next;
180 } while (true);
181
182 return nullptr;
183 }
184
185 QT_END_NAMESPACE
186
187 #endif