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" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 if (events_monitored_ & type) | 121 if (events_monitored_ & type) |
122 return; | 122 return; |
123 | 123 |
124 if (type == KEYBOARD_EVENT_MASK) | 124 if (type == KEYBOARD_EVENT_MASK) |
125 counter_.Reset(); | 125 counter_.Reset(); |
126 | 126 |
127 if (!window_) { | 127 if (!window_) { |
128 window_.reset(new base::win::MessageWindow()); | 128 window_.reset(new base::win::MessageWindow()); |
129 if (!window_->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage, | 129 if (!window_->Create(base::Bind(&UserInputMonitorWinCore::HandleMessage, |
130 base::Unretained(this)))) { | 130 base::Unretained(this)))) { |
131 LOG_GETLASTERROR(ERROR) << "Failed to create the raw input window"; | 131 PLOG(ERROR) << "Failed to create the raw input window"; |
132 window_.reset(); | 132 window_.reset(); |
133 return; | 133 return; |
134 } | 134 } |
135 } | 135 } |
136 | 136 |
137 // Register to receive raw mouse and/or keyboard input. | 137 // Register to receive raw mouse and/or keyboard input. |
138 scoped_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(type, RIDEV_INPUTSINK)); | 138 scoped_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(type, RIDEV_INPUTSINK)); |
139 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { | 139 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { |
140 LOG_GETLASTERROR(ERROR) | 140 PLOG(ERROR) << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK"; |
141 << "RegisterRawInputDevices() failed for RIDEV_INPUTSINK"; | |
142 window_.reset(); | 141 window_.reset(); |
143 return; | 142 return; |
144 } | 143 } |
145 | 144 |
146 // Start observing message loop destruction if we start monitoring the first | 145 // Start observing message loop destruction if we start monitoring the first |
147 // event. | 146 // event. |
148 if (!events_monitored_) | 147 if (!events_monitored_) |
149 base::MessageLoop::current()->AddDestructionObserver(this); | 148 base::MessageLoop::current()->AddDestructionObserver(this); |
150 | 149 |
151 events_monitored_ |= type; | 150 events_monitored_ |= type; |
152 } | 151 } |
153 | 152 |
154 void UserInputMonitorWinCore::StopMonitor(EventBitMask type) { | 153 void UserInputMonitorWinCore::StopMonitor(EventBitMask type) { |
155 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 154 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
156 | 155 |
157 if (!(events_monitored_ & type)) | 156 if (!(events_monitored_ & type)) |
158 return; | 157 return; |
159 | 158 |
160 // Stop receiving raw input. | 159 // Stop receiving raw input. |
161 DCHECK(window_); | 160 DCHECK(window_); |
162 scoped_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(type, RIDEV_REMOVE)); | 161 scoped_ptr<RAWINPUTDEVICE> device(GetRawInputDevices(type, RIDEV_REMOVE)); |
163 | 162 |
164 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { | 163 if (!RegisterRawInputDevices(device.get(), 1, sizeof(*device))) { |
165 LOG_GETLASTERROR(INFO) | 164 PLOG(INFO) << "RegisterRawInputDevices() failed for RIDEV_REMOVE"; |
166 << "RegisterRawInputDevices() failed for RIDEV_REMOVE"; | |
167 } | 165 } |
168 | 166 |
169 events_monitored_ &= ~type; | 167 events_monitored_ &= ~type; |
170 if (events_monitored_ == 0) { | 168 if (events_monitored_ == 0) { |
171 window_.reset(); | 169 window_.reset(); |
172 | 170 |
173 // Stop observing message loop destruction if no event is being monitored. | 171 // Stop observing message loop destruction if no event is being monitored. |
174 base::MessageLoop::current()->RemoveDestructionObserver(this); | 172 base::MessageLoop::current()->RemoveDestructionObserver(this); |
175 } | 173 } |
176 } | 174 } |
177 | 175 |
178 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) { | 176 LRESULT UserInputMonitorWinCore::OnInput(HRAWINPUT input_handle) { |
179 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 177 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
180 | 178 |
181 // Get the size of the input record. | 179 // Get the size of the input record. |
182 UINT size = 0; | 180 UINT size = 0; |
183 UINT result = GetRawInputData( | 181 UINT result = GetRawInputData( |
184 input_handle, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)); | 182 input_handle, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)); |
185 if (result == -1) { | 183 if (result == -1) { |
186 LOG_GETLASTERROR(ERROR) << "GetRawInputData() failed"; | 184 PLOG(ERROR) << "GetRawInputData() failed"; |
187 return 0; | 185 return 0; |
188 } | 186 } |
189 DCHECK_EQ(0u, result); | 187 DCHECK_EQ(0u, result); |
190 | 188 |
191 // Retrieve the input record itself. | 189 // Retrieve the input record itself. |
192 scoped_ptr<uint8[]> buffer(new uint8[size]); | 190 scoped_ptr<uint8[]> buffer(new uint8[size]); |
193 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get()); | 191 RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get()); |
194 result = GetRawInputData( | 192 result = GetRawInputData( |
195 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER)); | 193 input_handle, RID_INPUT, buffer.get(), &size, sizeof(RAWINPUTHEADER)); |
196 if (result == -1) { | 194 if (result == -1) { |
197 LOG_GETLASTERROR(ERROR) << "GetRawInputData() failed"; | 195 PLOG(ERROR) << "GetRawInputData() failed"; |
198 return 0; | 196 return 0; |
199 } | 197 } |
200 DCHECK_EQ(size, result); | 198 DCHECK_EQ(size, result); |
201 | 199 |
202 // Notify the observer about events generated locally. | 200 // Notify the observer about events generated locally. |
203 if (input->header.dwType == RIM_TYPEMOUSE && input->header.hDevice != NULL) { | 201 if (input->header.dwType == RIM_TYPEMOUSE && input->header.hDevice != NULL) { |
204 POINT position; | 202 POINT position; |
205 if (!GetCursorPos(&position)) { | 203 if (!GetCursorPos(&position)) { |
206 position.x = 0; | 204 position.x = 0; |
207 position.y = 0; | 205 position.y = 0; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 | 308 |
311 } // namespace | 309 } // namespace |
312 | 310 |
313 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( | 311 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( |
314 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 312 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
315 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { | 313 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { |
316 return scoped_ptr<UserInputMonitor>(new UserInputMonitorWin(ui_task_runner)); | 314 return scoped_ptr<UserInputMonitor>(new UserInputMonitorWin(ui_task_runner)); |
317 } | 315 } |
318 | 316 |
319 } // namespace media | 317 } // namespace media |
OLD | NEW |