| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/exo/gaming_seat.h" | 5 #include "components/exo/gaming_seat.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 | 43 |
| 44 // Implements all methods and resources running on the polling thread. | 44 // Implements all methods and resources running on the polling thread. |
| 45 // This class is reference counted to allow it to shut down safely on the | 45 // This class is reference counted to allow it to shut down safely on the |
| 46 // polling thread even if the Gamepad has been destroyed on the origin thread. | 46 // polling thread even if the Gamepad has been destroyed on the origin thread. |
| 47 class GamingSeat::ThreadSafeGamepadChangeFetcher | 47 class GamingSeat::ThreadSafeGamepadChangeFetcher |
| 48 : public device::GamepadPadStateProvider, | 48 : public device::GamepadPadStateProvider, |
| 49 public base::RefCountedThreadSafe< | 49 public base::RefCountedThreadSafe< |
| 50 GamingSeat::ThreadSafeGamepadChangeFetcher> { | 50 GamingSeat::ThreadSafeGamepadChangeFetcher> { |
| 51 public: | 51 public: |
| 52 using ProcessGamepadChangesCallback = | 52 using ProcessGamepadChangesCallback = |
| 53 base::Callback<void(int index, const blink::WebGamepad)>; | 53 base::Callback<void(int index, const device::Gamepad)>; |
| 54 | 54 |
| 55 ThreadSafeGamepadChangeFetcher( | 55 ThreadSafeGamepadChangeFetcher( |
| 56 const ProcessGamepadChangesCallback& post_gamepad_changes, | 56 const ProcessGamepadChangesCallback& post_gamepad_changes, |
| 57 const CreateGamepadDataFetcherCallback& create_fetcher_callback, | 57 const CreateGamepadDataFetcherCallback& create_fetcher_callback, |
| 58 base::SingleThreadTaskRunner* task_runner) | 58 base::SingleThreadTaskRunner* task_runner) |
| 59 : process_gamepad_changes_(post_gamepad_changes), | 59 : process_gamepad_changes_(post_gamepad_changes), |
| 60 create_fetcher_callback_(create_fetcher_callback), | 60 create_fetcher_callback_(create_fetcher_callback), |
| 61 polling_task_runner_(task_runner), | 61 polling_task_runner_(task_runner), |
| 62 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 62 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 63 thread_checker_.DetachFromThread(); | 63 thread_checker_.DetachFromThread(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // Polls devices for new data and posts gamepad changes back to origin thread. | 113 // Polls devices for new data and posts gamepad changes back to origin thread. |
| 114 void PollOnPollingThread() { | 114 void PollOnPollingThread() { |
| 115 DCHECK(thread_checker_.CalledOnValidThread()); | 115 DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 | 116 |
| 117 has_poll_scheduled_ = false; | 117 has_poll_scheduled_ = false; |
| 118 if (!is_enabled_) | 118 if (!is_enabled_) |
| 119 return; | 119 return; |
| 120 | 120 |
| 121 DCHECK(fetcher_); | 121 DCHECK(fetcher_); |
| 122 | 122 |
| 123 blink::WebGamepads new_state = state_; | 123 device::Gamepads new_state = state_; |
| 124 fetcher_->GetGamepadData( | 124 fetcher_->GetGamepadData( |
| 125 false /* No hardware changed notification from the system */); | 125 false /* No hardware changed notification from the system */); |
| 126 | 126 |
| 127 for (size_t i = 0; i < blink::WebGamepads::kItemsLengthCap; ++i) { | 127 for (size_t i = 0; i < device::Gamepads::kItemsLengthCap; ++i) { |
| 128 device::PadState& pad_state = pad_states_.get()[i]; | 128 device::PadState& pad_state = pad_states_.get()[i]; |
| 129 | 129 |
| 130 // After querying the gamepad clear the state if it did not have it's | 130 // After querying the gamepad clear the state if it did not have it's |
| 131 // active | 131 // active |
| 132 // state updated but is still listed as being associated with a specific | 132 // state updated but is still listed as being associated with a specific |
| 133 // source. This indicates the gamepad is disconnected. | 133 // source. This indicates the gamepad is disconnected. |
| 134 if (!pad_state.active_state && | 134 if (!pad_state.active_state && |
| 135 pad_state.source != device::GAMEPAD_SOURCE_NONE) { | 135 pad_state.source != device::GAMEPAD_SOURCE_NONE) { |
| 136 ClearPadState(pad_state); | 136 ClearPadState(pad_state); |
| 137 } | 137 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 164 // Reference to task runner on polling thread. | 164 // Reference to task runner on polling thread. |
| 165 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner_; | 165 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner_; |
| 166 | 166 |
| 167 // Reference to task runner on origin thread. | 167 // Reference to task runner on origin thread. |
| 168 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; | 168 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
| 169 | 169 |
| 170 // Gamepad data fetcher used for querying gamepad devices. | 170 // Gamepad data fetcher used for querying gamepad devices. |
| 171 std::unique_ptr<device::GamepadDataFetcher> fetcher_; | 171 std::unique_ptr<device::GamepadDataFetcher> fetcher_; |
| 172 | 172 |
| 173 // The current state of all gamepads. | 173 // The current state of all gamepads. |
| 174 blink::WebGamepads state_; | 174 device::Gamepads state_; |
| 175 | 175 |
| 176 // True if a poll has been scheduled. | 176 // True if a poll has been scheduled. |
| 177 bool has_poll_scheduled_ = false; | 177 bool has_poll_scheduled_ = false; |
| 178 | 178 |
| 179 // True if the polling thread is paused. | 179 // True if the polling thread is paused. |
| 180 bool is_enabled_ = false; | 180 bool is_enabled_ = false; |
| 181 | 181 |
| 182 // ThreadChecker for the polling thread. | 182 // ThreadChecker for the polling thread. |
| 183 base::ThreadChecker thread_checker_; | 183 base::ThreadChecker thread_checker_; |
| 184 | 184 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 209 helper->AddFocusObserver(this); | 209 helper->AddFocusObserver(this); |
| 210 OnWindowFocused(helper->GetFocusedWindow(), nullptr); | 210 OnWindowFocused(helper->GetFocusedWindow(), nullptr); |
| 211 } | 211 } |
| 212 | 212 |
| 213 GamingSeat::~GamingSeat() { | 213 GamingSeat::~GamingSeat() { |
| 214 // Disable polling. Since ThreadSafeGamepadChangeFetcher are reference | 214 // Disable polling. Since ThreadSafeGamepadChangeFetcher are reference |
| 215 // counted, we can safely have it shut down after Gamepad has been destroyed. | 215 // counted, we can safely have it shut down after Gamepad has been destroyed. |
| 216 gamepad_change_fetcher_->EnablePolling(false); | 216 gamepad_change_fetcher_->EnablePolling(false); |
| 217 | 217 |
| 218 delegate_->OnGamingSeatDestroying(this); | 218 delegate_->OnGamingSeatDestroying(this); |
| 219 for (size_t i = 0; i < blink::WebGamepads::kItemsLengthCap; ++i) { | 219 for (size_t i = 0; i < device::Gamepads::kItemsLengthCap; ++i) { |
| 220 if (gamepad_delegates_[i]) { | 220 if (gamepad_delegates_[i]) { |
| 221 gamepad_delegates_[i]->OnRemoved(); | 221 gamepad_delegates_[i]->OnRemoved(); |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 WMHelper::GetInstance()->RemoveFocusObserver(this); | 224 WMHelper::GetInstance()->RemoveFocusObserver(this); |
| 225 } | 225 } |
| 226 | 226 |
| 227 //////////////////////////////////////////////////////////////////////////////// | 227 //////////////////////////////////////////////////////////////////////////////// |
| 228 // aura::client::FocusChangeObserver overrides: | 228 // aura::client::FocusChangeObserver overrides: |
| 229 | 229 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 242 | 242 |
| 243 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target); | 243 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target); |
| 244 | 244 |
| 245 gamepad_change_fetcher_->EnablePolling(focused); | 245 gamepad_change_fetcher_->EnablePolling(focused); |
| 246 } | 246 } |
| 247 | 247 |
| 248 //////////////////////////////////////////////////////////////////////////////// | 248 //////////////////////////////////////////////////////////////////////////////// |
| 249 // GamingSeat, private: | 249 // GamingSeat, private: |
| 250 | 250 |
| 251 void GamingSeat::ProcessGamepadChanges(int index, | 251 void GamingSeat::ProcessGamepadChanges(int index, |
| 252 const blink::WebGamepad new_pad) { | 252 const device::Gamepad new_pad) { |
| 253 DCHECK(thread_checker_.CalledOnValidThread()); | 253 DCHECK(thread_checker_.CalledOnValidThread()); |
| 254 bool send_frame = false; | 254 bool send_frame = false; |
| 255 | 255 |
| 256 blink::WebGamepad& pad_state = pad_state_.items[index]; | 256 device::Gamepad& pad_state = pad_state_.items[index]; |
| 257 // Update connection state. | 257 // Update connection state. |
| 258 GamepadDelegate* delegate = gamepad_delegates_[index]; | 258 GamepadDelegate* delegate = gamepad_delegates_[index]; |
| 259 if (new_pad.connected != pad_state.connected) { | 259 if (new_pad.connected != pad_state.connected) { |
| 260 // New pad is disconnected. | 260 // New pad is disconnected. |
| 261 if (!new_pad.connected) { | 261 if (!new_pad.connected) { |
| 262 // If gamepad is disconnected now, it should be connected before, then | 262 // If gamepad is disconnected now, it should be connected before, then |
| 263 // gamepad_delegate should not be null. | 263 // gamepad_delegate should not be null. |
| 264 DCHECK(delegate); | 264 DCHECK(delegate); |
| 265 delegate->OnRemoved(); | 265 delegate->OnRemoved(); |
| 266 gamepad_delegates_[index] = nullptr; | 266 gamepad_delegates_[index] = nullptr; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 delegate->OnButton(button_id, new_button.pressed, new_button.value); | 299 delegate->OnButton(button_id, new_button.pressed, new_button.value); |
| 300 } | 300 } |
| 301 } | 301 } |
| 302 if (send_frame) | 302 if (send_frame) |
| 303 delegate->OnFrame(); | 303 delegate->OnFrame(); |
| 304 | 304 |
| 305 pad_state = new_pad; | 305 pad_state = new_pad; |
| 306 } | 306 } |
| 307 | 307 |
| 308 } // namespace exo | 308 } // namespace exo |
| OLD | NEW |