blob: 3d7630ac51c5ebe058904280ca0adc23f91d4fb2 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include "base/macros.h"
#include "base/stl_util.h"
#include "device/gamepad/gamepad_standard_mappings.h"
namespace device {
namespace {
void MapperLogitechDInput(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[2];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[0];
mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
DpadFromAxis(mapped, input.axes[9]);
// The Logitech button (BUTTON_INDEX_META) is not accessible through the
// device's D-mode.
mapped->buttons_length = BUTTON_INDEX_COUNT - 1;
mapped->axes_length = AXIS_INDEX_COUNT;
}
void Mapper2Axes8Keys(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[2];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[0];
mapped->buttons[BUTTON_INDEX_DPAD_UP] = AxisNegativeAsButton(input.axes[1]);
mapped->buttons[BUTTON_INDEX_DPAD_DOWN] = AxisPositiveAsButton(input.axes[1]);
mapped->buttons[BUTTON_INDEX_DPAD_LEFT] = AxisNegativeAsButton(input.axes[0]);
mapped->buttons[BUTTON_INDEX_DPAD_RIGHT] =
AxisPositiveAsButton(input.axes[0]);
// Missing buttons
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = GamepadButton();
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = GamepadButton();
mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = GamepadButton();
mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = GamepadButton();
mapped->buttons[BUTTON_INDEX_META] = GamepadButton();
mapped->buttons_length = BUTTON_INDEX_COUNT - 1;
mapped->axes_length = 0;
}
void MapperDualshock4(const Gamepad& input, Gamepad* mapped) {
enum Dualshock4Buttons {
DUALSHOCK_BUTTON_TOUCHPAD = BUTTON_INDEX_COUNT,
DUALSHOCK_BUTTON_COUNT
};
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[2];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[0];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[4];
mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[5];
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[3]);
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[4]);
mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[8];
mapped->buttons[BUTTON_INDEX_START] = input.buttons[9];
mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[10];
mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[11];
mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
mapped->buttons[DUALSHOCK_BUTTON_TOUCHPAD] = input.buttons[13];
mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
DpadFromAxis(mapped, input.axes[9]);
mapped->buttons_length = DUALSHOCK_BUTTON_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}
void MapperIBuffalo(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[0];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[2];
mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[6];
mapped->buttons[BUTTON_INDEX_START] = input.buttons[7];
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = input.buttons[4];
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = input.buttons[5];
mapped->buttons[BUTTON_INDEX_DPAD_UP] = AxisNegativeAsButton(input.axes[1]);
mapped->buttons[BUTTON_INDEX_DPAD_DOWN] = AxisPositiveAsButton(input.axes[1]);
mapped->buttons[BUTTON_INDEX_DPAD_LEFT] = AxisNegativeAsButton(input.axes[0]);
mapped->buttons[BUTTON_INDEX_DPAD_RIGHT] =
AxisPositiveAsButton(input.axes[0]);
mapped->buttons_length = BUTTON_INDEX_COUNT - 1; /* no meta */
mapped->axes_length = 2;
}
void MapperOnLiveWireless(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[2]);
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[5]);
mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[10];
mapped->buttons[BUTTON_INDEX_START] = input.buttons[11];
mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[3];
mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[4];
DpadFromAxis(mapped, input.axes[9]);
mapped->buttons_length = BUTTON_INDEX_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}
void MapperADT1(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[4]);
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[3]);
mapped->buttons[BUTTON_INDEX_BACK_SELECT] = NullButton();
mapped->buttons[BUTTON_INDEX_START] = NullButton();
mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
DpadFromAxis(mapped, input.axes[9]);
mapped->buttons_length = BUTTON_INDEX_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}
void MapperNvShield(const Gamepad& input, Gamepad* mapped) {
enum ShieldButtons {
SHIELD_BUTTON_CIRCLE = BUTTON_INDEX_COUNT,
SHIELD_BUTTON_COUNT
};
*mapped = input;
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[4]);
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[3]);
mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[9];
mapped->buttons[BUTTON_INDEX_START] = input.buttons[11];
mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
mapped->buttons[BUTTON_INDEX_META] = input.buttons[2];
mapped->buttons[SHIELD_BUTTON_CIRCLE] = input.buttons[5];
mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
DpadFromAxis(mapped, input.axes[9]);
mapped->buttons_length = SHIELD_BUTTON_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}
void MapperNvShield2017(const Gamepad& input, Gamepad* mapped) {
enum Shield2017Buttons {
SHIELD2017_BUTTON_PLAYPAUSE = BUTTON_INDEX_COUNT,
SHIELD2017_BUTTON_COUNT
};
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[4]);
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[3]);
mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[8];
mapped->buttons[BUTTON_INDEX_START] = input.buttons[5];
mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
mapped->buttons[BUTTON_INDEX_META] = input.buttons[2];
mapped->buttons[SHIELD2017_BUTTON_PLAYPAUSE] = input.buttons[11];
mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
DpadFromAxis(mapped, input.axes[9]);
mapped->buttons_length = SHIELD2017_BUTTON_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}
void MapperOUYA(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[2];
mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[4];
mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[5];
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[2]);
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[5]);
mapped->buttons[BUTTON_INDEX_BACK_SELECT] = NullButton();
mapped->buttons[BUTTON_INDEX_START] = NullButton();
mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[6];
mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[7];
mapped->buttons[BUTTON_INDEX_DPAD_UP] = input.buttons[8];
mapped->buttons[BUTTON_INDEX_DPAD_DOWN] = input.buttons[9];
mapped->buttons[BUTTON_INDEX_DPAD_LEFT] = input.buttons[10];
mapped->buttons[BUTTON_INDEX_DPAD_RIGHT] = input.buttons[11];
mapped->buttons[BUTTON_INDEX_META] = input.buttons[15];
mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[3];
mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[4];
mapped->buttons_length = BUTTON_INDEX_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}
void MapperRazerServal(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[4]);
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[3]);
mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[10];
mapped->buttons[BUTTON_INDEX_START] = input.buttons[11];
mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
DpadFromAxis(mapped, input.axes[9]);
mapped->buttons_length = BUTTON_INDEX_COUNT - 1; /* no meta */
mapped->axes_length = AXIS_INDEX_COUNT;
}
void MapperMogaPro(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[4]);
mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[3]);
mapped->buttons[BUTTON_INDEX_BACK_SELECT] = NullButton();
mapped->buttons[BUTTON_INDEX_START] = input.buttons[11];
mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
DpadFromAxis(mapped, input.axes[9]);
mapped->buttons_length = BUTTON_INDEX_COUNT - 1; /* no meta */
mapped->axes_length = AXIS_INDEX_COUNT;
}
struct MappingData {
const uint16_t vendor_id;
const uint16_t product_id;
GamepadStandardMappingFunction function;
} AvailableMappings[] = {
// http://www.linux-usb.org/usb.ids
{0x0079, 0x0011, Mapper2Axes8Keys}, // 2Axes 8Keys Game Pad
{0x046d, 0xc216, MapperLogitechDInput}, // Logitech F310, D-mode
{0x046d, 0xc218, MapperLogitechDInput}, // Logitech F510, D-mode
{0x046d, 0xc219, MapperLogitechDInput}, // Logitech F710, D-mode
{0x054c, 0x05c4, MapperDualshock4}, // Playstation Dualshock 4
{0x054c, 0x09cc, MapperDualshock4}, // Dualshock 4 (PS4 Slim)
{0x054c, 0x0ba0, MapperDualshock4}, // Dualshock 4 USB receiver
{0x0583, 0x2060, MapperIBuffalo}, // iBuffalo Classic
{0x0955, 0x7210, MapperNvShield}, // Nvidia Shield gamepad (2015)
{0x0955, 0x7214, MapperNvShield2017}, // Nvidia Shield gamepad (2017)
{0x0b05, 0x4500, MapperADT1}, // Nexus Player Controller
{0x1532, 0x0900, MapperRazerServal}, // Razer Serval Controller
{0x18d1, 0x2c40, MapperADT1}, // ADT-1 Controller
{0x20d6, 0x6271, MapperMogaPro}, // Moga Pro Controller (HID mode)
{0x2378, 0x1008, MapperOnLiveWireless}, // OnLive Controller (Bluetooth)
{0x2378, 0x100a, MapperOnLiveWireless}, // OnLive Controller (Wired)
{0x2836, 0x0001, MapperOUYA}, // OUYA Controller
};
const size_t kAvailableMappingsLen = base::size(AvailableMappings);
} // namespace
GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
const uint16_t vendor_id,
const uint16_t product_id,
const uint16_t version_number,
GamepadBusType bus_type) {
for (size_t i = 0; i < kAvailableMappingsLen; ++i) {
MappingData& item = AvailableMappings[i];
if (vendor_id == item.vendor_id && product_id == item.product_id)
return item.function;
}
return nullptr;
}
} // namespace device