| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "remoting/host/local_input_monitor.h" |
| 6 | 6 |
| 7 #include <sys/select.h> | 7 #include <sys/select.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #define XK_MISCELLANY | 11 #define XK_MISCELLANY |
| 12 #include <X11/keysymdef.h> | 12 #include <X11/keysymdef.h> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 17 #include "base/files/file_descriptor_watcher_posix.h" |
| 17 #include "base/location.h" | 18 #include "base/location.h" |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| 19 #include "base/macros.h" | 20 #include "base/macros.h" |
| 20 #include "base/message_loop/message_loop.h" | |
| 21 #include "base/message_loop/message_pump_libevent.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 "base/threading/non_thread_safe.h" |
| 24 #include "remoting/host/client_session_control.h" | 23 #include "remoting/host/client_session_control.h" |
| 25 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" | 24 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.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 remoting { | 32 namespace remoting { |
| 34 | 33 |
| 35 namespace { | 34 namespace { |
| 36 | 35 |
| 37 class LocalInputMonitorX11 : public base::NonThreadSafe, | 36 class LocalInputMonitorX11 : public base::NonThreadSafe, |
| 38 public LocalInputMonitor { | 37 public LocalInputMonitor { |
| 39 public: | 38 public: |
| 40 LocalInputMonitorX11( | 39 LocalInputMonitorX11( |
| 41 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 40 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 42 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, | 41 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 43 base::WeakPtr<ClientSessionControl> client_session_control); | 42 base::WeakPtr<ClientSessionControl> client_session_control); |
| 44 ~LocalInputMonitorX11() override; | 43 ~LocalInputMonitorX11() override; |
| 45 | 44 |
| 46 private: | 45 private: |
| 47 // The actual implementation resides in LocalInputMonitorX11::Core class. | 46 // The actual implementation resides in LocalInputMonitorX11::Core class. |
| 48 class Core | 47 class Core : public base::RefCountedThreadSafe<Core> { |
| 49 : public base::RefCountedThreadSafe<Core>, | |
| 50 public base::MessagePumpLibevent::Watcher { | |
| 51 public: | 48 public: |
| 52 Core(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 49 Core(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 53 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, | 50 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 54 base::WeakPtr<ClientSessionControl> client_session_control); | 51 base::WeakPtr<ClientSessionControl> client_session_control); |
| 55 | 52 |
| 56 void Start(); | 53 void Start(); |
| 57 void Stop(); | 54 void Stop(); |
| 58 | 55 |
| 59 private: | 56 private: |
| 60 friend class base::RefCountedThreadSafe<Core>; | 57 friend class base::RefCountedThreadSafe<Core>; |
| 61 ~Core() override; | 58 ~Core(); |
| 62 | 59 |
| 63 void StartOnInputThread(); | 60 void StartOnInputThread(); |
| 64 void StopOnInputThread(); | 61 void StopOnInputThread(); |
| 65 | 62 |
| 66 // base::MessagePumpLibevent::Watcher interface. | 63 // Called when there are pending X events. |
| 67 void OnFileCanReadWithoutBlocking(int fd) override; | 64 void OnPendingXEvents(); |
| 68 void OnFileCanWriteWithoutBlocking(int fd) override; | |
| 69 | 65 |
| 70 // Processes key and mouse events. | 66 // Processes key and mouse events. |
| 71 void ProcessXEvent(xEvent* event); | 67 void ProcessXEvent(xEvent* event); |
| 72 | 68 |
| 73 static void ProcessReply(XPointer self, XRecordInterceptData* data); | 69 static void ProcessReply(XPointer self, XRecordInterceptData* data); |
| 74 | 70 |
| 75 // Task runner on which public methods of this class must be called. | 71 // Task runner on which public methods of this class must be called. |
| 76 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | 72 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; |
| 77 | 73 |
| 78 // Task runner on which X Window events are received. | 74 // Task runner on which X Window events are received. |
| 79 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner_; | 75 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner_; |
| 80 | 76 |
| 81 // Points to the object receiving mouse event notifications and session | 77 // Points to the object receiving mouse event notifications and session |
| 82 // disconnect requests. | 78 // disconnect requests. |
| 83 base::WeakPtr<ClientSessionControl> client_session_control_; | 79 base::WeakPtr<ClientSessionControl> client_session_control_; |
| 84 | 80 |
| 85 // Used to receive base::MessagePumpLibevent::Watcher events. | 81 // Controls watching X events. |
| 86 base::MessagePumpLibevent::FileDescriptorWatcher controller_; | 82 std::unique_ptr<base::FileDescriptorWatcher::Controller> controller_; |
| 87 | 83 |
| 88 // True when Alt is pressed. | 84 // True when Alt is pressed. |
| 89 bool alt_pressed_; | 85 bool alt_pressed_; |
| 90 | 86 |
| 91 // True when Ctrl is pressed. | 87 // True when Ctrl is pressed. |
| 92 bool ctrl_pressed_; | 88 bool ctrl_pressed_; |
| 93 | 89 |
| 94 Display* display_; | 90 Display* display_; |
| 95 Display* x_record_display_; | 91 Display* x_record_display_; |
| 96 XRecordRange* x_record_range_[2]; | 92 XRecordRange* x_record_range_[2]; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 return; | 201 return; |
| 206 } | 202 } |
| 207 | 203 |
| 208 if (!XRecordEnableContextAsync(x_record_display_, x_record_context_, | 204 if (!XRecordEnableContextAsync(x_record_display_, x_record_context_, |
| 209 &Core::ProcessReply, | 205 &Core::ProcessReply, |
| 210 reinterpret_cast<XPointer>(this))) { | 206 reinterpret_cast<XPointer>(this))) { |
| 211 LOG(ERROR) << "XRecordEnableContextAsync failed."; | 207 LOG(ERROR) << "XRecordEnableContextAsync failed."; |
| 212 return; | 208 return; |
| 213 } | 209 } |
| 214 | 210 |
| 215 // Register OnFileCanReadWithoutBlocking() to be called every time there is | 211 // Register OnPendingXEvents() to be called every time there is |
| 216 // something to read from |x_record_display_|. | 212 // something to read from |x_record_display_|. |
| 217 base::MessageLoopForIO* message_loop = base::MessageLoopForIO::current(); | 213 controller_ = base::FileDescriptorWatcher::WatchReadable( |
| 218 int result = | 214 ConnectionNumber(x_record_display_), |
| 219 message_loop->WatchFileDescriptor(ConnectionNumber(x_record_display_), | 215 base::Bind(&Core::OnPendingXEvents, base::Unretained(this))); |
| 220 true, | |
| 221 base::MessageLoopForIO::WATCH_READ, | |
| 222 &controller_, | |
| 223 this); | |
| 224 if (!result) { | |
| 225 LOG(ERROR) << "Failed to create X record task."; | |
| 226 return; | |
| 227 } | |
| 228 | 216 |
| 229 // Fetch pending events if any. | 217 // Fetch pending events if any. |
| 230 while (XPending(x_record_display_)) { | 218 while (XPending(x_record_display_)) { |
| 231 XEvent ev; | 219 XEvent ev; |
| 232 XNextEvent(x_record_display_, &ev); | 220 XNextEvent(x_record_display_, &ev); |
| 233 } | 221 } |
| 234 } | 222 } |
| 235 | 223 |
| 236 void LocalInputMonitorX11::Core::StopOnInputThread() { | 224 void LocalInputMonitorX11::Core::StopOnInputThread() { |
| 237 DCHECK(input_task_runner_->BelongsToCurrentThread()); | 225 DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| 238 | 226 |
| 239 // Context must be disabled via the control channel because we can't send | 227 // Context must be disabled via the control channel because we can't send |
| 240 // any X protocol traffic over the data channel while it's recording. | 228 // any X protocol traffic over the data channel while it's recording. |
| 241 if (x_record_context_) { | 229 if (x_record_context_) { |
| 242 XRecordDisableContext(display_, x_record_context_); | 230 XRecordDisableContext(display_, x_record_context_); |
| 243 XFlush(display_); | 231 XFlush(display_); |
| 244 } | 232 } |
| 245 | 233 |
| 246 controller_.StopWatchingFileDescriptor(); | 234 controller_.reset(); |
| 247 | 235 |
| 248 if (x_record_range_[0]) { | 236 if (x_record_range_[0]) { |
| 249 XFree(x_record_range_[0]); | 237 XFree(x_record_range_[0]); |
| 250 x_record_range_[0] = nullptr; | 238 x_record_range_[0] = nullptr; |
| 251 } | 239 } |
| 252 if (x_record_range_[1]) { | 240 if (x_record_range_[1]) { |
| 253 XFree(x_record_range_[1]); | 241 XFree(x_record_range_[1]); |
| 254 x_record_range_[1] = nullptr; | 242 x_record_range_[1] = nullptr; |
| 255 } | 243 } |
| 256 if (x_record_context_) { | 244 if (x_record_context_) { |
| 257 XRecordFreeContext(x_record_display_, x_record_context_); | 245 XRecordFreeContext(x_record_display_, x_record_context_); |
| 258 x_record_context_ = 0; | 246 x_record_context_ = 0; |
| 259 } | 247 } |
| 260 if (x_record_display_) { | 248 if (x_record_display_) { |
| 261 XCloseDisplay(x_record_display_); | 249 XCloseDisplay(x_record_display_); |
| 262 x_record_display_ = nullptr; | 250 x_record_display_ = nullptr; |
| 263 } | 251 } |
| 264 if (display_) { | 252 if (display_) { |
| 265 XCloseDisplay(display_); | 253 XCloseDisplay(display_); |
| 266 display_ = nullptr; | 254 display_ = nullptr; |
| 267 } | 255 } |
| 268 } | 256 } |
| 269 | 257 |
| 270 void LocalInputMonitorX11::Core::OnFileCanReadWithoutBlocking(int fd) { | 258 void LocalInputMonitorX11::Core::OnPendingXEvents() { |
| 271 DCHECK(input_task_runner_->BelongsToCurrentThread()); | 259 DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| 272 | 260 |
| 273 // Fetch pending events if any. | 261 // Fetch pending events if any. |
| 274 while (XPending(x_record_display_)) { | 262 while (XPending(x_record_display_)) { |
| 275 XEvent ev; | 263 XEvent ev; |
| 276 XNextEvent(x_record_display_, &ev); | 264 XNextEvent(x_record_display_, &ev); |
| 277 } | 265 } |
| 278 } | 266 } |
| 279 | 267 |
| 280 void LocalInputMonitorX11::Core::OnFileCanWriteWithoutBlocking(int fd) { | |
| 281 NOTREACHED(); | |
| 282 } | |
| 283 | |
| 284 void LocalInputMonitorX11::Core::ProcessXEvent(xEvent* event) { | 268 void LocalInputMonitorX11::Core::ProcessXEvent(xEvent* event) { |
| 285 DCHECK(input_task_runner_->BelongsToCurrentThread()); | 269 DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| 286 | 270 |
| 287 if (event->u.u.type == MotionNotify) { | 271 if (event->u.u.type == MotionNotify) { |
| 288 webrtc::DesktopVector position(event->u.keyButtonPointer.rootX, | 272 webrtc::DesktopVector position(event->u.keyButtonPointer.rootX, |
| 289 event->u.keyButtonPointer.rootY); | 273 event->u.keyButtonPointer.rootY); |
| 290 caller_task_runner_->PostTask( | 274 caller_task_runner_->PostTask( |
| 291 FROM_HERE, base::Bind(&ClientSessionControl::OnLocalMouseMoved, | 275 FROM_HERE, base::Bind(&ClientSessionControl::OnLocalMouseMoved, |
| 292 client_session_control_, | 276 client_session_control_, |
| 293 position)); | 277 position)); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 322 std::unique_ptr<LocalInputMonitor> LocalInputMonitor::Create( | 306 std::unique_ptr<LocalInputMonitor> LocalInputMonitor::Create( |
| 323 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 307 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 324 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, | 308 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 325 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | 309 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 326 base::WeakPtr<ClientSessionControl> client_session_control) { | 310 base::WeakPtr<ClientSessionControl> client_session_control) { |
| 327 return base::WrapUnique(new LocalInputMonitorX11( | 311 return base::WrapUnique(new LocalInputMonitorX11( |
| 328 caller_task_runner, input_task_runner, client_session_control)); | 312 caller_task_runner, input_task_runner, client_session_control)); |
| 329 } | 313 } |
| 330 | 314 |
| 331 } // namespace remoting | 315 } // namespace remoting |
| OLD | NEW |