| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
| 14 #include "base/win/wrapped_window_proc.h" | 14 #include "base/win/wrapped_window_proc.h" |
| 15 #include "remoting/host/mouse_move_observer.h" | |
| 16 #include "third_party/skia/include/core/SkPoint.h" | 15 #include "third_party/skia/include/core/SkPoint.h" |
| 17 | 16 |
| 18 namespace remoting { | 17 namespace remoting { |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 const wchar_t kWindowClassFormat[] = L"Chromoting_LocalInputMonitorWin_%p"; | 21 const wchar_t kWindowClassFormat[] = L"Chromoting_LocalInputMonitorWin_%p"; |
| 23 | 22 |
| 24 // From the HID Usage Tables specification. | 23 // From the HID Usage Tables specification. |
| 25 const USHORT kGenericDesktopPage = 1; | 24 const USHORT kGenericDesktopPage = 1; |
| 26 const USHORT kMouseUsage = 2; | 25 const USHORT kMouseUsage = 2; |
| 27 | 26 |
| 28 class LocalInputMonitorWin : public base::NonThreadSafe, | 27 class LocalInputMonitorWin : public base::NonThreadSafe, |
| 29 public LocalInputMonitor { | 28 public LocalInputMonitor { |
| 30 public: | 29 public: |
| 31 LocalInputMonitorWin( | 30 LocalInputMonitorWin( |
| 32 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 31 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 33 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); | 32 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 33 SessionController::Delegate* delegate); |
| 34 ~LocalInputMonitorWin(); | 34 ~LocalInputMonitorWin(); |
| 35 | 35 |
| 36 virtual void Start(MouseMoveObserver* mouse_move_observer, | |
| 37 const base::Closure& disconnect_callback) OVERRIDE; | |
| 38 virtual void Stop() OVERRIDE; | |
| 39 | |
| 40 private: | 36 private: |
| 41 // The actual implementation resides in LocalInputMonitorWin::Core class. | 37 // The actual implementation resides in LocalInputMonitorWin::Core class. |
| 42 class Core : public base::RefCountedThreadSafe<Core> { | 38 class Core : public base::RefCountedThreadSafe<Core> { |
| 43 public: | 39 public: |
| 44 Core(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 40 Core(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 45 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); | 41 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 42 SessionController::Delegate* delegate); |
| 46 | 43 |
| 47 void Start(MouseMoveObserver* mouse_move_observer); | 44 void Start(); |
| 48 void Stop(); | 45 void Stop(); |
| 49 | 46 |
| 50 private: | 47 private: |
| 51 friend class base::RefCountedThreadSafe<Core>; | 48 friend class base::RefCountedThreadSafe<Core>; |
| 52 virtual ~Core(); | 49 virtual ~Core(); |
| 53 | 50 |
| 54 void StartOnUiThread(); | 51 void StartOnUiThread(); |
| 55 void StopOnUiThread(); | 52 void StopOnUiThread(); |
| 56 | 53 |
| 57 // Posts OnLocalMouseMoved() notification to |mouse_move_observer_| on | 54 // Posts OnLocalMouseMoved() notification to |delegate_| on |
| 58 // the |caller_task_runner_| thread. | 55 // the |caller_task_runner_| thread. |
| 59 void OnLocalMouseMoved(const SkIPoint& position); | 56 void OnLocalMouseMoved(const SkIPoint& position); |
| 60 | 57 |
| 61 // Handles WM_CREATE messages. | 58 // Handles WM_CREATE messages. |
| 62 LRESULT OnCreate(HWND hwnd); | 59 LRESULT OnCreate(HWND hwnd); |
| 63 | 60 |
| 64 // Handles WM_INPUT messages. | 61 // Handles WM_INPUT messages. |
| 65 LRESULT OnInput(HRAWINPUT input_handle); | 62 LRESULT OnInput(HRAWINPUT input_handle); |
| 66 | 63 |
| 67 // Window procedure invoked by the OS to process window messages. | 64 // Window procedure invoked by the OS to process window messages. |
| 68 static LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, | 65 static LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, |
| 69 WPARAM wparam, LPARAM lparam); | 66 WPARAM wparam, LPARAM lparam); |
| 70 | 67 |
| 71 // Task runner on which public methods of this class must be called. | 68 // Task runner on which public methods of this class must be called. |
| 72 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | 69 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; |
| 73 | 70 |
| 74 // Task runner on which |window_| is created. | 71 // Task runner on which |window_| is created. |
| 75 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 72 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 76 | 73 |
| 77 // Atom representing the input window class. | 74 // Atom representing the input window class. |
| 78 ATOM atom_; | 75 ATOM atom_; |
| 79 | 76 |
| 80 // Instance of the module containing the window procedure. | 77 // Instance of the module containing the window procedure. |
| 81 HINSTANCE instance_; | 78 HINSTANCE instance_; |
| 82 | 79 |
| 83 // Handle of the input window. | 80 // Handle of the input window. |
| 84 HWND window_; | 81 HWND window_; |
| 85 | 82 |
| 86 // Observer to dispatch mouse event notifications to. | 83 // Points to the object receiving mouse event notifications. |
| 87 MouseMoveObserver* mouse_move_observer_; | 84 SessionController::Delegate* delegate_; |
| 88 | 85 |
| 89 DISALLOW_COPY_AND_ASSIGN(Core); | 86 DISALLOW_COPY_AND_ASSIGN(Core); |
| 90 }; | 87 }; |
| 91 | 88 |
| 92 scoped_refptr<Core> core_; | 89 scoped_refptr<Core> core_; |
| 93 | 90 |
| 94 DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorWin); | 91 DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorWin); |
| 95 }; | 92 }; |
| 96 | 93 |
| 97 LocalInputMonitorWin::LocalInputMonitorWin( | 94 LocalInputMonitorWin::LocalInputMonitorWin( |
| 98 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 95 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 99 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) | 96 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 100 : core_(new Core(caller_task_runner, ui_task_runner)) { | 97 SessionController::Delegate* delegate) |
| 98 : core_(new Core(caller_task_runner, ui_task_runner, delegate)) { |
| 99 core_->Start(); |
| 101 } | 100 } |
| 102 | 101 |
| 103 LocalInputMonitorWin::~LocalInputMonitorWin() { | 102 LocalInputMonitorWin::~LocalInputMonitorWin() { |
| 104 } | |
| 105 | |
| 106 void LocalInputMonitorWin::Start(MouseMoveObserver* mouse_move_observer, | |
| 107 const base::Closure& disconnect_callback) { | |
| 108 core_->Start(mouse_move_observer); | |
| 109 } | |
| 110 | |
| 111 void LocalInputMonitorWin::Stop() { | |
| 112 core_->Stop(); | 103 core_->Stop(); |
| 113 } | 104 } |
| 114 | 105 |
| 115 LocalInputMonitorWin::Core::Core( | 106 LocalInputMonitorWin::Core::Core( |
| 116 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 107 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 117 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) | 108 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 109 SessionController::Delegate* delegate) |
| 118 : caller_task_runner_(caller_task_runner), | 110 : caller_task_runner_(caller_task_runner), |
| 119 ui_task_runner_(ui_task_runner), | 111 ui_task_runner_(ui_task_runner), |
| 120 atom_(0), | 112 atom_(0), |
| 121 instance_(NULL), | 113 instance_(NULL), |
| 122 window_(NULL), | 114 window_(NULL), |
| 123 mouse_move_observer_(NULL) { | 115 delegate_(delegate) { |
| 116 DCHECK(delegate_); |
| 124 } | 117 } |
| 125 | 118 |
| 126 void LocalInputMonitorWin::Core::Start(MouseMoveObserver* mouse_move_observer) { | 119 void LocalInputMonitorWin::Core::Start() { |
| 127 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 120 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 128 DCHECK(!mouse_move_observer_); | |
| 129 DCHECK(mouse_move_observer); | |
| 130 | 121 |
| 131 mouse_move_observer_ = mouse_move_observer; | |
| 132 ui_task_runner_->PostTask(FROM_HERE, | 122 ui_task_runner_->PostTask(FROM_HERE, |
| 133 base::Bind(&Core::StartOnUiThread, this)); | 123 base::Bind(&Core::StartOnUiThread, this)); |
| 134 } | 124 } |
| 135 | 125 |
| 136 void LocalInputMonitorWin::Core::Stop() { | 126 void LocalInputMonitorWin::Core::Stop() { |
| 137 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 127 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 138 | 128 |
| 139 mouse_move_observer_ = NULL; | 129 delegate_ = NULL; |
| 140 ui_task_runner_->PostTask(FROM_HERE, base::Bind(&Core::StopOnUiThread, this)); | 130 ui_task_runner_->PostTask(FROM_HERE, base::Bind(&Core::StopOnUiThread, this)); |
| 141 } | 131 } |
| 142 | 132 |
| 143 LocalInputMonitorWin::Core::~Core() { | 133 LocalInputMonitorWin::Core::~Core() { |
| 144 DCHECK(!atom_); | 134 DCHECK(!atom_); |
| 145 DCHECK(!instance_); | 135 DCHECK(!instance_); |
| 146 DCHECK(!window_); | 136 DCHECK(!window_); |
| 147 DCHECK(!mouse_move_observer_); | 137 DCHECK(!delegate_); |
| 148 } | 138 } |
| 149 | 139 |
| 150 void LocalInputMonitorWin::Core::StartOnUiThread() { | 140 void LocalInputMonitorWin::Core::StartOnUiThread() { |
| 151 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 141 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 152 | 142 |
| 153 // Create a window for receiving raw input. | 143 // Create a window for receiving raw input. |
| 154 string16 window_class_name = base::StringPrintf(kWindowClassFormat, this); | 144 string16 window_class_name = base::StringPrintf(kWindowClassFormat, this); |
| 155 WNDCLASSEX window_class; | 145 WNDCLASSEX window_class; |
| 156 base::win::InitializeWindowClass( | 146 base::win::InitializeWindowClass( |
| 157 window_class_name.c_str(), | 147 window_class_name.c_str(), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 } | 179 } |
| 190 } | 180 } |
| 191 | 181 |
| 192 void LocalInputMonitorWin::Core::OnLocalMouseMoved(const SkIPoint& position) { | 182 void LocalInputMonitorWin::Core::OnLocalMouseMoved(const SkIPoint& position) { |
| 193 if (!caller_task_runner_->BelongsToCurrentThread()) { | 183 if (!caller_task_runner_->BelongsToCurrentThread()) { |
| 194 caller_task_runner_->PostTask( | 184 caller_task_runner_->PostTask( |
| 195 FROM_HERE, base::Bind(&Core::OnLocalMouseMoved, this, position)); | 185 FROM_HERE, base::Bind(&Core::OnLocalMouseMoved, this, position)); |
| 196 return; | 186 return; |
| 197 } | 187 } |
| 198 | 188 |
| 199 if (mouse_move_observer_) | 189 if (delegate_) |
| 200 mouse_move_observer_->OnLocalMouseMoved(position); | 190 delegate_->OnLocalMouseMoved(position); |
| 201 } | 191 } |
| 202 | 192 |
| 203 LRESULT LocalInputMonitorWin::Core::OnCreate(HWND hwnd) { | 193 LRESULT LocalInputMonitorWin::Core::OnCreate(HWND hwnd) { |
| 204 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 194 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 205 | 195 |
| 206 // Register to receive raw mouse input. | 196 // Register to receive raw mouse input. |
| 207 RAWINPUTDEVICE device = {0}; | 197 RAWINPUTDEVICE device = {0}; |
| 208 device.dwFlags = RIDEV_INPUTSINK; | 198 device.dwFlags = RIDEV_INPUTSINK; |
| 209 device.usUsagePage = kGenericDesktopPage; | 199 device.usUsagePage = kGenericDesktopPage; |
| 210 device.usUsage = kMouseUsage; | 200 device.usUsage = kMouseUsage; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 default: | 274 default: |
| 285 return DefWindowProc(hwnd, message, wparam, lparam); | 275 return DefWindowProc(hwnd, message, wparam, lparam); |
| 286 } | 276 } |
| 287 } | 277 } |
| 288 | 278 |
| 289 } // namespace | 279 } // namespace |
| 290 | 280 |
| 291 scoped_ptr<LocalInputMonitor> LocalInputMonitor::Create( | 281 scoped_ptr<LocalInputMonitor> LocalInputMonitor::Create( |
| 292 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | 282 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 293 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, | 283 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 294 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { | 284 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 285 SessionController::Delegate* delegate) { |
| 295 return scoped_ptr<LocalInputMonitor>( | 286 return scoped_ptr<LocalInputMonitor>( |
| 296 new LocalInputMonitorWin(caller_task_runner, ui_task_runner)); | 287 new LocalInputMonitorWin(caller_task_runner, ui_task_runner, |
| 288 delegate)); |
| 297 } | 289 } |
| 298 | 290 |
| 299 } // namespace remoting | 291 } // namespace remoting |
| OLD | NEW |