Index: media/base/user_input_monitor_mac.cc |
diff --git a/media/base/user_input_monitor_mac.cc b/media/base/user_input_monitor_mac.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..44a8a884f6eb669c0bc34991975902b358a14f3d |
--- /dev/null |
+++ b/media/base/user_input_monitor_mac.cc |
@@ -0,0 +1,266 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/base/user_input_monitor.h" |
+ |
+#include <ApplicationServices/ApplicationServices.h> |
+ |
+#include "base/bind.h" |
+#include "base/logging.h" |
+#include "base/mac/scoped_cftyperef.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/time/time.h" |
+#include "base/timer/timer.h" |
+#include "third_party/skia/include/core/SkPoint.h" |
+ |
+namespace media { |
+namespace { |
+ |
+class UserInputMonitorMacCore; |
+ |
+class UserInputMonitorMac : public UserInputMonitor { |
+ public: |
+ typedef const base::Callback<void(const SkIPoint&)> MouseCallback; |
+ |
+ UserInputMonitorMac( |
+ scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); |
+ |
+ virtual ~UserInputMonitorMac(); |
+ |
+ private: |
+ virtual void StartMouseMonitoring() OVERRIDE; |
+ virtual void StopMouseMonitoring() OVERRIDE; |
+ virtual void StartKeyboardMonitoring() OVERRIDE; |
+ virtual void StopKeyboardMonitoring() OVERRIDE; |
+ |
+ void OnMouseMoved(const SkIPoint& position); |
+ void OnKeyStroke(); |
+ |
+ // Task runner on which input events are received. |
+ scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
+ |
+ scoped_ptr<UserInputMonitorMacCore> core_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(UserInputMonitorMac); |
+}; |
+ |
+// The class implements the event listening. Must be called on the UI thread. |
+class UserInputMonitorMacCore { |
+ public: |
+ UserInputMonitorMacCore( |
+ const UserInputMonitorMac::MouseCallback& mouse_callback, |
+ const base::Closure& key_stroke_callback); |
+ ~UserInputMonitorMacCore(); |
+ |
+ void StartMonitoringMouse(); |
+ void StopMonitoringMouse(); |
+ void StartPollingKeyState(); |
+ void StopPollingKeyState(); |
+ |
+ void OnMouseMoved(const SkIPoint& position); |
+ |
+ base::WeakPtr<UserInputMonitorMacCore> GetAsWeakPtr(); |
+ |
+ private: |
+ static CGEventRef MouseMoved(CGEventTapProxy proxy, |
+ CGEventType type, |
+ CGEventRef event, |
+ void* context); |
+ void PollKeyState(); |
+ |
+ UserInputMonitorMac::MouseCallback mouse_callback_; |
+ base::Closure key_stroke_callback_; |
+ |
+ base::ScopedCFTypeRef<CFRunLoopSourceRef> input_run_loop_source_; |
+ base::ScopedCFTypeRef<CFMachPortRef> input_mach_port_; |
+ |
+ base::Timer polling_timer_; |
+ // 0x5c is key "9", after that comes function keys. |
+ bool prev_key_state_[0x5d]; |
+ |
+ base::WeakPtrFactory<UserInputMonitorMacCore> weak_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(UserInputMonitorMacCore); |
+}; |
+ |
+UserInputMonitorMac::UserInputMonitorMac( |
+ scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) |
+ : ui_task_runner_(ui_task_runner), |
+ core_(new UserInputMonitorMacCore( |
+ base::Bind(&UserInputMonitorMac::OnMouseMoved, |
+ base::Unretained(this)), |
+ base::Bind(&UserInputMonitorMac::OnKeyStroke, |
+ base::Unretained(this)))) {} |
+ |
+UserInputMonitorMac::~UserInputMonitorMac() { |
+ ui_task_runner_->DeleteSoon(FROM_HERE, core_.release()); |
+} |
+ |
+void UserInputMonitorMac::StartMouseMonitoring() { |
+ if (!ui_task_runner_->BelongsToCurrentThread()) { |
+ ui_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&UserInputMonitorMacCore::StartMonitoringMouse, |
+ core_->GetAsWeakPtr())); |
+ return; |
+ } |
+ core_->StartMonitoringMouse(); |
+} |
+ |
+void UserInputMonitorMac::StopMouseMonitoring() { |
+ if (!ui_task_runner_->BelongsToCurrentThread()) { |
+ ui_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&UserInputMonitorMacCore::StopMonitoringMouse, |
+ core_->GetAsWeakPtr())); |
+ return; |
+ } |
+ core_->StopMonitoringMouse(); |
+} |
+ |
+void UserInputMonitorMac::StartKeyboardMonitoring() { |
+ if (!ui_task_runner_->BelongsToCurrentThread()) { |
+ ui_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&UserInputMonitorMacCore::StartPollingKeyState, |
+ core_->GetAsWeakPtr())); |
+ return; |
+ } |
+ core_->StartPollingKeyState(); |
+} |
+ |
+void UserInputMonitorMac::StopKeyboardMonitoring() { |
+ if (!ui_task_runner_->BelongsToCurrentThread()) { |
+ ui_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&UserInputMonitorMacCore::StopPollingKeyState, |
+ core_->GetAsWeakPtr())); |
+ return; |
+ } |
+ core_->StopPollingKeyState(); |
+} |
+ |
+void UserInputMonitorMac::OnMouseMoved(const SkIPoint& position) { |
+ UserInputMonitor::OnMouseEvent(position); |
+} |
+ |
+void UserInputMonitorMac::OnKeyStroke() { |
+ UserInputMonitor::OnKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN); |
+} |
+ |
+UserInputMonitorMacCore::UserInputMonitorMacCore( |
+ const UserInputMonitorMac::MouseCallback& mouse_callback, |
+ const base::Closure& key_stroke_callback) |
+ : mouse_callback_(mouse_callback), |
+ key_stroke_callback_(key_stroke_callback), |
+ polling_timer_(false, true), |
+ weak_factory_(this) {} |
+ |
+UserInputMonitorMacCore::~UserInputMonitorMacCore() { |
+ DCHECK(!input_mach_port_); |
+ DCHECK(!input_run_loop_source_); |
+ DCHECK(!polling_timer_.IsRunning()); |
+} |
+ |
+void UserInputMonitorMacCore::StartMonitoringMouse() { |
+ DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); |
+ |
+ input_mach_port_.reset(CGEventTapCreate(kCGSessionEventTap, |
+ kCGHeadInsertEventTap, |
+ kCGEventTapOptionListenOnly, |
+ CGEventMaskBit(kCGEventMouseMoved), |
+ MouseMoved, |
+ this)); |
+ if (input_mach_port_) { |
+ input_run_loop_source_.reset( |
+ CFMachPortCreateRunLoopSource(NULL, input_mach_port_, 0)); |
+ CFRunLoopAddSource( |
+ CFRunLoopGetMain(), input_run_loop_source_, kCFRunLoopCommonModes); |
+ } else { |
+ LOG(ERROR) << "CGEventTapCreate failed."; |
+ } |
+} |
+ |
+void UserInputMonitorMacCore::StopMonitoringMouse() { |
+ DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); |
+ |
+ if (input_run_loop_source_) { |
+ CFMachPortInvalidate(input_mach_port_); |
+ CFRunLoopRemoveSource( |
+ CFRunLoopGetMain(), input_run_loop_source_, kCFRunLoopCommonModes); |
+ input_run_loop_source_.reset(); |
+ input_mach_port_.reset(); |
+ } |
+} |
+ |
+void UserInputMonitorMacCore::StartPollingKeyState() { |
+ DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); |
+ |
+ polling_timer_.Start( |
+ FROM_HERE, |
+ base::TimeDelta::FromMilliseconds(30), |
+ base::Bind(&UserInputMonitorMacCore::PollKeyState, GetAsWeakPtr())); |
+} |
+ |
+void UserInputMonitorMacCore::StopPollingKeyState() { |
+ DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); |
+ |
+ polling_timer_.Stop(); |
+} |
+ |
+base::WeakPtr<UserInputMonitorMacCore> UserInputMonitorMacCore::GetAsWeakPtr() { |
+ return weak_factory_.GetWeakPtr(); |
+} |
+ |
+void UserInputMonitorMacCore::OnMouseMoved(const SkIPoint& position) { |
+ DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); |
+ |
+ mouse_callback_.Run(position); |
+} |
+ |
+CGEventRef UserInputMonitorMacCore::MouseMoved(CGEventTapProxy proxy, |
+ CGEventType type, |
+ CGEventRef event, |
+ void* context) { |
+ DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); |
+ |
+ int64_t pid = CGEventGetIntegerValueField(event, kCGEventSourceUnixProcessID); |
+ // |pid| 0 means the event is generated by hardware. |
+ if (pid == 0) { |
+ DCHECK_EQ(kCGEventMouseMoved, type); |
+ CGPoint cg_mouse_pos = CGEventGetLocation(event); |
+ SkIPoint mouse_pos = SkIPoint::Make(cg_mouse_pos.x, cg_mouse_pos.y); |
+ static_cast<UserInputMonitorMacCore*>(context)->OnMouseMoved(mouse_pos); |
+ } |
+ return NULL; |
+} |
+ |
+void UserInputMonitorMacCore::PollKeyState() { |
+ DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); |
+ |
+ bool key_pressed = false; |
+ for (CGKeyCode key_index = 0; key_index < arraysize(prev_key_state_); |
+ ++key_index) { |
+ bool key_state = |
+ CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, key_index); |
Wez
2013/08/21 22:41:57
This should be *SessionState, not *SystemState, I
jiayl
2013/08/21 22:58:23
The webrtc impl uses *SystemState:
https://code.go
Wez
2013/08/21 23:19:53
Monitoring session events is correct in terms of s
jiayl
2013/08/21 23:32:59
I'm not sure. What's the state of audio capturing
|
+ // A false -> true change in keymap means a key is pressed. |
+ key_pressed |= (key_state && !prev_key_state_[key_index]); |
+ // Save current state. |
+ prev_key_state_[key_index] = key_state; |
+ } |
+ |
+ if (key_pressed) |
+ key_stroke_callback_.Run(); |
+} |
+ |
+} // namespace |
+ |
+scoped_ptr<UserInputMonitor> UserInputMonitor::Create( |
+ const scoped_refptr<base::SingleThreadTaskRunner>& input_task_runner, |
+ const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { |
+ return scoped_ptr<UserInputMonitor>(new UserInputMonitorMac(ui_task_runner)); |
+} |
+ |
+} // namespace media |