| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 package org.chromium.content.browser.input; | |
| 6 | |
| 7 import android.os.SystemClock; | |
| 8 import android.view.InputDevice; | |
| 9 import android.view.InputDevice.MotionRange; | |
| 10 import android.view.KeyEvent; | |
| 11 import android.view.MotionEvent; | |
| 12 | |
| 13 import org.chromium.base.VisibleForTesting; | |
| 14 | |
| 15 import java.util.Arrays; | |
| 16 import java.util.List; | |
| 17 | |
| 18 /** | |
| 19 * Manages information related to each connected gamepad device. | |
| 20 */ | |
| 21 class GamepadDevice { | |
| 22 // Axis ids are used as indices which are empirically always smaller than 25
6 so this allows | |
| 23 // us to create cheap associative arrays. | |
| 24 @VisibleForTesting | |
| 25 static final int MAX_RAW_AXIS_VALUES = 256; | |
| 26 | |
| 27 // Keycodes are used as indices which are empirically always smaller than 25
6 so this allows | |
| 28 // us to create cheap associative arrays. | |
| 29 @VisibleForTesting | |
| 30 static final int MAX_RAW_BUTTON_VALUES = 256; | |
| 31 | |
| 32 // An id for the gamepad. | |
| 33 private int mDeviceId; | |
| 34 // The index of the gamepad in the Navigator. | |
| 35 private int mDeviceIndex; | |
| 36 // Last time the data for this gamepad was updated. | |
| 37 private long mTimestamp; | |
| 38 // If this gamepad is mapped to standard gamepad? | |
| 39 private boolean mIsStandardGamepad; | |
| 40 | |
| 41 // Array of values for all axes of the gamepad. | |
| 42 // All axis values must be linearly normalized to the range [-1.0 .. 1.0]. | |
| 43 // As appropriate, -1.0 should correspond to "up" or "left", and 1.0 | |
| 44 // should correspond to "down" or "right". | |
| 45 private final float[] mAxisValues = new float[CanonicalAxisIndex.COUNT]; | |
| 46 | |
| 47 private final float[] mButtonsValues = new float[CanonicalButtonIndex.COUNT]
; | |
| 48 | |
| 49 // When the user agent recognizes the attached inputDevice, it is recommende
d | |
| 50 // that it be remapped to a canonical ordering when possible. Devices that a
re | |
| 51 // not recognized should still be exposed in their raw form. Therefore we mu
st | |
| 52 // pass the raw Button and raw Axis values. | |
| 53 private final float[] mRawButtons = new float[MAX_RAW_BUTTON_VALUES]; | |
| 54 private final float[] mRawAxes = new float[MAX_RAW_AXIS_VALUES]; | |
| 55 | |
| 56 // An identification string for the gamepad. | |
| 57 private String mDeviceName; | |
| 58 | |
| 59 // Array of axes ids. | |
| 60 private int[] mAxes; | |
| 61 | |
| 62 GamepadDevice(int index, InputDevice inputDevice) { | |
| 63 mDeviceIndex = index; | |
| 64 mDeviceId = inputDevice.getId(); | |
| 65 mDeviceName = inputDevice.getName(); | |
| 66 mTimestamp = SystemClock.uptimeMillis(); | |
| 67 // Get axis ids and initialize axes values. | |
| 68 final List<MotionRange> ranges = inputDevice.getMotionRanges(); | |
| 69 mAxes = new int[ranges.size()]; | |
| 70 int i = 0; | |
| 71 for (MotionRange range : ranges) { | |
| 72 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | |
| 73 int axis = range.getAxis(); | |
| 74 assert axis < MAX_RAW_AXIS_VALUES; | |
| 75 mAxes[i++] = axis; | |
| 76 } | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * Updates the axes and buttons maping of a gamepad device to a standard gam
epad format. | |
| 82 */ | |
| 83 public void updateButtonsAndAxesMapping() { | |
| 84 mIsStandardGamepad = GamepadMappings.mapToStandardGamepad( | |
| 85 mAxisValues, mButtonsValues, mRawAxes, mRawButtons, mDeviceName)
; | |
| 86 } | |
| 87 | |
| 88 /** | |
| 89 * @return Device Id of the gamepad device. | |
| 90 */ | |
| 91 public int getId() { | |
| 92 return mDeviceId; | |
| 93 } | |
| 94 | |
| 95 /** | |
| 96 * @return Mapping status of the gamepad device. | |
| 97 */ | |
| 98 public boolean isStandardGamepad() { | |
| 99 return mIsStandardGamepad; | |
| 100 } | |
| 101 | |
| 102 /** | |
| 103 * @return Device name of the gamepad device. | |
| 104 */ | |
| 105 public String getName() { | |
| 106 return mDeviceName; | |
| 107 } | |
| 108 | |
| 109 /** | |
| 110 * @return Device index of the gamepad device. | |
| 111 */ | |
| 112 public int getIndex() { | |
| 113 return mDeviceIndex; | |
| 114 } | |
| 115 | |
| 116 /** | |
| 117 * @return The timestamp when the gamepad device was last interacted. | |
| 118 */ | |
| 119 public long getTimestamp() { | |
| 120 return mTimestamp; | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * @return The axes state of the gamepad device. | |
| 125 */ | |
| 126 public float[] getAxes() { | |
| 127 return mAxisValues; | |
| 128 } | |
| 129 | |
| 130 /** | |
| 131 * @return The buttons state of the gamepad device. | |
| 132 */ | |
| 133 public float[] getButtons() { | |
| 134 return mButtonsValues; | |
| 135 } | |
| 136 | |
| 137 /** | |
| 138 * Reset the axes and buttons data of the gamepad device everytime gamepad d
ata access is | |
| 139 * paused. | |
| 140 */ | |
| 141 public void clearData() { | |
| 142 Arrays.fill(mAxisValues, 0); | |
| 143 Arrays.fill(mRawAxes, 0); | |
| 144 Arrays.fill(mButtonsValues, 0); | |
| 145 Arrays.fill(mRawButtons, 0); | |
| 146 } | |
| 147 | |
| 148 /** | |
| 149 * Handles key event from the gamepad device. | |
| 150 * @return True if the key event from the gamepad device has been consumed. | |
| 151 */ | |
| 152 public boolean handleKeyEvent(KeyEvent event) { | |
| 153 // Ignore event if it is not for standard gamepad key. | |
| 154 if (!GamepadList.isGamepadEvent(event)) return false; | |
| 155 int keyCode = event.getKeyCode(); | |
| 156 assert keyCode < MAX_RAW_BUTTON_VALUES; | |
| 157 // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully p
ressed. | |
| 158 if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
| 159 mRawButtons[keyCode] = 1.0f; | |
| 160 } else if (event.getAction() == KeyEvent.ACTION_UP) { | |
| 161 mRawButtons[keyCode] = 0.0f; | |
| 162 } | |
| 163 mTimestamp = event.getEventTime(); | |
| 164 | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 /** | |
| 169 * Handles motion event from the gamepad device. | |
| 170 * @return True if the motion event from the gamepad device has been consume
d. | |
| 171 */ | |
| 172 public boolean handleMotionEvent(MotionEvent event) { | |
| 173 // Ignore event if it is not a standard gamepad motion event. | |
| 174 if (!GamepadList.isGamepadEvent(event)) return false; | |
| 175 // Update axes values. | |
| 176 for (int i = 0; i < mAxes.length; i++) { | |
| 177 int axis = mAxes[i]; | |
| 178 mRawAxes[axis] = event.getAxisValue(axis); | |
| 179 } | |
| 180 mTimestamp = event.getEventTime(); | |
| 181 return true; | |
| 182 } | |
| 183 } | |
| OLD | NEW |