Debian Codegraph @

xcb/libqt5xcbqpa-dev/5.12.2/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 (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 (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
233 #if QT_CONFIG(xcb_xinput)
234 void xi2SetupDevice(void *info, bool removeExisting = true);
235 void xi2SetupDevices();
236 struct TouchDeviceData {
237 QTouchDevice *qtTouchDevice = nullptr;
238 QHash<int, QWindowSystemInterface::TouchPoint> touchPoints;
239 QHash<int, QPointF> pointPressedPosition; // in screen coordinates where each point was pressed
240 struct ValuatorClassInfo {
241 double min = 0;
242 double max = 0;
243 int number = -1;
244 QXcbAtom::Atom label;
245 };
246 QVector<ValuatorClassInfo> valuatorInfo;
247
248 // Stuff that is relevant only for touchpads
249 QPointF firstPressedPosition; // in screen coordinates where the first point was pressed
250 QPointF firstPressedNormalPosition; // device coordinates (0 to 1, 0 to 1) where the first point was pressed
251 QSizeF size; // device size in mm
252 bool providesTouchOrientation = false;
253 };
254 TouchDeviceData *populateTouchDevices(void *info);
255 TouchDeviceData *touchDeviceForId(int id);
256 void xi2HandleEvent(xcb_ge_event_t *event);
257 void xi2HandleHierarchyEvent(void *event);
258 void xi2HandleDeviceChangedEvent(void *event);
259 void xi2ProcessTouch(void *xiDevEvent, QXcbWindow *platformWindow);
260 #if QT_CONFIG(tabletevent)
261 struct TabletData {
262 int deviceId = 0;
263 QTabletEvent::PointerType pointerType = QTabletEvent::UnknownPointer;
264 QTabletEvent::TabletDevice tool = QTabletEvent::Stylus;
265 Qt::MouseButtons buttons = 0;
266 qint64 serialId = 0;
267 bool inProximity = false;
268 struct ValuatorClassInfo {
269 double minVal = 0;
270 double maxVal = 0;
271 double curVal = 0;
272 int number = -1;
273 };
274 QHash<int, ValuatorClassInfo> valuatorInfo;
275 };
276 friend class QTypeInfo<TabletData>;
277 friend class QTypeInfo<TabletData::ValuatorClassInfo>;
278 bool xi2HandleTabletEvent(const void *event, TabletData *tabletData);
279 void xi2ReportTabletEvent(const void *event, TabletData *tabletData);
280 QVector<TabletData> m_tabletData;
281 TabletData *tabletDataForDevice(int id);
282 #endif // QT_CONFIG(tabletevent)
283 struct ScrollingDevice {
284 int deviceId = 0;
285 int verticalIndex = 0;
286 int horizontalIndex = 0;
287 double verticalIncrement = 0;
288 double horizontalIncrement = 0;
289 Qt::Orientations orientations = 0;
290 Qt::Orientations legacyOrientations = 0;
291 QPointF lastScrollPosition;
292 };
293 QHash<int, ScrollingDevice> m_scrollingDevices;
294 void xi2HandleScrollEvent(void *event, ScrollingDevice &scrollingDevice);
295 void xi2UpdateScrollingDevice(ScrollingDevice &scrollingDevice);
296 ScrollingDevice *scrollingDeviceForId(int id);
297
298 static bool xi2GetValuatorValueIfSet(const void *event, int valuatorNum, double *value);
299
300 QHash<int, TouchDeviceData> m_touchDevices;
301 struct StartSystemMoveResizeInfo {
302 xcb_window_t window = XCB_NONE;
303 uint16_t deviceid;
304 uint32_t pointid;
305 int corner;
306 } m_startSystemMoveResizeInfo;
307 #endif // QT_CONFIG(xcb_xinput)
308
309 const bool m_canGrabServer;
310 const xcb_visualid_t m_defaultVisualId;
311
312 QList<QXcbVirtualDesktop *> m_virtualDesktops;
313 QList<QXcbScreen *> m_screens;
314
315 xcb_timestamp_t m_time = XCB_CURRENT_TIME;
316 xcb_timestamp_t m_netWmUserTime = XCB_CURRENT_TIME;
317
318 QXcbKeyboard *m_keyboard = nullptr;
319 #ifndef QT_NO_CLIPBOARD
320 QXcbClipboard *m_clipboard = nullptr;
321 #endif
322 #if QT_CONFIG(draganddrop)
323 QXcbDrag *m_drag = nullptr;
324 #endif
325 QScopedPointer<QXcbWMSupport> m_wmSupport;
326 QXcbNativeInterface *m_nativeInterface = nullptr;
327
328 QXcbEventQueue *m_eventQueue = nullptr;
329
330 WindowMapper m_mapper;
331
332 QVector<PeekFunc> m_peekFuncs;
333
334 Qt::MouseButtons m_buttonState = 0;
335 Qt::MouseButton m_button = Qt::NoButton;
336
337 QXcbWindow *m_focusWindow = nullptr;
338 QXcbWindow *m_mouseGrabber = nullptr;
339 QXcbWindow *m_mousePressWindow = nullptr;
340
341 xcb_window_t m_clientLeader = 0;
342 QByteArray m_startupId;
343 QXcbSystemTrayTracker *m_systemTrayTracker = nullptr;
344 mutable QXcbGlIntegration *m_glIntegration = nullptr;
345 mutable bool m_glIntegrationInitialized = false;
346 bool m_xiGrab = false;
347 QVector<int> m_xiMasterPointerIds;
348
349 xcb_window_t m_qtSelectionOwner = 0;
350
351 friend class QXcbEventQueue;
352
353 QByteArray m_xdgCurrentDesktop;
354 };
355 #if QT_CONFIG(xcb_xinput)
356 #if QT_CONFIG(tabletevent)
357 Q_DECLARE_TYPEINFO(QXcbConnection::TabletData::ValuatorClassInfo, Q_PRIMITIVE_TYPE);
358 Q_DECLARE_TYPEINFO(QXcbConnection::TabletData, Q_MOVABLE_TYPE);
359 #endif
360 #endif
361
362 class QXcbConnectionGrabber
363 {
364 public:
365 QXcbConnectionGrabber(QXcbConnection *connection);
366 ~QXcbConnectionGrabber();
367 void release();
368 private:
369 QXcbConnection *m_connection;
370 };
371
372 // The xcb_send_event() requires all events to have 32 bytes. It calls memcpy() on the
373 // passed in event. If the passed in event is less than 32 bytes, memcpy() reaches into
374 // unrelated memory.
375 template <typename T>
376 struct alignas(32) q_padded_xcb_event : T { };
377
378 QT_END_NAMESPACE
379
380 #endif