OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 package org.chromium.content.browser.input; | 5 package org.chromium.content.browser.input; |
6 | 6 |
7 import android.os.SystemClock; | 7 import android.os.SystemClock; |
8 import android.view.InputDevice; | 8 import android.view.InputDevice; |
9 import android.view.InputDevice.MotionRange; | 9 import android.view.InputDevice.MotionRange; |
10 import android.view.KeyEvent; | 10 import android.view.KeyEvent; |
11 import android.view.MotionEvent; | 11 import android.view.MotionEvent; |
12 | 12 |
13 import java.util.Arrays; | 13 import java.util.Arrays; |
| 14 import java.util.List; |
14 | 15 |
15 /** | 16 /** |
16 * Manages information related to each connected gamepad device. | 17 * Manages information related to each connected gamepad device. |
17 */ | 18 */ |
18 class GamepadDevice { | 19 class GamepadDevice { |
19 // An id for the gamepad. | 20 // An id for the gamepad. |
20 private int mDeviceId; | 21 private int mDeviceId; |
21 // The index of the gamepad in the Navigator. | 22 // The index of the gamepad in the Navigator. |
22 private int mDeviceIndex; | 23 private int mDeviceIndex; |
23 // Last time the data for this gamepad was updated. | 24 // Last time the data for this gamepad was updated. |
24 private long mTimestamp; | 25 private long mTimestamp; |
25 // If this gamepad is mapped to standard gamepad? | 26 // If this gamepad is mapped to standard gamepad? |
26 private boolean mIsStandardGamepad; | 27 private boolean mIsStandardGamepad; |
27 | 28 |
28 // Array of values for all axes of the gamepad. | 29 // Array of values for all axes of the gamepad. |
29 // All axis values must be linearly normalized to the range [-1.0 .. 1.0]. | 30 // All axis values must be linearly normalized to the range [-1.0 .. 1.0]. |
30 // As appropriate, -1.0 should correspond to "up" or "left", and 1.0 | 31 // As appropriate, -1.0 should correspond to "up" or "left", and 1.0 |
31 // should correspond to "down" or "right". | 32 // should correspond to "down" or "right". |
32 private float[] mAxisValues; | 33 private final float[] mAxisValues = new float[CanonicalAxisIndex.NUM_CANONIC
AL_AXES]; |
33 | 34 |
34 private float[] mButtonsValues; | 35 private final float[] mButtonsValues = new float[CanonicalButtonIndex.NUM_CA
NONICAL_BUTTONS];; |
35 | 36 |
36 // When the user agent recognizes the attached inputDevice, it is recommende
d | 37 // When the user agent recognizes the attached inputDevice, it is recommende
d |
37 // that it be remapped to a canonical ordering when possible. Devices that a
re | 38 // that it be remapped to a canonical ordering when possible. Devices that a
re |
38 // not recognized should still be exposed in their raw form. Therefore we mu
st | 39 // not recognized should still be exposed in their raw form. Therefore we mu
st |
39 // pass the raw Button and raw Axis values. | 40 // pass the raw Button and raw Axis values. |
40 private float[] mRawButtons; | 41 private final float[] mRawButtons = new float[256]; |
41 private float[] mRawAxes; | 42 private final float[] mRawAxes = new float[256]; |
42 | 43 |
43 // An identification string for the gamepad. | 44 // An identification string for the gamepad. |
44 private String mDeviceName; | 45 private String mDeviceName; |
45 | 46 |
46 // Array of axes ids. | 47 // Array of axes ids. |
47 private int[] mAxes; | 48 private int[] mAxes; |
48 | 49 |
49 GamepadDevice(int index, InputDevice inputDevice) { | 50 GamepadDevice(int index, InputDevice inputDevice) { |
50 mDeviceIndex = index; | 51 mDeviceIndex = index; |
51 mDeviceId = inputDevice.getId(); | 52 mDeviceId = inputDevice.getId(); |
52 mDeviceName = inputDevice.getName(); | 53 mDeviceName = inputDevice.getName(); |
53 mTimestamp = SystemClock.uptimeMillis(); | 54 mTimestamp = SystemClock.uptimeMillis(); |
54 mButtonsValues = new float[CanonicalButtonIndex.NUM_CANONICAL_BUTTONS]; | 55 // Get axis ids and initialize axes values. |
55 mAxisValues = new float[CanonicalAxisIndex.NUM_CANONICAL_AXES]; | 56 final List<MotionRange> ranges = inputDevice.getMotionRanges(); |
56 mRawButtons = new float[256]; | 57 mAxes = new int[ranges.size()]; |
57 // Get the total number of axes supported by gamepad. | 58 int i = 0; |
58 int totalAxes = 0; | 59 for (MotionRange range : ranges) { |
59 for (MotionRange range : inputDevice.getMotionRanges()) { | |
60 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | 60 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { |
61 totalAxes++; | 61 int axis = range.getAxis(); |
62 } | 62 assert axis < 256; |
63 } | 63 mAxes[i++] = axis; |
64 // Get axis ids and initialize axes values. | |
65 mAxes = new int[totalAxes]; | |
66 mRawAxes = new float[totalAxes]; | |
67 int i = 0; | |
68 for (MotionRange range : inputDevice.getMotionRanges()) { | |
69 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | |
70 mAxes[i] = range.getAxis(); | |
71 mRawAxes[i] = 0.0f; | |
72 i++; | |
73 } | 64 } |
74 } | 65 } |
75 } | 66 } |
76 | 67 |
77 /** | 68 /** |
78 * Updates the axes and buttons maping of a gamepad device to a standard gam
epad format. | 69 * Updates the axes and buttons maping of a gamepad device to a standard gam
epad format. |
79 */ | 70 */ |
80 public void updateButtonsAndAxesMapping() { | 71 public void updateButtonsAndAxesMapping() { |
81 mIsStandardGamepad = GamepadMappings.mapToStandardGamepad( | 72 mIsStandardGamepad = GamepadMappings.mapToStandardGamepad( |
82 mAxisValues, mButtonsValues, mRawAxes, mRawButtons, mDeviceName)
; | 73 mAxisValues, mButtonsValues, mRawAxes, mRawButtons, mDeviceName)
; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 141 |
151 /** | 142 /** |
152 * Handles motion event from the gamepad device. | 143 * Handles motion event from the gamepad device. |
153 * @return True if the motion event from the gamepad device has been consume
d. | 144 * @return True if the motion event from the gamepad device has been consume
d. |
154 */ | 145 */ |
155 public boolean handleMotionEvent(MotionEvent event) { | 146 public boolean handleMotionEvent(MotionEvent event) { |
156 // Ignore event if it is not a standard gamepad motion event. | 147 // Ignore event if it is not a standard gamepad motion event. |
157 if (!GamepadList.isGamepadEvent(event)) return false; | 148 if (!GamepadList.isGamepadEvent(event)) return false; |
158 // Update axes values. | 149 // Update axes values. |
159 for (int i = 0; i < mAxes.length; i++) { | 150 for (int i = 0; i < mAxes.length; i++) { |
160 mRawAxes[i] = event.getAxisValue(mAxes[i]); | 151 int axis = mAxes[i]; |
| 152 mRawAxes[axis] = event.getAxisValue(axis); |
161 } | 153 } |
162 mTimestamp = event.getEventTime(); | 154 mTimestamp = event.getEventTime(); |
163 return true; | 155 return true; |
164 } | 156 } |
165 | |
166 } | 157 } |
OLD | NEW |