Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "base/single_thread_task_runner.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "base/synchronization/lock.h" | |
| 14 #include "base/win/message_window.h" | |
| 15 #include "media/base/keyboard_event_counter.h" | |
| 16 #include "third_party/skia/include/core/SkPoint.h" | |
| 17 #include "ui/base/keycodes/keyboard_code_conversion_win.h" | |
| 18 | |
| 7 namespace media { | 19 namespace media { |
| 8 | 20 namespace { |
| 9 // TODO(jiayl): add the implementation. | 21 |
| 22 // From the HID Usage Tables specification. | |
| 23 const USHORT kGenericDesktopPage = 1; | |
| 24 const USHORT kMouseUsage = 2; | |
| 25 const USHORT kKeyboardUsage = 6; | |
| 26 | |
| 27 // This is the actual implementation of event monitoring. It's separated from | |
| 28 // UserInputMonitorWin since it needs to be deleted on the UI thread. | |
| 29 class UserInputMonitorWinCore | |
| 30 : public base::SupportsWeakPtr<UserInputMonitorWinCore> { | |
|
Wez
2013/09/13 10:01:27
See comment on Linux impl re SupportsWeakPtr
| |
| 31 public: | |
| 32 enum EventBitMask { | |
| 33 MOUSE_EVENT_MASK = 1, | |
| 34 KEYBOARD_EVENT_MASK = 2, | |
| 35 }; | |
| 36 | |
| 37 explicit UserInputMonitorWinCore( | |
| 38 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
| 39 const scoped_refptr<UserInputMonitor::MouseListenerList>& | |
| 40 mouse_listeners); | |
| 41 ~UserInputMonitorWinCore(); | |
| 42 | |
| 43 size_t GetKeyPressCount() const; | |
| 44 void StartMonitor(EventBitMask type); | |
| 45 void StopMonitor(EventBitMask type); | |
| 46 | |
| 47 private: | |
| 48 // Handles WM_INPUT messages. | |
| 49 LRESULT OnInput(HRAWINPUT input_handle); | |
| 50 // Handles messages received by |window_|. | |
| 51 bool HandleMessage(UINT message, | |
| 52 WPARAM wparam, | |
| 53 LPARAM lparam, | |
| 54 LRESULT* result); | |
| 55 RAWINPUTDEVICE* GetRawInputDevices(EventBitMask event, DWORD flags); | |
| 56 | |
| 57 // Task runner on which |window_| is created. | |
| 58 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | |
| 59 scoped_refptr<ObserverListThreadSafe<UserInputMonitor::MouseEventListener> > | |
| 60 mouse_listeners_; | |
| 61 | |
| 62 // These members are only accessed on the UI thread. | |
| 63 scoped_ptr<base::win::MessageWindow> window_; | |
| 64 uint8 events_monitored_; | |
| 65 KeyboardEventCounter counter_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWinCore); | |
| 68 }; | |
| 69 | |
| 70 class UserInputMonitorWin : public UserInputMonitor { | |
| 71 public: | |
| 72 explicit UserInputMonitorWin( | |
| 73 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); | |
| 74 virtual ~UserInputMonitorWin(); | |
| 75 | |
| 76 // Public UserInputMonitor overrides. | |
| 77 virtual size_t GetKeyPressCount() const OVERRIDE; | |
| 78 | |
| 79 private: | |
| 80 // Private UserInputMonitor overrides. | |
| 81 virtual void StartKeyboardMonitoring() OVERRIDE; | |
| 82 virtual void StopKeyboardMonitoring() OVERRIDE; | |
| 83 virtual void StartMouseMonitoring() OVERRIDE; | |
| 84 virtual void StopMouseMonitoring() OVERRIDE; | |
| 85 | |
| 86 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | |
| 87 UserInputMonitorWinCore* core_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWin); | |
| 90 }; | |
| 91 | |
| 92 UserInputMonitorWinCore::UserInputMonitorWinCore( | |
| 93 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
| 94 const scoped_refptr<UserInputMonitor::MouseListenerList>& mouse_listeners) | |
| 95 : ui_task_runner_(ui_task_runner), | |
| 96 mouse_listeners_(mouse_listeners), | |
| 97 events_monitored_(0) {} | |
| 98 | |
| 99 UserInputMonitorWinCore::~UserInputMonitorWinCore() { | |
| 100 DCHECK(!window_); | |
| 101 DCHECK(!events_monitored_); | |
| 102 } | |
| 103 | |
| 104 size_t UserInputMonitorWinCore::GetKeyPressCount() const { | |
| 105 return counter_.GetKeyPressCount(); | |
| 106 } | |
| 107 | |
| 108 void UserInputMonitorWinCore::StartMonitor(EventBitMask type) { | |
| 109 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 110 | |
| 111 if (events_monitored_ & type) | |
| 112 return; | |
| 113 | |
| 114 if (type == KEYBOARD_EVENT_MASK) | |
| 115 counter_.Reset(); | |
| 116 | |
| 117 if (!window_) { | |
| 118 window_.reset(new base::win::MessageWindow()); | |
| 119 if (!window_->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage, | |
| 120 base::Unretained(this)))) { | |
| 121 LOG_GETLASTERROR(ERROR) << "Failed to create the raw input window"; | |
| 122 window_.reset(); | |
| 123 return; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 // Register to receive raw mouse and/or keyboard input. | |
| 128 scoped_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(type, RIDEV_INPUTSINK)); | |
| 129 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { | |
| 130 LOG_GETLASTERROR(ERROR) | |
| 131 << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK"; | |
| 132 return; | |
| 133 } | |
| 134 events_monitored_ |= type; | |
| 135 } | |
| 136 | |
| 137 void UserInputMonitorWinCore::StopMonitor(EventBitMask type) { | |
| 138 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 139 | |
| 140 if (!(events_monitored_ & type)) | |
| 141 return; | |
| 142 | |
| 143 // Stop receiving raw input. | |
| 144 DCHECK(window_); | |
| 145 scoped_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(type, RIDEV_REMOVE)); | |
| 146 | |
| 147 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { | |
| 148 LOG_GETLASTERROR(INFO) | |
| 149 << "RegisterRawInputDevices() failed for RIDEV_REMOVE"; | |
| 150 } | |
| 151 | |
| 152 events_monitored_ &= ~type; | |
| 153 if (events_monitored_ == 0) | |
| 154 window_.reset(); | |
| 155 } | |
| 156 | |
| 157 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) { | |
| 158 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 159 | |
| 160 // Get the size of the input record. | |
| 161 UINT size = 0; | |
| 162 UINT result = GetRawInputData( | |
| 163 input_handle, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)); | |
| 164 if (result == -1) { | |
| 165 LOG_GETLASTERROR(ERROR) << "GetRawInputData() failed"; | |
| 166 return 0; | |
| 167 } | |
| 168 DCHECK_EQ(0u, result); | |
| 169 | |
| 170 // Retrieve the input record itself. | |
| 171 scoped_ptr<uint8[]> buffer(new uint8[size]); | |
| 172 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get()); | |
| 173 result = GetRawInputData( | |
| 174 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER)); | |
| 175 if (result == -1) { | |
| 176 LOG_GETLASTERROR(ERROR) << "GetRawInputData() failed"; | |
| 177 return 0; | |
| 178 } | |
| 179 DCHECK_EQ(size, result); | |
| 180 | |
| 181 // Notify the observer about events generated locally. | |
| 182 if (input->header.dwType == RIM_TYPEMOUSE && input->header.hDevice != NULL) { | |
| 183 POINT position; | |
| 184 if (!GetCursorPos(&position)) { | |
| 185 position.x = 0; | |
| 186 position.y = 0; | |
| 187 } | |
| 188 mouse_listeners_->Notify( | |
| 189 &UserInputMonitor::MouseEventListener::OnMouseMoved, | |
| 190 SkIPoint::Make(position.x, position.y)); | |
| 191 } else if (input->header.dwType == RIM_TYPEKEYBOARD && | |
| 192 input->header.hDevice != NULL) { | |
| 193 ui::EventType event = (input->data.keyboard.Flags & RI_KEY_BREAK) | |
| 194 ? ui::ET_KEY_RELEASED | |
| 195 : ui::ET_KEY_PRESSED; | |
| 196 ui::KeyboardCode key_code = | |
| 197 ui::KeyboardCodeForWindowsKeyCode(input->data.keyboard.VKey); | |
| 198 counter_.OnKeyboardEvent(event, key_code); | |
| 199 } | |
| 200 | |
| 201 return DefRawInputProc(&input, 1, sizeof(RAWINPUTHEADER)); | |
| 202 } | |
| 203 | |
| 204 bool UserInputMonitorWinCore::HandleMessage(UINT message, | |
| 205 WPARAM wparam, | |
| 206 LPARAM lparam, | |
| 207 LRESULT* result) { | |
| 208 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 209 | |
| 210 switch (message) { | |
| 211 case WM_INPUT: | |
| 212 *result = OnInput(reinterpret_cast<HRAWINPUT>(lparam)); | |
| 213 return true; | |
| 214 | |
| 215 default: | |
| 216 return false; | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 RAWINPUTDEVICE* UserInputMonitorWinCore::GetRawInputDevices(EventBitMask event, | |
| 221 DWORD flags) { | |
| 222 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 223 | |
| 224 scoped_ptr<RAWINPUTDEVICE> device(new RAWINPUTDEVICE()); | |
| 225 if (event == MOUSE_EVENT_MASK) { | |
| 226 device->dwFlags = flags; | |
| 227 device->usUsagePage = kGenericDesktopPage; | |
| 228 device->usUsage = kMouseUsage; | |
| 229 device->hwndTarget = window_->hwnd(); | |
| 230 } else { | |
| 231 DCHECK_EQ(KEYBOARD_EVENT_MASK, event); | |
| 232 device->dwFlags = flags; | |
| 233 device->usUsagePage = kGenericDesktopPage; | |
| 234 device->usUsage = kKeyboardUsage; | |
| 235 device->hwndTarget = window_->hwnd(); | |
| 236 } | |
| 237 return device.release(); | |
| 238 } | |
| 239 | |
| 240 // | |
| 241 // Implementation of UserInputMonitorWin. | |
| 242 // | |
| 243 | |
| 244 UserInputMonitorWin::UserInputMonitorWin( | |
| 245 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) | |
| 246 : ui_task_runner_(ui_task_runner), | |
| 247 core_(new UserInputMonitorWinCore(ui_task_runner, mouse_listeners())) {} | |
| 248 | |
| 249 UserInputMonitorWin::~UserInputMonitorWin() { | |
| 250 if (!ui_task_runner_->DeleteSoon(FROM_HERE, core_)) | |
| 251 delete core_; | |
| 252 } | |
| 253 | |
| 254 size_t UserInputMonitorWin::GetKeyPressCount() const { | |
| 255 return core_->GetKeyPressCount(); | |
| 256 } | |
| 257 | |
| 258 void UserInputMonitorWin::StartKeyboardMonitoring() { | |
| 259 ui_task_runner_->PostTask( | |
| 260 FROM_HERE, | |
| 261 base::Bind(&UserInputMonitorWinCore::StartMonitor, | |
| 262 core_->AsWeakPtr(), | |
| 263 UserInputMonitorWinCore::KEYBOARD_EVENT_MASK)); | |
| 264 } | |
| 265 | |
| 266 void UserInputMonitorWin::StopKeyboardMonitoring() { | |
| 267 ui_task_runner_->PostTask( | |
| 268 FROM_HERE, | |
| 269 base::Bind(&UserInputMonitorWinCore::StopMonitor, | |
| 270 core_->AsWeakPtr(), | |
| 271 UserInputMonitorWinCore::KEYBOARD_EVENT_MASK)); | |
| 272 } | |
| 273 | |
| 274 void UserInputMonitorWin::StartMouseMonitoring() { | |
| 275 ui_task_runner_->PostTask( | |
| 276 FROM_HERE, | |
| 277 base::Bind(&UserInputMonitorWinCore::StartMonitor, | |
| 278 core_->AsWeakPtr(), | |
| 279 UserInputMonitorWinCore::MOUSE_EVENT_MASK)); | |
| 280 } | |
| 281 | |
| 282 void UserInputMonitorWin::StopMouseMonitoring() { | |
| 283 ui_task_runner_->PostTask( | |
| 284 FROM_HERE, | |
| 285 base::Bind(&UserInputMonitorWinCore::StopMonitor, | |
| 286 core_->AsWeakPtr(), | |
| 287 UserInputMonitorWinCore::MOUSE_EVENT_MASK)); | |
| 288 } | |
| 289 | |
| 290 } // namespace | |
| 291 | |
| 10 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( | 292 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( |
| 11 const scoped_refptr<base::SingleThreadTaskRunner>& input_task_runner, | 293 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 12 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { | 294 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { |
| 13 return scoped_ptr<UserInputMonitor>(); | 295 return scoped_ptr<UserInputMonitor>(new UserInputMonitorWin(ui_task_runner)); |
| 14 } | 296 } |
| 15 | 297 |
| 16 } // namespace media | 298 } // namespace media |
| OLD | NEW |