OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/user_input_monitor.h" |
| 6 |
| 7 #include <ApplicationServices/ApplicationServices.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/time/time.h" |
| 15 #include "base/timer/timer.h" |
| 16 #include "third_party/skia/include/core/SkPoint.h" |
| 17 |
| 18 namespace media { |
| 19 namespace { |
| 20 |
| 21 class UserInputMonitorMac : public UserInputMonitor { |
| 22 public: |
| 23 explicit UserInputMonitorMac( |
| 24 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); |
| 25 virtual ~UserInputMonitorMac(); |
| 26 |
| 27 virtual size_t GetKeyPressCount() const OVERRIDE; |
| 28 |
| 29 private: |
| 30 static CGEventRef MouseMoved(CGEventTapProxy proxy, |
| 31 CGEventType type, |
| 32 CGEventRef event, |
| 33 void* context); |
| 34 |
| 35 virtual void StartMouseMonitoring() OVERRIDE; |
| 36 virtual void StopMouseMonitoring() OVERRIDE; |
| 37 virtual void StartKeyboardMonitoring() OVERRIDE; |
| 38 virtual void StopKeyboardMonitoring() OVERRIDE; |
| 39 |
| 40 // Task runner on which input events are received. |
| 41 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 42 |
| 43 // These members should only be accessed on the UI thread. |
| 44 base::ScopedCFTypeRef<CFRunLoopSourceRef> input_run_loop_source_; |
| 45 base::ScopedCFTypeRef<CFMachPortRef> input_mach_port_; |
| 46 |
| 47 size_t initial_key_press_count_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorMac); |
| 50 }; |
| 51 |
| 52 UserInputMonitorMac::UserInputMonitorMac( |
| 53 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) |
| 54 : ui_task_runner_(ui_task_runner), |
| 55 initial_key_press_count_(0) {} |
| 56 |
| 57 UserInputMonitorMac::~UserInputMonitorMac() { |
| 58 DCHECK(!input_mach_port_); |
| 59 DCHECK(!input_run_loop_source_); |
| 60 DCHECK(!initial_key_press_count_); |
| 61 } |
| 62 |
| 63 size_t UserInputMonitorMac::GetKeyPressCount() const { |
| 64 size_t total_count = CGEventSourceCounterForEventType( |
| 65 kCGEventSourceStateHIDSystemState, kCGEventKeyDown); |
| 66 return total_count - initial_key_press_count_; |
| 67 } |
| 68 |
| 69 CGEventRef UserInputMonitorMac::MouseMoved(CGEventTapProxy proxy, |
| 70 CGEventType type, |
| 71 CGEventRef event, |
| 72 void* context) { |
| 73 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); |
| 74 |
| 75 int64_t pid = CGEventGetIntegerValueField(event, kCGEventSourceUnixProcessID); |
| 76 // |pid| 0 means the event is generated by hardware. |
| 77 if (pid == 0) { |
| 78 DCHECK_EQ(kCGEventMouseMoved, type); |
| 79 CGPoint cg_mouse_pos = CGEventGetLocation(event); |
| 80 SkIPoint mouse_pos = SkIPoint::Make(cg_mouse_pos.x, cg_mouse_pos.y); |
| 81 static_cast<UserInputMonitorMac*>(context)->OnMouseEvent(mouse_pos); |
| 82 } |
| 83 return NULL; |
| 84 } |
| 85 |
| 86 void UserInputMonitorMac::StartMouseMonitoring() { |
| 87 if (!ui_task_runner_->BelongsToCurrentThread()) { |
| 88 ui_task_runner_->PostTask( |
| 89 FROM_HERE, |
| 90 base::Bind(&UserInputMonitorMac::StartMouseMonitoring, |
| 91 base::Unretained(this))); |
| 92 return; |
| 93 } |
| 94 input_mach_port_.reset(CGEventTapCreate(kCGSessionEventTap, |
| 95 kCGHeadInsertEventTap, |
| 96 kCGEventTapOptionListenOnly, |
| 97 CGEventMaskBit(kCGEventMouseMoved), |
| 98 MouseMoved, |
| 99 this)); |
| 100 if (input_mach_port_) { |
| 101 input_run_loop_source_.reset( |
| 102 CFMachPortCreateRunLoopSource(NULL, input_mach_port_, 0)); |
| 103 CFRunLoopAddSource( |
| 104 CFRunLoopGetMain(), input_run_loop_source_, kCFRunLoopCommonModes); |
| 105 } else { |
| 106 LOG(ERROR) << "CGEventTapCreate failed."; |
| 107 } |
| 108 } |
| 109 |
| 110 void UserInputMonitorMac::StopMouseMonitoring() { |
| 111 if (!ui_task_runner_->BelongsToCurrentThread()) { |
| 112 ui_task_runner_->PostTask( |
| 113 FROM_HERE, |
| 114 base::Bind(&UserInputMonitorMac::StopMouseMonitoring, |
| 115 base::Unretained(this))); |
| 116 return; |
| 117 } |
| 118 if (input_run_loop_source_) { |
| 119 CFMachPortInvalidate(input_mach_port_); |
| 120 CFRunLoopRemoveSource( |
| 121 CFRunLoopGetMain(), input_run_loop_source_, kCFRunLoopCommonModes); |
| 122 input_run_loop_source_.reset(); |
| 123 input_mach_port_.reset(); |
| 124 } |
| 125 } |
| 126 |
| 127 void UserInputMonitorMac::StartKeyboardMonitoring() { |
| 128 initial_key_press_count_ = CGEventSourceCounterForEventType( |
| 129 kCGEventSourceStateHIDSystemState, kCGEventKeyDown); |
| 130 } |
| 131 |
| 132 void UserInputMonitorMac::StopKeyboardMonitoring() { |
| 133 initial_key_press_count_ = 0; |
| 134 } |
| 135 |
| 136 } // namespace |
| 137 |
| 138 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( |
| 139 const scoped_refptr<base::SingleThreadTaskRunner>& input_task_runner, |
| 140 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { |
| 141 return scoped_ptr<UserInputMonitor>(new UserInputMonitorMac(ui_task_runner)); |
| 142 } |
| 143 |
| 144 } // namespace media |
OLD | NEW |