Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / gamepad / gamepad_standard_mappings_win.cc
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 namespace content {
8
9 namespace {
10
11 void MapperLogitechDualAction(const blink::WebGamepad& input,
12                               blink::WebGamepad* mapped) {
13   *mapped = input;
14   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
15   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[2];
16   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[0];
17   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
18   DpadFromAxis(mapped, input.axes[9]);
19
20   mapped->buttonsLength = BUTTON_INDEX_COUNT;
21   mapped->axesLength = AXIS_INDEX_COUNT;
22 }
23
24 void Mapper2Axes8Keys(const blink::WebGamepad& input,
25                       blink::WebGamepad* mapped) {
26   *mapped = input;
27   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[2];
28   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
29   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
30   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[0];
31   mapped->buttons[BUTTON_INDEX_DPAD_UP] = AxisNegativeAsButton(input.axes[1]);
32   mapped->buttons[BUTTON_INDEX_DPAD_DOWN] = AxisPositiveAsButton(input.axes[1]);
33   mapped->buttons[BUTTON_INDEX_DPAD_LEFT] = AxisNegativeAsButton(input.axes[0]);
34   mapped->buttons[BUTTON_INDEX_DPAD_RIGHT] =
35       AxisPositiveAsButton(input.axes[0]);
36
37   // Missing buttons
38   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = blink::WebGamepadButton();
39   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = blink::WebGamepadButton();
40   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = blink::WebGamepadButton();
41   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = blink::WebGamepadButton();
42   mapped->buttons[BUTTON_INDEX_META] = blink::WebGamepadButton();
43
44   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1;
45   mapped->axesLength = 0;
46 }
47
48 void MapperDualshock4(const blink::WebGamepad& input,
49                       blink::WebGamepad* mapped) {
50   enum Dualshock4Buttons {
51     DUALSHOCK_BUTTON_TOUCHPAD = BUTTON_INDEX_COUNT,
52     DUALSHOCK_BUTTON_COUNT
53   };
54
55   *mapped = input;
56   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
57   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[2];
58   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[0];
59   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[3];
60   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[4];
61   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[5];
62   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[3]);
63   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[4]);
64   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[8];
65   mapped->buttons[BUTTON_INDEX_START] = input.buttons[9];
66   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[10];
67   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[11];
68   mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
69   mapped->buttons[DUALSHOCK_BUTTON_TOUCHPAD] = input.buttons[13];
70   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
71   DpadFromAxis(mapped, input.axes[9]);
72
73   mapped->buttonsLength = DUALSHOCK_BUTTON_COUNT;
74   mapped->axesLength = AXIS_INDEX_COUNT;
75 }
76
77 void MapperOnLiveWireless(const blink::WebGamepad& input,
78                           blink::WebGamepad* mapped) {
79   *mapped = input;
80   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
81   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
82   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
83   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
84   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
85   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
86   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[2]);
87   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[5]);
88   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[10];
89   mapped->buttons[BUTTON_INDEX_START] = input.buttons[11];
90   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
91   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
92   mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
93   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[3];
94   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[4];
95   DpadFromAxis(mapped, input.axes[9]);
96
97   mapped->buttonsLength = BUTTON_INDEX_COUNT;
98   mapped->axesLength = AXIS_INDEX_COUNT;
99 }
100
101 void MapperADT1(const blink::WebGamepad& input, blink::WebGamepad* mapped) {
102   *mapped = input;
103   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
104   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
105   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
106   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
107   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
108   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
109   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[4]);
110   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[3]);
111   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = NullButton();
112   mapped->buttons[BUTTON_INDEX_START] = NullButton();
113   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
114   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
115   mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
116   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
117   DpadFromAxis(mapped, input.axes[9]);
118
119   mapped->buttonsLength = BUTTON_INDEX_COUNT;
120   mapped->axesLength = AXIS_INDEX_COUNT;
121 }
122
123 struct MappingData {
124   const char* const vendor_id;
125   const char* const product_id;
126   GamepadStandardMappingFunction function;
127 } AvailableMappings[] = {
128       // http://www.linux-usb.org/usb.ids
129       {"046d", "c216", MapperLogitechDualAction},  // Logitech DualAction
130       {"0079", "0011", Mapper2Axes8Keys},          // 2Axes 8Keys Game Pad
131       {"054c", "05c4", MapperDualshock4},          // Playstation Dualshock 4
132       {"2378", "1008", MapperOnLiveWireless},  // OnLive Controller (Bluetooth)
133       {"2378", "100a", MapperOnLiveWireless},  // OnLive Controller (Wired)
134       {"18d1", "2c40", MapperADT1},            // ADT-1 Controller
135 };
136
137 }  // namespace
138
139 GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
140     const base::StringPiece& vendor_id,
141     const base::StringPiece& product_id) {
142   for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
143     MappingData& item = AvailableMappings[i];
144     if (vendor_id == item.vendor_id && product_id == item.product_id)
145       return item.function;
146   }
147   return NULL;
148 }
149
150 }  // namespace content