Chromium Code Reviews| 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..13b043c1faea4c5052b445c48cb01ccaa1af8818 |
| --- /dev/null |
| +++ b/media/base/user_input_monitor_mac.cc |
| @@ -0,0 +1,173 @@ |
| +// 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 UserInputMonitorMac : public UserInputMonitor { |
| + public: |
| + explicit UserInputMonitorMac( |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); |
| + virtual ~UserInputMonitorMac(); |
| + |
| + private: |
| + static CGEventRef MouseMoved(CGEventTapProxy proxy, |
| + CGEventType type, |
| + CGEventRef event, |
| + void* context); |
| + |
| + virtual void StartMouseMonitoring() OVERRIDE; |
| + virtual void StopMouseMonitoring() OVERRIDE; |
| + virtual void StartKeyboardMonitoring() OVERRIDE; |
| + virtual void StopKeyboardMonitoring() OVERRIDE; |
| + |
| + void PollKeyState(); |
| + |
| + // Task runner on which input events are received. |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| + |
| + // These members should only be accessed on the UI thread. |
| + 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]; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UserInputMonitorMac); |
| +}; |
| + |
| +UserInputMonitorMac::UserInputMonitorMac( |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) |
| + : ui_task_runner_(ui_task_runner), |
| + polling_timer_(false, true) {} |
| + |
| +UserInputMonitorMac::~UserInputMonitorMac() { |
| + DCHECK(!input_mach_port_); |
| + DCHECK(!input_run_loop_source_); |
| + DCHECK(!polling_timer_.IsRunning()); |
| +} |
| + |
| +CGEventRef UserInputMonitorMac::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<UserInputMonitorMac*>(context)->OnMouseEvent(mouse_pos); |
| + } |
| + return NULL; |
| +} |
| + |
| +void UserInputMonitorMac::StartMouseMonitoring() { |
| + if (!ui_task_runner_->BelongsToCurrentThread()) { |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&UserInputMonitorMac::StartMouseMonitoring, |
| + base::Unretained(this))); |
|
Wez
2013/08/21 23:19:53
This code will now crash if the calling thread e.g
jiayl
2013/08/21 23:32:59
UserInputMonitor should always outlive the UI/IO t
Wez
2013/08/21 23:43:54
That's specific to the BrowserMainLoop caller - I
|
| + return; |
| + } |
| + 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 UserInputMonitorMac::StopMouseMonitoring() { |
| + if (!ui_task_runner_->BelongsToCurrentThread()) { |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&UserInputMonitorMac::StopMouseMonitoring, |
| + base::Unretained(this))); |
| + return; |
| + } |
| + 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 UserInputMonitorMac::StartKeyboardMonitoring() { |
| + if (!ui_task_runner_->BelongsToCurrentThread()) { |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&UserInputMonitorMac::StartKeyboardMonitoring, |
| + base::Unretained(this))); |
| + return; |
| + } |
| + polling_timer_.Start( |
| + FROM_HERE, |
| + base::TimeDelta::FromMilliseconds(30), |
| + base::Bind(&UserInputMonitorMac::PollKeyState, base::Unretained(this))); |
| +} |
| + |
| +void UserInputMonitorMac::StopKeyboardMonitoring() { |
| + if (!ui_task_runner_->BelongsToCurrentThread()) { |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&UserInputMonitorMac::StopKeyboardMonitoring, |
| + base::Unretained(this))); |
| + return; |
| + } |
| + polling_timer_.Stop(); |
| +} |
| + |
| +void UserInputMonitorMac::PollKeyState() { |
| + DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| + |
| + bool key_pressed = false; |
| + for (CGKeyCode key_index = 0; key_index < arraysize(prev_key_state_); |
| + ++key_index) { |
| + bool key_state = |
| + CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, key_index); |
| + // 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) |
| + OnKeyboardEvent(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN); |
| +} |
| + |
| +} // 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 |