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. 4 5 #include "content/browser/gamepad/gamepad_standard_mappings.h" 6 7 #include "content/common/gamepad_hardware_buffer.h" 8 9 namespace content { 10 11 namespace { 12 13 float AxisToButton(float input) { 14 return (input + 1.f) / 2.f; 15 } 16 17 float AxisNegativeAsButton(float input) { 18 return (input < -0.5f) ? 1.f : 0.f; 19 } 20 21 float AxisPositiveAsButton(float input) { 22 return (input > 0.5f) ? 1.f : 0.f; 23 } 24 25 void MapperXInputStyleGamepad( 26 const blink::WebGamepad& input, 27 blink::WebGamepad* mapped) { 28 *mapped = input; 29 mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]); 30 mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]); 31 mapped->buttons[kButtonBackSelect] = input.buttons[6]; 32 mapped->buttons[kButtonStart] = input.buttons[7]; 33 mapped->buttons[kButtonLeftThumbstick] = input.buttons[9]; 34 mapped->buttons[kButtonRightThumbstick] = input.buttons[10]; 35 mapped->buttons[kButtonDpadUp] = AxisNegativeAsButton(input.axes[7]); 36 mapped->buttons[kButtonDpadDown] = AxisPositiveAsButton(input.axes[7]); 37 mapped->buttons[kButtonDpadLeft] = AxisNegativeAsButton(input.axes[6]); 38 mapped->buttons[kButtonDpadRight] = AxisPositiveAsButton(input.axes[6]); 39 mapped->buttons[kButtonMeta] = input.buttons[8]; 40 mapped->axes[kAxisRightStickX] = input.axes[3]; 41 mapped->axes[kAxisRightStickY] = input.axes[4]; 42 mapped->buttonsLength = kNumButtons; 43 mapped->axesLength = kNumAxes; 44 } 45 46 void MapperLakeviewResearch( 47 const blink::WebGamepad& input, 48 blink::WebGamepad* mapped) { 49 *mapped = input; 50 mapped->buttons[kButtonPrimary] = input.buttons[2]; 51 mapped->buttons[kButtonTertiary] = input.buttons[3]; 52 mapped->buttons[kButtonQuaternary] = input.buttons[0]; 53 mapped->buttons[kButtonLeftShoulder] = input.buttons[6]; 54 mapped->buttons[kButtonRightShoulder] = input.buttons[7]; 55 mapped->buttons[kButtonLeftTrigger] = input.buttons[4]; 56 mapped->buttons[kButtonRightTrigger] = input.buttons[5]; 57 mapped->buttons[kButtonBackSelect] = input.buttons[9]; 58 mapped->buttons[kButtonStart] = input.buttons[8]; 59 mapped->buttons[kButtonDpadUp] = AxisNegativeAsButton(input.axes[5]); 60 mapped->buttons[kButtonDpadDown] = AxisPositiveAsButton(input.axes[5]); 61 mapped->buttons[kButtonDpadLeft] = AxisNegativeAsButton(input.axes[4]); 62 mapped->buttons[kButtonDpadRight] = AxisPositiveAsButton(input.axes[4]); 63 mapped->buttonsLength = kNumButtons - 1; // no Meta on this device 64 mapped->axesLength = kNumAxes; 65 } 66 67 void MapperPlaystationSixAxis( 68 const blink::WebGamepad& input, 69 blink::WebGamepad* mapped) { 70 *mapped = input; 71 mapped->buttons[kButtonPrimary] = input.buttons[14]; 72 mapped->buttons[kButtonSecondary] = input.buttons[13]; 73 mapped->buttons[kButtonTertiary] = input.buttons[15]; 74 mapped->buttons[kButtonQuaternary] = input.buttons[12]; 75 mapped->buttons[kButtonLeftShoulder] = input.buttons[10]; 76 mapped->buttons[kButtonRightShoulder] = input.buttons[11]; 77 mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[12]); 78 mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[13]); 79 mapped->buttons[kButtonBackSelect] = input.buttons[0]; 80 mapped->buttons[kButtonStart] = input.buttons[3]; 81 mapped->buttons[kButtonLeftThumbstick] = input.buttons[1]; 82 mapped->buttons[kButtonRightThumbstick] = input.buttons[2]; 83 mapped->buttons[kButtonDpadUp] = AxisToButton(input.axes[8]); 84 mapped->buttons[kButtonDpadDown] = AxisToButton(input.axes[10]); 85 mapped->buttons[kButtonDpadLeft] = input.buttons[7]; 86 mapped->buttons[kButtonDpadRight] = AxisToButton(input.axes[9]); 87 mapped->buttons[kButtonMeta] = input.buttons[16]; 88 89 mapped->buttonsLength = kNumButtons; 90 mapped->axesLength = kNumAxes; 91 } 92 93 void MapperXGEAR( 94 const blink::WebGamepad& input, 95 blink::WebGamepad* mapped) { 96 *mapped = input; 97 mapped->buttons[kButtonPrimary] = input.buttons[2]; 98 mapped->buttons[kButtonSecondary] = input.buttons[1]; 99 mapped->buttons[kButtonTertiary] = input.buttons[3]; 100 mapped->buttons[kButtonQuaternary] = input.buttons[0]; 101 mapped->buttons[kButtonLeftShoulder] = input.buttons[6]; 102 mapped->buttons[kButtonRightShoulder] = input.buttons[7]; 103 mapped->buttons[kButtonLeftTrigger] = input.buttons[4]; 104 mapped->buttons[kButtonRightTrigger] = input.buttons[5]; 105 mapped->buttons[kButtonDpadUp] = AxisNegativeAsButton(input.axes[5]); 106 mapped->buttons[kButtonDpadDown] = AxisPositiveAsButton(input.axes[5]); 107 mapped->buttons[kButtonDpadLeft] = AxisNegativeAsButton(input.axes[4]); 108 mapped->buttons[kButtonDpadRight] = AxisPositiveAsButton(input.axes[4]); 109 mapped->axes[kAxisRightStickX] = input.axes[3]; 110 mapped->axes[kAxisRightStickY] = input.axes[2]; 111 mapped->buttonsLength = kNumButtons - 1; // no Meta on this device 112 mapped->axesLength = kNumAxes; 113 } 114 115 116 void MapperDragonRiseGeneric( 117 const blink::WebGamepad& input, 118 blink::WebGamepad* mapped) { 119 *mapped = input; 120 mapped->buttons[kButtonDpadUp] = AxisNegativeAsButton(input.axes[6]); 121 mapped->buttons[kButtonDpadDown] = AxisPositiveAsButton(input.axes[6]); 122 mapped->buttons[kButtonDpadLeft] = AxisNegativeAsButton(input.axes[5]); 123 mapped->buttons[kButtonDpadRight] = AxisPositiveAsButton(input.axes[5]); 124 mapped->axes[kAxisLeftStickX] = input.axes[0]; 125 mapped->axes[kAxisLeftStickY] = input.axes[1]; 126 mapped->axes[kAxisRightStickX] = input.axes[3]; 127 mapped->axes[kAxisRightStickY] = input.axes[4]; 128 mapped->buttonsLength = kNumButtons - 1; // no Meta on this device 129 mapped->axesLength = kNumAxes; 130 } 131 132 133 struct MappingData { 134 const char* const vendor_id; 135 const char* const product_id; 136 GamepadStandardMappingFunction function; 137 } AvailableMappings[] = { 138 // http://www.linux-usb.org/usb.ids 139 { "0079", "0006", MapperDragonRiseGeneric }, // DragonRise Generic USB 140 { "045e", "028e", MapperXInputStyleGamepad }, // Xbox 360 Controller 141 { "045e", "028f", MapperXInputStyleGamepad }, // Xbox 360 Wireless Controller 142 { "046d", "c21d", MapperXInputStyleGamepad }, // Logitech F310 143 { "046d", "c21e", MapperXInputStyleGamepad }, // Logitech F510 144 { "046d", "c21f", MapperXInputStyleGamepad }, // Logitech F710 145 { "054c", "0268", MapperPlaystationSixAxis }, // Playstation SIXAXIS 146 { "0925", "0005", MapperLakeviewResearch }, // SmartJoy PLUS Adapter 147 { "0925", "8866", MapperLakeviewResearch }, // WiseGroup MP-8866 148 { "0e8f", "0003", MapperXGEAR }, // XFXforce XGEAR PS2 Controller 149 }; 150 151 } // namespace 152 153 GamepadStandardMappingFunction GetGamepadStandardMappingFunction( 154 const base::StringPiece& vendor_id, 155 const base::StringPiece& product_id) { 156 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) { 157 MappingData& item = AvailableMappings[i]; 158 if (vendor_id == item.vendor_id && product_id == item.product_id) 159 return item.function; 160 } 161 return NULL; 162 } 163 164 } // namespace content 165