| 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 #define XK_MISCELLANY | 9 #define XK_MISCELLANY |
| 10 #include <X11/keysymdef.h> | 10 #include <X11/keysymdef.h> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // These includes need to be later than dictated by the style guide due to | 25 // These includes need to be later than dictated by the style guide due to |
| 26 // Xlib header pollution, specifically the min, max, and Status macros. | 26 // Xlib header pollution, specifically the min, max, and Status macros. |
| 27 #include <X11/XKBlib.h> | 27 #include <X11/XKBlib.h> |
| 28 #include <X11/Xlibint.h> | 28 #include <X11/Xlibint.h> |
| 29 #include <X11/extensions/record.h> | 29 #include <X11/extensions/record.h> |
| 30 | 30 |
| 31 namespace remoting { | 31 namespace remoting { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 class LocalInputMonitorX11 : public base::NonThreadSafe, | 35 class LocalInputMonitorLinux : public base::NonThreadSafe, |
| 36 public LocalInputMonitor { | 36 public LocalInputMonitor { |
| 37 public: | 37 public: |
| 38 LocalInputMonitorX11( | 38 LocalInputMonitorLinux( |
| 39 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 39 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 40 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, | 40 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 41 base::WeakPtr<ClientSessionControl> client_session_control); | 41 base::WeakPtr<ClientSessionControl> client_session_control); |
| 42 ~LocalInputMonitorX11() override; | 42 ~LocalInputMonitorLinux() override; |
| 43 | 43 |
| 44 private: | 44 private: |
| 45 // The actual implementation resides in LocalInputMonitorX11::Core class. | 45 // The actual implementation resides in LocalInputMonitorLinux::Core class. |
| 46 class Core | 46 class Core |
| 47 : public base::RefCountedThreadSafe<Core>, | 47 : public base::RefCountedThreadSafe<Core>, |
| 48 public base::MessagePumpLibevent::Watcher { | 48 public base::MessagePumpLibevent::Watcher { |
| 49 public: | 49 public: |
| 50 Core(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 50 Core(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 51 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, | 51 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 52 base::WeakPtr<ClientSessionControl> client_session_control); | 52 base::WeakPtr<ClientSessionControl> client_session_control); |
| 53 | 53 |
| 54 void Start(); | 54 void Start(); |
| 55 void Stop(); | 55 void Stop(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 Display* display_; | 92 Display* display_; |
| 93 Display* x_record_display_; | 93 Display* x_record_display_; |
| 94 XRecordRange* x_record_range_[2]; | 94 XRecordRange* x_record_range_[2]; |
| 95 XRecordContext x_record_context_; | 95 XRecordContext x_record_context_; |
| 96 | 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(Core); | 97 DISALLOW_COPY_AND_ASSIGN(Core); |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 scoped_refptr<Core> core_; | 100 scoped_refptr<Core> core_; |
| 101 | 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorX11); | 102 DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorLinux); |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 LocalInputMonitorX11::LocalInputMonitorX11( | 105 LocalInputMonitorLinux::LocalInputMonitorLinux( |
| 106 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 106 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 107 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, | 107 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 108 base::WeakPtr<ClientSessionControl> client_session_control) | 108 base::WeakPtr<ClientSessionControl> client_session_control) |
| 109 : core_(new Core(caller_task_runner, | 109 : core_(new Core(caller_task_runner, |
| 110 input_task_runner, | 110 input_task_runner, |
| 111 client_session_control)) { | 111 client_session_control)) { |
| 112 core_->Start(); | 112 core_->Start(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 LocalInputMonitorX11::~LocalInputMonitorX11() { | 115 LocalInputMonitorLinux::~LocalInputMonitorLinux() { |
| 116 core_->Stop(); | 116 core_->Stop(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 LocalInputMonitorX11::Core::Core( | 119 LocalInputMonitorLinux::Core::Core( |
| 120 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 120 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 121 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, | 121 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 122 base::WeakPtr<ClientSessionControl> client_session_control) | 122 base::WeakPtr<ClientSessionControl> client_session_control) |
| 123 : caller_task_runner_(caller_task_runner), | 123 : caller_task_runner_(caller_task_runner), |
| 124 input_task_runner_(input_task_runner), | 124 input_task_runner_(input_task_runner), |
| 125 client_session_control_(client_session_control), | 125 client_session_control_(client_session_control), |
| 126 alt_pressed_(false), | 126 alt_pressed_(false), |
| 127 ctrl_pressed_(false), | 127 ctrl_pressed_(false), |
| 128 display_(NULL), | 128 display_(NULL), |
| 129 x_record_display_(NULL), | 129 x_record_display_(NULL), |
| 130 x_record_context_(0) { | 130 x_record_context_(0) { |
| 131 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 131 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 132 DCHECK(client_session_control_.get()); | 132 DCHECK(client_session_control_.get()); |
| 133 | 133 |
| 134 x_record_range_[0] = NULL; | 134 x_record_range_[0] = NULL; |
| 135 x_record_range_[1] = NULL; | 135 x_record_range_[1] = NULL; |
| 136 } | 136 } |
| 137 | 137 |
| 138 void LocalInputMonitorX11::Core::Start() { | 138 void LocalInputMonitorLinux::Core::Start() { |
| 139 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 139 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 140 | 140 |
| 141 input_task_runner_->PostTask(FROM_HERE, | 141 input_task_runner_->PostTask(FROM_HERE, |
| 142 base::Bind(&Core::StartOnInputThread, this)); | 142 base::Bind(&Core::StartOnInputThread, this)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void LocalInputMonitorX11::Core::Stop() { | 145 void LocalInputMonitorLinux::Core::Stop() { |
| 146 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 146 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 147 | 147 |
| 148 input_task_runner_->PostTask(FROM_HERE, | 148 input_task_runner_->PostTask(FROM_HERE, |
| 149 base::Bind(&Core::StopOnInputThread, this)); | 149 base::Bind(&Core::StopOnInputThread, this)); |
| 150 } | 150 } |
| 151 | 151 |
| 152 LocalInputMonitorX11::Core::~Core() { | 152 LocalInputMonitorLinux::Core::~Core() { |
| 153 DCHECK(!display_); | 153 DCHECK(!display_); |
| 154 DCHECK(!x_record_display_); | 154 DCHECK(!x_record_display_); |
| 155 DCHECK(!x_record_range_[0]); | 155 DCHECK(!x_record_range_[0]); |
| 156 DCHECK(!x_record_range_[1]); | 156 DCHECK(!x_record_range_[1]); |
| 157 DCHECK(!x_record_context_); | 157 DCHECK(!x_record_context_); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void LocalInputMonitorX11::Core::StartOnInputThread() { | 160 void LocalInputMonitorLinux::Core::StartOnInputThread() { |
| 161 DCHECK(input_task_runner_->BelongsToCurrentThread()); | 161 DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| 162 DCHECK(!display_); | 162 DCHECK(!display_); |
| 163 DCHECK(!x_record_display_); | 163 DCHECK(!x_record_display_); |
| 164 DCHECK(!x_record_range_[0]); | 164 DCHECK(!x_record_range_[0]); |
| 165 DCHECK(!x_record_range_[1]); | 165 DCHECK(!x_record_range_[1]); |
| 166 DCHECK(!x_record_context_); | 166 DCHECK(!x_record_context_); |
| 167 | 167 |
| 168 // TODO(jamiewalch): We should pass the display in. At that point, since | 168 // TODO(jamiewalch): We should pass the display in. At that point, since |
| 169 // XRecord needs a private connection to the X Server for its data channel | 169 // XRecord needs a private connection to the X Server for its data channel |
| 170 // and both channels are used from a separate thread, we'll need to duplicate | 170 // and both channels are used from a separate thread, we'll need to duplicate |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 return; | 224 return; |
| 225 } | 225 } |
| 226 | 226 |
| 227 // Fetch pending events if any. | 227 // Fetch pending events if any. |
| 228 while (XPending(x_record_display_)) { | 228 while (XPending(x_record_display_)) { |
| 229 XEvent ev; | 229 XEvent ev; |
| 230 XNextEvent(x_record_display_, &ev); | 230 XNextEvent(x_record_display_, &ev); |
| 231 } | 231 } |
| 232 } | 232 } |
| 233 | 233 |
| 234 void LocalInputMonitorX11::Core::StopOnInputThread() { | 234 void LocalInputMonitorLinux::Core::StopOnInputThread() { |
| 235 DCHECK(input_task_runner_->BelongsToCurrentThread()); | 235 DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| 236 | 236 |
| 237 // Context must be disabled via the control channel because we can't send | 237 // Context must be disabled via the control channel because we can't send |
| 238 // any X protocol traffic over the data channel while it's recording. | 238 // any X protocol traffic over the data channel while it's recording. |
| 239 if (x_record_context_) { | 239 if (x_record_context_) { |
| 240 XRecordDisableContext(display_, x_record_context_); | 240 XRecordDisableContext(display_, x_record_context_); |
| 241 XFlush(display_); | 241 XFlush(display_); |
| 242 } | 242 } |
| 243 | 243 |
| 244 controller_.StopWatchingFileDescriptor(); | 244 controller_.StopWatchingFileDescriptor(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 258 if (x_record_display_) { | 258 if (x_record_display_) { |
| 259 XCloseDisplay(x_record_display_); | 259 XCloseDisplay(x_record_display_); |
| 260 x_record_display_ = NULL; | 260 x_record_display_ = NULL; |
| 261 } | 261 } |
| 262 if (display_) { | 262 if (display_) { |
| 263 XCloseDisplay(display_); | 263 XCloseDisplay(display_); |
| 264 display_ = NULL; | 264 display_ = NULL; |
| 265 } | 265 } |
| 266 } | 266 } |
| 267 | 267 |
| 268 void LocalInputMonitorX11::Core::OnFileCanReadWithoutBlocking(int fd) { | 268 void LocalInputMonitorLinux::Core::OnFileCanReadWithoutBlocking(int fd) { |
| 269 DCHECK(input_task_runner_->BelongsToCurrentThread()); | 269 DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| 270 | 270 |
| 271 // Fetch pending events if any. | 271 // Fetch pending events if any. |
| 272 while (XPending(x_record_display_)) { | 272 while (XPending(x_record_display_)) { |
| 273 XEvent ev; | 273 XEvent ev; |
| 274 XNextEvent(x_record_display_, &ev); | 274 XNextEvent(x_record_display_, &ev); |
| 275 } | 275 } |
| 276 } | 276 } |
| 277 | 277 |
| 278 void LocalInputMonitorX11::Core::OnFileCanWriteWithoutBlocking(int fd) { | 278 void LocalInputMonitorLinux::Core::OnFileCanWriteWithoutBlocking(int fd) { |
| 279 NOTREACHED(); | 279 NOTREACHED(); |
| 280 } | 280 } |
| 281 | 281 |
| 282 void LocalInputMonitorX11::Core::ProcessXEvent(xEvent* event) { | 282 void LocalInputMonitorLinux::Core::ProcessXEvent(xEvent* event) { |
| 283 DCHECK(input_task_runner_->BelongsToCurrentThread()); | 283 DCHECK(input_task_runner_->BelongsToCurrentThread()); |
| 284 | 284 |
| 285 if (event->u.u.type == MotionNotify) { | 285 if (event->u.u.type == MotionNotify) { |
| 286 webrtc::DesktopVector position(event->u.keyButtonPointer.rootX, | 286 webrtc::DesktopVector position(event->u.keyButtonPointer.rootX, |
| 287 event->u.keyButtonPointer.rootY); | 287 event->u.keyButtonPointer.rootY); |
| 288 caller_task_runner_->PostTask( | 288 caller_task_runner_->PostTask( |
| 289 FROM_HERE, base::Bind(&ClientSessionControl::OnLocalMouseMoved, | 289 FROM_HERE, base::Bind(&ClientSessionControl::OnLocalMouseMoved, |
| 290 client_session_control_, | 290 client_session_control_, |
| 291 position)); | 291 position)); |
| 292 } else { | 292 } else { |
| 293 int key_code = event->u.u.detail; | 293 int key_code = event->u.u.detail; |
| 294 bool down = event->u.u.type == KeyPress; | 294 bool down = event->u.u.type == KeyPress; |
| 295 KeySym key_sym = XkbKeycodeToKeysym(display_, key_code, 0, 0); | 295 KeySym key_sym = XkbKeycodeToKeysym(display_, key_code, 0, 0); |
| 296 if (key_sym == XK_Control_L || key_sym == XK_Control_R) { | 296 if (key_sym == XK_Control_L || key_sym == XK_Control_R) { |
| 297 ctrl_pressed_ = down; | 297 ctrl_pressed_ = down; |
| 298 } else if (key_sym == XK_Alt_L || key_sym == XK_Alt_R) { | 298 } else if (key_sym == XK_Alt_L || key_sym == XK_Alt_R) { |
| 299 alt_pressed_ = down; | 299 alt_pressed_ = down; |
| 300 } else if (key_sym == XK_Escape && down && alt_pressed_ && ctrl_pressed_) { | 300 } else if (key_sym == XK_Escape && down && alt_pressed_ && ctrl_pressed_) { |
| 301 caller_task_runner_->PostTask( | 301 caller_task_runner_->PostTask( |
| 302 FROM_HERE, base::Bind(&ClientSessionControl::DisconnectSession, | 302 FROM_HERE, base::Bind(&ClientSessionControl::DisconnectSession, |
| 303 client_session_control_)); | 303 client_session_control_)); |
| 304 } | 304 } |
| 305 } | 305 } |
| 306 } | 306 } |
| 307 | 307 |
| 308 // static | 308 // static |
| 309 void LocalInputMonitorX11::Core::ProcessReply(XPointer self, | 309 void LocalInputMonitorLinux::Core::ProcessReply(XPointer self, |
| 310 XRecordInterceptData* data) { | 310 XRecordInterceptData* data) { |
| 311 if (data->category == XRecordFromServer) { | 311 if (data->category == XRecordFromServer) { |
| 312 xEvent* event = reinterpret_cast<xEvent*>(data->data); | 312 xEvent* event = reinterpret_cast<xEvent*>(data->data); |
| 313 reinterpret_cast<Core*>(self)->ProcessXEvent(event); | 313 reinterpret_cast<Core*>(self)->ProcessXEvent(event); |
| 314 } | 314 } |
| 315 XRecordFreeData(data); | 315 XRecordFreeData(data); |
| 316 } | 316 } |
| 317 | 317 |
| 318 } // namespace | 318 } // namespace |
| 319 | 319 |
| 320 scoped_ptr<LocalInputMonitor> LocalInputMonitor::Create( | 320 scoped_ptr<LocalInputMonitor> LocalInputMonitor::Create( |
| 321 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 321 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 322 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, | 322 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 323 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | 323 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 324 base::WeakPtr<ClientSessionControl> client_session_control) { | 324 base::WeakPtr<ClientSessionControl> client_session_control) { |
| 325 return make_scoped_ptr(new LocalInputMonitorX11( | 325 return make_scoped_ptr(new LocalInputMonitorLinux( |
| 326 caller_task_runner, input_task_runner, client_session_control)); | 326 caller_task_runner, input_task_runner, client_session_control)); |
| 327 } | 327 } |
| 328 | 328 |
| 329 } // namespace remoting | 329 } // namespace remoting |
| OLD | NEW |