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 <cmath> | 5 #include <cmath> |
6 #include <set> | 6 #include <set> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 168 |
169 bool GamepadProvider::PadState::Match(const WebGamepad& pad) const { | 169 bool GamepadProvider::PadState::Match(const WebGamepad& pad) const { |
170 return connected_ == pad.connected && | 170 return connected_ == pad.connected && |
171 axes_length_ == pad.axesLength && | 171 axes_length_ == pad.axesLength && |
172 buttons_length_ == pad.buttonsLength && | 172 buttons_length_ == pad.buttonsLength && |
173 memcmp(id_, pad.id, arraysize(id_)) == 0 && | 173 memcmp(id_, pad.id, arraysize(id_)) == 0 && |
174 memcmp(mapping_, pad.mapping, arraysize(mapping_)) == 0; | 174 memcmp(mapping_, pad.mapping, arraysize(mapping_)) == 0; |
175 } | 175 } |
176 | 176 |
177 void GamepadProvider::PadState::SetPad(const WebGamepad& pad) { | 177 void GamepadProvider::PadState::SetPad(const WebGamepad& pad) { |
178 DCHECK(pad.connected); | 178 connected_ = pad.connected; |
179 connected_ = true; | |
180 axes_length_ = pad.axesLength; | 179 axes_length_ = pad.axesLength; |
181 buttons_length_ = pad.buttonsLength; | 180 buttons_length_ = pad.buttonsLength; |
182 memcpy(id_, pad.id, arraysize(id_)); | 181 memcpy(id_, pad.id, arraysize(id_)); |
183 memcpy(mapping_, pad.mapping, arraysize(mapping_)); | 182 memcpy(mapping_, pad.mapping, arraysize(mapping_)); |
184 } | 183 } |
185 | 184 |
186 void GamepadProvider::PadState::SetDisconnected() { | 185 void GamepadProvider::PadState::SetDisconnected() { |
187 connected_ = false; | 186 connected_ = false; |
188 axes_length_ = 0; | 187 axes_length_ = 0; |
189 buttons_length_ = 0; | 188 buttons_length_ = 0; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 { | 222 { |
224 base::AutoLock lock(shared_memory_lock_); | 223 base::AutoLock lock(shared_memory_lock_); |
225 | 224 |
226 // Acquire the SeqLock. There is only ever one writer to this data. | 225 // Acquire the SeqLock. There is only ever one writer to this data. |
227 // See gamepad_hardware_buffer.h. | 226 // See gamepad_hardware_buffer.h. |
228 hwbuf->sequence.WriteBegin(); | 227 hwbuf->sequence.WriteBegin(); |
229 data_fetcher_->GetGamepadData(&hwbuf->buffer, changed); | 228 data_fetcher_->GetGamepadData(&hwbuf->buffer, changed); |
230 hwbuf->sequence.WriteEnd(); | 229 hwbuf->sequence.WriteEnd(); |
231 } | 230 } |
232 | 231 |
233 CheckForUserGesture(); | |
234 | |
235 if (ever_had_user_gesture_) { | 232 if (ever_had_user_gesture_) { |
236 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { | 233 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
237 WebGamepad& pad = hwbuf->buffer.items[i]; | 234 WebGamepad& pad = hwbuf->buffer.items[i]; |
238 PadState& state = pad_states_.get()[i]; | 235 PadState& state = pad_states_.get()[i]; |
239 if (pad.connected && !state.connected()) { | 236 if (pad.connected && !state.connected()) { |
240 OnGamepadConnectionChange(true, i, pad); | 237 OnGamepadConnectionChange(true, i, pad); |
241 } else if (!pad.connected && state.connected()) { | 238 } else if (!pad.connected && state.connected()) { |
242 OnGamepadConnectionChange(false, i, pad); | 239 OnGamepadConnectionChange(false, i, pad); |
243 } else if (pad.connected && state.connected() && !state.Match(pad)) { | 240 } else if (pad.connected && state.connected() && !state.Match(pad)) { |
244 WebGamepad old_pad; | 241 WebGamepad old_pad; |
245 state.AsWebGamepad(&old_pad); | 242 state.AsWebGamepad(&old_pad); |
246 OnGamepadConnectionChange(false, i, old_pad); | 243 OnGamepadConnectionChange(false, i, old_pad); |
247 OnGamepadConnectionChange(true, i, pad); | 244 OnGamepadConnectionChange(true, i, pad); |
248 } | 245 } |
249 } | 246 } |
250 } | 247 } |
251 | 248 |
| 249 CheckForUserGesture(); |
| 250 |
252 // Schedule our next interval of polling. | 251 // Schedule our next interval of polling. |
253 ScheduleDoPoll(); | 252 ScheduleDoPoll(); |
254 } | 253 } |
255 | 254 |
256 void GamepadProvider::ScheduleDoPoll() { | 255 void GamepadProvider::ScheduleDoPoll() { |
257 DCHECK(base::MessageLoop::current() == polling_thread_->message_loop()); | 256 DCHECK(base::MessageLoop::current() == polling_thread_->message_loop()); |
258 if (have_scheduled_do_poll_) | 257 if (have_scheduled_do_poll_) |
259 return; | 258 return; |
260 | 259 |
261 { | 260 { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 void* mem = gamepad_shared_memory_.memory(); | 300 void* mem = gamepad_shared_memory_.memory(); |
302 CHECK(mem); | 301 CHECK(mem); |
303 return static_cast<GamepadHardwareBuffer*>(mem); | 302 return static_cast<GamepadHardwareBuffer*>(mem); |
304 } | 303 } |
305 | 304 |
306 void GamepadProvider::CheckForUserGesture() { | 305 void GamepadProvider::CheckForUserGesture() { |
307 base::AutoLock lock(user_gesture_lock_); | 306 base::AutoLock lock(user_gesture_lock_); |
308 if (user_gesture_observers_.empty() && ever_had_user_gesture_) | 307 if (user_gesture_observers_.empty() && ever_had_user_gesture_) |
309 return; | 308 return; |
310 | 309 |
311 if (GamepadsHaveUserGesture(SharedMemoryAsHardwareBuffer()->buffer)) { | 310 bool had_gesture_before = ever_had_user_gesture_; |
| 311 const WebGamepads& pads = SharedMemoryAsHardwareBuffer()->buffer; |
| 312 if (GamepadsHaveUserGesture(pads)) { |
312 ever_had_user_gesture_ = true; | 313 ever_had_user_gesture_ = true; |
313 for (size_t i = 0; i < user_gesture_observers_.size(); i++) { | 314 for (size_t i = 0; i < user_gesture_observers_.size(); i++) { |
314 user_gesture_observers_[i].message_loop->PostTask(FROM_HERE, | 315 user_gesture_observers_[i].message_loop->PostTask(FROM_HERE, |
315 user_gesture_observers_[i].closure); | 316 user_gesture_observers_[i].closure); |
316 } | 317 } |
317 user_gesture_observers_.clear(); | 318 user_gesture_observers_.clear(); |
318 } | 319 } |
| 320 if (!had_gesture_before && ever_had_user_gesture_) { |
| 321 // Initialize pad_states_ for the first time. |
| 322 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
| 323 pad_states_.get()[i].SetPad(pads.items[i]); |
| 324 } |
| 325 } |
319 } | 326 } |
320 | 327 |
321 } // namespace content | 328 } // namespace content |
OLD | NEW |