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

Side by Side 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, 5 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/exo/gamepad.h"
6
7 #include <cmath>
8
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/thread.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "components/exo/gamepad_delegate.h"
15 #include "device/gamepad/gamepad_data_fetcher.h"
16 #include "device/gamepad/gamepad_platform_data_fetcher.h"
17
18 namespace exo {
19
20 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.
21 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.
22 bool almostEquals(double a, double b) {
reveman 2016/06/29 21:55:44 GamepadButtonValuesAreEqual()?
denniskempin 2016/06/30 00:29:21 Done.
23 return fabs(a - b) < kEpsilon;
24 }
25
26 } // anonymous namespace
reveman 2016/06/29 21:55:44 nit: "} // namespace" to be consistent with rest
denniskempin 2016/06/30 00:29:21 Done.
27
28 ////////////////////////////////////////////////////////////////////////////////
29 // Gamepad, public:
30
31 Gamepad::Gamepad(size_t id, GamepadDelegate* delegate)
32 : Gamepad(id, delegate, nullptr) {}
33
34 Gamepad::Gamepad(size_t id,
35 GamepadDelegate* delegate,
36 std::unique_ptr<device::GamepadDataFetcher> fetcher)
37 : id_(id),
38 delegate_(delegate),
39 fetcher_(std::move(fetcher)),
40 polling_thread_(new base::Thread("Exo gamepad polling thread")),
41 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
42 polling_thread_->StartWithOptions(
43 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
44 polling_thread_->task_runner()->PostTask(
45 FROM_HERE,
46 base::Bind(&Gamepad::InitializePollingThread, base::Unretained(this)));
47 }
48
49 Gamepad::~Gamepad() {
50 delegate_->OnGamepadDestroying(this);
51 polling_thread_->Stop();
52 }
53
54 ////////////////////////////////////////////////////////////////////////////////
55 // Gamepad, private:
56
57 void Gamepad::ScheduleOnPoll() {
58 polling_thread_->task_runner()->PostDelayedTask(
59 FROM_HERE, base::Bind(&Gamepad::OnPoll, base::Unretained(this)),
60 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.
61 }
62
63 void Gamepad::InitializePollingThread() {
64 DCHECK(base::MessageLoop::current() == polling_thread_->message_loop());
65 // The platform data fetcher has to be initialized on the polling thread.
66 if (!fetcher_)
67 fetcher_.reset(new device::GamepadPlatformDataFetcher());
68 ScheduleOnPoll();
69 }
70
71 void Gamepad::OnPoll() {
72 DCHECK(base::MessageLoop::current() == polling_thread_->message_loop());
73 DCHECK(fetcher_);
74
75 // 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.
76 blink::WebGamepads new_state = state_;
77 fetcher_->GetGamepadData(&new_state, false);
78 PostGamepadChanges(new_state);
79
80 state_ = new_state;
81 ScheduleOnPoll();
82 }
83
84 void Gamepad::PostGamepadChanges(const blink::WebGamepads new_state) {
85 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
86
87 if (id_ >= std::max(new_state.length, state_.length))
88 return;
89
90 bool send_frame = false;
91 const blink::WebGamepad& new_pad = new_state.items[id_];
92 blink::WebGamepad& pad = state_.items[id_];
93
94 // Update connection state
reveman 2016/06/29 21:55:44 nit: s/state/state./
denniskempin 2016/06/30 00:29:21 Done.
95 if (new_pad.connected != pad.connected) {
96 origin_task_runner_->PostTask(
97 FROM_HERE, base::Bind(&GamepadDelegate::OnStateChange,
98 base::Unretained(delegate_), new_pad.connected));
reveman 2016/06/29 21:55:44 Is base::Unretained safe here and below? what if t
99 }
100
101 if (!new_pad.connected || new_pad.timestamp <= pad.timestamp)
102 return;
103
104 // Notify delegate of updated axes
reveman 2016/06/29 21:55:44 nit: s/axes/axes./
denniskempin 2016/06/30 00:29:21 Done.
105 for (size_t axis = 0; axis < std::max(pad.axesLength, new_pad.axesLength);
106 ++axis) {
107 if (!almostEquals(new_pad.axes[axis], pad.axes[axis])) {
108 send_frame = true;
109 origin_task_runner_->PostTask(
110 FROM_HERE,
111 base::Bind(&GamepadDelegate::OnAxis, base::Unretained(delegate_),
112 axis, new_pad.axes[axis]));
113 }
114 }
115
116 // Notify delegate of updated buttons
reveman 2016/06/29 21:55:44 nit: s/buttons/buttons./
denniskempin 2016/06/30 00:29:21 Done.
117 for (size_t button = 0;
118 button < std::max(pad.buttonsLength, new_pad.buttonsLength); ++button) {
119 if (!almostEquals(new_pad.buttons[button].value,
120 pad.buttons[button].value) ||
121 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.
122 send_frame = true;
123 origin_task_runner_->PostTask(
124 FROM_HERE,
125 base::Bind(&GamepadDelegate::OnButton, base::Unretained(delegate_),
126 button, new_pad.buttons[button].pressed,
127 new_pad.buttons[button].value));
128 }
129 }
130 if (send_frame) {
131 origin_task_runner_->PostTask(
132 FROM_HERE,
133 base::Bind(&GamepadDelegate::OnFrame, base::Unretained(delegate_)));
134 }
135 }
136
137 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698