Debian Codegraph @

xcb/libqt5xcbqpa-dev/5.14.1/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 #if QT_CONFIG(xcb_xinput)
73 virtual void handleXIMouseEvent(xcb_ge_event_t *, Qt::MouseEventSource = Qt::MouseEventNotSynthesized) {}
74 virtual void handleXIEnterLeave(xcb_ge_event_t *) {}
75 #endif
76 virtual QXcbWindow *toWindow() { return nullptr; }
77 };
78
79 typedef QHash<xcb_window_t, QXcbWindowEventListener *> WindowMapper;
80
81 class QXcbSyncWindowRequest : public QEvent
82 {
83 public:
84 QXcbSyncWindowRequest(QXcbWindow *w) : QEvent(QEvent::Type(QEvent::User + 1)), m_window(w) { }
85
86 QXcbWindow *window() const { return m_window; }
87 void invalidate();
88
89 private:
90 QXcbWindow *m_window;
91 };
92
93 class Q_XCB_EXPORT QXcbConnection : public QXcbBasicConnection
94 {
95 Q_OBJECT
96 public:
97 QXcbConnection(QXcbNativeInterface *nativeInterface, bool canGrabServer, xcb_visualid_t defaultVisualId, const char *displayName = 0);
98 ~QXcbConnection();
99
100 QXcbConnection *connection() const { return const_cast<QXcbConnection *>(this); }
101 QXcbEventQueue *eventQueue() const { return m_eventQueue; }
102
103 const QList<QXcbVirtualDesktop *> &virtualDesktops() const { return m_virtualDesktops; }
104 const QList<QXcbScreen *> &screens() const { return m_screens; }
105 QXcbVirtualDesktop *primaryVirtualDesktop() const {
106 return m_virtualDesktops.value(primaryScreenNumber());
107 }
108 QXcbScreen *primaryScreen() const;
109
110 const xcb_format_t *formatForDepth(uint8_t depth) const;
111
112 bool imageNeedsEndianSwap() const
113 {
114 if (!hasShm())
115 return false; // The non-Shm path does its own swapping
116 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
117 return setup()->image_byte_order != XCB_IMAGE_ORDER_MSB_FIRST;
118 #else
119 return setup()->image_byte_order != XCB_IMAGE_ORDER_LSB_FIRST;
120 #endif
121 }
122
123 QXcbKeyboard *keyboard() const { return m_keyboard; }
124
125 #ifndef QT_NO_CLIPBOARD
126 QXcbClipboard *clipboard() const { return m_clipboard; }
127 #endif
128 #if QT_CONFIG(draganddrop)
129 QXcbDrag *drag() const { return m_drag; }
130 #endif
131
132 QXcbWMSupport *wmSupport() const { return m_wmSupport.data(); }
133 xcb_window_t rootWindow();
134 xcb_window_t clientLeader();
135
136 bool hasDefaultVisualId() const { return m_defaultVisualId != UINT_MAX; }
137 xcb_visualid_t defaultVisualId() const { return m_defaultVisualId; }
138
139 void sync();
140
141 void handleXcbError(xcb_generic_error_t *error);
142 void printXcbError(const char *message, xcb_generic_error_t *error);
143 void handleXcbEvent(xcb_generic_event_t *event);
144 void printXcbEvent(const QLoggingCategory &log, const char *message,
145 xcb_generic_event_t *event) const;
146
147 void addWindowEventListener(xcb_window_t id, QXcbWindowEventListener *eventListener);
148 void removeWindowEventListener(xcb_window_t id);
149 QXcbWindowEventListener *windowEventListenerFromId(xcb_window_t id);
150 QXcbWindow *platformWindowFromId(xcb_window_t id);
151
152 inline xcb_timestamp_t time() const { return m_time; }
153 inline void setTime(xcb_timestamp_t t) { if (timeGreaterThan(t, m_time)) m_time = t; }
154
155 inline xcb_timestamp_t netWmUserTime() const { return m_netWmUserTime; }
156 inline void setNetWmUserTime(xcb_timestamp_t t) { if (timeGreaterThan(t, m_netWmUserTime)) m_netWmUserTime = t; }
157
158 xcb_timestamp_t getTimestamp();
159 xcb_window_t getSelectionOwner(xcb_atom_t atom) const;
160 xcb_window_t getQtSelectionOwner();
161
162 void setButtonState(Qt::MouseButton button, bool down);
163 Qt::MouseButtons buttonState() const { return m_buttonState; }
164 Qt::MouseButton button() const { return m_button; }
165 Qt::MouseButton translateMouseButton(xcb_button_t s);
166
167 QXcbWindow *focusWindow() const { return m_focusWindow; }
168 void setFocusWindow(QWindow *);
169 QXcbWindow *mouseGrabber() const { return m_mouseGrabber; }
170 void setMouseGrabber(QXcbWindow *);
171 QXcbWindow *mousePressWindow() const { return m_mousePressWindow; }
172 void setMousePressWindow(QXcbWindow *);
173
174 QByteArray startupId() const { return m_startupId; }
175 void setStartupId(const QByteArray &nextId) { m_startupId = nextId; }
176 void clearStartupId() { m_startupId.clear(); }
177
178 void grabServer();
179 void ungrabServer();
180
181 bool isUnity() const { return m_xdgCurrentDesktop == "unity"; }
182 bool isGnome() const { return m_xdgCurrentDesktop == "gnome"; }
183
184 QXcbNativeInterface *nativeInterface() const { return m_nativeInterface; }
185
186 QXcbSystemTrayTracker *systemTrayTracker() const;
187
188 Qt::MouseButtons queryMouseButtons() const;
189 Qt::KeyboardModifiers queryKeyboardModifiers() const;
190
191 bool isUserInputEvent(xcb_generic_event_t *event) const;
192
193 #if QT_CONFIG(xcb_xinput)
194 void xi2SelectStateEvents();
195 void xi2SelectDeviceEvents(xcb_window_t window);
196 void xi2SelectDeviceEventsCompatibility(xcb_window_t window);
197 bool xi2SetMouseGrabEnabled(xcb_window_t w, bool grab);
198 bool xi2MouseEventsDisabled() const;
199 Qt::MouseButton xiToQtMouseButton(uint32_t b);
200 void xi2UpdateScrollingDevices();
201 bool startSystemMoveResizeForTouchBegin(xcb_window_t window, const QPoint &point, int corner);
202 void abortSystemMoveResizeForTouch();
203 bool isTouchScreen(int id);
204 #endif
205
206 bool canGrab() const { return m_canGrabServer; }
207
208 QXcbGlIntegration *glIntegration() const;
209
210 void flush() { xcb_flush(xcb_connection()); }
211 void processXcbEvents(QEventLoop::ProcessEventsFlags flags);
212
213 QTimer &focusInTimer() { return m_focusInTimer; }
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 Qt::MouseButtons m_buttonState = nullptr;
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 QTimer m_focusInTimer;
355
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