Index: components/exo/gamepads.cc |
diff --git a/components/exo/gamepads.cc b/components/exo/gamepads.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d713e7bd80216a663ffed02921da825240c52839 |
--- /dev/null |
+++ b/components/exo/gamepads.cc |
@@ -0,0 +1,136 @@ |
+// Copyright 2015 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/gamepads.h" |
+ |
+#include <cmath> |
+ |
+#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/gamepads_delegate.h" |
+#include "device/gamepad/gamepad_data_fetcher.h" |
+#include "device/gamepad/gamepad_platform_data_fetcher.h" |
+ |
+namespace exo { |
+ |
+namespace { |
reveman
2016/06/27 17:30:35
nit: blank line after this. blank line before is n
|
+constexpr double kEpsilon = 0.001; |
+bool almostEquals(double a, double b) { |
reveman
2016/06/27 17:30:35
nit: AlmostEqual but might also be good to explain
|
+ return fabs(a - b) < kEpsilon; |
+} |
+ |
+} // anonymous namespace |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// Gamepads, public: |
+ |
+Gamepads::Gamepads(GamepadsDelegate* delegate) : Gamepads(delegate, nullptr) {} |
+ |
+Gamepads::Gamepads(GamepadsDelegate* delegate, |
+ std::unique_ptr<device::GamepadDataFetcher> fetcher) |
+ : delegate_(delegate), |
+ fetcher_(std::move(fetcher)), |
+ polling_thread_(new base::Thread("Exo gamepad polling thread")), |
+ origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
+ polling_thread_->StartWithOptions( |
+ base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
+ polling_thread_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&Gamepads::InitializePollingThread, base::Unretained(this))); |
+} |
+ |
+Gamepads::~Gamepads() { |
+ delegate_->OnGamepadsDestroying(this); |
+ polling_thread_->Stop(); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// Gamepads, private: |
+ |
+void Gamepads::ScheduleOnPoll() { |
+ polling_thread_->task_runner()->PostDelayedTask( |
+ FROM_HERE, base::Bind(&Gamepads::OnPoll, base::Unretained(this)), |
+ base::TimeDelta::FromMilliseconds(16)); |
+} |
+ |
+void Gamepads::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()); |
reveman
2016/06/27 17:30:34
Would be nice if this was always passed to the cto
|
+ ScheduleOnPoll(); |
+} |
+ |
+void Gamepads::OnPoll() { |
+ DCHECK(base::MessageLoop::current() == polling_thread_->message_loop()); |
+ DCHECK(fetcher_); |
+ |
+ // Update state with data from gamepad data fetcher |
+ blink::WebGamepads new_state = state_; |
+ fetcher_->GetGamepadData(&new_state, false); |
+ |
+ // Notify delegate of all changes since the last gamepad state |
+ for (size_t i = 0; i < std::max(state_.length, new_state.length); ++i) { |
+ bool send_frame = false; |
+ const blink::WebGamepad& new_pad = new_state.items[i]; |
+ blink::WebGamepad& pad = state_.items[i]; |
+ |
+ // Update connection state |
+ if (new_pad.connected != pad.connected) { |
+ if (new_pad.connected) { |
+ origin_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&GamepadsDelegate::OnConnected, |
+ base::Unretained(delegate_), i)); |
+ } else { |
+ origin_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&GamepadsDelegate::OnDisconnected, |
+ base::Unretained(delegate_), i)); |
+ } |
+ } |
+ |
+ if (!new_pad.connected || new_pad.timestamp <= pad.timestamp) |
+ continue; |
+ |
+ // Notify delegate of updated axes |
+ for (size_t axis = 0; axis < std::max(pad.axesLength, new_pad.axesLength); |
+ ++axis) { |
+ if (!almostEquals(new_pad.axes[axis], pad.axes[axis])) { |
+ send_frame = true; |
+ origin_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&GamepadsDelegate::OnAxis, base::Unretained(delegate_), |
+ i, 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 (!almostEquals(new_pad.buttons[button].value, |
+ pad.buttons[button].value) || |
+ pad.buttons[button].pressed != new_pad.buttons[button].pressed) { |
+ send_frame = true; |
+ origin_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&GamepadsDelegate::OnButton, base::Unretained(delegate_), |
+ i, button, new_pad.buttons[button].pressed, |
+ new_pad.buttons[button].value)); |
+ } |
+ } |
+ if (send_frame) { |
+ origin_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&GamepadsDelegate::OnFrame, |
+ base::Unretained(delegate_), i)); |
+ } |
+ } |
+ state_ = new_state; |
+ |
+ ScheduleOnPoll(); |
+} |
+ |
+} // namespace exo |