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

Side by Side Diff: media/base/user_input_monitor.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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/base/user_input_monitor.h" 5 #include "media/base/user_input_monitor.h"
6 6
7 #include "third_party/skia/include/core/SkPoint.h" 7 #include "third_party/skia/include/core/SkPoint.h"
8 8
9 #if defined(OS_MACOSX)
10 #include <ApplicationServices/ApplicationServices.h>
11
12 #include "base/bind.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/time/time.h"
15 #endif // defined(OS_MACOSX)
16
9 namespace media { 17 namespace media {
10 18
11 #ifdef DISABLE_USER_INPUT_MONITOR 19 #ifdef DISABLE_USER_INPUT_MONITOR
12 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( 20 scoped_ptr<UserInputMonitor> UserInputMonitor::Create(
13 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 21 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
14 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { 22 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) {
15 return scoped_ptr<UserInputMonitor>(); 23 return scoped_ptr<UserInputMonitor>();
16 } 24 }
17 #endif // DISABLE_USER_INPUT_MONITOR 25 #endif // DISABLE_USER_INPUT_MONITOR
18 26
(...skipping 14 matching lines...) Expand all
33 void UserInputMonitor::RemoveMouseListener(MouseEventListener* listener) { 41 void UserInputMonitor::RemoveMouseListener(MouseEventListener* listener) {
34 base::AutoLock auto_lock(lock_); 42 base::AutoLock auto_lock(lock_);
35 mouse_listeners_.RemoveObserver(listener); 43 mouse_listeners_.RemoveObserver(listener);
36 if (mouse_listeners_.size() == 0) { 44 if (mouse_listeners_.size() == 0) {
37 StopMouseMonitoring(); 45 StopMouseMonitoring();
38 monitoring_mouse_ = false; 46 monitoring_mouse_ = false;
39 DVLOG(2) << "Stopped mouse monitoring."; 47 DVLOG(2) << "Stopped mouse monitoring.";
40 } 48 }
41 } 49 }
42 void UserInputMonitor::AddKeyStrokeListener(KeyStrokeListener* listener) { 50 void UserInputMonitor::AddKeyStrokeListener(KeyStrokeListener* listener) {
43 base::AutoLock auto_lock(lock_); 51 bool start_polling = false;
44 key_stroke_listeners_.AddObserver(listener); 52 {
45 if (!monitoring_keyboard_) { 53 base::AutoLock auto_lock(lock_);
46 StartKeyboardMonitoring(); 54 key_stroke_listeners_.AddObserver(listener);
47 monitoring_keyboard_ = true; 55 if (!monitoring_keyboard_) {
48 DVLOG(2) << "Started keyboard monitoring."; 56 #if !defined(OS_MACOSX)
57 StartKeyboardMonitoring();
58 #else
59 start_polling = true;
60 #endif // defined(OS_MACOSX)
61 monitoring_keyboard_ = true;
62 DVLOG(2) << "Started keyboard monitoring.";
63 }
64 }
65
66 if (start_polling) {
67 PollKeyState();
49 } 68 }
50 } 69 }
51 void UserInputMonitor::RemoveKeyStrokeListener(KeyStrokeListener* listener) { 70 void UserInputMonitor::RemoveKeyStrokeListener(KeyStrokeListener* listener) {
52 base::AutoLock auto_lock(lock_); 71 base::AutoLock auto_lock(lock_);
53 key_stroke_listeners_.RemoveObserver(listener); 72 key_stroke_listeners_.RemoveObserver(listener);
54 if (key_stroke_listeners_.size() == 0) { 73 if (key_stroke_listeners_.size() == 0) {
74 #if !defined(OS_MACOSX)
55 StopKeyboardMonitoring(); 75 StopKeyboardMonitoring();
76 #endif // !defined(OS_MACOSX)
77
56 monitoring_keyboard_ = false; 78 monitoring_keyboard_ = false;
57 DVLOG(2) << "Stopped keyboard monitoring."; 79 DVLOG(2) << "Stopped keyboard monitoring.";
58 } 80 }
59 } 81 }
60 82
61 UserInputMonitor::UserInputMonitor() 83 UserInputMonitor::UserInputMonitor()
62 : monitoring_mouse_(false), monitoring_keyboard_(false) {} 84 : monitoring_mouse_(false), monitoring_keyboard_(false) {}
63 85
64 void UserInputMonitor::OnMouseEvent(const SkIPoint& position) { 86 void UserInputMonitor::OnMouseEvent(const SkIPoint& position) {
65 base::AutoLock auto_lock(lock_); 87 base::AutoLock auto_lock(lock_);
(...skipping 11 matching lines...) Expand all
77 pressed_keys_.insert(key_code); 99 pressed_keys_.insert(key_code);
78 DVLOG(6) << "Key stroke detected."; 100 DVLOG(6) << "Key stroke detected.";
79 FOR_EACH_OBSERVER(KeyStrokeListener, key_stroke_listeners_, OnKeyStroke()); 101 FOR_EACH_OBSERVER(KeyStrokeListener, key_stroke_listeners_, OnKeyStroke());
80 } else { 102 } else {
81 DCHECK_EQ(ui::ET_KEY_RELEASED, event); 103 DCHECK_EQ(ui::ET_KEY_RELEASED, event);
82 DCHECK(pressed_keys_.find(key_code) != pressed_keys_.end()); 104 DCHECK(pressed_keys_.find(key_code) != pressed_keys_.end());
83 pressed_keys_.erase(key_code); 105 pressed_keys_.erase(key_code);
84 } 106 }
85 } 107 }
86 108
109 void UserInputMonitor::PollKeyState() {
DaleCurtis 2013/08/19 23:49:00 Seems like this should go in UserInputMonitorMac,
jiayl 2013/08/20 00:21:15 I would need another pressed_key_map in UserInputM
DaleCurtis 2013/08/20 00:35:54 If you allow OnKeyboardEvent() to be called w/ ET_
jiayl 2013/08/20 00:42:59 So you mean firing Key_release for all keys not in
DaleCurtis 2013/08/20 00:52:18 OnKeyboardEvent() doesn't fire anything for ET_KEY
110 #if defined(OS_MACOSX)
111 {
112 base::AutoLock auto_lock(lock_);
113 if (!monitoring_keyboard_)
114 return;
115 }
116 bool key_pressed = false;
117
118 // 0x5d is key "9", after that comes function keys.
119 for (int key_index = 0; key_index <= 0x5d; ++key_index) {
120 bool key_state = CGEventSourceKeyState(kCGEventSourceStateHIDSystemState,
121 key_index);
DaleCurtis 2013/08/19 23:49:00 Indent is off.
jiayl 2013/08/20 00:21:15 Done.
122
123 if (key_state && pressed_keys_.find(key_index) == pressed_keys_.end()) {
DaleCurtis 2013/08/19 23:49:00 Save result so you're not calling find() twice.
jiayl 2013/08/20 00:21:15 Done.
124 key_pressed = true;;
125 pressed_keys_.insert(key_index);
126 } else if (!key_state &&
127 pressed_keys_.find(key_index) != pressed_keys_.end()) {
DaleCurtis 2013/08/19 23:49:00 Ditto.
jiayl 2013/08/20 00:21:15 Done.
128 pressed_keys_.erase(key_index);
129 }
130 }
131
132 if (key_pressed) {
133 base::AutoLock auto_lock(lock_);
134 DVLOG(6) << "Key stroke detected.";
135 FOR_EACH_OBSERVER(KeyStrokeListener, key_stroke_listeners_, OnKeyStroke());
136 }
137
138 base::MessageLoop::current()->PostDelayedTask(
DaleCurtis 2013/08/19 23:49:00 Which loop is this running on? It looks like it'll
jiayl 2013/08/20 00:21:15 Yeh, this is gross. But I don't want to run it on
DaleCurtis 2013/08/20 00:35:54 Well not only is it gross, it doesn't work: consid
DaleCurtis 2013/08/20 00:40:00 Ah, nevermind those require Assistive as well :/
jiayl 2013/08/20 00:42:59 Right, the document says A global event monitor lo
DaleCurtis 2013/08/20 00:52:18 Can we ask users for assistive access? FYI, on OS
139 FROM_HERE,
140 base::Bind(&UserInputMonitor::PollKeyState, base::Unretained(this)),
141 base::TimeDelta::FromMilliseconds(10));
142 #endif // defined(OS_MACOSX)
143 }
144
87 } // namespace media 145 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698