gamepad_standard_mappings_linux.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
9namespace content {
10
11namespace {
12
13float AxisToButton(float input) {
14  return (input + 1.f) / 2.f;
15}
16
17float AxisNegativeAsButton(float input) {
18  return (input < -0.5f) ? 1.f : 0.f;
19}
20
21float AxisPositiveAsButton(float input) {
22  return (input > 0.5f) ? 1.f : 0.f;
23}
24
25void MapperXInputStyleGamepad(
26    const WebKit::WebGamepad& input,
27    WebKit::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
46void MapperLakeviewResearch(
47    const WebKit::WebGamepad& input,
48    WebKit::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
67void MapperPlaystationSixAxis(
68    const WebKit::WebGamepad& input,
69    WebKit::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
93void MapperXGEAR(
94    const WebKit::WebGamepad& input,
95    WebKit::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
116struct MappingData {
117  const char* const vendor_id;
118  const char* const product_id;
119  GamepadStandardMappingFunction function;
120} AvailableMappings[] = {
121  // http://www.linux-usb.org/usb.ids
122  { "045e", "028e", MapperXInputStyleGamepad }, // Xbox 360 Controller
123  { "045e", "028f", MapperXInputStyleGamepad }, // Xbox 360 Wireless Controller
124  { "046d", "c21d", MapperXInputStyleGamepad }, // Logitech F310
125  { "046d", "c21e", MapperXInputStyleGamepad }, // Logitech F510
126  { "046d", "c21f", MapperXInputStyleGamepad }, // Logitech F710
127  { "054c", "0268", MapperPlaystationSixAxis }, // Playstation SIXAXIS
128  { "0925", "0005", MapperLakeviewResearch }, // SmartJoy PLUS Adapter
129  { "0925", "8866", MapperLakeviewResearch }, // WiseGroup MP-8866
130  { "0e8f", "0003", MapperXGEAR }, // XFXforce XGEAR PS2 Controller
131};
132
133}  // namespace
134
135GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
136    const base::StringPiece& vendor_id,
137    const base::StringPiece& product_id) {
138  for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
139    MappingData& item = AvailableMappings[i];
140    if (vendor_id == item.vendor_id && product_id == item.product_id)
141      return item.function;
142  }
143  return NULL;
144}
145
146}  // namespace content
147