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

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, 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 org.chromium.content.browser.input.CanonicalAxisIndex;
8 import org.chromium.content.browser.input.CanonicalButtonIndex;
9
10 /*
11 * Class to manage mapping information related to each supported gamepad control ler device.
12 */
13
14 class GamepadMappings {
kbalazs 2014/03/26 19:24:53 I would slightly prefer an abstract class implemen
15 // Following values should be changed if the gamepad layout changes
16 // or frameworks exposes different axes / buttons for a particular gamepad.
17 // mRawShieldAxes is the total raw axes on known shield gamepad.
18 protected static final int mRawShieldAxes = 10;
19 // mRawShieldButtons is the total raw buttons on known shield gamepad.
20 protected static final int mRawShieldButtons = 10;
21 // mRawXBoxAxes is the total raw axes on known XBox gamepad.
22 protected static final int mRawXBoxAxes = 10;
23 // mRawXBoxButtons is the total raw buttons on known XBox gamepad.
24 protected static final int mRawXBoxButtons = 9;
25
26 public static boolean mapToStandardGamepad(float[] mappedAxis, float[] mappe dButtons,
27 float[] rawAxes, float[] rawButto ns,
28 String deviceName) {
29 boolean isMapped = true;
30 if (deviceName.contains("NVIDIA Corporation NVIDIA Controller") &&
31 rawAxes.length == mRawShieldAxes && rawButtons.length == mRawShieldB uttons) {
32 mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes);
33 }
34 else if (deviceName.contains("Microsoft X-Box 360 pad") &&
35 rawAxes.length == mRawXBoxAxes && rawButtons.length == mRawXBox Buttons) {
36 mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes);
37 }
38 else {
39 isMapped = false;
40 }
41 return isMapped;
42 }
43
44 private static float negativeAxisValueAsButton(float input) {
45 return (input < -0.5f) ? 1.f : 0.f;
46 }
47
48 private static float positiveAxisValueAsButton(float input) {
49 return (input > 0.5f) ? 1.f : 0.f;
50 }
51
52 /**
53 * Method for mapping Nvidia gamepad axis and button values
54 * to standard gamepad button and axes values.
55 */
56 private static void mapShieldGamepad(float[] mappedButtons, float[] rawButto ns,
57 float[] mappedAxis, float[] rawAxes) {
58 // First four buttons on Nvidia gamepad appear in correct order so no ma pping is required.
59 mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = rawButtons[0];
kbalazs 2014/03/26 19:24:53 I would also prefer if we could use the symbolic c
60 mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = rawButtons[1];
61 mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = rawButtons[2];
62 mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = rawButtons[3];
63 // Third axis on Nvidia gamepad acts as a bottom left trigger button.
64 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2];
65 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = rawButtons[4] ;
66 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = rawButtons[5 ];
67 mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = rawButtons[6];
68 mappedButtons[CanonicalButtonIndex.BUTTON_START] = rawButtons[7];
69 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = rawButtons[ 8];
70 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = rawButtons [9];
71 // Seventh axis on Nvidia gamepad acts as a bottom right trigger button.
72 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6];
73 // Ninth axis on Nvidia gamepad acts as directional pad right and left b utton.
74 // Positive axis value indicates dpad right.
75 // Negative value indicates dpad left.
76 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] =
77 positiveAxisValueAsButton(rawAxes[8]);
78 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] =
79 negativeAxisValueAsButton(rawAxes[8]);
80 // Tenth axis on Nvidia gamepad acts as directional pad up and down butt on.
81 // Positive axis value indicates dpad down.
82 // Negative value indicates dpad up.
83 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] =
84 positiveAxisValueAsButton(rawAxes[9]);
85 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAs Button(rawAxes[9]);
86 // Other standard buttons are either not present on the gamepad or not e xposed to an
87 // application.
88 mappedButtons[CanonicalButtonIndex.BUTTON_META] = 0.0f;
89
90 // Standard gamepad can have only four axes.
91 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[0];
92 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[1];
93 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[4];
94 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[5];
95 }
96
97 /**
98 * Method for mapping Microsoft XBox 360 gamepad axis and button values
99 * to standard gamepad button and axes values.
100 */
101 private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButt ons,
102 float[] mappedAxis, float[] rawAxes) {
103 // First four buttons on Microsoft XBox 360 gamepad appear in correct or der so no mapping
104 // is required.
105 mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = rawButtons[0];
106 mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = rawButtons[1];
107 mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = rawButtons[2];
108 mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = rawButtons[3];
109 // Third axis on Microsoft XBox 360 gamepad acts as a left bottom should er button.
110 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2];
111 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = rawButtons[4] ;
112 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = rawButtons[5 ];
113 mappedButtons[CanonicalButtonIndex.BUTTON_START] = rawButtons[6];
114 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = rawButtons[ 7];
115 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = rawButtons [8];
116 // Seventh axis on Microsoft XBox 360 gamepad acts as a right bottom sho ulder button.
117 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6];
118 // Ninth axis on Microsoft XBox 360 gamepad acts as directional pad righ t and left button.
119 // Positive axis value indicates dpad right.
120 // Negative value indicates dpad left.
121 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] =
122 positiveAxisValueAsButton(rawAxes[8]);
123 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] =
124 negativeAxisValueAsButton(rawAxes[8]);
125 // Tenth axis on Microsoft XBox 360 gamepad acts as directional pad up a nd down button.
126 // Positive axis value indicates dpad down.
127 // Negative value indicates dpad up.
128 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] =
129 positiveAxisValueAsButton(rawAxes[9]);
130 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAs Button(rawAxes[9]);
131 // Other standard buttons are either not present on the gamepad or not e xposed to an
132 // application.
133 mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = 0.0f;
134 mappedButtons[CanonicalButtonIndex.BUTTON_META] = 0.0f;
135
136 // Standard gamepad can have only four axes.
137 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[0];
138 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[1];
139 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[4];
140 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[5];
141 }
142
143 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698