OLD | NEW |
| (Empty) |
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_linux.h" | |
6 | |
7 #include "content/common/gamepad_hardware_buffer.h" | |
8 | |
9 namespace content { | |
10 | |
11 namespace { | |
12 | |
13 // This defines our canonical mapping order for gamepad-like devices. If these | |
14 // items cannot all be satisfied, it is a case-by-case judgement as to whether | |
15 // it is better to leave the device unmapped, or to partially map it. In | |
16 // general, err towards leaving it *unmapped* so that content can handle | |
17 // appropriately. | |
18 | |
19 enum CanonicalButtonIndex { | |
20 kButtonPrimary, | |
21 kButtonSecondary, | |
22 kButtonTertiary, | |
23 kButtonQuaternary, | |
24 kButtonLeftShoulder, | |
25 kButtonRightShoulder, | |
26 kButtonLeftTrigger, | |
27 kButtonRightTrigger, | |
28 kButtonBackSelect, | |
29 kButtonStart, | |
30 kButtonLeftThumbstick, | |
31 kButtonRightThumbstick, | |
32 kButtonDpadUp, | |
33 kButtonDpadDown, | |
34 kButtonDpadLeft, | |
35 kButtonDpadRight, | |
36 kButtonMeta, | |
37 kNumButtons | |
38 }; | |
39 | |
40 enum CanonicalAxisIndex { | |
41 kAxisLeftStickX, | |
42 kAxisLeftStickY, | |
43 kAxisRightStickX, | |
44 kAxisRightStickY, | |
45 kNumAxes | |
46 }; | |
47 | |
48 float AxisToButton(float input) { | |
49 return (input + 1.f) / 2.f; | |
50 } | |
51 | |
52 float AxisNegativeAsButton(float input) { | |
53 return (input < -0.5f) ? 1.f : 0.f; | |
54 } | |
55 | |
56 float AxisPositiveAsButton(float input) { | |
57 return (input > 0.5f) ? 1.f : 0.f; | |
58 } | |
59 | |
60 void MapperXInputStyleGamepad( | |
61 const WebKit::WebGamepad& input, | |
62 WebKit::WebGamepad* mapped) { | |
63 *mapped = input; | |
64 mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]); | |
65 mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]); | |
66 mapped->buttons[kButtonBackSelect] = input.buttons[6]; | |
67 mapped->buttons[kButtonStart] = input.buttons[7]; | |
68 mapped->buttons[kButtonLeftThumbstick] = input.buttons[9]; | |
69 mapped->buttons[kButtonRightThumbstick] = input.buttons[10]; | |
70 mapped->buttons[kButtonDpadUp] = AxisNegativeAsButton(input.axes[7]); | |
71 mapped->buttons[kButtonDpadDown] = AxisPositiveAsButton(input.axes[7]); | |
72 mapped->buttons[kButtonDpadLeft] = AxisNegativeAsButton(input.axes[6]); | |
73 mapped->buttons[kButtonDpadRight] = AxisPositiveAsButton(input.axes[6]); | |
74 mapped->buttons[kButtonMeta] = input.buttons[8]; | |
75 mapped->axes[kAxisRightStickX] = input.axes[3]; | |
76 mapped->axes[kAxisRightStickY] = input.axes[4]; | |
77 mapped->buttonsLength = kNumButtons; | |
78 mapped->axesLength = kNumAxes; | |
79 } | |
80 | |
81 void MapperMP8866( | |
82 const WebKit::WebGamepad& input, | |
83 WebKit::WebGamepad* mapped) { | |
84 *mapped = input; | |
85 mapped->buttons[kButtonPrimary] = input.buttons[2]; | |
86 mapped->buttons[kButtonTertiary] = input.buttons[3]; | |
87 mapped->buttons[kButtonQuaternary] = input.buttons[0]; | |
88 mapped->buttons[kButtonLeftShoulder] = input.buttons[6]; | |
89 mapped->buttons[kButtonRightShoulder] = input.buttons[7]; | |
90 mapped->buttons[kButtonLeftTrigger] = input.buttons[4]; | |
91 mapped->buttons[kButtonRightTrigger] = input.buttons[5]; | |
92 mapped->buttons[kButtonBackSelect] = input.buttons[9]; | |
93 mapped->buttons[kButtonStart] = input.buttons[8]; | |
94 mapped->buttons[kButtonDpadUp] = AxisNegativeAsButton(input.axes[5]); | |
95 mapped->buttons[kButtonDpadDown] = AxisPositiveAsButton(input.axes[5]); | |
96 mapped->buttons[kButtonDpadLeft] = AxisNegativeAsButton(input.axes[4]); | |
97 mapped->buttons[kButtonDpadRight] = AxisPositiveAsButton(input.axes[4]); | |
98 mapped->buttonsLength = kNumButtons - 1; // no Meta on this device | |
99 mapped->axesLength = kNumAxes; | |
100 } | |
101 | |
102 void MapperPlaystationSixAxis( | |
103 const WebKit::WebGamepad& input, | |
104 WebKit::WebGamepad* mapped) { | |
105 *mapped = input; | |
106 mapped->buttons[kButtonPrimary] = input.buttons[14]; | |
107 mapped->buttons[kButtonSecondary] = input.buttons[13]; | |
108 mapped->buttons[kButtonTertiary] = input.buttons[15]; | |
109 mapped->buttons[kButtonQuaternary] = input.buttons[12]; | |
110 mapped->buttons[kButtonLeftShoulder] = input.buttons[10]; | |
111 mapped->buttons[kButtonRightShoulder] = input.buttons[11]; | |
112 mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[12]); | |
113 mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[13]); | |
114 mapped->buttons[kButtonBackSelect] = input.buttons[0]; | |
115 mapped->buttons[kButtonStart] = input.buttons[3]; | |
116 mapped->buttons[kButtonLeftThumbstick] = input.buttons[1]; | |
117 mapped->buttons[kButtonRightThumbstick] = input.buttons[2]; | |
118 mapped->buttons[kButtonDpadUp] = AxisToButton(input.axes[8]); | |
119 mapped->buttons[kButtonDpadDown] = AxisToButton(input.axes[10]); | |
120 mapped->buttons[kButtonDpadLeft] = input.buttons[7]; | |
121 mapped->buttons[kButtonDpadRight] = AxisToButton(input.axes[9]); | |
122 mapped->buttons[kButtonMeta] = input.buttons[16]; | |
123 | |
124 mapped->buttonsLength = kNumButtons; | |
125 mapped->axesLength = kNumAxes; | |
126 } | |
127 | |
128 struct MappingData { | |
129 const char* const vendor_id; | |
130 const char* const product_id; | |
131 GamepadStandardMappingFunction function; | |
132 } AvailableMappings[] = { | |
133 // http://www.linux-usb.org/usb.ids | |
134 { "045e", "028e", MapperXInputStyleGamepad }, // Xbox 360 Controller | |
135 { "045e", "028f", MapperXInputStyleGamepad }, // Xbox 360 Wireless Controller | |
136 { "046d", "c21d", MapperXInputStyleGamepad }, // Logitech F310 | |
137 { "046d", "c21e", MapperXInputStyleGamepad }, // Logitech F510 | |
138 { "046d", "c21f", MapperXInputStyleGamepad }, // Logitech F710 | |
139 { "054c", "0268", MapperPlaystationSixAxis }, // Playstation SIXAXIS | |
140 { "0925", "8866", MapperMP8866 }, // WiseGroup MP-8866 | |
141 }; | |
142 | |
143 } // namespace | |
144 | |
145 GamepadStandardMappingFunction GetGamepadStandardMappingFunction( | |
146 const base::StringPiece& vendor_id, | |
147 const base::StringPiece& product_id) { | |
148 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) { | |
149 MappingData& item = AvailableMappings[i]; | |
150 if (vendor_id == item.vendor_id && product_id == item.product_id) | |
151 return item.function; | |
152 } | |
153 return NULL; | |
154 } | |
155 | |
156 } // namespace content | |
OLD | NEW |