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.Iterator; | |
14 import java.util.LinkedHashMap; | |
15 | |
16 /* | |
17 * Class to manage information related to each connected gamepad device. | |
18 */ | |
19 | |
20 class GamepadDevice { | |
21 // An id for the gamepad. | |
22 protected int mDeviceId; | |
23 // The index of the gamepad in the Navigator. | |
24 protected int mDeviceIndex; | |
25 // An identification string for the gamepad. | |
26 protected String mDeviceName; | |
27 // Last time the data for this gamepad was updated. | |
28 protected long mTimestamp; | |
29 | |
30 // Array of values for all axes of the gamepad. | |
31 // All axis values must be linearly normalized to the range [-1.0 .. 1.0]. | |
32 // As appropriate, -1.0 should correspond to "up" or "left", and 1.0 | |
33 // should correspond to "down" or "right". | |
34 protected float[] mAxisValues; | |
35 // Array of axes ids. | |
36 private int[] mAxes; | |
37 | |
38 protected GamepadMappings.GamepadType mGamepadType; | |
39 | |
40 // List of keycodes that can be sent by a standard gamepad. Standard gamepad can have | |
41 // buttons as per the spec: | |
42 // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping | |
43 // Need to maintain this list to get input device capabilities. We query | |
44 // input devices with this list and device returns list mentioning which | |
45 // Keycodes out of these can be sent by a gamepad device. | |
46 private static final int[] standardGamepadKeyCodes = { | |
bajones
2014/02/28 23:56:11
This is more-or-less a duplicate of the StandardBu
SaurabhK
2014/03/11 17:39:27
On 2014/02/28 23:56:11, bajones wrote:
I have to
| |
47 KeyEvent.KEYCODE_BUTTON_A, // Button 0 | |
48 KeyEvent.KEYCODE_BUTTON_B, // Button 1 | |
49 KeyEvent.KEYCODE_BUTTON_X, // Button 2 | |
50 KeyEvent.KEYCODE_BUTTON_Y, // Button 3 | |
51 KeyEvent.KEYCODE_BUTTON_L1, // Button 4 | |
52 KeyEvent.KEYCODE_BUTTON_R1, // Button 5 | |
53 KeyEvent.KEYCODE_BUTTON_L2, // Button 6 | |
54 KeyEvent.KEYCODE_BUTTON_R2, // Button 7 | |
55 KeyEvent.KEYCODE_BUTTON_SELECT, // Button 8 | |
56 KeyEvent.KEYCODE_BUTTON_START, // Button 9 | |
57 KeyEvent.KEYCODE_BUTTON_THUMBL, // Button 10 | |
58 KeyEvent.KEYCODE_BUTTON_THUMBR, // Button 11 | |
59 KeyEvent.KEYCODE_DPAD_UP, // Button 12 | |
60 KeyEvent.KEYCODE_DPAD_DOWN, // Button 13 | |
61 KeyEvent.KEYCODE_DPAD_LEFT, // Button 14 | |
62 KeyEvent.KEYCODE_DPAD_RIGHT, // Button 15 | |
63 KeyEvent.KEYCODE_BUTTON_MODE, // Button 16?? | |
64 }; | |
65 | |
66 // Hash map of supported keycodes and their current value linearly | |
67 // normalized to the range [0.0 .. 1.0]. | |
68 // 0.0 must mean fully unpressed, and 1.0 must mean fully pressed. | |
69 protected LinkedHashMap<Integer, Float> mKeyCodeValueMap = new LinkedHashMap <Integer, Float>(); | |
70 | |
71 GamepadDevice(int index, InputDevice inputDevice) { | |
72 mDeviceIndex = index; | |
73 mDeviceId = inputDevice.getId(); | |
74 mDeviceName = inputDevice.getName(); | |
75 mTimestamp = SystemClock.uptimeMillis(); | |
76 // Get the total number of axes supported by gamepad. | |
77 int totalAxes = 0; | |
78 for (MotionRange range : inputDevice.getMotionRanges()) { | |
79 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | |
80 totalAxes++; | |
81 } | |
82 } | |
83 // Get axis ids and initialize axes values. | |
84 mAxes = new int[totalAxes]; | |
85 mAxisValues = new float[totalAxes]; | |
86 int i = 0; | |
87 for (MotionRange range : inputDevice.getMotionRanges()) { | |
88 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | |
89 mAxes[i] = range.getAxis(); | |
90 mAxisValues[i] = 0.0f; | |
91 i++; | |
92 } | |
93 } | |
94 // Update supported buttons list. | |
95 boolean[] deviceKeys = inputDevice.hasKeys(standardGamepadKeyCodes); | |
96 for (int j = 0; j < standardGamepadKeyCodes.length; ++j) { | |
97 if (deviceKeys[j]) { | |
98 mKeyCodeValueMap.put(standardGamepadKeyCodes[j], 0.0f); | |
99 } | |
100 } | |
101 | |
102 // When the user agent recognizes the attached inputDevice, it is recomm ended | |
103 // that it be remapped to a canonical ordering when possible. Devices | |
104 // that are not recognized should still be exposed in their raw form. | |
105 setGamepadType(); | |
106 } | |
107 | |
108 GamepadDevice() {} | |
109 | |
110 protected void setGamepadType() { | |
bajones
2014/02/28 23:56:11
I'd rather this logic occur in the GamepadMappings
SaurabhK
2014/03/11 17:39:27
On 2014/02/28 23:56:11, bajones wrote:
Done.
| |
111 if (isKnownShieldGamepad()) { | |
112 mGamepadType = GamepadMappings.GamepadType.Shield; | |
113 } else if (isKnownXBoxGamepad()) { | |
114 mGamepadType = GamepadMappings.GamepadType.XBox360; | |
115 } else { | |
116 // TODO: Add support for mapping other common gamepads to the standa rd gamepads. | |
117 mGamepadType = GamepadMappings.GamepadType.Unknown; | |
118 } | |
119 } | |
120 | |
121 private boolean isKnownShieldGamepad() { | |
122 if (mDeviceName.contains("NVIDIA") && mDeviceName.contains("Controller") ) { | |
bajones
2014/02/28 23:56:11
This feels imprecise. If NVIDIA ever makes another
SaurabhK
2014/03/11 17:39:27
On 2014/02/28 23:56:11, bajones wrote:
Done.
| |
123 // Known shield gamepad should have mRawShieldAxes and mRawShieldBut tons. | |
124 if (mAxisValues.length == GamepadMappings.mRawShieldAxes && | |
125 mKeyCodeValueMap.size() == GamepadMappings.mRawShieldBut tons) { | |
126 return true; | |
127 } | |
128 } | |
129 return false; | |
130 } | |
131 | |
132 private boolean isKnownXBoxGamepad() { | |
133 if (mDeviceName.contains("Microsoft") && mDeviceName.contains("X-Box") & & | |
134 mDeviceName.contains("360") && mDeviceName.contains("pad")) { | |
135 // Known XBox gamepad should have mRawXBoxAxes and mRawXBoxButtons. | |
136 if (mAxisValues.length == GamepadMappings.mRawXBoxAxes && | |
137 mKeyCodeValueMap.size() == GamepadMappings.mRawXBoxButtons) { | |
138 return true; | |
139 } | |
140 } | |
141 return false; | |
142 } | |
143 | |
144 public int getId() { return mDeviceId; } | |
145 | |
146 public String getName() { return mDeviceName; } | |
147 | |
148 public int getIndex() { return mDeviceIndex; } | |
149 | |
150 public long getTimestamp() { return mTimestamp; } | |
151 | |
152 public float[] getAxes() { | |
153 if ( mGamepadType != GamepadMappings.GamepadType.Unknown) { | |
154 return GamepadMappings.mapToStandardGamepadAxes(mAxisValues, mGamepa dType); | |
155 } | |
156 return mAxisValues; | |
157 } | |
158 | |
159 public float[] getButtons() { | |
160 int size = mKeyCodeValueMap.size(); | |
161 float[] values = new float[size]; | |
162 int i = 0; | |
163 Iterator itr = mKeyCodeValueMap.values().iterator(); | |
164 while (itr.hasNext()) { | |
165 values[i++] = (Float)itr.next(); | |
166 } | |
167 if ( mGamepadType != GamepadMappings.GamepadType.Unknown) { | |
168 return GamepadMappings.mapToStandardGamepadButtons(values, mAxisValu es, mGamepadType); | |
169 } | |
170 return values; | |
171 } | |
172 | |
173 public boolean updateForEvent(KeyEvent event) { | |
174 mTimestamp = SystemClock.uptimeMillis(); | |
175 // Ignore event if it is not for standard gamepad key. | |
176 int keyCode = event.getKeyCode(); | |
177 if (mKeyCodeValueMap.get(keyCode) == null) { | |
178 return true; | |
179 } | |
180 // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully p ressed. | |
181 if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
182 mKeyCodeValueMap.put(keyCode, 1.0f); | |
183 } else if (event.getAction() == KeyEvent.ACTION_UP) { | |
184 mKeyCodeValueMap.put(keyCode, 0.0f); | |
185 } | |
186 | |
187 return true; | |
188 } | |
189 | |
190 public boolean updateForEvent(MotionEvent event) { | |
191 mTimestamp = SystemClock.uptimeMillis(); | |
192 // Update axes values. | |
193 for (int i = 0; i < mAxes.length; ++i) { | |
194 mAxisValues[i] = event.getAxisValue(mAxes[i]); | |
195 } | |
196 return true; | |
197 } | |
198 | |
199 public boolean isKnownGamepadLayout() { | |
200 return mGamepadType != GamepadMappings.GamepadType.Unknown; | |
201 } | |
202 } | |
OLD | NEW |