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