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