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