Debian Codegraph @

xcb/libqt5xcbqpa-dev/5.15.5/qxcbeventqueue.h

raw
1 // Copyright (C) 2018 The Qt Company Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5 #ifndef QXCBEVENTQUEUE_H
6 #define QXCBEVENTQUEUE_H
7
8 #include <QtCore/QThread>
9 #include <QtCore/QHash>
10 #include <QtCore/QEventLoop>
11 #include <QtCore/QVector>
12 #include <QtCore/QMutex>
13 #include <QtCore/QWaitCondition>
14
15 #include <xcb/xcb.h>
16
17 #include <atomic>
18
19 QT_BEGIN_NAMESPACE
20
21 struct QXcbEventNode {
22 QXcbEventNode(xcb_generic_event_t *e = nullptr)
23 : event(e) { }
24
25 xcb_generic_event_t *event;
26 QXcbEventNode *next = nullptr;
27 bool fromHeap = false;
28 };
29
30 class QXcbConnection;
31 class QAbstractEventDispatcher;
32
33 class QXcbEventQueue : public QThread
34 {
35 Q_OBJECT
36 public:
37 QXcbEventQueue(QXcbConnection *connection);
38 ~QXcbEventQueue();
39
40 enum { PoolSize = 100 }; // 2.4 kB with 100 nodes
41
42 enum PeekOption {
43 PeekDefault = 0, // see qx11info_x11.h for docs
44 PeekFromCachedIndex = 1,
45 PeekRetainMatch = 2,
46 PeekRemoveMatch = 3,
47 PeekRemoveMatchContinue = 4
48 };
49 Q_DECLARE_FLAGS(PeekOptions, PeekOption)
50
51 void run() override;
52
53 bool isEmpty() const { return m_head == m_flushedTail && !m_head->event; }
54 xcb_generic_event_t *takeFirst(QEventLoop::ProcessEventsFlags flags);
55 xcb_generic_event_t *takeFirst();
56 void flushBufferedEvents();
57 void wakeUpDispatcher();
58
59 // ### peek() and peekEventQueue() could be unified. Note that peekEventQueue()
60 // is public API exposed via QX11Extras/QX11Info.
61 template<typename Peeker>
62 xcb_generic_event_t *peek(Peeker &&peeker) {
63 return peek(PeekRemoveMatch, std::forward<Peeker>(peeker));
64 }
65 template<typename Peeker>
66 inline xcb_generic_event_t *peek(PeekOption config, Peeker &&peeker);
67
68 qint32 generatePeekerId();
69 bool removePeekerId(qint32 peekerId);
70
71 using PeekerCallback = bool (*)(xcb_generic_event_t *event, void *peekerData);
72 bool peekEventQueue(PeekerCallback peeker, void *peekerData = nullptr,
73 PeekOptions option = PeekDefault, qint32 peekerId = -1);
74
75 const QXcbEventNode *flushedTail() const { return m_flushedTail; }
76 void waitForNewEvents(const QXcbEventNode *sinceFlushedTail, unsigned long time = ULONG_MAX);
77
78 private:
79 QXcbEventNode *qXcbEventNodeFactory(xcb_generic_event_t *event);
80 void dequeueNode();
81
82 void sendCloseConnectionEvent() const;
83 bool isCloseConnectionEvent(const xcb_generic_event_t *event);
84
85 QXcbEventNode *m_head = nullptr;
86 QXcbEventNode *m_flushedTail = nullptr;
87 std::atomic<QXcbEventNode *> m_tail { nullptr };
88 std::atomic_uint m_nodesRestored { 0 };
89
90 QXcbConnection *m_connection = nullptr;
91 bool m_closeConnectionDetected = false;
92
93 uint m_freeNodes = PoolSize;
94 uint m_poolIndex = 0;
95
96 qint32 m_peekerIdSource = 0;
97 bool m_queueModified = false;
98 bool m_peekerIndexCacheDirty = false;
99 QHash<qint32, QXcbEventNode *> m_peekerToNode;
100
101 QVector<xcb_generic_event_t *> m_inputEvents;
102
103 // debug stats
104 quint64 m_nodesOnHeap = 0;
105
106 QMutex m_newEventsMutex;
107 QWaitCondition m_newEventsCondition;
108 };
109
110 template<typename Peeker>
111 xcb_generic_event_t *QXcbEventQueue::peek(PeekOption option, Peeker &&peeker)
112 {
113 flushBufferedEvents();
114 if (isEmpty())
115 return nullptr;
116
117 QXcbEventNode *node = m_head;
118 do {
119 xcb_generic_event_t *event = node->event;
120 if (event && peeker(event, event->response_type & ~0x80)) {
121 if (option == PeekRemoveMatch || option == PeekRemoveMatchContinue)
122 node->event = nullptr;
123 if (option != PeekRemoveMatchContinue)
124 return event;
125 }
126 if (node == m_flushedTail)
127 break;
128 node = node->next;
129 } while (true);
130
131 return nullptr;
132 }
133
134 QT_END_NAMESPACE
135
136 #endif