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

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/GamepadMappings.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 /*
8 * Class to manage mapping information related to each supported gamepad control ler device.
9 */
10
11 class GamepadMappings {
12
13 // Standard gamepad buttons.
14 public static class StandardButtons {
15 public static final int BUTTON_A = 0;
jdduke (slow) 2014/03/18 09:43:15 I believe we're getting these values from gamepad_
SaurabhK 2014/03/18 12:45:30 On 2014/03/18 09:43:15, jdduke wrote: Incorporati
16 public static final int BUTTON_B = 1;
17 public static final int BUTTON_X = 2;
18 public static final int BUTTON_Y = 3;
19 public static final int LEFT_TRIGGER = 4;
20 public static final int RIGHT_TRIGGER = 5;
21 public static final int BOTTOM_LEFT_TRIGGER = 6;
22 public static final int BOTTOM_RIGHT_TRIGGER = 7;
23 public static final int SELECT = 8;
24 public static final int START = 9;
25 public static final int LEFT_THUMB = 10;
26 public static final int RIGHT_THUMB = 11;
27 public static final int DPAD_UP = 12;
28 public static final int DPAD_DOWN = 13;
29 public static final int DPAD_LEFT = 14;
30 public static final int DPAD_RIGHT = 15;
31 public static final int MODE = 16;
32 public static final int MAX_BUTTONS = 17;
33 }
34
35 // Standard gamepad axes.
36 public static class StandardAxes {
37 public static final int AXIS_0 = 0;
38 public static final int AXIS_1 = 1;
39 public static final int AXIS_2 = 2;
40 public static final int AXIS_3 = 3;
41 public static final int MAX_AXES = 4;
42 }
43
44 // Following values should be changed if the gamepad layout changes
45 // or frameworks exposes different axes / buttons for a particular gamepad.
46 // mRawShieldAxes is the total raw axes on known shield gamepad.
47 protected static final int mRawShieldAxes = 10;
48 // mRawShieldButtons is the total raw buttons on known shield gamepad.
49 protected static final int mRawShieldButtons = 10;
50 // mRawXBoxAxes is the total raw axes on known XBox gamepad.
51 protected static final int mRawXBoxAxes = 10;
52 // mRawXBoxButtons is the total raw buttons on known XBox gamepad.
53 protected static final int mRawXBoxButtons = 9;
54
55 public static boolean mapToStandardGamepad(float[] mappedAxis, float[] mappe dButtons,
56 float[] rawAxes, float[] rawButto ns,
57 String deviceName) {
58 boolean isMapped = true;
59 if (deviceName.contains("NVIDIA Corporation NVIDIA Controller") &&
60 rawAxes.length == mRawShieldAxes && rawButtons.length == mRawShieldB uttons) {
61 mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes);
62 }
63 else if (deviceName.contains("Microsoft X-Box 360 pad") &&
64 rawAxes.length == mRawXBoxAxes && rawButtons.length == mRawXBox Buttons) {
65 mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes);
66 }
67 else {
68 isMapped = false;
69 }
70 return isMapped;
71 }
72
73 private static float negativeAxisValueAsButton(float input) {
74 return (input < -0.5f) ? 1.f : 0.f;
75 }
76
77 private static float positiveAxisValueAsButton(float input) {
78 return (input > 0.5f) ? 1.f : 0.f;
79 }
80
81 /**
82 * Method for mapping Nvidia gamepad axis and button values
83 * to standard gamepad button and axes values.
84 */
85 private static void mapShieldGamepad(float[] mappedButtons, float[] rawButto ns,
86 float[] mappedAxis, float[] rawAxes) {
87 // First four buttons on Nvidia gamepad appear in correct order so no ma pping is required.
88 mappedButtons[StandardButtons.BUTTON_A] = rawButtons[0];
89 mappedButtons[StandardButtons.BUTTON_B] = rawButtons[1];
90 mappedButtons[StandardButtons.BUTTON_X] = rawButtons[2];
91 mappedButtons[StandardButtons.BUTTON_Y] = rawButtons[3];
92 // Third axis on Nvidia gamepad acts as a bottom left trigger button.
93 mappedButtons[StandardButtons.BOTTOM_LEFT_TRIGGER] = rawAxes[2];
94 mappedButtons[StandardButtons.LEFT_TRIGGER] = rawButtons[4];
95 mappedButtons[StandardButtons.RIGHT_TRIGGER] = rawButtons[5];
96 mappedButtons[StandardButtons.SELECT] = rawButtons[6];
97 mappedButtons[StandardButtons.START] = rawButtons[7];
98 mappedButtons[StandardButtons.LEFT_THUMB] = rawButtons[8];
99 mappedButtons[StandardButtons.RIGHT_THUMB] = rawButtons[9];
100 // Seventh axis on Nvidia gamepad acts as a bottom right trigger button.
101 mappedButtons[StandardButtons.BOTTOM_RIGHT_TRIGGER] = rawAxes[6];
102 // Ninth axis on Nvidia gamepad acts as directional pad right and left b utton.
103 // Positive axis value indicates dpad right.
104 // Negative value indicates dpad left.
105 mappedButtons[StandardButtons.DPAD_RIGHT] = positiveAxisValueAsButton(ra wAxes[8]);
106 mappedButtons[StandardButtons.DPAD_LEFT] = negativeAxisValueAsButton(raw Axes[8]);
107 // Tenth axis on Nvidia gamepad acts as directional pad up and down butt on.
108 // Positive axis value indicates dpad down.
109 // Negative value indicates dpad up.
110 mappedButtons[StandardButtons.DPAD_DOWN] = positiveAxisValueAsButton(raw Axes[9]);
111 mappedButtons[StandardButtons.DPAD_UP] = negativeAxisValueAsButton(rawAx es[9]);
112 // Other standard buttons are either not present on the gamepad or not e xposed to an
113 // application.
114 mappedButtons[StandardButtons.MODE] = 0.0f;
115
116 // Standard gamepad can have only four axes.
117 mappedAxis[StandardAxes.AXIS_0] = rawAxes[0];
118 mappedAxis[StandardAxes.AXIS_1] = rawAxes[1];
119 mappedAxis[StandardAxes.AXIS_2] = rawAxes[4];
120 mappedAxis[StandardAxes.AXIS_3] = rawAxes[5];
121 }
122
123 /**
124 * Method for mapping Microsoft XBox 360 gamepad axis and button values
125 * to standard gamepad button and axes values.
126 */
127 private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButt ons,
128 float[] mappedAxis, float[] rawAxes) {
129 // First four buttons on Microsoft XBox 360 gamepad appear in correct or der so no mapping
130 // is required.
131 mappedButtons[StandardButtons.BUTTON_A] = rawButtons[0];
132 mappedButtons[StandardButtons.BUTTON_B] = rawButtons[1];
133 mappedButtons[StandardButtons.BUTTON_X] = rawButtons[2];
134 mappedButtons[StandardButtons.BUTTON_Y] = rawButtons[3];
135 // Third axis on Microsoft XBox 360 gamepad acts as a left bottom should er button.
136 mappedButtons[StandardButtons.BOTTOM_LEFT_TRIGGER] = rawAxes[2];
137 mappedButtons[StandardButtons.LEFT_TRIGGER] = rawButtons[4];
138 mappedButtons[StandardButtons.RIGHT_TRIGGER] = rawButtons[5];
139 mappedButtons[StandardButtons.START] = rawButtons[6];
140 mappedButtons[StandardButtons.LEFT_THUMB] = rawButtons[7];
141 mappedButtons[StandardButtons.RIGHT_THUMB] = rawButtons[8];
142 // Seventh axis on Microsoft XBox 360 gamepad acts as a right bottom sho ulder button.
143 mappedButtons[StandardButtons.BOTTOM_RIGHT_TRIGGER] = rawAxes[6];
144 // Ninth axis on Microsoft XBox 360 gamepad acts as directional pad righ t and left button.
145 // Positive axis value indicates dpad right.
146 // Negative value indicates dpad left.
147 mappedButtons[StandardButtons.DPAD_RIGHT] = positiveAxisValueAsButton(ra wAxes[8]);
148 mappedButtons[StandardButtons.DPAD_LEFT] = negativeAxisValueAsButton(raw Axes[8]);
149 // Tenth axis on Microsoft XBox 360 gamepad acts as directional pad up a nd down button.
150 // Positive axis value indicates dpad down.
151 // Negative value indicates dpad up.
152 mappedButtons[StandardButtons.DPAD_DOWN] = positiveAxisValueAsButton(raw Axes[9]);
153 mappedButtons[StandardButtons.DPAD_UP] = negativeAxisValueAsButton(rawAx es[9]);
154 // Other standard buttons are either not present on the gamepad or not e xposed to an
155 // application.
156 mappedButtons[StandardButtons.SELECT] = 0.0f;
157 mappedButtons[StandardButtons.MODE] = 0.0f;
158
159 // Standard gamepad can have only four axes.
160 mappedAxis[StandardAxes.AXIS_0] = rawAxes[0];
161 mappedAxis[StandardAxes.AXIS_1] = rawAxes[1];
162 mappedAxis[StandardAxes.AXIS_2] = rawAxes[4];
163 mappedAxis[StandardAxes.AXIS_3] = rawAxes[5];
164 }
165
166 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698