Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1310)

Unified Diff: media/base/user_input_monitor_mac.cc

Issue 22801007: Adds the UserInputMonitor implementation for Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..b0f7d84be37714b18d153e469c05ed7280ec598d
--- /dev/null
+++ b/media/base/user_input_monitor_mac.cc
@@ -0,0 +1,144 @@
+// 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();
+
+ virtual size_t GetKeyPressCount() const OVERRIDE;
+
+ 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;
+
+ // 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_;
+
+ size_t initial_key_press_count_;
+
+ DISALLOW_COPY_AND_ASSIGN(UserInputMonitorMac);
+};
+
+UserInputMonitorMac::UserInputMonitorMac(
+ scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
+ : ui_task_runner_(ui_task_runner),
+ initial_key_press_count_(0) {}
+
+UserInputMonitorMac::~UserInputMonitorMac() {
+ DCHECK(!input_mach_port_);
+ DCHECK(!input_run_loop_source_);
+ DCHECK(!initial_key_press_count_);
+}
+
+size_t UserInputMonitorMac::GetKeyPressCount() const {
+ size_t total_count = CGEventSourceCounterForEventType(
+ kCGEventSourceStateHIDSystemState, kCGEventKeyDown);
+ return total_count - initial_key_press_count_;
+}
+
+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)));
+ 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() {
+ initial_key_press_count_ = CGEventSourceCounterForEventType(
+ kCGEventSourceStateHIDSystemState, kCGEventKeyDown);
+}
+
+void UserInputMonitorMac::StopKeyboardMonitoring() {
+ initial_key_press_count_ = 0;
+}
+
+} // 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

Powered by Google App Engine
This is Rietveld 408576698