File: gamepad_comparisons.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (285 lines) | stat: -rw-r--r-- 10,295 bytes parent folder | download | duplicates (6)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/gamepad/gamepad_comparisons.h"

#include "third_party/blink/renderer/modules/gamepad/gamepad.h"

namespace blink {

namespace {

// A button press must have a value at least this large to qualify as a user
// activation. The selected value should be greater than 0.5 so that axes
// incorrectly mapped as triggers do not generate activations in the idle
// position.
const double kButtonActivationThreshold = 0.9;

template <typename T>
auto AsSpan(const T& collection) {
  return collection.AsSpan();
}

base::span<const GamepadTouchVector::ValueType> AsSpan(
    const GamepadTouchVector& collection) {
  return base::span(collection);
}

template <typename Collection,
          typename Pred = std::equal_to<typename Collection::ValueType>>
bool Compare(const Collection* old_array,
             const Collection* new_array,
             Pred pred = Pred{}) {
  if (old_array && new_array) {
    // Both arrays are non-null.
    return !std::ranges::equal(AsSpan(*old_array), AsSpan(*new_array), pred);
  } else if (old_array != new_array) {
    // Exactly one array is non-null.
    return true;
  }
  // Both arrays are null, or the arrays are identical.
  return false;
}

}  // namespace

// static
bool GamepadComparisons::HasUserActivation(
    const HeapVector<Member<Gamepad>> gamepads) {
  // A button press counts as a user activation if the button's value is greater
  // than the activation threshold. A threshold is used so that analog buttons
  // or triggers do not generate an activation from a light touch.
  for (Gamepad* pad : gamepads) {
    if (pad) {
      for (auto button : pad->buttons()) {
        if (button->value() > kButtonActivationThreshold)
          return true;
      }
    }
  }
  return false;
}

// static
void GamepadComparisons::HasGamepadConnectionChanged(bool old_connected,
                                                     bool new_connected,
                                                     bool id_changed,
                                                     bool* gamepad_found,
                                                     bool* gamepad_lost) {
  if (gamepad_found)
    *gamepad_found = id_changed || (!old_connected && new_connected);
  if (gamepad_lost)
    *gamepad_lost = id_changed || (old_connected && !new_connected);
}

GamepadStateCompareResult::GamepadStateCompareResult(
    const HeapVector<Member<Gamepad>> old_gamepads,
    const HeapVector<Member<Gamepad>> new_gamepads,
    bool compare_all_axes,
    bool compare_all_buttons) {
  any_change_ = CompareGamepads(old_gamepads, new_gamepads, compare_all_axes,
                                compare_all_buttons);
}

bool GamepadStateCompareResult::IsDifferent() const {
  return any_change_;
}

bool GamepadStateCompareResult::IsGamepadConnected(size_t pad_index) const {
  DCHECK_LT(pad_index, device::Gamepads::kItemsLengthCap);
  return gamepad_connected_.test(pad_index);
}

bool GamepadStateCompareResult::IsGamepadDisconnected(size_t pad_index) const {
  DCHECK_LT(pad_index, device::Gamepads::kItemsLengthCap);
  return gamepad_disconnected_.test(pad_index);
}

bool GamepadStateCompareResult::IsAxisChanged(size_t pad_index,
                                              size_t axis_index) const {
  DCHECK_LT(pad_index, device::Gamepads::kItemsLengthCap);
  DCHECK_LT(axis_index, device::Gamepad::kAxesLengthCap);
  return axis_changed_[pad_index].test(axis_index);
}

bool GamepadStateCompareResult::IsButtonChanged(size_t pad_index,
                                                size_t button_index) const {
  DCHECK_LT(pad_index, device::Gamepads::kItemsLengthCap);
  DCHECK_LT(button_index, device::Gamepad::kButtonsLengthCap);
  return button_changed_[pad_index].test(button_index);
}

bool GamepadStateCompareResult::IsButtonDown(size_t pad_index,
                                             size_t button_index) const {
  DCHECK_LT(pad_index, device::Gamepads::kItemsLengthCap);
  DCHECK_LT(button_index, device::Gamepad::kButtonsLengthCap);
  return button_down_[pad_index].test(button_index);
}

bool GamepadStateCompareResult::IsButtonUp(size_t pad_index,
                                           size_t button_index) const {
  DCHECK_LT(pad_index, device::Gamepads::kItemsLengthCap);
  DCHECK_LT(button_index, device::Gamepad::kButtonsLengthCap);
  return button_up_[pad_index].test(button_index);
}

bool GamepadStateCompareResult::CompareGamepads(
    const HeapVector<Member<Gamepad>> old_gamepads,
    const HeapVector<Member<Gamepad>> new_gamepads,
    bool compare_all_axes,
    bool compare_all_buttons) {
  bool any_change = false;
  for (uint32_t i = 0; i < new_gamepads.size(); ++i) {
    Gamepad* old_gamepad = i < old_gamepads.size() ? old_gamepads[i] : nullptr;
    Gamepad* new_gamepad = new_gamepads[i];
    // Check whether the gamepad is newly connected or disconnected.
    bool newly_connected = false;
    bool newly_disconnected = false;
    bool old_connected = old_gamepad && old_gamepad->connected();
    bool new_connected = new_gamepad && new_gamepad->connected();
    if (old_gamepad && new_gamepad) {
      GamepadComparisons::HasGamepadConnectionChanged(
          old_connected, new_connected, old_gamepad->id() != new_gamepad->id(),
          &newly_connected, &newly_disconnected);
    } else {
      newly_connected = new_connected;
      newly_disconnected = old_connected;
    }

    bool any_axis_updated =
        CompareAxes(old_gamepad, new_gamepad, i, compare_all_axes);
    bool any_button_updated =
        CompareButtons(old_gamepad, new_gamepad, i, compare_all_buttons);
    bool any_touch_updated = CompareTouches(old_gamepad, new_gamepad);

    if (newly_connected)
      gamepad_connected_.set(i);
    if (newly_disconnected)
      gamepad_disconnected_.set(i);
    if (newly_connected || newly_disconnected || any_axis_updated ||
        any_button_updated || any_touch_updated) {
      any_change = true;
    }
  }
  return any_change;
}

bool GamepadStateCompareResult::CompareAxes(Gamepad* old_gamepad,
                                            Gamepad* new_gamepad,
                                            size_t index,
                                            bool compare_all) {
  DCHECK_LT(index, device::Gamepads::kItemsLengthCap);
  if (!new_gamepad)
    return false;
  auto& changed_set = axis_changed_[index];
  const auto& new_axes = new_gamepad->axes();
  const auto* old_axes = old_gamepad ? &old_gamepad->axes() : nullptr;
  bool any_axis_changed = false;
  for (wtf_size_t i = 0; i < new_axes.size(); ++i) {
    double new_value = new_axes[i];
    if (old_axes && i < old_axes->size()) {
      double old_value = old_axes->at(i);
      if (old_value != new_value) {
        any_axis_changed = true;
        if (!compare_all)
          break;
        changed_set.set(i);
      }
    } else {
      if (new_value) {
        any_axis_changed = true;
        if (!compare_all)
          break;
        changed_set.set(i);
      }
    }
  }
  return any_axis_changed;
}

bool GamepadStateCompareResult::CompareButtons(Gamepad* old_gamepad,
                                               Gamepad* new_gamepad,
                                               size_t index,
                                               bool compare_all) {
  DCHECK_LT(index, device::Gamepads::kItemsLengthCap);
  if (!new_gamepad)
    return false;
  auto& changed_set = button_changed_[index];
  auto& down_set = button_down_[index];
  auto& up_set = button_up_[index];
  const auto& new_buttons = new_gamepad->buttons();
  const auto* old_buttons = old_gamepad ? &old_gamepad->buttons() : nullptr;
  bool any_button_changed = false;
  for (wtf_size_t i = 0; i < new_buttons.size(); ++i) {
    double new_value = new_buttons[i]->value();
    bool new_pressed = new_buttons[i]->pressed();
    if (old_buttons && i < old_buttons->size()) {
      double old_value = old_buttons->at(i)->value();
      bool old_pressed = old_buttons->at(i)->pressed();
      if (old_value != new_value) {
        any_button_changed = true;
        if (!compare_all)
          break;
        changed_set.set(i);
      }
      if (old_pressed != new_pressed) {
        any_button_changed = true;
        if (!compare_all)
          break;
        if (new_pressed)
          down_set.set(i);
        else
          up_set.set(i);
      }
    } else {
      if (new_value > 0.0) {
        any_button_changed = true;
        if (!compare_all)
          break;
        changed_set.set(i);
      }
      if (new_pressed) {
        any_button_changed = true;
        if (!compare_all)
          break;
        down_set.set(i);
      }
    }
  }
  return any_button_changed;
}

bool GamepadStateCompareResult::CompareTouches(Gamepad* old_gamepad,
                                               Gamepad* new_gamepad) {
  if (!new_gamepad) {
    return false;
  }

  const auto* new_touches = new_gamepad->touchEvents();
  const auto* old_touches = old_gamepad ? old_gamepad->touchEvents() : nullptr;

  return Compare(old_touches, new_touches,
                 [](const Member<GamepadTouch>& new_touch,
                    const Member<GamepadTouch>& old_touch) {
                   return new_touch->touchId() == old_touch->touchId() &&
                          new_touch->surfaceId() == old_touch->surfaceId() &&
                          new_touch->HasSurfaceDimensions() ==
                              old_touch->HasSurfaceDimensions() &&
                          !Compare(new_touch->surfaceDimensions().Get(),
                                   old_touch->surfaceDimensions().Get()) &&
                          !Compare(new_touch->position().Get(),
                                   old_touch->position().Get());
                 });
}

GamepadStateCompareResult GamepadComparisons::Compare(
    const HeapVector<Member<Gamepad>> old_gamepads,
    const HeapVector<Member<Gamepad>> new_gamepads,
    bool compare_all_axes,
    bool compare_all_buttons) {
  return GamepadStateCompareResult(old_gamepads, new_gamepads, compare_all_axes,
                                   compare_all_buttons);
}

}  // namespace blink