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, |
| 47 KEYBOARD_EVENT |
| 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 |
| 195 if (type == MOUSE_EVENT) { |
| 196 x_record_range_[type]->device_events.first = MotionNotify; |
| 197 x_record_range_[type]->device_events.last = MotionNotify; |
| 198 } else { |
| 199 DCHECK_EQ(KEYBOARD_EVENT, type); |
| 200 x_record_range_[type]->device_events.first = KeyPress; |
| 201 x_record_range_[type]->device_events.last = KeyRelease; |
| 202 } |
| 203 |
| 204 if (x_record_context_) { |
| 205 XRecordDisableContext(display_, x_record_context_); |
| 206 XFlush(display_); |
| 207 XRecordFreeContext(x_record_display_, x_record_context_); |
| 208 x_record_context_ = 0; |
| 209 } |
| 210 XRecordRange** record_range_to_use = |
| 211 (x_record_range_[0] && x_record_range_[1]) ? x_record_range_ |
| 212 : &x_record_range_[type]; |
| 213 int number_of_ranges = (x_record_range_[0] && x_record_range_[1]) ? 2 : 1; |
| 214 |
| 215 XRecordClientSpec client_spec = XRecordAllClients; |
| 216 x_record_context_ = XRecordCreateContext(x_record_display_, |
| 217 0, |
| 218 &client_spec, |
| 219 1, |
| 220 record_range_to_use, |
| 221 number_of_ranges); |
| 222 if (!x_record_context_) { |
| 223 LOG(ERROR) << "XRecordCreateContext failed."; |
| 224 return; |
| 225 } |
| 226 |
| 227 if (!XRecordEnableContextAsync(x_record_display_, |
| 228 x_record_context_, |
| 229 &Core::ProcessReply, |
| 230 reinterpret_cast<XPointer>(this))) { |
| 231 LOG(ERROR) << "XRecordEnableContextAsync failed."; |
| 232 return; |
| 233 } |
| 234 |
| 235 if (!x_record_range_[0] || !x_record_range_[1]) { |
| 236 // Register OnFileCanReadWithoutBlocking() to be called every time there is |
| 237 // something to read from |x_record_display_|. |
| 238 base::MessageLoopForIO* message_loop = base::MessageLoopForIO::current(); |
| 239 int result = |
| 240 message_loop->WatchFileDescriptor(ConnectionNumber(x_record_display_), |
| 241 true, |
| 242 base::MessageLoopForIO::WATCH_READ, |
| 243 &controller_, |
| 244 this); |
| 245 if (!result) { |
| 246 LOG(ERROR) << "Failed to create X record task."; |
| 247 return; |
| 248 } |
| 249 } |
| 250 |
| 251 // Fetch pending events if any. |
| 252 OnFileCanReadWithoutBlocking(ConnectionNumber(x_record_display_)); |
| 253 } |
| 254 |
| 255 void UserInputMonitorLinux::Core::StopMonitor(EventType type) { |
| 256 DCHECK(base::MessageLoopForIO::current()); |
| 257 |
| 258 if (x_record_range_[type]) { |
| 259 XFree(x_record_range_[type]); |
| 260 x_record_range_[type] = NULL; |
| 261 } |
| 262 if (x_record_range_[0] || x_record_range_[1]) |
| 263 return; |
| 264 |
| 265 // 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. |
| 267 if (x_record_context_) { |
| 268 XRecordDisableContext(display_, x_record_context_); |
| 269 XFlush(display_); |
| 270 XRecordFreeContext(x_record_display_, x_record_context_); |
| 271 x_record_context_ = 0; |
| 272 |
| 273 controller_.StopWatchingFileDescriptor(); |
| 274 if (x_record_display_) { |
| 275 XCloseDisplay(x_record_display_); |
| 276 x_record_display_ = NULL; |
| 277 } |
| 278 if (display_) { |
| 279 XCloseDisplay(display_); |
| 280 display_ = NULL; |
| 281 } |
| 282 } |
| 283 } |
| 284 |
| 285 void UserInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) { |
| 286 DCHECK(base::MessageLoopForIO::current()); |
| 287 XEvent event; |
| 288 // Fetch pending events if any. |
| 289 while (XPending(x_record_display_)) { |
| 290 XNextEvent(x_record_display_, &event); |
| 291 } |
| 292 } |
| 293 |
| 294 void UserInputMonitorLinux::Core::OnFileCanWriteWithoutBlocking(int fd) { |
| 295 NOTREACHED(); |
| 296 } |
| 297 |
| 298 void UserInputMonitorLinux::Core::ProcessXEvent(xEvent* event) { |
| 299 if (event->u.u.type == MotionNotify) { |
| 300 SkIPoint position(SkIPoint::Make(event->u.keyButtonPointer.rootX, |
| 301 event->u.keyButtonPointer.rootY)); |
| 302 mouse_callback_.Run(position); |
| 303 } else { |
| 304 ui::EventType type; |
| 305 if (event->u.u.type == KeyPress) { |
| 306 type = ui::ET_KEY_PRESSED; |
| 307 } else if (event->u.u.type == KeyRelease) { |
| 308 type = ui::ET_KEY_RELEASED; |
| 309 } else { |
| 310 NOTREACHED(); |
| 311 } |
| 312 |
| 313 KeySym key_sym = XkbKeycodeToKeysym(display_, event->u.u.detail, 0, 0); |
| 314 ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(key_sym); |
| 315 keyboard_callback_.Run(type, key_code); |
| 316 } |
| 317 } |
| 318 |
| 319 // static |
| 320 void UserInputMonitorLinux::Core::ProcessReply(XPointer self, |
| 321 XRecordInterceptData* data) { |
| 322 if (data->category == XRecordFromServer) { |
| 323 xEvent* event = reinterpret_cast<xEvent*>(data->data); |
| 324 reinterpret_cast<Core*>(self)->ProcessXEvent(event); |
| 325 } |
| 326 XRecordFreeData(data); |
| 327 } |
| 328 |
| 329 } // namespace |
| 330 |
| 331 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( |
| 332 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 333 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { |
| 334 return scoped_ptr<UserInputMonitor>( |
| 335 new UserInputMonitorLinux(io_task_runner)); |
| 336 } |
| 337 |
| 338 } // namespace media |
OLD | NEW |