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

Side by Side Diff: media/base/user_input_monitor_linux.cc

Issue 23702008: Adds the UserInputMonitor implementation for Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 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 <sys/select.h> 7 #include <sys/select.h>
8 #include <unistd.h> 8 #include <unistd.h>
9 #define XK_MISCELLANY 9 #define XK_MISCELLANY
10 #include <X11/keysymdef.h> 10 #include <X11/keysymdef.h>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/callback.h" 14 #include "base/callback.h"
15 #include "base/compiler_specific.h" 15 #include "base/compiler_specific.h"
16 #include "base/location.h" 16 #include "base/location.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/memory/weak_ptr.h"
18 #include "base/message_loop/message_loop.h" 19 #include "base/message_loop/message_loop.h"
19 #include "base/message_loop/message_pump_libevent.h" 20 #include "base/message_loop/message_pump_libevent.h"
21 #include "base/observer_list_threadsafe.h"
20 #include "base/posix/eintr_wrapper.h" 22 #include "base/posix/eintr_wrapper.h"
21 #include "base/single_thread_task_runner.h" 23 #include "base/single_thread_task_runner.h"
24 #include "base/synchronization/lock.h"
22 #include "media/base/keyboard_event_counter.h" 25 #include "media/base/keyboard_event_counter.h"
23 #include "third_party/skia/include/core/SkPoint.h" 26 #include "third_party/skia/include/core/SkPoint.h"
24 #include "ui/base/keycodes/keyboard_code_conversion_x.h" 27 #include "ui/base/keycodes/keyboard_code_conversion_x.h"
25 28
26 // These includes need to be later than dictated by the style guide due to 29 // These includes need to be later than dictated by the style guide due to
27 // Xlib header pollution, specifically the min, max, and Status macros. 30 // Xlib header pollution, specifically the min, max, and Status macros.
28 #include <X11/XKBlib.h> 31 #include <X11/XKBlib.h>
29 #include <X11/Xlibint.h> 32 #include <X11/Xlibint.h>
30 #include <X11/extensions/record.h> 33 #include <X11/extensions/record.h>
31 34
32 namespace media { 35 namespace media {
33 namespace { 36 namespace {
34 37
35 class UserInputMonitorLinux : public UserInputMonitor , 38 class UserInputMonitorLinux : public UserInputMonitor {
36 public base::MessagePumpLibevent::Watcher {
37 public: 39 public:
38 explicit UserInputMonitorLinux( 40 explicit UserInputMonitorLinux(
39 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); 41 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
40 virtual ~UserInputMonitorLinux(); 42 virtual ~UserInputMonitorLinux();
41 43
44 // Public interface of UserInputMonitor.
45 virtual void AddMouseListener(MouseEventListener* listener) OVERRIDE;
46 virtual void RemoveMouseListener(MouseEventListener* listener) OVERRIDE;
42 virtual size_t GetKeyPressCount() const OVERRIDE; 47 virtual size_t GetKeyPressCount() const OVERRIDE;
43 48
44 private: 49 private:
50 // Private interface of UserInputMonitor.
51 virtual void StartKeyboardMonitoring() OVERRIDE;
52 virtual void StopKeyboardMonitoring() OVERRIDE;
53
54 class Core;
55 Core* core_;
56
57 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux);
58 };
59
60 // This is the actual implementation of event monitoring. It's separated from
61 // UserInputMonitorLinux since it needs to be deleted on the IO thread.
62 class UserInputMonitorLinux::Core : public base::MessagePumpLibevent::Watcher {
63 public:
64 explicit Core(
65 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
66 virtual ~Core();
67
68 void AddMouseListener(MouseEventListener* listener);
69 void RemoveMouseListener(MouseEventListener* listener);
70 size_t GetKeyPressCount() const;
71 void StartKeyboardMonitoring();
72 void StopKeyboardMonitoring();
73 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner() {
74 return io_task_runner_;
75 }
76
77 private:
45 enum EventType { 78 enum EventType {
46 MOUSE_EVENT, 79 MOUSE_EVENT,
47 KEYBOARD_EVENT 80 KEYBOARD_EVENT
48 }; 81 };
49 82
50 virtual void StartMouseMonitoring() OVERRIDE;
51 virtual void StopMouseMonitoring() OVERRIDE;
52 virtual void StartKeyboardMonitoring() OVERRIDE;
53 virtual void StopKeyboardMonitoring() OVERRIDE;
54
55 //
56 // The following methods must be called on the IO thread.
57 //
58 void StartMonitor(EventType type); 83 void StartMonitor(EventType type);
59 void StopMonitor(EventType type); 84 void StopMonitor(EventType type);
60 85
61 // base::MessagePumpLibevent::Watcher interface. 86 // base::MessagePumpLibevent::Watcher interface.
62 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; 87 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
63 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; 88 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
64 89
65 // Processes key and mouse events. 90 // Processes key and mouse events.
66 void ProcessXEvent(xEvent* event); 91 void ProcessXEvent(xEvent* event);
67 static void ProcessReply(XPointer self, XRecordInterceptData* data); 92 static void ProcessReply(XPointer self, XRecordInterceptData* data);
68 93
69 // Task runner on which X Window events are received. 94 base::Lock lock_;
70 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 95 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
96 scoped_refptr<ObserverListThreadSafe<MouseEventListener> > mouse_listeners_;
97 size_t mouse_listeners_count_;
71 98
72 // 99 //
73 // The following members should only be accessed on the IO thread. 100 // The following members should only be accessed on the IO thread.
74 // 101 //
75 base::MessagePumpLibevent::FileDescriptorWatcher controller_; 102 base::MessagePumpLibevent::FileDescriptorWatcher controller_;
76 Display* display_; 103 Display* display_;
77 Display* x_record_display_; 104 Display* x_record_display_;
78 XRecordRange* x_record_range_[2]; 105 XRecordRange* x_record_range_[2];
79 XRecordContext x_record_context_; 106 XRecordContext x_record_context_;
80 KeyboardEventCounter counter_; 107 KeyboardEventCounter counter_;
108 base::WeakPtrFactory<Core> weak_factory_;
81 109
82 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); 110 DISALLOW_COPY_AND_ASSIGN(Core);
83 }; 111 };
84 112
85 UserInputMonitorLinux::UserInputMonitorLinux( 113 UserInputMonitorLinux::Core::Core(
86 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) 114 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
87 : io_task_runner_(io_task_runner), 115 : io_task_runner_(io_task_runner),
116 mouse_listeners_(new ObserverListThreadSafe<MouseEventListener>()),
117 mouse_listeners_count_(0),
88 display_(NULL), 118 display_(NULL),
89 x_record_display_(NULL), 119 x_record_display_(NULL),
90 x_record_context_(0) { 120 x_record_context_(0),
121 weak_factory_(this) {
91 x_record_range_[0] = NULL; 122 x_record_range_[0] = NULL;
92 x_record_range_[1] = NULL; 123 x_record_range_[1] = NULL;
93 } 124 }
94 125
95 UserInputMonitorLinux::~UserInputMonitorLinux() { 126 UserInputMonitorLinux::Core::~Core() {
127 DCHECK_EQ(0u, mouse_listeners_count_);
96 DCHECK(!display_); 128 DCHECK(!display_);
97 DCHECK(!x_record_display_); 129 DCHECK(!x_record_display_);
98 DCHECK(!x_record_range_[0]); 130 DCHECK(!x_record_range_[0]);
99 DCHECK(!x_record_range_[1]); 131 DCHECK(!x_record_range_[1]);
100 DCHECK(!x_record_context_); 132 DCHECK(!x_record_context_);
101 } 133 }
102 134
103 size_t UserInputMonitorLinux::GetKeyPressCount() const { 135 void UserInputMonitorLinux::Core::AddMouseListener(
136 MouseEventListener* listener) {
137 mouse_listeners_->AddObserver(listener);
138 {
139 base::AutoLock auto_lock(lock_);
140 mouse_listeners_count_++;
141 if (mouse_listeners_count_ == 1) {
142 StartMonitor(MOUSE_EVENT);
143 DVLOG(2) << "Started mouse monitoring.";
144 }
145 }
146 }
147
148 void UserInputMonitorLinux::Core::RemoveMouseListener(
149 MouseEventListener* listener) {
150 mouse_listeners_->RemoveObserver(listener);
151 {
152 base::AutoLock auto_lock(lock_);
153 mouse_listeners_count_--;
154 if (mouse_listeners_count_ == 0) {
155 StopMonitor(MOUSE_EVENT);
156 DVLOG(2) << "Stopped mouse monitoring.";
157 }
158 }
159 }
160
161 size_t UserInputMonitorLinux::Core::GetKeyPressCount() const {
104 return counter_.GetKeyPressCount(); 162 return counter_.GetKeyPressCount();
105 } 163 }
106 164
107 void UserInputMonitorLinux::StartMouseMonitoring() { 165 void UserInputMonitorLinux::Core::StartKeyboardMonitoring() {
166 StartMonitor(KEYBOARD_EVENT);
167 }
168
169 void UserInputMonitorLinux::Core::StopKeyboardMonitoring() {
170 StopMonitor(KEYBOARD_EVENT);
171 }
172
173 void UserInputMonitorLinux::Core::StartMonitor(EventType type) {
108 if (!io_task_runner_->BelongsToCurrentThread()) { 174 if (!io_task_runner_->BelongsToCurrentThread()) {
109 io_task_runner_->PostTask( 175 io_task_runner_->PostTask(
110 FROM_HERE, 176 FROM_HERE,
111 base::Bind(&UserInputMonitorLinux::StartMonitor, 177 base::Bind(&Core::StartMonitor, weak_factory_.GetWeakPtr(), type));
112 base::Unretained(this),
113 MOUSE_EVENT));
114 return; 178 return;
115 } 179 }
116 StartMonitor(MOUSE_EVENT);
117 }
118
119 void UserInputMonitorLinux::StopMouseMonitoring() {
120 if (!io_task_runner_->BelongsToCurrentThread()) {
121 io_task_runner_->PostTask(
122 FROM_HERE,
123 base::Bind(&UserInputMonitorLinux::StopMonitor,
124 base::Unretained(this),
125 MOUSE_EVENT));
126 return;
127 }
128 StopMonitor(MOUSE_EVENT);
129 }
130
131 void UserInputMonitorLinux::StartKeyboardMonitoring() {
132 if (!io_task_runner_->BelongsToCurrentThread()) {
133 io_task_runner_->PostTask(
134 FROM_HERE,
135 base::Bind(&UserInputMonitorLinux::StartMonitor,
136 base::Unretained(this),
137 KEYBOARD_EVENT));
138 return;
139 }
140 StartMonitor(KEYBOARD_EVENT);
141 }
142
143 void UserInputMonitorLinux::StopKeyboardMonitoring() {
144 if (!io_task_runner_->BelongsToCurrentThread()) {
145 io_task_runner_->PostTask(
146 FROM_HERE,
147 base::Bind(&UserInputMonitorLinux::StopMonitor,
148 base::Unretained(this),
149 KEYBOARD_EVENT));
150 return;
151 }
152 StopMonitor(KEYBOARD_EVENT);
153 }
154
155 void UserInputMonitorLinux::StartMonitor(EventType type) {
156 DCHECK(io_task_runner_->BelongsToCurrentThread());
157 180
158 if (type == KEYBOARD_EVENT) 181 if (type == KEYBOARD_EVENT)
159 counter_.Reset(); 182 counter_.Reset();
160 183
161 // TODO(jamiewalch): We should pass the display in. At that point, since 184 // TODO(jamiewalch): We should pass the display in. At that point, since
162 // XRecord needs a private connection to the X Server for its data channel 185 // XRecord needs a private connection to the X Server for its data channel
163 // and both channels are used from a separate thread, we'll need to duplicate 186 // and both channels are used from a separate thread, we'll need to duplicate
164 // them with something like the following: 187 // them with something like the following:
165 // XOpenDisplay(DisplayString(display)); 188 // XOpenDisplay(DisplayString(display));
166 if (!display_) 189 if (!display_)
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 1, 238 1,
216 record_range_to_use, 239 record_range_to_use,
217 number_of_ranges); 240 number_of_ranges);
218 if (!x_record_context_) { 241 if (!x_record_context_) {
219 LOG(ERROR) << "XRecordCreateContext failed."; 242 LOG(ERROR) << "XRecordCreateContext failed.";
220 return; 243 return;
221 } 244 }
222 245
223 if (!XRecordEnableContextAsync(x_record_display_, 246 if (!XRecordEnableContextAsync(x_record_display_,
224 x_record_context_, 247 x_record_context_,
225 &UserInputMonitorLinux::ProcessReply, 248 &Core::ProcessReply,
226 reinterpret_cast<XPointer>(this))) { 249 reinterpret_cast<XPointer>(this))) {
227 LOG(ERROR) << "XRecordEnableContextAsync failed."; 250 LOG(ERROR) << "XRecordEnableContextAsync failed.";
228 return; 251 return;
229 } 252 }
230 253
231 if (!x_record_range_[0] || !x_record_range_[1]) { 254 if (!x_record_range_[0] || !x_record_range_[1]) {
232 // Register OnFileCanReadWithoutBlocking() to be called every time there is 255 // Register OnFileCanReadWithoutBlocking() to be called every time there is
233 // something to read from |x_record_display_|. 256 // something to read from |x_record_display_|.
234 base::MessageLoopForIO* message_loop = base::MessageLoopForIO::current(); 257 base::MessageLoopForIO* message_loop = base::MessageLoopForIO::current();
235 int result = 258 int result =
236 message_loop->WatchFileDescriptor(ConnectionNumber(x_record_display_), 259 message_loop->WatchFileDescriptor(ConnectionNumber(x_record_display_),
237 true, 260 true,
238 base::MessageLoopForIO::WATCH_READ, 261 base::MessageLoopForIO::WATCH_READ,
239 &controller_, 262 &controller_,
240 this); 263 this);
241 if (!result) { 264 if (!result) {
242 LOG(ERROR) << "Failed to create X record task."; 265 LOG(ERROR) << "Failed to create X record task.";
243 return; 266 return;
244 } 267 }
245 } 268 }
246 269
247 // Fetch pending events if any. 270 // Fetch pending events if any.
248 OnFileCanReadWithoutBlocking(ConnectionNumber(x_record_display_)); 271 OnFileCanReadWithoutBlocking(ConnectionNumber(x_record_display_));
249 } 272 }
250 273
251 void UserInputMonitorLinux::StopMonitor(EventType type) { 274 void UserInputMonitorLinux::Core::StopMonitor(EventType type) {
252 DCHECK(io_task_runner_->BelongsToCurrentThread()); 275 if (!io_task_runner_->BelongsToCurrentThread()) {
276 io_task_runner_->PostTask(
277 FROM_HERE,
278 base::Bind(&Core::StopMonitor, weak_factory_.GetWeakPtr(), type));
279 return;
280 }
253 281
254 if (x_record_range_[type]) { 282 if (x_record_range_[type]) {
255 XFree(x_record_range_[type]); 283 XFree(x_record_range_[type]);
256 x_record_range_[type] = NULL; 284 x_record_range_[type] = NULL;
257 } 285 }
258 if (x_record_range_[0] || x_record_range_[1]) 286 if (x_record_range_[0] || x_record_range_[1])
259 return; 287 return;
260 288
261 // Context must be disabled via the control channel because we can't send 289 // Context must be disabled via the control channel because we can't send
262 // any X protocol traffic over the data channel while it's recording. 290 // any X protocol traffic over the data channel while it's recording.
263 if (x_record_context_) { 291 if (x_record_context_) {
264 XRecordDisableContext(display_, x_record_context_); 292 XRecordDisableContext(display_, x_record_context_);
265 XFlush(display_); 293 XFlush(display_);
266 XRecordFreeContext(x_record_display_, x_record_context_); 294 XRecordFreeContext(x_record_display_, x_record_context_);
267 x_record_context_ = 0; 295 x_record_context_ = 0;
268 296
269 controller_.StopWatchingFileDescriptor(); 297 controller_.StopWatchingFileDescriptor();
270 if (x_record_display_) { 298 if (x_record_display_) {
271 XCloseDisplay(x_record_display_); 299 XCloseDisplay(x_record_display_);
272 x_record_display_ = NULL; 300 x_record_display_ = NULL;
273 } 301 }
274 if (display_) { 302 if (display_) {
275 XCloseDisplay(display_); 303 XCloseDisplay(display_);
276 display_ = NULL; 304 display_ = NULL;
277 } 305 }
278 } 306 }
279 } 307 }
280 308
281 void UserInputMonitorLinux::OnFileCanReadWithoutBlocking(int fd) { 309 void UserInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) {
282 DCHECK(io_task_runner_->BelongsToCurrentThread()); 310 DCHECK(io_task_runner_->BelongsToCurrentThread());
283 XEvent event; 311 XEvent event;
284 // Fetch pending events if any. 312 // Fetch pending events if any.
285 while (XPending(x_record_display_)) { 313 while (XPending(x_record_display_)) {
286 XNextEvent(x_record_display_, &event); 314 XNextEvent(x_record_display_, &event);
287 } 315 }
288 } 316 }
289 317
290 void UserInputMonitorLinux::OnFileCanWriteWithoutBlocking(int fd) { 318 void UserInputMonitorLinux::Core::OnFileCanWriteWithoutBlocking(int fd) {
291 NOTREACHED(); 319 NOTREACHED();
292 } 320 }
293 321
294 void UserInputMonitorLinux::ProcessXEvent(xEvent* event) { 322 void UserInputMonitorLinux::Core::ProcessXEvent(xEvent* event) {
295 DCHECK(io_task_runner_->BelongsToCurrentThread()); 323 DCHECK(io_task_runner_->BelongsToCurrentThread());
296 if (event->u.u.type == MotionNotify) { 324 if (event->u.u.type == MotionNotify) {
297 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX, 325 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX,
298 event->u.keyButtonPointer.rootY)); 326 event->u.keyButtonPointer.rootY));
299 OnMouseEvent(position); 327 mouse_listeners_->Notify(&MouseEventListener::OnMouseMoved, position);
300 } else { 328 } else {
301 ui::EventType type; 329 ui::EventType type;
302 if (event->u.u.type == KeyPress) { 330 if (event->u.u.type == KeyPress) {
303 type = ui::ET_KEY_PRESSED; 331 type = ui::ET_KEY_PRESSED;
304 } else if (event->u.u.type == KeyRelease) { 332 } else if (event->u.u.type == KeyRelease) {
305 type = ui::ET_KEY_RELEASED; 333 type = ui::ET_KEY_RELEASED;
306 } else { 334 } else {
307 NOTREACHED(); 335 NOTREACHED();
308 return; 336 return;
309 } 337 }
310 338
311 KeySym key_sym = XkbKeycodeToKeysym(display_, event->u.u.detail, 0, 0); 339 KeySym key_sym = XkbKeycodeToKeysym(display_, event->u.u.detail, 0, 0);
312 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym); 340 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym);
313 counter_.OnKeyboardEvent(type, key_code); 341 counter_.OnKeyboardEvent(type, key_code);
314 } 342 }
315 } 343 }
316 344
317 // static 345 // static
318 void UserInputMonitorLinux::ProcessReply(XPointer self, 346 void UserInputMonitorLinux::Core::ProcessReply(XPointer self,
319 XRecordInterceptData* data) { 347 XRecordInterceptData* data) {
320 if (data->category == XRecordFromServer) { 348 if (data->category == XRecordFromServer) {
321 xEvent* event = reinterpret_cast<xEvent*>(data->data); 349 xEvent* event = reinterpret_cast<xEvent*>(data->data);
322 reinterpret_cast<UserInputMonitorLinux*>(self)->ProcessXEvent(event); 350 reinterpret_cast<Core*>(self)->ProcessXEvent(event);
323 } 351 }
324 XRecordFreeData(data); 352 XRecordFreeData(data);
325 } 353 }
326 354
355 //
356 // Implementation of UserInputMonitorLinux.
357 //
358
359 UserInputMonitorLinux::UserInputMonitorLinux(
DaleCurtis 2013/09/06 21:51:08 Blank line between each method.
jiayl 2013/09/06 22:43:30 Done.
360 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
361 : core_(new Core(io_task_runner)) {}
362 UserInputMonitorLinux::~UserInputMonitorLinux() {
363 if (!core_->io_task_runner()->DeleteSoon(FROM_HERE, core_))
364 delete core_;
365 }
366 void UserInputMonitorLinux::AddMouseListener(MouseEventListener* listener) {
367 core_->AddMouseListener(listener);
368 }
369 void UserInputMonitorLinux::RemoveMouseListener(MouseEventListener* listener) {
370 core_->RemoveMouseListener(listener);
371 }
372 size_t UserInputMonitorLinux::GetKeyPressCount() const {
373 return core_->GetKeyPressCount();
374 }
375 void UserInputMonitorLinux::StartKeyboardMonitoring() {
376 core_->StartKeyboardMonitoring();
377 }
378 void UserInputMonitorLinux::StopKeyboardMonitoring() {
379 core_->StopKeyboardMonitoring();
380 }
381
327 } // namespace 382 } // namespace
328 383
329 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( 384 scoped_ptr<UserInputMonitor> UserInputMonitor::Create(
330 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 385 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
331 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { 386 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) {
332 return scoped_ptr<UserInputMonitor>( 387 return scoped_ptr<UserInputMonitor>(
333 new UserInputMonitorLinux(io_task_runner)); 388 new UserInputMonitorLinux(io_task_runner));
334 } 389 }
335 390
336 } // namespace media 391 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698