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

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: removed id from Gamepad and updated wording 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 {
21 constexpr double kEpsilon = 0.001;
22 bool almostEquals(double a, double b) {
23 return fabs(a - b) < kEpsilon;
24 }
25
26 } // anonymous namespace
27
28 ////////////////////////////////////////////////////////////////////////////////
29 // Gamepad, public:
30
31 Gamepad::Gamepad(GamepadDelegate* delegate) : Gamepad(delegate, nullptr) {}
32
33 Gamepad::Gamepad(GamepadDelegate* 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(&Gamepad::InitializePollingThread, base::Unretained(this)));
44 }
45
46 Gamepad::~Gamepad() {
47 delegate_->OnGamepadDestroying(this);
48 polling_thread_->Stop();
49 }
50
51 ////////////////////////////////////////////////////////////////////////////////
52 // Gamepad, private:
53
54 void Gamepad::ScheduleOnPoll() {
55 polling_thread_->task_runner()->PostDelayedTask(
56 FROM_HERE, base::Bind(&Gamepad::OnPoll, base::Unretained(this)),
57 base::TimeDelta::FromMilliseconds(16));
58 }
59
60 void Gamepad::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());
65 ScheduleOnPoll();
66 }
67
68 void Gamepad::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 PostGamepadChanges(new_state);
76
77 state_ = new_state;
78 ScheduleOnPoll();
79 }
80
81 void Gamepad::PostGamepadChanges(const blink::WebGamepads new_state) {
82 DCHECK(base::MessageLoop::current() == polling_thread_->message_loop());
83
84 if (std::max(new_state.length, state_.length) > 0)
85 return;
86
87 bool send_frame = false;
88 const blink::WebGamepad& new_pad = new_state.items[0];
89 blink::WebGamepad& pad = state_.items[0];
90
91 // Update connection state
92 if (new_pad.connected != pad.connected) {
93 origin_task_runner_->PostTask(
94 FROM_HERE, base::Bind(&GamepadDelegate::OnStateChange,
95 base::Unretained(delegate_), new_pad.connected));
96 }
97
98 if (!new_pad.connected || new_pad.timestamp <= pad.timestamp)
99 return;
100
101 // Notify delegate of updated axes
102 for (size_t axis = 0; axis < std::max(pad.axesLength, new_pad.axesLength);
103 ++axis) {
104 if (!almostEquals(new_pad.axes[axis], pad.axes[axis])) {
105 send_frame = true;
106 origin_task_runner_->PostTask(
107 FROM_HERE,
108 base::Bind(&GamepadDelegate::OnAxis, base::Unretained(delegate_),
109 axis, new_pad.axes[axis]));
110 }
111 }
112
113 // Notify delegate of updated buttons
114 for (size_t button = 0;
115 button < std::max(pad.buttonsLength, new_pad.buttonsLength); ++button) {
116 if (!almostEquals(new_pad.buttons[button].value,
117 pad.buttons[button].value) ||
118 pad.buttons[button].pressed != new_pad.buttons[button].pressed) {
119 send_frame = true;
120 origin_task_runner_->PostTask(
121 FROM_HERE,
122 base::Bind(&GamepadDelegate::OnButton, base::Unretained(delegate_),
123 button, new_pad.buttons[button].pressed,
124 new_pad.buttons[button].value));
125 }
126 }
127 if (send_frame) {
128 origin_task_runner_->PostTask(
129 FROM_HERE,
130 base::Bind(&GamepadDelegate::OnFrame, base::Unretained(delegate_)));
131 }
132 }
133
134 } // namespace exo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698