| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/events/platform/x11/x11_event_source.h" | 5 #include "ui/events/platform/x11/x11_event_source.h" |
| 6 | 6 |
| 7 #include <X11/Xlib.h> | 7 #include <X11/Xlib.h> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/message_loop/message_pump_libevent.h" | 10 #include "base/message_loop/message_pump_libevent.h" |
| 11 | 11 |
| 12 namespace ui { | 12 namespace ui { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class X11EventSourceLibevent : public X11EventSource, | 16 class X11EventSourceLibevent : public X11EventSource, |
| 17 public base::MessagePumpLibevent::EventSource, |
| 17 public base::MessagePumpLibevent::Watcher { | 18 public base::MessagePumpLibevent::Watcher { |
| 18 public: | 19 public: |
| 19 explicit X11EventSourceLibevent(XDisplay* display) | 20 explicit X11EventSourceLibevent(XDisplay* display) |
| 20 : X11EventSource(display), | 21 : X11EventSource(display), |
| 21 initialized_(false) { | 22 initialized_(false) { |
| 22 AddEventWatcher(); | 23 AddEventWatcher(); |
| 23 } | 24 } |
| 24 | 25 |
| 25 ~X11EventSourceLibevent() override { | 26 ~X11EventSourceLibevent() override { |
| 27 if (initialized_) |
| 28 base::MessageLoopForUI::current()->ClearEventSource(); |
| 26 } | 29 } |
| 27 | 30 |
| 28 private: | 31 private: |
| 29 void AddEventWatcher() { | 32 void AddEventWatcher() { |
| 30 if (initialized_) | 33 if (initialized_) |
| 31 return; | 34 return; |
| 32 if (!base::MessageLoop::current()) | 35 if (!base::MessageLoop::current()) |
| 33 return; | 36 return; |
| 34 | 37 |
| 35 int fd = ConnectionNumber(display()); | 38 int fd = ConnectionNumber(display()); |
| 36 base::MessageLoopForUI::current()->WatchFileDescriptor(fd, true, | 39 base::MessageLoopForUI::current()->WatchFileDescriptor( |
| 37 base::MessagePumpLibevent::WATCH_READ, &watcher_controller_, this); | 40 fd, true, base::MessagePumpLibevent::WATCH_READ, &watcher_controller_, |
| 41 this); |
| 42 base::MessageLoopForUI::current()->SetEventSource(this); |
| 38 initialized_ = true; | 43 initialized_ = true; |
| 39 } | 44 } |
| 40 | 45 |
| 41 // PlatformEventSource: | 46 // PlatformEventSource: |
| 42 void OnDispatcherListChanged() override { | 47 void OnDispatcherListChanged() override { AddEventWatcher(); } |
| 43 AddEventWatcher(); | 48 |
| 44 } | 49 // base::MessagePumpLibEvent::EventSource: |
| 50 bool Poll() override { return DispatchXEvents(); } |
| 45 | 51 |
| 46 // base::MessagePumpLibevent::Watcher: | 52 // base::MessagePumpLibevent::Watcher: |
| 47 void OnFileCanReadWithoutBlocking(int fd) override { | 53 void OnFileCanReadWithoutBlocking(int fd) override { |
| 48 DispatchXEvents(); | 54 DispatchXEvents(); |
| 49 } | 55 } |
| 50 | 56 |
| 51 void OnFileCanWriteWithoutBlocking(int fd) override { | 57 void OnFileCanWriteWithoutBlocking(int fd) override { |
| 52 NOTREACHED(); | 58 NOTREACHED(); |
| 53 } | 59 } |
| 54 | 60 |
| 55 base::MessagePumpLibevent::FileDescriptorWatcher watcher_controller_; | 61 base::MessagePumpLibevent::FileDescriptorWatcher watcher_controller_; |
| 56 bool initialized_; | 62 bool initialized_; |
| 57 | 63 |
| 58 DISALLOW_COPY_AND_ASSIGN(X11EventSourceLibevent); | 64 DISALLOW_COPY_AND_ASSIGN(X11EventSourceLibevent); |
| 59 }; | 65 }; |
| 60 | 66 |
| 61 } // namespace | 67 } // namespace |
| 62 | 68 |
| 63 scoped_ptr<PlatformEventSource> PlatformEventSource::CreateDefault() { | 69 scoped_ptr<PlatformEventSource> PlatformEventSource::CreateDefault() { |
| 64 return make_scoped_ptr(new X11EventSourceLibevent(gfx::GetXDisplay())); | 70 return make_scoped_ptr(new X11EventSourceLibevent(gfx::GetXDisplay())); |
| 65 } | 71 } |
| 66 | 72 |
| 67 } // namespace ui | 73 } // namespace ui |
| OLD | NEW |