Debian Codegraph @

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