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

Side by Side Diff: components/exo/gamepads.cc

Issue 2076013002: exo: Implement wayland gamepad support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serv
Patch Set: minor adjustments to protocol 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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/gamepads.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/gamepads_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/27 17:30:35 nit: blank line after this. blank line before is n
21 constexpr double kEpsilon = 0.001;
22 bool almostEquals(double a, double b) {
reveman 2016/06/27 17:30:35 nit: AlmostEqual but might also be good to explain
23 return fabs(a - b) < kEpsilon;
24 }
25
26 } // anonymous namespace
27
28 ////////////////////////////////////////////////////////////////////////////////
29 // Gamepads, public:
30
31 Gamepads::Gamepads(GamepadsDelegate* delegate) : Gamepads(delegate, nullptr) {}
32
33 Gamepads::Gamepads(GamepadsDelegate* delegate,
34 std::unique_ptr<device::GamepadDataFetcher> fetcher)
35 : delegate_(delegate),
36 fetcher_(std::move(fetcher)),
37 polling_thread_(new base::Thread("Exo gamepad polling thread")),
38 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
39 polling_thread_->StartWithOptions(
40 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
41 polling_thread_->task_runner()->PostTask(
42 FROM_HERE,
43 base::Bind(&Gamepads::InitializePollingThread, base::Unretained(this)));
44 }
45
46 Gamepads::~Gamepads() {
47 delegate_->OnGamepadsDestroying(this);
48 polling_thread_->Stop();
49 }
50
51 ////////////////////////////////////////////////////////////////////////////////
52 // Gamepads, private:
53
54 void Gamepads::ScheduleOnPoll() {
55 polling_thread_->task_runner()->PostDelayedTask(
56 FROM_HERE, base::Bind(&Gamepads::OnPoll, base::Unretained(this)),
57 base::TimeDelta::FromMilliseconds(16));
58 }
59
60 void Gamepads::InitializePollingThread() {
61 DCHECK(base::MessageLoop::current() == polling_thread_->message_loop());
62 // The platform data fetcher has to be initialized on the polling thread.
63 if (!fetcher_)
64 fetcher_.reset(new device::GamepadPlatformDataFetcher());
reveman 2016/06/27 17:30:34 Would be nice if this was always passed to the cto
65 ScheduleOnPoll();
66 }
67
68 void Gamepads::OnPoll() {
69 DCHECK(base::MessageLoop::current() == polling_thread_->message_loop());
70 DCHECK(fetcher_);
71
72 // Update state with data from gamepad data fetcher
73 blink::WebGamepads new_state = state_;
74 fetcher_->GetGamepadData(&new_state, false);
75
76 // Notify delegate of all changes since the last gamepad state
77 for (size_t i = 0; i < std::max(state_.length, new_state.length); ++i) {
78 bool send_frame = false;
79 const blink::WebGamepad& new_pad = new_state.items[i];
80 blink::WebGamepad& pad = state_.items[i];
81
82 // Update connection state
83 if (new_pad.connected != pad.connected) {
84 if (new_pad.connected) {
85 origin_task_runner_->PostTask(
86 FROM_HERE, base::Bind(&GamepadsDelegate::OnConnected,
87 base::Unretained(delegate_), i));
88 } else {
89 origin_task_runner_->PostTask(
90 FROM_HERE, base::Bind(&GamepadsDelegate::OnDisconnected,
91 base::Unretained(delegate_), i));
92 }
93 }
94
95 if (!new_pad.connected || new_pad.timestamp <= pad.timestamp)
96 continue;
97
98 // Notify delegate of updated axes
99 for (size_t axis = 0; axis < std::max(pad.axesLength, new_pad.axesLength);
100 ++axis) {
101 if (!almostEquals(new_pad.axes[axis], pad.axes[axis])) {
102 send_frame = true;
103 origin_task_runner_->PostTask(
104 FROM_HERE,
105 base::Bind(&GamepadsDelegate::OnAxis, base::Unretained(delegate_),
106 i, axis, new_pad.axes[axis]));
107 }
108 }
109
110 // Notify delegate of updated buttons
111 for (size_t button = 0;
112 button < std::max(pad.buttonsLength, new_pad.buttonsLength);
113 ++button) {
114 if (!almostEquals(new_pad.buttons[button].value,
115 pad.buttons[button].value) ||
116 pad.buttons[button].pressed != new_pad.buttons[button].pressed) {
117 send_frame = true;
118 origin_task_runner_->PostTask(
119 FROM_HERE,
120 base::Bind(&GamepadsDelegate::OnButton, base::Unretained(delegate_),
121 i, button, new_pad.buttons[button].pressed,
122 new_pad.buttons[button].value));
123 }
124 }
125 if (send_frame) {
126 origin_task_runner_->PostTask(
127 FROM_HERE, base::Bind(&GamepadsDelegate::OnFrame,
128 base::Unretained(delegate_), i));
129 }
130 }
131 state_ = new_state;
132
133 ScheduleOnPoll();
134 }
135
136 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698