Chromium Code Reviews| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <sys/select.h> | 8 #include <sys/select.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 #define XK_MISCELLANY | 10 #define XK_MISCELLANY |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 namespace media { | 35 namespace media { |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 // This is the actual implementation of event monitoring. It's separated from | 38 // This is the actual implementation of event monitoring. It's separated from |
| 39 // UserInputMonitorLinux since it needs to be deleted on the IO thread. | 39 // UserInputMonitorLinux since it needs to be deleted on the IO thread. |
| 40 class UserInputMonitorLinuxCore | 40 class UserInputMonitorLinuxCore |
| 41 : public base::SupportsWeakPtr<UserInputMonitorLinuxCore>, | 41 : public base::SupportsWeakPtr<UserInputMonitorLinuxCore>, |
| 42 public base::MessageLoop::DestructionObserver { | 42 public base::MessageLoop::DestructionObserver { |
| 43 public: | 43 public: |
| 44 enum EventType { | 44 enum EventType { |
| 45 MOUSE_EVENT, | 45 MOUSE_EVENT, |
|
Wez
2016/12/16 02:06:43
nit: You can get rid of this now :)
CJ
2016/12/22 22:42:58
Done.
| |
| 46 KEYBOARD_EVENT | 46 KEYBOARD_EVENT |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 explicit UserInputMonitorLinuxCore( | 49 explicit UserInputMonitorLinuxCore( |
| 50 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 50 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); |
| 51 const scoped_refptr<UserInputMonitor::MouseListenerList>& | |
| 52 mouse_listeners); | |
| 53 ~UserInputMonitorLinuxCore() override; | 51 ~UserInputMonitorLinuxCore() override; |
| 54 | 52 |
| 55 // DestructionObserver overrides. | 53 // DestructionObserver overrides. |
| 56 void WillDestroyCurrentMessageLoop() override; | 54 void WillDestroyCurrentMessageLoop() override; |
| 57 | 55 |
| 58 size_t GetKeyPressCount() const; | 56 size_t GetKeyPressCount() const; |
| 59 void StartMonitor(EventType type); | 57 void StartMonitor(EventType type); |
| 60 void StopMonitor(EventType type); | 58 void StopMonitor(EventType type); |
| 61 | 59 |
| 62 private: | 60 private: |
| 63 void OnXEvent(); | 61 void OnXEvent(); |
| 64 | 62 |
| 65 // Processes key and mouse events. | 63 // Processes key events. |
| 66 void ProcessXEvent(xEvent* event); | 64 void ProcessXEvent(xEvent* event); |
| 67 static void ProcessReply(XPointer self, XRecordInterceptData* data); | 65 static void ProcessReply(XPointer self, XRecordInterceptData* data); |
| 68 | 66 |
| 69 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 67 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 70 scoped_refptr<base::ObserverListThreadSafe< | |
| 71 UserInputMonitor::MouseEventListener>> mouse_listeners_; | |
| 72 | 68 |
| 73 // | 69 // |
| 74 // The following members should only be accessed on the IO thread. | 70 // The following members should only be accessed on the IO thread. |
| 75 // | 71 // |
| 76 std::unique_ptr<base::FileDescriptorWatcher::Controller> watch_controller_; | 72 std::unique_ptr<base::FileDescriptorWatcher::Controller> watch_controller_; |
| 77 Display* x_control_display_; | 73 Display* x_control_display_; |
| 78 Display* x_record_display_; | 74 Display* x_record_display_; |
| 79 XRecordRange* x_record_range_[2]; | 75 XRecordRange* x_record_range_[2]; |
|
Wez
2016/12/16 02:06:43
nit: Don't need an arrange of XRecordRange, just a
CJ
2016/12/22 22:42:58
Done.
| |
| 80 XRecordContext x_record_context_; | 76 XRecordContext x_record_context_; |
| 81 KeyboardEventCounter counter_; | 77 KeyboardEventCounter counter_; |
| 82 | 78 |
| 83 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinuxCore); | 79 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinuxCore); |
| 84 }; | 80 }; |
| 85 | 81 |
| 86 class UserInputMonitorLinux : public UserInputMonitor { | 82 class UserInputMonitorLinux : public UserInputMonitor { |
| 87 public: | 83 public: |
| 88 explicit UserInputMonitorLinux( | 84 explicit UserInputMonitorLinux( |
| 89 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); | 85 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); |
| 90 ~UserInputMonitorLinux() override; | 86 ~UserInputMonitorLinux() override; |
| 91 | 87 |
| 92 // Public UserInputMonitor overrides. | 88 // Public UserInputMonitor overrides. |
| 93 size_t GetKeyPressCount() const override; | 89 size_t GetKeyPressCount() const override; |
| 94 | 90 |
| 95 private: | 91 private: |
| 96 // Private UserInputMonitor overrides. | 92 // Private UserInputMonitor overrides. |
| 97 void StartKeyboardMonitoring() override; | 93 void StartKeyboardMonitoring() override; |
| 98 void StopKeyboardMonitoring() override; | 94 void StopKeyboardMonitoring() override; |
| 99 void StartMouseMonitoring() override; | |
| 100 void StopMouseMonitoring() override; | |
| 101 | 95 |
| 102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 96 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 103 UserInputMonitorLinuxCore* core_; | 97 UserInputMonitorLinuxCore* core_; |
| 104 | 98 |
| 105 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); | 99 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); |
| 106 }; | 100 }; |
| 107 | 101 |
| 108 UserInputMonitorLinuxCore::UserInputMonitorLinuxCore( | 102 UserInputMonitorLinuxCore::UserInputMonitorLinuxCore( |
| 109 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 103 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 110 const scoped_refptr<UserInputMonitor::MouseListenerList>& mouse_listeners) | |
| 111 : io_task_runner_(io_task_runner), | 104 : io_task_runner_(io_task_runner), |
| 112 mouse_listeners_(mouse_listeners), | |
| 113 x_control_display_(NULL), | 105 x_control_display_(NULL), |
| 114 x_record_display_(NULL), | 106 x_record_display_(NULL), |
| 115 x_record_context_(0) { | 107 x_record_context_(0) { |
| 116 x_record_range_[0] = NULL; | 108 x_record_range_[0] = NULL; |
| 117 x_record_range_[1] = NULL; | 109 x_record_range_[1] = NULL; |
| 118 } | 110 } |
| 119 | 111 |
| 120 UserInputMonitorLinuxCore::~UserInputMonitorLinuxCore() { | 112 UserInputMonitorLinuxCore::~UserInputMonitorLinuxCore() { |
| 121 DCHECK(!x_control_display_); | 113 DCHECK(!x_control_display_); |
| 122 DCHECK(!x_record_display_); | 114 DCHECK(!x_record_display_); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 | 160 |
| 169 if (!x_record_range_[type]) | 161 if (!x_record_range_[type]) |
| 170 x_record_range_[type] = XRecordAllocRange(); | 162 x_record_range_[type] = XRecordAllocRange(); |
| 171 | 163 |
| 172 if (!x_record_range_[type]) { | 164 if (!x_record_range_[type]) { |
| 173 LOG(ERROR) << "XRecordAllocRange failed."; | 165 LOG(ERROR) << "XRecordAllocRange failed."; |
| 174 StopMonitor(type); | 166 StopMonitor(type); |
| 175 return; | 167 return; |
| 176 } | 168 } |
| 177 | 169 |
| 178 if (type == MOUSE_EVENT) { | 170 if (type == MOUSE_EVENT) { |
|
Wez
2016/12/16 02:06:43
This needs stripping out, as well - we now only wa
CJ
2016/12/22 22:42:57
Done.
| |
| 179 x_record_range_[type]->device_events.first = MotionNotify; | 171 x_record_range_[type]->device_events.first = MotionNotify; |
| 180 x_record_range_[type]->device_events.last = MotionNotify; | 172 x_record_range_[type]->device_events.last = MotionNotify; |
| 181 } else { | 173 } else { |
| 182 DCHECK_EQ(KEYBOARD_EVENT, type); | 174 DCHECK_EQ(KEYBOARD_EVENT, type); |
| 183 x_record_range_[type]->device_events.first = KeyPress; | 175 x_record_range_[type]->device_events.first = KeyPress; |
| 184 x_record_range_[type]->device_events.last = KeyRelease; | 176 x_record_range_[type]->device_events.last = KeyRelease; |
| 185 } | 177 } |
| 186 | 178 |
| 187 if (x_record_context_) { | 179 if (x_record_context_) { |
| 188 XRecordDisableContext(x_control_display_, x_record_context_); | 180 XRecordDisableContext(x_control_display_, x_record_context_); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 262 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 271 XEvent event; | 263 XEvent event; |
| 272 // Fetch pending events if any. | 264 // Fetch pending events if any. |
| 273 while (XPending(x_record_display_)) { | 265 while (XPending(x_record_display_)) { |
| 274 XNextEvent(x_record_display_, &event); | 266 XNextEvent(x_record_display_, &event); |
| 275 } | 267 } |
| 276 } | 268 } |
| 277 | 269 |
| 278 void UserInputMonitorLinuxCore::ProcessXEvent(xEvent* event) { | 270 void UserInputMonitorLinuxCore::ProcessXEvent(xEvent* event) { |
| 279 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 271 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 280 if (event->u.u.type == MotionNotify) { | 272 if (event->u.u.type != MotionNotify) { |
|
Wez
2016/12/16 02:06:43
nit: Looks like you don't need this at all - just
CJ
2016/12/22 22:42:58
Done.
| |
| 281 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX, | |
| 282 event->u.keyButtonPointer.rootY)); | |
| 283 mouse_listeners_->Notify( | |
| 284 FROM_HERE, &UserInputMonitor::MouseEventListener::OnMouseMoved, | |
| 285 position); | |
| 286 } else { | |
| 287 ui::EventType type; | 273 ui::EventType type; |
| 288 if (event->u.u.type == KeyPress) { | 274 if (event->u.u.type == KeyPress) { |
| 289 type = ui::ET_KEY_PRESSED; | 275 type = ui::ET_KEY_PRESSED; |
| 290 } else if (event->u.u.type == KeyRelease) { | 276 } else if (event->u.u.type == KeyRelease) { |
| 291 type = ui::ET_KEY_RELEASED; | 277 type = ui::ET_KEY_RELEASED; |
| 292 } else { | 278 } else { |
| 293 NOTREACHED(); | 279 NOTREACHED(); |
| 294 return; | 280 return; |
| 295 } | 281 } |
| 296 | 282 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 311 XRecordFreeData(data); | 297 XRecordFreeData(data); |
| 312 } | 298 } |
| 313 | 299 |
| 314 // | 300 // |
| 315 // Implementation of UserInputMonitorLinux. | 301 // Implementation of UserInputMonitorLinux. |
| 316 // | 302 // |
| 317 | 303 |
| 318 UserInputMonitorLinux::UserInputMonitorLinux( | 304 UserInputMonitorLinux::UserInputMonitorLinux( |
| 319 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 305 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 320 : io_task_runner_(io_task_runner), | 306 : io_task_runner_(io_task_runner), |
| 321 core_(new UserInputMonitorLinuxCore(io_task_runner, mouse_listeners())) {} | 307 core_(new UserInputMonitorLinuxCore(io_task_runner)) {} |
| 322 | 308 |
| 323 UserInputMonitorLinux::~UserInputMonitorLinux() { | 309 UserInputMonitorLinux::~UserInputMonitorLinux() { |
| 324 if (!io_task_runner_->DeleteSoon(FROM_HERE, core_)) | 310 if (!io_task_runner_->DeleteSoon(FROM_HERE, core_)) |
| 325 delete core_; | 311 delete core_; |
| 326 } | 312 } |
| 327 | 313 |
| 328 size_t UserInputMonitorLinux::GetKeyPressCount() const { | 314 size_t UserInputMonitorLinux::GetKeyPressCount() const { |
| 329 return core_->GetKeyPressCount(); | 315 return core_->GetKeyPressCount(); |
| 330 } | 316 } |
| 331 | 317 |
| 332 void UserInputMonitorLinux::StartKeyboardMonitoring() { | 318 void UserInputMonitorLinux::StartKeyboardMonitoring() { |
| 333 io_task_runner_->PostTask( | 319 io_task_runner_->PostTask( |
| 334 FROM_HERE, | 320 FROM_HERE, |
| 335 base::Bind(&UserInputMonitorLinuxCore::StartMonitor, | 321 base::Bind(&UserInputMonitorLinuxCore::StartMonitor, |
| 336 core_->AsWeakPtr(), | 322 core_->AsWeakPtr(), |
| 337 UserInputMonitorLinuxCore::KEYBOARD_EVENT)); | 323 UserInputMonitorLinuxCore::KEYBOARD_EVENT)); |
| 338 } | 324 } |
| 339 | 325 |
| 340 void UserInputMonitorLinux::StopKeyboardMonitoring() { | 326 void UserInputMonitorLinux::StopKeyboardMonitoring() { |
| 341 io_task_runner_->PostTask( | 327 io_task_runner_->PostTask( |
| 342 FROM_HERE, | 328 FROM_HERE, |
| 343 base::Bind(&UserInputMonitorLinuxCore::StopMonitor, | 329 base::Bind(&UserInputMonitorLinuxCore::StopMonitor, |
| 344 core_->AsWeakPtr(), | 330 core_->AsWeakPtr(), |
| 345 UserInputMonitorLinuxCore::KEYBOARD_EVENT)); | 331 UserInputMonitorLinuxCore::KEYBOARD_EVENT)); |
| 346 } | 332 } |
| 347 | 333 |
| 348 void UserInputMonitorLinux::StartMouseMonitoring() { | |
| 349 io_task_runner_->PostTask(FROM_HERE, | |
| 350 base::Bind(&UserInputMonitorLinuxCore::StartMonitor, | |
| 351 core_->AsWeakPtr(), | |
| 352 UserInputMonitorLinuxCore::MOUSE_EVENT)); | |
| 353 } | |
| 354 | |
| 355 void UserInputMonitorLinux::StopMouseMonitoring() { | |
| 356 io_task_runner_->PostTask(FROM_HERE, | |
| 357 base::Bind(&UserInputMonitorLinuxCore::StopMonitor, | |
| 358 core_->AsWeakPtr(), | |
| 359 UserInputMonitorLinuxCore::MOUSE_EVENT)); | |
| 360 } | |
| 361 | |
| 362 } // namespace | 334 } // namespace |
| 363 | 335 |
| 364 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create( | 336 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create( |
| 365 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 337 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 366 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { | 338 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { |
| 367 return base::WrapUnique(new UserInputMonitorLinux(io_task_runner)); | 339 return base::WrapUnique(new UserInputMonitorLinux(io_task_runner)); |
| 368 } | 340 } |
| 369 | 341 |
| 370 } // namespace media | 342 } // namespace media |
| OLD | NEW |