Home | History | Annotate | Download | only in gamepad
      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 MapperXbox360Gamepad(
     12     const blink::WebGamepad& input,
     13     blink::WebGamepad* mapped) {
     14   *mapped = input;
     15   mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]);
     16   mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]);
     17   mapped->buttons[kButtonBackSelect] = input.buttons[9];
     18   mapped->buttons[kButtonStart] = input.buttons[8];
     19   mapped->buttons[kButtonLeftThumbstick] = input.buttons[6];
     20   mapped->buttons[kButtonRightThumbstick] = input.buttons[7];
     21   mapped->buttons[kButtonDpadUp] = input.buttons[11];
     22   mapped->buttons[kButtonDpadDown] = input.buttons[12];
     23   mapped->buttons[kButtonDpadLeft] = input.buttons[13];
     24   mapped->buttons[kButtonDpadRight] = input.buttons[14];
     25   mapped->buttons[kButtonMeta] = input.buttons[10];
     26   mapped->axes[kAxisRightStickX] = input.axes[3];
     27   mapped->axes[kAxisRightStickY] = input.axes[4];
     28   mapped->buttonsLength = kNumButtons;
     29   mapped->axesLength = kNumAxes;
     30 }
     31 
     32 void MapperPlaystationSixAxis(
     33     const blink::WebGamepad& input,
     34     blink::WebGamepad* mapped) {
     35   *mapped = input;
     36   mapped->buttons[kButtonPrimary] = input.buttons[14];
     37   mapped->buttons[kButtonSecondary] = input.buttons[13];
     38   mapped->buttons[kButtonTertiary] = input.buttons[15];
     39   mapped->buttons[kButtonQuaternary] = input.buttons[12];
     40   mapped->buttons[kButtonLeftShoulder] = input.buttons[10];
     41   mapped->buttons[kButtonRightShoulder] = input.buttons[11];
     42 
     43   mapped->buttons[kButtonLeftTrigger] = ButtonFromButtonAndAxis(
     44       input.buttons[8], input.axes[14]);
     45   mapped->buttons[kButtonRightTrigger] = ButtonFromButtonAndAxis(
     46       input.buttons[9], input.axes[15]);
     47 
     48   mapped->buttons[kButtonBackSelect] = input.buttons[0];
     49   mapped->buttons[kButtonStart] = input.buttons[3];
     50   mapped->buttons[kButtonLeftThumbstick] = input.buttons[1];
     51   mapped->buttons[kButtonRightThumbstick] = input.buttons[2];
     52 
     53   // The SixAxis Dpad is pressure sensative
     54   mapped->buttons[kButtonDpadUp] = ButtonFromButtonAndAxis(
     55       input.buttons[4], input.axes[10]);
     56   mapped->buttons[kButtonDpadDown] = ButtonFromButtonAndAxis(
     57       input.buttons[6], input.axes[12]);
     58   mapped->buttons[kButtonDpadLeft] = ButtonFromButtonAndAxis(
     59       input.buttons[7], input.axes[13]);
     60   mapped->buttons[kButtonDpadRight] = ButtonFromButtonAndAxis(
     61       input.buttons[5], input.axes[11]);
     62 
     63   mapped->buttons[kButtonMeta] = input.buttons[16];
     64   mapped->axes[kAxisRightStickY] = input.axes[5];
     65 
     66   mapped->buttonsLength = kNumButtons;
     67   mapped->axesLength = kNumAxes;
     68 }
     69 
     70 void MapperDualshock4(
     71     const blink::WebGamepad& input,
     72     blink::WebGamepad* mapped) {
     73   enum Dualshock4Buttons {
     74     kTouchpadButton = kNumButtons,
     75     kNumDualshock4Buttons
     76   };
     77 
     78   *mapped = input;
     79   mapped->buttons[kButtonPrimary] = input.buttons[1];
     80   mapped->buttons[kButtonSecondary] = input.buttons[2];
     81   mapped->buttons[kButtonTertiary] = input.buttons[0];
     82   mapped->buttons[kButtonQuaternary] = input.buttons[3];
     83   mapped->buttons[kButtonLeftShoulder] = input.buttons[4];
     84   mapped->buttons[kButtonRightShoulder] = input.buttons[5];
     85   mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[3]);
     86   mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[4]);
     87   mapped->buttons[kButtonBackSelect] = input.buttons[8];
     88   mapped->buttons[kButtonStart] = input.buttons[9];
     89   mapped->buttons[kButtonLeftThumbstick] = input.buttons[10];
     90   mapped->buttons[kButtonRightThumbstick] = input.buttons[11];
     91   mapped->buttons[kButtonMeta] = input.buttons[12];
     92   mapped->buttons[kTouchpadButton] = input.buttons[13];
     93   mapped->axes[kAxisRightStickY] = input.axes[5];
     94   DpadFromAxis(mapped, input.axes[9]);
     95 
     96   mapped->buttonsLength = kNumDualshock4Buttons;
     97   mapped->axesLength = kNumAxes;
     98 }
     99 
    100 void MapperDirectInputStyle(
    101     const blink::WebGamepad& input,
    102     blink::WebGamepad* mapped) {
    103   *mapped = input;
    104   mapped->buttons[kButtonPrimary] = input.buttons[1];
    105   mapped->buttons[kButtonSecondary] = input.buttons[2];
    106   mapped->buttons[kButtonTertiary] = input.buttons[0];
    107   mapped->axes[kAxisRightStickY] = input.axes[5];
    108   DpadFromAxis(mapped, input.axes[9]);
    109   mapped->buttonsLength = kNumButtons - 1; /* no meta */
    110   mapped->axesLength = kNumAxes;
    111 }
    112 
    113 void MapperMacallyIShock(
    114     const blink::WebGamepad& input,
    115     blink::WebGamepad* mapped) {
    116   enum IShockButtons {
    117     kButtonC = kNumButtons,
    118     kButtonD,
    119     kButtonE,
    120     kNumIShockButtons
    121   };
    122 
    123   *mapped = input;
    124   mapped->buttons[kButtonPrimary] = input.buttons[6];
    125   mapped->buttons[kButtonSecondary] = input.buttons[5];
    126   mapped->buttons[kButtonTertiary] = input.buttons[7];
    127   mapped->buttons[kButtonQuaternary] = input.buttons[4];
    128   mapped->buttons[kButtonLeftShoulder] = input.buttons[14];
    129   mapped->buttons[kButtonRightShoulder] = input.buttons[12];
    130   mapped->buttons[kButtonLeftTrigger] = input.buttons[15];
    131   mapped->buttons[kButtonRightTrigger] = input.buttons[13];
    132   mapped->buttons[kButtonBackSelect] = input.buttons[9];
    133   mapped->buttons[kButtonStart] = input.buttons[10];
    134   mapped->buttons[kButtonLeftThumbstick] = input.buttons[16];
    135   mapped->buttons[kButtonRightThumbstick] = input.buttons[17];
    136   mapped->buttons[kButtonDpadUp] = input.buttons[0];
    137   mapped->buttons[kButtonDpadDown] = input.buttons[1];
    138   mapped->buttons[kButtonDpadLeft] = input.buttons[2];
    139   mapped->buttons[kButtonDpadRight] = input.buttons[3];
    140   mapped->buttons[kButtonMeta] = input.buttons[11];
    141   mapped->buttons[kButtonC] = input.buttons[8];
    142   mapped->buttons[kButtonD] = input.buttons[18];
    143   mapped->buttons[kButtonE] = input.buttons[19];
    144   mapped->axes[kAxisLeftStickX] = input.axes[0];
    145   mapped->axes[kAxisLeftStickY] = input.axes[1];
    146   mapped->axes[kAxisRightStickX] = -input.axes[5];
    147   mapped->axes[kAxisRightStickY] = input.axes[6];
    148 
    149   mapped->buttonsLength = kNumIShockButtons;
    150   mapped->axesLength = kNumAxes;
    151 }
    152 
    153 void MapperXGEAR(
    154     const blink::WebGamepad& input,
    155     blink::WebGamepad* mapped) {
    156   *mapped = input;
    157   mapped->buttons[kButtonPrimary] = input.buttons[2];
    158   mapped->buttons[kButtonTertiary] = input.buttons[3];
    159   mapped->buttons[kButtonQuaternary] = input.buttons[0];
    160   mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
    161   mapped->buttons[kButtonRightShoulder] = input.buttons[7];
    162   mapped->buttons[kButtonLeftTrigger] = input.buttons[4];
    163   mapped->buttons[kButtonRightTrigger] = input.buttons[5];
    164   DpadFromAxis(mapped, input.axes[9]);
    165   mapped->axes[kAxisRightStickX] = input.axes[5];
    166   mapped->axes[kAxisRightStickY] = input.axes[2];
    167   mapped->buttonsLength = kNumButtons - 1; /* no meta */
    168   mapped->axesLength = kNumAxes;
    169 }
    170 
    171 void MapperSmartJoyPLUS(
    172     const blink::WebGamepad& input,
    173     blink::WebGamepad* mapped) {
    174   *mapped = input;
    175   mapped->buttons[kButtonPrimary] = input.buttons[2];
    176   mapped->buttons[kButtonTertiary] = input.buttons[3];
    177   mapped->buttons[kButtonQuaternary] = input.buttons[0];
    178   mapped->buttons[kButtonStart] = input.buttons[8];
    179   mapped->buttons[kButtonBackSelect] = input.buttons[9];
    180   mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
    181   mapped->buttons[kButtonRightShoulder] = input.buttons[7];
    182   mapped->buttons[kButtonLeftTrigger] = input.buttons[4];
    183   mapped->buttons[kButtonRightTrigger] = input.buttons[5];
    184   DpadFromAxis(mapped, input.axes[9]);
    185   mapped->axes[kAxisRightStickY] = input.axes[5];
    186   mapped->buttonsLength = kNumButtons - 1; /* no meta */
    187   mapped->axesLength = kNumAxes;
    188 }
    189 
    190 void MapperDragonRiseGeneric(
    191     const blink::WebGamepad& input,
    192     blink::WebGamepad* mapped) {
    193   *mapped = input;
    194   DpadFromAxis(mapped, input.axes[9]);
    195   mapped->axes[kAxisLeftStickX] = input.axes[0];
    196   mapped->axes[kAxisLeftStickY] = input.axes[1];
    197   mapped->axes[kAxisRightStickX] = input.axes[2];
    198   mapped->axes[kAxisRightStickY] = input.axes[5];
    199   mapped->buttonsLength = kNumButtons - 1; /* no meta */
    200   mapped->axesLength = kNumAxes;
    201 }
    202 
    203 void MapperOnLiveWireless(
    204     const blink::WebGamepad& input,
    205     blink::WebGamepad* mapped) {
    206   *mapped = input;
    207   mapped->buttons[kButtonPrimary] = input.buttons[0];
    208   mapped->buttons[kButtonSecondary] = input.buttons[1];
    209   mapped->buttons[kButtonTertiary] = input.buttons[3];
    210   mapped->buttons[kButtonQuaternary] = input.buttons[4];
    211   mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
    212   mapped->buttons[kButtonRightShoulder] = input.buttons[7];
    213   mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]);
    214   mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]);
    215   mapped->buttons[kButtonBackSelect] = input.buttons[10];
    216   mapped->buttons[kButtonStart] = input.buttons[11];
    217   mapped->buttons[kButtonLeftThumbstick] = input.buttons[13];
    218   mapped->buttons[kButtonRightThumbstick] = input.buttons[14];
    219   mapped->buttons[kButtonMeta] = input.buttons[12];
    220   mapped->axes[kAxisRightStickX] = input.axes[3];
    221   mapped->axes[kAxisRightStickY] = input.axes[4];
    222   DpadFromAxis(mapped, input.axes[9]);
    223 
    224   mapped->buttonsLength = kNumButtons;
    225   mapped->axesLength = kNumAxes;
    226 }
    227 
    228 void MapperADT1(
    229     const blink::WebGamepad& input,
    230     blink::WebGamepad* mapped) {
    231   *mapped = input;
    232   mapped->buttons[kButtonPrimary] = input.buttons[0];
    233   mapped->buttons[kButtonSecondary] = input.buttons[1];
    234   mapped->buttons[kButtonTertiary] = input.buttons[3];
    235   mapped->buttons[kButtonQuaternary] = input.buttons[4];
    236   mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
    237   mapped->buttons[kButtonRightShoulder] = input.buttons[7];
    238   mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[3]);
    239   mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[4]);
    240   mapped->buttons[kButtonBackSelect] = NullButton();
    241   mapped->buttons[kButtonStart] = NullButton();
    242   mapped->buttons[kButtonLeftThumbstick] = input.buttons[13];
    243   mapped->buttons[kButtonRightThumbstick] = input.buttons[14];
    244   mapped->buttons[kButtonMeta] = input.buttons[12];
    245   mapped->axes[kAxisRightStickY] = input.axes[5];
    246   DpadFromAxis(mapped, input.axes[9]);
    247 
    248   mapped->buttonsLength = kNumButtons;
    249   mapped->axesLength = kNumAxes;
    250 }
    251 
    252 struct MappingData {
    253   const char* const vendor_id;
    254   const char* const product_id;
    255   GamepadStandardMappingFunction function;
    256 } AvailableMappings[] = {
    257   // http://www.linux-usb.org/usb.ids
    258   { "0079", "0006", MapperDragonRiseGeneric },  // DragonRise Generic USB
    259   { "045e", "028e", MapperXbox360Gamepad },     // Xbox 360 Controller
    260   { "045e", "028f", MapperXbox360Gamepad },     // Xbox 360 Wireless Controller
    261   { "046d", "c216", MapperDirectInputStyle },   // Logitech F310, D mode
    262   { "046d", "c218", MapperDirectInputStyle },   // Logitech F510, D mode
    263   { "046d", "c219", MapperDirectInputStyle },   // Logitech F710, D mode
    264   { "054c", "0268", MapperPlaystationSixAxis }, // Playstation SIXAXIS
    265   { "054c", "05c4", MapperDualshock4 },         // Playstation Dualshock 4
    266   { "0925", "0005", MapperSmartJoyPLUS },       // SmartJoy PLUS Adapter
    267   { "0e8f", "0003", MapperXGEAR },              // XFXforce XGEAR PS2 Controller
    268   { "2222", "0060", MapperDirectInputStyle },   // Macally iShockX, analog mode
    269   { "2222", "4010", MapperMacallyIShock },      // Macally iShock
    270   { "2378", "1008", MapperOnLiveWireless },     // OnLive Controller (Bluetooth)
    271   { "2378", "100a", MapperOnLiveWireless },     // OnLive Controller (Wired)
    272   { "18d1", "2c40", MapperADT1 },               // ADT-1 Controller
    273 };
    274 
    275 }  // namespace
    276 
    277 GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
    278     const base::StringPiece& vendor_id,
    279     const base::StringPiece& product_id) {
    280   for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
    281     MappingData& item = AvailableMappings[i];
    282     if (vendor_id == item.vendor_id && product_id == item.product_id)
    283       return item.function;
    284   }
    285   return NULL;
    286 }
    287 
    288 }  // namespace content
    289