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/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" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "components/exo/gamepad_delegate.h" | 15 #include "components/exo/gamepad_delegate.h" |
| 16 #include "components/exo/shell_surface.h" | 16 #include "components/exo/shell_surface.h" |
| 17 #include "components/exo/surface.h" | 17 #include "components/exo/surface.h" |
| 18 #include "device/gamepad/gamepad_data_fetcher.h" | 18 #include "device/gamepad/gamepad_data_fetcher.h" |
| 19 #include "device/gamepad/gamepad_platform_data_fetcher.h" | 19 #include "device/gamepad/gamepad_pad_state_provider.h" |
| 20 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h" | |
| 20 #include "ui/aura/client/focus_client.h" | 21 #include "ui/aura/client/focus_client.h" |
| 21 #include "ui/aura/window.h" | 22 #include "ui/aura/window.h" |
| 22 | 23 |
| 23 namespace exo { | 24 namespace exo { |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 constexpr double kGamepadButtonValueEpsilon = 0.001; | 27 constexpr double kGamepadButtonValueEpsilon = 0.001; |
| 27 bool GamepadButtonValuesAreEqual(double a, double b) { | 28 bool GamepadButtonValuesAreEqual(double a, double b) { |
| 28 return fabs(a - b) < kGamepadButtonValueEpsilon; | 29 return fabs(a - b) < kGamepadButtonValueEpsilon; |
| 29 } | 30 } |
| 30 | 31 |
| 31 std::unique_ptr<device::GamepadDataFetcher> CreateGamepadPlatformDataFetcher() { | 32 std::unique_ptr<device::GamepadDataFetcher> CreateGamepadPlatformDataFetcher() { |
| 32 return std::unique_ptr<device::GamepadDataFetcher>( | 33 return std::unique_ptr<device::GamepadDataFetcher>( |
| 33 new device::GamepadPlatformDataFetcher()); | 34 new device::GamepadPlatformDataFetcherLinux()); |
| 34 } | 35 } |
| 35 | 36 |
| 36 // Time between gamepad polls in milliseconds. | 37 // Time between gamepad polls in milliseconds. |
| 37 constexpr unsigned kPollingTimeIntervalMs = 16; | 38 constexpr unsigned kPollingTimeIntervalMs = 16; |
| 38 | 39 |
| 39 } // namespace | 40 } // namespace |
| 40 | 41 |
| 41 //////////////////////////////////////////////////////////////////////////////// | 42 //////////////////////////////////////////////////////////////////////////////// |
| 42 // Gamepad::ThreadSafeGamepadChangeFetcher | 43 // Gamepad::ThreadSafeGamepadChangeFetcher |
| 43 | 44 |
| 44 // Implements all methods and resources running on the polling thread. | 45 // 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 // 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 // polling thread even if the Gamepad has been destroyed on the origin thread. |
| 47 class Gamepad::ThreadSafeGamepadChangeFetcher | 48 class Gamepad::ThreadSafeGamepadChangeFetcher |
| 48 : public base::RefCountedThreadSafe< | 49 : public device::GamepadPadStateProvider, |
| 50 public base::RefCountedThreadSafe< | |
| 49 Gamepad::ThreadSafeGamepadChangeFetcher> { | 51 Gamepad::ThreadSafeGamepadChangeFetcher> { |
| 50 public: | 52 public: |
| 51 using ProcessGamepadChangesCallback = | 53 using ProcessGamepadChangesCallback = |
| 52 base::Callback<void(const blink::WebGamepad)>; | 54 base::Callback<void(const blink::WebGamepad)>; |
| 53 | 55 |
| 54 ThreadSafeGamepadChangeFetcher( | 56 ThreadSafeGamepadChangeFetcher( |
| 55 const ProcessGamepadChangesCallback& post_gamepad_changes, | 57 const ProcessGamepadChangesCallback& post_gamepad_changes, |
| 56 const CreateGamepadDataFetcherCallback& create_fetcher_callback, | 58 const CreateGamepadDataFetcherCallback& create_fetcher_callback, |
| 57 base::SingleThreadTaskRunner* task_runner) | 59 base::SingleThreadTaskRunner* task_runner) |
| 58 : process_gamepad_changes_(post_gamepad_changes), | 60 : process_gamepad_changes_(post_gamepad_changes), |
| 59 create_fetcher_callback_(create_fetcher_callback), | 61 create_fetcher_callback_(create_fetcher_callback), |
| 60 polling_task_runner_(task_runner), | 62 polling_task_runner_(task_runner), |
| 61 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 63 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 62 thread_checker_.DetachFromThread(); | 64 thread_checker_.DetachFromThread(); |
| 63 } | 65 } |
| 64 | 66 |
| 65 // Enable or disable gamepad polling. Can be called from any thread. | 67 // Enable or disable gamepad polling. Can be called from any thread. |
| 66 void EnablePolling(bool enabled) { | 68 void EnablePolling(bool enabled) { |
| 67 polling_task_runner_->PostTask( | 69 polling_task_runner_->PostTask( |
| 68 FROM_HERE, | 70 FROM_HERE, |
| 69 base::Bind( | 71 base::Bind( |
| 70 &ThreadSafeGamepadChangeFetcher::EnablePollingOnPollingThread, | 72 &ThreadSafeGamepadChangeFetcher::EnablePollingOnPollingThread, |
| 71 make_scoped_refptr(this), enabled)); | 73 make_scoped_refptr(this), enabled)); |
| 72 } | 74 } |
| 73 | 75 |
| 74 private: | 76 private: |
| 75 friend class base::RefCountedThreadSafe<ThreadSafeGamepadChangeFetcher>; | 77 friend class base::RefCountedThreadSafe<ThreadSafeGamepadChangeFetcher>; |
| 76 | 78 |
| 77 virtual ~ThreadSafeGamepadChangeFetcher() {} | 79 ~ThreadSafeGamepadChangeFetcher() override {} |
| 78 | 80 |
| 79 // Enables or disables polling. | 81 // Enables or disables polling. |
| 80 void EnablePollingOnPollingThread(bool enabled) { | 82 void EnablePollingOnPollingThread(bool enabled) { |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 82 is_enabled_ = enabled; | 84 is_enabled_ = enabled; |
| 83 | 85 |
| 84 if (is_enabled_) { | 86 if (is_enabled_) { |
| 85 if (!fetcher_) { | 87 if (!fetcher_) { |
| 86 fetcher_ = create_fetcher_callback_.Run(); | 88 fetcher_ = create_fetcher_callback_.Run(); |
| 89 InitializeDataFetcher(fetcher_.get()); | |
| 87 DCHECK(fetcher_); | 90 DCHECK(fetcher_); |
| 88 } | 91 } |
| 89 SchedulePollOnPollingThread(); | 92 SchedulePollOnPollingThread(); |
| 90 } else { | 93 } else { |
| 91 fetcher_.reset(); | 94 fetcher_.reset(); |
| 92 } | 95 } |
| 93 } | 96 } |
| 94 | 97 |
| 95 // Schedules the next poll on the polling thread. | 98 // Schedules the next poll on the polling thread. |
| 96 void SchedulePollOnPollingThread() { | 99 void SchedulePollOnPollingThread() { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 112 void PollOnPollingThread() { | 115 void PollOnPollingThread() { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 | 117 |
| 115 has_poll_scheduled_ = false; | 118 has_poll_scheduled_ = false; |
| 116 if (!is_enabled_) | 119 if (!is_enabled_) |
| 117 return; | 120 return; |
| 118 | 121 |
| 119 DCHECK(fetcher_); | 122 DCHECK(fetcher_); |
| 120 | 123 |
| 121 blink::WebGamepads new_state = state_; | 124 blink::WebGamepads new_state = state_; |
| 122 fetcher_->GetGamepadData(&new_state, false); | 125 fetcher_->GetGamepadData(false); |
|
reveman
2016/08/07 16:45:43
nit: not new to this patch but can you add (false
| |
| 126 | |
| 127 new_state.length = 0; | |
| 128 device::PadState& pad_state = pad_states_.get()[0]; | |
| 129 | |
| 130 if (!pad_state.active_state && | |
|
reveman
2016/08/07 16:45:43
nit: I don't really understand this part. Can you
| |
| 131 pad_state.source != device::GAMEPAD_SOURCE_NONE) { | |
| 132 ClearPadState(pad_state); | |
| 133 } | |
| 134 | |
| 135 MapAndSanitizeGamepadData(&pad_state, &new_state.items[0], false); | |
|
reveman
2016/08/07 16:45:43
nit: false /* name */ here too
| |
| 136 | |
| 137 if (pad_state.active_state) { | |
|
reveman
2016/08/07 16:45:43
nit: also don't understand this. if active_state i
| |
| 138 new_state.length++; | |
| 139 pad_state.active_state = device::GAMEPAD_INACTIVE; | |
| 140 } | |
| 123 | 141 |
| 124 if (std::max(new_state.length, state_.length) > 0) { | 142 if (std::max(new_state.length, state_.length) > 0) { |
| 125 if (new_state.items[0].connected != state_.items[0].connected || | 143 if (new_state.items[0].connected != state_.items[0].connected || |
| 126 new_state.items[0].timestamp > state_.items[0].timestamp) { | 144 new_state.items[0].timestamp > state_.items[0].timestamp) { |
| 127 origin_task_runner_->PostTask( | 145 origin_task_runner_->PostTask( |
| 128 FROM_HERE, | 146 FROM_HERE, |
| 129 base::Bind(process_gamepad_changes_, new_state.items[0])); | 147 base::Bind(process_gamepad_changes_, new_state.items[0])); |
| 130 } | 148 } |
| 131 } | 149 } |
| 132 | 150 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 delegate_->OnButton(button_id, new_button.pressed, new_button.value); | 274 delegate_->OnButton(button_id, new_button.pressed, new_button.value); |
| 257 } | 275 } |
| 258 } | 276 } |
| 259 if (send_frame) | 277 if (send_frame) |
| 260 delegate_->OnFrame(); | 278 delegate_->OnFrame(); |
| 261 | 279 |
| 262 pad_state_ = new_pad; | 280 pad_state_ = new_pad; |
| 263 } | 281 } |
| 264 | 282 |
| 265 } // namespace exo | 283 } // namespace exo |
| OLD | NEW |