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

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, 9 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 java.util.Arrays;
14 import java.util.Iterator;
15 import java.util.LinkedHashMap;
16
17 import org.chromium.content.browser.input.CanonicalAxisIndex;
18 import org.chromium.content.browser.input.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 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 // A boolean that is set to true when the user interacts with the gamepad de vice.
jdduke (slow) 2014/03/26 16:37:58 Nit: Just say "Set to true when", no need to decla
SaurabhK 2014/03/27 13:32:31 On 2014/03/26 16:37:58, jdduke wrote: Done.
56 private boolean mHasDeviceBeenInteractedWith = false;
57
58 // List of keycodes that can be sent by a standard gamepad. Standard gamepad can have
59 // buttons as per the spec:
60 // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping
61 // Need to maintain this list to get input device capabilities. We query
62 // input devices with this list and device returns list mentioning which
63 // Keycodes out of these can be sent by a gamepad device.
64 private static final int[] standardGamepadKeyCodes = {
65 KeyEvent.KEYCODE_BUTTON_A, // Button 0
66 KeyEvent.KEYCODE_BUTTON_B, // Button 1
67 KeyEvent.KEYCODE_BUTTON_X, // Button 2
68 KeyEvent.KEYCODE_BUTTON_Y, // Button 3
69 KeyEvent.KEYCODE_BUTTON_L1, // Button 4
70 KeyEvent.KEYCODE_BUTTON_R1, // Button 5
71 KeyEvent.KEYCODE_BUTTON_L2, // Button 6
72 KeyEvent.KEYCODE_BUTTON_R2, // Button 7
73 KeyEvent.KEYCODE_BUTTON_SELECT, // Button 8
74 KeyEvent.KEYCODE_BUTTON_START, // Button 9
75 KeyEvent.KEYCODE_BUTTON_THUMBL, // Button 10
76 KeyEvent.KEYCODE_BUTTON_THUMBR, // Button 11
77 KeyEvent.KEYCODE_DPAD_UP, // Button 12
78 KeyEvent.KEYCODE_DPAD_DOWN, // Button 13
79 KeyEvent.KEYCODE_DPAD_LEFT, // Button 14
80 KeyEvent.KEYCODE_DPAD_RIGHT, // Button 15
81 KeyEvent.KEYCODE_BUTTON_MODE, // Button 16??
82 };
83
84 // Hash map of supported keycodes and their current value linearly
85 // normalized to the range [0.0 .. 1.0].
86 // 0.0 must mean fully unpressed, and 1.0 must mean fully pressed.
87 private LinkedHashMap<Integer, Float> mKeyCodeValueMap = new LinkedHashMap<I nteger, Float>();
88
89 GamepadDevice(int index, InputDevice inputDevice) {
90 mDeviceIndex = index;
91 mDeviceId = inputDevice.getId();
92 mDeviceName = inputDevice.getName();
93 mTimestamp = SystemClock.uptimeMillis();
94 mMapping = " ";
95 mButtonsValues = new float[CanonicalButtonIndex.NUM_CANONICAL_BUTTONS];
96 mAxisValues = new float[CanonicalAxisIndex.NUM_CANONICAL_AXES];
97 // Get the total number of axes supported by gamepad.
98 int totalAxes = 0;
99 for (MotionRange range : inputDevice.getMotionRanges()) {
100 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
101 totalAxes++;
102 }
103 }
104 // Get axis ids and initialize axes values.
105 mAxes = new int[totalAxes];
106 mRawAxes = new float[totalAxes];
107 int i = 0;
108 for (MotionRange range : inputDevice.getMotionRanges()) {
109 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
110 mAxes[i] = range.getAxis();
111 mRawAxes[i] = 0.0f;
112 i++;
113 }
114 }
115 // Update supported buttons list.
116 boolean[] deviceKeys = inputDevice.hasKeys(standardGamepadKeyCodes);
117 for (int j = 0; j < standardGamepadKeyCodes.length; ++j) {
118 if (deviceKeys[j]) {
119 mKeyCodeValueMap.put(standardGamepadKeyCodes[j], 0.0f);
120 }
121 }
122 mRawButtons = new float[mKeyCodeValueMap.size()];
123 }
124
125 GamepadDevice() {}
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,
134 mRawButtons,
135 mDeviceName) ? "standard " : " ";
136 }
137
138 public int getId() { return mDeviceId; }
139
140 public String getMapping() { return mMapping; }
141
142 public String getName() { return mDeviceName; }
143
144 public int getIndex() { return mDeviceIndex; }
145
146 public long getTimestamp() { return mTimestamp; }
147
148 public float[] getAxes() {
149 if (mMapping.equals("standard"))
150 return mAxisValues;
151 else
152 return mRawAxes;
153 }
154
155 public float[] getButtons() {
156 if (mMapping.equals("standard"))
157 return mButtonsValues;
158 else
159 return mRawButtons;
160 }
161
162 public boolean hasDeviceBeenInteractedWith() { return mHasDeviceBeenInteract edWith; }
163
164 public void clearData() {
165 Arrays.fill(mAxisValues, 0);
jdduke (slow) 2014/03/26 16:37:58 Can you also reset |mHasDeviceBeenInteractedWith|?
kbalazs 2014/03/26 19:24:53 I'm not sure we should do that because the fact th
SaurabhK 2014/03/27 13:32:31 On 2014/03/26 19:24:53, kbalazs wrote: Yes, even
166 Arrays.fill(mRawAxes, 0);
167 Arrays.fill(mButtonsValues, 0);
168 Arrays.fill(mRawButtons, 0);
169 }
170
171 public boolean updateForEvent(KeyEvent event) {
172 // Ignore event if it is not for standard gamepad key.
173 int keyCode = event.getKeyCode();
174 if (mKeyCodeValueMap.get(keyCode) == null) {
175 return false;
176 }
177 // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully p ressed.
178 if (event.getAction() == KeyEvent.ACTION_DOWN) {
179 mKeyCodeValueMap.put(keyCode, 1.0f);
kbalazs 2014/03/26 19:24:53 This creates 2 new objects possibly several times
SaurabhK 2014/03/27 13:32:31 On 2014/03/26 19:24:53, kbalazs wrote: In my opin
180 } else if (event.getAction() == KeyEvent.ACTION_UP) {
181 mKeyCodeValueMap.put(keyCode, 0.0f);
182 }
183 mHasDeviceBeenInteractedWith = true;
184 mTimestamp = SystemClock.uptimeMillis();
kbalazs 2014/03/26 19:24:53 Why not using the timestamp from the event?
SaurabhK 2014/03/27 13:32:31 On 2014/03/26 19:24:53, kbalazs wrote: Using it n
185
186 return true;
187 }
188
189 public boolean updateForEvent(MotionEvent event) {
190 // Update axes values.
191 for (int i = 0; i < mAxes.length; ++i) {
192 mRawAxes[i] = event.getAxisValue(mAxes[i]);
193 }
194 mHasDeviceBeenInteractedWith = true;
195 mTimestamp = SystemClock.uptimeMillis();
kbalazs 2014/03/26 19:24:53 Why not using the timestamp from the event?
SaurabhK 2014/03/27 13:32:31 On 2014/03/26 19:24:53, kbalazs wrote: Done.
196 return true;
197 }
198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698