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

Side by Side Diff: media/base/user_input_monitor_linux.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, 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
« no previous file with comments | « media/base/user_input_monitor.cc ('k') | media/base/user_input_monitor_mac.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
19 #include "base/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
20 #include "base/message_loop/message_pump_libevent.h" 19 #include "base/message_loop/message_pump_libevent.h"
21 #include "base/posix/eintr_wrapper.h" 20 #include "base/posix/eintr_wrapper.h"
22 #include "base/single_thread_task_runner.h" 21 #include "base/single_thread_task_runner.h"
23 #include "base/threading/non_thread_safe.h" 22 #include "media/base/keyboard_event_counter.h"
24 #include "third_party/skia/include/core/SkPoint.h" 23 #include "third_party/skia/include/core/SkPoint.h"
25 #include "ui/base/keycodes/keyboard_code_conversion_x.h" 24 #include "ui/base/keycodes/keyboard_code_conversion_x.h"
26 25
27 // These includes need to be later than dictated by the style guide due to 26 // These includes need to be later than dictated by the style guide due to
28 // Xlib header pollution, specifically the min, max, and Status macros. 27 // Xlib header pollution, specifically the min, max, and Status macros.
29 #include <X11/XKBlib.h> 28 #include <X11/XKBlib.h>
30 #include <X11/Xlibint.h> 29 #include <X11/Xlibint.h>
31 #include <X11/extensions/record.h> 30 #include <X11/extensions/record.h>
32 31
33 namespace media { 32 namespace media {
34
35 namespace { 33 namespace {
36 34
37 class UserInputMonitorLinux : public base::NonThreadSafe, 35 class UserInputMonitorLinux : public UserInputMonitor ,
38 public UserInputMonitor { 36 public base::MessagePumpLibevent::Watcher {
39 public: 37 public:
40 UserInputMonitorLinux( 38 explicit UserInputMonitorLinux(
41 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); 39 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
42 virtual ~UserInputMonitorLinux(); 40 virtual ~UserInputMonitorLinux();
43 41
42 virtual size_t GetKeyPressCount() const OVERRIDE;
43
44 private: 44 private:
45 enum EventType { 45 enum EventType {
46 MOUSE_EVENT, 46 MOUSE_EVENT,
47 KEYBOARD_EVENT 47 KEYBOARD_EVENT
48 }; 48 };
49 49
50 // The actual implementation resides in UserInputMonitorLinux::Core class.
51 // Must be called on the io_task_runner thread.
52 class Core : public base::RefCountedThreadSafe<Core>,
53 public base::MessagePumpLibevent::Watcher {
54 public:
55 typedef const base::Callback<void(const SkIPoint&)> MouseCallback;
56 typedef base::Callback<void(ui::EventType event, ui::KeyboardCode key_code)>
57 KeyboardCallback;
58 Core(const MouseCallback& mouse_callback,
59 const KeyboardCallback& keyboard_callback);
60
61 void StartMonitor(EventType type);
62 void StopMonitor(EventType type);
63
64 private:
65 friend class base::RefCountedThreadSafe<Core>;
66 virtual ~Core();
67
68 // base::MessagePumpLibevent::Watcher interface.
69 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
70 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
71
72 // Processes key and mouse events.
73 void ProcessXEvent(xEvent* event);
74 static void ProcessReply(XPointer self, XRecordInterceptData* data);
75
76 // Used to receive base::MessagePumpLibevent::Watcher events.
77 base::MessagePumpLibevent::FileDescriptorWatcher controller_;
78
79 Display* display_;
80 Display* x_record_display_;
81 XRecordRange* x_record_range_[2];
82 XRecordContext x_record_context_;
83 base::Callback<void(const SkIPoint&)> mouse_callback_;
84 base::Callback<void(ui::EventType event, ui::KeyboardCode key_code)>
85 keyboard_callback_;
86
87 DISALLOW_COPY_AND_ASSIGN(Core);
88 };
89
90 virtual void StartMouseMonitoring() OVERRIDE; 50 virtual void StartMouseMonitoring() OVERRIDE;
91 virtual void StopMouseMonitoring() OVERRIDE; 51 virtual void StopMouseMonitoring() OVERRIDE;
92 virtual void StartKeyboardMonitoring() OVERRIDE; 52 virtual void StartKeyboardMonitoring() OVERRIDE;
93 virtual void StopKeyboardMonitoring() OVERRIDE; 53 virtual void StopKeyboardMonitoring() OVERRIDE;
94 54
95 void OnMouseEvent(const SkIPoint& position); 55 //
96 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); 56 // The following methods must be called on the IO thread.
57 //
58 void StartMonitor(EventType type);
59 void StopMonitor(EventType type);
60
61 // base::MessagePumpLibevent::Watcher interface.
62 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
63 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
64
65 // Processes key and mouse events.
66 void ProcessXEvent(xEvent* event);
67 static void ProcessReply(XPointer self, XRecordInterceptData* data);
97 68
98 // Task runner on which X Window events are received. 69 // Task runner on which X Window events are received.
99 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 70 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
100 scoped_refptr<Core> core_; 71
72 //
73 // The following members should only be accessed on the IO thread.
74 //
75 base::MessagePumpLibevent::FileDescriptorWatcher controller_;
76 Display* display_;
77 Display* x_record_display_;
78 XRecordRange* x_record_range_[2];
79 XRecordContext x_record_context_;
80 KeyboardEventCounter counter_;
101 81
102 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); 82 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux);
103 }; 83 };
104 84
105 UserInputMonitorLinux::UserInputMonitorLinux( 85 UserInputMonitorLinux::UserInputMonitorLinux(
106 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) 86 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
107 : io_task_runner_(io_task_runner), 87 : io_task_runner_(io_task_runner),
108 core_(new Core(base::Bind(&UserInputMonitorLinux::OnMouseEvent, 88 display_(NULL),
109 base::Unretained(this)),
110 base::Bind(&UserInputMonitorLinux::OnKeyboardEvent,
111 base::Unretained(this)))) {}
112
113 UserInputMonitorLinux::~UserInputMonitorLinux() {}
114
115 void UserInputMonitorLinux::StartMouseMonitoring() {
116 io_task_runner_->PostTask(
117 FROM_HERE, base::Bind(&Core::StartMonitor, core_.get(), MOUSE_EVENT));
118 }
119
120 void UserInputMonitorLinux::StopMouseMonitoring() {
121 io_task_runner_->PostTask(
122 FROM_HERE, base::Bind(&Core::StopMonitor, core_.get(), MOUSE_EVENT));
123 }
124
125 void UserInputMonitorLinux::StartKeyboardMonitoring() {
126 io_task_runner_->PostTask(
127 FROM_HERE, base::Bind(&Core::StartMonitor, core_.get(), KEYBOARD_EVENT));
128 }
129
130 void UserInputMonitorLinux::StopKeyboardMonitoring() {
131 io_task_runner_->PostTask(
132 FROM_HERE, base::Bind(&Core::StopMonitor, core_.get(), KEYBOARD_EVENT));
133 }
134
135 void UserInputMonitorLinux::OnMouseEvent(const SkIPoint& position) {
136 UserInputMonitor::OnMouseEvent(position);
137 }
138
139 void UserInputMonitorLinux::OnKeyboardEvent(ui::EventType event,
140 ui::KeyboardCode key_code) {
141 UserInputMonitor::OnKeyboardEvent(event, key_code);
142 }
143
144 UserInputMonitorLinux::Core::Core(const MouseCallback& mouse_callback,
145 const KeyboardCallback& keyboard_callback)
146 : display_(NULL),
147 x_record_display_(NULL), 89 x_record_display_(NULL),
148 x_record_context_(0), 90 x_record_context_(0) {
149 mouse_callback_(mouse_callback),
150 keyboard_callback_(keyboard_callback) {
151 x_record_range_[0] = NULL; 91 x_record_range_[0] = NULL;
152 x_record_range_[1] = NULL; 92 x_record_range_[1] = NULL;
153 } 93 }
154 94
155 UserInputMonitorLinux::Core::~Core() { 95 UserInputMonitorLinux::~UserInputMonitorLinux() {
156 DCHECK(!display_); 96 DCHECK(!display_);
157 DCHECK(!x_record_display_); 97 DCHECK(!x_record_display_);
158 DCHECK(!x_record_range_[0]); 98 DCHECK(!x_record_range_[0]);
159 DCHECK(!x_record_range_[1]); 99 DCHECK(!x_record_range_[1]);
160 DCHECK(!x_record_context_); 100 DCHECK(!x_record_context_);
161 } 101 }
162 102
163 void UserInputMonitorLinux::Core::StartMonitor(EventType type) { 103 size_t UserInputMonitorLinux::GetKeyPressCount() const {
164 DCHECK(base::MessageLoopForIO::current()); 104 return counter_.GetKeyPressCount();
105 }
106
107 void UserInputMonitorLinux::StartMouseMonitoring() {
108 if (!io_task_runner_->BelongsToCurrentThread()) {
109 io_task_runner_->PostTask(
110 FROM_HERE,
111 base::Bind(&UserInputMonitorLinux::StartMonitor,
112 base::Unretained(this),
113 MOUSE_EVENT));
114 return;
115 }
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
158 if (type == KEYBOARD_EVENT)
159 counter_.Reset();
160
165 // TODO(jamiewalch): We should pass the display in. At that point, since 161 // TODO(jamiewalch): We should pass the display in. At that point, since
166 // XRecord needs a private connection to the X Server for its data channel 162 // XRecord needs a private connection to the X Server for its data channel
167 // and both channels are used from a separate thread, we'll need to duplicate 163 // and both channels are used from a separate thread, we'll need to duplicate
168 // them with something like the following: 164 // them with something like the following:
169 // XOpenDisplay(DisplayString(display)); 165 // XOpenDisplay(DisplayString(display));
170 if (!display_) 166 if (!display_)
171 display_ = XOpenDisplay(NULL); 167 display_ = XOpenDisplay(NULL);
172 168
173 if (!x_record_display_) 169 if (!x_record_display_)
174 x_record_display_ = XOpenDisplay(NULL); 170 x_record_display_ = XOpenDisplay(NULL);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 1, 215 1,
220 record_range_to_use, 216 record_range_to_use,
221 number_of_ranges); 217 number_of_ranges);
222 if (!x_record_context_) { 218 if (!x_record_context_) {
223 LOG(ERROR) << "XRecordCreateContext failed."; 219 LOG(ERROR) << "XRecordCreateContext failed.";
224 return; 220 return;
225 } 221 }
226 222
227 if (!XRecordEnableContextAsync(x_record_display_, 223 if (!XRecordEnableContextAsync(x_record_display_,
228 x_record_context_, 224 x_record_context_,
229 &Core::ProcessReply, 225 &UserInputMonitorLinux::ProcessReply,
230 reinterpret_cast<XPointer>(this))) { 226 reinterpret_cast<XPointer>(this))) {
231 LOG(ERROR) << "XRecordEnableContextAsync failed."; 227 LOG(ERROR) << "XRecordEnableContextAsync failed.";
232 return; 228 return;
233 } 229 }
234 230
235 if (!x_record_range_[0] || !x_record_range_[1]) { 231 if (!x_record_range_[0] || !x_record_range_[1]) {
236 // Register OnFileCanReadWithoutBlocking() to be called every time there is 232 // Register OnFileCanReadWithoutBlocking() to be called every time there is
237 // something to read from |x_record_display_|. 233 // something to read from |x_record_display_|.
238 base::MessageLoopForIO* message_loop = base::MessageLoopForIO::current(); 234 base::MessageLoopForIO* message_loop = base::MessageLoopForIO::current();
239 int result = 235 int result =
240 message_loop->WatchFileDescriptor(ConnectionNumber(x_record_display_), 236 message_loop->WatchFileDescriptor(ConnectionNumber(x_record_display_),
241 true, 237 true,
242 base::MessageLoopForIO::WATCH_READ, 238 base::MessageLoopForIO::WATCH_READ,
243 &controller_, 239 &controller_,
244 this); 240 this);
245 if (!result) { 241 if (!result) {
246 LOG(ERROR) << "Failed to create X record task."; 242 LOG(ERROR) << "Failed to create X record task.";
247 return; 243 return;
248 } 244 }
249 } 245 }
250 246
251 // Fetch pending events if any. 247 // Fetch pending events if any.
252 OnFileCanReadWithoutBlocking(ConnectionNumber(x_record_display_)); 248 OnFileCanReadWithoutBlocking(ConnectionNumber(x_record_display_));
253 } 249 }
254 250
255 void UserInputMonitorLinux::Core::StopMonitor(EventType type) { 251 void UserInputMonitorLinux::StopMonitor(EventType type) {
256 DCHECK(base::MessageLoopForIO::current()); 252 DCHECK(io_task_runner_->BelongsToCurrentThread());
257 253
258 if (x_record_range_[type]) { 254 if (x_record_range_[type]) {
259 XFree(x_record_range_[type]); 255 XFree(x_record_range_[type]);
260 x_record_range_[type] = NULL; 256 x_record_range_[type] = NULL;
261 } 257 }
262 if (x_record_range_[0] || x_record_range_[1]) 258 if (x_record_range_[0] || x_record_range_[1])
263 return; 259 return;
264 260
265 // Context must be disabled via the control channel because we can't send 261 // Context must be disabled via the control channel because we can't send
266 // any X protocol traffic over the data channel while it's recording. 262 // any X protocol traffic over the data channel while it's recording.
267 if (x_record_context_) { 263 if (x_record_context_) {
268 XRecordDisableContext(display_, x_record_context_); 264 XRecordDisableContext(display_, x_record_context_);
269 XFlush(display_); 265 XFlush(display_);
270 XRecordFreeContext(x_record_display_, x_record_context_); 266 XRecordFreeContext(x_record_display_, x_record_context_);
271 x_record_context_ = 0; 267 x_record_context_ = 0;
272 268
273 controller_.StopWatchingFileDescriptor(); 269 controller_.StopWatchingFileDescriptor();
274 if (x_record_display_) { 270 if (x_record_display_) {
275 XCloseDisplay(x_record_display_); 271 XCloseDisplay(x_record_display_);
276 x_record_display_ = NULL; 272 x_record_display_ = NULL;
277 } 273 }
278 if (display_) { 274 if (display_) {
279 XCloseDisplay(display_); 275 XCloseDisplay(display_);
280 display_ = NULL; 276 display_ = NULL;
281 } 277 }
282 } 278 }
283 } 279 }
284 280
285 void UserInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) { 281 void UserInputMonitorLinux::OnFileCanReadWithoutBlocking(int fd) {
286 DCHECK(base::MessageLoopForIO::current()); 282 DCHECK(io_task_runner_->BelongsToCurrentThread());
287 XEvent event; 283 XEvent event;
288 // Fetch pending events if any. 284 // Fetch pending events if any.
289 while (XPending(x_record_display_)) { 285 while (XPending(x_record_display_)) {
290 XNextEvent(x_record_display_, &event); 286 XNextEvent(x_record_display_, &event);
291 } 287 }
292 } 288 }
293 289
294 void UserInputMonitorLinux::Core::OnFileCanWriteWithoutBlocking(int fd) { 290 void UserInputMonitorLinux::OnFileCanWriteWithoutBlocking(int fd) {
295 NOTREACHED(); 291 NOTREACHED();
296 } 292 }
297 293
298 void UserInputMonitorLinux::Core::ProcessXEvent(xEvent* event) { 294 void UserInputMonitorLinux::ProcessXEvent(xEvent* event) {
295 DCHECK(io_task_runner_->BelongsToCurrentThread());
299 if (event->u.u.type == MotionNotify) { 296 if (event->u.u.type == MotionNotify) {
300 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX, 297 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX,
301 event->u.keyButtonPointer.rootY)); 298 event->u.keyButtonPointer.rootY));
302 mouse_callback_.Run(position); 299 OnMouseEvent(position);
303 } else { 300 } else {
304 ui::EventType type; 301 ui::EventType type;
305 if (event->u.u.type == KeyPress) { 302 if (event->u.u.type == KeyPress) {
306 type = ui::ET_KEY_PRESSED; 303 type = ui::ET_KEY_PRESSED;
307 } else if (event->u.u.type == KeyRelease) { 304 } else if (event->u.u.type == KeyRelease) {
308 type = ui::ET_KEY_RELEASED; 305 type = ui::ET_KEY_RELEASED;
309 } else { 306 } else {
310 NOTREACHED(); 307 NOTREACHED();
308 return;
311 } 309 }
312 310
313 KeySym key_sym = XkbKeycodeToKeysym(display_, event->u.u.detail, 0, 0); 311 KeySym key_sym = XkbKeycodeToKeysym(display_, event->u.u.detail, 0, 0);
314 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym); 312 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym);
315 keyboard_callback_.Run(type, key_code); 313 counter_.OnKeyboardEvent(type, key_code);
316 } 314 }
317 } 315 }
318 316
319 // static 317 // static
320 void UserInputMonitorLinux::Core::ProcessReply(XPointer self, 318 void UserInputMonitorLinux::ProcessReply(XPointer self,
321 XRecordInterceptData* data) { 319 XRecordInterceptData* data) {
322 if (data->category == XRecordFromServer) { 320 if (data->category == XRecordFromServer) {
323 xEvent* event = reinterpret_cast<xEvent*>(data->data); 321 xEvent* event = reinterpret_cast<xEvent*>(data->data);
324 reinterpret_cast<Core*>(self)->ProcessXEvent(event); 322 reinterpret_cast<UserInputMonitorLinux*>(self)->ProcessXEvent(event);
325 } 323 }
326 XRecordFreeData(data); 324 XRecordFreeData(data);
327 } 325 }
328 326
329 } // namespace 327 } // namespace
330 328
331 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( 329 scoped_ptr<UserInputMonitor> UserInputMonitor::Create(
332 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 330 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
333 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { 331 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) {
334 return scoped_ptr<UserInputMonitor>( 332 return scoped_ptr<UserInputMonitor>(
335 new UserInputMonitorLinux(io_task_runner)); 333 new UserInputMonitorLinux(io_task_runner));
336 } 334 }
337 335
338 } // namespace media 336 } // namespace media
OLDNEW
« no previous file with comments | « media/base/user_input_monitor.cc ('k') | media/base/user_input_monitor_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698