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);
445 case XCB_CLIENT_MESSAGE:
446 HANDLE_PLATFORM_WINDOW_EVENT(xcb_client_message_event_t, window, handleClientMessageEvent);
447 case XCB_ENTER_NOTIFY:
448 HANDLE_PLATFORM_WINDOW_EVENT(xcb_enter_notify_event_t, event, handleEnterNotifyEvent);
449 case XCB_LEAVE_NOTIFY:
450 HANDLE_PLATFORM_WINDOW_EVENT(xcb_leave_notify_event_t, event, handleLeaveNotifyEvent);
452 HANDLE_PLATFORM_WINDOW_EVENT(xcb_focus_in_event_t, event, handleFocusInEvent);
454 HANDLE_PLATFORM_WINDOW_EVENT(xcb_focus_out_event_t, event, handleFocusOutEvent);
456 HANDLE_KEYBOARD_EVENT(xcb_key_press_event_t, handleKeyPressEvent);
457 case XCB_KEY_RELEASE:
458 HANDLE_KEYBOARD_EVENT(xcb_key_release_event_t, handleKeyReleaseEvent);
459 case XCB_MAPPING_NOTIFY:
460 m_keyboard->handleMappingNotifyEvent((xcb_mapping_notify_event_t *)event);
462 case XCB_SELECTION_REQUEST:
463 m_clipboard->handleSelectionRequest((xcb_selection_request_event_t *)event);
465 case XCB_SELECTION_CLEAR:
466 qDebug() << "XCB_SELECTION_CLEAR";
469 case XCB_SELECTION_NOTIFY:
470 qDebug() << "XCB_SELECTION_NOTIFY";
478 printXcbEvent("Handled XCB event", event);
480 printXcbEvent("Unhandled XCB event", event);
483 void QXcbConnection::processXcbEvents()
485 while (xcb_generic_event_t *event = xcb_poll_for_event(xcb_connection()))
486 eventqueue.append(event);
488 for(int i = 0; i < eventqueue.size(); ++i) {
489 xcb_generic_event_t *event = eventqueue.at(i);
493 uint response_type = event->response_type & ~0x80;
495 if (!response_type) {
496 handleXcbError((xcb_generic_error_t *)event);
498 handleXcbEvent(event);
506 xcb_flush(xcb_connection());
509 xcb_generic_event_t *QXcbConnection::checkEvent(int type)
511 while (xcb_generic_event_t *event = xcb_poll_for_event(xcb_connection()))
512 eventqueue.append(event);
514 for (int i = 0; i < eventqueue.size(); ++i) {
515 xcb_generic_event_t *event = eventqueue.at(i);
516 if (event->response_type == type) {
524 static const char * xcb_atomnames = {
525 // window-manager <-> client protocols
530 "_NET_WM_CONTEXT_HELP\0"
531 "_NET_WM_SYNC_REQUEST\0"
532 "_NET_WM_SYNC_REQUEST_COUNTER\0"
534 // ICCCM window state
538 // Session management
552 "_QT_CLIPBOARD_SENTINEL\0"
553 "_QT_SELECTION_SENTINEL\0"
554 "CLIPBOARD_MANAGER\0"
561 "_QT_INPUT_ENCODING\0"
566 "ENLIGHTENMENT_DESKTOP\0"
568 "_SGI_DESKS_MANAGER\0"
572 "_NET_VIRTUAL_ROOTS\0"
575 "_NET_MOVERESIZE_WINDOW\0"
576 "_NET_WM_MOVERESIZE\0"
579 "_NET_WM_ICON_NAME\0"
584 "_NET_WM_WINDOW_OPACITY\0"
587 "_NET_WM_STATE_ABOVE\0"
588 "_NET_WM_STATE_BELOW\0"
589 "_NET_WM_STATE_FULLSCREEN\0"
590 "_NET_WM_STATE_MAXIMIZED_HORZ\0"
591 "_NET_WM_STATE_MAXIMIZED_VERT\0"
592 "_NET_WM_STATE_MODAL\0"
593 "_NET_WM_STATE_STAYS_ON_TOP\0"
594 "_NET_WM_STATE_DEMANDS_ATTENTION\0"
596 "_NET_WM_USER_TIME\0"
597 "_NET_WM_USER_TIME_WINDOW\0"
598 "_NET_WM_FULL_PLACEMENT\0"
600 "_NET_WM_WINDOW_TYPE\0"
601 "_NET_WM_WINDOW_TYPE_DESKTOP\0"
602 "_NET_WM_WINDOW_TYPE_DOCK\0"
603 "_NET_WM_WINDOW_TYPE_TOOLBAR\0"
604 "_NET_WM_WINDOW_TYPE_MENU\0"
605 "_NET_WM_WINDOW_TYPE_UTILITY\0"
606 "_NET_WM_WINDOW_TYPE_SPLASH\0"
607 "_NET_WM_WINDOW_TYPE_DIALOG\0"
608 "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU\0"
609 "_NET_WM_WINDOW_TYPE_POPUP_MENU\0"
610 "_NET_WM_WINDOW_TYPE_TOOLTIP\0"
611 "_NET_WM_WINDOW_TYPE_NOTIFICATION\0"
612 "_NET_WM_WINDOW_TYPE_COMBO\0"
613 "_NET_WM_WINDOW_TYPE_DND\0"
614 "_NET_WM_WINDOW_TYPE_NORMAL\0"
615 "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE\0"
617 "_KDE_NET_WM_FRAME_STRUT\0"
619 "_NET_STARTUP_INFO\0"
620 "_NET_STARTUP_INFO_BEGIN\0"
622 "_NET_SUPPORTING_WM_CHECK\0"
626 "_NET_SYSTEM_TRAY_VISUAL\0"
628 "_NET_ACTIVE_WINDOW\0"
652 "XdndActionPrivate\0"
655 "_MOTIF_DRAG_AND_DROP_MESSAGE\0"
656 "_MOTIF_DRAG_INITIATOR_INFO\0"
657 "_MOTIF_DRAG_RECEIVER_INFO\0"
658 "_MOTIF_DRAG_WINDOW\0"
659 "_MOTIF_DRAG_TARGETS\0"
661 "XmTRANSFER_SUCCESS\0"
662 "XmTRANSFER_FAILURE\0"
671 // Wacom old. (before version 0.10)
681 xcb_atom_t QXcbConnection::atom(QXcbAtom::Atom atom)
683 return m_allAtoms[atom];
686 void QXcbConnection::initializeAllAtoms() {
687 const char *names[QXcbAtom::NAtoms];
688 const char *ptr = xcb_atomnames;
698 Q_ASSERT(i == QXcbAtom::NPredefinedAtoms);
700 QByteArray settings_atom_name("_QT_SETTINGS_TIMESTAMP_");
701 settings_atom_name += m_displayName;
702 names[i++] = settings_atom_name;
704 xcb_intern_atom_cookie_t cookies[QXcbAtom::NAtoms];
706 Q_ASSERT(i == QXcbAtom::NAtoms);
707 for (i = 0; i < QXcbAtom::NAtoms; ++i)
708 cookies[i] = xcb_intern_atom(xcb_connection(), false, strlen(names[i]), names[i]);
710 for (i = 0; i < QXcbAtom::NAtoms; ++i) {
711 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(xcb_connection(), cookies[i], 0);
712 m_allAtoms[i] = reply->atom;
717 xcb_atom_t QXcbConnection::internAtom(const char *name)
719 if (!name || *name == 0)
722 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(xcb_connection(), false, strlen(name), name);
723 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(xcb_connection(), cookie, 0);
724 int atom = reply->atom;
729 QByteArray QXcbConnection::atomName(xcb_atom_t atom)
731 xcb_get_atom_name_cookie_t cookie = xcb_get_atom_name_unchecked(xcb_connection(), atom);
732 xcb_get_atom_name_reply_t *reply = xcb_get_atom_name_reply(xcb_connection(), cookie, 0);
734 QByteArray result(xcb_get_atom_name_name(reply), xcb_get_atom_name_name_length(reply));
741 void QXcbConnection::sync()
744 xcb_get_input_focus_cookie_t cookie = Q_XCB_CALL(xcb_get_input_focus(xcb_connection()));
745 free(xcb_get_input_focus_reply(xcb_connection(), cookie, 0));
748 #if defined(XCB_USE_EGL)
749 bool QXcbConnection::hasEgl() const
753 #endif // defined(XCB_USE_EGL)
756 void QXcbConnection::initializeDri2()
758 xcb_dri2_connect_cookie_t connect_cookie = xcb_dri2_connect_unchecked (m_connection,
759 m_screens[0]->root(),
760 XCB_DRI2_DRIVER_TYPE_DRI);
762 xcb_dri2_connect_reply_t *connect = xcb_dri2_connect_reply (m_connection,
763 connect_cookie, NULL);
765 if (! connect || connect->driver_name_length + connect->device_name_length == 0) {
766 qDebug() << "Failed to connect to dri2";
770 m_dri2_device_name = QByteArray(xcb_dri2_connect_device_name (connect),
771 xcb_dri2_connect_device_name_length (connect));
774 int fd = open(m_dri2_device_name.constData(), O_RDWR);
776 qDebug() << "InitializeDri2: Could'nt open device << dri2DeviceName";
777 m_dri2_device_name = QByteArray();
782 if (drmGetMagic(fd, &magic)) {
783 qDebug() << "Failed to get drmMagic";
787 xcb_dri2_authenticate_cookie_t authenticate_cookie = xcb_dri2_authenticate_unchecked(m_connection,
788 m_screens[0]->root(), magic);
789 xcb_dri2_authenticate_reply_t *authenticate = xcb_dri2_authenticate_reply(m_connection,
790 authenticate_cookie, NULL);
791 if (authenticate == NULL || !authenticate->authenticated) {
792 fprintf(stderr, "DRI2: failed to authenticate\n");
799 EGLDisplay display = eglGetDRMDisplayMESA(fd);
801 fprintf(stderr, "failed to create display\n");
805 m_egl_display = display;
807 if (!eglInitialize(display, &major, &minor)) {
808 fprintf(stderr, "failed to initialize display\n");
813 bool QXcbConnection::hasSupportForDri2() const
815 if (!m_dri2_support_probed) {
816 xcb_generic_error_t *error = 0;
818 xcb_prefetch_extension_data (m_connection, &xcb_xfixes_id);
819 xcb_prefetch_extension_data (m_connection, &xcb_dri2_id);
821 xcb_xfixes_query_version_cookie_t xfixes_query_cookie = xcb_xfixes_query_version(m_connection,
822 XCB_XFIXES_MAJOR_VERSION,
823 XCB_XFIXES_MINOR_VERSION);
825 xcb_dri2_query_version_cookie_t dri2_query_cookie = xcb_dri2_query_version (m_connection,
826 XCB_DRI2_MAJOR_VERSION,
827 XCB_DRI2_MINOR_VERSION);
829 xcb_xfixes_query_version_reply_t *xfixes_query = xcb_xfixes_query_version_reply (m_connection,
830 xfixes_query_cookie, &error);
831 if (!xfixes_query || error || xfixes_query->major_version < 2) {
838 xcb_dri2_query_version_reply_t *dri2_query = xcb_dri2_query_version_reply (m_connection,
839 dri2_query_cookie, &error);
840 if (!dri2_query || error) {
846 QXcbConnection *that = const_cast<QXcbConnection *>(this);
847 that->m_dri2_major = dri2_query->major_version;
848 that->m_dri2_minor = dri2_query->minor_version;
850 that->m_has_support_for_dri2 = true;
851 that->m_dri2_support_probed = true;
853 return m_has_support_for_dri2;
855 #endif //XCB_USE_DRI2