| 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/gamepad.h" | 5 #include "components/exo/gamepad.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 class Gamepad::ThreadSafeGamepadChangeFetcher | 47 class Gamepad::ThreadSafeGamepadChangeFetcher |
| 48 : public base::RefCountedThreadSafe< | 48 : public base::RefCountedThreadSafe< |
| 49 Gamepad::ThreadSafeGamepadChangeFetcher> { | 49 Gamepad::ThreadSafeGamepadChangeFetcher> { |
| 50 public: | 50 public: |
| 51 using ProcessGamepadChangesCallback = | 51 using ProcessGamepadChangesCallback = |
| 52 base::Callback<void(const blink::WebGamepad)>; | 52 base::Callback<void(const blink::WebGamepad)>; |
| 53 | 53 |
| 54 ThreadSafeGamepadChangeFetcher( | 54 ThreadSafeGamepadChangeFetcher( |
| 55 const ProcessGamepadChangesCallback& post_gamepad_changes, | 55 const ProcessGamepadChangesCallback& post_gamepad_changes, |
| 56 const CreateGamepadDataFetcherCallback& create_fetcher_callback, | 56 const CreateGamepadDataFetcherCallback& create_fetcher_callback, |
| 57 base::SingleThreadTaskRunner* task_runner) | 57 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 58 : process_gamepad_changes_(post_gamepad_changes), | 58 : process_gamepad_changes_(post_gamepad_changes), |
| 59 create_fetcher_callback_(create_fetcher_callback), | 59 create_fetcher_callback_(create_fetcher_callback), |
| 60 polling_task_runner_(task_runner), | 60 polling_task_runner_(task_runner), |
| 61 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 61 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 62 thread_checker_.DetachFromThread(); | 62 thread_checker_.DetachFromThread(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Enable or disable gamepad polling. Can be called from any thread. | 65 // Enable or disable gamepad polling. Can be called from any thread. |
| 66 void EnablePolling(bool enabled) { | 66 void EnablePolling(bool enabled) { |
| 67 polling_task_runner_->PostTask( | 67 polling_task_runner_->PostTask( |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 SchedulePollOnPollingThread(); | 134 SchedulePollOnPollingThread(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Callback to ProcessGamepadChanges using weak reference to Gamepad. | 137 // Callback to ProcessGamepadChanges using weak reference to Gamepad. |
| 138 ProcessGamepadChangesCallback process_gamepad_changes_; | 138 ProcessGamepadChangesCallback process_gamepad_changes_; |
| 139 | 139 |
| 140 // Callback method to create a gamepad data fetcher. | 140 // Callback method to create a gamepad data fetcher. |
| 141 CreateGamepadDataFetcherCallback create_fetcher_callback_; | 141 CreateGamepadDataFetcherCallback create_fetcher_callback_; |
| 142 | 142 |
| 143 // Reference to task runner on polling thread. | 143 // Reference to task runner on polling thread. |
| 144 base::SingleThreadTaskRunner* polling_task_runner_; | 144 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner_; |
| 145 | 145 |
| 146 // Reference to task runner on origin thread. | 146 // Reference to task runner on origin thread. |
| 147 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; | 147 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
| 148 | 148 |
| 149 // Gamepad data fetcher used for querying gamepad devices. | 149 // Gamepad data fetcher used for querying gamepad devices. |
| 150 std::unique_ptr<device::GamepadDataFetcher> fetcher_; | 150 std::unique_ptr<device::GamepadDataFetcher> fetcher_; |
| 151 | 151 |
| 152 // The current state of all gamepads. | 152 // The current state of all gamepads. |
| 153 blink::WebGamepads state_; | 153 blink::WebGamepads state_; |
| 154 | 154 |
| 155 // True if a poll has been scheduled. | 155 // True if a poll has been scheduled. |
| 156 bool has_poll_scheduled_ = false; | 156 bool has_poll_scheduled_ = false; |
| 157 | 157 |
| 158 // True if the polling thread is paused. | 158 // True if the polling thread is paused. |
| 159 bool is_enabled_ = false; | 159 bool is_enabled_ = false; |
| 160 | 160 |
| 161 // ThreadChecker for the polling thread. | 161 // ThreadChecker for the polling thread. |
| 162 base::ThreadChecker thread_checker_; | 162 base::ThreadChecker thread_checker_; |
| 163 | 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(ThreadSafeGamepadChangeFetcher); | 164 DISALLOW_COPY_AND_ASSIGN(ThreadSafeGamepadChangeFetcher); |
| 165 }; | 165 }; |
| 166 | 166 |
| 167 //////////////////////////////////////////////////////////////////////////////// | 167 //////////////////////////////////////////////////////////////////////////////// |
| 168 // Gamepad, public: | 168 // Gamepad, public: |
| 169 | 169 |
| 170 Gamepad::Gamepad(GamepadDelegate* delegate, | 170 Gamepad::Gamepad( |
| 171 base::SingleThreadTaskRunner* polling_task_runner) | 171 GamepadDelegate* delegate, |
| 172 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner) |
| 172 : Gamepad(delegate, | 173 : Gamepad(delegate, |
| 173 polling_task_runner, | 174 polling_task_runner, |
| 174 base::Bind(CreateGamepadPlatformDataFetcher)) {} | 175 base::Bind(CreateGamepadPlatformDataFetcher)) {} |
| 175 | 176 |
| 176 Gamepad::Gamepad(GamepadDelegate* delegate, | 177 Gamepad::Gamepad( |
| 177 base::SingleThreadTaskRunner* polling_task_runner, | 178 GamepadDelegate* delegate, |
| 178 CreateGamepadDataFetcherCallback create_fetcher_callback) | 179 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner, |
| 180 CreateGamepadDataFetcherCallback create_fetcher_callback) |
| 179 : delegate_(delegate), weak_factory_(this) { | 181 : delegate_(delegate), weak_factory_(this) { |
| 180 gamepad_change_fetcher_ = new ThreadSafeGamepadChangeFetcher( | 182 gamepad_change_fetcher_ = new ThreadSafeGamepadChangeFetcher( |
| 181 base::Bind(&Gamepad::ProcessGamepadChanges, weak_factory_.GetWeakPtr()), | 183 base::Bind(&Gamepad::ProcessGamepadChanges, weak_factory_.GetWeakPtr()), |
| 182 create_fetcher_callback, polling_task_runner); | 184 create_fetcher_callback, polling_task_runner); |
| 183 | 185 |
| 184 aura::client::FocusClient* focus_client = | 186 aura::client::FocusClient* focus_client = |
| 185 aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); | 187 aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); |
| 186 focus_client->AddObserver(this); | 188 focus_client->AddObserver(this); |
| 187 OnWindowFocused(focus_client->GetFocusedWindow(), nullptr); | 189 OnWindowFocused(focus_client->GetFocusedWindow(), nullptr); |
| 188 } | 190 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 delegate_->OnButton(button_id, new_button.pressed, new_button.value); | 258 delegate_->OnButton(button_id, new_button.pressed, new_button.value); |
| 257 } | 259 } |
| 258 } | 260 } |
| 259 if (send_frame) | 261 if (send_frame) |
| 260 delegate_->OnFrame(); | 262 delegate_->OnFrame(); |
| 261 | 263 |
| 262 pad_state_ = new_pad; | 264 pad_state_ = new_pad; |
| 263 } | 265 } |
| 264 | 266 |
| 265 } // namespace exo | 267 } // namespace exo |
| OLD | NEW |