summaryrefslogtreecommitdiffstats
path: root/ui/events/platform/x11/x11_event_source_libevent.cc
blob: d92e12a5d82cd911577c0bf1ff3142b643fab216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/events/platform/x11/x11_event_source.h"

#include <X11/Xlib.h>

#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_pump_libevent.h"

namespace ui {

namespace {

class X11EventSourceLibevent : public X11EventSource,
                               public base::MessagePumpLibevent::Watcher {
 public:
  explicit X11EventSourceLibevent(XDisplay* display)
      : X11EventSource(display),
        initialized_(false) {
    AddEventWatcher();
  }

  virtual ~X11EventSourceLibevent() {
  }

 private:
  void AddEventWatcher() {
    if (initialized_)
      return;
    if (!base::MessageLoop::current())
      return;

    int fd = ConnectionNumber(display());
    base::MessageLoopForUI::current()->WatchFileDescriptor(fd, true,
        base::MessagePumpLibevent::WATCH_READ, &watcher_controller_, this);
    initialized_ = true;
  }

  // PlatformEventSource:
  virtual void OnDispatcherListChanged() OVERRIDE {
    AddEventWatcher();
  }

  // base::MessagePumpLibevent::Watcher:
  virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
    DispatchXEvents();
  }

  virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {
    NOTREACHED();
  }

  base::MessagePumpLibevent::FileDescriptorWatcher watcher_controller_;
  bool initialized_;

  DISALLOW_COPY_AND_ASSIGN(X11EventSourceLibevent);
};

}  // namespace

scoped_ptr<PlatformEventSource> PlatformEventSource::CreateDefault() {
  return scoped_ptr<PlatformEventSource>(
      new X11EventSourceLibevent(gfx::GetXDisplay()));
}

}  // namespace ui