| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/events/platform/x11/x11_event_source.h" | 5 #include "ui/events/platform/x11/x11_event_source.h" |
| 6 | 6 |
| 7 #include <glib.h> | |
| 8 #include <X11/extensions/XInput2.h> | 7 #include <X11/extensions/XInput2.h> |
| 9 #include <X11/X.h> | 8 #include <X11/X.h> |
| 10 #include <X11/Xlib.h> | 9 #include <X11/Xlib.h> |
| 11 #include <X11/XKBlib.h> | 10 #include <X11/XKBlib.h> |
| 12 | 11 |
| 13 #include "base/logging.h" | 12 #include "base/logging.h" |
| 14 #include "ui/events/event_utils.h" | 13 #include "ui/events/event_utils.h" |
| 15 #include "ui/events/platform/platform_event_dispatcher.h" | 14 #include "ui/events/platform/platform_event_dispatcher.h" |
| 16 #include "ui/gfx/x/x11_types.h" | 15 #include "ui/gfx/x/x11_types.h" |
| 17 | 16 |
| 18 namespace ui { | 17 namespace ui { |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 struct GLibX11Source : public GSource { | |
| 23 // Note: The GLibX11Source is created and destroyed by GLib. So its | |
| 24 // constructor/destructor may or may not get called. | |
| 25 XDisplay* display; | |
| 26 GPollFD* poll_fd; | |
| 27 }; | |
| 28 | |
| 29 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) { | |
| 30 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source); | |
| 31 if (XPending(gxsource->display)) | |
| 32 *timeout_ms = 0; | |
| 33 else | |
| 34 *timeout_ms = -1; | |
| 35 return FALSE; | |
| 36 } | |
| 37 | |
| 38 gboolean XSourceCheck(GSource* source) { | |
| 39 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source); | |
| 40 return XPending(gxsource->display); | |
| 41 } | |
| 42 | |
| 43 gboolean XSourceDispatch(GSource* source, | |
| 44 GSourceFunc unused_func, | |
| 45 gpointer data) { | |
| 46 X11EventSource* x11_source = static_cast<X11EventSource*>(data); | |
| 47 x11_source->DispatchXEvents(); | |
| 48 return TRUE; | |
| 49 } | |
| 50 | |
| 51 GSourceFuncs XSourceFuncs = { | |
| 52 XSourcePrepare, | |
| 53 XSourceCheck, | |
| 54 XSourceDispatch, | |
| 55 NULL | |
| 56 }; | |
| 57 | |
| 58 int g_xinput_opcode = -1; | 21 int g_xinput_opcode = -1; |
| 59 | 22 |
| 60 bool InitializeXInput2(XDisplay* display) { | 23 bool InitializeXInput2(XDisplay* display) { |
| 61 if (!display) | 24 if (!display) |
| 62 return false; | 25 return false; |
| 63 | 26 |
| 64 int event, err; | 27 int event, err; |
| 65 | 28 |
| 66 int xiopcode; | 29 int xiopcode; |
| 67 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { | 30 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 DVLOG(1) << "XKB not supported in the server."; | 73 DVLOG(1) << "XKB not supported in the server."; |
| 111 return false; | 74 return false; |
| 112 } | 75 } |
| 113 | 76 |
| 114 return true; | 77 return true; |
| 115 } | 78 } |
| 116 | 79 |
| 117 } // namespace | 80 } // namespace |
| 118 | 81 |
| 119 X11EventSource::X11EventSource(XDisplay* display) | 82 X11EventSource::X11EventSource(XDisplay* display) |
| 120 : display_(display), | 83 : display_(display) { |
| 121 x_source_(NULL) { | |
| 122 CHECK(display_); | 84 CHECK(display_); |
| 123 InitializeXInput2(display_); | 85 InitializeXInput2(display_); |
| 124 InitializeXkb(display_); | 86 InitializeXkb(display_); |
| 125 | |
| 126 InitXSource(); | |
| 127 } | 87 } |
| 128 | 88 |
| 129 X11EventSource::~X11EventSource() { | 89 X11EventSource::~X11EventSource() { |
| 130 g_source_destroy(x_source_); | |
| 131 g_source_unref(x_source_); | |
| 132 } | 90 } |
| 133 | 91 |
| 134 // static | 92 // static |
| 135 X11EventSource* X11EventSource::GetInstance() { | 93 X11EventSource* X11EventSource::GetInstance() { |
| 136 return static_cast<X11EventSource*>(PlatformEventSource::GetInstance()); | 94 return static_cast<X11EventSource*>(PlatformEventSource::GetInstance()); |
| 137 } | 95 } |
| 138 | 96 |
| 139 //////////////////////////////////////////////////////////////////////////////// | 97 //////////////////////////////////////////////////////////////////////////////// |
| 140 // X11EventSource, public | 98 // X11EventSource, public |
| 141 | 99 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 159 // Block until there's a message of |event_mask| type on |w|. Then remove | 117 // Block until there's a message of |event_mask| type on |w|. Then remove |
| 160 // it from the queue and stuff it in |event|. | 118 // it from the queue and stuff it in |event|. |
| 161 XWindowEvent(display_, window, StructureNotifyMask, &event); | 119 XWindowEvent(display_, window, StructureNotifyMask, &event); |
| 162 DispatchEvent(&event); | 120 DispatchEvent(&event); |
| 163 } while (event.type != MapNotify); | 121 } while (event.type != MapNotify); |
| 164 } | 122 } |
| 165 | 123 |
| 166 //////////////////////////////////////////////////////////////////////////////// | 124 //////////////////////////////////////////////////////////////////////////////// |
| 167 // X11EventSource, private | 125 // X11EventSource, private |
| 168 | 126 |
| 169 void X11EventSource::InitXSource() { | |
| 170 CHECK(!x_source_); | |
| 171 CHECK(display_) << "Unable to get connection to X server"; | |
| 172 | |
| 173 x_poll_.reset(new GPollFD()); | |
| 174 x_poll_->fd = ConnectionNumber(display_); | |
| 175 x_poll_->events = G_IO_IN; | |
| 176 x_poll_->revents = 0; | |
| 177 | |
| 178 GLibX11Source* glib_x_source = static_cast<GLibX11Source*> | |
| 179 (g_source_new(&XSourceFuncs, sizeof(GLibX11Source))); | |
| 180 glib_x_source->display = display_; | |
| 181 glib_x_source->poll_fd = x_poll_.get(); | |
| 182 | |
| 183 x_source_ = glib_x_source; | |
| 184 g_source_add_poll(x_source_, x_poll_.get()); | |
| 185 g_source_set_can_recurse(x_source_, TRUE); | |
| 186 g_source_set_callback(x_source_, NULL, this, NULL); | |
| 187 g_source_attach(x_source_, g_main_context_default()); | |
| 188 } | |
| 189 | |
| 190 uint32_t X11EventSource::DispatchEvent(XEvent* xevent) { | 127 uint32_t X11EventSource::DispatchEvent(XEvent* xevent) { |
| 191 bool have_cookie = false; | 128 bool have_cookie = false; |
| 192 if (xevent->type == GenericEvent && | 129 if (xevent->type == GenericEvent && |
| 193 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { | 130 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { |
| 194 have_cookie = true; | 131 have_cookie = true; |
| 195 } | 132 } |
| 196 | 133 |
| 197 uint32_t action = PlatformEventSource::DispatchEvent(xevent); | 134 uint32_t action = PlatformEventSource::DispatchEvent(xevent); |
| 198 if (xevent->type == GenericEvent && | 135 if (xevent->type == GenericEvent && |
| 199 xevent->xgeneric.evtype == XI_HierarchyChanged) { | 136 xevent->xgeneric.evtype == XI_HierarchyChanged) { |
| 200 ui::UpdateDeviceList(); | 137 ui::UpdateDeviceList(); |
| 201 } | 138 } |
| 202 | 139 |
| 203 if (have_cookie) | 140 if (have_cookie) |
| 204 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie); | 141 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie); |
| 205 return action; | 142 return action; |
| 206 } | 143 } |
| 207 | 144 |
| 208 scoped_ptr<PlatformEventSource> PlatformEventSource::CreateDefault() { | |
| 209 return scoped_ptr<PlatformEventSource>( | |
| 210 new X11EventSource(gfx::GetXDisplay())); | |
| 211 } | |
| 212 | |
| 213 } // namespace ui | 145 } // namespace ui |
| OLD | NEW |