OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/base/user_input_monitor.h" | |
6 | |
7 #include <sys/select.h> | |
8 #include <unistd.h> | |
9 #define XK_MISCELLANY | |
10 #include <X11/keysymdef.h> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/bind.h" | |
14 #include "base/callback.h" | |
15 #include "base/compiler_specific.h" | |
16 #include "base/location.h" | |
17 #include "base/logging.h" | |
18 #include "base/memory/weak_ptr.h" | |
19 #include "base/message_loop/message_loop.h" | |
20 #include "base/message_loop/message_pump_libevent.h" | |
21 #include "base/posix/eintr_wrapper.h" | |
22 #include "base/single_thread_task_runner.h" | |
23 #include "base/threading/non_thread_safe.h" | |
24 #include "third_party/skia/include/core/SkPoint.h" | |
25 #include "ui/base/keycodes/keyboard_code_conversion_x.h" | |
26 | |
27 // 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. | |
29 #include <X11/XKBlib.h> | |
30 #include <X11/Xlibint.h> | |
31 #include <X11/extensions/record.h> | |
32 | |
33 namespace media { | |
34 | |
35 namespace { | |
36 | |
37 class UserInputMonitorLinux : public base::NonThreadSafe, | |
38 public UserInputMonitor { | |
39 public: | |
40 UserInputMonitorLinux( | |
41 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); | |
42 virtual ~UserInputMonitorLinux(); | |
43 | |
44 private: | |
45 enum EventType { | |
46 MOUSE_EVENT = 0, | |
47 KEYBOARD_EVENT = 1 | |
48 }; | |
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; | |
91 virtual void StopMouseMonitoring() OVERRIDE; | |
92 virtual void StartKeyboardMonitoring() OVERRIDE; | |
93 virtual void StopKeyboardMonitoring() OVERRIDE; | |
94 | |
95 void OnMouseEvent(const SkIPoint& position); | |
96 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); | |
97 | |
98 // Task runner on which X Window events are received. | |
99 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
100 scoped_refptr<Core> core_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); | |
103 }; | |
104 | |
105 UserInputMonitorLinux::UserInputMonitorLinux( | |
106 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
107 : io_task_runner_(io_task_runner), | |
108 core_(new Core(base::Bind(&UserInputMonitorLinux::OnMouseEvent, | |
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), | |
148 x_record_context_(0), | |
149 mouse_callback_(mouse_callback), | |
150 keyboard_callback_(keyboard_callback) { | |
151 x_record_range_[0] = NULL; | |
152 x_record_range_[1] = NULL; | |
153 } | |
154 | |
155 UserInputMonitorLinux::Core::~Core() { | |
156 DCHECK(!display_); | |
157 DCHECK(!x_record_display_); | |
158 DCHECK(!x_record_range_[0]); | |
159 DCHECK(!x_record_range_[1]); | |
160 DCHECK(!x_record_context_); | |
161 } | |
162 | |
163 void UserInputMonitorLinux::Core::StartMonitor(EventType type) { | |
164 DCHECK(base::MessageLoopForIO::current()); | |
165 // 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 | |
167 // and both channels are used from a separate thread, we'll need to duplicate | |
168 // them with something like the following: | |
169 // XOpenDisplay(DisplayString(display)); | |
170 if (!display_) | |
171 display_ = XOpenDisplay(NULL); | |
172 | |
173 if (!x_record_display_) | |
174 x_record_display_ = XOpenDisplay(NULL); | |
175 | |
176 if (!display_ || !x_record_display_) { | |
177 LOG(ERROR) << "Couldn't open X display"; | |
178 return; | |
179 } | |
180 | |
181 int xr_opcode, xr_event, xr_error; | |
182 if (!XQueryExtension(display_, "RECORD", &xr_opcode, &xr_event, &xr_error)) { | |
183 LOG(ERROR) << "X Record extension not available."; | |
184 return; | |
185 } | |
186 | |
187 if (!x_record_range_[type]) | |
188 x_record_range_[type] = XRecordAllocRange(); | |
189 | |
190 if (!x_record_range_[type]) { | |
191 LOG(ERROR) << "XRecordAllocRange failed."; | |
192 return; | |
193 } | |
194 if (type == MOUSE_EVENT) { | |
DaleCurtis
2013/08/12 20:24:11
Add line above -- doesn't go with block above.
jiayl
2013/08/12 23:13:46
Done.
| |
195 x_record_range_[type]->device_events.first = MotionNotify; | |
196 x_record_range_[type]->device_events.last = MotionNotify; | |
197 } else { | |
198 DCHECK_EQ(KEYBOARD_EVENT, type); | |
199 x_record_range_[type]->device_events.first = KeyPress; | |
200 x_record_range_[type]->device_events.last = KeyRelease; | |
201 } | |
202 | |
203 if (x_record_context_) { | |
204 XRecordDisableContext(display_, x_record_context_); | |
205 XFlush(display_); | |
206 XRecordFreeContext(x_record_display_, x_record_context_); | |
207 x_record_context_ = 0; | |
208 } | |
209 XRecordRange** record_range_to_use = | |
210 (x_record_range_[0] && x_record_range_[1]) ? x_record_range_ | |
211 : &x_record_range_[type]; | |
212 int number_of_ranges = (x_record_range_[0] && x_record_range_[1]) ? 2 : 1; | |
213 | |
214 XRecordClientSpec client_spec = XRecordAllClients; | |
215 x_record_context_ = XRecordCreateContext(x_record_display_, | |
216 0, | |
217 &client_spec, | |
218 1, | |
219 record_range_to_use, | |
220 number_of_ranges); | |
221 if (!x_record_context_) { | |
222 LOG(ERROR) << "XRecordCreateContext failed."; | |
223 return; | |
224 } | |
225 | |
226 if (!XRecordEnableContextAsync(x_record_display_, | |
227 x_record_context_, | |
228 &Core::ProcessReply, | |
229 reinterpret_cast<XPointer>(this))) { | |
230 LOG(ERROR) << "XRecordEnableContextAsync failed."; | |
231 return; | |
232 } | |
233 | |
234 if (!x_record_range_[0] || !x_record_range_[1]) { | |
235 // Register OnFileCanReadWithoutBlocking() to be called every time there is | |
236 // something to read from |x_record_display_|. | |
237 base::MessageLoopForIO* message_loop = base::MessageLoopForIO::current(); | |
238 int result = | |
239 message_loop->WatchFileDescriptor(ConnectionNumber(x_record_display_), | |
240 true, | |
241 base::MessageLoopForIO::WATCH_READ, | |
242 &controller_, | |
243 this); | |
244 if (!result) { | |
245 LOG(ERROR) << "Failed to create X record task."; | |
246 return; | |
247 } | |
248 } | |
249 | |
250 // Fetch pending events if any. | |
251 while (XPending(x_record_display_)) { | |
252 XEvent ev; | |
DaleCurtis
2013/08/12 20:24:11
Is a new XEvent necessary to stack allocate every
jiayl
2013/08/12 23:13:46
Done.
| |
253 XNextEvent(x_record_display_, &ev); | |
254 } | |
255 } | |
256 | |
257 void UserInputMonitorLinux::Core::StopMonitor(EventType type) { | |
258 DCHECK(base::MessageLoopForIO::current()); | |
259 | |
260 if (x_record_range_[type]) { | |
261 XFree(x_record_range_[type]); | |
262 x_record_range_[type] = NULL; | |
263 } | |
264 if (x_record_range_[0] || x_record_range_[1]) | |
265 return; | |
266 | |
267 // Context must be disabled via the control channel because we can't send | |
268 // any X protocol traffic over the data channel while it's recording. | |
269 if (x_record_context_) { | |
270 XRecordDisableContext(display_, x_record_context_); | |
271 XFlush(display_); | |
272 XRecordFreeContext(x_record_display_, x_record_context_); | |
273 x_record_context_ = 0; | |
274 | |
275 controller_.StopWatchingFileDescriptor(); | |
276 if (x_record_display_) { | |
277 XCloseDisplay(x_record_display_); | |
278 x_record_display_ = NULL; | |
279 } | |
280 if (display_) { | |
281 XCloseDisplay(display_); | |
282 display_ = NULL; | |
283 } | |
284 } | |
285 } | |
286 | |
287 void UserInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) { | |
288 DCHECK(base::MessageLoopForIO::current()); | |
289 | |
290 // Fetch pending events if any. | |
291 while (XPending(x_record_display_)) { | |
292 XEvent ev; | |
DaleCurtis
2013/08/12 20:24:11
Ditto.
jiayl
2013/08/12 23:13:46
Done.
| |
293 XNextEvent(x_record_display_, &ev); | |
294 } | |
295 } | |
296 | |
297 void UserInputMonitorLinux::Core::OnFileCanWriteWithoutBlocking(int fd) { | |
298 NOTREACHED(); | |
299 } | |
300 | |
301 void UserInputMonitorLinux::Core::ProcessXEvent(xEvent* event) { | |
302 if (event->u.u.type == MotionNotify) { | |
303 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX, | |
304 event->u.keyButtonPointer.rootY)); | |
305 mouse_callback_.Run(position); | |
306 } else { | |
307 ui::EventType type; | |
308 if (event->u.u.type == KeyPress) { | |
309 type = ui::ET_KEY_PRESSED; | |
310 } else if (event->u.u.type == KeyRelease) { | |
311 type = ui::ET_KEY_RELEASED; | |
312 } else { | |
313 NOTREACHED(); | |
314 } | |
315 | |
316 KeySym key_sym = XkbKeycodeToKeysym(display_, event->u.u.detail, 0, 0); | |
317 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym); | |
318 keyboard_callback_.Run(type, key_code); | |
319 } | |
320 } | |
321 | |
322 // static | |
323 void UserInputMonitorLinux::Core::ProcessReply(XPointer self, | |
324 XRecordInterceptData* data) { | |
325 if (data->category == XRecordFromServer) { | |
326 xEvent* event = reinterpret_cast<xEvent*>(data->data); | |
327 reinterpret_cast<Core*>(self)->ProcessXEvent(event); | |
328 } | |
329 XRecordFreeData(data); | |
330 } | |
331 | |
332 } // namespace | |
333 | |
334 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( | |
335 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | |
336 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { | |
337 return scoped_ptr<UserInputMonitor>( | |
338 new UserInputMonitorLinux(io_task_runner)); | |
339 } | |
340 | |
341 } // namespace media | |
OLD | NEW |