Debian Codegraph @

xcb/libqt5xcbqpa-dev/5.12.0/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 <QtGui/private/qtguiglobal_p.h>
12 #include "qxcbexport.h"
13 #include <QHash>
14 #include <QList>
15 #include <QVector>
16 #include <qpa/qwindowsysteminterface.h>
17 #include <QtCore/QLoggingCategory>
18 #include <QtCore/private/qglobal_p.h>
19
20 #include "qxcbeventqueue.h"
21 #include "qxcbconnection_basic.h"
22
23 #if QT_CONFIG(tabletevent)
24 #include <QTabletEvent>
25 #endif
26
27 QT_BEGIN_NAMESPACE
28
29 Q_DECLARE_LOGGING_CATEGORY(lcQpaXInput)
30 Q_DECLARE_LOGGING_CATEGORY(lcQpaXInputDevices)
31 Q_DECLARE_LOGGING_CATEGORY(lcQpaXInputEvents)
32 Q_DECLARE_LOGGING_CATEGORY(lcQpaScreen)
33 Q_DECLARE_LOGGING_CATEGORY(lcQpaEvents)
34 Q_DECLARE_LOGGING_CATEGORY(lcQpaPeeker)
35 Q_DECLARE_LOGGING_CATEGORY(lcQpaKeyboard)
36 Q_DECLARE_LOGGING_CATEGORY(lcQpaXDnd)
37 Q_DECLARE_LOGGING_CATEGORY(lcQpaEventReader)
38
39 class QXcbVirtualDesktop;
40 class QXcbScreen;
41 class QXcbWindow;
42 class QXcbDrag;
43 class QXcbKeyboard;
44 class QXcbClipboard;
45 class QXcbWMSupport;
46 class QXcbNativeInterface;
47 class QXcbSystemTrayTracker;
48 class QXcbGlIntegration;
49
50 class QXcbWindowEventListener
51 {
52 public:
53 virtual ~QXcbWindowEventListener() {}
54 virtual bool handleNativeEvent(xcb_generic_event_t *) { return false; }
55
56 virtual void handleExposeEvent(const xcb_expose_event_t *) {}
57 virtual void handleClientMessageEvent(const xcb_client_message_event_t *) {}
58 virtual void handleConfigureNotifyEvent(const xcb_configure_notify_event_t *) {}
59 virtual void handleMapNotifyEvent(const xcb_map_notify_event_t *) {}
60 virtual void handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *) {}
61 virtual void handleDestroyNotifyEvent(const xcb_destroy_notify_event_t *) {}
62 virtual void handleButtonPressEvent(const xcb_button_press_event_t *) {}
63 virtual void handleButtonReleaseEvent(const xcb_button_release_event_t *) {}
64 virtual void handleMotionNotifyEvent(const xcb_motion_notify_event_t *) {}
65 virtual void handleEnterNotifyEvent(const xcb_enter_notify_event_t *) {}
66 virtual void handleLeaveNotifyEvent(const xcb_leave_notify_event_t *) {}
67 virtual void handleFocusInEvent(const xcb_focus_in_event_t *) {}
68 virtual void handleFocusOutEvent(const xcb_focus_out_event_t *) {}
69 virtual void handlePropertyNotifyEvent(const xcb_property_notify_event_t *) {}
70 #if QT_CONFIG(xcb_xinput)
71 virtual void handleXIMouseEvent(xcb_ge_event_t *, Qt::MouseEventSource = Qt::MouseEventNotSynthesized) {}
72 virtual void handleXIEnterLeave(xcb_ge_event_t *) {}
73 #endif
74 virtual QXcbWindow *toWindow() { return 0; }
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 = 0);
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 typedef bool (*PeekFunc)(QXcbConnection *, xcb_generic_event_t *);
151 void addPeekFunc(PeekFunc f);
152
153 inline xcb_timestamp_t time() const { return m_time; }
154 inline void setTime(xcb_timestamp_t t) { if (t > m_time) m_time = t; }
155
156 inline xcb_timestamp_t netWmUserTime() const { return m_netWmUserTime; }
157 inline void setNetWmUserTime(xcb_timestamp_t t) { if (t > m_netWmUserTime) m_netWmUserTime = t; }
158
159 xcb_timestamp_t getTimestamp();
160 xcb_window_t getSelectionOwner(xcb_atom_t atom) const;
161 xcb_window_t getQtSelectionOwner();
162
163 void setButtonState(Qt::MouseButton button, bool down);
164 Qt::MouseButtons buttonState() const { return m_buttonState; }
165 Qt::MouseButton button() const { return m_button; }
166 Qt::MouseButton translateMouseButton(xcb_button_t s);
167
168 QXcbWindow *focusWindow() const { return m_focusWindow; }
169 void setFocusWindow(QWindow *);
170 QXcbWindow *mouseGrabber() const { return m_mouseGrabber; }
171 void setMouseGrabber(QXcbWindow *);
172 QXcbWindow *mousePressWindow() const { return m_mousePressWindow; }
173 void setMousePressWindow(QXcbWindow *);
174
175 QByteArray startupId() const { return m_startupId; }
176 void setStartupId(const QByteArray &nextId) { m_startupId = nextId; }
177 void clearStartupId() { m_startupId.clear(); }
178
179 void grabServer();
180 void ungrabServer();
181
182 bool isUnity() const { return m_xdgCurrentDesktop == "unity"; }
183 bool isGnome() const { return m_xdgCurrentDesktop == "gnome"; }
184
185 QXcbNativeInterface *nativeInterface() const { return m_nativeInterface; }
186
187 QXcbSystemTrayTracker *systemTrayTracker() const;
188
189 Qt::MouseButtons queryMouseButtons() const;
190 Qt::KeyboardModifiers queryKeyboardModifiers() const;
191
192 bool isUserInputEvent(xcb_generic_event_t *event) const;
193
194 #if QT_CONFIG(xcb_xinput)
195 void xi2SelectStateEvents();
196 void xi2SelectDeviceEvents(xcb_window_t window);
197 void xi2SelectDeviceEventsCompatibility(xcb_window_t window);
198 bool xi2SetMouseGrabEnabled(xcb_window_t w, bool grab);
199 bool xi2MouseEventsDisabled() const;
200 Qt::MouseButton xiToQtMouseButton(uint32_t b);
201 void xi2UpdateScrollingDevices();
202 bool startSystemMoveResizeForTouchBegin(xcb_window_t window, const QPoint &point, int corner);
203 void abortSystemMoveResizeForTouch();
204 bool isTouchScreen(int id);
205 #endif
206
207 bool canGrab() const { return m_canGrabServer; }
208
209 QXcbGlIntegration *glIntegration() const;
210
211 void flush() { xcb_flush(xcb_connection()); }
212 void processXcbEvents(QEventLoop::ProcessEventsFlags flags);
213
214 protected:
215 bool event(QEvent *e) override;
216
217 private:
218 void xrandrSelectEvents();
219 QXcbScreen* findScreenForCrtc(xcb_window_t rootWindow, xcb_randr_crtc_t crtc) const;
220 QXcbScreen* findScreenForOutput(xcb_window_t rootWindow, xcb_randr_output_t output) const;
221 QXcbVirtualDesktop* virtualDesktopForRootWindow(xcb_window_t rootWindow) const;
222 void updateScreens(const xcb_randr_notify_event_t *event);
223 bool checkOutputIsPrimary(xcb_window_t rootWindow, xcb_randr_output_t output);
224 void updateScreen(QXcbScreen *screen, const xcb_randr_output_change_t &outputChange);
225 QXcbScreen *createScreen(QXcbVirtualDesktop *virtualDesktop,
226 const xcb_randr_output_change_t &outputChange,
227 xcb_randr_get_output_info_reply_t *outputInfo);
228 void destroyScreen(QXcbScreen *screen);
229 void initializeScreens();
230 bool compressEvent(xcb_generic_event_t *event) const;
231
232 #if QT_CONFIG(xcb_xinput)
233 void xi2SetupDevice(void *info, bool removeExisting = true);
234 void xi2SetupDevices();
235 struct TouchDeviceData {
236 QTouchDevice *qtTouchDevice = nullptr;
237 QHash<int, QWindowSystemInterface::TouchPoint> touchPoints;
238 QHash<int, QPointF> pointPressedPosition; // in screen coordinates where each point was pressed
239 struct ValuatorClassInfo {
240 double min = 0;
241 double max = 0;
242 int number = -1;
243 QXcbAtom::Atom label;
244 };
245 QVector<ValuatorClassInfo> valuatorInfo;
246
247 // Stuff that is relevant only for touchpads
248 QPointF firstPressedPosition; // in screen coordinates where the first point was pressed
249 QPointF firstPressedNormalPosition; // device coordinates (0 to 1, 0 to 1) where the first point was pressed
250 QSizeF size; // device size in mm
251 bool providesTouchOrientation = false;
252 };
253 TouchDeviceData *populateTouchDevices(void *info);
254 TouchDeviceData *touchDeviceForId(int id);
255 void xi2HandleEvent(xcb_ge_event_t *event);
256 void xi2HandleHierarchyEvent(void *event);
257 void xi2HandleDeviceChangedEvent(void *event);
258 void xi2ProcessTouch(void *xiDevEvent, QXcbWindow *platformWindow);
259 #if QT_CONFIG(tabletevent)
260 struct TabletData {
261 int deviceId = 0;
262 QTabletEvent::PointerType pointerType = QTabletEvent::UnknownPointer;
263 QTabletEvent::TabletDevice tool = QTabletEvent::Stylus;
264 Qt::MouseButtons buttons = 0;
265 qint64 serialId = 0;
266 bool inProximity = false;
267 struct ValuatorClassInfo {
268 double minVal = 0;
269 double maxVal = 0;
270 double curVal = 0;
271 int number = -1;
272 };
273 QHash<int, ValuatorClassInfo> valuatorInfo;
274 };
275 friend class QTypeInfo<TabletData>;
276 friend class QTypeInfo<TabletData::ValuatorClassInfo>;
277 bool xi2HandleTabletEvent(const void *event, TabletData *tabletData);
278 void xi2ReportTabletEvent(const void *event, TabletData *tabletData);
279 QVector<TabletData> m_tabletData;
280 TabletData *tabletDataForDevice(int id);
281 #endif // QT_CONFIG(tabletevent)
282 struct ScrollingDevice {
283 int deviceId = 0;
284 int verticalIndex = 0;
285 int horizontalIndex = 0;
286 double verticalIncrement = 0;
287 double horizontalIncrement = 0;
288 Qt::Orientations orientations = 0;
289 Qt::Orientations legacyOrientations = 0;
290 QPointF lastScrollPosition;
291 };
292 QHash<int, ScrollingDevice> m_scrollingDevices;
293 void xi2HandleScrollEvent(void *event, ScrollingDevice &scrollingDevice);
294 void xi2UpdateScrollingDevice(ScrollingDevice &scrollingDevice);
295 ScrollingDevice *scrollingDeviceForId(int id);
296
297 static bool xi2GetValuatorValueIfSet(const void *event, int valuatorNum, double *value);
298
299 QHash<int, TouchDeviceData> m_touchDevices;
300 struct StartSystemMoveResizeInfo {
301 xcb_window_t window = XCB_NONE;
302 uint16_t deviceid;
303 uint32_t pointid;
304 int corner;
305 } m_startSystemMoveResizeInfo;
306 #endif // QT_CONFIG(xcb_xinput)
307
308 const bool m_canGrabServer;
309 const xcb_visualid_t m_defaultVisualId;
310
311 QList<QXcbVirtualDesktop *> m_virtualDesktops;
312 QList<QXcbScreen *> m_screens;
313
314 xcb_timestamp_t m_time = XCB_CURRENT_TIME;
315 xcb_timestamp_t m_netWmUserTime = XCB_CURRENT_TIME;
316
317 QXcbKeyboard *m_keyboard = nullptr;
318 #ifndef QT_NO_CLIPBOARD
319 QXcbClipboard *m_clipboard = nullptr;
320 #endif
321 #if QT_CONFIG(draganddrop)
322 QXcbDrag *m_drag = nullptr;
323 #endif
324 QScopedPointer<QXcbWMSupport> m_wmSupport;
325 QXcbNativeInterface *m_nativeInterface = nullptr;
326
327 QXcbEventQueue *m_eventQueue = nullptr;
328
329 WindowMapper m_mapper;
330
331 QVector<PeekFunc> m_peekFuncs;
332
333 Qt::MouseButtons m_buttonState = 0;
334 Qt::MouseButton m_button = Qt::NoButton;
335
336 QXcbWindow *m_focusWindow = nullptr;
337 QXcbWindow *m_mouseGrabber = nullptr;
338 QXcbWindow *m_mousePressWindow = nullptr;
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 QVector<int> m_xiMasterPointerIds;
347
348 xcb_window_t m_qtSelectionOwner = 0;
349
350 friend class QXcbEventQueue;
351
352 QByteArray m_xdgCurrentDesktop;
353 };
354 #if QT_CONFIG(xcb_xinput)
355 #if QT_CONFIG(tabletevent)
356 Q_DECLARE_TYPEINFO(QXcbConnection::TabletData::ValuatorClassInfo, Q_PRIMITIVE_TYPE);
357 Q_DECLARE_TYPEINFO(QXcbConnection::TabletData, Q_MOVABLE_TYPE);
358 #endif
359 #endif
360
361 class QXcbConnectionGrabber
362 {
363 public:
364 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
379 #endif