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

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

Issue 2577573002: Removes mouse listeners from UserInputMonitor. (Closed)
Patch Set: Fix win by removing std::move. Created 3 years, 11 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 <stddef.h> 7 #include <stddef.h>
8 #include <sys/select.h> 8 #include <sys/select.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 #define XK_MISCELLANY 10 #define XK_MISCELLANY
(...skipping 23 matching lines...) Expand all
34 34
35 namespace media { 35 namespace media {
36 namespace { 36 namespace {
37 37
38 // This is the actual implementation of event monitoring. It's separated from 38 // This is the actual implementation of event monitoring. It's separated from
39 // UserInputMonitorLinux since it needs to be deleted on the IO thread. 39 // UserInputMonitorLinux since it needs to be deleted on the IO thread.
40 class UserInputMonitorLinuxCore 40 class UserInputMonitorLinuxCore
41 : public base::SupportsWeakPtr<UserInputMonitorLinuxCore>, 41 : public base::SupportsWeakPtr<UserInputMonitorLinuxCore>,
42 public base::MessageLoop::DestructionObserver { 42 public base::MessageLoop::DestructionObserver {
43 public: 43 public:
44 enum EventType {
45 MOUSE_EVENT,
46 KEYBOARD_EVENT
47 };
48
49 explicit UserInputMonitorLinuxCore( 44 explicit UserInputMonitorLinuxCore(
50 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 45 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
51 const scoped_refptr<UserInputMonitor::MouseListenerList>&
52 mouse_listeners);
53 ~UserInputMonitorLinuxCore() override; 46 ~UserInputMonitorLinuxCore() override;
54 47
55 // DestructionObserver overrides. 48 // DestructionObserver overrides.
56 void WillDestroyCurrentMessageLoop() override; 49 void WillDestroyCurrentMessageLoop() override;
57 50
58 size_t GetKeyPressCount() const; 51 size_t GetKeyPressCount() const;
59 void StartMonitor(EventType type); 52 void StartMonitor();
60 void StopMonitor(EventType type); 53 void StopMonitor();
61 54
62 private: 55 private:
63 void OnXEvent(); 56 void OnXEvent();
64 57
65 // Processes key and mouse events. 58 // Processes key events.
66 void ProcessXEvent(xEvent* event); 59 void ProcessXEvent(xEvent* event);
67 static void ProcessReply(XPointer self, XRecordInterceptData* data); 60 static void ProcessReply(XPointer self, XRecordInterceptData* data);
68 61
69 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 62 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
70 scoped_refptr<base::ObserverListThreadSafe<
71 UserInputMonitor::MouseEventListener>> mouse_listeners_;
72 63
73 // 64 //
74 // The following members should only be accessed on the IO thread. 65 // The following members should only be accessed on the IO thread.
75 // 66 //
76 std::unique_ptr<base::FileDescriptorWatcher::Controller> watch_controller_; 67 std::unique_ptr<base::FileDescriptorWatcher::Controller> watch_controller_;
77 Display* x_control_display_; 68 Display* x_control_display_;
78 Display* x_record_display_; 69 Display* x_record_display_;
79 XRecordRange* x_record_range_[2]; 70 XRecordRange* x_record_range_;
80 XRecordContext x_record_context_; 71 XRecordContext x_record_context_;
81 KeyboardEventCounter counter_; 72 KeyboardEventCounter counter_;
82 73
83 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinuxCore); 74 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinuxCore);
84 }; 75 };
85 76
86 class UserInputMonitorLinux : public UserInputMonitor { 77 class UserInputMonitorLinux : public UserInputMonitor {
87 public: 78 public:
88 explicit UserInputMonitorLinux( 79 explicit UserInputMonitorLinux(
89 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); 80 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
90 ~UserInputMonitorLinux() override; 81 ~UserInputMonitorLinux() override;
91 82
92 // Public UserInputMonitor overrides. 83 // Public UserInputMonitor overrides.
93 size_t GetKeyPressCount() const override; 84 size_t GetKeyPressCount() const override;
94 85
95 private: 86 private:
96 // Private UserInputMonitor overrides. 87 // Private UserInputMonitor overrides.
97 void StartKeyboardMonitoring() override; 88 void StartKeyboardMonitoring() override;
98 void StopKeyboardMonitoring() override; 89 void StopKeyboardMonitoring() override;
99 void StartMouseMonitoring() override;
100 void StopMouseMonitoring() override;
101 90
102 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 91 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
103 UserInputMonitorLinuxCore* core_; 92 UserInputMonitorLinuxCore* core_;
104 93
105 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux); 94 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorLinux);
106 }; 95 };
107 96
108 UserInputMonitorLinuxCore::UserInputMonitorLinuxCore( 97 UserInputMonitorLinuxCore::UserInputMonitorLinuxCore(
109 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 98 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
110 const scoped_refptr<UserInputMonitor::MouseListenerList>& mouse_listeners)
111 : io_task_runner_(io_task_runner), 99 : io_task_runner_(io_task_runner),
112 mouse_listeners_(mouse_listeners),
113 x_control_display_(NULL), 100 x_control_display_(NULL),
114 x_record_display_(NULL), 101 x_record_display_(NULL),
115 x_record_context_(0) { 102 x_record_range_(NULL),
116 x_record_range_[0] = NULL; 103 x_record_context_(0) {}
117 x_record_range_[1] = NULL;
118 }
119 104
120 UserInputMonitorLinuxCore::~UserInputMonitorLinuxCore() { 105 UserInputMonitorLinuxCore::~UserInputMonitorLinuxCore() {
121 DCHECK(!x_control_display_); 106 DCHECK(!x_control_display_);
122 DCHECK(!x_record_display_); 107 DCHECK(!x_record_display_);
123 DCHECK(!x_record_range_[0]); 108 DCHECK(!x_record_range_);
124 DCHECK(!x_record_range_[1]);
125 DCHECK(!x_record_context_); 109 DCHECK(!x_record_context_);
126 } 110 }
127 111
128 void UserInputMonitorLinuxCore::WillDestroyCurrentMessageLoop() { 112 void UserInputMonitorLinuxCore::WillDestroyCurrentMessageLoop() {
129 DCHECK(io_task_runner_->BelongsToCurrentThread()); 113 DCHECK(io_task_runner_->BelongsToCurrentThread());
130 StopMonitor(MOUSE_EVENT); 114 StopMonitor();
131 StopMonitor(KEYBOARD_EVENT); 115 StopMonitor();
132 } 116 }
133 117
134 size_t UserInputMonitorLinuxCore::GetKeyPressCount() const { 118 size_t UserInputMonitorLinuxCore::GetKeyPressCount() const {
135 return counter_.GetKeyPressCount(); 119 return counter_.GetKeyPressCount();
136 } 120 }
137 121
138 void UserInputMonitorLinuxCore::StartMonitor(EventType type) { 122 void UserInputMonitorLinuxCore::StartMonitor() {
139 DCHECK(io_task_runner_->BelongsToCurrentThread()); 123 DCHECK(io_task_runner_->BelongsToCurrentThread());
140 124
141 if (type == KEYBOARD_EVENT)
142 counter_.Reset();
143
144 // TODO(jamiewalch): We should pass the display in. At that point, since 125 // TODO(jamiewalch): We should pass the display in. At that point, since
145 // XRecord needs a private connection to the X Server for its data channel 126 // XRecord needs a private connection to the X Server for its data channel
146 // and both channels are used from a separate thread, we'll need to duplicate 127 // and both channels are used from a separate thread, we'll need to duplicate
147 // them with something like the following: 128 // them with something like the following:
148 // XOpenDisplay(DisplayString(display)); 129 // XOpenDisplay(DisplayString(display));
149 if (!x_control_display_) 130 if (!x_control_display_)
150 x_control_display_ = gfx::OpenNewXDisplay(); 131 x_control_display_ = gfx::OpenNewXDisplay();
151 132
152 if (!x_record_display_) 133 if (!x_record_display_)
153 x_record_display_ = gfx::OpenNewXDisplay(); 134 x_record_display_ = gfx::OpenNewXDisplay();
154 135
155 if (!x_control_display_ || !x_record_display_) { 136 if (!x_control_display_ || !x_record_display_) {
156 LOG(ERROR) << "Couldn't open X display"; 137 LOG(ERROR) << "Couldn't open X display";
157 StopMonitor(type); 138 StopMonitor();
158 return; 139 return;
159 } 140 }
160 141
161 int xr_opcode, xr_event, xr_error; 142 int xr_opcode, xr_event, xr_error;
162 if (!XQueryExtension( 143 if (!XQueryExtension(
163 x_control_display_, "RECORD", &xr_opcode, &xr_event, &xr_error)) { 144 x_control_display_, "RECORD", &xr_opcode, &xr_event, &xr_error)) {
164 LOG(ERROR) << "X Record extension not available."; 145 LOG(ERROR) << "X Record extension not available.";
165 StopMonitor(type); 146 StopMonitor();
166 return; 147 return;
167 } 148 }
168 149
169 if (!x_record_range_[type]) 150 if (!x_record_range_)
170 x_record_range_[type] = XRecordAllocRange(); 151 x_record_range_ = XRecordAllocRange();
171 152
172 if (!x_record_range_[type]) { 153 if (!x_record_range_) {
173 LOG(ERROR) << "XRecordAllocRange failed."; 154 LOG(ERROR) << "XRecordAllocRange failed.";
174 StopMonitor(type); 155 StopMonitor();
175 return; 156 return;
176 } 157 }
177 158
178 if (type == MOUSE_EVENT) { 159 x_record_range_->device_events.first = KeyPress;
179 x_record_range_[type]->device_events.first = MotionNotify; 160 x_record_range_->device_events.last = KeyRelease;
180 x_record_range_[type]->device_events.last = MotionNotify;
181 } else {
182 DCHECK_EQ(KEYBOARD_EVENT, type);
183 x_record_range_[type]->device_events.first = KeyPress;
184 x_record_range_[type]->device_events.last = KeyRelease;
185 }
186 161
187 if (x_record_context_) { 162 if (x_record_context_) {
188 XRecordDisableContext(x_control_display_, x_record_context_); 163 XRecordDisableContext(x_control_display_, x_record_context_);
189 XFlush(x_control_display_); 164 XFlush(x_control_display_);
190 XRecordFreeContext(x_record_display_, x_record_context_); 165 XRecordFreeContext(x_record_display_, x_record_context_);
191 x_record_context_ = 0; 166 x_record_context_ = 0;
192 } 167 }
193 XRecordRange** record_range_to_use = 168 int number_of_ranges = 1;
194 (x_record_range_[0] && x_record_range_[1]) ? x_record_range_
195 : &x_record_range_[type];
196 int number_of_ranges = (x_record_range_[0] && x_record_range_[1]) ? 2 : 1;
197 169
198 XRecordClientSpec client_spec = XRecordAllClients; 170 XRecordClientSpec client_spec = XRecordAllClients;
199 x_record_context_ = XRecordCreateContext(x_record_display_, 171 x_record_context_ =
200 0, 172 XRecordCreateContext(x_record_display_, 0, &client_spec, 1,
201 &client_spec, 173 &x_record_range_, number_of_ranges);
202 1,
203 record_range_to_use,
204 number_of_ranges);
205 if (!x_record_context_) { 174 if (!x_record_context_) {
206 LOG(ERROR) << "XRecordCreateContext failed."; 175 LOG(ERROR) << "XRecordCreateContext failed.";
207 StopMonitor(type); 176 StopMonitor();
208 return; 177 return;
209 } 178 }
210 179
211 if (!XRecordEnableContextAsync(x_record_display_, 180 if (!XRecordEnableContextAsync(x_record_display_,
212 x_record_context_, 181 x_record_context_,
213 &UserInputMonitorLinuxCore::ProcessReply, 182 &UserInputMonitorLinuxCore::ProcessReply,
214 reinterpret_cast<XPointer>(this))) { 183 reinterpret_cast<XPointer>(this))) {
215 LOG(ERROR) << "XRecordEnableContextAsync failed."; 184 LOG(ERROR) << "XRecordEnableContextAsync failed.";
216 StopMonitor(type); 185 StopMonitor();
217 return; 186 return;
218 } 187 }
219 188
220 if (!x_record_range_[0] || !x_record_range_[1]) { 189 // Register OnXEvent() to be called every time there is something to read
221 // Register OnXEvent() to be called every time there is something to read 190 // from |x_record_display_|.
222 // from |x_record_display_|. 191 watch_controller_ = base::FileDescriptorWatcher::WatchReadable(
223 watch_controller_ = base::FileDescriptorWatcher::WatchReadable( 192 ConnectionNumber(x_record_display_),
224 ConnectionNumber(x_record_display_), 193 base::Bind(&UserInputMonitorLinuxCore::OnXEvent, base::Unretained(this)));
225 base::Bind(&UserInputMonitorLinuxCore::OnXEvent,
226 base::Unretained(this)));
227 194
228 // Start observing message loop destruction if we start monitoring the first 195 // Start observing message loop destruction if we start monitoring the first
229 // event. 196 // event.
230 base::MessageLoop::current()->AddDestructionObserver(this); 197 base::MessageLoop::current()->AddDestructionObserver(this);
231 }
232 198
233 // Fetch pending events if any. 199 // Fetch pending events if any.
234 OnXEvent(); 200 OnXEvent();
235 } 201 }
236 202
237 void UserInputMonitorLinuxCore::StopMonitor(EventType type) { 203 void UserInputMonitorLinuxCore::StopMonitor() {
238 DCHECK(io_task_runner_->BelongsToCurrentThread()); 204 DCHECK(io_task_runner_->BelongsToCurrentThread());
239 205
240 if (x_record_range_[type]) { 206 if (x_record_range_) {
241 XFree(x_record_range_[type]); 207 XFree(x_record_range_);
242 x_record_range_[type] = NULL; 208 x_record_range_ = NULL;
243 } 209 }
244 if (x_record_range_[0] || x_record_range_[1]) 210 if (x_record_range_)
245 return; 211 return;
246 212
247 // Context must be disabled via the control channel because we can't send 213 // Context must be disabled via the control channel because we can't send
248 // any X protocol traffic over the data channel while it's recording. 214 // any X protocol traffic over the data channel while it's recording.
249 if (x_record_context_) { 215 if (x_record_context_) {
250 XRecordDisableContext(x_control_display_, x_record_context_); 216 XRecordDisableContext(x_control_display_, x_record_context_);
251 XFlush(x_control_display_); 217 XFlush(x_control_display_);
252 XRecordFreeContext(x_record_display_, x_record_context_); 218 XRecordFreeContext(x_record_display_, x_record_context_);
253 x_record_context_ = 0; 219 x_record_context_ = 0;
254 220
(...skipping 15 matching lines...) Expand all
270 DCHECK(io_task_runner_->BelongsToCurrentThread()); 236 DCHECK(io_task_runner_->BelongsToCurrentThread());
271 XEvent event; 237 XEvent event;
272 // Fetch pending events if any. 238 // Fetch pending events if any.
273 while (XPending(x_record_display_)) { 239 while (XPending(x_record_display_)) {
274 XNextEvent(x_record_display_, &event); 240 XNextEvent(x_record_display_, &event);
275 } 241 }
276 } 242 }
277 243
278 void UserInputMonitorLinuxCore::ProcessXEvent(xEvent* event) { 244 void UserInputMonitorLinuxCore::ProcessXEvent(xEvent* event) {
279 DCHECK(io_task_runner_->BelongsToCurrentThread()); 245 DCHECK(io_task_runner_->BelongsToCurrentThread());
280 if (event->u.u.type == MotionNotify) { 246 DCHECK(event->u.u.type == KeyRelease || event->u.u.type == KeyPress);
281 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX,
282 event->u.keyButtonPointer.rootY));
283 mouse_listeners_->Notify(
284 FROM_HERE, &UserInputMonitor::MouseEventListener::OnMouseMoved,
285 position);
286 } else {
287 ui::EventType type;
288 if (event->u.u.type == KeyPress) {
289 type = ui::ET_KEY_PRESSED;
290 } else if (event->u.u.type == KeyRelease) {
291 type = ui::ET_KEY_RELEASED;
292 } else {
293 NOTREACHED();
294 return;
295 }
296 247
297 KeySym key_sym = 248 ui::EventType type =
298 XkbKeycodeToKeysym(x_control_display_, event->u.u.detail, 0, 0); 249 (event->u.u.type == KeyPress) ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED;
299 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym); 250
300 counter_.OnKeyboardEvent(type, key_code); 251 KeySym key_sym =
301 } 252 XkbKeycodeToKeysym(x_control_display_, event->u.u.detail, 0, 0);
253 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym);
254 counter_.OnKeyboardEvent(type, key_code);
302 } 255 }
303 256
304 // static 257 // static
305 void UserInputMonitorLinuxCore::ProcessReply(XPointer self, 258 void UserInputMonitorLinuxCore::ProcessReply(XPointer self,
306 XRecordInterceptData* data) { 259 XRecordInterceptData* data) {
307 if (data->category == XRecordFromServer) { 260 if (data->category == XRecordFromServer) {
308 xEvent* event = reinterpret_cast<xEvent*>(data->data); 261 xEvent* event = reinterpret_cast<xEvent*>(data->data);
309 reinterpret_cast<UserInputMonitorLinuxCore*>(self)->ProcessXEvent(event); 262 reinterpret_cast<UserInputMonitorLinuxCore*>(self)->ProcessXEvent(event);
310 } 263 }
311 XRecordFreeData(data); 264 XRecordFreeData(data);
312 } 265 }
313 266
314 // 267 //
315 // Implementation of UserInputMonitorLinux. 268 // Implementation of UserInputMonitorLinux.
316 // 269 //
317 270
318 UserInputMonitorLinux::UserInputMonitorLinux( 271 UserInputMonitorLinux::UserInputMonitorLinux(
319 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) 272 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
320 : io_task_runner_(io_task_runner), 273 : io_task_runner_(io_task_runner),
321 core_(new UserInputMonitorLinuxCore(io_task_runner, mouse_listeners())) {} 274 core_(new UserInputMonitorLinuxCore(io_task_runner)) {}
322 275
323 UserInputMonitorLinux::~UserInputMonitorLinux() { 276 UserInputMonitorLinux::~UserInputMonitorLinux() {
324 if (!io_task_runner_->DeleteSoon(FROM_HERE, core_)) 277 if (!io_task_runner_->DeleteSoon(FROM_HERE, core_))
325 delete core_; 278 delete core_;
326 } 279 }
327 280
328 size_t UserInputMonitorLinux::GetKeyPressCount() const { 281 size_t UserInputMonitorLinux::GetKeyPressCount() const {
329 return core_->GetKeyPressCount(); 282 return core_->GetKeyPressCount();
330 } 283 }
331 284
332 void UserInputMonitorLinux::StartKeyboardMonitoring() { 285 void UserInputMonitorLinux::StartKeyboardMonitoring() {
333 io_task_runner_->PostTask( 286 io_task_runner_->PostTask(
334 FROM_HERE, 287 FROM_HERE,
335 base::Bind(&UserInputMonitorLinuxCore::StartMonitor, 288 base::Bind(&UserInputMonitorLinuxCore::StartMonitor, core_->AsWeakPtr()));
336 core_->AsWeakPtr(),
337 UserInputMonitorLinuxCore::KEYBOARD_EVENT));
338 } 289 }
339 290
340 void UserInputMonitorLinux::StopKeyboardMonitoring() { 291 void UserInputMonitorLinux::StopKeyboardMonitoring() {
341 io_task_runner_->PostTask( 292 io_task_runner_->PostTask(
342 FROM_HERE, 293 FROM_HERE,
343 base::Bind(&UserInputMonitorLinuxCore::StopMonitor, 294 base::Bind(&UserInputMonitorLinuxCore::StopMonitor, core_->AsWeakPtr()));
344 core_->AsWeakPtr(),
345 UserInputMonitorLinuxCore::KEYBOARD_EVENT));
346 }
347
348 void UserInputMonitorLinux::StartMouseMonitoring() {
349 io_task_runner_->PostTask(FROM_HERE,
350 base::Bind(&UserInputMonitorLinuxCore::StartMonitor,
351 core_->AsWeakPtr(),
352 UserInputMonitorLinuxCore::MOUSE_EVENT));
353 }
354
355 void UserInputMonitorLinux::StopMouseMonitoring() {
356 io_task_runner_->PostTask(FROM_HERE,
357 base::Bind(&UserInputMonitorLinuxCore::StopMonitor,
358 core_->AsWeakPtr(),
359 UserInputMonitorLinuxCore::MOUSE_EVENT));
360 } 295 }
361 296
362 } // namespace 297 } // namespace
363 298
364 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create( 299 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create(
365 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 300 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
366 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { 301 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) {
367 return base::WrapUnique(new UserInputMonitorLinux(io_task_runner)); 302 return base::MakeUnique<UserInputMonitorLinux>(io_task_runner);
368 } 303 }
369 304
370 } // namespace media 305 } // 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