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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qxcbconnection.h"
43 #include "qxcbkeyboard.h"
44 #include "qxcbscreen.h"
45 #include "qxcbwindow.h"
47 #include <QtAlgorithms>
48 #include <QSocketNotifier>
49 #include <QtGui/private/qguiapplication_p.h>
50 #include <QAbstractEventDispatcher>
52 #include <QtCore/QDebug>
59 #include <X11/Xlib-xcb.h>
62 #ifdef XCB_USE_EGL //dont pull in eglext prototypes
68 #include <xcb/xfixes.h>
72 #define MESA_EGL_NO_X11_HEADERS
73 #define EGL_EGLEXT_PROTOTYPES
75 #include <EGL/eglext.h>
78 QXcbConnection::QXcbConnection(const char *displayName)
79 : m_displayName(displayName ? QByteArray(displayName) : qgetenv("DISPLAY"))
83 , m_dri2_support_probed(false)
84 , m_has_support_for_dri2(false)
87 int primaryScreen = 0;
90 Display *dpy = XOpenDisplay(m_displayName.constData());
91 primaryScreen = DefaultScreen(dpy);
92 m_connection = XGetXCBConnection(dpy);
93 XSetEventQueueOwner(dpy, XCBOwnsEventQueue);
96 EGLDisplay eglDisplay = eglGetDisplay(dpy);
97 m_egl_display = eglDisplay;
99 eglBindAPI(EGL_OPENGL_ES_API);
100 m_has_egl = eglInitialize(eglDisplay,&major,&minor);
103 m_connection = xcb_connect(m_displayName.constData(), &primaryScreen);
105 #endif //XCB_USE_XLIB
106 m_setup = xcb_get_setup(xcb_connection());
108 initializeAllAtoms();
110 xcb_screen_iterator_t it = xcb_setup_roots_iterator(m_setup);
112 int screenNumber = 0;
114 m_screens << new QXcbScreen(this, it.data, screenNumber++);
115 xcb_screen_next(&it);
118 m_keyboard = new QXcbKeyboard(this);
124 QSocketNotifier *notifier = new QSocketNotifier(xcb_get_file_descriptor(xcb_connection()), QSocketNotifier::Read, this);
125 connect(notifier, SIGNAL(activated(int)), this, SLOT(processXcbEvents()));
127 QAbstractEventDispatcher *dispatcher = QAbstractEventDispatcher::instance(qApp->thread());
128 connect(dispatcher, SIGNAL(aboutToBlock()), this, SLOT(processXcbEvents()));
133 QXcbConnection::~QXcbConnection()
135 qDeleteAll(m_screens);
138 XCloseDisplay((Display *)m_xlib_display);
140 xcb_disconnect(xcb_connection());
146 void QXcbConnection::addWindow(xcb_window_t id, QXcbWindow *window)
148 m_mapper.insert(id, window);
151 void QXcbConnection::removeWindow(xcb_window_t id)
156 QXcbWindow *QXcbConnection::platformWindowFromId(xcb_window_t id)
158 return m_mapper.value(id, 0);
161 #define HANDLE_PLATFORM_WINDOW_EVENT(event_t, windowMember, handler) \
163 event_t *e = (event_t *)event; \
164 if (QXcbWindow *platformWindow = platformWindowFromId(e->windowMember)) \
165 platformWindow->handler(e); \
169 #define HANDLE_KEYBOARD_EVENT(event_t, handler) \
171 event_t *e = (event_t *)event; \
172 if (QXcbWindow *platformWindow = platformWindowFromId(e->event)) \
173 m_keyboard->handler(platformWindow->window(), e); \
177 //#define XCB_EVENT_DEBUG
179 void printXcbEvent(const char *message, xcb_generic_event_t *event)
181 #ifdef XCB_EVENT_DEBUG
182 #define PRINT_XCB_EVENT(ev) \
184 printf("%s: %d - %s - sequence: %d\n", message, int(ev), #ev, event->sequence); \
187 switch (event->response_type & ~0x80) {
188 PRINT_XCB_EVENT(XCB_KEY_PRESS);
189 PRINT_XCB_EVENT(XCB_KEY_RELEASE);
190 PRINT_XCB_EVENT(XCB_BUTTON_PRESS);
191 PRINT_XCB_EVENT(XCB_BUTTON_RELEASE);
192 PRINT_XCB_EVENT(XCB_MOTION_NOTIFY);
193 PRINT_XCB_EVENT(XCB_ENTER_NOTIFY);
194 PRINT_XCB_EVENT(XCB_LEAVE_NOTIFY);
195 PRINT_XCB_EVENT(XCB_FOCUS_IN);
196 PRINT_XCB_EVENT(XCB_FOCUS_OUT);
197 PRINT_XCB_EVENT(XCB_KEYMAP_NOTIFY);
198 PRINT_XCB_EVENT(XCB_EXPOSE);
199 PRINT_XCB_EVENT(XCB_GRAPHICS_EXPOSURE);
200 PRINT_XCB_EVENT(XCB_VISIBILITY_NOTIFY);
201 PRINT_XCB_EVENT(XCB_CREATE_NOTIFY);
202 PRINT_XCB_EVENT(XCB_DESTROY_NOTIFY);
203 PRINT_XCB_EVENT(XCB_UNMAP_NOTIFY);
204 PRINT_XCB_EVENT(XCB_MAP_NOTIFY);
205 PRINT_XCB_EVENT(XCB_MAP_REQUEST);
206 PRINT_XCB_EVENT(XCB_REPARENT_NOTIFY);
207 PRINT_XCB_EVENT(XCB_CONFIGURE_NOTIFY);
208 PRINT_XCB_EVENT(XCB_CONFIGURE_REQUEST);
209 PRINT_XCB_EVENT(XCB_GRAVITY_NOTIFY);
210 PRINT_XCB_EVENT(XCB_RESIZE_REQUEST);
211 PRINT_XCB_EVENT(XCB_CIRCULATE_NOTIFY);
212 PRINT_XCB_EVENT(XCB_CIRCULATE_REQUEST);
213 PRINT_XCB_EVENT(XCB_PROPERTY_NOTIFY);
214 PRINT_XCB_EVENT(XCB_SELECTION_CLEAR);
215 PRINT_XCB_EVENT(XCB_SELECTION_REQUEST);
216 PRINT_XCB_EVENT(XCB_SELECTION_NOTIFY);
217 PRINT_XCB_EVENT(XCB_COLORMAP_NOTIFY);
218 PRINT_XCB_EVENT(XCB_CLIENT_MESSAGE);
219 PRINT_XCB_EVENT(XCB_MAPPING_NOTIFY);
221 printf("%s: unknown event - response_type: %d - sequence: %d\n", message, int(event->response_type & ~0x80), int(event->sequence));
229 const char *xcb_errors[] =
252 const char *xcb_protocol_request_codes[] =
256 "ChangeWindowAttributes",
257 "GetWindowAttributes",
284 "ChangeActivePointerGrab",
334 "CopyColormapAndFree",
337 "ListInstalledColormaps",
354 "ChangeKeyboardMapping",
355 "GetKeyboardMapping",
356 "ChangeKeyboardControl",
357 "GetKeyboardControl",
359 "ChangePointerControl",
372 "SetModifierMapping",
373 "GetModifierMapping",
378 void QXcbConnection::log(const char *file, int line, int sequence)
381 info.sequence = sequence;
388 void QXcbConnection::handleXcbError(xcb_generic_error_t *error)
390 uint clamped_error_code = qMin<uint>(error->error_code, (sizeof(xcb_errors) / sizeof(xcb_errors[0])) - 1);
391 uint clamped_major_code = qMin<uint>(error->major_code, (sizeof(xcb_protocol_request_codes) / sizeof(xcb_protocol_request_codes[0])) - 1);
393 printf("XCB error: %d (%s), sequence: %d, resource id: %d, major code: %d (%s), minor code: %d\n",
394 int(error->error_code), xcb_errors[clamped_error_code],
395 int(error->sequence), int(error->resource_id),
396 int(error->major_code), xcb_protocol_request_codes[clamped_major_code],
397 int(error->minor_code));
400 for (; i < m_callLog.size(); ++i) {
401 if (m_callLog.at(i).sequence == error->sequence) {
402 printf("Caused by: %s:%d\n", qPrintable(m_callLog.at(i).file), m_callLog.at(i).line);
404 } else if (m_callLog.at(i).sequence > error->sequence) {
405 printf("Caused some time before: %s:%d\n", qPrintable(m_callLog.at(i).file), m_callLog.at(i).line);
407 printf("and after: %s:%d\n", qPrintable(m_callLog.at(i-1).file), m_callLog.at(i-1).line);
411 if (i == m_callLog.size() && !m_callLog.isEmpty())
412 printf("Caused some time after: %s:%d\n", qPrintable(m_callLog.first().file), m_callLog.first().line);
416 void QXcbConnection::processXcbEvents()
418 while (xcb_generic_event_t *event = xcb_poll_for_event(xcb_connection())) {
421 uint response_type = event->response_type & ~0x80;
423 if (!response_type) {
424 handleXcbError((xcb_generic_error_t *)event);
431 for (; i < m_callLog.size(); ++i)
432 if (m_callLog.at(i).sequence >= event->sequence)
434 m_callLog.remove(0, i);
438 switch (response_type) {
440 HANDLE_PLATFORM_WINDOW_EVENT(xcb_expose_event_t, window, handleExposeEvent);
441 case XCB_BUTTON_PRESS:
442 HANDLE_PLATFORM_WINDOW_EVENT(xcb_button_press_event_t, event, handleButtonPressEvent);
443 case XCB_BUTTON_RELEASE:
444 HANDLE_PLATFORM_WINDOW_EVENT(xcb_button_release_event_t, event, handleButtonReleaseEvent);
445 case XCB_MOTION_NOTIFY:
446 HANDLE_PLATFORM_WINDOW_EVENT(xcb_motion_notify_event_t, event, handleMotionNotifyEvent);
447 case XCB_CONFIGURE_NOTIFY:
448 HANDLE_PLATFORM_WINDOW_EVENT(xcb_configure_notify_event_t, event, handleConfigureNotifyEvent);
449 case XCB_CLIENT_MESSAGE:
450 HANDLE_PLATFORM_WINDOW_EVENT(xcb_client_message_event_t, window, handleClientMessageEvent);
451 case XCB_ENTER_NOTIFY:
452 HANDLE_PLATFORM_WINDOW_EVENT(xcb_enter_notify_event_t, event, handleEnterNotifyEvent);
453 case XCB_LEAVE_NOTIFY:
454 HANDLE_PLATFORM_WINDOW_EVENT(xcb_leave_notify_event_t, event, handleLeaveNotifyEvent);
456 HANDLE_PLATFORM_WINDOW_EVENT(xcb_focus_in_event_t, event, handleFocusInEvent);
458 HANDLE_PLATFORM_WINDOW_EVENT(xcb_focus_out_event_t, event, handleFocusOutEvent);
460 HANDLE_KEYBOARD_EVENT(xcb_key_press_event_t, handleKeyPressEvent);
461 case XCB_KEY_RELEASE:
462 HANDLE_KEYBOARD_EVENT(xcb_key_release_event_t, handleKeyReleaseEvent);
463 case XCB_MAPPING_NOTIFY:
464 m_keyboard->handleMappingNotifyEvent((xcb_mapping_notify_event_t *)event);
471 printXcbEvent("Handled XCB event", event);
473 printXcbEvent("Unhandled XCB event", event);
476 xcb_flush(xcb_connection());
479 static const char * xcb_atomnames = {
480 // window-manager <-> client protocols
485 "_NET_WM_CONTEXT_HELP\0"
486 "_NET_WM_SYNC_REQUEST\0"
487 "_NET_WM_SYNC_REQUEST_COUNTER\0"
489 // ICCCM window state
493 // Session management
507 "_QT_CLIPBOARD_SENTINEL\0"
508 "_QT_SELECTION_SENTINEL\0"
509 "CLIPBOARD_MANAGER\0"
516 "_QT_INPUT_ENCODING\0"
521 "ENLIGHTENMENT_DESKTOP\0"
523 "_SGI_DESKS_MANAGER\0"
527 "_NET_VIRTUAL_ROOTS\0"
530 "_NET_MOVERESIZE_WINDOW\0"
531 "_NET_WM_MOVERESIZE\0"
534 "_NET_WM_ICON_NAME\0"
539 "_NET_WM_WINDOW_OPACITY\0"
542 "_NET_WM_STATE_ABOVE\0"
543 "_NET_WM_STATE_BELOW\0"
544 "_NET_WM_STATE_FULLSCREEN\0"
545 "_NET_WM_STATE_MAXIMIZED_HORZ\0"
546 "_NET_WM_STATE_MAXIMIZED_VERT\0"
547 "_NET_WM_STATE_MODAL\0"
548 "_NET_WM_STATE_STAYS_ON_TOP\0"
549 "_NET_WM_STATE_DEMANDS_ATTENTION\0"
551 "_NET_WM_USER_TIME\0"
552 "_NET_WM_USER_TIME_WINDOW\0"
553 "_NET_WM_FULL_PLACEMENT\0"
555 "_NET_WM_WINDOW_TYPE\0"
556 "_NET_WM_WINDOW_TYPE_DESKTOP\0"
557 "_NET_WM_WINDOW_TYPE_DOCK\0"
558 "_NET_WM_WINDOW_TYPE_TOOLBAR\0"
559 "_NET_WM_WINDOW_TYPE_MENU\0"
560 "_NET_WM_WINDOW_TYPE_UTILITY\0"
561 "_NET_WM_WINDOW_TYPE_SPLASH\0"
562 "_NET_WM_WINDOW_TYPE_DIALOG\0"
563 "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU\0"
564 "_NET_WM_WINDOW_TYPE_POPUP_MENU\0"
565 "_NET_WM_WINDOW_TYPE_TOOLTIP\0"
566 "_NET_WM_WINDOW_TYPE_NOTIFICATION\0"
567 "_NET_WM_WINDOW_TYPE_COMBO\0"
568 "_NET_WM_WINDOW_TYPE_DND\0"
569 "_NET_WM_WINDOW_TYPE_NORMAL\0"
570 "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE\0"
572 "_KDE_NET_WM_FRAME_STRUT\0"
574 "_NET_STARTUP_INFO\0"
575 "_NET_STARTUP_INFO_BEGIN\0"
577 "_NET_SUPPORTING_WM_CHECK\0"
581 "_NET_SYSTEM_TRAY_VISUAL\0"
583 "_NET_ACTIVE_WINDOW\0"
608 "XdndActionPrivate\0"
611 "_MOTIF_DRAG_AND_DROP_MESSAGE\0"
612 "_MOTIF_DRAG_INITIATOR_INFO\0"
613 "_MOTIF_DRAG_RECEIVER_INFO\0"
614 "_MOTIF_DRAG_WINDOW\0"
615 "_MOTIF_DRAG_TARGETS\0"
617 "XmTRANSFER_SUCCESS\0"
618 "XmTRANSFER_FAILURE\0"
627 // Wacom old. (before version 0.10)
637 xcb_atom_t QXcbConnection::atom(QXcbAtom::Atom atom)
639 return m_allAtoms[atom];
642 void QXcbConnection::initializeAllAtoms() {
643 const char *names[QXcbAtom::NAtoms];
644 const char *ptr = xcb_atomnames;
654 Q_ASSERT(i == QXcbAtom::NPredefinedAtoms);
656 QByteArray settings_atom_name("_QT_SETTINGS_TIMESTAMP_");
657 settings_atom_name += m_displayName;
658 names[i++] = settings_atom_name;
660 xcb_intern_atom_cookie_t cookies[QXcbAtom::NAtoms];
662 Q_ASSERT(i == QXcbAtom::NAtoms);
663 for (i = 0; i < QXcbAtom::NAtoms; ++i)
664 cookies[i] = xcb_intern_atom(xcb_connection(), false, strlen(names[i]), names[i]);
666 for (i = 0; i < QXcbAtom::NAtoms; ++i)
667 m_allAtoms[i] = xcb_intern_atom_reply(xcb_connection(), cookies[i], 0)->atom;
670 void QXcbConnection::sync()
673 xcb_get_input_focus_cookie_t cookie = Q_XCB_CALL(xcb_get_input_focus(xcb_connection()));
674 free(xcb_get_input_focus_reply(xcb_connection(), cookie, 0));
677 #if defined(XCB_USE_EGL)
678 bool QXcbConnection::hasEgl() const
682 #endif // defined(XCB_USE_EGL)
685 void QXcbConnection::initializeDri2()
687 xcb_dri2_connect_cookie_t connect_cookie = xcb_dri2_connect_unchecked (m_connection,
688 m_screens[0]->root(),
689 XCB_DRI2_DRIVER_TYPE_DRI);
691 xcb_dri2_connect_reply_t *connect = xcb_dri2_connect_reply (m_connection,
692 connect_cookie, NULL);
694 if (! connect || connect->driver_name_length + connect->device_name_length == 0) {
695 qDebug() << "Failed to connect to dri2";
699 m_dri2_device_name = QByteArray(xcb_dri2_connect_device_name (connect),
700 xcb_dri2_connect_device_name_length (connect));
703 int fd = open(m_dri2_device_name.constData(), O_RDWR);
705 qDebug() << "InitializeDri2: Could'nt open device << dri2DeviceName";
706 m_dri2_device_name = QByteArray();
711 if (drmGetMagic(fd, &magic)) {
712 qDebug() << "Failed to get drmMagic";
716 xcb_dri2_authenticate_cookie_t authenticate_cookie = xcb_dri2_authenticate_unchecked(m_connection,
717 m_screens[0]->root(), magic);
718 xcb_dri2_authenticate_reply_t *authenticate = xcb_dri2_authenticate_reply(m_connection,
719 authenticate_cookie, NULL);
720 if (authenticate == NULL || !authenticate->authenticated) {
721 fprintf(stderr, "DRI2: failed to authenticate\n");
728 EGLDisplay display = eglGetDRMDisplayMESA(fd);
730 fprintf(stderr, "failed to create display\n");
734 m_egl_display = display;
736 if (!eglInitialize(display, &major, &minor)) {
737 fprintf(stderr, "failed to initialize display\n");
742 bool QXcbConnection::hasSupportForDri2() const
744 if (!m_dri2_support_probed) {
745 xcb_generic_error_t *error = 0;
747 xcb_prefetch_extension_data (m_connection, &xcb_xfixes_id);
748 xcb_prefetch_extension_data (m_connection, &xcb_dri2_id);
750 xcb_xfixes_query_version_cookie_t xfixes_query_cookie = xcb_xfixes_query_version(m_connection,
751 XCB_XFIXES_MAJOR_VERSION,
752 XCB_XFIXES_MINOR_VERSION);
754 xcb_dri2_query_version_cookie_t dri2_query_cookie = xcb_dri2_query_version (m_connection,
755 XCB_DRI2_MAJOR_VERSION,
756 XCB_DRI2_MINOR_VERSION);
758 xcb_xfixes_query_version_reply_t *xfixes_query = xcb_xfixes_query_version_reply (m_connection,
759 xfixes_query_cookie, &error);
760 if (!xfixes_query || error || xfixes_query->major_version < 2) {
767 xcb_dri2_query_version_reply_t *dri2_query = xcb_dri2_query_version_reply (m_connection,
768 dri2_query_cookie, &error);
769 if (!dri2_query || error) {
775 QXcbConnection *that = const_cast<QXcbConnection *>(this);
776 that->m_dri2_major = dri2_query->major_version;
777 that->m_dri2_minor = dri2_query->minor_version;
779 that->m_has_support_for_dri2 = true;
780 that->m_dri2_support_probed = true;
782 return m_has_support_for_dri2;
784 #endif //XCB_USE_DRI2