| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "media/base/user_input_monitor.h" | 5 #include "media/base/user_input_monitor.h" |
| 6 | 6 |
| 7 #include <sys/select.h> | 7 #include <sys/select.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 #define XK_MISCELLANY | 9 #define XK_MISCELLANY |
| 10 #include <X11/keysymdef.h> | 10 #include <X11/keysymdef.h> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
| 20 #include "base/message_loop/message_pump_libevent.h" | 20 #include "base/message_loop/message_pump_libevent.h" |
| 21 #include "base/posix/eintr_wrapper.h" | 21 #include "base/posix/eintr_wrapper.h" |
| 22 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
| 23 #include "base/threading/non_thread_safe.h" | 23 #include "base/threading/non_thread_safe.h" |
| 24 #include "media/base/keyboard_event_counter.h" |
| 24 #include "third_party/skia/include/core/SkPoint.h" | 25 #include "third_party/skia/include/core/SkPoint.h" |
| 25 #include "ui/base/keycodes/keyboard_code_conversion_x.h" | 26 #include "ui/base/keycodes/keyboard_code_conversion_x.h" |
| 26 | 27 |
| 27 // These includes need to be later than dictated by the style guide due to | 28 // These includes need to be later than dictated by the style guide due to |
| 28 // Xlib header pollution, specifically the min, max, and Status macros. | 29 // Xlib header pollution, specifically the min, max, and Status macros. |
| 29 #include <X11/XKBlib.h> | 30 #include <X11/XKBlib.h> |
| 30 #include <X11/Xlibint.h> | 31 #include <X11/Xlibint.h> |
| 31 #include <X11/extensions/record.h> | 32 #include <X11/extensions/record.h> |
| 32 | 33 |
| 33 namespace media { | 34 namespace media { |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 class UserInputMonitorLinux : public base::NonThreadSafe, | 38 class UserInputMonitorLinux : public base::NonThreadSafe, |
| 38 public UserInputMonitor { | 39 public UserInputMonitor { |
| 39 public: | 40 public: |
| 40 UserInputMonitorLinux( | 41 UserInputMonitorLinux( |
| 41 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); | 42 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); |
| 42 virtual ~UserInputMonitorLinux(); | 43 virtual ~UserInputMonitorLinux(); |
| 43 | 44 |
| 45 virtual size_t GetKeyPressCount() const OVERRIDE; |
| 46 |
| 44 private: | 47 private: |
| 45 enum EventType { | 48 enum EventType { |
| 46 MOUSE_EVENT, | 49 MOUSE_EVENT, |
| 47 KEYBOARD_EVENT | 50 KEYBOARD_EVENT |
| 48 }; | 51 }; |
| 49 | 52 |
| 50 // The actual implementation resides in UserInputMonitorLinux::Core class. | 53 // The actual implementation resides in UserInputMonitorLinux::Core class. |
| 51 // Must be called on the io_task_runner thread. | 54 // Must be called on the io_task_runner thread. |
| 52 class Core : public base::RefCountedThreadSafe<Core>, | 55 class Core : public base::RefCountedThreadSafe<Core>, |
| 53 public base::MessagePumpLibevent::Watcher { | 56 public base::MessagePumpLibevent::Watcher { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 virtual void StopMouseMonitoring() OVERRIDE; | 94 virtual void StopMouseMonitoring() OVERRIDE; |
| 92 virtual void StartKeyboardMonitoring() OVERRIDE; | 95 virtual void StartKeyboardMonitoring() OVERRIDE; |
| 93 virtual void StopKeyboardMonitoring() OVERRIDE; | 96 virtual void StopKeyboardMonitoring() OVERRIDE; |
| 94 | 97 |
| 95 void OnMouseEvent(const SkIPoint& position); | 98 void OnMouseEvent(const SkIPoint& position); |
| 96 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); | 99 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); |
| 97 | 100 |
| 98 // Task runner on which X Window events are received. | 101 // Task runner on which X Window events are received. |
| 99 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 100 scoped_refptr<Core> core_; | 103 scoped_refptr<Core> core_; |
| 104 KeyboardEventCounter counter_; |
| 101 | 105 |
| 102 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); | 106 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); |
| 103 }; | 107 }; |
| 104 | 108 |
| 105 UserInputMonitorLinux::UserInputMonitorLinux( | 109 UserInputMonitorLinux::UserInputMonitorLinux( |
| 106 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 110 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 107 : io_task_runner_(io_task_runner), | 111 : io_task_runner_(io_task_runner), |
| 108 core_(new Core(base::Bind(&UserInputMonitorLinux::OnMouseEvent, | 112 core_(new Core(base::Bind(&UserInputMonitorLinux::OnMouseEvent, |
| 109 base::Unretained(this)), | 113 base::Unretained(this)), |
| 110 base::Bind(&UserInputMonitorLinux::OnKeyboardEvent, | 114 base::Bind(&UserInputMonitorLinux::OnKeyboardEvent, |
| 111 base::Unretained(this)))) {} | 115 base::Unretained(this)))) {} |
| 112 | 116 |
| 113 UserInputMonitorLinux::~UserInputMonitorLinux() {} | 117 UserInputMonitorLinux::~UserInputMonitorLinux() {} |
| 114 | 118 |
| 119 size_t UserInputMonitorLinux::GetKeyPressCount() const { |
| 120 return counter_.GetKeyPressCount(); |
| 121 } |
| 122 |
| 115 void UserInputMonitorLinux::StartMouseMonitoring() { | 123 void UserInputMonitorLinux::StartMouseMonitoring() { |
| 116 io_task_runner_->PostTask( | 124 io_task_runner_->PostTask( |
| 117 FROM_HERE, base::Bind(&Core::StartMonitor, core_.get(), MOUSE_EVENT)); | 125 FROM_HERE, base::Bind(&Core::StartMonitor, core_.get(), MOUSE_EVENT)); |
| 118 } | 126 } |
| 119 | 127 |
| 120 void UserInputMonitorLinux::StopMouseMonitoring() { | 128 void UserInputMonitorLinux::StopMouseMonitoring() { |
| 121 io_task_runner_->PostTask( | 129 io_task_runner_->PostTask( |
| 122 FROM_HERE, base::Bind(&Core::StopMonitor, core_.get(), MOUSE_EVENT)); | 130 FROM_HERE, base::Bind(&Core::StopMonitor, core_.get(), MOUSE_EVENT)); |
| 123 } | 131 } |
| 124 | 132 |
| 125 void UserInputMonitorLinux::StartKeyboardMonitoring() { | 133 void UserInputMonitorLinux::StartKeyboardMonitoring() { |
| 134 counter_.Reset(); |
| 126 io_task_runner_->PostTask( | 135 io_task_runner_->PostTask( |
| 127 FROM_HERE, base::Bind(&Core::StartMonitor, core_.get(), KEYBOARD_EVENT)); | 136 FROM_HERE, base::Bind(&Core::StartMonitor, core_.get(), KEYBOARD_EVENT)); |
| 128 } | 137 } |
| 129 | 138 |
| 130 void UserInputMonitorLinux::StopKeyboardMonitoring() { | 139 void UserInputMonitorLinux::StopKeyboardMonitoring() { |
| 131 io_task_runner_->PostTask( | 140 io_task_runner_->PostTask( |
| 132 FROM_HERE, base::Bind(&Core::StopMonitor, core_.get(), KEYBOARD_EVENT)); | 141 FROM_HERE, base::Bind(&Core::StopMonitor, core_.get(), KEYBOARD_EVENT)); |
| 133 } | 142 } |
| 134 | 143 |
| 135 void UserInputMonitorLinux::OnMouseEvent(const SkIPoint& position) { | 144 void UserInputMonitorLinux::OnMouseEvent(const SkIPoint& position) { |
| 136 UserInputMonitor::OnMouseEvent(position); | 145 UserInputMonitor::OnMouseEvent(position); |
| 137 } | 146 } |
| 138 | 147 |
| 139 void UserInputMonitorLinux::OnKeyboardEvent(ui::EventType event, | 148 void UserInputMonitorLinux::OnKeyboardEvent(ui::EventType event, |
| 140 ui::KeyboardCode key_code) { | 149 ui::KeyboardCode key_code) { |
| 141 UserInputMonitor::OnKeyboardEvent(event, key_code); | 150 counter_.OnKeyboardEvent(event, key_code); |
| 142 } | 151 } |
| 143 | 152 |
| 144 UserInputMonitorLinux::Core::Core(const MouseCallback& mouse_callback, | 153 UserInputMonitorLinux::Core::Core(const MouseCallback& mouse_callback, |
| 145 const KeyboardCallback& keyboard_callback) | 154 const KeyboardCallback& keyboard_callback) |
| 146 : display_(NULL), | 155 : display_(NULL), |
| 147 x_record_display_(NULL), | 156 x_record_display_(NULL), |
| 148 x_record_context_(0), | 157 x_record_context_(0), |
| 149 mouse_callback_(mouse_callback), | 158 mouse_callback_(mouse_callback), |
| 150 keyboard_callback_(keyboard_callback) { | 159 keyboard_callback_(keyboard_callback) { |
| 151 x_record_range_[0] = NULL; | 160 x_record_range_[0] = NULL; |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 } // namespace | 338 } // namespace |
| 330 | 339 |
| 331 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( | 340 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( |
| 332 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 341 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 333 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { | 342 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { |
| 334 return scoped_ptr<UserInputMonitor>( | 343 return scoped_ptr<UserInputMonitor>( |
| 335 new UserInputMonitorLinux(io_task_runner)); | 344 new UserInputMonitorLinux(io_task_runner)); |
| 336 } | 345 } |
| 337 | 346 |
| 338 } // namespace media | 347 } // namespace media |
| OLD | NEW |