Debian Codegraph @

xcb/libqt5xcbqpa-dev/5.3.0/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 signals:
278 void eventPending();
279
280 private slots:
281 void registerForEvents();
282
283 private:
284 void addEvent(xcb_generic_event_t *event);
285
286 QMutex m_mutex;
287 QXcbEventArray m_events;
288 QXcbConnection *m_connection;
289
290 typedef xcb_generic_event_t * (*XcbPollForQueuedEventFunctionPointer)(xcb_connection_t *c);
291 XcbPollForQueuedEventFunctionPointer m_xcb_poll_for_queued_event;
292 };
293
294 class QXcbWindowEventListener
295 {
296 public:
297 virtual bool handleGenericEvent(xcb_generic_event_t *, long *) { return false; }
298
299 virtual void handleExposeEvent(const xcb_expose_event_t *) {}
300 virtual void handleClientMessageEvent(const xcb_client_message_event_t *) {}
301 virtual void handleConfigureNotifyEvent(const xcb_configure_notify_event_t *) {}
302 virtual void handleMapNotifyEvent(const xcb_map_notify_event_t *) {}
303 virtual void handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *) {}
304 virtual void handleDestroyNotifyEvent(const xcb_destroy_notify_event_t *) {}
305 virtual void handleButtonPressEvent(const xcb_button_press_event_t *) {}
306 virtual void handleButtonReleaseEvent(const xcb_button_release_event_t *) {}
307 virtual void handleMotionNotifyEvent(const xcb_motion_notify_event_t *) {}
308 virtual void handleEnterNotifyEvent(const xcb_enter_notify_event_t *) {}
309 virtual void handleLeaveNotifyEvent(const xcb_leave_notify_event_t *) {}
310 virtual void handleFocusInEvent(const xcb_focus_in_event_t *) {}
311 virtual void handleFocusOutEvent(const xcb_focus_out_event_t *) {}
312 virtual void handlePropertyNotifyEvent(const xcb_property_notify_event_t *) {}
313
314 virtual QXcbWindow *toWindow() { return 0; }
315 };
316
317 typedef QHash<xcb_window_t, QXcbWindowEventListener *> WindowMapper;
318
319 class QAbstractEventDispatcher;
320 class QXcbConnection : public QObject
321 {
322 Q_OBJECT
323 public:
324 QXcbConnection(QXcbNativeInterface *nativeInterface, bool canGrabServer, const char *displayName = 0);
325 ~QXcbConnection();
326
327 QXcbConnection *connection() const { return const_cast<QXcbConnection *>(this); }
328
329 const QList<QXcbScreen *> &screens() const { return m_screens; }
330 int primaryScreen() const { return m_primaryScreen; }
331
332 inline xcb_atom_t atom(QXcbAtom::Atom atom) const { return m_allAtoms[atom]; }
333 QXcbAtom::Atom qatom(xcb_atom_t atom) const;
334 xcb_atom_t internAtom(const char *name);
335 QByteArray atomName(xcb_atom_t atom);
336
337 const char *displayName() const { return m_displayName.constData(); }
338 xcb_connection_t *xcb_connection() const { return m_connection; }
339 const xcb_setup_t *setup() const { return m_setup; }
340 const xcb_format_t *formatForDepth(uint8_t depth) const;
341
342 QXcbKeyboard *keyboard() const { return m_keyboard; }
343
344 #ifndef QT_NO_CLIPBOARD
345 QXcbClipboard *clipboard() const { return m_clipboard; }
346 #endif
347 #ifndef QT_NO_DRAGANDDROP
348 QXcbDrag *drag() const { return m_drag; }
349 #endif
350
351 QXcbWMSupport *wmSupport() const { return m_wmSupport.data(); }
352 xcb_window_t rootWindow();
353 #ifdef XCB_USE_XLIB
354 void *xlib_display() const { return m_xlib_display; }
355 #endif
356
357 #ifdef XCB_USE_EGL
358 bool hasEgl() const;
359 #endif
360 #if defined(XCB_USE_EGL)
361 void *egl_display() const { return m_egl_display; }
362 #endif
363 #ifdef XCB_USE_XINPUT2_MAEMO
364 bool isUsingXInput2Maemo();
365 #elif defined(XCB_USE_XINPUT2)
366 void xi2Select(xcb_window_t window);
367 #endif
368 #ifdef XCB_USE_XINPUT21
369 bool isUsingXInput21() { return m_xi2Enabled && m_xi2Minor >= 1; }
370 #else
371 bool isUsingXInput21() { return false; }
372 #endif
373
374
375 void sync();
376 void flush() { xcb_flush(m_connection); }
377
378 void handleXcbError(xcb_generic_error_t *error);
379 void handleXcbEvent(xcb_generic_event_t *event);
380
381 void addWindowEventListener(xcb_window_t id, QXcbWindowEventListener *eventListener);
382 void removeWindowEventListener(xcb_window_t id);
383 QXcbWindowEventListener *windowEventListenerFromId(xcb_window_t id);
384 QXcbWindow *platformWindowFromId(xcb_window_t id);
385
386 xcb_generic_event_t *checkEvent(int type);
387 template<typename T>
388 inline xcb_generic_event_t *checkEvent(T &checker);
389
390 typedef bool (*PeekFunc)(QXcbConnection *, xcb_generic_event_t *);
391 void addPeekFunc(PeekFunc f);
392
393 inline xcb_timestamp_t time() const { return m_time; }
394 inline void setTime(xcb_timestamp_t t) { if (t > m_time) m_time = t; }
395
396 inline xcb_timestamp_t netWmUserTime() const { return m_netWmUserTime; }
397 inline void setNetWmUserTime(xcb_timestamp_t t) { if (t > m_netWmUserTime) m_netWmUserTime = t; }
398
399 bool hasGLX() const { return has_glx_extension; }
400 bool hasXFixes() const { return xfixes_first_event > 0; }
401 bool hasXShape() const { return has_shape_extension; }
402 bool hasXRandr() const { return has_randr_extension; }
403 bool hasInputShape() const { return has_input_shape; }
404 bool hasTouchWithoutMouseEmulation() const { return has_touch_without_mouse_emulation; }
405 bool hasXKB() const { return has_xkb; }
406
407 bool supportsThreadedRendering() const { return m_reader->isRunning(); }
408
409 xcb_timestamp_t getTimestamp();
410
411 Qt::MouseButtons buttons() const { return m_buttons; }
412
413 QXcbWindow *focusWindow() const { return m_focusWindow; }
414 void setFocusWindow(QXcbWindow *);
415
416 QByteArray startupId() const { return m_startupId; }
417 void clearStartupId() { m_startupId.clear(); }
418
419 void grabServer();
420 void ungrabServer();
421
422 QXcbNativeInterface *nativeInterface() const { return m_nativeInterface; }
423
424 QXcbSystemTrayTracker *systemTrayTracker();
425
426 #ifdef XCB_USE_XINPUT2
427 void handleEnterEvent(const xcb_enter_notify_event_t *);
428 #endif
429
430 private slots:
431 void processXcbEvents();
432
433 private:
434 void initializeAllAtoms();
435 void sendConnectionEvent(QXcbAtom::Atom atom, uint id = 0);
436 void initializeGLX();
437 void initializeXFixes();
438 void initializeXRender();
439 void initializeXRandr();
440 void initializeXShape();
441 void initializeXKB();
442 #ifdef XCB_USE_XINPUT2_MAEMO
443 void initializeXInput2Maemo();
444 void finalizeXInput2Maemo();
445 void handleGenericEventMaemo(xcb_ge_event_t *event);
446 #endif
447 void handleClientMessageEvent(const xcb_client_message_event_t *event);
448 QXcbScreen* findOrCreateScreen(QList<QXcbScreen *>& newScreens, int screenNumber,
449 xcb_screen_t* xcbScreen, xcb_randr_get_output_info_reply_t *output = NULL);
450 void updateScreens();
451 void handleButtonPress(xcb_generic_event_t *event);
452 void handleButtonRelease(xcb_generic_event_t *event);
453
454 bool m_xi2Enabled;
455 int m_xi2Minor;
456 #ifdef XCB_USE_XINPUT2
457 void initializeXInput2();
458 void finalizeXInput2();
459 XInput2DeviceData *deviceForId(int id);
460 void xi2HandleEvent(xcb_ge_event_t *event);
461 int m_xiOpCode, m_xiEventBase, m_xiErrorBase;
462 #ifndef QT_NO_TABLETEVENT
463 struct TabletData {
464 TabletData() : deviceId(0), down(false), serialId(0), inProximity(false) { }
465 int deviceId;
466 QTabletEvent::PointerType pointerType;
467 bool down;
468 qint64 serialId;
469 bool inProximity;
470 struct ValuatorClassInfo {
471 ValuatorClassInfo() : minVal(0), maxVal(0) { }
472 double minVal;
473 double maxVal;
474 int number;
475 };
476 QHash<int, ValuatorClassInfo> valuatorInfo;
477 };
478 bool xi2HandleTabletEvent(void *event, TabletData *tabletData);
479 void xi2ReportTabletEvent(const TabletData &tabletData, void *event);
480 QVector<TabletData> m_tabletData;
481 #endif
482 struct ScrollingDevice {
483 ScrollingDevice() : deviceId(0), verticalIndex(0), horizontalIndex(0), orientations(0), legacyOrientations(0) { }
484 int deviceId;
485 int verticalIndex, horizontalIndex;
486 double verticalIncrement, horizontalIncrement;
487 Qt::Orientations orientations;
488 Qt::Orientations legacyOrientations;
489 QPointF lastScrollPosition;
490 };
491 void xi2HandleScrollEvent(void *event, ScrollingDevice &scrollingDevice);
492 QHash<int, ScrollingDevice> m_scrollingDevices;
493 #endif // XCB_USE_XINPUT2
494
495 #if defined(XCB_USE_XINPUT2) || defined(XCB_USE_XINPUT2_MAEMO)
496 static bool xi2GetValuatorValueIfSet(void *event, int valuatorNum, double *value);
497 static bool xi2PrepareXIGenericDeviceEvent(xcb_ge_event_t *event, int opCode);
498 #endif
499
500 xcb_connection_t *m_connection;
501 const xcb_setup_t *m_setup;
502 bool m_canGrabServer;
503
504 QList<QXcbScreen *> m_screens;
505 int m_primaryScreen;
506
507 xcb_atom_t m_allAtoms[QXcbAtom::NAtoms];
508
509 xcb_timestamp_t m_time;
510 xcb_timestamp_t m_netWmUserTime;
511
512 QByteArray m_displayName;
513
514 QXcbKeyboard *m_keyboard;
515 #ifndef QT_NO_CLIPBOARD
516 QXcbClipboard *m_clipboard;
517 #endif
518 #ifndef QT_NO_DRAGANDDROP
519 QXcbDrag *m_drag;
520 #endif
521 QScopedPointer<QXcbWMSupport> m_wmSupport;
522 QXcbNativeInterface *m_nativeInterface;
523
524 #if defined(XCB_USE_XLIB)
525 void *m_xlib_display;
526 #endif
527 QXcbEventReader *m_reader;
528 #ifdef XCB_USE_XINPUT2_MAEMO
529 XInput2MaemoData *m_xinputData;
530 #elif defined(XCB_USE_XINPUT2)
531 QHash<int, QWindowSystemInterface::TouchPoint> m_touchPoints;
532 QHash<int, XInput2DeviceData*> m_touchDevices;
533 #endif
534 #if defined(XCB_USE_EGL)
535 void *m_egl_display;
536 bool m_has_egl;
537 #endif
538 #ifdef Q_XCB_DEBUG
539 struct CallInfo {
540 int sequence;
541 QByteArray file;
542 int line;
543 };
544 QVector<CallInfo> m_callLog;
545 QMutex m_callLogMutex;
546 void log(const char *file, int line, int sequence);
547 template <typename cookie_t>
548 friend cookie_t q_xcb_call_template(const cookie_t &cookie, QXcbConnection *connection, const char *file, int line);
549 #endif
550
551 WindowMapper m_mapper;
552
553 QVector<PeekFunc> m_peekFuncs;
554
555 uint32_t xfixes_first_event;
556 uint32_t xrandr_first_event;
557 uint32_t xkb_first_event;
558
559 bool has_glx_extension;
560 bool has_shape_extension;
561 bool has_randr_extension;
562 bool has_input_shape;
563 bool has_touch_without_mouse_emulation;
564 bool has_xkb;
565 bool debug_xinput_devices;
566 bool debug_xinput;
567
568 Qt::MouseButtons m_buttons;
569
570 QXcbWindow *m_focusWindow;
571
572 QByteArray m_startupId;
573 QXcbSystemTrayTracker *m_systemTrayTracker;
574
575 friend class QXcbEventReader;
576 };
577
578 #define DISPLAY_FROM_XCB(object) ((Display *)(object->connection()->xlib_display()))
579
580 template<typename T>
581 xcb_generic_event_t *QXcbConnection::checkEvent(T &checker)
582 {
583 QXcbEventArray *eventqueue = m_reader->lock();
584
585 for (int i = 0; i < eventqueue->size(); ++i) {
586 xcb_generic_event_t *event = eventqueue->at(i);
587 if (checker.checkEvent(event)) {
588 (*eventqueue)[i] = 0;
589 m_reader->unlock();
590 return event;
591 }
592 }
593 m_reader->unlock();
594 return 0;
595 }
596
597 class QXcbConnectionGrabber
598 {
599 public:
600 QXcbConnectionGrabber(QXcbConnection *connection);
601 ~QXcbConnectionGrabber();
602 void release();
603 private:
604 QXcbConnection *m_connection;
605 };
606
607 #ifdef Q_XCB_DEBUG
608 template <typename cookie_t>
609 cookie_t q_xcb_call_template(const cookie_t &cookie, QXcbConnection *connection, const char *file, int line)
610 {
611 connection->log(file, line, cookie.sequence);
612 return cookie;
613 }
614 #define Q_XCB_CALL(x) q_xcb_call_template(x, connection(), __FILE__, __LINE__)
615 #define Q_XCB_CALL2(x, connection) q_xcb_call_template(x, connection, __FILE__, __LINE__)
616 #define Q_XCB_NOOP(c) q_xcb_call_template(xcb_no_operation(c->xcb_connection()), c, __FILE__, __LINE__);
617 #else
618 #define Q_XCB_CALL(x) x
619 #define Q_XCB_CALL2(x, connection) x
620 #define Q_XCB_NOOP(c)
621 #endif
622
623
624 #if defined(XCB_USE_EGL)
625 #define EGL_DISPLAY_FROM_XCB(object) ((EGLDisplay)(object->connection()->egl_display()))
626 #endif
627
628 QT_END_NAMESPACE
629
630 #endif