| 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 float AxisToButton(float input) { |
| 14 return (input + 1.f) / 2.f; |
| 15 } |
| 16 |
| 13 // Maps 0..65535 to -1..1. | 17 // Maps 0..65535 to -1..1. |
| 14 float NormalizeDirectInputAxis(long value) { | 18 float NormalizeDirectInputAxis(long value) { |
| 15 return (value * 1.f / 32767.5f) - 1.f; | 19 return (value * 1.f / 32767.5f) - 1.f; |
| 16 } | 20 } |
| 17 | 21 |
| 18 float AxisNegativeAsButton(long value) { | 22 float AxisNegativeAsButton(long value) { |
| 19 return (value < 32767) ? 1.f : 0.f; | 23 return (value < 32767) ? 1.f : 0.f; |
| 20 } | 24 } |
| 21 | 25 |
| 22 float AxisPositiveAsButton(long value) { | 26 float AxisPositiveAsButton(long value) { |
| 23 return (value > 32767) ? 1.f : 0.f; | 27 return (value > 32767) ? 1.f : 0.f; |
| 24 } | 28 } |
| 25 | 29 |
| 30 void DpadFromAxis(blink::WebGamepad* mapped, float dir) { |
| 31 // Dpad is mapped as a direction on one axis, where -1 is up and it |
| 32 // increases clockwise to 1, which is up + left. It's set to a large (> 1.f) |
| 33 // number when nothing is depressed, except on start up, sometimes it's 0.0 |
| 34 // for no data, rather than the large number. |
| 35 if (dir == 0.0f) { |
| 36 mapped->buttons[kButtonDpadUp] = 0.f; |
| 37 mapped->buttons[kButtonDpadDown] = 0.f; |
| 38 mapped->buttons[kButtonDpadLeft] = 0.f; |
| 39 mapped->buttons[kButtonDpadRight] = 0.f; |
| 40 } else { |
| 41 mapped->buttons[kButtonDpadUp] = (dir >= -1.f && dir < -0.7f) || |
| 42 (dir >= .95f && dir <= 1.f); |
| 43 mapped->buttons[kButtonDpadRight] = dir >= -.75f && dir < -.1f; |
| 44 mapped->buttons[kButtonDpadDown] = dir >= -.2f && dir < .45f; |
| 45 mapped->buttons[kButtonDpadLeft] = dir >= .4f && dir <= 1.f; |
| 46 } |
| 47 } |
| 48 |
| 26 void MapperDragonRiseGeneric( | 49 void MapperDragonRiseGeneric( |
| 27 const blink::WebGamepad& input, | 50 const blink::WebGamepad& input, |
| 28 blink::WebGamepad* mapped) { | 51 blink::WebGamepad* mapped) { |
| 29 *mapped = input; | 52 *mapped = input; |
| 30 mapped->buttons[0] = input.buttons[1]; | 53 mapped->buttons[0] = input.buttons[1]; |
| 31 mapped->buttons[1] = input.buttons[2]; | 54 mapped->buttons[1] = input.buttons[2]; |
| 32 mapped->buttons[2] = input.buttons[0]; | 55 mapped->buttons[2] = input.buttons[0]; |
| 33 mapped->buttons[12] = input.buttons[16]; | 56 mapped->buttons[12] = input.buttons[16]; |
| 34 mapped->buttons[13] = input.buttons[17]; | 57 mapped->buttons[13] = input.buttons[17]; |
| 35 mapped->buttons[14] = input.buttons[18]; | 58 mapped->buttons[14] = input.buttons[18]; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 mapped->buttons[kButtonLeftThumbstick] = 0; // Not present | 112 mapped->buttons[kButtonLeftThumbstick] = 0; // Not present |
| 90 mapped->buttons[kButtonRightThumbstick] = 0; // Not present | 113 mapped->buttons[kButtonRightThumbstick] = 0; // Not present |
| 91 mapped->buttons[12] = AxisNegativeAsButton(input.axes[1]); | 114 mapped->buttons[12] = AxisNegativeAsButton(input.axes[1]); |
| 92 mapped->buttons[13] = AxisPositiveAsButton(input.axes[1]); | 115 mapped->buttons[13] = AxisPositiveAsButton(input.axes[1]); |
| 93 mapped->buttons[14] = AxisNegativeAsButton(input.axes[0]); | 116 mapped->buttons[14] = AxisNegativeAsButton(input.axes[0]); |
| 94 mapped->buttons[15] = AxisPositiveAsButton(input.axes[0]); | 117 mapped->buttons[15] = AxisPositiveAsButton(input.axes[0]); |
| 95 mapped->buttonsLength = 16; | 118 mapped->buttonsLength = 16; |
| 96 mapped->axesLength = 0; | 119 mapped->axesLength = 0; |
| 97 } | 120 } |
| 98 | 121 |
| 122 void MapperDualshock4( |
| 123 const blink::WebGamepad& input, |
| 124 blink::WebGamepad* mapped) { |
| 125 enum Dualshock4Buttons { |
| 126 kTouchpadButton = kNumButtons, |
| 127 kNumDualshock4Buttons |
| 128 }; |
| 129 |
| 130 *mapped = input; |
| 131 mapped->buttons[kButtonPrimary] = input.buttons[1]; |
| 132 mapped->buttons[kButtonSecondary] = input.buttons[2]; |
| 133 mapped->buttons[kButtonTertiary] = input.buttons[0]; |
| 134 mapped->buttons[kButtonQuaternary] = input.buttons[3]; |
| 135 mapped->buttons[kButtonLeftShoulder] = input.buttons[4]; |
| 136 mapped->buttons[kButtonRightShoulder] = input.buttons[5]; |
| 137 mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[3]); |
| 138 mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[4]); |
| 139 mapped->buttons[kButtonBackSelect] = input.buttons[8]; |
| 140 mapped->buttons[kButtonStart] = input.buttons[9]; |
| 141 mapped->buttons[kButtonLeftThumbstick] = input.buttons[10]; |
| 142 mapped->buttons[kButtonRightThumbstick] = input.buttons[11]; |
| 143 mapped->buttons[kButtonMeta] = input.buttons[12]; |
| 144 mapped->buttons[kTouchpadButton] = input.buttons[13]; |
| 145 mapped->axes[kAxisRightStickY] = input.axes[5]; |
| 146 DpadFromAxis(mapped, input.axes[9]); |
| 147 |
| 148 mapped->buttonsLength = kNumDualshock4Buttons; |
| 149 mapped->axesLength = kNumAxes; |
| 150 } |
| 151 |
| 99 struct MappingData { | 152 struct MappingData { |
| 100 const char* const vendor_id; | 153 const char* const vendor_id; |
| 101 const char* const product_id; | 154 const char* const product_id; |
| 102 GamepadStandardMappingFunction function; | 155 GamepadStandardMappingFunction function; |
| 103 } AvailableMappings[] = { | 156 } AvailableMappings[] = { |
| 104 // http://www.linux-usb.org/usb.ids | 157 // http://www.linux-usb.org/usb.ids |
| 158 { "054c", "05c4", MapperDualshock4 }, // Playstation Dualshock 4 |
| 159 |
| 160 // Untested DirectInput mappings. May not match Raw Input layout. |
| 161 // TODO: Test and re-enable |
| 162 /* |
| 105 { "0079", "0006", MapperDragonRiseGeneric }, // DragonRise Generic USB | 163 { "0079", "0006", MapperDragonRiseGeneric }, // DragonRise Generic USB |
| 106 { "046d", "c216", MapperLogitechDualAction }, // Logitech DualAction | 164 { "046d", "c216", MapperLogitechDualAction }, // Logitech DualAction |
| 107 { "046d", "c21a", MapperLogitechPrecision }, // Logitech Precision | 165 { "046d", "c21a", MapperLogitechPrecision }, // Logitech Precision |
| 108 { "12bd", "d012", Mapper2Axes8Keys }, // 2Axes 8Keys Game Pad | 166 { "12bd", "d012", Mapper2Axes8Keys }, // 2Axes 8Keys Game Pad |
| 167 */ |
| 109 }; | 168 }; |
| 110 | 169 |
| 111 } // namespace | 170 } // namespace |
| 112 | 171 |
| 113 GamepadStandardMappingFunction GetGamepadStandardMappingFunction( | 172 GamepadStandardMappingFunction GetGamepadStandardMappingFunction( |
| 114 const base::StringPiece& vendor_id, | 173 const base::StringPiece& vendor_id, |
| 115 const base::StringPiece& product_id) { | 174 const base::StringPiece& product_id) { |
| 116 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) { | 175 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) { |
| 117 MappingData& item = AvailableMappings[i]; | 176 MappingData& item = AvailableMappings[i]; |
| 118 if (vendor_id == item.vendor_id && product_id == item.product_id) | 177 if (vendor_id == item.vendor_id && product_id == item.product_id) |
| 119 return item.function; | 178 return item.function; |
| 120 } | 179 } |
| 121 return NULL; | 180 return NULL; |
| 122 } | 181 } |
| 123 | 182 |
| 124 } // namespace content | 183 } // namespace content |
| OLD | NEW |