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; | |
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 import java.util.Iterator; | |
15 import java.util.LinkedHashMap; | |
16 | |
17 import org.chromium.content.browser.input.gamepad_mapping.CanonicalAxisIndex; | |
18 import org.chromium.content.browser.input.gamepad_mapping.CanonicalButtonIndex; | |
19 | |
20 /* | |
21 * Class to manage information related to each connected gamepad device. | |
22 */ | |
23 | |
24 class GamepadDevice { | |
25 // An id for the gamepad. | |
26 protected int mDeviceId; | |
jdduke (slow)
2014/03/24 17:38:15
Do you plan on subclassing this? If not, these mem
SaurabhK
2014/03/26 16:10:07
On 2014/03/24 17:38:15, jdduke wrote:
Done.
| |
27 // The index of the gamepad in the Navigator. | |
28 protected int mDeviceIndex; | |
29 // Last time the data for this gamepad was updated. | |
30 protected long mTimestamp; | |
31 // If this gamepad is mapped to standard gamepad? | |
32 protected String mMapping; | |
33 | |
34 // Array of values for all axes of the gamepad. | |
35 // All axis values must be linearly normalized to the range [-1.0 .. 1.0]. | |
36 // As appropriate, -1.0 should correspond to "up" or "left", and 1.0 | |
37 // should correspond to "down" or "right". | |
38 protected float[] mAxisValues; | |
39 | |
40 protected float[] mButtonsValues; | |
41 | |
42 // When the user agent recognizes the attached inputDevice, it is recommende d | |
43 // that it be remapped to a canonical ordering when possible. Devices that a re | |
44 // not recognized should still be exposed in their raw form. Therefore we mu st | |
45 // pass the raw Button and raw Axis values. | |
46 protected float[] mRawButtons; | |
47 protected float[] mRawAxes; | |
48 | |
49 // An identification string for the gamepad. | |
50 protected String mDeviceName; | |
51 | |
52 // Array of axes ids. | |
53 private int[] mAxes; | |
54 | |
55 // List of keycodes that can be sent by a standard gamepad. Standard gamepad can have | |
56 // buttons as per the spec: | |
57 // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping | |
58 // Need to maintain this list to get input device capabilities. We query | |
59 // input devices with this list and device returns list mentioning which | |
60 // Keycodes out of these can be sent by a gamepad device. | |
61 private static final int[] standardGamepadKeyCodes = { | |
62 KeyEvent.KEYCODE_BUTTON_A, // Button 0 | |
63 KeyEvent.KEYCODE_BUTTON_B, // Button 1 | |
64 KeyEvent.KEYCODE_BUTTON_X, // Button 2 | |
65 KeyEvent.KEYCODE_BUTTON_Y, // Button 3 | |
66 KeyEvent.KEYCODE_BUTTON_L1, // Button 4 | |
67 KeyEvent.KEYCODE_BUTTON_R1, // Button 5 | |
68 KeyEvent.KEYCODE_BUTTON_L2, // Button 6 | |
69 KeyEvent.KEYCODE_BUTTON_R2, // Button 7 | |
70 KeyEvent.KEYCODE_BUTTON_SELECT, // Button 8 | |
71 KeyEvent.KEYCODE_BUTTON_START, // Button 9 | |
72 KeyEvent.KEYCODE_BUTTON_THUMBL, // Button 10 | |
73 KeyEvent.KEYCODE_BUTTON_THUMBR, // Button 11 | |
74 KeyEvent.KEYCODE_DPAD_UP, // Button 12 | |
75 KeyEvent.KEYCODE_DPAD_DOWN, // Button 13 | |
76 KeyEvent.KEYCODE_DPAD_LEFT, // Button 14 | |
77 KeyEvent.KEYCODE_DPAD_RIGHT, // Button 15 | |
78 KeyEvent.KEYCODE_BUTTON_MODE, // Button 16?? | |
79 }; | |
80 | |
81 // Hash map of supported keycodes and their current value linearly | |
82 // normalized to the range [0.0 .. 1.0]. | |
83 // 0.0 must mean fully unpressed, and 1.0 must mean fully pressed. | |
84 protected LinkedHashMap<Integer, Float> mKeyCodeValueMap = new LinkedHashMap <Integer, Float>(); | |
85 | |
86 GamepadDevice(int index, InputDevice inputDevice) { | |
87 mDeviceIndex = index; | |
88 mDeviceId = inputDevice.getId(); | |
89 mDeviceName = inputDevice.getName(); | |
90 mTimestamp = SystemClock.uptimeMillis(); | |
91 mMapping = " "; | |
92 mButtonsValues = new float[CanonicalButtonIndex.NUM_CANONICAL_BUTTONS]; | |
93 mAxisValues = new float[CanonicalAxisIndex.NUM_CANONICAL_AXES]; | |
94 // Get the total number of axes supported by gamepad. | |
95 int totalAxes = 0; | |
96 for (MotionRange range : inputDevice.getMotionRanges()) { | |
97 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | |
98 totalAxes++; | |
99 } | |
100 } | |
101 // Get axis ids and initialize axes values. | |
102 mAxes = new int[totalAxes]; | |
103 mRawAxes = new float[totalAxes]; | |
104 int i = 0; | |
105 for (MotionRange range : inputDevice.getMotionRanges()) { | |
106 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | |
107 mAxes[i] = range.getAxis(); | |
108 mRawAxes[i] = 0.0f; | |
109 i++; | |
110 } | |
111 } | |
112 // Update supported buttons list. | |
113 boolean[] deviceKeys = inputDevice.hasKeys(standardGamepadKeyCodes); | |
114 for (int j = 0; j < standardGamepadKeyCodes.length; ++j) { | |
115 if (deviceKeys[j]) { | |
116 mKeyCodeValueMap.put(standardGamepadKeyCodes[j], 0.0f); | |
117 } | |
118 } | |
119 mRawButtons = new float[mKeyCodeValueMap.size()]; | |
120 } | |
121 | |
122 GamepadDevice() {} | |
123 | |
124 public void mapButtonsAndAxes() { | |
125 int i = 0; | |
126 Iterator itr = mKeyCodeValueMap.values().iterator(); | |
127 while (itr.hasNext()) { | |
128 mRawButtons[i++] = (Float)itr.next(); | |
129 } | |
130 mMapping = GamepadMappings.mapToStandardGamepad(mAxisValues, mButtonsVal ues, mRawAxes, | |
jdduke (slow)
2014/03/24 17:38:15
GamepadMappings.mapToStandardGamepad returns a Boo
SaurabhK
2014/03/26 16:10:07
On 2014/03/24 17:38:15, jdduke wrote:
I am settin
| |
131 mRawButtons, | |
132 mDeviceName) ? "standard " : " "; | |
133 } | |
134 | |
135 public int getId() { return mDeviceId; } | |
136 | |
137 public String getMapping() { return mMapping; } | |
138 | |
139 public String getName() { return mDeviceName; } | |
140 | |
141 public int getIndex() { return mDeviceIndex; } | |
142 | |
143 public long getTimestamp() { return mTimestamp; } | |
144 | |
145 public float[] getAxes() { | |
146 if (mMapping.equals("standard")) | |
147 return mAxisValues; | |
148 else | |
149 return mRawAxes; | |
150 } | |
151 | |
152 public float[] getButtons() { | |
153 if (mMapping.equals("standard")) | |
154 return mButtonsValues; | |
155 else | |
156 return mRawButtons; | |
157 } | |
158 | |
159 public void clearData() { | |
jdduke (slow)
2014/03/24 17:38:15
I'm going to led tedchoc@ or another Java speciali
SaurabhK
2014/03/26 16:10:07
On 2014/03/24 17:38:15, jdduke wrote:
SO, shall i
jdduke (slow)
2014/03/26 16:37:58
No, you should definitely comment this and all oth
SaurabhK
2014/03/27 13:32:31
On 2014/03/26 16:37:58, jdduke wrote:
Done.
| |
160 Arrays.fill(mAxisValues, 0); | |
161 Arrays.fill(mRawAxes, 0); | |
162 Arrays.fill(mButtonsValues, 0); | |
163 Arrays.fill(mRawButtons, 0); | |
164 } | |
165 | |
166 public boolean updateForEvent(KeyEvent event) { | |
167 mTimestamp = SystemClock.uptimeMillis(); | |
jdduke (slow)
2014/03/24 17:38:15
Do you want really want to update mTimestamp if it
SaurabhK
2014/03/26 16:10:07
On 2014/03/24 17:38:15, jdduke wrote:
Changeing it
| |
168 // Ignore event if it is not for standard gamepad key. | |
169 int keyCode = event.getKeyCode(); | |
170 if (mKeyCodeValueMap.get(keyCode) == null) { | |
171 return false; | |
172 } | |
173 // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully p ressed. | |
174 if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
175 mKeyCodeValueMap.put(keyCode, 1.0f); | |
176 } else if (event.getAction() == KeyEvent.ACTION_UP) { | |
177 mKeyCodeValueMap.put(keyCode, 0.0f); | |
178 } | |
179 | |
180 return true; | |
181 } | |
182 | |
183 public boolean updateForEvent(MotionEvent event) { | |
184 mTimestamp = SystemClock.uptimeMillis(); | |
185 // Update axes values. | |
186 for (int i = 0; i < mAxes.length; ++i) { | |
187 mRawAxes[i] = event.getAxisValue(mAxes[i]); | |
188 } | |
189 return true; | |
190 } | |
191 } | |
OLD | NEW |