| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 18 #include "base/win/message_window.h" | 19 #include "base/win/message_window.h" |
| 19 #include "media/base/keyboard_event_counter.h" | 20 #include "media/base/keyboard_event_counter.h" |
| 20 #include "third_party/skia/include/core/SkPoint.h" | 21 #include "third_party/skia/include/core/SkPoint.h" |
| 21 #include "ui/events/keycodes/keyboard_code_conversion_win.h" | 22 #include "ui/events/keycodes/keyboard_code_conversion_win.h" |
| 22 | 23 |
| 23 namespace media { | 24 namespace media { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 LPARAM lparam, | 62 LPARAM lparam, |
| 62 LRESULT* result); | 63 LRESULT* result); |
| 63 RAWINPUTDEVICE* GetRawInputDevices(EventBitMask event, DWORD flags); | 64 RAWINPUTDEVICE* GetRawInputDevices(EventBitMask event, DWORD flags); |
| 64 | 65 |
| 65 // Task runner on which |window_| is created. | 66 // Task runner on which |window_| is created. |
| 66 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 67 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 67 scoped_refptr<base::ObserverListThreadSafe< | 68 scoped_refptr<base::ObserverListThreadSafe< |
| 68 UserInputMonitor::MouseEventListener>> mouse_listeners_; | 69 UserInputMonitor::MouseEventListener>> mouse_listeners_; |
| 69 | 70 |
| 70 // These members are only accessed on the UI thread. | 71 // These members are only accessed on the UI thread. |
| 71 scoped_ptr<base::win::MessageWindow> window_; | 72 std::unique_ptr<base::win::MessageWindow> window_; |
| 72 uint8_t events_monitored_; | 73 uint8_t events_monitored_; |
| 73 KeyboardEventCounter counter_; | 74 KeyboardEventCounter counter_; |
| 74 | 75 |
| 75 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWinCore); | 76 DISALLOW_COPY_AND_ASSIGN(UserInputMonitorWinCore); |
| 76 }; | 77 }; |
| 77 | 78 |
| 78 class UserInputMonitorWin : public UserInputMonitor { | 79 class UserInputMonitorWin : public UserInputMonitor { |
| 79 public: | 80 public: |
| 80 explicit UserInputMonitorWin( | 81 explicit UserInputMonitorWin( |
| 81 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); | 82 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 window_.reset(new base::win::MessageWindow()); | 133 window_.reset(new base::win::MessageWindow()); |
| 133 if (!window_->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage, | 134 if (!window_->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage, |
| 134 base::Unretained(this)))) { | 135 base::Unretained(this)))) { |
| 135 PLOG(ERROR) << "Failed to create the raw input window"; | 136 PLOG(ERROR) << "Failed to create the raw input window"; |
| 136 window_.reset(); | 137 window_.reset(); |
| 137 return; | 138 return; |
| 138 } | 139 } |
| 139 } | 140 } |
| 140 | 141 |
| 141 // Register to receive raw mouse and/or keyboard input. | 142 // Register to receive raw mouse and/or keyboard input. |
| 142 scoped_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(type, RIDEV_INPUTSINK)); | 143 std::unique_ptr<RAWINPUTDEVICE> device( |
| 144 GetRawInputDevices(type, RIDEV_INPUTSINK)); |
| 143 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { | 145 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { |
| 144 PLOG(ERROR) << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK"; | 146 PLOG(ERROR) << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK"; |
| 145 window_.reset(); | 147 window_.reset(); |
| 146 return; | 148 return; |
| 147 } | 149 } |
| 148 | 150 |
| 149 // Start observing message loop destruction if we start monitoring the first | 151 // Start observing message loop destruction if we start monitoring the first |
| 150 // event. | 152 // event. |
| 151 if (!events_monitored_) | 153 if (!events_monitored_) |
| 152 base::MessageLoop::current()->AddDestructionObserver(this); | 154 base::MessageLoop::current()->AddDestructionObserver(this); |
| 153 | 155 |
| 154 events_monitored_ |= type; | 156 events_monitored_ |= type; |
| 155 } | 157 } |
| 156 | 158 |
| 157 void UserInputMonitorWinCore::StopMonitor(EventBitMask type) { | 159 void UserInputMonitorWinCore::StopMonitor(EventBitMask type) { |
| 158 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 160 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 159 | 161 |
| 160 if (!(events_monitored_ & type)) | 162 if (!(events_monitored_ & type)) |
| 161 return; | 163 return; |
| 162 | 164 |
| 163 // Stop receiving raw input. | 165 // Stop receiving raw input. |
| 164 DCHECK(window_); | 166 DCHECK(window_); |
| 165 scoped_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(type, RIDEV_REMOVE)); | 167 std::unique_ptr<RAWINPUTDEVICE> device( |
| 168 GetRawInputDevices(type, RIDEV_REMOVE)); |
| 166 | 169 |
| 167 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { | 170 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { |
| 168 PLOG(INFO) << "RegisterRawInputDevices() failed for RIDEV_REMOVE"; | 171 PLOG(INFO) << "RegisterRawInputDevices() failed for RIDEV_REMOVE"; |
| 169 } | 172 } |
| 170 | 173 |
| 171 events_monitored_ &= ~type; | 174 events_monitored_ &= ~type; |
| 172 if (events_monitored_ == 0) { | 175 if (events_monitored_ == 0) { |
| 173 window_.reset(); | 176 window_.reset(); |
| 174 | 177 |
| 175 // Stop observing message loop destruction if no event is being monitored. | 178 // Stop observing message loop destruction if no event is being monitored. |
| 176 base::MessageLoop::current()->RemoveDestructionObserver(this); | 179 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 177 } | 180 } |
| 178 } | 181 } |
| 179 | 182 |
| 180 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) { | 183 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) { |
| 181 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 184 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 182 | 185 |
| 183 // Get the size of the input record. | 186 // Get the size of the input record. |
| 184 UINT size = 0; | 187 UINT size = 0; |
| 185 UINT result = GetRawInputData( | 188 UINT result = GetRawInputData( |
| 186 input_handle, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)); | 189 input_handle, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)); |
| 187 if (result == static_cast<UINT>(-1)) { | 190 if (result == static_cast<UINT>(-1)) { |
| 188 PLOG(ERROR) << "GetRawInputData() failed"; | 191 PLOG(ERROR) << "GetRawInputData() failed"; |
| 189 return 0; | 192 return 0; |
| 190 } | 193 } |
| 191 DCHECK_EQ(0u, result); | 194 DCHECK_EQ(0u, result); |
| 192 | 195 |
| 193 // Retrieve the input record itself. | 196 // Retrieve the input record itself. |
| 194 scoped_ptr<uint8_t[]> buffer(new uint8_t[size]); | 197 std::unique_ptr<uint8_t[]> buffer(new uint8_t[size]); |
| 195 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get()); | 198 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get()); |
| 196 result = GetRawInputData( | 199 result = GetRawInputData( |
| 197 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER)); | 200 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER)); |
| 198 if (result == static_cast<UINT>(-1)) { | 201 if (result == static_cast<UINT>(-1)) { |
| 199 PLOG(ERROR) << "GetRawInputData() failed"; | 202 PLOG(ERROR) << "GetRawInputData() failed"; |
| 200 return 0; | 203 return 0; |
| 201 } | 204 } |
| 202 DCHECK_EQ(size, result); | 205 DCHECK_EQ(size, result); |
| 203 | 206 |
| 204 // Notify the observer about events generated locally. | 207 // Notify the observer about events generated locally. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 | 240 |
| 238 default: | 241 default: |
| 239 return false; | 242 return false; |
| 240 } | 243 } |
| 241 } | 244 } |
| 242 | 245 |
| 243 RAWINPUTDEVICE* UserInputMonitorWinCore::GetRawInputDevices(EventBitMask event, | 246 RAWINPUTDEVICE* UserInputMonitorWinCore::GetRawInputDevices(EventBitMask event, |
| 244 DWORD flags) { | 247 DWORD flags) { |
| 245 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 248 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 246 | 249 |
| 247 scoped_ptr<RAWINPUTDEVICE> device(new RAWINPUTDEVICE()); | 250 std::unique_ptr<RAWINPUTDEVICE> device(new RAWINPUTDEVICE()); |
| 248 if (event == MOUSE_EVENT_MASK) { | 251 if (event == MOUSE_EVENT_MASK) { |
| 249 device->dwFlags = flags; | 252 device->dwFlags = flags; |
| 250 device->usUsagePage = kGenericDesktopPage; | 253 device->usUsagePage = kGenericDesktopPage; |
| 251 device->usUsage = kMouseUsage; | 254 device->usUsage = kMouseUsage; |
| 252 device->hwndTarget = window_->hwnd(); | 255 device->hwndTarget = window_->hwnd(); |
| 253 } else { | 256 } else { |
| 254 DCHECK_EQ(KEYBOARD_EVENT_MASK, event); | 257 DCHECK_EQ(KEYBOARD_EVENT_MASK, event); |
| 255 device->dwFlags = flags; | 258 device->dwFlags = flags; |
| 256 device->usUsagePage = kGenericDesktopPage; | 259 device->usUsagePage = kGenericDesktopPage; |
| 257 device->usUsage = kKeyboardUsage; | 260 device->usUsage = kKeyboardUsage; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 void UserInputMonitorWin::StopMouseMonitoring() { | 308 void UserInputMonitorWin::StopMouseMonitoring() { |
| 306 ui_task_runner_->PostTask( | 309 ui_task_runner_->PostTask( |
| 307 FROM_HERE, | 310 FROM_HERE, |
| 308 base::Bind(&UserInputMonitorWinCore::StopMonitor, | 311 base::Bind(&UserInputMonitorWinCore::StopMonitor, |
| 309 core_->AsWeakPtr(), | 312 core_->AsWeakPtr(), |
| 310 UserInputMonitorWinCore::MOUSE_EVENT_MASK)); | 313 UserInputMonitorWinCore::MOUSE_EVENT_MASK)); |
| 311 } | 314 } |
| 312 | 315 |
| 313 } // namespace | 316 } // namespace |
| 314 | 317 |
| 315 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( | 318 std::unique_ptr<UserInputMonitor> UserInputMonitor::Create( |
| 316 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 319 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 317 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { | 320 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { |
| 318 return scoped_ptr<UserInputMonitor>(new UserInputMonitorWin(ui_task_runner)); | 321 return base::WrapUnique(new UserInputMonitorWin(ui_task_runner)); |
| 319 } | 322 } |
| 320 | 323 |
| 321 } // namespace media | 324 } // namespace media |
| OLD | NEW |