Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / events / platform / x11 / x11_event_source_glib.cc
blobf616cddbee694d1a22e20800ef2dbf27168a3812
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"
7 #include <glib.h>
8 #include <X11/Xlib.h>
10 namespace ui {
12 namespace {
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;
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;
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,
37 gpointer data) {
38 X11EventSource* x11_source = static_cast<X11EventSource*>(data);
39 x11_source->DispatchXEvents();
40 return TRUE;
43 GSourceFuncs XSourceFuncs = {
44 XSourcePrepare,
45 XSourceCheck,
46 XSourceDispatch,
47 NULL
50 class X11EventSourceGlib : public X11EventSource {
51 public:
52 explicit X11EventSourceGlib(XDisplay* display)
53 : X11EventSource(display),
54 x_source_(NULL) {
55 InitXSource(ConnectionNumber(display));
58 ~X11EventSourceGlib() override {
59 g_source_destroy(x_source_);
60 g_source_unref(x_source_);
63 private:
64 void InitXSource(int fd) {
65 CHECK(!x_source_);
66 CHECK(display()) << "Unable to get connection to X server";
68 x_poll_.reset(new GPollFD());
69 x_poll_->fd = fd;
70 x_poll_->events = G_IO_IN;
71 x_poll_->revents = 0;
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.
86 GSource* x_source_;
88 // The poll attached to |x_source_|.
89 scoped_ptr<GPollFD> x_poll_;
91 DISALLOW_COPY_AND_ASSIGN(X11EventSourceGlib);
94 } // namespace
96 scoped_ptr<PlatformEventSource> PlatformEventSource::CreateDefault() {
97 return make_scoped_ptr(new X11EventSourceGlib(gfx::GetXDisplay()));
100 } // namespace ui