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 java.util.Arrays; | |
14 | |
15 /** | |
16 * Manages information related to each connected gamepad device. | |
17 */ | |
18 class GamepadDevice { | |
19 // An id for the gamepad. | |
20 private int mDeviceId; | |
21 // The index of the gamepad in the Navigator. | |
22 private int mDeviceIndex; | |
23 // Last time the data for this gamepad was updated. | |
24 private long mTimestamp; | |
25 // If this gamepad is mapped to standard gamepad? | |
26 private boolean mIsStandardGamepad; | |
27 | |
28 // 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 // As appropriate, -1.0 should correspond to "up" or "left", and 1.0 | |
31 // should correspond to "down" or "right". | |
32 private float[] mAxisValues; | |
33 | |
34 private float[] mButtonsValues; | |
35 | |
36 // 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 // not recognized should still be exposed in their raw form. Therefore we mu
st | |
39 // pass the raw Button and raw Axis values. | |
40 private float[] mRawButtons; | |
41 private float[] mRawAxes; | |
42 | |
43 // An identification string for the gamepad. | |
44 private String mDeviceName; | |
45 | |
46 // Array of axes ids. | |
47 private int[] mAxes; | |
48 | |
49 GamepadDevice(int index, InputDevice inputDevice) { | |
50 mDeviceIndex = index; | |
51 mDeviceId = inputDevice.getId(); | |
52 mDeviceName = inputDevice.getName(); | |
53 mTimestamp = SystemClock.uptimeMillis(); | |
54 mButtonsValues = new float[CanonicalButtonIndex.NUM_CANONICAL_BUTTONS]; | |
55 mAxisValues = new float[CanonicalAxisIndex.NUM_CANONICAL_AXES]; | |
56 mRawButtons = new float[256]; | |
57 // Get the total number of axes supported by gamepad. | |
58 int totalAxes = 0; | |
59 for (MotionRange range : inputDevice.getMotionRanges()) { | |
60 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | |
61 totalAxes++; | |
62 } | |
63 } | |
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 } | |
74 } | |
75 } | |
76 | |
77 /** | |
78 * Updates the axes and buttons maping of a gamepad device to a standard gam
epad format. | |
79 */ | |
80 public void updateButtonsAndAxesMapping() { | |
81 mIsStandardGamepad = GamepadMappings.mapToStandardGamepad( | |
82 mAxisValues, mButtonsValues, mRawAxes, mRawButtons, mDeviceName)
; | |
83 } | |
84 | |
85 /** | |
86 * @return Device Id of the gamepad device. | |
87 */ | |
88 public int getId() { return mDeviceId; } | |
89 | |
90 /** | |
91 * @return Mapping status of the gamepad device. | |
92 */ | |
93 public boolean isStandardGamepad() { return mIsStandardGamepad; } | |
94 | |
95 /** | |
96 * @return Device name of the gamepad device. | |
97 */ | |
98 public String getName() { return mDeviceName; } | |
99 | |
100 /** | |
101 * @return Device index of the gamepad device. | |
102 */ | |
103 public int getIndex() { return mDeviceIndex; } | |
104 | |
105 /** | |
106 * @return The timestamp when the gamepad device was last interacted. | |
107 */ | |
108 public long getTimestamp() { return mTimestamp; } | |
109 | |
110 /** | |
111 * @return The axes state of the gamepad device. | |
112 */ | |
113 public float[] getAxes() { return mAxisValues; } | |
114 | |
115 /** | |
116 * @return The buttons state of the gamepad device. | |
117 */ | |
118 public float[] getButtons() { return mButtonsValues; } | |
119 | |
120 /** | |
121 * Reset the axes and buttons data of the gamepad device everytime gamepad d
ata access is | |
122 * paused. | |
123 */ | |
124 public void clearData() { | |
125 Arrays.fill(mAxisValues, 0); | |
126 Arrays.fill(mRawAxes, 0); | |
127 Arrays.fill(mButtonsValues, 0); | |
128 Arrays.fill(mRawButtons, 0); | |
129 } | |
130 | |
131 /** | |
132 * Handles key event from the gamepad device. | |
133 * @return True if the key event from the gamepad device has been consumed. | |
134 */ | |
135 public boolean handleKeyEvent(KeyEvent event) { | |
136 // Ignore event if it is not for standard gamepad key. | |
137 if (!GamepadList.isGamepadEvent(event)) return false; | |
138 int keyCode = event.getKeyCode(); | |
139 assert keyCode < 256; | |
140 // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully p
ressed. | |
141 if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
142 mRawButtons[keyCode] = 1.0f; | |
143 } else if (event.getAction() == KeyEvent.ACTION_UP) { | |
144 mRawButtons[keyCode] = 0.0f; | |
145 } | |
146 mTimestamp = event.getEventTime(); | |
147 | |
148 return true; | |
149 } | |
150 | |
151 /** | |
152 * Handles motion event from the gamepad device. | |
153 * @return True if the motion event from the gamepad device has been consume
d. | |
154 */ | |
155 public boolean handleMotionEvent(MotionEvent event) { | |
156 // Ignore event if it is not a standard gamepad motion event. | |
157 if (!GamepadList.isGamepadEvent(event)) return false; | |
158 // Update axes values. | |
159 for (int i = 0; i < mAxes.length; i++) { | |
160 mRawAxes[i] = event.getAxisValue(mAxes[i]); | |
161 } | |
162 mTimestamp = event.getEventTime(); | |
163 return true; | |
164 } | |
165 | |
166 } | |
OLD | NEW |