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

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: fixed polling thread. 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
Index: components/exo/gamepad.cc
diff --git a/components/exo/gamepad.cc b/components/exo/gamepad.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2e899972abaadd7cb894a534c3ba681e0424448c
--- /dev/null
+++ b/components/exo/gamepad.cc
@@ -0,0 +1,137 @@
+// 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 {
reveman 2016/06/29 21:55:44 nit: blank line after this. blank line before is n
denniskempin 2016/06/30 00:29:21 Done.
+constexpr double kEpsilon = 0.001;
reveman 2016/06/29 21:55:44 nit: kGamepadButtonValueEpsilon and blank line aft
denniskempin 2016/06/30 00:29:21 Done.
+bool almostEquals(double a, double b) {
reveman 2016/06/29 21:55:44 GamepadButtonValuesAreEqual()?
denniskempin 2016/06/30 00:29:21 Done.
+ return fabs(a - b) < kEpsilon;
+}
+
+} // anonymous namespace
reveman 2016/06/29 21:55:44 nit: "} // namespace" to be consistent with rest
denniskempin 2016/06/30 00:29:21 Done.
+
+////////////////////////////////////////////////////////////////////////////////
+// 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()) {
+ 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));
reveman 2016/06/29 21:55:44 16? Please add a constant and a comment for this m
denniskempin 2016/06/30 00:29:21 Done.
+}
+
+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
reveman 2016/06/29 21:55:44 s/fetcher/fetcher./
denniskempin 2016/06/30 00:29:21 Done.
+ 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());
reveman 2016/06/29 21:55:44 Can you use the thread checker here instead?
denniskempin 2016/06/30 00:29:21 It's a different thread. This runs in the polling
+
+ 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
reveman 2016/06/29 21:55:44 nit: s/state/state./
denniskempin 2016/06/30 00:29:21 Done.
+ if (new_pad.connected != pad.connected) {
+ origin_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&GamepadDelegate::OnStateChange,
+ base::Unretained(delegate_), new_pad.connected));
reveman 2016/06/29 21:55:44 Is base::Unretained safe here and below? what if t
+ }
+
+ if (!new_pad.connected || new_pad.timestamp <= pad.timestamp)
+ return;
+
+ // Notify delegate of updated axes
reveman 2016/06/29 21:55:44 nit: s/axes/axes./
denniskempin 2016/06/30 00:29:21 Done.
+ 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
reveman 2016/06/29 21:55:44 nit: s/buttons/buttons./
denniskempin 2016/06/30 00:29:21 Done.
+ 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) {
reveman 2016/06/29 21:55:44 check "pressed" first as it's cheaper
denniskempin 2016/06/30 00:29:21 Done.
+ 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

Powered by Google App Engine
This is Rietveld 408576698