1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/*
* Copyright (C) 2011, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#include "third_party/blink/renderer/modules/gamepad/gamepad.h"
#include <algorithm>
#include "base/trace_event/trace_event.h"
#include "third_party/blink/renderer/core/timing/performance.h"
#include "third_party/blink/renderer/modules/gamepad/gamepad_comparisons.h"
#include "third_party/blink/renderer/platform/wtf/text/string_view.h"
namespace blink {
Gamepad::Gamepad(Client* client,
int index,
base::TimeTicks time_origin,
base::TimeTicks time_floor)
: client_(client),
index_(index),
timestamp_(0.0),
has_vibration_actuator_(false),
vibration_actuator_type_(device::GamepadHapticActuatorType::kDualRumble),
has_touch_events_(false),
is_axis_data_dirty_(true),
is_button_data_dirty_(true),
is_touch_data_dirty_(true),
time_origin_(time_origin),
time_floor_(time_floor) {
DCHECK(!time_origin_.is_null());
DCHECK(!time_floor_.is_null());
DCHECK_LE(time_origin_, time_floor_);
}
Gamepad::~Gamepad() = default;
void Gamepad::UpdateFromDeviceState(const device::Gamepad& device_gamepad,
bool cross_origin_isolated_capability) {
bool newly_connected;
GamepadComparisons::HasGamepadConnectionChanged(
connected(), // Old connected.
device_gamepad.connected, // New connected.
id() != StringView(device_gamepad.id), // ID changed.
&newly_connected, nullptr);
SetConnected(device_gamepad.connected);
SetTimestamp(device_gamepad, cross_origin_isolated_capability);
SetAxes(base::span(device_gamepad.axes).first(device_gamepad.axes_length));
SetButtons(
base::span(device_gamepad.buttons).first(device_gamepad.buttons_length));
if (device_gamepad.supports_touch_events_) {
SetTouchEvents(base::span(device_gamepad.touch_events)
.first(device_gamepad.touch_events_length));
}
// Always called as gamepads require additional steps to determine haptics
// capability and thus may provide them when not |newly_connected|. This is
// also simpler than logic to conditionally call.
SetVibrationActuatorInfo(device_gamepad.vibration_actuator);
// These fields are not expected to change and will only be written when the
// gamepad is newly connected.
if (newly_connected) {
SetId(device_gamepad.id);
SetMapping(device_gamepad.mapping);
}
}
void Gamepad::SetMapping(device::GamepadMapping mapping) {
switch (mapping) {
case device::GamepadMapping::kNone:
mapping_ = "";
return;
case device::GamepadMapping::kStandard:
mapping_ = "standard";
return;
case device::GamepadMapping::kXrStandard:
mapping_ = "xr-standard";
return;
}
NOTREACHED();
}
const Gamepad::DoubleVector& Gamepad::axes() {
is_axis_data_dirty_ = false;
return axes_;
}
void Gamepad::SetAxes(base::span<const double> data) {
if (std::ranges::equal(data, axes_)) {
return;
}
axes_.resize(base::checked_cast<wtf_size_t>(data.size()));
if (!data.empty()) {
base::span(axes_).copy_from(data);
}
is_axis_data_dirty_ = true;
}
const GamepadButtonVector& Gamepad::buttons() {
is_button_data_dirty_ = false;
return buttons_;
}
const GamepadTouchVector* Gamepad::touchEvents() {
is_touch_data_dirty_ = false;
if (!has_touch_events_) {
return nullptr;
}
return &touch_events_;
}
void Gamepad::SetTouchEvents(base::span<const device::GamepadTouch> data) {
has_touch_events_ = true;
if (data.empty()) {
touch_events_.clear();
return;
}
bool skip_update =
std::ranges::equal(data, touch_events_,
[](const device::GamepadTouch& device_gamepad_touch,
const Member<GamepadTouch>& gamepad_touch) {
return gamepad_touch->IsEqual(device_gamepad_touch);
});
if (skip_update) {
return;
}
if (touch_events_.size() != data.size()) {
touch_events_.clear();
touch_events_.resize(base::checked_cast<wtf_size_t>(data.size()));
for (size_t i = 0; i < data.size(); ++i) {
touch_events_[i] = MakeGarbageCollected<GamepadTouch>();
}
}
if (client_) {
client_->SetTouchEvents(*this, touch_events_, data);
} else {
for (size_t i = 0; i < data.size(); ++i) {
touch_events_[i]->UpdateValuesFrom(data[i], data[i].touch_id);
}
}
is_touch_data_dirty_ = true;
}
void Gamepad::SetButtons(base::span<const device::GamepadButton> data) {
bool skip_update = std::ranges::equal(
data, buttons_,
[](const device::GamepadButton& device_gamepad_button,
const Member<GamepadButton>& gamepad_button) {
return gamepad_button->IsEqual(device_gamepad_button);
});
if (skip_update)
return;
if (buttons_.size() != data.size()) {
buttons_.resize(base::checked_cast<wtf_size_t>(data.size()));
for (size_t i = 0; i < data.size(); ++i) {
buttons_[i] = MakeGarbageCollected<GamepadButton>();
}
}
for (size_t i = 0; i < data.size(); ++i) {
buttons_[i]->UpdateValuesFrom(data[i]);
}
is_button_data_dirty_ = true;
}
GamepadHapticActuator* Gamepad::vibrationActuator() const {
return client_->GetVibrationActuatorForGamepad(*this);
}
void Gamepad::SetVibrationActuatorInfo(
const device::GamepadHapticActuator& actuator) {
has_vibration_actuator_ = actuator.not_null;
vibration_actuator_type_ = actuator.type;
}
// Convert the raw timestamp from the device to a relative one and apply the
// floor.
void Gamepad::SetTimestamp(const device::Gamepad& device_gamepad,
bool cross_origin_isolated_capability) {
base::TimeTicks last_updated =
base::TimeTicks() + base::Microseconds(device_gamepad.timestamp);
if (last_updated < time_floor_)
last_updated = time_floor_;
timestamp_ = Performance::MonotonicTimeToDOMHighResTimeStamp(
time_origin_, last_updated, /*allow_negative_value=*/false,
cross_origin_isolated_capability);
if (device_gamepad.is_xr) {
base::TimeTicks now = base::TimeTicks::Now();
TRACE_COUNTER1("input", "XR gamepad pose age (ms)",
(now - last_updated).InMilliseconds());
}
}
void Gamepad::Trace(Visitor* visitor) const {
visitor->Trace(client_);
visitor->Trace(buttons_);
visitor->Trace(touch_events_);
ScriptWrappable::Trace(visitor);
}
} // namespace blink
|