Debian Codegraph @

xcb/libqt5xcbqpa-dev/5.3.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
10 #include <QHash>
11 #include <QList>
12 #include <QMutex>
13 #include <QObject>
14 #include <QThread>
15 #include <QVector>
16 #include <QVarLengthArray>
17 #include <qpa/qwindowsysteminterface.h>
18
19 // This is needed to make Qt compile together with XKB. xkb.h is using a variable
20 // which is called 'explicit', this is a reserved keyword in c++
21 #ifndef QT_NO_XKB
22 #define explicit dont_use_cxx_explicit
23 #include <xcb/xkb.h>
24 #undef explicit
25 #endif
26
27 #ifndef QT_NO_TABLETEVENT
28 #include <QTabletEvent>
29 #endif
30
31 #ifdef XCB_USE_XINPUT2_MAEMO
32 struct XInput2MaemoData;
33 #elif XCB_USE_XINPUT2
34 #include <X11/extensions/XI2.h>
35 #ifdef XIScrollClass
36 #define XCB_USE_XINPUT21 // XI 2.1 adds smooth scrolling support
37 #ifdef XI_TouchBeginMask
38 #define XCB_USE_XINPUT22 // XI 2.2 adds multi-point touch support
39 #endif
40 #endif
41 struct XInput2DeviceData;
42 #endif
43 struct xcb_randr_get_output_info_reply_t;
44
45 //#define Q_XCB_DEBUG
46
47 QT_BEGIN_NAMESPACE
48
49 class QXcbScreen;
50 class QXcbWindow;
51 class QXcbDrag;
52 class QXcbKeyboard;
53 class QXcbClipboard;
54 class QXcbWMSupport;
55 class QXcbNativeInterface;
56 class QXcbSystemTrayTracker;
57
58 namespace QXcbAtom {
59 enum Atom {
60 // window-manager <-> client protocols
61 WM_PROTOCOLS,
62 WM_DELETE_WINDOW,
63 WM_TAKE_FOCUS,
64 _NET_WM_PING,
65 _NET_WM_CONTEXT_HELP,
66 _NET_WM_SYNC_REQUEST,
67 _NET_WM_SYNC_REQUEST_COUNTER,
68 MANAGER, // System tray notification
69 _NET_SYSTEM_TRAY_OPCODE, // System tray operation
70
71 // ICCCM window state
72 WM_STATE,
73 WM_CHANGE_STATE,
74 WM_CLASS,
75
76 // Session management
77 WM_CLIENT_LEADER,
78 WM_WINDOW_ROLE,
79 SM_CLIENT_ID,
80
81 // Clipboard
82 CLIPBOARD,
83 INCR,
84 TARGETS,
85 MULTIPLE,
86 TIMESTAMP,
87 SAVE_TARGETS,
88 CLIP_TEMPORARY,
89 _QT_SELECTION,
90 _QT_CLIPBOARD_SENTINEL,
91 _QT_SELECTION_SENTINEL,
92 CLIPBOARD_MANAGER,
93
94 RESOURCE_MANAGER,
95
96 _XSETROOT_ID,
97
98 _QT_SCROLL_DONE,
99 _QT_INPUT_ENCODING,
100
101 // Qt/XCB specific
102 _QT_CLOSE_CONNECTION,
103
104 _MOTIF_WM_HINTS,
105
106 DTWM_IS_RUNNING,
107 ENLIGHTENMENT_DESKTOP,
108 _DT_SAVE_MODE,
109 _SGI_DESKS_MANAGER,
110
111 // EWMH (aka NETWM)
112 _NET_SUPPORTED,
113 _NET_VIRTUAL_ROOTS,
114 _NET_WORKAREA,
115
116 _NET_MOVERESIZE_WINDOW,
117 _NET_WM_MOVERESIZE,
118
119 _NET_WM_NAME,
120 _NET_WM_ICON_NAME,
121 _NET_WM_ICON,
122
123 _NET_WM_PID,
124
125 _NET_WM_WINDOW_OPACITY,
126
127 _NET_WM_STATE,
128 _NET_WM_STATE_ABOVE,
129 _NET_WM_STATE_BELOW,
130 _NET_WM_STATE_FULLSCREEN,
131 _NET_WM_STATE_MAXIMIZED_HORZ,
132 _NET_WM_STATE_MAXIMIZED_VERT,
133 _NET_WM_STATE_MODAL,
134 _NET_WM_STATE_STAYS_ON_TOP,
135 _NET_WM_STATE_DEMANDS_ATTENTION,
136
137 _NET_WM_USER_TIME,
138 _NET_WM_USER_TIME_WINDOW,
139 _NET_WM_FULL_PLACEMENT,
140
141 _NET_WM_WINDOW_TYPE,
142 _NET_WM_WINDOW_TYPE_DESKTOP,
143 _NET_WM_WINDOW_TYPE_DOCK,
144 _NET_WM_WINDOW_TYPE_TOOLBAR,
145 _NET_WM_WINDOW_TYPE_MENU,
146 _NET_WM_WINDOW_TYPE_UTILITY,
147 _NET_WM_WINDOW_TYPE_SPLASH,
148 _NET_WM_WINDOW_TYPE_DIALOG,
149 _NET_WM_WINDOW_TYPE_DROPDOWN_MENU,
150 _NET_WM_WINDOW_TYPE_POPUP_MENU,
151 _NET_WM_WINDOW_TYPE_TOOLTIP,
152 _NET_WM_WINDOW_TYPE_NOTIFICATION,
153 _NET_WM_WINDOW_TYPE_COMBO,
154 _NET_WM_WINDOW_TYPE_DND,
155 _NET_WM_WINDOW_TYPE_NORMAL,
156 _KDE_NET_WM_WINDOW_TYPE_OVERRIDE,
157
158 _KDE_NET_WM_FRAME_STRUT,
159
160 _NET_STARTUP_INFO,
161 _NET_STARTUP_INFO_BEGIN,
162
163 _NET_SUPPORTING_WM_CHECK,
164
165 _NET_WM_CM_S0,
166
167 _NET_SYSTEM_TRAY_VISUAL,
168
169 _NET_ACTIVE_WINDOW,
170
171 // Property formats
172 TEXT,
173 UTF8_STRING,
174 CARDINAL,
175
176 // Xdnd
177 XdndEnter,
178 XdndPosition,
179 XdndStatus,
180 XdndLeave,
181 XdndDrop,
182 XdndFinished,
183 XdndTypelist,
184 XdndActionList,
185
186 XdndSelection,
187
188 XdndAware,
189 XdndProxy,
190
191 XdndActionCopy,
192 XdndActionLink,
193 XdndActionMove,
194 XdndActionPrivate,
195
196 // Motif DND
197 _MOTIF_DRAG_AND_DROP_MESSAGE,
198 _MOTIF_DRAG_INITIATOR_INFO,
199 _MOTIF_DRAG_RECEIVER_INFO,
200 _MOTIF_DRAG_WINDOW,
201 _MOTIF_DRAG_TARGETS,
202
203 XmTRANSFER_SUCCESS,
204 XmTRANSFER_FAILURE,
205
206 // Xkb
207 _XKB_RULES_NAMES,
208
209 // XEMBED
210 _XEMBED,
211 _XEMBED_INFO,
212
213 // XInput2
214 ButtonLeft,
215 ButtonMiddle,
216 ButtonRight,
217 ButtonWheelUp,
218 ButtonWheelDown,
219 ButtonHorizWheelLeft,
220 ButtonHorizWheelRight,
221 AbsMTPositionX,
222 AbsMTPositionY,
223 AbsMTTouchMajor,
224 AbsMTTouchMinor,
225 AbsMTPressure,
226 AbsMTTrackingID,
227 MaxContacts,
228 RelX,
229 RelY,
230 // XInput2 tablet
231 AbsX,
232 AbsY,
233 AbsPressure,
234 AbsTiltX,
235 AbsTiltY,
236 AbsWheel,
237 AbsDistance,
238 WacomSerialIDs,
239 INTEGER,
240 RelHorizWheel,
241 RelVertWheel,
242 RelHorizScroll,
243 RelVertScroll,
244
245 #if XCB_USE_MAEMO_WINDOW_PROPERTIES
246 MeegoTouchOrientationAngle,
247 #endif
248 _XSETTINGS_SETTINGS,
249
250 _COMPIZ_DECOR_PENDING,
251 _COMPIZ_DECOR_REQUEST,
252 _COMPIZ_DECOR_DELETE_PIXMAP,
253
254 NPredefinedAtoms,
255
256 _QT_SETTINGS_TIMESTAMP = NPredefinedAtoms,
257 NAtoms
258 };
259 }
260
261 typedef QVarLengthArray<xcb_generic_event_t *, 64> QXcbEventArray;
262
263 class QXcbConnection;
264 class QXcbEventReader : public QThread
265 {
266 Q_OBJECT
267 public:
268 QXcbEventReader(QXcbConnection *connection);
269
270 void run();
271
272 QXcbEventArray *lock();
273 void unlock();
274
275 void start();
276
277 void registerEventDispatcher(QAbstractEventDispatcher *dispatcher);
278
279 signals:
280 void eventPending();
281
282 private slots:
283 void registerForEvents();
284
285 private:
286 void addEvent(xcb_generic_event_t *event);
287
288 QMutex m_mutex;
289 QXcbEventArray m_events;
290 QXcbConnection *m_connection;
291
292 typedef xcb_generic_event_t * (*XcbPollForQueuedEventFunctionPointer)(xcb_connection_t *c);
293 XcbPollForQueuedEventFunctionPointer m_xcb_poll_for_queued_event;
294 };
295
296 class QXcbWindowEventListener
297 {
298 public:
299 virtual bool handleGenericEvent(xcb_generic_event_t *, long *) { return false; }
300
301 virtual void handleExposeEvent(const xcb_expose_event_t *) {}
302 virtual void handleClientMessageEvent(const xcb_client_message_event_t *) {}
303 virtual void handleConfigureNotifyEvent(const xcb_configure_notify_event_t *) {}
304 virtual void handleMapNotifyEvent(const xcb_map_notify_event_t *) {}
305 virtual void handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *) {}
306 virtual void handleDestroyNotifyEvent(const xcb_destroy_notify_event_t *) {}
307 virtual void handleButtonPressEvent(const xcb_button_press_event_t *) {}
308 virtual void handleButtonReleaseEvent(const xcb_button_release_event_t *) {}
309 virtual void handleMotionNotifyEvent(const xcb_motion_notify_event_t *) {}
310 virtual void handleEnterNotifyEvent(const xcb_enter_notify_event_t *) {}
311 virtual void handleLeaveNotifyEvent(const xcb_leave_notify_event_t *) {}
312 virtual void handleFocusInEvent(const xcb_focus_in_event_t *) {}
313 virtual void handleFocusOutEvent(const xcb_focus_out_event_t *) {}
314 virtual void handlePropertyNotifyEvent(const xcb_property_notify_event_t *) {}
315
316 virtual QXcbWindow *toWindow() { return 0; }
317 };
318
319 typedef QHash<xcb_window_t, QXcbWindowEventListener *> WindowMapper;
320
321 class QAbstractEventDispatcher;
322 class QXcbConnection : public QObject
323 {
324 Q_OBJECT
325 public:
326 QXcbConnection(QXcbNativeInterface *nativeInterface, bool canGrabServer, const char *displayName = 0);
327 ~QXcbConnection();
328
329 QXcbConnection *connection() const { return const_cast<QXcbConnection *>(this); }
330
331 const QList<QXcbScreen *> &screens() const { return m_screens; }
332 int primaryScreen() const { return m_primaryScreen; }
333
334 inline xcb_atom_t atom(QXcbAtom::Atom atom) const { return m_allAtoms[atom]; }
335 QXcbAtom::Atom qatom(xcb_atom_t atom) const;
336 xcb_atom_t internAtom(const char *name);
337 QByteArray atomName(xcb_atom_t atom);
338
339 const char *displayName() const { return m_displayName.constData(); }
340 xcb_connection_t *xcb_connection() const { return m_connection; }
341 const xcb_setup_t *setup() const { return m_setup; }
342 const xcb_format_t *formatForDepth(uint8_t depth) const;
343
344 QXcbKeyboard *keyboard() const { return m_keyboard; }
345
346 #ifndef QT_NO_CLIPBOARD
347 QXcbClipboard *clipboard() const { return m_clipboard; }
348 #endif
349 #ifndef QT_NO_DRAGANDDROP
350 QXcbDrag *drag() const { return m_drag; }
351 #endif
352
353 QXcbWMSupport *wmSupport() const { return m_wmSupport.data(); }
354 xcb_window_t rootWindow();
355 #ifdef XCB_USE_XLIB
356 void *xlib_display() const { return m_xlib_display; }
357 #endif
358
359 #ifdef XCB_USE_EGL
360 bool hasEgl() const;
361 #endif
362 #if defined(XCB_USE_EGL)
363 void *egl_display() const { return m_egl_display; }
364 #endif
365 #ifdef XCB_USE_XINPUT2_MAEMO
366 bool isUsingXInput2Maemo();
367 #elif defined(XCB_USE_XINPUT2)
368 void xi2Select(xcb_window_t window);
369 #endif
370 #ifdef XCB_USE_XINPUT21
371 bool isUsingXInput21() { return m_xi2Enabled && m_xi2Minor >= 1; }
372 #else
373 bool isUsingXInput21() { return false; }
374 #endif
375
376
377 void sync();
378
379 void handleXcbError(xcb_generic_error_t *error);
380 void handleXcbEvent(xcb_generic_event_t *event);
381
382 void addWindowEventListener(xcb_window_t id, QXcbWindowEventListener *eventListener);
383 void removeWindowEventListener(xcb_window_t id);
384 QXcbWindowEventListener *windowEventListenerFromId(xcb_window_t id);
385 QXcbWindow *platformWindowFromId(xcb_window_t id);
386
387 xcb_generic_event_t *checkEvent(int type);
388 template<typename T>
389 inline xcb_generic_event_t *checkEvent(T &checker);
390
391 typedef bool (*PeekFunc)(QXcbConnection *, xcb_generic_event_t *);
392 void addPeekFunc(PeekFunc f);
393
394 inline xcb_timestamp_t time() const { return m_time; }
395 inline void setTime(xcb_timestamp_t t) { if (t > m_time) m_time = t; }
396
397 inline xcb_timestamp_t netWmUserTime() const { return m_netWmUserTime; }
398 inline void setNetWmUserTime(xcb_timestamp_t t) { if (t > m_netWmUserTime) m_netWmUserTime = t; }
399
400 bool hasGLX() const { return has_glx_extension; }
401 bool hasXFixes() const { return xfixes_first_event > 0; }
402 bool hasXShape() const { return has_shape_extension; }
403 bool hasXRandr() const { return has_randr_extension; }
404 bool hasInputShape() const { return has_input_shape; }
405 bool hasTouchWithoutMouseEmulation() const { return has_touch_without_mouse_emulation; }
406 bool hasXKB() const { return has_xkb; }
407
408 bool supportsThreadedRendering() const { return m_reader->isRunning(); }
409
410 xcb_timestamp_t getTimestamp();
411
412 Qt::MouseButtons buttons() const { return m_buttons; }
413
414 QXcbWindow *focusWindow() const { return m_focusWindow; }
415 void setFocusWindow(QXcbWindow *);
416
417 QByteArray startupId() const { return m_startupId; }
418 void clearStartupId() { m_startupId.clear(); }
419
420 void grabServer();
421 void ungrabServer();
422
423 QXcbNativeInterface *nativeInterface() const { return m_nativeInterface; }
424
425 QXcbSystemTrayTracker *systemTrayTracker();
426
427 #ifdef XCB_USE_XINPUT2
428 void handleEnterEvent(const xcb_enter_notify_event_t *);
429 #endif
430
431 QXcbEventReader *eventReader() const { return m_reader; }
432
433 public slots:
434 void syncWindow(QXcbWindow *window);
435 void flush() { xcb_flush(m_connection); }
436
437 private slots:
438 void processXcbEvents();
439
440 private:
441 void initializeAllAtoms();
442 void sendConnectionEvent(QXcbAtom::Atom atom, uint id = 0);
443 void initializeGLX();
444 void initializeXFixes();
445 void initializeXRender();
446 void initializeXRandr();
447 void initializeXShape();
448 void initializeXKB();
449 #ifdef XCB_USE_XINPUT2_MAEMO
450 void initializeXInput2Maemo();
451 void finalizeXInput2Maemo();
452 void handleGenericEventMaemo(xcb_ge_event_t *event);
453 #endif
454 void handleClientMessageEvent(const xcb_client_message_event_t *event);
455 QXcbScreen* findOrCreateScreen(QList<QXcbScreen *>& newScreens, int screenNumber,
456 xcb_screen_t* xcbScreen, xcb_randr_get_output_info_reply_t *output = NULL);
457 void updateScreens();
458 void handleButtonPress(xcb_generic_event_t *event);
459 void handleButtonRelease(xcb_generic_event_t *event);
460
461 bool m_xi2Enabled;
462 int m_xi2Minor;
463 #ifdef XCB_USE_XINPUT2
464 void initializeXInput2();
465 void finalizeXInput2();
466 void xi2SetupDevices();
467 XInput2DeviceData *deviceForId(int id);
468 void xi2HandleEvent(xcb_ge_event_t *event);
469 void xi2HandleHierachyEvent(void *event);
470 int m_xiOpCode, m_xiEventBase, m_xiErrorBase;
471 #ifndef QT_NO_TABLETEVENT
472 struct TabletData {
473 TabletData() : deviceId(0), down(false), serialId(0), inProximity(false) { }
474 int deviceId;
475 QTabletEvent::PointerType pointerType;
476 bool down;
477 qint64 serialId;
478 bool inProximity;
479 struct ValuatorClassInfo {
480 ValuatorClassInfo() : minVal(0), maxVal(0) { }
481 double minVal;
482 double maxVal;
483 int number;
484 };
485 QHash<int, ValuatorClassInfo> valuatorInfo;
486 };
487 bool xi2HandleTabletEvent(void *event, TabletData *tabletData);
488 void xi2ReportTabletEvent(const TabletData &tabletData, void *event);
489 QVector<TabletData> m_tabletData;
490 #endif
491 struct ScrollingDevice {
492 ScrollingDevice() : deviceId(0), verticalIndex(0), horizontalIndex(0), orientations(0), legacyOrientations(0) { }
493 int deviceId;
494 int verticalIndex, horizontalIndex;
495 double verticalIncrement, horizontalIncrement;
496 Qt::Orientations orientations;
497 Qt::Orientations legacyOrientations;
498 QPointF lastScrollPosition;
499 };
500 void xi2HandleScrollEvent(void *event, ScrollingDevice &scrollingDevice);
501 QHash<int, ScrollingDevice> m_scrollingDevices;
502 #endif // XCB_USE_XINPUT2
503
504 #if defined(XCB_USE_XINPUT2) || defined(XCB_USE_XINPUT2_MAEMO)
505 static bool xi2GetValuatorValueIfSet(void *event, int valuatorNum, double *value);
506 static bool xi2PrepareXIGenericDeviceEvent(xcb_ge_event_t *event, int opCode);
507 #endif
508
509 xcb_connection_t *m_connection;
510 const xcb_setup_t *m_setup;
511 bool m_canGrabServer;
512
513 QList<QXcbScreen *> m_screens;
514 int m_primaryScreen;
515
516 xcb_atom_t m_allAtoms[QXcbAtom::NAtoms];
517
518 xcb_timestamp_t m_time;
519 xcb_timestamp_t m_netWmUserTime;
520
521 QByteArray m_displayName;
522
523 QXcbKeyboard *m_keyboard;
524 #ifndef QT_NO_CLIPBOARD
525 QXcbClipboard *m_clipboard;
526 #endif
527 #ifndef QT_NO_DRAGANDDROP
528 QXcbDrag *m_drag;
529 #endif
530 QScopedPointer<QXcbWMSupport> m_wmSupport;
531 QXcbNativeInterface *m_nativeInterface;
532
533 #if defined(XCB_USE_XLIB)
534 void *m_xlib_display;
535 #endif
536 QXcbEventReader *m_reader;
537 #ifdef XCB_USE_XINPUT2_MAEMO
538 XInput2MaemoData *m_xinputData;
539 #elif defined(XCB_USE_XINPUT2)
540 QHash<int, QWindowSystemInterface::TouchPoint> m_touchPoints;
541 QHash<int, XInput2DeviceData*> m_touchDevices;
542 #endif
543 #if defined(XCB_USE_EGL)
544 void *m_egl_display;
545 bool m_has_egl;
546 #endif
547 #ifdef Q_XCB_DEBUG
548 struct CallInfo {
549 int sequence;
550 QByteArray file;
551 int line;
552 };
553 QVector<CallInfo> m_callLog;
554 QMutex m_callLogMutex;
555 void log(const char *file, int line, int sequence);
556 template <typename cookie_t>
557 friend cookie_t q_xcb_call_template(const cookie_t &cookie, QXcbConnection *connection, const char *file, int line);
558 #endif
559
560 WindowMapper m_mapper;
561
562 QVector<PeekFunc> m_peekFuncs;
563
564 uint32_t xfixes_first_event;
565 uint32_t xrandr_first_event;
566 uint32_t xkb_first_event;
567
568 bool has_glx_extension;
569 bool has_shape_extension;
570 bool has_randr_extension;
571 bool has_input_shape;
572 bool has_touch_without_mouse_emulation;
573 bool has_xkb;
574 bool debug_xinput_devices;
575 bool debug_xinput;
576
577 Qt::MouseButtons m_buttons;
578
579 QXcbWindow *m_focusWindow;
580
581 QByteArray m_startupId;
582 QXcbSystemTrayTracker *m_systemTrayTracker;
583
584 friend class QXcbEventReader;
585 };
586
587 #define DISPLAY_FROM_XCB(object) ((Display *)(object->connection()->xlib_display()))
588
589 template<typename T>
590 xcb_generic_event_t *QXcbConnection::checkEvent(T &checker)
591 {
592 QXcbEventArray *eventqueue = m_reader->lock();
593
594 for (int i = 0; i < eventqueue->size(); ++i) {
595 xcb_generic_event_t *event = eventqueue->at(i);
596 if (checker.checkEvent(event)) {
597 (*eventqueue)[i] = 0;
598 m_reader->unlock();
599 return event;
600 }
601 }
602 m_reader->unlock();
603 return 0;
604 }
605
606 class QXcbConnectionGrabber
607 {
608 public:
609 QXcbConnectionGrabber(QXcbConnection *connection);
610 ~QXcbConnectionGrabber();
611 void release();
612 private:
613 QXcbConnection *m_connection;
614 };
615
616 #ifdef Q_XCB_DEBUG
617 template <typename cookie_t>
618 cookie_t q_xcb_call_template(const cookie_t &cookie, QXcbConnection *connection, const char *file, int line)
619 {
620 connection->log(file, line, cookie.sequence);
621 return cookie;
622 }
623 #define Q_XCB_CALL(x) q_xcb_call_template(x, connection(), __FILE__, __LINE__)
624 #define Q_XCB_CALL2(x, connection) q_xcb_call_template(x, connection, __FILE__, __LINE__)
625 #define Q_XCB_NOOP(c) q_xcb_call_template(xcb_no_operation(c->xcb_connection()), c, __FILE__, __LINE__);
626 #else
627 #define Q_XCB_CALL(x) x
628 #define Q_XCB_CALL2(x, connection) x
629 #define Q_XCB_NOOP(c)
630 #endif
631
632
633 #if defined(XCB_USE_EGL)
634 #define EGL_DISPLAY_FROM_XCB(object) ((EGLDisplay)(object->connection()->egl_display()))
635 #endif
636
637 QT_END_NAMESPACE
638
639 #endif