| 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" | |
| 11 #include "base/single_thread_task_runner.h" | |
| 12 #include "base/threading/thread.h" | |
| 13 #include "base/threading/thread_task_runner_handle.h" | |
| 14 #include "components/exo/gamepad_delegate.h" | 10 #include "components/exo/gamepad_delegate.h" |
| 15 #include "components/exo/gaming_seat_delegate.h" | 11 #include "components/exo/gaming_seat_delegate.h" |
| 16 #include "components/exo/shell_surface.h" | 12 #include "components/exo/shell_surface.h" |
| 17 #include "components/exo/surface.h" | 13 #include "components/exo/surface.h" |
| 18 #include "device/gamepad/gamepad_data_fetcher.h" | 14 #include "device/gamepad/gamepad_data_fetcher.h" |
| 19 #include "device/gamepad/gamepad_pad_state_provider.h" | 15 #include "device/gamepad/gamepad_pad_state_provider.h" |
| 20 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h" | 16 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h" |
| 21 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
| 22 | 18 |
| 23 namespace exo { | 19 namespace exo { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 53 base::Callback<void(int index, const device::Gamepad)>; | 49 base::Callback<void(int index, const device::Gamepad)>; |
| 54 | 50 |
| 55 ThreadSafeGamepadChangeFetcher( | 51 ThreadSafeGamepadChangeFetcher( |
| 56 const ProcessGamepadChangesCallback& post_gamepad_changes, | 52 const ProcessGamepadChangesCallback& post_gamepad_changes, |
| 57 const CreateGamepadDataFetcherCallback& create_fetcher_callback, | 53 const CreateGamepadDataFetcherCallback& create_fetcher_callback, |
| 58 base::SingleThreadTaskRunner* task_runner) | 54 base::SingleThreadTaskRunner* task_runner) |
| 59 : process_gamepad_changes_(post_gamepad_changes), | 55 : process_gamepad_changes_(post_gamepad_changes), |
| 60 create_fetcher_callback_(create_fetcher_callback), | 56 create_fetcher_callback_(create_fetcher_callback), |
| 61 polling_task_runner_(task_runner), | 57 polling_task_runner_(task_runner), |
| 62 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 58 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 63 thread_checker_.DetachFromThread(); | 59 DETACH_FROM_THREAD(thread_checker_); |
| 64 } | 60 } |
| 65 | 61 |
| 66 // Enable or disable gamepad polling. Can be called from any thread. | 62 // Enable or disable gamepad polling. Can be called from any thread. |
| 67 void EnablePolling(bool enabled) { | 63 void EnablePolling(bool enabled) { |
| 68 polling_task_runner_->PostTask( | 64 polling_task_runner_->PostTask( |
| 69 FROM_HERE, | 65 FROM_HERE, |
| 70 base::Bind( | 66 base::Bind( |
| 71 &ThreadSafeGamepadChangeFetcher::EnablePollingOnPollingThread, | 67 &ThreadSafeGamepadChangeFetcher::EnablePollingOnPollingThread, |
| 72 make_scoped_refptr(this), enabled)); | 68 make_scoped_refptr(this), enabled)); |
| 73 } | 69 } |
| 74 | 70 |
| 75 private: | 71 private: |
| 76 friend class base::RefCountedThreadSafe<ThreadSafeGamepadChangeFetcher>; | 72 friend class base::RefCountedThreadSafe<ThreadSafeGamepadChangeFetcher>; |
| 77 | 73 |
| 78 ~ThreadSafeGamepadChangeFetcher() override {} | 74 ~ThreadSafeGamepadChangeFetcher() override {} |
| 79 | 75 |
| 80 // Enables or disables polling. | 76 // Enables or disables polling. |
| 81 void EnablePollingOnPollingThread(bool enabled) { | 77 void EnablePollingOnPollingThread(bool enabled) { |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); | 78 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 83 is_enabled_ = enabled; | 79 is_enabled_ = enabled; |
| 84 | 80 |
| 85 if (is_enabled_) { | 81 if (is_enabled_) { |
| 86 if (!fetcher_) { | 82 if (!fetcher_) { |
| 87 fetcher_ = create_fetcher_callback_.Run(); | 83 fetcher_ = create_fetcher_callback_.Run(); |
| 88 InitializeDataFetcher(fetcher_.get()); | 84 InitializeDataFetcher(fetcher_.get()); |
| 89 DCHECK(fetcher_); | 85 DCHECK(fetcher_); |
| 90 } | 86 } |
| 91 SchedulePollOnPollingThread(); | 87 SchedulePollOnPollingThread(); |
| 92 } else { | 88 } else { |
| 93 fetcher_.reset(); | 89 fetcher_.reset(); |
| 94 } | 90 } |
| 95 } | 91 } |
| 96 | 92 |
| 97 // Schedules the next poll on the polling thread. | 93 // Schedules the next poll on the polling thread. |
| 98 void SchedulePollOnPollingThread() { | 94 void SchedulePollOnPollingThread() { |
| 99 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 100 DCHECK(fetcher_); | 96 DCHECK(fetcher_); |
| 101 | 97 |
| 102 if (!is_enabled_ || has_poll_scheduled_) | 98 if (!is_enabled_ || has_poll_scheduled_) |
| 103 return; | 99 return; |
| 104 | 100 |
| 105 has_poll_scheduled_ = true; | 101 has_poll_scheduled_ = true; |
| 106 polling_task_runner_->PostDelayedTask( | 102 polling_task_runner_->PostDelayedTask( |
| 107 FROM_HERE, | 103 FROM_HERE, |
| 108 base::Bind(&ThreadSafeGamepadChangeFetcher::PollOnPollingThread, | 104 base::Bind(&ThreadSafeGamepadChangeFetcher::PollOnPollingThread, |
| 109 make_scoped_refptr(this)), | 105 make_scoped_refptr(this)), |
| 110 base::TimeDelta::FromMilliseconds(kPollingTimeIntervalMs)); | 106 base::TimeDelta::FromMilliseconds(kPollingTimeIntervalMs)); |
| 111 } | 107 } |
| 112 | 108 |
| 113 // Polls devices for new data and posts gamepad changes back to origin thread. | 109 // Polls devices for new data and posts gamepad changes back to origin thread. |
| 114 void PollOnPollingThread() { | 110 void PollOnPollingThread() { |
| 115 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 116 | 112 |
| 117 has_poll_scheduled_ = false; | 113 has_poll_scheduled_ = false; |
| 118 if (!is_enabled_) | 114 if (!is_enabled_) |
| 119 return; | 115 return; |
| 120 | 116 |
| 121 DCHECK(fetcher_); | 117 DCHECK(fetcher_); |
| 122 | 118 |
| 123 device::Gamepads new_state = state_; | 119 device::Gamepads new_state = state_; |
| 124 fetcher_->GetGamepadData( | 120 fetcher_->GetGamepadData( |
| 125 false /* No hardware changed notification from the system */); | 121 false /* No hardware changed notification from the system */); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // The current state of all gamepads. | 169 // The current state of all gamepads. |
| 174 device::Gamepads state_; | 170 device::Gamepads state_; |
| 175 | 171 |
| 176 // True if a poll has been scheduled. | 172 // True if a poll has been scheduled. |
| 177 bool has_poll_scheduled_ = false; | 173 bool has_poll_scheduled_ = false; |
| 178 | 174 |
| 179 // True if the polling thread is paused. | 175 // True if the polling thread is paused. |
| 180 bool is_enabled_ = false; | 176 bool is_enabled_ = false; |
| 181 | 177 |
| 182 // ThreadChecker for the polling thread. | 178 // ThreadChecker for the polling thread. |
| 183 base::ThreadChecker thread_checker_; | 179 THREAD_CHECKER(thread_checker_); |
| 184 | 180 |
| 185 DISALLOW_COPY_AND_ASSIGN(ThreadSafeGamepadChangeFetcher); | 181 DISALLOW_COPY_AND_ASSIGN(ThreadSafeGamepadChangeFetcher); |
| 186 }; | 182 }; |
| 187 | 183 |
| 188 //////////////////////////////////////////////////////////////////////////////// | 184 //////////////////////////////////////////////////////////////////////////////// |
| 189 // GamingSeat, public: | 185 // GamingSeat, public: |
| 190 | 186 |
| 191 GamingSeat::GamingSeat(GamingSeatDelegate* gaming_seat_delegate, | 187 GamingSeat::GamingSeat(GamingSeatDelegate* gaming_seat_delegate, |
| 192 base::SingleThreadTaskRunner* polling_task_runner) | 188 base::SingleThreadTaskRunner* polling_task_runner) |
| 193 : GamingSeat(gaming_seat_delegate, | |
| 194 polling_task_runner, | |
| 195 base::Bind(CreateGamepadPlatformDataFetcher)) {} | |
| 196 | |
| 197 GamingSeat::GamingSeat(GamingSeatDelegate* gaming_seat_delegate, | |
| 198 base::SingleThreadTaskRunner* polling_task_runner, | |
| 199 CreateGamepadDataFetcherCallback create_fetcher_callback) | |
| 200 : delegate_(gaming_seat_delegate), | 189 : delegate_(gaming_seat_delegate), |
| 201 gamepad_delegates_{nullptr}, | 190 gamepad_delegates_{nullptr}, |
| 202 weak_ptr_factory_(this) { | 191 weak_ptr_factory_(this) { |
| 203 gamepad_change_fetcher_ = new ThreadSafeGamepadChangeFetcher( | 192 gamepad_change_fetcher_ = new ThreadSafeGamepadChangeFetcher( |
| 204 base::Bind(&GamingSeat::ProcessGamepadChanges, | 193 base::Bind(&GamingSeat::ProcessGamepadChanges, |
| 205 weak_ptr_factory_.GetWeakPtr()), | 194 weak_ptr_factory_.GetWeakPtr()), |
| 206 create_fetcher_callback, polling_task_runner); | 195 base::Bind(CreateGamepadPlatformDataFetcher), polling_task_runner); |
| 207 | 196 |
| 208 auto* helper = WMHelper::GetInstance(); | 197 auto* helper = WMHelper::GetInstance(); |
| 209 helper->AddFocusObserver(this); | 198 helper->AddFocusObserver(this); |
| 210 OnWindowFocused(helper->GetFocusedWindow(), nullptr); | 199 OnWindowFocused(helper->GetFocusedWindow(), nullptr); |
| 211 } | 200 } |
| 212 | 201 |
| 213 GamingSeat::~GamingSeat() { | 202 GamingSeat::~GamingSeat() { |
| 214 // Disable polling. Since ThreadSafeGamepadChangeFetcher are reference | 203 // Disable polling. Since ThreadSafeGamepadChangeFetcher are reference |
| 215 // counted, we can safely have it shut down after Gamepad has been destroyed. | 204 // counted, we can safely have it shut down after Gamepad has been destroyed. |
| 216 gamepad_change_fetcher_->EnablePolling(false); | 205 gamepad_change_fetcher_->EnablePolling(false); |
| 217 | 206 |
| 218 delegate_->OnGamingSeatDestroying(this); | 207 delegate_->OnGamingSeatDestroying(this); |
| 219 for (size_t i = 0; i < device::Gamepads::kItemsLengthCap; ++i) { | 208 for (size_t i = 0; i < device::Gamepads::kItemsLengthCap; ++i) { |
| 220 if (gamepad_delegates_[i]) { | 209 if (gamepad_delegates_[i]) { |
| 221 gamepad_delegates_[i]->OnRemoved(); | 210 gamepad_delegates_[i]->OnRemoved(); |
| 222 } | 211 } |
| 223 } | 212 } |
| 224 WMHelper::GetInstance()->RemoveFocusObserver(this); | 213 WMHelper::GetInstance()->RemoveFocusObserver(this); |
| 225 } | 214 } |
| 226 | 215 |
| 227 //////////////////////////////////////////////////////////////////////////////// | 216 //////////////////////////////////////////////////////////////////////////////// |
| 228 // aura::client::FocusChangeObserver overrides: | 217 // aura::client::FocusChangeObserver overrides: |
| 229 | 218 |
| 230 void GamingSeat::OnWindowFocused(aura::Window* gained_focus, | 219 void GamingSeat::OnWindowFocused(aura::Window* gained_focus, |
| 231 aura::Window* lost_focus) { | 220 aura::Window* lost_focus) { |
| 232 DCHECK(thread_checker_.CalledOnValidThread()); | 221 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 233 Surface* target = nullptr; | 222 Surface* target = nullptr; |
| 234 if (gained_focus) { | 223 if (gained_focus) { |
| 235 target = Surface::AsSurface(gained_focus); | 224 target = Surface::AsSurface(gained_focus); |
| 236 if (!target) { | 225 if (!target) { |
| 237 aura::Window* top_level_window = gained_focus->GetToplevelWindow(); | 226 aura::Window* top_level_window = gained_focus->GetToplevelWindow(); |
| 238 if (top_level_window) | 227 if (top_level_window) |
| 239 target = ShellSurface::GetMainSurface(top_level_window); | 228 target = ShellSurface::GetMainSurface(top_level_window); |
| 240 } | 229 } |
| 241 } | 230 } |
| 242 | 231 |
| 243 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target); | 232 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target); |
| 244 | 233 |
| 245 gamepad_change_fetcher_->EnablePolling(focused); | 234 gamepad_change_fetcher_->EnablePolling(focused); |
| 246 } | 235 } |
| 247 | 236 |
| 248 //////////////////////////////////////////////////////////////////////////////// | 237 //////////////////////////////////////////////////////////////////////////////// |
| 249 // GamingSeat, private: | 238 // GamingSeat, private: |
| 250 | 239 |
| 251 void GamingSeat::ProcessGamepadChanges(int index, | 240 void GamingSeat::ProcessGamepadChanges(int index, |
| 252 const device::Gamepad new_pad) { | 241 const device::Gamepad new_pad) { |
| 253 DCHECK(thread_checker_.CalledOnValidThread()); | 242 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 254 bool send_frame = false; | 243 bool send_frame = false; |
| 255 | 244 |
| 256 device::Gamepad& pad_state = pad_state_.items[index]; | 245 device::Gamepad& pad_state = pad_state_.items[index]; |
| 257 // Update connection state. | 246 // Update connection state. |
| 258 GamepadDelegate* delegate = gamepad_delegates_[index]; | 247 GamepadDelegate* delegate = gamepad_delegates_[index]; |
| 259 if (new_pad.connected != pad_state.connected) { | 248 if (new_pad.connected != pad_state.connected) { |
| 260 // New pad is disconnected. | 249 // New pad is disconnected. |
| 261 if (!new_pad.connected) { | 250 if (!new_pad.connected) { |
| 262 // If gamepad is disconnected now, it should be connected before, then | 251 // If gamepad is disconnected now, it should be connected before, then |
| 263 // gamepad_delegate should not be null. | 252 // gamepad_delegate should not be null. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 delegate->OnButton(button_id, new_button.pressed, new_button.value); | 288 delegate->OnButton(button_id, new_button.pressed, new_button.value); |
| 300 } | 289 } |
| 301 } | 290 } |
| 302 if (send_frame) | 291 if (send_frame) |
| 303 delegate->OnFrame(); | 292 delegate->OnFrame(); |
| 304 | 293 |
| 305 pad_state = new_pad; | 294 pad_state = new_pad; |
| 306 } | 295 } |
| 307 | 296 |
| 308 } // namespace exo | 297 } // namespace exo |
| OLD | NEW |