OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/gamepad/gamepad_standard_mappings.h" | 5 #include "content/browser/gamepad/gamepad_standard_mappings.h" |
6 | 6 |
7 #include "content/common/gamepad_hardware_buffer.h" | 7 #include "content/common/gamepad_hardware_buffer.h" |
8 | 8 |
9 namespace content { | 9 namespace content { |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 // Maps 0..65535 to -1..1. | 13 float AxisToButton(float input) { |
14 float NormalizeDirectInputAxis(long value) { | 14 return (input + 1.f) / 2.f; |
15 return (value * 1.f / 32767.5f) - 1.f; | |
16 } | 15 } |
17 | 16 |
18 float AxisNegativeAsButton(long value) { | 17 float AxisNegativeAsButton(float input) { |
19 return (value < 32767) ? 1.f : 0.f; | 18 return (input < -0.5f) ? 1.f : 0.f; |
20 } | 19 } |
21 | 20 |
22 float AxisPositiveAsButton(long value) { | 21 float AxisPositiveAsButton(float input) { |
23 return (value > 32767) ? 1.f : 0.f; | 22 return (input > 0.5f) ? 1.f : 0.f; |
24 } | 23 } |
25 | 24 |
26 // Need at least one mapper to prevent compile errors | 25 void DpadFromAxis(blink::WebGamepad* mapped, float dir) { |
27 // Remove when other mappings are added | 26 // Dpad is mapped as a direction on one axis, where -1 is up and it |
28 void MapperNull( | 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. |
| 30 if (dir == 0.0f) { |
| 31 mapped->buttons[kButtonDpadUp] = 0.f; |
| 32 mapped->buttons[kButtonDpadDown] = 0.f; |
| 33 mapped->buttons[kButtonDpadLeft] = 0.f; |
| 34 mapped->buttons[kButtonDpadRight] = 0.f; |
| 35 } else { |
| 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; |
| 41 } |
| 42 } |
| 43 |
| 44 void MapperLogitechDualAction( |
29 const blink::WebGamepad& input, | 45 const blink::WebGamepad& input, |
30 blink::WebGamepad* mapped) { | 46 blink::WebGamepad* mapped) { |
31 *mapped = input; | 47 *mapped = input; |
| 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]); |
| 53 |
| 54 mapped->buttonsLength = kNumButtons; |
| 55 mapped->axesLength = kNumAxes; |
| 56 } |
| 57 |
| 58 void Mapper2Axes8Keys( |
| 59 const blink::WebGamepad& input, |
| 60 blink::WebGamepad* mapped) { |
| 61 *mapped = input; |
| 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]); |
| 70 |
| 71 // Missing buttons |
| 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; |
| 77 |
| 78 mapped->buttonsLength = kNumButtons - 1; |
| 79 mapped->axesLength = 0; |
| 80 } |
| 81 |
| 82 void MapperDualshock4( |
| 83 const blink::WebGamepad& input, |
| 84 blink::WebGamepad* mapped) { |
| 85 enum Dualshock4Buttons { |
| 86 kTouchpadButton = kNumButtons, |
| 87 kNumDualshock4Buttons |
| 88 }; |
| 89 |
| 90 *mapped = input; |
| 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]); |
| 107 |
| 108 mapped->buttonsLength = kNumDualshock4Buttons; |
| 109 mapped->axesLength = kNumAxes; |
| 110 } |
| 111 |
| 112 void MapperOnLiveWireless( |
| 113 const blink::WebGamepad& input, |
| 114 blink::WebGamepad* mapped) { |
| 115 *mapped = input; |
| 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]); |
| 132 |
| 133 mapped->buttonsLength = kNumButtons; |
| 134 mapped->axesLength = kNumAxes; |
32 } | 135 } |
33 | 136 |
34 struct MappingData { | 137 struct MappingData { |
35 const char* const vendor_id; | 138 const char* const vendor_id; |
36 const char* const product_id; | 139 const char* const product_id; |
37 GamepadStandardMappingFunction function; | 140 GamepadStandardMappingFunction function; |
38 } AvailableMappings[] = { | 141 } AvailableMappings[] = { |
39 // http://www.linux-usb.org/usb.ids | 142 // http://www.linux-usb.org/usb.ids |
40 { "0000", "0000", MapperNull }, | 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) |
41 }; | 148 }; |
42 | 149 |
43 } // namespace | 150 } // namespace |
44 | 151 |
45 GamepadStandardMappingFunction GetGamepadStandardMappingFunction( | 152 GamepadStandardMappingFunction GetGamepadStandardMappingFunction( |
46 const base::StringPiece& vendor_id, | 153 const base::StringPiece& vendor_id, |
47 const base::StringPiece& product_id) { | 154 const base::StringPiece& product_id) { |
48 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) { | 155 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) { |
49 MappingData& item = AvailableMappings[i]; | 156 MappingData& item = AvailableMappings[i]; |
50 if (vendor_id == item.vendor_id && product_id == item.product_id) | 157 if (vendor_id == item.vendor_id && product_id == item.product_id) |
51 return item.function; | 158 return item.function; |
52 } | 159 } |
53 return NULL; | 160 return NULL; |
54 } | 161 } |
55 | 162 |
56 } // namespace content | 163 } // namespace content |
OLD | NEW |