1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/platform/x11/x11_event_source.h"
7 #include <X11/extensions/XInput2.h>
10 #include <X11/XKBlib.h>
12 #include "base/logging.h"
13 #include "ui/events/event_utils.h"
14 #include "ui/events/platform/platform_event_dispatcher.h"
15 #include "ui/gfx/x/x11_types.h"
21 int g_xinput_opcode = -1;
23 bool InitializeXInput2(XDisplay* display) {
30 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) {
31 DVLOG(1) << "X Input extension not available.";
34 g_xinput_opcode = xiopcode;
36 #if defined(USE_XI2_MT)
37 // USE_XI2_MT also defines the required XI2 minor minimum version.
38 int major = 2, minor = USE_XI2_MT;
40 int major = 2, minor = 0;
42 if (XIQueryVersion(display, &major, &minor) == BadRequest) {
43 DVLOG(1) << "XInput2 not supported in the server.";
46 #if defined(USE_XI2_MT)
47 if (major < 2 || (major == 2 && minor < USE_XI2_MT)) {
48 DVLOG(1) << "XI version on server is " << major << "." << minor << ". "
49 << "But 2." << USE_XI2_MT << " is required.";
57 bool InitializeXkb(XDisplay* display) {
61 int opcode, event, error;
62 int major = XkbMajorVersion;
63 int minor = XkbMinorVersion;
64 if (!XkbQueryExtension(display, &opcode, &event, &error, &major, &minor)) {
65 DVLOG(1) << "Xkb extension not available.";
69 // Ask the server not to send KeyRelease event when the user holds down a key.
71 Bool supported_return;
72 if (!XkbSetDetectableAutoRepeat(display, True, &supported_return)) {
73 DVLOG(1) << "XKB not supported in the server.";
82 X11EventSource::X11EventSource(XDisplay* display)
85 InitializeXInput2(display_);
86 InitializeXkb(display_);
89 X11EventSource::~X11EventSource() {
93 X11EventSource* X11EventSource::GetInstance() {
94 return static_cast<X11EventSource*>(PlatformEventSource::GetInstance());
97 ////////////////////////////////////////////////////////////////////////////////
98 // X11EventSource, public
100 void X11EventSource::DispatchXEvents() {
102 // Handle all pending events.
103 // It may be useful to eventually align this event dispatch with vsync, but
105 while (XPending(display_)) {
107 XNextEvent(display_, &xevent);
108 uint32_t action = DispatchEvent(&xevent);
109 if (action & POST_DISPATCH_QUIT_LOOP)
114 void X11EventSource::BlockUntilWindowMapped(XID window) {
117 // Block until there's a message of |event_mask| type on |w|. Then remove
118 // it from the queue and stuff it in |event|.
119 XWindowEvent(display_, window, StructureNotifyMask, &event);
120 DispatchEvent(&event);
121 } while (event.type != MapNotify);
124 ////////////////////////////////////////////////////////////////////////////////
125 // X11EventSource, private
127 uint32_t X11EventSource::DispatchEvent(XEvent* xevent) {
128 bool have_cookie = false;
129 if (xevent->type == GenericEvent &&
130 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) {
134 uint32_t action = PlatformEventSource::DispatchEvent(xevent);
135 if (xevent->type == GenericEvent &&
136 xevent->xgeneric.evtype == XI_HierarchyChanged) {
137 ui::UpdateDeviceList();
141 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie);