Debian Codegraph @

xcb/libqt6xcbqpa-dev/6.2.4/qxcbconnection.h

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