OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/events/platform/x11/x11_event_source.h" |
| 6 |
| 7 #include <glib.h> |
| 8 #include <X11/extensions/XInput2.h> |
| 9 #include <X11/X.h> |
| 10 #include <X11/Xlib.h> |
| 11 #include <X11/XKBlib.h> |
| 12 |
| 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "ui/events/platform/platform_event_dispatcher.h" |
| 16 |
| 17 namespace ui { |
| 18 |
| 19 namespace { |
| 20 |
| 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. |
| 24 XDisplay* display; |
| 25 GPollFD* poll_fd; |
| 26 }; |
| 27 |
| 28 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) { |
| 29 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source); |
| 30 if (XPending(gxsource->display)) |
| 31 *timeout_ms = 0; |
| 32 else |
| 33 *timeout_ms = -1; |
| 34 return FALSE; |
| 35 } |
| 36 |
| 37 gboolean XSourceCheck(GSource* source) { |
| 38 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source); |
| 39 return XPending(gxsource->display); |
| 40 } |
| 41 |
| 42 gboolean XSourceDispatch(GSource* source, |
| 43 GSourceFunc unused_func, |
| 44 gpointer data) { |
| 45 X11EventSource* x11_source = static_cast<X11EventSource*>(data); |
| 46 x11_source->DispatchXEvents(); |
| 47 return TRUE; |
| 48 } |
| 49 |
| 50 GSourceFuncs XSourceFuncs = { |
| 51 XSourcePrepare, |
| 52 XSourceCheck, |
| 53 XSourceDispatch, |
| 54 NULL |
| 55 }; |
| 56 |
| 57 int g_xinput_opcode = -1; |
| 58 |
| 59 bool InitializeXInput2(XDisplay* display) { |
| 60 if (!display) |
| 61 return false; |
| 62 |
| 63 int event, err; |
| 64 |
| 65 int xiopcode; |
| 66 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { |
| 67 DVLOG(1) << "X Input extension not available."; |
| 68 return false; |
| 69 } |
| 70 g_xinput_opcode = xiopcode; |
| 71 |
| 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; |
| 75 #else |
| 76 int major = 2, minor = 0; |
| 77 #endif |
| 78 if (XIQueryVersion(display, &major, &minor) == BadRequest) { |
| 79 DVLOG(1) << "XInput2 not supported in the server."; |
| 80 return false; |
| 81 } |
| 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."; |
| 86 return false; |
| 87 } |
| 88 #endif |
| 89 |
| 90 return true; |
| 91 } |
| 92 |
| 93 bool InitializeXkb(XDisplay* display) { |
| 94 if (!display) |
| 95 return false; |
| 96 |
| 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."; |
| 102 return false; |
| 103 } |
| 104 |
| 105 // Ask the server not to send KeyRelease event when the user holds down a key. |
| 106 // crbug.com/138092 |
| 107 Bool supported_return; |
| 108 if (!XkbSetDetectableAutoRepeat(display, True, &supported_return)) { |
| 109 DVLOG(1) << "XKB not supported in the server."; |
| 110 return false; |
| 111 } |
| 112 |
| 113 return true; |
| 114 } |
| 115 |
| 116 } // namespace |
| 117 |
| 118 X11EventSource::X11EventSource(XDisplay* display) |
| 119 : display_(display), |
| 120 x_source_(NULL) { |
| 121 CHECK(display_); |
| 122 InitializeXInput2(display_); |
| 123 InitializeXkb(display_); |
| 124 |
| 125 InitXSource(); |
| 126 } |
| 127 |
| 128 X11EventSource::~X11EventSource() { |
| 129 g_source_destroy(x_source_); |
| 130 g_source_unref(x_source_); |
| 131 } |
| 132 |
| 133 // static |
| 134 X11EventSource* X11EventSource::GetInstance() { |
| 135 return static_cast<X11EventSource*>(PlatformEventSource::GetInstance()); |
| 136 } |
| 137 |
| 138 //////////////////////////////////////////////////////////////////////////////// |
| 139 // X11EventSource, public |
| 140 |
| 141 void X11EventSource::DispatchXEvents() { |
| 142 DCHECK(display_); |
| 143 // Handle all pending events. |
| 144 // It may be useful to eventually align this event dispatch with vsync, but |
| 145 // not yet. |
| 146 while (XPending(display_)) { |
| 147 XEvent xevent; |
| 148 XNextEvent(display_, &xevent); |
| 149 uint32_t action = DispatchEvent(&xevent); |
| 150 if (action & POST_DISPATCH_QUIT_LOOP) |
| 151 break; |
| 152 } |
| 153 } |
| 154 |
| 155 void X11EventSource::BlockUntilWindowMapped(XID window) { |
| 156 XEvent event; |
| 157 do { |
| 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); |
| 163 } |
| 164 |
| 165 //////////////////////////////////////////////////////////////////////////////// |
| 166 // X11EventSource, private |
| 167 |
| 168 void X11EventSource::InitXSource() { |
| 169 CHECK(!x_source_); |
| 170 CHECK(display_) << "Unable to get connection to X server"; |
| 171 |
| 172 x_poll_.reset(new GPollFD()); |
| 173 x_poll_->fd = ConnectionNumber(display_); |
| 174 x_poll_->events = G_IO_IN; |
| 175 x_poll_->revents = 0; |
| 176 |
| 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(); |
| 181 |
| 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()); |
| 187 } |
| 188 |
| 189 uint32_t X11EventSource::DispatchEvent(XEvent* xevent) { |
| 190 bool have_cookie = false; |
| 191 if (xevent->type == GenericEvent && |
| 192 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { |
| 193 have_cookie = true; |
| 194 } |
| 195 int action = PlatformEventSource::DispatchEvent(xevent); |
| 196 if (have_cookie) |
| 197 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie); |
| 198 return action; |
| 199 } |
| 200 |
| 201 } // namespace ui |
OLD | NEW |