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