Debian Codegraph @

xcb/libqt5xcbqpa-dev/5.12.1/qxcbeventqueue.h

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