| Index: components/exo/gamepad.cc
|
| diff --git a/components/exo/gamepad.cc b/components/exo/gamepad.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..75d554c637b628bfbc36539917e1696fd5a7491f
|
| --- /dev/null
|
| +++ b/components/exo/gamepad.cc
|
| @@ -0,0 +1,134 @@
|
| +// 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 "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 "device/gamepad/gamepad_data_fetcher.h"
|
| +#include "device/gamepad/gamepad_platform_data_fetcher.h"
|
| +
|
| +namespace exo {
|
| +
|
| +namespace {
|
| +constexpr double kEpsilon = 0.001;
|
| +bool almostEquals(double a, double b) {
|
| + return fabs(a - b) < kEpsilon;
|
| +}
|
| +
|
| +} // anonymous 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")),
|
| + 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(&Gamepad::InitializePollingThread, base::Unretained(this)));
|
| +}
|
| +
|
| +Gamepad::~Gamepad() {
|
| + delegate_->OnGamepadDestroying(this);
|
| + polling_thread_->Stop();
|
| +}
|
| +
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +// Gamepad, private:
|
| +
|
| +void Gamepad::ScheduleOnPoll() {
|
| + polling_thread_->task_runner()->PostDelayedTask(
|
| + FROM_HERE, base::Bind(&Gamepad::OnPoll, base::Unretained(this)),
|
| + base::TimeDelta::FromMilliseconds(16));
|
| +}
|
| +
|
| +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_);
|
| +
|
| + // Update state with data from gamepad data fetcher
|
| + blink::WebGamepads new_state = state_;
|
| + fetcher_->GetGamepadData(&new_state, false);
|
| + 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,
|
| + base::Unretained(delegate_), new_pad.connected));
|
| + }
|
| +
|
| + 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 (!almostEquals(new_pad.axes[axis], pad.axes[axis])) {
|
| + send_frame = true;
|
| + origin_task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&GamepadDelegate::OnAxis, base::Unretained(delegate_),
|
| + 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(&GamepadDelegate::OnButton, base::Unretained(delegate_),
|
| + button, new_pad.buttons[button].pressed,
|
| + new_pad.buttons[button].value));
|
| + }
|
| + }
|
| + if (send_frame) {
|
| + origin_task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&GamepadDelegate::OnFrame, base::Unretained(delegate_)));
|
| + }
|
| +}
|
| +
|
| +} // namespace exo
|
|
|