Chromium Code Reviews| Index: media/base/user_input_monitor_mac.mm |
| diff --git a/media/base/user_input_monitor_mac.mm b/media/base/user_input_monitor_mac.mm |
| index 4ffad42a72d9b479d1611e4a10fea96ae41e0e5d..57c7930a95b2c3898f15e94d36a9dc4e561cb4a4 100644 |
| --- a/media/base/user_input_monitor_mac.mm |
| +++ b/media/base/user_input_monitor_mac.mm |
| @@ -4,13 +4,232 @@ |
| #include "media/base/user_input_monitor.h" |
| +#import <AppKit/AppKit.h> |
| + |
| +#include <ApplicationServices/ApplicationServices.h> |
| + |
| +#include "base/bind.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/location.h" |
| +#include "base/logging.h" |
| +#include "base/mac/scoped_cftyperef.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/threading/non_thread_safe.h" |
| +#include "base/time/time.h" |
| +#include "third_party/skia/include/core/SkPoint.h" |
| +#import "third_party/GTM/AppKit/GTMCarbonEvent.h" |
| + |
| namespace media { |
| +namespace { |
| + |
| +class UserInputMonitorMacCore; |
| + |
| +class UserInputMonitorMac : public base::NonThreadSafe, |
| + 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 X Window events are received. |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| + |
| + scoped_refptr<UserInputMonitorMacCore> core_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UserInputMonitorMac); |
| +}; |
| + |
| +// The class implements the event listening. Must be called on the UI thread. |
| +class UserInputMonitorMacCore |
| + : public base::RefCountedThreadSafe<UserInputMonitorMacCore> { |
| + public: |
| + UserInputMonitorMacCore( |
| + const UserInputMonitorMac::MouseCallback& mouse_callback, |
| + const base::Closure& key_stroke_callback); |
| + |
| + void StartMonitoringMouse(); |
| + void StopMonitoringMouse(); |
| + void StartPollingKeyState(); |
| + void StopPollingKeyState(); |
| + |
| + void OnMouseMoved(const SkIPoint& position); |
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<UserInputMonitorMacCore>; |
| + virtual ~UserInputMonitorMacCore(); |
| + |
| + void PollKeyState(); |
| + |
| + UserInputMonitorMac::MouseCallback mouse_callback_; |
| + base::Closure key_stroke_callback_; |
| + |
| + CFRunLoopSourceRef input_run_loop_source_; |
| + base::ScopedCFTypeRef<CFMachPortRef> input_mach_port_; |
| + |
| + bool polling_key_state_; |
| + // 0x5d is key "9", after that comes function keys. |
| + bool prev_key_state_[0x5d]; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UserInputMonitorMacCore); |
| +}; |
| + |
| +static CGEventRef inputEvent(CGEventTapProxy proxy, |
|
Robert Sesek
2013/08/21 14:43:24
This should be a static member function.
|
| + CGEventType type, |
| + CGEventRef event, |
| + void* context) { |
| + int64_t pid = CGEventGetIntegerValueField(event, kCGEventSourceUnixProcessID); |
| + if (pid == 0) { |
| + DCHECK(type == kCGEventMouseMoved); |
| + 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; |
| +} |
| + |
| +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() {} |
| + |
| +void UserInputMonitorMac::StartMouseMonitoring() { |
|
Robert Sesek
2013/08/21 14:43:24
I'm wondering if this could be simplified s.t. rat
|
| + ui_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&UserInputMonitorMacCore::StartMonitoringMouse, |
| + core_.get())); |
| +} |
| + |
| +void UserInputMonitorMac::StopMouseMonitoring() { |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&UserInputMonitorMacCore::StopMonitoringMouse, |
| + core_.get())); |
| +} |
| + |
| +void UserInputMonitorMac::StartKeyboardMonitoring() { |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&UserInputMonitorMacCore::StartPollingKeyState, |
| + core_.get())); |
| +} |
| + |
| +void UserInputMonitorMac::StopKeyboardMonitoring() { |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&UserInputMonitorMacCore::StopPollingKeyState, |
| + core_.get())); |
| +} |
| + |
| +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_key_state_(false) {} |
| + |
| +void UserInputMonitorMacCore::StartMonitoringMouse() { |
| + input_mach_port_.reset(CGEventTapCreate( |
| + kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionListenOnly, |
| + CGEventMaskBit(kCGEventMouseMoved), inputEvent, this)); |
| + if (input_mach_port_) { |
| + input_run_loop_source_ = CFMachPortCreateRunLoopSource( |
| + NULL, input_mach_port_, 0); |
| + CFRunLoopAddSource( |
| + CFRunLoopGetMain(), input_run_loop_source_, kCFRunLoopCommonModes); |
| + } else { |
| + LOG(ERROR) << "CGEventTapCreate failed."; |
| + } |
| +} |
| + |
| +void UserInputMonitorMacCore::StopMonitoringMouse() { |
| + if (input_run_loop_source_) { |
| + CFMachPortInvalidate(input_mach_port_); |
| + CFRunLoopRemoveSource( |
| + CFRunLoopGetMain(), input_run_loop_source_, kCFRunLoopCommonModes); |
| + CFRelease(input_run_loop_source_); |
| + input_mach_port_.reset(); |
| + input_run_loop_source_ = NULL; |
| + } |
| +} |
| + |
| +void UserInputMonitorMacCore::StartPollingKeyState() { |
| + polling_key_state_ = true; |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&UserInputMonitorMacCore::PollKeyState, this), |
| + base::TimeDelta::FromMilliseconds(10)); |
| +} |
| + |
| +void UserInputMonitorMacCore::StopPollingKeyState() { |
| + polling_key_state_ = false; |
| +} |
| + |
| +UserInputMonitorMacCore::~UserInputMonitorMacCore() { |
| + DCHECK(!input_mach_port_); |
| + DCHECK(input_run_loop_source_); |
| + DCHECK(!polling_key_state_); |
| +} |
| + |
| +void UserInputMonitorMacCore::OnMouseMoved(const SkIPoint& position) { |
| + mouse_callback_.Run(position); |
| +} |
| + |
| +void UserInputMonitorMacCore::PollKeyState() { |
| + if (!polling_key_state_) |
| + return; |
| + |
| + bool key_pressed = false; |
| + for (uint key_index = 0; |
| + key_index <= sizeof(prev_key_state_)/sizeof(prev_key_state_[0]); |
| + ++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) |
| + key_stroke_callback_.Run(); |
| + |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&UserInputMonitorMacCore::PollKeyState, this), |
| + base::TimeDelta::FromMilliseconds(10)); |
| +} |
| + |
| +} // namespace |
| -// TODO(jiayl): add the implementation. |
| 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>(); |
| + return scoped_ptr<UserInputMonitor>(new UserInputMonitorMac(ui_task_runner)); |
| } |
| } // namespace media |