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" | |
10 #include "base/bind.h" | 9 #include "base/bind.h" |
11 #include "base/location.h" | 10 #include "base/location.h" |
12 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
15 #include "components/exo/gamepad_delegate.h" | 14 #include "components/exo/gamepad_delegate.h" |
16 #include "components/exo/shell_surface.h" | 15 #include "components/exo/shell_surface.h" |
17 #include "components/exo/surface.h" | 16 #include "components/exo/surface.h" |
18 #include "device/gamepad/gamepad_data_fetcher.h" | 17 #include "device/gamepad/gamepad_data_fetcher.h" |
19 #include "device/gamepad/gamepad_pad_state_provider.h" | 18 #include "device/gamepad/gamepad_pad_state_provider.h" |
20 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h" | 19 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h" |
21 #include "ui/aura/client/focus_client.h" | |
22 #include "ui/aura/window.h" | 20 #include "ui/aura/window.h" |
23 | 21 |
24 namespace exo { | 22 namespace exo { |
25 namespace { | 23 namespace { |
26 | 24 |
27 constexpr double kGamepadButtonValueEpsilon = 0.001; | 25 constexpr double kGamepadButtonValueEpsilon = 0.001; |
28 bool GamepadButtonValuesAreEqual(double a, double b) { | 26 bool GamepadButtonValuesAreEqual(double a, double b) { |
29 return fabs(a - b) < kGamepadButtonValueEpsilon; | 27 return fabs(a - b) < kGamepadButtonValueEpsilon; |
30 } | 28 } |
31 | 29 |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 base::Bind(CreateGamepadPlatformDataFetcher)) {} | 199 base::Bind(CreateGamepadPlatformDataFetcher)) {} |
202 | 200 |
203 Gamepad::Gamepad(GamepadDelegate* delegate, | 201 Gamepad::Gamepad(GamepadDelegate* delegate, |
204 base::SingleThreadTaskRunner* polling_task_runner, | 202 base::SingleThreadTaskRunner* polling_task_runner, |
205 CreateGamepadDataFetcherCallback create_fetcher_callback) | 203 CreateGamepadDataFetcherCallback create_fetcher_callback) |
206 : delegate_(delegate), weak_factory_(this) { | 204 : delegate_(delegate), weak_factory_(this) { |
207 gamepad_change_fetcher_ = new ThreadSafeGamepadChangeFetcher( | 205 gamepad_change_fetcher_ = new ThreadSafeGamepadChangeFetcher( |
208 base::Bind(&Gamepad::ProcessGamepadChanges, weak_factory_.GetWeakPtr()), | 206 base::Bind(&Gamepad::ProcessGamepadChanges, weak_factory_.GetWeakPtr()), |
209 create_fetcher_callback, polling_task_runner); | 207 create_fetcher_callback, polling_task_runner); |
210 | 208 |
211 aura::client::FocusClient* focus_client = | 209 auto* helper = WMHelper::GetInstance(); |
212 aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); | 210 helper->AddFocusObserver(this); |
213 focus_client->AddObserver(this); | 211 OnWindowFocused(helper->GetFocusedWindow(), nullptr); |
214 OnWindowFocused(focus_client->GetFocusedWindow(), nullptr); | |
215 } | 212 } |
216 | 213 |
217 Gamepad::~Gamepad() { | 214 Gamepad::~Gamepad() { |
218 // Disable polling. Since ThreadSafeGamepadChangeFetcher are reference | 215 // Disable polling. Since ThreadSafeGamepadChangeFetcher are reference |
219 // counted, we can safely have it shut down after Gamepad has been destroyed. | 216 // counted, we can safely have it shut down after Gamepad has been destroyed. |
220 gamepad_change_fetcher_->EnablePolling(false); | 217 gamepad_change_fetcher_->EnablePolling(false); |
221 | 218 |
222 delegate_->OnGamepadDestroying(this); | 219 delegate_->OnGamepadDestroying(this); |
223 aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()) | 220 WMHelper::GetInstance()->RemoveFocusObserver(this); |
224 ->RemoveObserver(this); | |
225 } | 221 } |
226 | 222 |
227 //////////////////////////////////////////////////////////////////////////////// | 223 //////////////////////////////////////////////////////////////////////////////// |
228 // aura::client::FocusChangeObserver overrides: | 224 // aura::client::FocusChangeObserver overrides: |
229 | 225 |
230 void Gamepad::OnWindowFocused(aura::Window* gained_focus, | 226 void Gamepad::OnWindowFocused(aura::Window* gained_focus, |
231 aura::Window* lost_focus) { | 227 aura::Window* lost_focus) { |
232 DCHECK(thread_checker_.CalledOnValidThread()); | 228 DCHECK(thread_checker_.CalledOnValidThread()); |
233 Surface* target = nullptr; | 229 Surface* target = nullptr; |
234 if (gained_focus) { | 230 if (gained_focus) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 delegate_->OnButton(button_id, new_button.pressed, new_button.value); | 279 delegate_->OnButton(button_id, new_button.pressed, new_button.value); |
284 } | 280 } |
285 } | 281 } |
286 if (send_frame) | 282 if (send_frame) |
287 delegate_->OnFrame(); | 283 delegate_->OnFrame(); |
288 | 284 |
289 pad_state_ = new_pad; | 285 pad_state_ = new_pad; |
290 } | 286 } |
291 | 287 |
292 } // namespace exo | 288 } // namespace exo |
OLD | NEW |