1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #pragma once |
5 | |
6 | #include <xcb/xcb.h> |
7 | #include <xcb/randr.h> |
8 | |
9 | #include <QtCore/QTimer> |
10 | #include <QtGui/qpointingdevice.h> |
11 | #include <QtGui/private/qtguiglobal_p.h> |
12 | #include "qxcbexport.h" |
13 | #include <QHash> |
14 | #include <QList> |
15 | #include <qpa/qwindowsysteminterface.h> |
16 | #include <QtCore/QLoggingCategory> |
17 | #include <QtCore/private/qglobal_p.h> |
18 | |
19 | #include "qxcbeventqueue.h" |
20 | #include "qxcbconnection_basic.h" |
21 | |
22 | #if QT_CONFIG(tabletevent) |
23 | #include <QTabletEvent> |
24 | #endif |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | Q_DECLARE_LOGGING_CATEGORY(lcQpaXInput) |
29 | Q_DECLARE_LOGGING_CATEGORY(lcQpaXInputEvents) |
30 | Q_DECLARE_LOGGING_CATEGORY(lcQpaScreen) |
31 | Q_DECLARE_LOGGING_CATEGORY(lcQpaEvents) |
32 | Q_DECLARE_LOGGING_CATEGORY(lcQpaPeeker) |
33 | Q_DECLARE_LOGGING_CATEGORY(lcQpaKeyboard) |
34 | Q_DECLARE_LOGGING_CATEGORY(lcQpaClipboard) |
35 | Q_DECLARE_LOGGING_CATEGORY(lcQpaXDnd) |
36 | Q_DECLARE_LOGGING_CATEGORY(lcQpaEventReader) |
37 | |
38 | class QXcbVirtualDesktop; |
39 | class QXcbScreen; |
40 | class QXcbWindow; |
41 | class QXcbDrag; |
42 | class QXcbKeyboard; |
43 | class QXcbScrollingDevice; |
44 | class QXcbScrollingDevicePrivate; |
45 | class QXcbClipboard; |
46 | class QXcbWMSupport; |
47 | class QXcbNativeInterface; |
48 | class QXcbSystemTrayTracker; |
49 | class QXcbGlIntegration; |
50 | |
51 | class QXcbWindowEventListener |
52 | { |
53 | public: |
54 | virtual ~QXcbWindowEventListener() {} |
55 | virtual bool handleNativeEvent(xcb_generic_event_t *) { return false; } |
56 | |
57 | virtual void handleExposeEvent(const xcb_expose_event_t *) {} |
58 | virtual void handleClientMessageEvent(const xcb_client_message_event_t *) {} |
59 | virtual void handleConfigureNotifyEvent(const xcb_configure_notify_event_t *) {} |
60 | virtual void handleMapNotifyEvent(const xcb_map_notify_event_t *) {} |
61 | virtual void handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *) {} |
62 | virtual void handleDestroyNotifyEvent(const xcb_destroy_notify_event_t *) {} |
63 | virtual void handleButtonPressEvent(const xcb_button_press_event_t *) {} |
64 | virtual void handleButtonReleaseEvent(const xcb_button_release_event_t *) {} |
65 | virtual void handleMotionNotifyEvent(const xcb_motion_notify_event_t *) {} |
66 | virtual void handleEnterNotifyEvent(const xcb_enter_notify_event_t *) {} |
67 | virtual void handleLeaveNotifyEvent(const xcb_leave_notify_event_t *) {} |
68 | virtual void handleFocusInEvent(const xcb_focus_in_event_t *) {} |
69 | virtual void handleFocusOutEvent(const xcb_focus_out_event_t *) {} |
70 | virtual void handlePropertyNotifyEvent(const xcb_property_notify_event_t *) {} |
71 | virtual void handleXIMouseEvent(xcb_ge_event_t *, Qt::MouseEventSource = Qt::MouseEventNotSynthesized) {} |
72 | virtual void handleXIEnterLeave(xcb_ge_event_t *) {} |
73 | virtual QXcbWindow *toWindow() { return nullptr; } |
74 | }; |
75 | |
76 | typedef QHash<xcb_window_t, QXcbWindowEventListener *> WindowMapper; |
77 | |
78 | class QXcbSyncWindowRequest : public QEvent |
79 | { |
80 | public: |
81 | QXcbSyncWindowRequest(QXcbWindow *w) : QEvent(QEvent::Type(QEvent::User + 1)), m_window(w) { } |
82 | |
83 | QXcbWindow *window() const { return m_window; } |
84 | void invalidate(); |
85 | |
86 | private: |
87 | QXcbWindow *m_window; |
88 | }; |
89 | |
90 | class Q_XCB_EXPORT QXcbConnection : public QXcbBasicConnection |
91 | { |
92 | Q_OBJECT |
93 | public: |
94 | QXcbConnection(QXcbNativeInterface *nativeInterface, bool canGrabServer, xcb_visualid_t defaultVisualId, const char *displayName = nullptr); |
95 | ~QXcbConnection(); |
96 | |
97 | QXcbConnection *connection() const { return const_cast<QXcbConnection *>(this); } |
98 | QXcbEventQueue *eventQueue() const { return m_eventQueue; } |
99 | |
100 | const QList<QXcbVirtualDesktop *> &virtualDesktops() const { return m_virtualDesktops; } |
101 | const QList<QXcbScreen *> &screens() const { return m_screens; } |
102 | QXcbVirtualDesktop *primaryVirtualDesktop() const { |
103 | return m_virtualDesktops.value(i: primaryScreenNumber()); |
104 | } |
105 | QXcbScreen *primaryScreen() const; |
106 | |
107 | const xcb_format_t *formatForDepth(uint8_t depth) const; |
108 | |
109 | bool imageNeedsEndianSwap() const |
110 | { |
111 | if (!hasShm()) |
112 | return false; // The non-Shm path does its own swapping |
113 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN |
114 | return setup()->image_byte_order != XCB_IMAGE_ORDER_MSB_FIRST; |
115 | #else |
116 | return setup()->image_byte_order != XCB_IMAGE_ORDER_LSB_FIRST; |
117 | #endif |
118 | } |
119 | |
120 | QXcbKeyboard *keyboard() const { return m_keyboard; } |
121 | |
122 | #ifndef QT_NO_CLIPBOARD |
123 | QXcbClipboard *clipboard() const { return m_clipboard; } |
124 | #endif |
125 | #if QT_CONFIG(draganddrop) |
126 | QXcbDrag *drag() const { return m_drag; } |
127 | #endif |
128 | |
129 | QXcbWMSupport *wmSupport() const { return m_wmSupport.data(); } |
130 | xcb_window_t rootWindow(); |
131 | xcb_window_t clientLeader(); |
132 | |
133 | bool hasDefaultVisualId() const { return m_defaultVisualId != UINT_MAX; } |
134 | xcb_visualid_t defaultVisualId() const { return m_defaultVisualId; } |
135 | |
136 | void sync(); |
137 | |
138 | void handleXcbError(xcb_generic_error_t *error); |
139 | void printXcbError(const char *message, xcb_generic_error_t *error); |
140 | void handleXcbEvent(xcb_generic_event_t *event); |
141 | void printXcbEvent(const QLoggingCategory &log, const char *message, |
142 | xcb_generic_event_t *event) const; |
143 | |
144 | void addWindowEventListener(xcb_window_t id, QXcbWindowEventListener *eventListener); |
145 | void removeWindowEventListener(xcb_window_t id); |
146 | QXcbWindowEventListener *windowEventListenerFromId(xcb_window_t id); |
147 | QXcbWindow *platformWindowFromId(xcb_window_t id); |
148 | |
149 | inline xcb_timestamp_t time() const { return m_time; } |
150 | inline void setTime(xcb_timestamp_t t) { if (timeGreaterThan(a: t, b: m_time)) m_time = t; } |
151 | |
152 | inline xcb_timestamp_t netWmUserTime() const { return m_netWmUserTime; } |
153 | inline void setNetWmUserTime(xcb_timestamp_t t) { if (timeGreaterThan(a: t, b: m_netWmUserTime)) m_netWmUserTime = t; } |
154 | |
155 | xcb_timestamp_t getTimestamp(); |
156 | xcb_window_t selectionOwner(xcb_atom_t atom) const; |
157 | xcb_window_t qtSelectionOwner(); |
158 | |
159 | void setButtonState(Qt::MouseButton button, bool down); |
160 | Qt::MouseButtons buttonState() const { return m_buttonState; } |
161 | Qt::MouseButton button() const { return m_button; } |
162 | Qt::MouseButton translateMouseButton(xcb_button_t s); |
163 | |
164 | QXcbWindow *focusWindow() const { return m_focusWindow; } |
165 | void setFocusWindow(QWindow *); |
166 | QXcbWindow *mouseGrabber() const { return m_mouseGrabber; } |
167 | void setMouseGrabber(QXcbWindow *); |
168 | QXcbWindow *mousePressWindow() const { return m_mousePressWindow; } |
169 | void setMousePressWindow(QXcbWindow *); |
170 | |
171 | QByteArray startupId() const; |
172 | void setStartupId(const QByteArray &nextId); |
173 | |
174 | void grabServer(); |
175 | void ungrabServer(); |
176 | |
177 | QString windowManagerName() const; |
178 | |
179 | QXcbNativeInterface *nativeInterface() const { return m_nativeInterface; } |
180 | |
181 | QXcbSystemTrayTracker *systemTrayTracker() const; |
182 | |
183 | Qt::MouseButtons queryMouseButtons() const; |
184 | |
185 | bool isUserInputEvent(xcb_generic_event_t *event) const; |
186 | |
187 | void xi2SelectStateEvents(); |
188 | void xi2SelectDeviceEvents(xcb_window_t window); |
189 | bool xi2SetMouseGrabEnabled(xcb_window_t w, bool grab); |
190 | |
191 | Qt::MouseButton xiToQtMouseButton(uint32_t b); |
192 | void xi2UpdateScrollingDevices(); |
193 | bool isTouchScreen(int id); |
194 | |
195 | bool startSystemMoveResizeForTouch(xcb_window_t window, int edges); |
196 | void abortSystemMoveResize(xcb_window_t window); |
197 | bool isDuringSystemMoveResize() const; |
198 | void setDuringSystemMoveResize(bool during); |
199 | |
200 | bool canGrab() const { return m_canGrabServer; } |
201 | |
202 | QXcbGlIntegration *glIntegration() const; |
203 | |
204 | void flush() { xcb_flush(c: xcb_connection()); } |
205 | void processXcbEvents(QEventLoop::ProcessEventsFlags flags); |
206 | |
207 | QTimer &focusInTimer() { return m_focusInTimer; } |
208 | |
209 | protected: |
210 | bool event(QEvent *e) override; |
211 | |
212 | private: |
213 | void xrandrSelectEvents(); |
214 | QXcbScreen* findScreenForCrtc(xcb_window_t rootWindow, xcb_randr_crtc_t crtc) const; |
215 | QXcbScreen* findScreenForOutput(xcb_window_t rootWindow, xcb_randr_output_t output) const; |
216 | QXcbVirtualDesktop* virtualDesktopForRootWindow(xcb_window_t rootWindow) const; |
217 | void updateScreens(const xcb_randr_notify_event_t *event); |
218 | bool checkOutputIsPrimary(xcb_window_t rootWindow, xcb_randr_output_t output); |
219 | void updateScreen(QXcbScreen *screen, const xcb_randr_output_change_t &outputChange); |
220 | QXcbScreen *createScreen(QXcbVirtualDesktop *virtualDesktop, |
221 | const xcb_randr_output_change_t &outputChange, |
222 | xcb_randr_get_output_info_reply_t *outputInfo); |
223 | void destroyScreen(QXcbScreen *screen); |
224 | void initializeScreens(bool initialized); |
225 | void initializeScreensWithoutXRandR(xcb_screen_iterator_t *it, int screenNumber, QXcbScreen **primaryScreen); |
226 | void initializeScreensFromOutput(xcb_screen_iterator_t *it, int screenNumber, QXcbScreen **primaryScreen); |
227 | |
228 | QXcbVirtualDesktop* virtualDesktopForNumber(int n) const; |
229 | QXcbScreen* findScreenForMonitorInfo(const QList<QPlatformScreen *> &screens, xcb_randr_monitor_info_t *monitorInfo); |
230 | void initializeScreensFromMonitor(xcb_screen_iterator_t *it, int screenNumber, QXcbScreen **primaryScreen, bool initialized); |
231 | |
232 | bool compressEvent(xcb_generic_event_t *event) const; |
233 | inline bool timeGreaterThan(xcb_timestamp_t a, xcb_timestamp_t b) const |
234 | { return static_cast<int32_t>(a - b) > 0 || b == XCB_CURRENT_TIME; } |
235 | |
236 | void xi2SetupSlavePointerDevice(void *info, bool removeExisting = true, QPointingDevice *master = nullptr); |
237 | void xi2SetupDevices(); |
238 | // TODO get rid of this: store minimal necessary info in a subclass of QPointingDevicePrivate |
239 | struct TouchDeviceData { |
240 | QPointingDevice *qtTouchDevice = nullptr; |
241 | QHash<int, QWindowSystemInterface::TouchPoint> touchPoints; |
242 | QHash<int, QPointF> pointPressedPosition; // in screen coordinates where each point was pressed |
243 | struct ValuatorClassInfo { |
244 | double min = 0; |
245 | double max = 0; |
246 | int number = -1; |
247 | QXcbAtom::Atom label; |
248 | }; |
249 | QList<ValuatorClassInfo> valuatorInfo; |
250 | |
251 | // Stuff that is relevant only for touchpads |
252 | QPointF firstPressedPosition; // in screen coordinates where the first point was pressed |
253 | QPointF firstPressedNormalPosition; // device coordinates (0 to 1, 0 to 1) where the first point was pressed |
254 | QSizeF size; // device size in mm |
255 | bool providesTouchOrientation = false; |
256 | }; |
257 | TouchDeviceData *populateTouchDevices(void *info, QXcbScrollingDevicePrivate *scrollingDeviceP, bool *used = nullptr); |
258 | TouchDeviceData *touchDeviceForId(int id); |
259 | void xi2HandleEvent(xcb_ge_event_t *event); |
260 | void xi2HandleGesturePinchEvent(void *event); |
261 | void xi2HandleGestureSwipeEvent(void *event); |
262 | void xi2HandleHierarchyEvent(void *event); |
263 | void xi2HandleDeviceChangedEvent(void *event); |
264 | void xi2ProcessTouch(void *xiDevEvent, QXcbWindow *platformWindow); |
265 | #if QT_CONFIG(tabletevent) |
266 | // TODO get rid of this: store minimal necessary info in a subclass of QXcbScrollingDevice (some tablets can scroll) |
267 | struct TabletData { |
268 | int deviceId = 0; |
269 | QString name; |
270 | QPointingDevice::PointerType pointerType = QPointingDevice::PointerType::Unknown; |
271 | QInputDevice::DeviceType tool = QInputDevice::DeviceType::Stylus; |
272 | Qt::MouseButtons buttons; |
273 | qint64 serialId = 0; |
274 | bool inProximity = false; |
275 | struct ValuatorClassInfo { |
276 | double minVal = 0; |
277 | double maxVal = 0; |
278 | double curVal = 0; |
279 | int number = -1; |
280 | }; |
281 | QHash<int, ValuatorClassInfo> valuatorInfo; |
282 | }; |
283 | friend class QTypeInfo<TabletData>; |
284 | friend class QTypeInfo<TabletData::ValuatorClassInfo>; |
285 | bool xi2HandleTabletEvent(const void *event, TabletData *tabletData); |
286 | void xi2ReportTabletEvent(const void *event, TabletData *tabletData); |
287 | QList<TabletData> m_tabletData; |
288 | TabletData *tabletDataForDevice(int id); |
289 | #endif // QT_CONFIG(tabletevent) |
290 | void xi2HandleScrollEvent(void *event, const QPointingDevice *scrollingDevice); |
291 | void xi2UpdateScrollingDevice(QInputDevice *scrollingDevice); |
292 | QXcbScrollingDevice *scrollingDeviceForId(int id); |
293 | |
294 | static bool xi2GetValuatorValueIfSet(const void *event, int valuatorNum, double *value); |
295 | |
296 | QHash<int, TouchDeviceData> m_touchDevices; |
297 | |
298 | struct StartSystemMoveResizeInfo { |
299 | xcb_window_t window = XCB_NONE; |
300 | uint16_t deviceid; |
301 | uint32_t pointid; |
302 | int edges; |
303 | } m_startSystemMoveResizeInfo; |
304 | bool m_duringSystemMoveResize; |
305 | |
306 | const bool m_canGrabServer; |
307 | const xcb_visualid_t m_defaultVisualId; |
308 | |
309 | QList<QXcbVirtualDesktop *> m_virtualDesktops; |
310 | QList<QXcbScreen *> m_screens; |
311 | |
312 | xcb_timestamp_t m_time = XCB_CURRENT_TIME; |
313 | xcb_timestamp_t m_netWmUserTime = XCB_CURRENT_TIME; |
314 | |
315 | QXcbKeyboard *m_keyboard = nullptr; |
316 | #ifndef QT_NO_CLIPBOARD |
317 | QXcbClipboard *m_clipboard = nullptr; |
318 | #endif |
319 | #if QT_CONFIG(draganddrop) |
320 | QXcbDrag *m_drag = nullptr; |
321 | #endif |
322 | QScopedPointer<QXcbWMSupport> m_wmSupport; |
323 | QXcbNativeInterface *m_nativeInterface = nullptr; |
324 | |
325 | QXcbEventQueue *m_eventQueue = nullptr; |
326 | |
327 | WindowMapper m_mapper; |
328 | |
329 | Qt::MouseButtons m_buttonState; |
330 | Qt::MouseButton m_button = Qt::NoButton; |
331 | |
332 | QXcbWindow *m_focusWindow = nullptr; |
333 | QXcbWindow *m_mouseGrabber = nullptr; |
334 | QXcbWindow *m_mousePressWindow = nullptr; |
335 | |
336 | #if QT_CONFIG(gestures) |
337 | qreal m_lastPinchScale = 0; |
338 | #endif |
339 | |
340 | xcb_window_t m_clientLeader = 0; |
341 | QByteArray m_startupId; |
342 | QXcbSystemTrayTracker *m_systemTrayTracker = nullptr; |
343 | mutable QXcbGlIntegration *m_glIntegration = nullptr; |
344 | mutable bool m_glIntegrationInitialized = false; |
345 | bool m_xiGrab = false; |
346 | QList<int> m_xiMasterPointerIds; |
347 | QList<int> m_xiSlavePointerIds; |
348 | |
349 | xcb_window_t m_qtSelectionOwner = 0; |
350 | |
351 | friend class QXcbEventQueue; |
352 | |
353 | QTimer m_focusInTimer; |
354 | |
355 | }; |
356 | #if QT_CONFIG(tabletevent) |
357 | Q_DECLARE_TYPEINFO(QXcbConnection::TabletData::ValuatorClassInfo, Q_PRIMITIVE_TYPE); |
358 | Q_DECLARE_TYPEINFO(QXcbConnection::TabletData, Q_RELOCATABLE_TYPE); |
359 | #endif |
360 | |
361 | class QXcbConnectionGrabber |
362 | { |
363 | public: |
364 | Q_NODISCARD_CTOR QXcbConnectionGrabber(QXcbConnection *connection); |
365 | ~QXcbConnectionGrabber(); |
366 | void release(); |
367 | private: |
368 | QXcbConnection *m_connection; |
369 | }; |
370 | |
371 | // The xcb_send_event() requires all events to have 32 bytes. It calls memcpy() on the |
372 | // passed in event. If the passed in event is less than 32 bytes, memcpy() reaches into |
373 | // unrelated memory. |
374 | template <typename T> |
375 | struct alignas(32) q_padded_xcb_event : T { }; |
376 | |
377 | QT_END_NAMESPACE |
378 | |