1 // Copyright (c) 2012 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.
5 #include "content/browser/gamepad/gamepad_standard_mappings.h"
7 #include "content/common/gamepad_hardware_buffer.h"
13 float AxisToButton(float input) {
14 return (input + 1.f) / 2.f;
17 float AxisNegativeAsButton(float input) {
18 return (input < -0.5f) ? 1.f : 0.f;
21 float AxisPositiveAsButton(float input) {
22 return (input > 0.5f) ? 1.f : 0.f;
25 void DpadFromAxis(blink::WebGamepad* mapped, float dir) {
26 // Dpad is mapped as a direction on one axis, where -1 is up and it
27 // increases clockwise to 1, which is up + left. It's set to a large (> 1.f)
28 // number when nothing is depressed, except on start up, sometimes it's 0.0
29 // for no data, rather than the large number.
31 mapped->buttons[kButtonDpadUp] = 0.f;
32 mapped->buttons[kButtonDpadDown] = 0.f;
33 mapped->buttons[kButtonDpadLeft] = 0.f;
34 mapped->buttons[kButtonDpadRight] = 0.f;
36 mapped->buttons[kButtonDpadUp] = (dir >= -1.f && dir < -0.7f) ||
37 (dir >= .95f && dir <= 1.f);
38 mapped->buttons[kButtonDpadRight] = dir >= -.75f && dir < -.1f;
39 mapped->buttons[kButtonDpadDown] = dir >= -.2f && dir < .45f;
40 mapped->buttons[kButtonDpadLeft] = dir >= .4f && dir <= 1.f;
44 void MapperLogitechDualAction(
45 const blink::WebGamepad& input,
46 blink::WebGamepad* mapped) {
48 mapped->buttons[kButtonPrimary] = input.buttons[1];
49 mapped->buttons[kButtonSecondary] = input.buttons[2];
50 mapped->buttons[kButtonTertiary] = input.buttons[0];
51 mapped->axes[kAxisRightStickY] = input.axes[5];
52 DpadFromAxis(mapped, input.axes[9]);
54 mapped->buttonsLength = kNumButtons;
55 mapped->axesLength = kNumAxes;
58 void Mapper2Axes8Keys(
59 const blink::WebGamepad& input,
60 blink::WebGamepad* mapped) {
62 mapped->buttons[kButtonPrimary] = input.buttons[2];
63 mapped->buttons[kButtonSecondary] = input.buttons[1];
64 mapped->buttons[kButtonTertiary] = input.buttons[3];
65 mapped->buttons[kButtonQuaternary] = input.buttons[0];
66 mapped->buttons[kButtonDpadUp] = AxisNegativeAsButton(input.axes[1]);
67 mapped->buttons[kButtonDpadDown] = AxisPositiveAsButton(input.axes[1]);
68 mapped->buttons[kButtonDpadLeft] = AxisNegativeAsButton(input.axes[0]);
69 mapped->buttons[kButtonDpadRight] = AxisPositiveAsButton(input.axes[0]);
72 mapped->buttons[kButtonLeftTrigger] = 0;
73 mapped->buttons[kButtonRightTrigger] = 0;
74 mapped->buttons[kButtonLeftThumbstick] = 0;
75 mapped->buttons[kButtonRightThumbstick] = 0;
76 mapped->buttons[kButtonMeta] = 0;
78 mapped->buttonsLength = kNumButtons - 1;
79 mapped->axesLength = 0;
82 void MapperDualshock4(
83 const blink::WebGamepad& input,
84 blink::WebGamepad* mapped) {
85 enum Dualshock4Buttons {
86 kTouchpadButton = kNumButtons,
91 mapped->buttons[kButtonPrimary] = input.buttons[1];
92 mapped->buttons[kButtonSecondary] = input.buttons[2];
93 mapped->buttons[kButtonTertiary] = input.buttons[0];
94 mapped->buttons[kButtonQuaternary] = input.buttons[3];
95 mapped->buttons[kButtonLeftShoulder] = input.buttons[4];
96 mapped->buttons[kButtonRightShoulder] = input.buttons[5];
97 mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[3]);
98 mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[4]);
99 mapped->buttons[kButtonBackSelect] = input.buttons[8];
100 mapped->buttons[kButtonStart] = input.buttons[9];
101 mapped->buttons[kButtonLeftThumbstick] = input.buttons[10];
102 mapped->buttons[kButtonRightThumbstick] = input.buttons[11];
103 mapped->buttons[kButtonMeta] = input.buttons[12];
104 mapped->buttons[kTouchpadButton] = input.buttons[13];
105 mapped->axes[kAxisRightStickY] = input.axes[5];
106 DpadFromAxis(mapped, input.axes[9]);
108 mapped->buttonsLength = kNumDualshock4Buttons;
109 mapped->axesLength = kNumAxes;
112 void MapperOnLiveWireless(
113 const blink::WebGamepad& input,
114 blink::WebGamepad* mapped) {
116 mapped->buttons[kButtonPrimary] = input.buttons[0];
117 mapped->buttons[kButtonSecondary] = input.buttons[1];
118 mapped->buttons[kButtonTertiary] = input.buttons[3];
119 mapped->buttons[kButtonQuaternary] = input.buttons[4];
120 mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
121 mapped->buttons[kButtonRightShoulder] = input.buttons[7];
122 mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]);
123 mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]);
124 mapped->buttons[kButtonBackSelect] = input.buttons[10];
125 mapped->buttons[kButtonStart] = input.buttons[11];
126 mapped->buttons[kButtonLeftThumbstick] = input.buttons[13];
127 mapped->buttons[kButtonRightThumbstick] = input.buttons[14];
128 mapped->buttons[kButtonMeta] = input.buttons[12];
129 mapped->axes[kAxisRightStickX] = input.axes[3];
130 mapped->axes[kAxisRightStickY] = input.axes[4];
131 DpadFromAxis(mapped, input.axes[9]);
133 mapped->buttonsLength = kNumButtons;
134 mapped->axesLength = kNumAxes;
138 const char* const vendor_id;
139 const char* const product_id;
140 GamepadStandardMappingFunction function;
141 } AvailableMappings[] = {
142 // http://www.linux-usb.org/usb.ids
143 { "046d", "c216", MapperLogitechDualAction }, // Logitech DualAction
144 { "0079", "0011", Mapper2Axes8Keys }, // 2Axes 8Keys Game Pad
145 { "054c", "05c4", MapperDualshock4 }, // Playstation Dualshock 4
146 { "2378", "1008", MapperOnLiveWireless }, // OnLive Controller (Bluetooth)
147 { "2378", "100a", MapperOnLiveWireless }, // OnLive Controller (Wired)
152 GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
153 const base::StringPiece& vendor_id,
154 const base::StringPiece& product_id) {
155 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
156 MappingData& item = AvailableMappings[i];
157 if (vendor_id == item.vendor_id && product_id == item.product_id)
158 return item.function;
163 } // namespace content