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/Xlib.h> |
| 9 |
| 10 namespace ui { |
| 11 |
| 12 namespace { |
| 13 |
| 14 struct GLibX11Source : public GSource { |
| 15 // Note: The GLibX11Source is created and destroyed by GLib. So its |
| 16 // constructor/destructor may or may not get called. |
| 17 XDisplay* display; |
| 18 GPollFD* poll_fd; |
| 19 }; |
| 20 |
| 21 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) { |
| 22 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source); |
| 23 if (XPending(gxsource->display)) |
| 24 *timeout_ms = 0; |
| 25 else |
| 26 *timeout_ms = -1; |
| 27 return FALSE; |
| 28 } |
| 29 |
| 30 gboolean XSourceCheck(GSource* source) { |
| 31 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source); |
| 32 return XPending(gxsource->display); |
| 33 } |
| 34 |
| 35 gboolean XSourceDispatch(GSource* source, |
| 36 GSourceFunc unused_func, |
| 37 gpointer data) { |
| 38 X11EventSource* x11_source = static_cast<X11EventSource*>(data); |
| 39 x11_source->DispatchXEvents(); |
| 40 return TRUE; |
| 41 } |
| 42 |
| 43 GSourceFuncs XSourceFuncs = { |
| 44 XSourcePrepare, |
| 45 XSourceCheck, |
| 46 XSourceDispatch, |
| 47 NULL |
| 48 }; |
| 49 |
| 50 class X11EventSourceGlib : public X11EventSource { |
| 51 public: |
| 52 explicit X11EventSourceGlib(XDisplay* display) |
| 53 : X11EventSource(display), |
| 54 x_source_(NULL) { |
| 55 InitXSource(ConnectionNumber(display)); |
| 56 } |
| 57 |
| 58 virtual ~X11EventSourceGlib() { |
| 59 g_source_destroy(x_source_); |
| 60 g_source_unref(x_source_); |
| 61 } |
| 62 |
| 63 private: |
| 64 void InitXSource(int fd) { |
| 65 CHECK(!x_source_); |
| 66 CHECK(display()) << "Unable to get connection to X server"; |
| 67 |
| 68 x_poll_.reset(new GPollFD()); |
| 69 x_poll_->fd = fd; |
| 70 x_poll_->events = G_IO_IN; |
| 71 x_poll_->revents = 0; |
| 72 |
| 73 GLibX11Source* glib_x_source = static_cast<GLibX11Source*> |
| 74 (g_source_new(&XSourceFuncs, sizeof(GLibX11Source))); |
| 75 glib_x_source->display = display(); |
| 76 glib_x_source->poll_fd = x_poll_.get(); |
| 77 |
| 78 x_source_ = glib_x_source; |
| 79 g_source_add_poll(x_source_, x_poll_.get()); |
| 80 g_source_set_can_recurse(x_source_, TRUE); |
| 81 g_source_set_callback(x_source_, NULL, this, NULL); |
| 82 g_source_attach(x_source_, g_main_context_default()); |
| 83 } |
| 84 |
| 85 // The GLib event source for X events. |
| 86 GSource* x_source_; |
| 87 |
| 88 // The poll attached to |x_source_|. |
| 89 scoped_ptr<GPollFD> x_poll_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(X11EventSourceGlib); |
| 92 }; |
| 93 |
| 94 } // namespace |
| 95 |
| 96 scoped_ptr<PlatformEventSource> PlatformEventSource::CreateDefault() { |
| 97 return scoped_ptr<PlatformEventSource>( |
| 98 new X11EventSourceGlib(gfx::GetXDisplay())); |
| 99 } |
| 100 |
| 101 } // namespace ui |
OLD | NEW |