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"
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.
21 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) {
22 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source);
23 if (XPending(gxsource->display))
30 gboolean XSourceCheck(GSource* source) {
31 GLibX11Source* gxsource = static_cast<GLibX11Source*>(source);
32 return XPending(gxsource->display);
35 gboolean XSourceDispatch(GSource* source,
36 GSourceFunc unused_func,
38 X11EventSource* x11_source = static_cast<X11EventSource*>(data);
39 x11_source->DispatchXEvents();
43 GSourceFuncs XSourceFuncs = {
50 class X11EventSourceGlib : public X11EventSource {
52 explicit X11EventSourceGlib(XDisplay* display)
53 : X11EventSource(display),
55 InitXSource(ConnectionNumber(display));
58 virtual ~X11EventSourceGlib() {
59 g_source_destroy(x_source_);
60 g_source_unref(x_source_);
64 void InitXSource(int fd) {
66 CHECK(display()) << "Unable to get connection to X server";
68 x_poll_.reset(new GPollFD());
70 x_poll_->events = G_IO_IN;
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();
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());
85 // The GLib event source for X events.
88 // The poll attached to |x_source_|.
89 scoped_ptr<GPollFD> x_poll_;
91 DISALLOW_COPY_AND_ASSIGN(X11EventSourceGlib);
96 scoped_ptr<PlatformEventSource> PlatformEventSource::CreateDefault() {
97 return scoped_ptr<PlatformEventSource>(
98 new X11EventSourceGlib(gfx::GetXDisplay()));