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