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"
8 #include <X11/extensions/XInput2.h>
11 #include <X11/XKBlib.h>
13 #include "base/logging.h"
14 #include "base/message_loop/message_loop.h"
15 #include "ui/events/platform/platform_event_dispatcher.h"
21 struct GLibX11Source : public GSource {
22 // Note: The GLibX11Source is created and destroyed by GLib. So its
23 // constructor/destructor may or may not get called.
28 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) {
29 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source);
30 if (XPending(gxsource->display))
37 gboolean XSourceCheck(GSource* source) {
38 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source);
39 return XPending(gxsource->display);
42 gboolean XSourceDispatch(GSource* source,
43 GSourceFunc unused_func,
45 X11EventSource* x11_source = static_cast<X11EventSource*>(data);
46 x11_source->DispatchXEvents();
50 GSourceFuncs XSourceFuncs = {
57 int g_xinput_opcode = -1;
59 bool InitializeXInput2(XDisplay* display) {
66 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) {
67 DVLOG(1) << "X Input extension not available.";
70 g_xinput_opcode = xiopcode;
72 #if defined(USE_XI2_MT)
73 // USE_XI2_MT also defines the required XI2 minor minimum version.
74 int major = 2, minor = USE_XI2_MT;
76 int major = 2, minor = 0;
78 if (XIQueryVersion(display, &major, &minor) == BadRequest) {
79 DVLOG(1) << "XInput2 not supported in the server.";
82 #if defined(USE_XI2_MT)
83 if (major < 2 || (major == 2 && minor < USE_XI2_MT)) {
84 DVLOG(1) << "XI version on server is " << major << "." << minor << ". "
85 << "But 2." << USE_XI2_MT << " is required.";
93 bool InitializeXkb(XDisplay* display) {
97 int opcode, event, error;
98 int major = XkbMajorVersion;
99 int minor = XkbMinorVersion;
100 if (!XkbQueryExtension(display, &opcode, &event, &error, &major, &minor)) {
101 DVLOG(1) << "Xkb extension not available.";
105 // Ask the server not to send KeyRelease event when the user holds down a key.
107 Bool supported_return;
108 if (!XkbSetDetectableAutoRepeat(display, True, &supported_return)) {
109 DVLOG(1) << "XKB not supported in the server.";
118 X11EventSource::X11EventSource(XDisplay* display)
122 InitializeXInput2(display_);
123 InitializeXkb(display_);
128 X11EventSource::~X11EventSource() {
129 g_source_destroy(x_source_);
130 g_source_unref(x_source_);
134 X11EventSource* X11EventSource::GetInstance() {
135 return static_cast<X11EventSource*>(PlatformEventSource::GetInstance());
138 ////////////////////////////////////////////////////////////////////////////////
139 // X11EventSource, public
141 void X11EventSource::DispatchXEvents() {
143 // Handle all pending events.
144 // It may be useful to eventually align this event dispatch with vsync, but
146 while (XPending(display_)) {
148 XNextEvent(display_, &xevent);
149 uint32_t action = DispatchEvent(&xevent);
150 if (action & POST_DISPATCH_QUIT_LOOP)
155 void X11EventSource::BlockUntilWindowMapped(XID window) {
158 // Block until there's a message of |event_mask| type on |w|. Then remove
159 // it from the queue and stuff it in |event|.
160 XWindowEvent(display_, window, StructureNotifyMask, &event);
161 DispatchEvent(&event);
162 } while (event.type != MapNotify);
165 ////////////////////////////////////////////////////////////////////////////////
166 // X11EventSource, private
168 void X11EventSource::InitXSource() {
170 CHECK(display_) << "Unable to get connection to X server";
172 x_poll_.reset(new GPollFD());
173 x_poll_->fd = ConnectionNumber(display_);
174 x_poll_->events = G_IO_IN;
175 x_poll_->revents = 0;
177 GLibX11Source* glib_x_source = static_cast<GLibX11Source*>
178 (g_source_new(&XSourceFuncs, sizeof(GLibX11Source)));
179 glib_x_source->display = display_;
180 glib_x_source->poll_fd = x_poll_.get();
182 x_source_ = glib_x_source;
183 g_source_add_poll(x_source_, x_poll_.get());
184 g_source_set_can_recurse(x_source_, TRUE);
185 g_source_set_callback(x_source_, NULL, this, NULL);
186 g_source_attach(x_source_, g_main_context_default());
189 uint32_t X11EventSource::DispatchEvent(XEvent* xevent) {
190 bool have_cookie = false;
191 if (xevent->type == GenericEvent &&
192 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) {
195 int action = PlatformEventSource::DispatchEvent(xevent);
197 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie);