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 // Maps 0..65535 to -1..1.
     14 float NormalizeDirectInputAxis(long value) {
     15   return (value * 1.f / 32767.5f) - 1.f;
     16 }
     17 
     18 float AxisNegativeAsButton(long value) {
     19   return (value < 32767) ? 1.f : 0.f;
     20 }
     21 
     22 float AxisPositiveAsButton(long value) {
     23   return (value > 32767) ? 1.f : 0.f;
     24 }
     25 
     26 void MapperDragonRiseGeneric(
     27     const WebKit::WebGamepad& input,
     28     WebKit::WebGamepad* mapped) {
     29   *mapped = input;
     30   mapped->buttons[0] = input.buttons[1];
     31   mapped->buttons[1] = input.buttons[2];
     32   mapped->buttons[2] = input.buttons[0];
     33   mapped->buttons[12] = input.buttons[16];
     34   mapped->buttons[13] = input.buttons[17];
     35   mapped->buttons[14] = input.buttons[18];
     36   mapped->buttons[15] = input.buttons[19];
     37   mapped->buttonsLength = 16;
     38   mapped->axes[0] = NormalizeDirectInputAxis(input.axes[0]);
     39   mapped->axes[1] = NormalizeDirectInputAxis(input.axes[1]);
     40   mapped->axes[2] = NormalizeDirectInputAxis(input.axes[2]);
     41   mapped->axes[3] = NormalizeDirectInputAxis(input.axes[5]);
     42   mapped->axesLength = 4;
     43 }
     44 
     45 void MapperLogitechDualAction(
     46     const WebKit::WebGamepad& input,
     47     WebKit::WebGamepad* mapped) {
     48   *mapped = input;
     49   mapped->buttons[0] = input.buttons[1];
     50   mapped->buttons[1] = input.buttons[2];
     51   mapped->buttons[2] = input.buttons[0];
     52   mapped->buttons[12] = input.buttons[16];
     53   mapped->buttons[13] = input.buttons[17];
     54   mapped->buttons[14] = input.buttons[18];
     55   mapped->buttons[15] = input.buttons[19];
     56   mapped->buttonsLength = 16;
     57   mapped->axes[0] = NormalizeDirectInputAxis(input.axes[0]);
     58   mapped->axes[1] = NormalizeDirectInputAxis(input.axes[1]);
     59   mapped->axes[2] = NormalizeDirectInputAxis(input.axes[2]);
     60   mapped->axes[3] = NormalizeDirectInputAxis(input.axes[5]);
     61   mapped->axesLength = 4;
     62 }
     63 
     64 void MapperLogitechPrecision(
     65     const WebKit::WebGamepad& input,
     66     WebKit::WebGamepad* mapped) {
     67   *mapped = input;
     68   mapped->buttons[0] = input.buttons[1];
     69   mapped->buttons[1] = input.buttons[2];
     70   mapped->buttons[2] = input.buttons[0];
     71   mapped->buttons[kButtonLeftThumbstick] = 0;  // Not present
     72   mapped->buttons[kButtonRightThumbstick] = 0;  // Not present
     73   mapped->buttons[12] = AxisNegativeAsButton(input.axes[1]);
     74   mapped->buttons[13] = AxisPositiveAsButton(input.axes[1]);
     75   mapped->buttons[14] = AxisNegativeAsButton(input.axes[0]);
     76   mapped->buttons[15] = AxisPositiveAsButton(input.axes[0]);
     77   mapped->buttonsLength = 16;
     78   mapped->axesLength = 0;
     79 }
     80 
     81 void Mapper2Axes8Keys(
     82     const WebKit::WebGamepad& input,
     83     WebKit::WebGamepad* mapped) {
     84   *mapped = input;
     85   mapped->buttons[kButtonLeftTrigger] = 0;  // Not present
     86   mapped->buttons[kButtonRightTrigger] = 0;  // Not present
     87   mapped->buttons[8] = input.buttons[6];
     88   mapped->buttons[9] = input.buttons[7];
     89   mapped->buttons[kButtonLeftThumbstick] = 0;  // Not present
     90   mapped->buttons[kButtonRightThumbstick] = 0;  // Not present
     91   mapped->buttons[12] = AxisNegativeAsButton(input.axes[1]);
     92   mapped->buttons[13] = AxisPositiveAsButton(input.axes[1]);
     93   mapped->buttons[14] = AxisNegativeAsButton(input.axes[0]);
     94   mapped->buttons[15] = AxisPositiveAsButton(input.axes[0]);
     95   mapped->buttonsLength = 16;
     96   mapped->axesLength = 0;
     97 }
     98 
     99 struct MappingData {
    100   const char* const vendor_id;
    101   const char* const product_id;
    102   GamepadStandardMappingFunction function;
    103 } AvailableMappings[] = {
    104   // http://www.linux-usb.org/usb.ids
    105   { "0079", "0006", MapperDragonRiseGeneric },  // DragonRise Generic USB
    106   { "046d", "c216", MapperLogitechDualAction },  // Logitech DualAction
    107   { "046d", "c21a", MapperLogitechPrecision },  // Logitech Precision
    108   { "12bd", "d012", Mapper2Axes8Keys },  // 2Axes 8Keys Game Pad
    109 };
    110 
    111 }  // namespace
    112 
    113 GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
    114     const base::StringPiece& vendor_id,
    115     const base::StringPiece& product_id) {
    116   for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
    117     MappingData& item = AvailableMappings[i];
    118     if (vendor_id == item.vendor_id && product_id == item.product_id)
    119       return item.function;
    120   }
    121   return NULL;
    122 }
    123 
    124 }  // namespace content
    125