Debian Codegraph @

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