OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/exo/gamepad.h" |
| 6 |
| 7 #include <cmath> |
| 8 |
| 9 #include "ash/shell.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/location.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/thread.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "components/exo/gamepad_delegate.h" |
| 16 #include "components/exo/shell_surface.h" |
| 17 #include "components/exo/surface.h" |
| 18 #include "device/gamepad/gamepad_data_fetcher.h" |
| 19 #include "device/gamepad/gamepad_platform_data_fetcher.h" |
| 20 #include "ui/aura/client/focus_client.h" |
| 21 #include "ui/aura/window.h" |
| 22 |
| 23 namespace exo { |
| 24 namespace { |
| 25 |
| 26 constexpr double kGamepadButtonValueEpsilon = 0.001; |
| 27 bool GamepadButtonValuesAreEqual(double a, double b) { |
| 28 return fabs(a - b) < kGamepadButtonValueEpsilon; |
| 29 } |
| 30 |
| 31 std::unique_ptr<device::GamepadDataFetcher> CreateGamepadPlatformDataFetcher() { |
| 32 return std::unique_ptr<device::GamepadDataFetcher>( |
| 33 new device::GamepadPlatformDataFetcher()); |
| 34 } |
| 35 |
| 36 // Time between gamepad polls in milliseconds. |
| 37 constexpr unsigned kPollingTimeIntervalMs = 16; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 //////////////////////////////////////////////////////////////////////////////// |
| 42 // Gamepad::ThreadSafeGamepadChangeFetcher |
| 43 |
| 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 |
| 46 // polling thread even if the Gamepad has been destroyed on the origin thread. |
| 47 class Gamepad::ThreadSafeGamepadChangeFetcher |
| 48 : public base::RefCountedThreadSafe< |
| 49 Gamepad::ThreadSafeGamepadChangeFetcher> { |
| 50 public: |
| 51 using ProcessGamepadChangesCallback = |
| 52 base::Callback<void(const blink::WebGamepad)>; |
| 53 |
| 54 ThreadSafeGamepadChangeFetcher( |
| 55 const ProcessGamepadChangesCallback& post_gamepad_changes, |
| 56 const CreateGamepadDataFetcherCallback& create_fetcher_callback, |
| 57 base::SingleThreadTaskRunner* task_runner) |
| 58 : process_gamepad_changes_(post_gamepad_changes), |
| 59 create_fetcher_callback_(create_fetcher_callback), |
| 60 polling_task_runner_(task_runner), |
| 61 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 62 thread_checker_.DetachFromThread(); |
| 63 } |
| 64 |
| 65 // Enable or disable gamepad polling. Can be called from any thread. |
| 66 void EnablePolling(bool enabled) { |
| 67 polling_task_runner_->PostTask( |
| 68 FROM_HERE, |
| 69 base::Bind( |
| 70 &ThreadSafeGamepadChangeFetcher::EnablePollingOnPollingThread, |
| 71 make_scoped_refptr(this), enabled)); |
| 72 } |
| 73 |
| 74 private: |
| 75 friend class base::RefCountedThreadSafe<ThreadSafeGamepadChangeFetcher>; |
| 76 |
| 77 virtual ~ThreadSafeGamepadChangeFetcher() {} |
| 78 |
| 79 // Enables or disables polling. |
| 80 void EnablePollingOnPollingThread(bool enabled) { |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); |
| 82 is_enabled_ = enabled; |
| 83 |
| 84 if (is_enabled_) { |
| 85 if (!fetcher_) { |
| 86 fetcher_ = create_fetcher_callback_.Run(); |
| 87 DCHECK(fetcher_); |
| 88 } |
| 89 SchedulePollOnPollingThread(); |
| 90 } else { |
| 91 fetcher_.reset(); |
| 92 } |
| 93 } |
| 94 |
| 95 // Schedules the next poll on the polling thread. |
| 96 void SchedulePollOnPollingThread() { |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); |
| 98 DCHECK(fetcher_); |
| 99 |
| 100 if (!is_enabled_ || has_poll_scheduled_) |
| 101 return; |
| 102 |
| 103 has_poll_scheduled_ = true; |
| 104 polling_task_runner_->PostDelayedTask( |
| 105 FROM_HERE, |
| 106 base::Bind(&ThreadSafeGamepadChangeFetcher::PollOnPollingThread, |
| 107 make_scoped_refptr(this)), |
| 108 base::TimeDelta::FromMilliseconds(kPollingTimeIntervalMs)); |
| 109 } |
| 110 |
| 111 // Polls devices for new data and posts gamepad changes back to origin thread. |
| 112 void PollOnPollingThread() { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 |
| 115 has_poll_scheduled_ = false; |
| 116 if (!is_enabled_) |
| 117 return; |
| 118 |
| 119 DCHECK(fetcher_); |
| 120 |
| 121 blink::WebGamepads new_state = state_; |
| 122 fetcher_->GetGamepadData(&new_state, false); |
| 123 |
| 124 if (std::max(new_state.length, state_.length) > 0) { |
| 125 if (new_state.items[0].connected != state_.items[0].connected || |
| 126 new_state.items[0].timestamp > state_.items[0].timestamp) { |
| 127 origin_task_runner_->PostTask( |
| 128 FROM_HERE, |
| 129 base::Bind(process_gamepad_changes_, new_state.items[0])); |
| 130 } |
| 131 } |
| 132 |
| 133 state_ = new_state; |
| 134 SchedulePollOnPollingThread(); |
| 135 } |
| 136 |
| 137 // Callback to ProcessGamepadChanges using weak reference to Gamepad. |
| 138 ProcessGamepadChangesCallback process_gamepad_changes_; |
| 139 |
| 140 // Callback method to create a gamepad data fetcher. |
| 141 CreateGamepadDataFetcherCallback create_fetcher_callback_; |
| 142 |
| 143 // Reference to task runner on polling thread. |
| 144 base::SingleThreadTaskRunner* polling_task_runner_; |
| 145 |
| 146 // Reference to task runner on origin thread. |
| 147 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
| 148 |
| 149 // Gamepad data fetcher used for querying gamepad devices. |
| 150 std::unique_ptr<device::GamepadDataFetcher> fetcher_; |
| 151 |
| 152 // The current state of all gamepads. |
| 153 blink::WebGamepads state_; |
| 154 |
| 155 // True if a poll has been scheduled. |
| 156 bool has_poll_scheduled_ = false; |
| 157 |
| 158 // True if the polling thread is paused. |
| 159 bool is_enabled_ = false; |
| 160 |
| 161 // ThreadChecker for the polling thread. |
| 162 base::ThreadChecker thread_checker_; |
| 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(ThreadSafeGamepadChangeFetcher); |
| 165 }; |
| 166 |
| 167 //////////////////////////////////////////////////////////////////////////////// |
| 168 // Gamepad, public: |
| 169 |
| 170 Gamepad::Gamepad(GamepadDelegate* delegate, |
| 171 base::SingleThreadTaskRunner* polling_task_runner) |
| 172 : Gamepad(delegate, |
| 173 polling_task_runner, |
| 174 base::Bind(CreateGamepadPlatformDataFetcher)) {} |
| 175 |
| 176 Gamepad::Gamepad(GamepadDelegate* delegate, |
| 177 base::SingleThreadTaskRunner* polling_task_runner, |
| 178 CreateGamepadDataFetcherCallback create_fetcher_callback) |
| 179 : delegate_(delegate), weak_factory_(this) { |
| 180 gamepad_change_fetcher_ = new ThreadSafeGamepadChangeFetcher( |
| 181 base::Bind(&Gamepad::ProcessGamepadChanges, weak_factory_.GetWeakPtr()), |
| 182 create_fetcher_callback, polling_task_runner); |
| 183 |
| 184 aura::client::FocusClient* focus_client = |
| 185 aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); |
| 186 focus_client->AddObserver(this); |
| 187 OnWindowFocused(focus_client->GetFocusedWindow(), nullptr); |
| 188 } |
| 189 |
| 190 Gamepad::~Gamepad() { |
| 191 // Disable polling. Since ThreadSafeGamepadChangeFetcher are reference |
| 192 // counted, we can safely have it shut down after Gamepad has been destroyed. |
| 193 gamepad_change_fetcher_->EnablePolling(false); |
| 194 |
| 195 delegate_->OnGamepadDestroying(this); |
| 196 aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()) |
| 197 ->RemoveObserver(this); |
| 198 } |
| 199 |
| 200 //////////////////////////////////////////////////////////////////////////////// |
| 201 // aura::client::FocusChangeObserver overrides: |
| 202 |
| 203 void Gamepad::OnWindowFocused(aura::Window* gained_focus, |
| 204 aura::Window* lost_focus) { |
| 205 DCHECK(thread_checker_.CalledOnValidThread()); |
| 206 Surface* target = nullptr; |
| 207 if (gained_focus) { |
| 208 target = Surface::AsSurface(gained_focus); |
| 209 if (!target) { |
| 210 aura::Window* top_level_window = gained_focus->GetToplevelWindow(); |
| 211 if (top_level_window) |
| 212 target = ShellSurface::GetMainSurface(top_level_window); |
| 213 } |
| 214 } |
| 215 |
| 216 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target); |
| 217 gamepad_change_fetcher_->EnablePolling(focused); |
| 218 } |
| 219 |
| 220 //////////////////////////////////////////////////////////////////////////////// |
| 221 // Gamepad, private: |
| 222 |
| 223 void Gamepad::ProcessGamepadChanges(const blink::WebGamepad new_pad) { |
| 224 DCHECK(thread_checker_.CalledOnValidThread()); |
| 225 bool send_frame = false; |
| 226 |
| 227 // Update connection state. |
| 228 if (new_pad.connected != pad_state_.connected) { |
| 229 delegate_->OnStateChange(new_pad.connected); |
| 230 } |
| 231 |
| 232 if (!new_pad.connected || new_pad.timestamp <= pad_state_.timestamp) { |
| 233 pad_state_ = new_pad; |
| 234 return; |
| 235 } |
| 236 |
| 237 // Notify delegate of updated axes. |
| 238 for (size_t axis = 0; |
| 239 axis < std::max(pad_state_.axesLength, new_pad.axesLength); ++axis) { |
| 240 if (!GamepadButtonValuesAreEqual(new_pad.axes[axis], |
| 241 pad_state_.axes[axis])) { |
| 242 send_frame = true; |
| 243 delegate_->OnAxis(axis, new_pad.axes[axis]); |
| 244 } |
| 245 } |
| 246 |
| 247 // Notify delegate of updated buttons. |
| 248 for (size_t button_id = 0; |
| 249 button_id < std::max(pad_state_.buttonsLength, new_pad.buttonsLength); |
| 250 ++button_id) { |
| 251 auto button = pad_state_.buttons[button_id]; |
| 252 auto new_button = new_pad.buttons[button_id]; |
| 253 if (button.pressed != new_button.pressed || |
| 254 !GamepadButtonValuesAreEqual(button.value, new_button.value)) { |
| 255 send_frame = true; |
| 256 delegate_->OnButton(button_id, new_button.pressed, new_button.value); |
| 257 } |
| 258 } |
| 259 if (send_frame) |
| 260 delegate_->OnFrame(); |
| 261 |
| 262 pad_state_ = new_pad; |
| 263 } |
| 264 |
| 265 } // namespace exo |
OLD | NEW |