Debian Codegraph @

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