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

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: do not depend on webkit but device/gamepad 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
« no previous file with comments | « components/exo/gamepad.h ('k') | components/exo/gamepad_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(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()) {
denniskempin 2016/06/29 18:20:54 reveman: An update on this. Eventually we want to
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));
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
76 blink::WebGamepads new_state = state_;
77 fetcher_->GetGamepadData(&new_state, false);
78
79 if (id_ >= std::max(new_state.length, state_.length))
80 return;
81
82 bool send_frame = false;
83 const blink::WebGamepad& new_pad = new_state.items[id_];
84 blink::WebGamepad& pad = state_.items[id_];
85
86 // Update connection state
87 if (new_pad.connected != pad.connected) {
88 origin_task_runner_->PostTask(
89 FROM_HERE, base::Bind(&GamepadDelegate::OnStateChange,
90 base::Unretained(delegate_), new_pad.connected));
91 }
92
93 if (!new_pad.connected || new_pad.timestamp <= pad.timestamp)
94 return;
95
96 // Notify delegate of updated axes
97 for (size_t axis = 0; axis < std::max(pad.axesLength, new_pad.axesLength);
98 ++axis) {
99 if (!almostEquals(new_pad.axes[axis], pad.axes[axis])) {
100 send_frame = true;
101 origin_task_runner_->PostTask(
102 FROM_HERE,
103 base::Bind(&GamepadDelegate::OnAxis, base::Unretained(delegate_),
104 axis, new_pad.axes[axis]));
105 }
106 }
107
108 // Notify delegate of updated buttons
109 for (size_t button = 0;
110 button < std::max(pad.buttonsLength, new_pad.buttonsLength); ++button) {
111 if (!almostEquals(new_pad.buttons[button].value,
112 pad.buttons[button].value) ||
113 pad.buttons[button].pressed != new_pad.buttons[button].pressed) {
114 send_frame = true;
115 origin_task_runner_->PostTask(
116 FROM_HERE,
117 base::Bind(&GamepadDelegate::OnButton, base::Unretained(delegate_),
118 button, new_pad.buttons[button].pressed,
119 new_pad.buttons[button].value));
120 }
121 }
122 if (send_frame) {
123 origin_task_runner_->PostTask(
124 FROM_HERE,
125 base::Bind(&GamepadDelegate::OnFrame, base::Unretained(delegate_)));
126 }
127
128 state_ = new_state;
129
130 ScheduleOnPoll();
131 }
132
133 } // namespace exo
OLDNEW
« 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