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/QVector>
46#include <QtCore/QMutex>
47#include <QtCore/QWaitCondition>
48
49#include <xcb/xcb.h>
50
51#include <atomic>
52
53QT_BEGIN_NAMESPACE
54
55struct QXcbEventNode {
56 QXcbEventNode(xcb_generic_event_t *e = nullptr)
57 : event(e) { }
58
59 xcb_generic_event_t *event;
60 QXcbEventNode *next = nullptr;
61 bool fromHeap = false;
62};
63
64class QXcbConnection;
65class QAbstractEventDispatcher;
66
67class QXcbEventQueue : public QThread
68{
69 Q_OBJECT
70public:
71 QXcbEventQueue(QXcbConnection *connection);
72 ~QXcbEventQueue();
73
74 enum { PoolSize = 100 }; // 2.4 kB with 100 nodes
75
76 enum PeekOption {
77 PeekDefault = 0, // see qx11info_x11.h for docs
78 PeekFromCachedIndex = 1,
79 PeekRetainMatch = 2,
80 PeekRemoveMatch = 3,
81 PeekRemoveMatchContinue = 4
82 };
83 Q_DECLARE_FLAGS(PeekOptions, PeekOption)
84
85 void run() override;
86
87 bool isEmpty() const { return m_head == m_flushedTail && !m_head->event; }
88 xcb_generic_event_t *takeFirst(QEventLoop::ProcessEventsFlags flags);
89 xcb_generic_event_t *takeFirst();
90 void flushBufferedEvents();
91 void wakeUpDispatcher();
92
93 // ### peek() and peekEventQueue() could be unified. Note that peekEventQueue()
94 // is public API exposed via QX11Extras/QX11Info.
95 template<typename Peeker>
96 xcb_generic_event_t *peek(Peeker &&peeker) {
97 return peek(PeekRemoveMatch, std::forward<Peeker>(peeker));
98 }
99 template<typename Peeker>
100 inline xcb_generic_event_t *peek(PeekOption config, Peeker &&peeker);
101
102 qint32 generatePeekerId();
103 bool removePeekerId(qint32 peekerId);
104
105 using PeekerCallback = bool (*)(xcb_generic_event_t *event, void *peekerData);
106 bool peekEventQueue(PeekerCallback peeker, void *peekerData = nullptr,
107 PeekOptions option = PeekDefault, qint32 peekerId = -1);
108
109 void waitForNewEvents(unsigned long time = ULONG_MAX);
110
111private:
112 QXcbEventNode *qXcbEventNodeFactory(xcb_generic_event_t *event);
113 void dequeueNode();
114
115 void sendCloseConnectionEvent() const;
116 bool isCloseConnectionEvent(const xcb_generic_event_t *event);
117
118 QXcbEventNode *m_head = nullptr;
119 QXcbEventNode *m_flushedTail = nullptr;
120 std::atomic<QXcbEventNode *> m_tail { nullptr };
121 std::atomic_uint m_nodesRestored { 0 };
122
123 QXcbConnection *m_connection = nullptr;
124 bool m_closeConnectionDetected = false;
125
126 uint m_freeNodes = PoolSize;
127 uint m_poolIndex = 0;
128
129 qint32 m_peekerIdSource = 0;
130 bool m_queueModified = false;
131 bool m_peekerIndexCacheDirty = false;
132 QHash<qint32, QXcbEventNode *> m_peekerToNode;
133
134 QVector<xcb_generic_event_t *> m_inputEvents;
135
136 // debug stats
137 quint64 m_nodesOnHeap = 0;
138
139 QMutex m_newEventsMutex;
140 QWaitCondition m_newEventsCondition;
141};
142
143template<typename Peeker>
144xcb_generic_event_t *QXcbEventQueue::peek(PeekOption option, Peeker &&peeker)
145{
146 flushBufferedEvents();
147 if (isEmpty())
148 return nullptr;
149
150 QXcbEventNode *node = m_head;
151 do {
152 xcb_generic_event_t *event = node->event;
153 if (event && peeker(event, event->response_type & ~0x80)) {
154 if (option == PeekRemoveMatch || option == PeekRemoveMatchContinue)
155 node->event = nullptr;
156 if (option != PeekRemoveMatchContinue)
157 return event;
158 }
159 if (node == m_flushedTail)
160 break;
161 node = node->next;
162 } while (true);
163
164 return nullptr;
165}
166
167QT_END_NAMESPACE
168
169#endif
170