1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/
#ifndef QXCBEVENTQUEUE_H
#define QXCBEVENTQUEUE_H
#include <QtCore/QThread>
#include <QtCore/QHash>
#include <QtCore/QEventLoop>
#include <QtCore/QVector>
#include <QtCore/QMutex>
#include <QtCore/QWaitCondition>
#include <xcb/xcb.h>
#include <atomic>
QT_BEGIN_NAMESPACE
struct QXcbEventNode {
QXcbEventNode(xcb_generic_event_t *e = nullptr)
: event(e) { }
xcb_generic_event_t *event;
QXcbEventNode *next = nullptr;
bool fromHeap = false;
};
class QXcbConnection;
class QAbstractEventDispatcher;
class QXcbEventQueue : public QThread
{
Q_OBJECT
public:
QXcbEventQueue(QXcbConnection *connection);
~QXcbEventQueue();
enum { PoolSize = 100 }; // 2.4 kB with 100 nodes
enum PeekOption {
PeekDefault = 0, // see qx11info_x11.h for docs
PeekFromCachedIndex = 1,
PeekRetainMatch = 2,
PeekRemoveMatch = 3,
PeekRemoveMatchContinue = 4
};
Q_DECLARE_FLAGS(PeekOptions, PeekOption)
void run() override;
bool isEmpty() const { return m_head == m_flushedTail && !m_head->event; }
xcb_generic_event_t *takeFirst(QEventLoop::ProcessEventsFlags flags);
xcb_generic_event_t *takeFirst();
void flushBufferedEvents();
void wakeUpDispatcher();
// ### peek() and peekEventQueue() could be unified. Note that peekEventQueue()
// is public API exposed via QX11Extras/QX11Info.
template<typename Peeker>
xcb_generic_event_t *peek(Peeker &&peeker) {
return peek(PeekRemoveMatch, std::forward<Peeker>(peeker));
}
template<typename Peeker>
inline xcb_generic_event_t *peek(PeekOption config, Peeker &&peeker);
qint32 generatePeekerId();
bool removePeekerId(qint32 peekerId);
using PeekerCallback = bool (*)(xcb_generic_event_t *event, void *peekerData);
bool peekEventQueue(PeekerCallback peeker, void *peekerData = nullptr,
PeekOptions option = PeekDefault, qint32 peekerId = -1);
const QXcbEventNode *flushedTail() const { return m_flushedTail; }
void waitForNewEvents(const QXcbEventNode *sinceFlushedTail, unsigned long time = ULONG_MAX);
private:
QXcbEventNode *qXcbEventNodeFactory(xcb_generic_event_t *event);
void dequeueNode();
void sendCloseConnectionEvent() const;
bool isCloseConnectionEvent(const xcb_generic_event_t *event);
QXcbEventNode *m_head = nullptr;
QXcbEventNode *m_flushedTail = nullptr;
std::atomic<QXcbEventNode *> m_tail { nullptr };
std::atomic_uint m_nodesRestored { 0 };
QXcbConnection *m_connection = nullptr;
bool m_closeConnectionDetected = false;
uint m_freeNodes = PoolSize;
uint m_poolIndex = 0;
qint32 m_peekerIdSource = 0;
bool m_queueModified = false;
bool m_peekerIndexCacheDirty = false;
QHash<qint32, QXcbEventNode *> m_peekerToNode;
QVector<xcb_generic_event_t *> m_inputEvents;
// debug stats
quint64 m_nodesOnHeap = 0;
QMutex m_newEventsMutex;
QWaitCondition m_newEventsCondition;
};
template<typename Peeker>
xcb_generic_event_t *QXcbEventQueue::peek(PeekOption option, Peeker &&peeker)
{
flushBufferedEvents();
if (isEmpty())
return nullptr;
QXcbEventNode *node = m_head;
do {
xcb_generic_event_t *event = node->event;
if (event && peeker(event, event->response_type & ~0x80)) {
if (option == PeekRemoveMatch || option == PeekRemoveMatchContinue)
node->event = nullptr;
if (option != PeekRemoveMatchContinue)
return event;
}
if (node == m_flushedTail)
break;
node = node->next;
} while (true);
return nullptr;
}
QT_END_NAMESPACE
#endif
|