| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/gamepad/gamepad_data_fetcher.h" | 5 #include "device/gamepad/gamepad_data_fetcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | |
| 8 #include <string.h> | |
| 9 | |
| 10 #include <cmath> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "build/build_config.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 #if !defined(OS_ANDROID) | |
| 18 const float kMinAxisResetValue = 0.1f; | |
| 19 #endif | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 namespace device { | 7 namespace device { |
| 24 | 8 |
| 25 using blink::WebGamepad; | 9 GamepadDataFetcher::GamepadDataFetcher() : provider_(nullptr) {} |
| 26 using blink::WebGamepads; | |
| 27 | 10 |
| 28 #if !defined(OS_ANDROID) | 11 void GamepadDataFetcher::InitializeProvider(GamepadProvider* provider) { |
| 29 void GamepadDataFetcher::MapAndSanitizeGamepadData(PadState* pad_state, | 12 DCHECK(provider); |
| 30 WebGamepad* pad) { | |
| 31 DCHECK(pad_state); | |
| 32 DCHECK(pad); | |
| 33 | 13 |
| 34 if (!pad_state->data.connected) { | 14 provider_ = provider; |
| 35 memset(pad, 0, sizeof(WebGamepad)); | 15 OnAddedToProvider(); |
| 36 return; | 16 } |
| 37 } | |
| 38 | 17 |
| 39 // Copy the current state to the output buffer, using the mapping | 18 GamepadDataFetcherFactory::GamepadDataFetcherFactory() {} |
| 40 // function, if there is one available. | |
| 41 if (pad_state->mapper) | |
| 42 pad_state->mapper(pad_state->data, pad); | |
| 43 else | |
| 44 *pad = pad_state->data; | |
| 45 | |
| 46 // About sanitization: Gamepads may report input event if the user is not | |
| 47 // interacting with it, due to hardware problems or environmental ones (pad | |
| 48 // has something heavy leaning against an axis.) This may cause user gestures | |
| 49 // to be detected erroniously, exposing gamepad information when the user had | |
| 50 // no intention of doing so. To avoid this we require that each button or axis | |
| 51 // report being at rest (zero) at least once before exposing its value to the | |
| 52 // Gamepad API. This state is tracked by the axis_mask and button_mask | |
| 53 // bitfields. If the bit for an axis or button is 0 it means the axis has | |
| 54 // never reported being at rest, and the value will be forced to zero. | |
| 55 | |
| 56 // We can skip axis sanitation if all available axes have been masked. | |
| 57 uint32_t full_axis_mask = (1 << pad->axesLength) - 1; | |
| 58 if (pad_state->axis_mask != full_axis_mask) { | |
| 59 for (size_t axis = 0; axis < pad->axesLength; ++axis) { | |
| 60 if (!(pad_state->axis_mask & 1 << axis)) { | |
| 61 if (fabs(pad->axes[axis]) < kMinAxisResetValue) { | |
| 62 pad_state->axis_mask |= 1 << axis; | |
| 63 } else { | |
| 64 pad->axes[axis] = 0.0f; | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 // We can skip button sanitation if all available buttons have been masked. | |
| 71 uint32_t full_button_mask = (1 << pad->buttonsLength) - 1; | |
| 72 if (pad_state->button_mask != full_button_mask) { | |
| 73 for (size_t button = 0; button < pad->buttonsLength; ++button) { | |
| 74 if (!(pad_state->button_mask & 1 << button)) { | |
| 75 if (!pad->buttons[button].pressed) { | |
| 76 pad_state->button_mask |= 1 << button; | |
| 77 } else { | |
| 78 pad->buttons[button].pressed = false; | |
| 79 pad->buttons[button].value = 0.0f; | |
| 80 } | |
| 81 } | |
| 82 } | |
| 83 } | |
| 84 } | |
| 85 #endif | |
| 86 | 19 |
| 87 } // namespace device | 20 } // namespace device |
| OLD | NEW |