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

Unified Diff: components/exo/gamepad.cc

Issue 2076013002: exo: Implement wayland gamepad support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serv
Patch Set: do not depend on webkit but device/gamepad Created 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/exo/gamepad.h ('k') | components/exo/gamepad_delegate.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/exo/gamepad.cc
diff --git a/components/exo/gamepad.cc b/components/exo/gamepad.cc
new file mode 100644
index 0000000000000000000000000000000000000000..073cfe94e2e46f22518124136a8fa2c191ac7ed3
--- /dev/null
+++ b/components/exo/gamepad.cc
@@ -0,0 +1,133 @@
+// 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(size_t id, GamepadDelegate* delegate)
+ : Gamepad(id, delegate, nullptr) {}
+
+Gamepad::Gamepad(size_t id,
+ GamepadDelegate* delegate,
+ std::unique_ptr<device::GamepadDataFetcher> fetcher)
+ : id_(id),
+ delegate_(delegate),
+ fetcher_(std::move(fetcher)),
+ polling_thread_(new base::Thread("Exo gamepad polling thread")),
+ origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
denniskempin 2016/06/29 18:20:54 reveman: An update on this. Eventually we want to
+ 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);
+
+ if (id_ >= std::max(new_state.length, state_.length))
+ return;
+
+ bool send_frame = false;
+ const blink::WebGamepad& new_pad = new_state.items[id_];
+ blink::WebGamepad& pad = state_.items[id_];
+
+ // 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_)));
+ }
+
+ state_ = new_state;
+
+ ScheduleOnPoll();
+}
+
+} // namespace exo
« no previous file with comments | « components/exo/gamepad.h ('k') | components/exo/gamepad_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698