Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java

Issue 133943002: Gamepad API support for chrome on android (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 org.chromium.content.browser.input.CanonicalAxisIndex;
14 import org.chromium.content.browser.input.CanonicalButtonIndex;
15
16 import java.util.Arrays;
17 import java.util.Iterator;
18 import java.util.LinkedHashMap;
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 private int mDeviceId;
27 // The index of the gamepad in the Navigator.
28 private int mDeviceIndex;
29 // Last time the data for this gamepad was updated.
30 private long mTimestamp;
31 // If this gamepad is mapped to standard gamepad?
32 private 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 private float[] mAxisValues;
39
40 private 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 private float[] mRawButtons;
47 private float[] mRawAxes;
48
49 // An identification string for the gamepad.
50 private 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 private LinkedHashMap<Integer, Float> mKeyCodeValueMap = new LinkedHashMap<I nteger, 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 /**
125 * Map the axes and buttons of a gamepad device to a standard gamepad format.
126 */
127 public void mapButtonsAndAxes() {
128 int i = 0;
129 Iterator itr = mKeyCodeValueMap.values().iterator();
130 while (itr.hasNext()) {
131 mRawButtons[i++] = (Float)itr.next();
132 }
133 mMapping = GamepadMappings.mapToStandardGamepad(mAxisValues, mButtonsVal ues, mRawAxes,
jdduke (slow) 2014/04/04 15:10:17 I still don't understand why we're assigning |mMap
SaurabhK 2014/04/11 14:41:42 On 2014/04/04 15:10:17, jdduke wrote: As earlier
134 mRawButtons,
135 mDeviceName) ? "standard " : " ";
136 }
137
138 /**
139 * @return Device Id of the gamepad device.
140 */
141 public int getId() { return mDeviceId; }
142
143 /**
144 * @return Mapping status of the gamepad device.
145 */
146 public String getMapping() { return mMapping; }
147
148 /**
149 * @return Device name of the gamepad device.
150 */
151 public String getName() { return mDeviceName; }
152
153 /**
154 * @return Device index of the gamepad device.
155 */
156 public int getIndex() { return mDeviceIndex; }
157
158 /**
159 * @return The timestamp when the gamepad device was last interacted.
160 */
161 public long getTimestamp() { return mTimestamp; }
162
163 /**
164 * @return The axes state of the gamepad device.
165 */
166 public float[] getAxes() {
167 if (mMapping.equals("standard"))
168 return mAxisValues;
169 else
170 return mRawAxes;
171 }
172
173 /**
174 * @return The buttons state of the gamepad device.
175 */
176 public float[] getButtons() {
177 if (mMapping.equals("standard"))
178 return mButtonsValues;
179 else
180 return mRawButtons;
181 }
182
183 /**
184 * Reset the axes and buttons data of the gamepad device everytime gamepad da ta access is
185 * paused.
186 */
187 public void clearData() {
188 Arrays.fill(mAxisValues, 0);
189 Arrays.fill(mRawAxes, 0);
190 Arrays.fill(mButtonsValues, 0);
191 Arrays.fill(mRawButtons, 0);
192 }
193
194 /**
195 * Handles key event from the gamepad device.
196 * @return True if the key event from the gamepad device has been consumed.
197 */
198 public boolean updateForEvent(KeyEvent event) {
199 // Ignore event if it is not for standard gamepad key.
200 int keyCode = event.getKeyCode();
201 if (mKeyCodeValueMap.get(keyCode) == null) {
202 return false;
203 }
204 // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully p ressed.
205 if (event.getAction() == KeyEvent.ACTION_DOWN) {
206 mKeyCodeValueMap.put(keyCode, 1.0f);
207 } else if (event.getAction() == KeyEvent.ACTION_UP) {
208 mKeyCodeValueMap.put(keyCode, 0.0f);
209 }
210 mTimestamp = event.getEventTime();
211
212 return true;
213 }
214
215 /**
216 * Handles motion event from the gamepad device.
217 * @return True if the motion event from the gamepad device has been consumed .
218 */
219 public boolean updateForEvent(MotionEvent event) {
220 // Update axes values.
221 for (int i = 0; i < mAxes.length; ++i) {
222 mRawAxes[i] = event.getAxisValue(mAxes[i]);
223 }
224 mTimestamp = event.getEventTime();
225 return true;
226 }
227 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698