Index: components/exo/gamepad.cc |
diff --git a/components/exo/gamepad.cc b/components/exo/gamepad.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1ebdf034b7fe880b449092040a68608f4f0ef40e |
--- /dev/null |
+++ b/components/exo/gamepad.cc |
@@ -0,0 +1,177 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/exo/gamepad.h" |
+ |
+#include <cmath> |
+ |
+#include "ash/shell.h" |
+#include "base/bind.h" |
+#include "base/location.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/threading/thread.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "components/exo/gamepad_delegate.h" |
+#include "components/exo/shell_surface.h" |
+#include "components/exo/surface.h" |
+#include "device/gamepad/gamepad_data_fetcher.h" |
+#include "device/gamepad/gamepad_platform_data_fetcher.h" |
+#include "ui/aura/client/focus_client.h" |
+#include "ui/aura/window.h" |
+ |
+namespace exo { |
+namespace { |
+ |
+constexpr double kGamepadButtonValueEpsilon = 0.001; |
+bool GamepadButtonValuesAreEqual(double a, double b) { |
+ return fabs(a - b) < kGamepadButtonValueEpsilon; |
+} |
+ |
+// Time between gamepad polls in milliseconds. |
+constexpr unsigned kPollingTimeDelta = 16; |
reveman
2016/06/30 01:19:05
nit: s/TimeDelta/Interval/? Chromium usually adds
denniskempin
2016/06/30 03:23:43
Unfortunately we cannot do anything about this, wi
denniskempin
2016/06/30 17:08:29
Done on the nit.
|
+ |
+} // namespace |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// Gamepad, public: |
+ |
+Gamepad::Gamepad(GamepadDelegate* delegate) : Gamepad(delegate, nullptr) {} |
+ |
+Gamepad::Gamepad(GamepadDelegate* delegate, |
+ std::unique_ptr<device::GamepadDataFetcher> fetcher) |
+ : delegate_(delegate), |
+ fetcher_(std::move(fetcher)), |
+ polling_thread_(new base::Thread("Exo gamepad polling thread")), |
+ exo_focused_(false), |
reveman
2016/06/30 01:19:05
nit: all exo code is now providing default init of
denniskempin
2016/06/30 17:08:29
Done.
|
+ origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
+ aura::client::FocusClient* focus_client = |
+ aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow()); |
+ focus_client->AddObserver(this); |
+ OnWindowFocused(focus_client->GetFocusedWindow(), nullptr); |
+ |
+ polling_thread_->StartWithOptions( |
+ base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
+ polling_thread_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&Gamepad::InitializePollingThread, base::Unretained(this))); |
+} |
+ |
+Gamepad::~Gamepad() { |
+ delegate_->OnGamepadDestroying(this); |
+ polling_thread_->Stop(); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// aura::client::FocusChangeObserver overrides: |
+ |
+void Gamepad::OnWindowFocused(aura::Window* gained_focus, |
+ aura::Window* lost_focus) { |
+ base::AutoLock _l(lock_); |
reveman
2016/06/30 01:19:05
nit: base::AutoLock lock(lock_);
It's hard to fol
denniskempin
2016/06/30 03:23:43
I am not quite sure how that would look like. Even
denniskempin
2016/06/30 04:56:46
Actually I take that back. I thought a bit about i
denniskempin
2016/06/30 17:08:29
Done.
|
+ |
+ Surface* target = Surface::AsSurface(gained_focus); |
+ if (!target) { |
+ aura::Window* top_level_window = gained_focus->GetToplevelWindow(); |
+ if (top_level_window) |
+ target = ShellSurface::GetMainSurface(top_level_window); |
+ } |
+ exo_focused_ = (target != nullptr); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// Gamepad, private: |
+ |
+void Gamepad::ScheduleOnPoll() { |
+ polling_thread_->task_runner()->PostDelayedTask( |
+ FROM_HERE, base::Bind(&Gamepad::OnPoll, base::Unretained(this)), |
+ base::TimeDelta::FromMilliseconds(kPollingTimeDelta)); |
+} |
+ |
+void Gamepad::InitializePollingThread() { |
+ DCHECK(base::MessageLoop::current() == polling_thread_->message_loop()); |
+ // The platform data fetcher has to be initialized on the polling thread. |
+ if (!fetcher_) |
+ fetcher_.reset(new device::GamepadPlatformDataFetcher()); |
+ ScheduleOnPoll(); |
+} |
+ |
+void Gamepad::OnPoll() { |
+ DCHECK(base::MessageLoop::current() == polling_thread_->message_loop()); |
+ DCHECK(fetcher_); |
+ |
+ blink::WebGamepads new_state = state_; |
+ fetcher_->GetGamepadData(&new_state, false); |
+ |
+ { |
+ // Lock only needed for access to exo_focused_ |
+ base::AutoLock _l(lock_); |
+ |
+ if (!exo_focused_) { |
+ // Keep polling but do not post changes until we are back in focus. |
+ // Once back in focus, the delta between state_ and new_state will update |
+ // the client to the current state of the gamepad. |
+ ScheduleOnPoll(); |
+ return; |
+ } |
+ } |
+ |
+ PostGamepadChanges(new_state); |
+ |
+ state_ = new_state; |
+ ScheduleOnPoll(); |
+} |
+ |
+void Gamepad::PostGamepadChanges(const blink::WebGamepads new_state) { |
+ DCHECK(base::MessageLoop::current() == polling_thread_->message_loop()); |
+ |
+ if (std::max(new_state.length, state_.length) == 0) |
+ return; |
+ |
+ bool send_frame = false; |
+ const blink::WebGamepad& new_pad = new_state.items[0]; |
+ blink::WebGamepad& pad = state_.items[0]; |
+ |
+ // Update connection state. |
+ if (new_pad.connected != pad.connected) { |
+ origin_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&GamepadDelegate::OnStateChange, |
+ delegate_->GetWeakPtr(), new_pad.connected)); |
reveman
2016/06/30 01:19:05
GetWeakPtr() can't be called and the weak ptr retu
denniskempin
2016/06/30 17:08:29
no longer needed
|
+ } |
+ |
+ if (!new_pad.connected || new_pad.timestamp <= pad.timestamp) |
+ return; |
+ |
+ // Notify delegate of updated axes. |
+ for (size_t axis = 0; axis < std::max(pad.axesLength, new_pad.axesLength); |
+ ++axis) { |
+ if (!GamepadButtonValuesAreEqual(new_pad.axes[axis], pad.axes[axis])) { |
+ send_frame = true; |
+ origin_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&GamepadDelegate::OnAxis, delegate_->GetWeakPtr(), axis, |
+ new_pad.axes[axis])); |
+ } |
+ } |
+ |
+ // Notify delegate of updated buttons. |
+ for (size_t button = 0; |
+ button < std::max(pad.buttonsLength, new_pad.buttonsLength); ++button) { |
+ if (pad.buttons[button].pressed != new_pad.buttons[button].pressed || |
+ !GamepadButtonValuesAreEqual(new_pad.buttons[button].value, |
+ pad.buttons[button].value)) { |
+ send_frame = true; |
+ origin_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&GamepadDelegate::OnButton, delegate_->GetWeakPtr(), |
+ button, new_pad.buttons[button].pressed, |
+ new_pad.buttons[button].value)); |
+ } |
+ } |
+ if (send_frame) { |
+ origin_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&GamepadDelegate::OnFrame, delegate_->GetWeakPtr())); |
+ } |
+} |
+ |
+} // namespace exo |