1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the plugins of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qxcbconnection.h"
43 #include "qxcbkeyboard.h"
44 #include "qxcbscreen.h"
45 #include "qxcbwindow.h"
46 #include "qxcbclipboard.h"
48 #include <QtAlgorithms>
49 #include <QSocketNotifier>
50 #include <QtGui/private/qguiapplication_p.h>
51 #include <QAbstractEventDispatcher>
53 #include <QtCore/QDebug>
60 #include <X11/Xlib-xcb.h>
63 #ifdef XCB_USE_EGL //dont pull in eglext prototypes
69 #include <xcb/xfixes.h>
73 #define MESA_EGL_NO_X11_HEADERS
74 #define EGL_EGLEXT_PROTOTYPES
76 #include <EGL/eglext.h>
79 QXcbConnection::QXcbConnection(const char *displayName)
80 : m_displayName(displayName ? QByteArray(displayName) : qgetenv("DISPLAY"))
84 , m_dri2_support_probed(false)
85 , m_has_support_for_dri2(false)
91 Display *dpy = XOpenDisplay(m_displayName.constData());
92 m_primaryScreen = DefaultScreen(dpy);
93 m_connection = XGetXCBConnection(dpy);
94 XSetEventQueueOwner(dpy, XCBOwnsEventQueue);
97 EGLDisplay eglDisplay = eglGetDisplay(dpy);
98 m_egl_display = eglDisplay;
100 eglBindAPI(EGL_OPENGL_ES_API);
101 m_has_egl = eglInitialize(eglDisplay,&major,&minor);
104 m_connection = xcb_connect(m_displayName.constData(), &primaryScreen);
106 #endif //XCB_USE_XLIB
107 m_setup = xcb_get_setup(xcb_connection());
109 initializeAllAtoms();
111 xcb_screen_iterator_t it = xcb_setup_roots_iterator(m_setup);
113 int screenNumber = 0;
115 m_screens << new QXcbScreen(this, it.data, screenNumber++);
116 xcb_screen_next(&it);
119 m_keyboard = new QXcbKeyboard(this);
120 m_clipboard = new QXcbClipboard(this);
126 QSocketNotifier *notifier = new QSocketNotifier(xcb_get_file_descriptor(xcb_connection()), QSocketNotifier::Read, this);
127 connect(notifier, SIGNAL(activated(int)), this, SLOT(processXcbEvents()));
129 QAbstractEventDispatcher *dispatcher = QAbstractEventDispatcher::instance(qApp->thread());
130 connect(dispatcher, SIGNAL(aboutToBlock()), this, SLOT(processXcbEvents()));
135 QXcbConnection::~QXcbConnection()
137 qDeleteAll(m_screens);
140 XCloseDisplay((Display *)m_xlib_display);
142 xcb_disconnect(xcb_connection());
149 void QXcbConnection::addWindow(xcb_window_t id, QXcbWindow *window)
151 m_mapper.insert(id, window);
154 void QXcbConnection::removeWindow(xcb_window_t id)
159 QXcbWindow *QXcbConnection::platformWindowFromId(xcb_window_t id)
161 return m_mapper.value(id, 0);
164 #define HANDLE_PLATFORM_WINDOW_EVENT(event_t, windowMember, handler) \
166 event_t *e = (event_t *)event; \
167 if (QXcbWindow *platformWindow = platformWindowFromId(e->windowMember)) \
168 platformWindow->handler(e); \
172 #define HANDLE_KEYBOARD_EVENT(event_t, handler) \
174 event_t *e = (event_t *)event; \
175 if (QXcbWindow *platformWindow = platformWindowFromId(e->event)) \
176 m_keyboard->handler(platformWindow->window(), e); \
180 //#define XCB_EVENT_DEBUG
182 void printXcbEvent(const char *message, xcb_generic_event_t *event)
184 #ifdef XCB_EVENT_DEBUG
185 #define PRINT_XCB_EVENT(ev) \
187 printf("%s: %d - %s - sequence: %d\n", message, int(ev), #ev, event->sequence); \
190 switch (event->response_type & ~0x80) {
191 PRINT_XCB_EVENT(XCB_KEY_PRESS);
192 PRINT_XCB_EVENT(XCB_KEY_RELEASE);
193 PRINT_XCB_EVENT(XCB_BUTTON_PRESS);
194 PRINT_XCB_EVENT(XCB_BUTTON_RELEASE);
195 PRINT_XCB_EVENT(XCB_MOTION_NOTIFY);
196 PRINT_XCB_EVENT(XCB_ENTER_NOTIFY);
197 PRINT_XCB_EVENT(XCB_LEAVE_NOTIFY);
198 PRINT_XCB_EVENT(XCB_FOCUS_IN);
199 PRINT_XCB_EVENT(XCB_FOCUS_OUT);
200 PRINT_XCB_EVENT(XCB_KEYMAP_NOTIFY);
201 PRINT_XCB_EVENT(XCB_EXPOSE);
202 PRINT_XCB_EVENT(XCB_GRAPHICS_EXPOSURE);
203 PRINT_XCB_EVENT(XCB_VISIBILITY_NOTIFY);
204 PRINT_XCB_EVENT(XCB_CREATE_NOTIFY);
205 PRINT_XCB_EVENT(XCB_DESTROY_NOTIFY);
206 PRINT_XCB_EVENT(XCB_UNMAP_NOTIFY);
207 PRINT_XCB_EVENT(XCB_MAP_NOTIFY);
208 PRINT_XCB_EVENT(XCB_MAP_REQUEST);
209 PRINT_XCB_EVENT(XCB_REPARENT_NOTIFY);
210 PRINT_XCB_EVENT(XCB_CONFIGURE_NOTIFY);
211 PRINT_XCB_EVENT(XCB_CONFIGURE_REQUEST);
212 PRINT_XCB_EVENT(XCB_GRAVITY_NOTIFY);
213 PRINT_XCB_EVENT(XCB_RESIZE_REQUEST);
214 PRINT_XCB_EVENT(XCB_CIRCULATE_NOTIFY);
215 PRINT_XCB_EVENT(XCB_CIRCULATE_REQUEST);
216 PRINT_XCB_EVENT(XCB_PROPERTY_NOTIFY);
217 PRINT_XCB_EVENT(XCB_SELECTION_CLEAR);
218 PRINT_XCB_EVENT(XCB_SELECTION_REQUEST);
219 PRINT_XCB_EVENT(XCB_SELECTION_NOTIFY);
220 PRINT_XCB_EVENT(XCB_COLORMAP_NOTIFY);
221 PRINT_XCB_EVENT(XCB_CLIENT_MESSAGE);
222 PRINT_XCB_EVENT(XCB_MAPPING_NOTIFY);
224 printf("%s: unknown event - response_type: %d - sequence: %d\n", message, int(event->response_type & ~0x80), int(event->sequence));
232 const char *xcb_errors[] =
255 const char *xcb_protocol_request_codes[] =
259 "ChangeWindowAttributes",
260 "GetWindowAttributes",
287 "ChangeActivePointerGrab",
337 "CopyColormapAndFree",
340 "ListInstalledColormaps",
357 "ChangeKeyboardMapping",
358 "GetKeyboardMapping",
359 "ChangeKeyboardControl",
360 "GetKeyboardControl",
362 "ChangePointerControl",
375 "SetModifierMapping",
376 "GetModifierMapping",
381 void QXcbConnection::log(const char *file, int line, int sequence)
384 info.sequence = sequence;
391 void QXcbConnection::handleXcbError(xcb_generic_error_t *error)
393 uint clamped_error_code = qMin<uint>(error->error_code, (sizeof(xcb_errors) / sizeof(xcb_errors[0])) - 1);
394 uint clamped_major_code = qMin<uint>(error->major_code, (sizeof(xcb_protocol_request_codes) / sizeof(xcb_protocol_request_codes[0])) - 1);
396 printf("XCB error: %d (%s), sequence: %d, resource id: %d, major code: %d (%s), minor code: %d\n",
397 int(error->error_code), xcb_errors[clamped_error_code],
398 int(error->sequence), int(error->resource_id),
399 int(error->major_code), xcb_protocol_request_codes[clamped_major_code],
400 int(error->minor_code));
403 for (; i < m_callLog.size(); ++i) {
404 if (m_callLog.at(i).sequence == error->sequence) {
405 printf("Caused by: %s:%d\n", qPrintable(m_callLog.at(i).file), m_callLog.at(i).line);
407 } else if (m_callLog.at(i).sequence > error->sequence) {
408 printf("Caused some time before: %s:%d\n", qPrintable(m_callLog.at(i).file), m_callLog.at(i).line);
410 printf("and after: %s:%d\n", qPrintable(m_callLog.at(i-1).file), m_callLog.at(i-1).line);
414 if (i == m_callLog.size() && !m_callLog.isEmpty())
415 printf("Caused some time after: %s:%d\n", qPrintable(m_callLog.first().file), m_callLog.first().line);
419 void QXcbConnection::handleXcbEvent(xcb_generic_event_t *event)
424 for (; i < m_callLog.size(); ++i)
425 if (m_callLog.at(i).sequence >= event->sequence)
427 m_callLog.remove(0, i);
432 uint response_type = event->response_type & ~0x80;
434 switch (response_type) {
436 HANDLE_PLATFORM_WINDOW_EVENT(xcb_expose_event_t, window, handleExposeEvent);
437 case XCB_BUTTON_PRESS:
438 HANDLE_PLATFORM_WINDOW_EVENT(xcb_button_press_event_t, event, handleButtonPressEvent);
439 case XCB_BUTTON_RELEASE:
440 HANDLE_PLATFORM_WINDOW_EVENT(xcb_button_release_event_t, event, handleButtonReleaseEvent);
441 case XCB_MOTION_NOTIFY:
442 HANDLE_PLATFORM_WINDOW_EVENT(xcb_motion_notify_event_t, event, handleMotionNotifyEvent);
443 case XCB_CONFIGURE_NOTIFY:
444 HANDLE_PLATFORM_WINDOW_EVENT(xcb_configure_notify_event_t, event, handleConfigureNotifyEvent);
446 HANDLE_PLATFORM_WINDOW_EVENT(xcb_map_notify_event_t, event, handleMapNotifyEvent);
447 case XCB_CLIENT_MESSAGE:
448 HANDLE_PLATFORM_WINDOW_EVENT(xcb_client_message_event_t, window, handleClientMessageEvent);
449 case XCB_ENTER_NOTIFY:
450 HANDLE_PLATFORM_WINDOW_EVENT(xcb_enter_notify_event_t, event, handleEnterNotifyEvent);
451 case XCB_LEAVE_NOTIFY:
452 HANDLE_PLATFORM_WINDOW_EVENT(xcb_leave_notify_event_t, event, handleLeaveNotifyEvent);
454 HANDLE_PLATFORM_WINDOW_EVENT(xcb_focus_in_event_t, event, handleFocusInEvent);
456 HANDLE_PLATFORM_WINDOW_EVENT(xcb_focus_out_event_t, event, handleFocusOutEvent);
458 HANDLE_KEYBOARD_EVENT(xcb_key_press_event_t, handleKeyPressEvent);
459 case XCB_KEY_RELEASE:
460 HANDLE_KEYBOARD_EVENT(xcb_key_release_event_t, handleKeyReleaseEvent);
461 case XCB_MAPPING_NOTIFY:
462 m_keyboard->handleMappingNotifyEvent((xcb_mapping_notify_event_t *)event);
464 case XCB_SELECTION_REQUEST:
465 m_clipboard->handleSelectionRequest((xcb_selection_request_event_t *)event);
467 case XCB_SELECTION_CLEAR:
468 qDebug() << "XCB_SELECTION_CLEAR";
471 case XCB_SELECTION_NOTIFY:
472 qDebug() << "XCB_SELECTION_NOTIFY";
480 printXcbEvent("Handled XCB event", event);
482 printXcbEvent("Unhandled XCB event", event);
485 void QXcbConnection::processXcbEvents()
487 while (xcb_generic_event_t *event = xcb_poll_for_event(xcb_connection()))
488 eventqueue.append(event);
490 for(int i = 0; i < eventqueue.size(); ++i) {
491 xcb_generic_event_t *event = eventqueue.at(i);
495 uint response_type = event->response_type & ~0x80;
497 if (!response_type) {
498 handleXcbError((xcb_generic_error_t *)event);
500 handleXcbEvent(event);
508 xcb_flush(xcb_connection());
511 xcb_generic_event_t *QXcbConnection::checkEvent(int type)
513 while (xcb_generic_event_t *event = xcb_poll_for_event(xcb_connection()))
514 eventqueue.append(event);
516 for (int i = 0; i < eventqueue.size(); ++i) {
517 xcb_generic_event_t *event = eventqueue.at(i);
518 if (event->response_type == type) {
526 static const char * xcb_atomnames = {
527 // window-manager <-> client protocols
532 "_NET_WM_CONTEXT_HELP\0"
533 "_NET_WM_SYNC_REQUEST\0"
534 "_NET_WM_SYNC_REQUEST_COUNTER\0"
536 // ICCCM window state
540 // Session management
554 "_QT_CLIPBOARD_SENTINEL\0"
555 "_QT_SELECTION_SENTINEL\0"
556 "CLIPBOARD_MANAGER\0"
563 "_QT_INPUT_ENCODING\0"
568 "ENLIGHTENMENT_DESKTOP\0"
570 "_SGI_DESKS_MANAGER\0"
574 "_NET_VIRTUAL_ROOTS\0"
577 "_NET_MOVERESIZE_WINDOW\0"
578 "_NET_WM_MOVERESIZE\0"
581 "_NET_WM_ICON_NAME\0"
586 "_NET_WM_WINDOW_OPACITY\0"
589 "_NET_WM_STATE_ABOVE\0"
590 "_NET_WM_STATE_BELOW\0"
591 "_NET_WM_STATE_FULLSCREEN\0"
592 "_NET_WM_STATE_MAXIMIZED_HORZ\0"
593 "_NET_WM_STATE_MAXIMIZED_VERT\0"
594 "_NET_WM_STATE_MODAL\0"
595 "_NET_WM_STATE_STAYS_ON_TOP\0"
596 "_NET_WM_STATE_DEMANDS_ATTENTION\0"
598 "_NET_WM_USER_TIME\0"
599 "_NET_WM_USER_TIME_WINDOW\0"
600 "_NET_WM_FULL_PLACEMENT\0"
602 "_NET_WM_WINDOW_TYPE\0"
603 "_NET_WM_WINDOW_TYPE_DESKTOP\0"
604 "_NET_WM_WINDOW_TYPE_DOCK\0"
605 "_NET_WM_WINDOW_TYPE_TOOLBAR\0"
606 "_NET_WM_WINDOW_TYPE_MENU\0"
607 "_NET_WM_WINDOW_TYPE_UTILITY\0"
608 "_NET_WM_WINDOW_TYPE_SPLASH\0"
609 "_NET_WM_WINDOW_TYPE_DIALOG\0"
610 "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU\0"
611 "_NET_WM_WINDOW_TYPE_POPUP_MENU\0"
612 "_NET_WM_WINDOW_TYPE_TOOLTIP\0"
613 "_NET_WM_WINDOW_TYPE_NOTIFICATION\0"
614 "_NET_WM_WINDOW_TYPE_COMBO\0"
615 "_NET_WM_WINDOW_TYPE_DND\0"
616 "_NET_WM_WINDOW_TYPE_NORMAL\0"
617 "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE\0"
619 "_KDE_NET_WM_FRAME_STRUT\0"
621 "_NET_STARTUP_INFO\0"
622 "_NET_STARTUP_INFO_BEGIN\0"
624 "_NET_SUPPORTING_WM_CHECK\0"
628 "_NET_SYSTEM_TRAY_VISUAL\0"
630 "_NET_ACTIVE_WINDOW\0"
654 "XdndActionPrivate\0"
657 "_MOTIF_DRAG_AND_DROP_MESSAGE\0"
658 "_MOTIF_DRAG_INITIATOR_INFO\0"
659 "_MOTIF_DRAG_RECEIVER_INFO\0"
660 "_MOTIF_DRAG_WINDOW\0"
661 "_MOTIF_DRAG_TARGETS\0"
663 "XmTRANSFER_SUCCESS\0"
664 "XmTRANSFER_FAILURE\0"
673 // Wacom old. (before version 0.10)
683 xcb_atom_t QXcbConnection::atom(QXcbAtom::Atom atom)
685 return m_allAtoms[atom];
688 void QXcbConnection::initializeAllAtoms() {
689 const char *names[QXcbAtom::NAtoms];
690 const char *ptr = xcb_atomnames;
700 Q_ASSERT(i == QXcbAtom::NPredefinedAtoms);
702 QByteArray settings_atom_name("_QT_SETTINGS_TIMESTAMP_");
703 settings_atom_name += m_displayName;
704 names[i++] = settings_atom_name;
706 xcb_intern_atom_cookie_t cookies[QXcbAtom::NAtoms];
708 Q_ASSERT(i == QXcbAtom::NAtoms);
709 for (i = 0; i < QXcbAtom::NAtoms; ++i)
710 cookies[i] = xcb_intern_atom(xcb_connection(), false, strlen(names[i]), names[i]);
712 for (i = 0; i < QXcbAtom::NAtoms; ++i) {
713 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(xcb_connection(), cookies[i], 0);
714 m_allAtoms[i] = reply->atom;
719 xcb_atom_t QXcbConnection::internAtom(const char *name)
721 if (!name || *name == 0)
724 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(xcb_connection(), false, strlen(name), name);
725 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(xcb_connection(), cookie, 0);
726 int atom = reply->atom;
731 QByteArray QXcbConnection::atomName(xcb_atom_t atom)
733 xcb_get_atom_name_cookie_t cookie = xcb_get_atom_name_unchecked(xcb_connection(), atom);
734 xcb_get_atom_name_reply_t *reply = xcb_get_atom_name_reply(xcb_connection(), cookie, 0);
736 QByteArray result(xcb_get_atom_name_name(reply), xcb_get_atom_name_name_length(reply));
743 void QXcbConnection::sync()
746 xcb_get_input_focus_cookie_t cookie = Q_XCB_CALL(xcb_get_input_focus(xcb_connection()));
747 free(xcb_get_input_focus_reply(xcb_connection(), cookie, 0));
750 #if defined(XCB_USE_EGL)
751 bool QXcbConnection::hasEgl() const
755 #endif // defined(XCB_USE_EGL)
758 void QXcbConnection::initializeDri2()
760 xcb_dri2_connect_cookie_t connect_cookie = xcb_dri2_connect_unchecked (m_connection,
761 m_screens[0]->root(),
762 XCB_DRI2_DRIVER_TYPE_DRI);
764 xcb_dri2_connect_reply_t *connect = xcb_dri2_connect_reply (m_connection,
765 connect_cookie, NULL);
767 if (! connect || connect->driver_name_length + connect->device_name_length == 0) {
768 qDebug() << "Failed to connect to dri2";
772 m_dri2_device_name = QByteArray(xcb_dri2_connect_device_name (connect),
773 xcb_dri2_connect_device_name_length (connect));
776 int fd = open(m_dri2_device_name.constData(), O_RDWR);
778 qDebug() << "InitializeDri2: Could'nt open device << dri2DeviceName";
779 m_dri2_device_name = QByteArray();
784 if (drmGetMagic(fd, &magic)) {
785 qDebug() << "Failed to get drmMagic";
789 xcb_dri2_authenticate_cookie_t authenticate_cookie = xcb_dri2_authenticate_unchecked(m_connection,
790 m_screens[0]->root(), magic);
791 xcb_dri2_authenticate_reply_t *authenticate = xcb_dri2_authenticate_reply(m_connection,
792 authenticate_cookie, NULL);
793 if (authenticate == NULL || !authenticate->authenticated) {
794 fprintf(stderr, "DRI2: failed to authenticate\n");
801 EGLDisplay display = eglGetDRMDisplayMESA(fd);
803 fprintf(stderr, "failed to create display\n");
807 m_egl_display = display;
809 if (!eglInitialize(display, &major, &minor)) {
810 fprintf(stderr, "failed to initialize display\n");
815 bool QXcbConnection::hasSupportForDri2() const
817 if (!m_dri2_support_probed) {
818 xcb_generic_error_t *error = 0;
820 xcb_prefetch_extension_data (m_connection, &xcb_xfixes_id);
821 xcb_prefetch_extension_data (m_connection, &xcb_dri2_id);
823 xcb_xfixes_query_version_cookie_t xfixes_query_cookie = xcb_xfixes_query_version(m_connection,
824 XCB_XFIXES_MAJOR_VERSION,
825 XCB_XFIXES_MINOR_VERSION);
827 xcb_dri2_query_version_cookie_t dri2_query_cookie = xcb_dri2_query_version (m_connection,
828 XCB_DRI2_MAJOR_VERSION,
829 XCB_DRI2_MINOR_VERSION);
831 xcb_xfixes_query_version_reply_t *xfixes_query = xcb_xfixes_query_version_reply (m_connection,
832 xfixes_query_cookie, &error);
833 if (!xfixes_query || error || xfixes_query->major_version < 2) {
840 xcb_dri2_query_version_reply_t *dri2_query = xcb_dri2_query_version_reply (m_connection,
841 dri2_query_cookie, &error);
842 if (!dri2_query || error) {
848 QXcbConnection *that = const_cast<QXcbConnection *>(this);
849 that->m_dri2_major = dri2_query->major_version;
850 that->m_dri2_minor = dri2_query->minor_version;
852 that->m_has_support_for_dri2 = true;
853 that->m_dri2_support_probed = true;
855 return m_has_support_for_dri2;
857 #endif //XCB_USE_DRI2