10529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
20529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Use of this source code is governed by a BSD-style license that can be
30529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// found in the LICENSE file.
40529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
50529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "ui/events/platform/x11/x11_event_source.h"
60529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include <X11/Xlib.h>
80529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
90529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/message_loop/message_loop.h"
100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/message_loop/message_pump_libevent.h"
110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochnamespace ui {
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochnamespace {
150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass X11EventSourceLibevent : public X11EventSource,
170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                               public base::MessagePumpLibevent::Watcher {
180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch public:
190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  explicit X11EventSourceLibevent(XDisplay* display)
205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      : X11EventSource(display),
215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        initialized_(false) {
225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    AddEventWatcher();
230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual ~X11EventSourceLibevent() {
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch private:
295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  void AddEventWatcher() {
305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    if (initialized_)
315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      return;
325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    if (!base::MessageLoop::current())
335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      return;
345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    int fd = ConnectionNumber(display());
365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    base::MessageLoopForUI::current()->WatchFileDescriptor(fd, true,
375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        base::MessagePumpLibevent::WATCH_READ, &watcher_controller_, this);
385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    initialized_ = true;
395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
405c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
415c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // PlatformEventSource:
425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  virtual void OnDispatcherListChanged() OVERRIDE {
435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    AddEventWatcher();
445c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
455c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // base::MessagePumpLibevent::Watcher:
470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    DispatchXEvents();
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {
520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    NOTREACHED();
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  base::MessagePumpLibevent::FileDescriptorWatcher watcher_controller_;
565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  bool initialized_;
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  DISALLOW_COPY_AND_ASSIGN(X11EventSourceLibevent);
590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch};
600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}  // namespace
620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochscoped_ptr<PlatformEventSource> PlatformEventSource::CreateDefault() {
640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return scoped_ptr<PlatformEventSource>(
650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      new X11EventSourceLibevent(gfx::GetXDisplay()));
660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}  // namespace ui
69