| 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 "device/gamepad/gamepad_provider.h" | 5 #include "device/gamepad/gamepad_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 22 #include "device/gamepad/gamepad_data_fetcher.h" | 22 #include "device/gamepad/gamepad_data_fetcher.h" |
| 23 #include "device/gamepad/gamepad_data_fetcher_manager.h" | 23 #include "device/gamepad/gamepad_data_fetcher_manager.h" |
| 24 #include "device/gamepad/gamepad_user_gesture.h" | 24 #include "device/gamepad/gamepad_user_gesture.h" |
| 25 #include "mojo/public/cpp/system/platform_handle.h" | 25 #include "mojo/public/cpp/system/platform_handle.h" |
| 26 | 26 |
| 27 using blink::WebGamepad; | |
| 28 using blink::WebGamepads; | |
| 29 | |
| 30 namespace device { | 27 namespace device { |
| 31 | 28 |
| 32 GamepadProvider::ClosureAndThread::ClosureAndThread( | 29 GamepadProvider::ClosureAndThread::ClosureAndThread( |
| 33 const base::Closure& c, | 30 const base::Closure& c, |
| 34 const scoped_refptr<base::SingleThreadTaskRunner>& m) | 31 const scoped_refptr<base::SingleThreadTaskRunner>& m) |
| 35 : closure(c), task_runner(m) {} | 32 : closure(c), task_runner(m) {} |
| 36 | 33 |
| 37 GamepadProvider::ClosureAndThread::ClosureAndThread( | 34 GamepadProvider::ClosureAndThread::ClosureAndThread( |
| 38 const ClosureAndThread& other) = default; | 35 const ClosureAndThread& other) = default; |
| 39 | 36 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 92 |
| 96 mojo::ScopedSharedBufferHandle GamepadProvider::GetSharedBufferHandle() { | 93 mojo::ScopedSharedBufferHandle GamepadProvider::GetSharedBufferHandle() { |
| 97 // TODO(heke): Use mojo::SharedBuffer rather than base::SharedMemory in | 94 // TODO(heke): Use mojo::SharedBuffer rather than base::SharedMemory in |
| 98 // GamepadSharedBuffer. See crbug.com/670655 for details | 95 // GamepadSharedBuffer. See crbug.com/670655 for details |
| 99 base::SharedMemoryHandle handle = base::SharedMemory::DuplicateHandle( | 96 base::SharedMemoryHandle handle = base::SharedMemory::DuplicateHandle( |
| 100 gamepad_shared_buffer_->shared_memory()->handle()); | 97 gamepad_shared_buffer_->shared_memory()->handle()); |
| 101 return mojo::WrapSharedMemoryHandle(handle, sizeof(GamepadHardwareBuffer), | 98 return mojo::WrapSharedMemoryHandle(handle, sizeof(GamepadHardwareBuffer), |
| 102 true /* read_only */); | 99 true /* read_only */); |
| 103 } | 100 } |
| 104 | 101 |
| 105 void GamepadProvider::GetCurrentGamepadData(WebGamepads* data) { | 102 void GamepadProvider::GetCurrentGamepadData(Gamepads* data) { |
| 106 const WebGamepads* pads = gamepad_shared_buffer_->buffer(); | 103 const Gamepads* pads = gamepad_shared_buffer_->buffer(); |
| 107 base::AutoLock lock(shared_memory_lock_); | 104 base::AutoLock lock(shared_memory_lock_); |
| 108 *data = *pads; | 105 *data = *pads; |
| 109 } | 106 } |
| 110 | 107 |
| 111 void GamepadProvider::Pause() { | 108 void GamepadProvider::Pause() { |
| 112 { | 109 { |
| 113 base::AutoLock lock(is_paused_lock_); | 110 base::AutoLock lock(is_paused_lock_); |
| 114 is_paused_ = true; | 111 is_paused_ = true; |
| 115 } | 112 } |
| 116 base::MessageLoop* polling_loop = polling_thread_->message_loop(); | 113 base::MessageLoop* polling_loop = polling_thread_->message_loop(); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 } | 217 } |
| 221 } | 218 } |
| 222 | 219 |
| 223 void GamepadProvider::DoPoll() { | 220 void GamepadProvider::DoPoll() { |
| 224 DCHECK(polling_thread_->task_runner()->BelongsToCurrentThread()); | 221 DCHECK(polling_thread_->task_runner()->BelongsToCurrentThread()); |
| 225 DCHECK(have_scheduled_do_poll_); | 222 DCHECK(have_scheduled_do_poll_); |
| 226 have_scheduled_do_poll_ = false; | 223 have_scheduled_do_poll_ = false; |
| 227 | 224 |
| 228 bool changed; | 225 bool changed; |
| 229 | 226 |
| 230 ANNOTATE_BENIGN_RACE_SIZED(gamepad_shared_buffer_->buffer(), | 227 ANNOTATE_BENIGN_RACE_SIZED(gamepad_shared_buffer_->buffer(), sizeof(Gamepads), |
| 231 sizeof(WebGamepads), "Racey reads are discarded"); | 228 "Racey reads are discarded"); |
| 232 | 229 |
| 233 { | 230 { |
| 234 base::AutoLock lock(devices_changed_lock_); | 231 base::AutoLock lock(devices_changed_lock_); |
| 235 changed = devices_changed_; | 232 changed = devices_changed_; |
| 236 devices_changed_ = false; | 233 devices_changed_ = false; |
| 237 } | 234 } |
| 238 | 235 |
| 239 // Loop through each registered data fetcher and poll it's gamepad data. | 236 // Loop through each registered data fetcher and poll it's gamepad data. |
| 240 // It's expected that GetGamepadData will mark each gamepad as active (via | 237 // It's expected that GetGamepadData will mark each gamepad as active (via |
| 241 // GetPadState). If a gamepad is not marked as active during the calls to | 238 // GetPadState). If a gamepad is not marked as active during the calls to |
| 242 // GetGamepadData then it's assumed to be disconnected. | 239 // GetGamepadData then it's assumed to be disconnected. |
| 243 for (const auto& it : data_fetchers_) { | 240 for (const auto& it : data_fetchers_) { |
| 244 it->GetGamepadData(changed); | 241 it->GetGamepadData(changed); |
| 245 } | 242 } |
| 246 | 243 |
| 247 blink::WebGamepads* buffer = gamepad_shared_buffer_->buffer(); | 244 Gamepads* buffer = gamepad_shared_buffer_->buffer(); |
| 248 | 245 |
| 249 // Send out disconnect events using the last polled data before we wipe it out | 246 // Send out disconnect events using the last polled data before we wipe it out |
| 250 // in the mapping step. | 247 // in the mapping step. |
| 251 if (ever_had_user_gesture_) { | 248 if (ever_had_user_gesture_) { |
| 252 for (unsigned i = 0; i < WebGamepads::kItemsLengthCap; ++i) { | 249 for (unsigned i = 0; i < Gamepads::kItemsLengthCap; ++i) { |
| 253 PadState& state = pad_states_.get()[i]; | 250 PadState& state = pad_states_.get()[i]; |
| 254 | 251 |
| 255 if (!state.active_state && state.source != GAMEPAD_SOURCE_NONE) { | 252 if (!state.active_state && state.source != GAMEPAD_SOURCE_NONE) { |
| 256 auto pad = buffer->items[i]; | 253 auto pad = buffer->items[i]; |
| 257 pad.connected = false; | 254 pad.connected = false; |
| 258 OnGamepadConnectionChange(false, i, pad); | 255 OnGamepadConnectionChange(false, i, pad); |
| 259 ClearPadState(state); | 256 ClearPadState(state); |
| 260 } | 257 } |
| 261 } | 258 } |
| 262 } | 259 } |
| 263 | 260 |
| 264 { | 261 { |
| 265 base::AutoLock lock(shared_memory_lock_); | 262 base::AutoLock lock(shared_memory_lock_); |
| 266 | 263 |
| 267 // Acquire the SeqLock. There is only ever one writer to this data. | 264 // Acquire the SeqLock. There is only ever one writer to this data. |
| 268 // See gamepad_shared_buffer.h. | 265 // See gamepad_shared_buffer.h. |
| 269 gamepad_shared_buffer_->WriteBegin(); | 266 gamepad_shared_buffer_->WriteBegin(); |
| 270 for (unsigned i = 0; i < WebGamepads::kItemsLengthCap; ++i) { | 267 for (unsigned i = 0; i < Gamepads::kItemsLengthCap; ++i) { |
| 271 PadState& state = pad_states_.get()[i]; | 268 PadState& state = pad_states_.get()[i]; |
| 272 // Must run through the map+sanitize here or CheckForUserGesture may fail. | 269 // Must run through the map+sanitize here or CheckForUserGesture may fail. |
| 273 MapAndSanitizeGamepadData(&state, &buffer->items[i], sanitize_); | 270 MapAndSanitizeGamepadData(&state, &buffer->items[i], sanitize_); |
| 274 } | 271 } |
| 275 gamepad_shared_buffer_->WriteEnd(); | 272 gamepad_shared_buffer_->WriteEnd(); |
| 276 } | 273 } |
| 277 | 274 |
| 278 if (ever_had_user_gesture_) { | 275 if (ever_had_user_gesture_) { |
| 279 for (unsigned i = 0; i < WebGamepads::kItemsLengthCap; ++i) { | 276 for (unsigned i = 0; i < Gamepads::kItemsLengthCap; ++i) { |
| 280 PadState& state = pad_states_.get()[i]; | 277 PadState& state = pad_states_.get()[i]; |
| 281 | 278 |
| 282 if (state.active_state) { | 279 if (state.active_state) { |
| 283 if (state.active_state == GAMEPAD_NEWLY_ACTIVE) | 280 if (state.active_state == GAMEPAD_NEWLY_ACTIVE) |
| 284 OnGamepadConnectionChange(true, i, buffer->items[i]); | 281 OnGamepadConnectionChange(true, i, buffer->items[i]); |
| 285 state.active_state = GAMEPAD_INACTIVE; | 282 state.active_state = GAMEPAD_INACTIVE; |
| 286 } | 283 } |
| 287 } | 284 } |
| 288 } | 285 } |
| 289 | 286 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 305 } | 302 } |
| 306 | 303 |
| 307 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 304 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 308 FROM_HERE, base::Bind(&GamepadProvider::DoPoll, Unretained(this)), | 305 FROM_HERE, base::Bind(&GamepadProvider::DoPoll, Unretained(this)), |
| 309 base::TimeDelta::FromMilliseconds(kDesiredSamplingIntervalMs)); | 306 base::TimeDelta::FromMilliseconds(kDesiredSamplingIntervalMs)); |
| 310 have_scheduled_do_poll_ = true; | 307 have_scheduled_do_poll_ = true; |
| 311 } | 308 } |
| 312 | 309 |
| 313 void GamepadProvider::OnGamepadConnectionChange(bool connected, | 310 void GamepadProvider::OnGamepadConnectionChange(bool connected, |
| 314 int index, | 311 int index, |
| 315 const WebGamepad& pad) { | 312 const Gamepad& pad) { |
| 316 if (connection_change_client_) | 313 if (connection_change_client_) |
| 317 connection_change_client_->OnGamepadConnectionChange(connected, index, pad); | 314 connection_change_client_->OnGamepadConnectionChange(connected, index, pad); |
| 318 } | 315 } |
| 319 | 316 |
| 320 void GamepadProvider::CheckForUserGesture() { | 317 void GamepadProvider::CheckForUserGesture() { |
| 321 base::AutoLock lock(user_gesture_lock_); | 318 base::AutoLock lock(user_gesture_lock_); |
| 322 if (user_gesture_observers_.empty() && ever_had_user_gesture_) | 319 if (user_gesture_observers_.empty() && ever_had_user_gesture_) |
| 323 return; | 320 return; |
| 324 | 321 |
| 325 const WebGamepads* pads = gamepad_shared_buffer_->buffer(); | 322 const Gamepads* pads = gamepad_shared_buffer_->buffer(); |
| 326 if (GamepadsHaveUserGesture(*pads)) { | 323 if (GamepadsHaveUserGesture(*pads)) { |
| 327 ever_had_user_gesture_ = true; | 324 ever_had_user_gesture_ = true; |
| 328 for (size_t i = 0; i < user_gesture_observers_.size(); i++) { | 325 for (size_t i = 0; i < user_gesture_observers_.size(); i++) { |
| 329 user_gesture_observers_[i].task_runner->PostTask( | 326 user_gesture_observers_[i].task_runner->PostTask( |
| 330 FROM_HERE, user_gesture_observers_[i].closure); | 327 FROM_HERE, user_gesture_observers_[i].closure); |
| 331 } | 328 } |
| 332 user_gesture_observers_.clear(); | 329 user_gesture_observers_.clear(); |
| 333 } | 330 } |
| 334 } | 331 } |
| 335 | 332 |
| 336 } // namespace device | 333 } // namespace device |
| OLD | NEW |