Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: components/exo/gaming_seat.cc

Issue 2900773003: Allow gaming_seat to use ozone gamepad as back-end (Closed)
Patch Set: Add gaming_seat_ozone to exo Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 8
9 #include "base/bind.h" 9 #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" 10 #include "components/exo/gamepad_delegate.h"
15 #include "components/exo/gaming_seat_delegate.h" 11 #include "components/exo/gaming_seat_delegate.h"
16 #include "components/exo/shell_surface.h" 12 #include "components/exo/shell_surface.h"
17 #include "components/exo/surface.h" 13 #include "components/exo/surface.h"
18 #include "device/gamepad/gamepad_data_fetcher.h" 14 #include "device/gamepad/gamepad_data_fetcher.h"
19 #include "device/gamepad/gamepad_pad_state_provider.h" 15 #include "device/gamepad/gamepad_pad_state_provider.h"
20 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h" 16 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h"
21 #include "ui/aura/window.h" 17 #include "ui/aura/window.h"
22 18
23 namespace exo { 19 namespace exo {
24 namespace { 20 namespace {
25 21
26 constexpr double kGamepadButtonValueEpsilon = 0.001; 22 constexpr double kGamepadButtonValueEpsilon = 0.001;
27 bool GamepadButtonValuesAreEqual(double a, double b) { 23 bool GamepadButtonValuesAreEqual(double a, double b) {
28 return fabs(a - b) < kGamepadButtonValueEpsilon; 24 return fabs(a - b) < kGamepadButtonValueEpsilon;
29 } 25 }
30 26
31 std::unique_ptr<device::GamepadDataFetcher> CreateGamepadPlatformDataFetcher() { 27 std::unique_ptr<device::GamepadDataFetcher> CreateGamepadPlatformDataFetcher() {
32 return std::unique_ptr<device::GamepadDataFetcher>( 28 return std::unique_ptr<device::GamepadDataFetcher>(
33 new device::GamepadPlatformDataFetcherLinux()); 29 new device::GamepadPlatformDataFetcherLinux());
34 } 30 }
35 31
36 // Time between gamepad polls in milliseconds. 32 // Time between gamepad polls in milliseconds.
37 constexpr unsigned kPollingTimeIntervalMs = 16; 33 constexpr unsigned kPollingTimeIntervalMs = 16;
38 34
39 } // namespace 35 } // namespace
40 36
37 base::Thread* GamingSeat::CreatePollingThreadIfNeeded() {
38 base::Thread* polling_thread =
39 new base::Thread("Exo gaming input polling thread.");
40 polling_thread->StartWithOptions(
41 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
42 return polling_thread;
43 }
44
41 //////////////////////////////////////////////////////////////////////////////// 45 ////////////////////////////////////////////////////////////////////////////////
42 // GamingSeat::ThreadSafeGamepadChangeFetcher 46 // GamingSeat::ThreadSafeGamepadChangeFetcher
43 47
44 // Implements all methods and resources running on the polling thread. 48 // 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 49 // 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. 50 // polling thread even if the Gamepad has been destroyed on the origin thread.
47 class GamingSeat::ThreadSafeGamepadChangeFetcher 51 class GamingSeat::ThreadSafeGamepadChangeFetcher
48 : public device::GamepadPadStateProvider, 52 : public device::GamepadPadStateProvider,
49 public base::RefCountedThreadSafe< 53 public base::RefCountedThreadSafe<
50 GamingSeat::ThreadSafeGamepadChangeFetcher> { 54 GamingSeat::ThreadSafeGamepadChangeFetcher> {
51 public: 55 public:
52 using ProcessGamepadChangesCallback = 56 using ProcessGamepadChangesCallback =
53 base::Callback<void(int index, const device::Gamepad)>; 57 base::Callback<void(int index, const device::Gamepad)>;
54 58
55 ThreadSafeGamepadChangeFetcher( 59 ThreadSafeGamepadChangeFetcher(
56 const ProcessGamepadChangesCallback& post_gamepad_changes, 60 const ProcessGamepadChangesCallback& post_gamepad_changes,
57 const CreateGamepadDataFetcherCallback& create_fetcher_callback, 61 const CreateGamepadDataFetcherCallback& create_fetcher_callback,
58 base::SingleThreadTaskRunner* task_runner) 62 base::SingleThreadTaskRunner* task_runner)
59 : process_gamepad_changes_(post_gamepad_changes), 63 : process_gamepad_changes_(post_gamepad_changes),
60 create_fetcher_callback_(create_fetcher_callback), 64 create_fetcher_callback_(create_fetcher_callback),
61 polling_task_runner_(task_runner), 65 polling_task_runner_(task_runner),
62 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { 66 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
63 thread_checker_.DetachFromThread(); 67 DETACH_FROM_THREAD(thread_checker_);
64 } 68 }
65 69
66 // Enable or disable gamepad polling. Can be called from any thread. 70 // Enable or disable gamepad polling. Can be called from any thread.
67 void EnablePolling(bool enabled) { 71 void EnablePolling(bool enabled) {
68 polling_task_runner_->PostTask( 72 polling_task_runner_->PostTask(
69 FROM_HERE, 73 FROM_HERE,
70 base::Bind( 74 base::Bind(
71 &ThreadSafeGamepadChangeFetcher::EnablePollingOnPollingThread, 75 &ThreadSafeGamepadChangeFetcher::EnablePollingOnPollingThread,
72 make_scoped_refptr(this), enabled)); 76 make_scoped_refptr(this), enabled));
73 } 77 }
74 78
75 private: 79 private:
76 friend class base::RefCountedThreadSafe<ThreadSafeGamepadChangeFetcher>; 80 friend class base::RefCountedThreadSafe<ThreadSafeGamepadChangeFetcher>;
77 81
78 ~ThreadSafeGamepadChangeFetcher() override {} 82 ~ThreadSafeGamepadChangeFetcher() override {}
79 83
80 // Enables or disables polling. 84 // Enables or disables polling.
81 void EnablePollingOnPollingThread(bool enabled) { 85 void EnablePollingOnPollingThread(bool enabled) {
82 DCHECK(thread_checker_.CalledOnValidThread()); 86 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
83 is_enabled_ = enabled; 87 is_enabled_ = enabled;
84 88
85 if (is_enabled_) { 89 if (is_enabled_) {
86 if (!fetcher_) { 90 if (!fetcher_) {
87 fetcher_ = create_fetcher_callback_.Run(); 91 fetcher_ = create_fetcher_callback_.Run();
88 InitializeDataFetcher(fetcher_.get()); 92 InitializeDataFetcher(fetcher_.get());
89 DCHECK(fetcher_); 93 DCHECK(fetcher_);
90 } 94 }
91 SchedulePollOnPollingThread(); 95 SchedulePollOnPollingThread();
92 } else { 96 } else {
93 fetcher_.reset(); 97 fetcher_.reset();
94 } 98 }
95 } 99 }
96 100
97 // Schedules the next poll on the polling thread. 101 // Schedules the next poll on the polling thread.
98 void SchedulePollOnPollingThread() { 102 void SchedulePollOnPollingThread() {
99 DCHECK(thread_checker_.CalledOnValidThread()); 103 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
100 DCHECK(fetcher_); 104 DCHECK(fetcher_);
101 105
102 if (!is_enabled_ || has_poll_scheduled_) 106 if (!is_enabled_ || has_poll_scheduled_)
103 return; 107 return;
104 108
105 has_poll_scheduled_ = true; 109 has_poll_scheduled_ = true;
106 polling_task_runner_->PostDelayedTask( 110 polling_task_runner_->PostDelayedTask(
107 FROM_HERE, 111 FROM_HERE,
108 base::Bind(&ThreadSafeGamepadChangeFetcher::PollOnPollingThread, 112 base::Bind(&ThreadSafeGamepadChangeFetcher::PollOnPollingThread,
109 make_scoped_refptr(this)), 113 make_scoped_refptr(this)),
110 base::TimeDelta::FromMilliseconds(kPollingTimeIntervalMs)); 114 base::TimeDelta::FromMilliseconds(kPollingTimeIntervalMs));
111 } 115 }
112 116
113 // Polls devices for new data and posts gamepad changes back to origin thread. 117 // Polls devices for new data and posts gamepad changes back to origin thread.
114 void PollOnPollingThread() { 118 void PollOnPollingThread() {
115 DCHECK(thread_checker_.CalledOnValidThread()); 119 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
116 120
117 has_poll_scheduled_ = false; 121 has_poll_scheduled_ = false;
118 if (!is_enabled_) 122 if (!is_enabled_)
119 return; 123 return;
120 124
121 DCHECK(fetcher_); 125 DCHECK(fetcher_);
122 126
123 device::Gamepads new_state = state_; 127 device::Gamepads new_state = state_;
124 fetcher_->GetGamepadData( 128 fetcher_->GetGamepadData(
125 false /* No hardware changed notification from the system */); 129 false /* No hardware changed notification from the system */);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 // The current state of all gamepads. 177 // The current state of all gamepads.
174 device::Gamepads state_; 178 device::Gamepads state_;
175 179
176 // True if a poll has been scheduled. 180 // True if a poll has been scheduled.
177 bool has_poll_scheduled_ = false; 181 bool has_poll_scheduled_ = false;
178 182
179 // True if the polling thread is paused. 183 // True if the polling thread is paused.
180 bool is_enabled_ = false; 184 bool is_enabled_ = false;
181 185
182 // ThreadChecker for the polling thread. 186 // ThreadChecker for the polling thread.
183 base::ThreadChecker thread_checker_; 187 THREAD_CHECKER(thread_checker_);
184 188
185 DISALLOW_COPY_AND_ASSIGN(ThreadSafeGamepadChangeFetcher); 189 DISALLOW_COPY_AND_ASSIGN(ThreadSafeGamepadChangeFetcher);
186 }; 190 };
187 191
188 //////////////////////////////////////////////////////////////////////////////// 192 ////////////////////////////////////////////////////////////////////////////////
189 // GamingSeat, public: 193 // GamingSeat, public:
190 194
191 GamingSeat::GamingSeat(GamingSeatDelegate* gaming_seat_delegate, 195 GamingSeat::GamingSeat(GamingSeatDelegate* gaming_seat_delegate,
192 base::SingleThreadTaskRunner* polling_task_runner) 196 base::Thread* polling_thread)
193 : GamingSeat(gaming_seat_delegate, 197 : GamingSeat(gaming_seat_delegate,
194 polling_task_runner, 198 polling_thread->task_runner().get(),
195 base::Bind(CreateGamepadPlatformDataFetcher)) {} 199 base::Bind(CreateGamepadPlatformDataFetcher)) {}
196 200
197 GamingSeat::GamingSeat(GamingSeatDelegate* gaming_seat_delegate, 201 GamingSeat::GamingSeat(GamingSeatDelegate* gaming_seat_delegate,
198 base::SingleThreadTaskRunner* polling_task_runner, 202 base::SingleThreadTaskRunner* polling_task_runner,
199 CreateGamepadDataFetcherCallback create_fetcher_callback) 203 CreateGamepadDataFetcherCallback create_fetcher_callback)
200 : delegate_(gaming_seat_delegate), 204 : delegate_(gaming_seat_delegate),
201 gamepad_delegates_{nullptr}, 205 gamepad_delegates_{nullptr},
202 weak_ptr_factory_(this) { 206 weak_ptr_factory_(this) {
203 gamepad_change_fetcher_ = new ThreadSafeGamepadChangeFetcher( 207 gamepad_change_fetcher_ = new ThreadSafeGamepadChangeFetcher(
204 base::Bind(&GamingSeat::ProcessGamepadChanges, 208 base::Bind(&GamingSeat::ProcessGamepadChanges,
(...skipping 17 matching lines...) Expand all
222 } 226 }
223 } 227 }
224 WMHelper::GetInstance()->RemoveFocusObserver(this); 228 WMHelper::GetInstance()->RemoveFocusObserver(this);
225 } 229 }
226 230
227 //////////////////////////////////////////////////////////////////////////////// 231 ////////////////////////////////////////////////////////////////////////////////
228 // aura::client::FocusChangeObserver overrides: 232 // aura::client::FocusChangeObserver overrides:
229 233
230 void GamingSeat::OnWindowFocused(aura::Window* gained_focus, 234 void GamingSeat::OnWindowFocused(aura::Window* gained_focus,
231 aura::Window* lost_focus) { 235 aura::Window* lost_focus) {
232 DCHECK(thread_checker_.CalledOnValidThread()); 236 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
233 Surface* target = nullptr; 237 Surface* target = nullptr;
234 if (gained_focus) { 238 if (gained_focus) {
235 target = Surface::AsSurface(gained_focus); 239 target = Surface::AsSurface(gained_focus);
236 if (!target) { 240 if (!target) {
237 aura::Window* top_level_window = gained_focus->GetToplevelWindow(); 241 aura::Window* top_level_window = gained_focus->GetToplevelWindow();
238 if (top_level_window) 242 if (top_level_window)
239 target = ShellSurface::GetMainSurface(top_level_window); 243 target = ShellSurface::GetMainSurface(top_level_window);
240 } 244 }
241 } 245 }
242 246
243 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target); 247 bool focused = target && delegate_->CanAcceptGamepadEventsForSurface(target);
244 248
245 gamepad_change_fetcher_->EnablePolling(focused); 249 gamepad_change_fetcher_->EnablePolling(focused);
246 } 250 }
247 251
248 //////////////////////////////////////////////////////////////////////////////// 252 ////////////////////////////////////////////////////////////////////////////////
249 // GamingSeat, private: 253 // GamingSeat, private:
250 254
251 void GamingSeat::ProcessGamepadChanges(int index, 255 void GamingSeat::ProcessGamepadChanges(int index,
252 const device::Gamepad new_pad) { 256 const device::Gamepad new_pad) {
253 DCHECK(thread_checker_.CalledOnValidThread()); 257 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
254 bool send_frame = false; 258 bool send_frame = false;
255 259
256 device::Gamepad& pad_state = pad_state_.items[index]; 260 device::Gamepad& pad_state = pad_state_.items[index];
257 // Update connection state. 261 // Update connection state.
258 GamepadDelegate* delegate = gamepad_delegates_[index]; 262 GamepadDelegate* delegate = gamepad_delegates_[index];
259 if (new_pad.connected != pad_state.connected) { 263 if (new_pad.connected != pad_state.connected) {
260 // New pad is disconnected. 264 // New pad is disconnected.
261 if (!new_pad.connected) { 265 if (!new_pad.connected) {
262 // If gamepad is disconnected now, it should be connected before, then 266 // If gamepad is disconnected now, it should be connected before, then
263 // gamepad_delegate should not be null. 267 // gamepad_delegate should not be null.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 delegate->OnButton(button_id, new_button.pressed, new_button.value); 303 delegate->OnButton(button_id, new_button.pressed, new_button.value);
300 } 304 }
301 } 305 }
302 if (send_frame) 306 if (send_frame)
303 delegate->OnFrame(); 307 delegate->OnFrame();
304 308
305 pad_state = new_pad; 309 pad_state = new_pad;
306 } 310 }
307 311
308 } // namespace exo 312 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698