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 #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 void DpadFromAxis(WebKit::WebGamepad* mapped, float dir) {
     18   // Dpad is mapped as a direction on one axis, where -1 is up and it
     19   // increases clockwise to 1, which is up + left. It's set to a large (> 1.f)
     20   // number when nothing is depressed, except on start up, sometimes it's 0.0
     21   // for no data, rather than the large number.
     22   if (dir == 0.0f) {
     23     mapped->buttons[kButtonDpadUp] = 0.f;
     24     mapped->buttons[kButtonDpadDown] = 0.f;
     25     mapped->buttons[kButtonDpadLeft] = 0.f;
     26     mapped->buttons[kButtonDpadRight] = 0.f;
     27   } else {
     28     mapped->buttons[kButtonDpadUp] = (dir >= -1.f && dir < -0.7f) ||
     29                                      (dir >= .95f && dir <= 1.f);
     30     mapped->buttons[kButtonDpadRight] = dir >= -.75f && dir < -.1f;
     31     mapped->buttons[kButtonDpadDown] = dir >= -.2f && dir < .45f;
     32     mapped->buttons[kButtonDpadLeft] = dir >= .4f && dir <= 1.f;
     33   }
     34 }
     35 
     36 void MapperXbox360Gamepad(
     37     const WebKit::WebGamepad& input,
     38     WebKit::WebGamepad* mapped) {
     39   *mapped = input;
     40   mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]);
     41   mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]);
     42   mapped->buttons[kButtonBackSelect] = input.buttons[9];
     43   mapped->buttons[kButtonStart] = input.buttons[8];
     44   mapped->buttons[kButtonLeftThumbstick] = input.buttons[6];
     45   mapped->buttons[kButtonRightThumbstick] = input.buttons[7];
     46   mapped->buttons[kButtonDpadUp] = input.buttons[11];
     47   mapped->buttons[kButtonDpadDown] = input.buttons[12];
     48   mapped->buttons[kButtonDpadLeft] = input.buttons[13];
     49   mapped->buttons[kButtonDpadRight] = input.buttons[14];
     50   mapped->buttons[kButtonMeta] = input.buttons[10];
     51   mapped->axes[kAxisRightStickX] = input.axes[3];
     52   mapped->axes[kAxisRightStickY] = input.axes[4];
     53   mapped->buttonsLength = kNumButtons;
     54   mapped->axesLength = kNumAxes;
     55 }
     56 
     57 void MapperPlaystationSixAxis(
     58     const WebKit::WebGamepad& input,
     59     WebKit::WebGamepad* mapped) {
     60   *mapped = input;
     61   mapped->buttons[kButtonPrimary] = input.buttons[14];
     62   mapped->buttons[kButtonSecondary] = input.buttons[13];
     63   mapped->buttons[kButtonTertiary] = input.buttons[15];
     64   mapped->buttons[kButtonQuaternary] = input.buttons[12];
     65   mapped->buttons[kButtonLeftShoulder] = input.buttons[10];
     66   mapped->buttons[kButtonRightShoulder] = input.buttons[11];
     67   mapped->buttons[kButtonLeftTrigger] = input.buttons[8];
     68   mapped->buttons[kButtonRightTrigger] = input.buttons[9];
     69   mapped->buttons[kButtonBackSelect] = input.buttons[0];
     70   mapped->buttons[kButtonStart] = input.buttons[3];
     71   mapped->buttons[kButtonLeftThumbstick] = input.buttons[1];
     72   mapped->buttons[kButtonRightThumbstick] = input.buttons[2];
     73   mapped->buttons[kButtonDpadUp] = input.buttons[4];
     74   mapped->buttons[kButtonDpadDown] = input.buttons[6];
     75   mapped->buttons[kButtonDpadLeft] = input.buttons[7];
     76   mapped->buttons[kButtonDpadRight] = input.buttons[5];
     77   mapped->buttons[kButtonMeta] = input.buttons[16];
     78   mapped->axes[kAxisRightStickY] = input.axes[5];
     79 
     80   mapped->buttonsLength = kNumButtons;
     81   mapped->axesLength = kNumAxes;
     82 }
     83 
     84 void MapperDirectInputStyle(
     85     const WebKit::WebGamepad& input,
     86     WebKit::WebGamepad* mapped) {
     87   *mapped = input;
     88   mapped->buttons[kButtonPrimary] = input.buttons[1];
     89   mapped->buttons[kButtonSecondary] = input.buttons[2];
     90   mapped->buttons[kButtonTertiary] = input.buttons[0];
     91   mapped->axes[kAxisRightStickY] = input.axes[5];
     92   DpadFromAxis(mapped, input.axes[9]);
     93   mapped->buttonsLength = kNumButtons - 1; /* no meta */
     94   mapped->axesLength = kNumAxes;
     95 }
     96 
     97 void MapperMacallyIShock(
     98     const WebKit::WebGamepad& input,
     99     WebKit::WebGamepad* mapped) {
    100   enum IShockButtons {
    101     kButtonC = kNumButtons,
    102     kButtonD,
    103     kButtonE,
    104     kNumIShockButtons
    105   };
    106 
    107   *mapped = input;
    108   mapped->buttons[kButtonPrimary] = input.buttons[6];
    109   mapped->buttons[kButtonSecondary] = input.buttons[5];
    110   mapped->buttons[kButtonTertiary] = input.buttons[7];
    111   mapped->buttons[kButtonQuaternary] = input.buttons[4];
    112   mapped->buttons[kButtonLeftShoulder] = input.buttons[14];
    113   mapped->buttons[kButtonRightShoulder] = input.buttons[12];
    114   mapped->buttons[kButtonLeftTrigger] = input.buttons[15];
    115   mapped->buttons[kButtonRightTrigger] = input.buttons[13];
    116   mapped->buttons[kButtonBackSelect] = input.buttons[9];
    117   mapped->buttons[kButtonStart] = input.buttons[10];
    118   mapped->buttons[kButtonLeftThumbstick] = input.buttons[16];
    119   mapped->buttons[kButtonRightThumbstick] = input.buttons[17];
    120   mapped->buttons[kButtonDpadUp] = input.buttons[0];
    121   mapped->buttons[kButtonDpadDown] = input.buttons[1];
    122   mapped->buttons[kButtonDpadLeft] = input.buttons[2];
    123   mapped->buttons[kButtonDpadRight] = input.buttons[3];
    124   mapped->buttons[kButtonMeta] = input.buttons[11];
    125   mapped->buttons[kButtonC] = input.buttons[8];
    126   mapped->buttons[kButtonD] = input.buttons[18];
    127   mapped->buttons[kButtonE] = input.buttons[19];
    128   mapped->axes[kAxisLeftStickX] = input.axes[0];
    129   mapped->axes[kAxisLeftStickY] = input.axes[1];
    130   mapped->axes[kAxisRightStickX] = -input.axes[5];
    131   mapped->axes[kAxisRightStickY] = input.axes[6];
    132 
    133   mapped->buttonsLength = kNumIShockButtons;
    134   mapped->axesLength = kNumAxes;
    135 }
    136 
    137 void MapperXGEAR(
    138     const WebKit::WebGamepad& input,
    139     WebKit::WebGamepad* mapped) {
    140   *mapped = input;
    141   mapped->buttons[kButtonPrimary] = input.buttons[2];
    142   mapped->buttons[kButtonTertiary] = input.buttons[3];
    143   mapped->buttons[kButtonQuaternary] = input.buttons[0];
    144   mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
    145   mapped->buttons[kButtonRightShoulder] = input.buttons[7];
    146   mapped->buttons[kButtonLeftTrigger] = input.buttons[4];
    147   mapped->buttons[kButtonRightTrigger] = input.buttons[5];
    148   DpadFromAxis(mapped, input.axes[9]);
    149   mapped->axes[kAxisRightStickX] = input.axes[5];
    150   mapped->axes[kAxisRightStickY] = input.axes[2];
    151   mapped->buttonsLength = kNumButtons - 1; /* no meta */
    152   mapped->axesLength = kNumAxes;
    153 }
    154 
    155 void MapperSmartJoyPLUS(
    156     const WebKit::WebGamepad& input,
    157     WebKit::WebGamepad* mapped) {
    158   *mapped = input;
    159   mapped->buttons[kButtonPrimary] = input.buttons[2];
    160   mapped->buttons[kButtonTertiary] = input.buttons[3];
    161   mapped->buttons[kButtonQuaternary] = input.buttons[0];
    162   mapped->buttons[kButtonStart] = input.buttons[8];
    163   mapped->buttons[kButtonBackSelect] = input.buttons[9];
    164   mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
    165   mapped->buttons[kButtonRightShoulder] = input.buttons[7];
    166   mapped->buttons[kButtonLeftTrigger] = input.buttons[4];
    167   mapped->buttons[kButtonRightTrigger] = input.buttons[5];
    168   DpadFromAxis(mapped, input.axes[9]);
    169   mapped->axes[kAxisRightStickY] = input.axes[5];
    170   mapped->buttonsLength = kNumButtons - 1; /* no meta */
    171   mapped->axesLength = kNumAxes;
    172 }
    173 
    174 void MapperDragonRiseGeneric(
    175     const WebKit::WebGamepad& input,
    176     WebKit::WebGamepad* mapped) {
    177   *mapped = input;
    178   DpadFromAxis(mapped, input.axes[9]);
    179   mapped->axes[kAxisLeftStickX] = input.axes[0];
    180   mapped->axes[kAxisLeftStickY] = input.axes[1];
    181   mapped->axes[kAxisRightStickX] = input.axes[2];
    182   mapped->axes[kAxisRightStickY] = input.axes[5];
    183   mapped->buttonsLength = kNumButtons - 1; /* no meta */
    184   mapped->axesLength = kNumAxes;
    185 }
    186 
    187 struct MappingData {
    188   const char* const vendor_id;
    189   const char* const product_id;
    190   GamepadStandardMappingFunction function;
    191 } AvailableMappings[] = {
    192   // http://www.linux-usb.org/usb.ids
    193   { "0079", "0006", MapperDragonRiseGeneric },  // DragonRise Generic USB
    194   { "045e", "028e", MapperXbox360Gamepad },     // Xbox 360 Controller
    195   { "045e", "028f", MapperXbox360Gamepad },     // Xbox 360 Wireless Controller
    196   { "046d", "c216", MapperDirectInputStyle },   // Logitech F310, D mode
    197   { "046d", "c218", MapperDirectInputStyle },   // Logitech F510, D mode
    198   { "046d", "c219", MapperDirectInputStyle },   // Logitech F710, D mode
    199   { "054c", "0268", MapperPlaystationSixAxis }, // Playstation SIXAXIS
    200   { "0925", "0005", MapperSmartJoyPLUS },       // SmartJoy PLUS Adapter
    201   { "0e8f", "0003", MapperXGEAR },              // XFXforce XGEAR PS2 Controller
    202   { "2222", "0060", MapperDirectInputStyle },   // Macally iShockX, analog mode
    203   { "2222", "4010", MapperMacallyIShock },      // Macally iShock
    204 };
    205 
    206 }  // namespace
    207 
    208 GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
    209     const base::StringPiece& vendor_id,
    210     const base::StringPiece& product_id) {
    211   for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
    212     MappingData& item = AvailableMappings[i];
    213     if (vendor_id == item.vendor_id && product_id == item.product_id)
    214       return item.function;
    215   }
    216   return NULL;
    217 }
    218 
    219 }  // namespace content
    220