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 
53 QT_BEGIN_NAMESPACE
54 
55 struct QXcbEventNode {
56     QXcbEventNode(xcb_generic_event_t *e = nullptr)
eventQXcbEventNode57         : event(e) { }
58 
59     xcb_generic_event_t *event;
60     QXcbEventNode *next = nullptr;
61     bool fromHeap = false;
62 };
63 
64 class QXcbConnection;
65 class QAbstractEventDispatcher;
66 
67 class QXcbEventQueue : public QThread
68 {
69     Q_OBJECT
70 public:
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 
isEmpty()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>
peek(Peeker && 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 
flushedTail()109     const QXcbEventNode *flushedTail() const { return m_flushedTail; }
110     void waitForNewEvents(const QXcbEventNode *sinceFlushedTail,
111                           unsigned long time = ULONG_MAX);
112 
113 private:
114     QXcbEventNode *qXcbEventNodeFactory(xcb_generic_event_t *event);
115     void dequeueNode();
116 
117     void sendCloseConnectionEvent() const;
118     bool isCloseConnectionEvent(const xcb_generic_event_t *event);
119 
120     QXcbEventNode *m_head = nullptr;
121     QXcbEventNode *m_flushedTail = nullptr;
122     std::atomic<QXcbEventNode *> m_tail { nullptr };
123     std::atomic_uint m_nodesRestored { 0 };
124 
125     QXcbConnection *m_connection = nullptr;
126     bool m_closeConnectionDetected = false;
127 
128     uint m_freeNodes = PoolSize;
129     uint m_poolIndex = 0;
130 
131     qint32 m_peekerIdSource = 0;
132     bool m_queueModified = false;
133     bool m_peekerIndexCacheDirty = false;
134     QHash<qint32, QXcbEventNode *> m_peekerToNode;
135 
136     QVector<xcb_generic_event_t *> m_inputEvents;
137 
138     // debug stats
139     quint64 m_nodesOnHeap = 0;
140 
141     QMutex m_newEventsMutex;
142     QWaitCondition m_newEventsCondition;
143 };
144 
145 template<typename Peeker>
peek(PeekOption option,Peeker && peeker)146 xcb_generic_event_t *QXcbEventQueue::peek(PeekOption option, Peeker &&peeker)
147 {
148     flushBufferedEvents();
149     if (isEmpty())
150         return nullptr;
151 
152     QXcbEventNode *node = m_head;
153     do {
154         xcb_generic_event_t *event = node->event;
155         if (event && peeker(event, event->response_type & ~0x80)) {
156             if (option == PeekRemoveMatch || option == PeekRemoveMatchContinue)
157                 node->event = nullptr;
158             if (option != PeekRemoveMatchContinue)
159                 return event;
160         }
161         if (node == m_flushedTail)
162             break;
163         node = node->next;
164     } while (true);
165 
166     return nullptr;
167 }
168 
169 QT_END_NAMESPACE
170 
171 #endif
172