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

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

Issue 21183002: Adding key press detection in the browser process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add linux impl 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 "remoting/host/local_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/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
19 #include "base/message_loop/message_pump_libevent.h" 19 #include "base/message_loop/message_pump_libevent.h"
20 #include "base/posix/eintr_wrapper.h" 20 #include "base/posix/eintr_wrapper.h"
21 #include "base/single_thread_task_runner.h" 21 #include "base/single_thread_task_runner.h"
22 #include "base/threading/non_thread_safe.h" 22 #include "base/threading/non_thread_safe.h"
23 #include "remoting/host/client_session_control.h"
24 #include "third_party/skia/include/core/SkPoint.h" 23 #include "third_party/skia/include/core/SkPoint.h"
24 #include "ui/base/keycodes/keyboard_code_conversion_x.h"
25 25
26 // 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
27 // Xlib header pollution, specifically the min, max, and Status macros. 27 // Xlib header pollution, specifically the min, max, and Status macros.
28 #include <X11/XKBlib.h> 28 #include <X11/XKBlib.h>
29 #include <X11/Xlibint.h> 29 #include <X11/Xlibint.h>
30 #include <X11/extensions/record.h> 30 #include <X11/extensions/record.h>
31 31
32 namespace remoting { 32 namespace media {
33 33
34 namespace { 34 namespace {
35 35
36 class LocalInputMonitorLinux : public base::NonThreadSafe, 36 class UserInputMonitorLinux : public base::NonThreadSafe,
DaleCurtis 2013/08/07 17:50:39 Instead of copying this code from LocalInputMonito
37 public LocalInputMonitor { 37 public UserInputMonitor {
38 public: 38 public:
39 LocalInputMonitorLinux( 39 UserInputMonitorLinux(
40 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
41 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, 40 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
42 base::WeakPtr<ClientSessionControl> client_session_control); 41 scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner,
43 virtual ~LocalInputMonitorLinux(); 42 uint8 events_of_interest,
43 base::WeakPtr<UserInputObserver> observer);
44 virtual ~UserInputMonitorLinux();
44 45
45 private: 46 private:
46 // The actual implementation resides in LocalInputMonitorLinux::Core class. 47 // The actual implementation resides in UserInputMonitorLinux::Core class.
47 class Core 48 class Core : public base::RefCountedThreadSafe<Core>,
48 : public base::RefCountedThreadSafe<Core>, 49 public base::MessagePumpLibevent::Watcher {
49 public base::MessagePumpLibevent::Watcher {
50 public: 50 public:
51 Core(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, 51 Core(scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
52 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, 52 scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner,
53 base::WeakPtr<ClientSessionControl> client_session_control); 53 uint8 events_of_interest,
54 base::WeakPtr<UserInputObserver> observer);
54 55
55 void Start(); 56 void Start();
56 void Stop(); 57 void Stop();
57 58
58 private: 59 private:
59 friend class base::RefCountedThreadSafe<Core>; 60 friend class base::RefCountedThreadSafe<Core>;
60 virtual ~Core(); 61 virtual ~Core();
61 62
62 void StartOnInputThread(); 63 void StartOnInputThread();
63 void StopOnInputThread(); 64 void StopOnInputThread();
64 65
65 // base::MessagePumpLibevent::Watcher interface. 66 // base::MessagePumpLibevent::Watcher interface.
66 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; 67 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
67 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; 68 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
68 69
69 // Processes key and mouse events. 70 // Processes key and mouse events.
70 void ProcessXEvent(xEvent* event); 71 void ProcessXEvent(xEvent* event);
71 72
72 static void ProcessReply(XPointer self, XRecordInterceptData* data); 73 static void ProcessReply(XPointer self, XRecordInterceptData* data);
73 74
74 // Task runner on which public methods of this class must be called.
75 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
76
77 // Task runner on which X Window events are received. 75 // Task runner on which X Window events are received.
78 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner_; 76 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner_;
79 77
80 // Points to the object receiving mouse event notifications and session 78 // Task runner on which public methods of this class must be called.
81 // disconnect requests. 79 scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner_;
82 base::WeakPtr<ClientSessionControl> client_session_control_; 80
81 // Points to the object receiving notifications.
82 base::WeakPtr<UserInputObserver> observer_;
83 83
84 // Used to receive base::MessagePumpLibevent::Watcher events. 84 // Used to receive base::MessagePumpLibevent::Watcher events.
85 base::MessagePumpLibevent::FileDescriptorWatcher controller_; 85 base::MessagePumpLibevent::FileDescriptorWatcher controller_;
86 86
87 // True when Alt is pressed.
88 bool alt_pressed_;
89
90 // True when Ctrl is pressed.
91 bool ctrl_pressed_;
92
93 Display* display_; 87 Display* display_;
94 Display* x_record_display_; 88 Display* x_record_display_;
95 XRecordRange* x_record_range_[2]; 89 XRecordRange* x_record_range_[2];
96 XRecordContext x_record_context_; 90 XRecordContext x_record_context_;
91 uint8 events_of_interest_;
97 92
98 DISALLOW_COPY_AND_ASSIGN(Core); 93 DISALLOW_COPY_AND_ASSIGN(Core);
99 }; 94 };
100 95
101 scoped_refptr<Core> core_; 96 scoped_refptr<Core> core_;
102 97
103 DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorLinux); 98 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux);
104 }; 99 };
105 100
106 LocalInputMonitorLinux::LocalInputMonitorLinux( 101 UserInputMonitorLinux::UserInputMonitorLinux(
107 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
108 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, 102 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
109 base::WeakPtr<ClientSessionControl> client_session_control) 103 scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner,
110 : core_(new Core(caller_task_runner, 104 uint8 events_of_interest,
111 input_task_runner, 105 base::WeakPtr<UserInputObserver> observer)
112 client_session_control)) { 106 : core_(new Core(input_task_runner,
107 observer_task_runner,
108 events_of_interest,
109 observer)) {
113 core_->Start(); 110 core_->Start();
114 } 111 }
115 112
116 LocalInputMonitorLinux::~LocalInputMonitorLinux() { 113 UserInputMonitorLinux::~UserInputMonitorLinux() { core_->Stop(); }
117 core_->Stop();
118 }
119 114
120 LocalInputMonitorLinux::Core::Core( 115 UserInputMonitorLinux::Core::Core(
121 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
122 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, 116 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
123 base::WeakPtr<ClientSessionControl> client_session_control) 117 scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner,
124 : caller_task_runner_(caller_task_runner), 118 uint8 events_of_interest,
125 input_task_runner_(input_task_runner), 119 base::WeakPtr<UserInputObserver> observer)
126 client_session_control_(client_session_control), 120 : input_task_runner_(input_task_runner),
127 alt_pressed_(false), 121 observer_task_runner_(observer_task_runner),
128 ctrl_pressed_(false), 122 observer_(observer),
129 display_(NULL), 123 display_(NULL),
130 x_record_display_(NULL), 124 x_record_display_(NULL),
131 x_record_context_(0) { 125 x_record_context_(0),
132 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 126 events_of_interest_(events_of_interest) {
133 DCHECK(client_session_control_.get()); 127 DCHECK(observer_.get());
134 128
135 x_record_range_[0] = NULL; 129 x_record_range_[0] = NULL;
136 x_record_range_[1] = NULL; 130 x_record_range_[1] = NULL;
137 } 131 }
138 132
139 void LocalInputMonitorLinux::Core::Start() { 133 void UserInputMonitorLinux::Core::Start() {
140 DCHECK(caller_task_runner_->BelongsToCurrentThread());
141
142 input_task_runner_->PostTask(FROM_HERE, 134 input_task_runner_->PostTask(FROM_HERE,
143 base::Bind(&Core::StartOnInputThread, this)); 135 base::Bind(&Core::StartOnInputThread, this));
144 } 136 }
145 137
146 void LocalInputMonitorLinux::Core::Stop() { 138 void UserInputMonitorLinux::Core::Stop() {
147 DCHECK(caller_task_runner_->BelongsToCurrentThread());
148
149 input_task_runner_->PostTask(FROM_HERE, 139 input_task_runner_->PostTask(FROM_HERE,
150 base::Bind(&Core::StopOnInputThread, this)); 140 base::Bind(&Core::StopOnInputThread, this));
151 } 141 }
152 142
153 LocalInputMonitorLinux::Core::~Core() { 143 UserInputMonitorLinux::Core::~Core() {
154 DCHECK(!display_); 144 DCHECK(!display_);
155 DCHECK(!x_record_display_); 145 DCHECK(!x_record_display_);
156 DCHECK(!x_record_range_[0]); 146 DCHECK(!x_record_range_[0]);
157 DCHECK(!x_record_range_[1]); 147 DCHECK(!x_record_range_[1]);
158 DCHECK(!x_record_context_); 148 DCHECK(!x_record_context_);
159 } 149 }
160 150
161 void LocalInputMonitorLinux::Core::StartOnInputThread() { 151 void UserInputMonitorLinux::Core::StartOnInputThread() {
162 DCHECK(input_task_runner_->BelongsToCurrentThread()); 152 DCHECK(input_task_runner_->BelongsToCurrentThread());
163 DCHECK(!display_); 153 DCHECK(!display_);
164 DCHECK(!x_record_display_); 154 DCHECK(!x_record_display_);
165 DCHECK(!x_record_range_[0]); 155 DCHECK(!x_record_range_[0]);
166 DCHECK(!x_record_range_[1]); 156 DCHECK(!x_record_range_[1]);
167 DCHECK(!x_record_context_); 157 DCHECK(!x_record_context_);
168 158
169 // TODO(jamiewalch): We should pass the display in. At that point, since 159 // TODO(jamiewalch): We should pass the display in. At that point, since
170 // XRecord needs a private connection to the X Server for its data channel 160 // XRecord needs a private connection to the X Server for its data channel
171 // and both channels are used from a separate thread, we'll need to duplicate 161 // and both channels are used from a separate thread, we'll need to duplicate
(...skipping 11 matching lines...) Expand all
183 LOG(ERROR) << "X Record extension not available."; 173 LOG(ERROR) << "X Record extension not available.";
184 return; 174 return;
185 } 175 }
186 176
187 x_record_range_[0] = XRecordAllocRange(); 177 x_record_range_[0] = XRecordAllocRange();
188 x_record_range_[1] = XRecordAllocRange(); 178 x_record_range_[1] = XRecordAllocRange();
189 if (!x_record_range_[0] || !x_record_range_[1]) { 179 if (!x_record_range_[0] || !x_record_range_[1]) {
190 LOG(ERROR) << "XRecordAllocRange failed."; 180 LOG(ERROR) << "XRecordAllocRange failed.";
191 return; 181 return;
192 } 182 }
193 x_record_range_[0]->device_events.first = MotionNotify; 183 if (events_of_interest_ & MOUSE_MOVEMENT) {
194 x_record_range_[0]->device_events.last = MotionNotify; 184 x_record_range_[0]->device_events.first = MotionNotify;
195 x_record_range_[1]->device_events.first = KeyPress; 185 x_record_range_[0]->device_events.last = MotionNotify;
196 x_record_range_[1]->device_events.last = KeyRelease; 186 }
187 if (events_of_interest_ & KEYBOARD_EVENT) {
188 x_record_range_[1]->device_events.first = KeyPress;
189 x_record_range_[1]->device_events.last = KeyRelease;
190 }
191
197 XRecordClientSpec client_spec = XRecordAllClients; 192 XRecordClientSpec client_spec = XRecordAllClients;
198 193 x_record_context_ = XRecordCreateContext(x_record_display_,
199 x_record_context_ = XRecordCreateContext( 194 0,
200 x_record_display_, 0, &client_spec, 1, x_record_range_, 195 &client_spec,
201 arraysize(x_record_range_)); 196 1,
197 x_record_range_,
198 arraysize(x_record_range_));
202 if (!x_record_context_) { 199 if (!x_record_context_) {
203 LOG(ERROR) << "XRecordCreateContext failed."; 200 LOG(ERROR) << "XRecordCreateContext failed.";
204 return; 201 return;
205 } 202 }
206 203
207 if (!XRecordEnableContextAsync(x_record_display_, x_record_context_, 204 if (!XRecordEnableContextAsync(x_record_display_,
205 x_record_context_,
208 &Core::ProcessReply, 206 &Core::ProcessReply,
209 reinterpret_cast<XPointer>(this))) { 207 reinterpret_cast<XPointer>(this))) {
210 LOG(ERROR) << "XRecordEnableContextAsync failed."; 208 LOG(ERROR) << "XRecordEnableContextAsync failed.";
211 return; 209 return;
212 } 210 }
213 211
214 // Register OnFileCanReadWithoutBlocking() to be called every time there is 212 // Register OnFileCanReadWithoutBlocking() to be called every time there is
215 // something to read from |x_record_display_|. 213 // something to read from |x_record_display_|.
216 base::MessageLoopForIO* message_loop = base::MessageLoopForIO::current(); 214 base::MessageLoopForIO* message_loop = base::MessageLoopForIO::current();
217 int result = 215 int result =
218 message_loop->WatchFileDescriptor(ConnectionNumber(x_record_display_), 216 message_loop->WatchFileDescriptor(ConnectionNumber(x_record_display_),
219 true, 217 true,
220 base::MessageLoopForIO::WATCH_READ, 218 base::MessageLoopForIO::WATCH_READ,
221 &controller_, 219 &controller_,
222 this); 220 this);
223 if (!result) { 221 if (!result) {
224 LOG(ERROR) << "Failed to create X record task."; 222 LOG(ERROR) << "Failed to create X record task.";
225 return; 223 return;
226 } 224 }
227 225
228 // Fetch pending events if any. 226 // Fetch pending events if any.
229 while (XPending(x_record_display_)) { 227 while (XPending(x_record_display_)) {
230 XEvent ev; 228 XEvent ev;
231 XNextEvent(x_record_display_, &ev); 229 XNextEvent(x_record_display_, &ev);
232 } 230 }
233 } 231 }
234 232
235 void LocalInputMonitorLinux::Core::StopOnInputThread() { 233 void UserInputMonitorLinux::Core::StopOnInputThread() {
236 DCHECK(input_task_runner_->BelongsToCurrentThread()); 234 DCHECK(input_task_runner_->BelongsToCurrentThread());
237 235
238 // Context must be disabled via the control channel because we can't send 236 // Context must be disabled via the control channel because we can't send
239 // any X protocol traffic over the data channel while it's recording. 237 // any X protocol traffic over the data channel while it's recording.
240 if (x_record_context_) { 238 if (x_record_context_) {
241 XRecordDisableContext(display_, x_record_context_); 239 XRecordDisableContext(display_, x_record_context_);
242 XFlush(display_); 240 XFlush(display_);
243 } 241 }
244 242
245 controller_.StopWatchingFileDescriptor(); 243 controller_.StopWatchingFileDescriptor();
(...skipping 13 matching lines...) Expand all
259 if (x_record_display_) { 257 if (x_record_display_) {
260 XCloseDisplay(x_record_display_); 258 XCloseDisplay(x_record_display_);
261 x_record_display_ = NULL; 259 x_record_display_ = NULL;
262 } 260 }
263 if (display_) { 261 if (display_) {
264 XCloseDisplay(display_); 262 XCloseDisplay(display_);
265 display_ = NULL; 263 display_ = NULL;
266 } 264 }
267 } 265 }
268 266
269 void LocalInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) { 267 void UserInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) {
270 DCHECK(input_task_runner_->BelongsToCurrentThread()); 268 DCHECK(input_task_runner_->BelongsToCurrentThread());
271 269
272 // Fetch pending events if any. 270 // Fetch pending events if any.
273 while (XPending(x_record_display_)) { 271 while (XPending(x_record_display_)) {
274 XEvent ev; 272 XEvent ev;
275 XNextEvent(x_record_display_, &ev); 273 XNextEvent(x_record_display_, &ev);
276 } 274 }
277 } 275 }
278 276
279 void LocalInputMonitorLinux::Core::OnFileCanWriteWithoutBlocking(int fd) { 277 void UserInputMonitorLinux::Core::OnFileCanWriteWithoutBlocking(int fd) {
280 NOTREACHED(); 278 NOTREACHED();
281 } 279 }
282 280
283 void LocalInputMonitorLinux::Core::ProcessXEvent(xEvent* event) { 281 void UserInputMonitorLinux::Core::ProcessXEvent(xEvent* event) {
284 DCHECK(input_task_runner_->BelongsToCurrentThread()); 282 DCHECK(input_task_runner_->BelongsToCurrentThread());
285 283
286 if (event->u.u.type == MotionNotify) { 284 if (event->u.u.type == MotionNotify) {
287 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX, 285 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX,
288 event->u.keyButtonPointer.rootY)); 286 event->u.keyButtonPointer.rootY));
289 caller_task_runner_->PostTask( 287 observer_task_runner_->PostTask(
290 FROM_HERE, base::Bind(&ClientSessionControl::OnLocalMouseMoved, 288 FROM_HERE,
291 client_session_control_, 289 base::Bind(&UserInputObserver::OnMouseMoved, observer_, position));
292 position));
293 } else { 290 } else {
294 int key_code = event->u.u.detail; 291 ui::EventType type;
295 bool down = event->u.u.type == KeyPress; 292 if (event->u.u.type == KeyPress)
296 KeySym key_sym = XkbKeycodeToKeysym(display_, key_code, 0, 0); 293 type = ui::ET_KEY_PRESSED;
297 if (key_sym == XK_Control_L || key_sym == XK_Control_R) { 294 else if (event->u.u.type == KeyRelease)
298 ctrl_pressed_ = down; 295 type = ui::ET_KEY_RELEASED;
299 } else if (key_sym == XK_Alt_L || key_sym == XK_Alt_R) { 296 else
300 alt_pressed_ = down; 297 return;
301 } else if (key_sym == XK_Escape && down && alt_pressed_ && ctrl_pressed_) { 298 KeySym key_sym = XkbKeycodeToKeysym(display_, event->u.u.detail, 0, 0);
302 caller_task_runner_->PostTask( 299 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym);
303 FROM_HERE, base::Bind(&ClientSessionControl::DisconnectSession, 300 observer_task_runner_->PostTask(
304 client_session_control_)); 301 FROM_HERE,
305 } 302 base::Bind(
303 &UserInputObserver::OnKeyboardEvent, observer_, type, key_code));
306 } 304 }
307 } 305 }
308 306
309 // static 307 // static
310 void LocalInputMonitorLinux::Core::ProcessReply(XPointer self, 308 void UserInputMonitorLinux::Core::ProcessReply(XPointer self,
311 XRecordInterceptData* data) { 309 XRecordInterceptData* data) {
312 if (data->category == XRecordFromServer) { 310 if (data->category == XRecordFromServer) {
313 xEvent* event = reinterpret_cast<xEvent*>(data->data); 311 xEvent* event = reinterpret_cast<xEvent*>(data->data);
314 reinterpret_cast<Core*>(self)->ProcessXEvent(event); 312 reinterpret_cast<Core*>(self)->ProcessXEvent(event);
315 } 313 }
316 XRecordFreeData(data); 314 XRecordFreeData(data);
317 } 315 }
318 316
319 } // namespace 317 } // namespace
320 318
321 scoped_ptr<LocalInputMonitor> LocalInputMonitor::Create( 319 scoped_ptr<UserInputMonitor> UserInputMonitor::Create(
322 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
323 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, 320 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
324 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, 321 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
325 base::WeakPtr<ClientSessionControl> client_session_control) { 322 scoped_refptr<base::SingleThreadTaskRunner> observer_task_runner,
326 return scoped_ptr<LocalInputMonitor>( 323 uint8 events_of_interest,
327 new LocalInputMonitorLinux(caller_task_runner, 324 base::WeakPtr<UserInputObserver> observer) {
328 input_task_runner, 325 return scoped_ptr<UserInputMonitor>(new UserInputMonitorLinux(
329 client_session_control)); 326 input_task_runner, observer_task_runner, events_of_interest, observer));
330 } 327 }
331 328
332 } // namespace remoting 329 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698