Skip to content

Commit

Permalink
gamepad: Deduplicate common Switch mapping functions
Browse files Browse the repository at this point in the history
The Gamepad backend defines platform-specific mapping functions to
reorder button/axis inputs to match the Standard Gamepad described in
the Gamepad API specification. Historically, these mappings are
platform-specific due to differences in how the platforms enumerate
button and axis inputs.

In many cases the platforms enumerate the inputs the same way, leading
to identical or near-identical mapping functions. This CL moves the
Switch gamepad mapping functions, which are identical on Windows,
macOS, and Linux/ChromeOS, to gamepad_standard_mappings.h.

R=mattreynolds@chromium.org

Bug: 1145647
Change-Id: I1c6e3ce32fd594d5d37957cc8eb0a5480068797f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2764084
Reviewed-by: Matt Reynolds <mattreynolds@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#869295}
  • Loading branch information
jobed-h authored and Chromium LUCI CQ committed Apr 5, 2021
1 parent e13efbe commit bf0785c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 105 deletions.
28 changes: 28 additions & 0 deletions device/gamepad/gamepad_standard_mappings.cc
Expand Up @@ -78,4 +78,32 @@ float RenormalizeAndClampAxis(float value, float min, float max) {
return value < -1.f ? -1.f : (value > 1.f ? 1.f : value);
}

void MapperSwitchPro(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons_length = SWITCH_PRO_BUTTON_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperSwitchJoyCon(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons_length = BUTTON_INDEX_COUNT;
mapped->axes_length = 2;
}

void MapperSwitchComposite(const Gamepad& input, Gamepad* mapped) {
// In composite mode, the inputs from two Joy-Cons are combined to form one
// virtual gamepad. Some buttons do not have equivalents in the Standard
// Gamepad and are exposed as extra buttons:
// * Capture button (Joy-Con L): BUTTON_INDEX_COUNT
// * SL (Joy-Con L): BUTTON_INDEX_COUNT + 1
// * SR (Joy-Con L): BUTTON_INDEX_COUNT + 2
// * SL (Joy-Con R): BUTTON_INDEX_COUNT + 3
// * SR (Joy-Con R): BUTTON_INDEX_COUNT + 4
constexpr size_t kSwitchCompositeExtraButtonCount = 5;
*mapped = input;
mapped->buttons_length =
BUTTON_INDEX_COUNT + kSwitchCompositeExtraButtonCount;
mapped->axes_length = AXIS_INDEX_COUNT;
}

} // namespace device
12 changes: 12 additions & 0 deletions device/gamepad/gamepad_standard_mappings.h
Expand Up @@ -85,6 +85,13 @@ enum CanonicalAxisIndex {
AXIS_INDEX_COUNT
};

// The Switch Pro controller has a Capture button that has no equivalent in the
// Standard Gamepad.
enum SwitchProButtons {
SWITCH_PRO_BUTTON_CAPTURE = BUTTON_INDEX_COUNT,
SWITCH_PRO_BUTTON_COUNT
};

// Common mapping functions
GamepadButton AxisToButton(float input);
GamepadButton AxisNegativeAsButton(float input);
Expand All @@ -94,6 +101,11 @@ GamepadButton NullButton();
void DpadFromAxis(Gamepad* mapped, float dir);
float RenormalizeAndClampAxis(float value, float min, float max);

// Gamepad common mapping functions
void MapperSwitchPro(const Gamepad& input, Gamepad* mapped);
void MapperSwitchJoyCon(const Gamepad& input, Gamepad* mapped);
void MapperSwitchComposite(const Gamepad& input, Gamepad* mapped);

} // namespace device

#endif // DEVICE_GAMEPAD_GAMEPAD_STANDARD_MAPPINGS_H_
35 changes: 0 additions & 35 deletions device/gamepad/gamepad_standard_mappings_linux.cc
Expand Up @@ -35,13 +35,6 @@ enum StadiaGamepadButtons {
STADIA_GAMEPAD_BUTTON_COUNT
};

// The Switch Pro controller has a Capture button that has no equivalent in the
// Standard Gamepad.
enum SwitchProButtons {
SWITCH_PRO_BUTTON_CAPTURE = BUTTON_INDEX_COUNT,
SWITCH_PRO_BUTTON_COUNT
};

void MapperXInputStyleGamepad(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[2]);
Expand Down Expand Up @@ -646,34 +639,6 @@ void MapperSteelSeriesStratusXLBt(const Gamepad& input, Gamepad* mapped) {
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperSwitchJoyCon(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons_length = BUTTON_INDEX_COUNT;
mapped->axes_length = 2;
}

void MapperSwitchPro(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons_length = SWITCH_PRO_BUTTON_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperSwitchComposite(const Gamepad& input, Gamepad* mapped) {
// In composite mode, the inputs from two Joy-Cons are combined to form one
// virtual gamepad. Some buttons do not have equivalents in the Standard
// Gamepad and are exposed as extra buttons:
// * Capture button (Joy-Con L): BUTTON_INDEX_COUNT
// * SL (Joy-Con L): BUTTON_INDEX_COUNT + 1
// * SR (Joy-Con L): BUTTON_INDEX_COUNT + 2
// * SL (Joy-Con R): BUTTON_INDEX_COUNT + 3
// * SR (Joy-Con R): BUTTON_INDEX_COUNT + 4
const size_t kSwitchCompositeExtraButtonCount = 5;
*mapped = input;
mapped->buttons_length =
BUTTON_INDEX_COUNT + kSwitchCompositeExtraButtonCount;
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperLogitechDInput(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
Expand Down
35 changes: 0 additions & 35 deletions device/gamepad/gamepad_standard_mappings_mac.mm
Expand Up @@ -26,13 +26,6 @@
STADIA_GAMEPAD_BUTTON_COUNT
};

// The Switch Pro controller has a Capture button that has no equivalent in the
// Standard Gamepad.
enum SwitchProButtons {
SWITCH_PRO_BUTTON_CAPTURE = BUTTON_INDEX_COUNT,
SWITCH_PRO_BUTTON_COUNT
};

void MapperXbox360Gamepad(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[2]);
Expand Down Expand Up @@ -589,34 +582,6 @@ void MapperBoomN64Psx(const Gamepad& input, Gamepad* mapped) {
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperSwitchJoyCon(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons_length = BUTTON_INDEX_COUNT;
mapped->axes_length = 2;
}

void MapperSwitchPro(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons_length = SWITCH_PRO_BUTTON_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperSwitchComposite(const Gamepad& input, Gamepad* mapped) {
// In composite mode, the inputs from two Joy-Cons are combined to form one
// virtual gamepad. Some buttons do not have equivalents in the Standard
// Gamepad and are exposed as extra buttons:
// * Capture button (Joy-Con L): BUTTON_INDEX_COUNT
// * SL (Joy-Con L): BUTTON_INDEX_COUNT + 1
// * SR (Joy-Con L): BUTTON_INDEX_COUNT + 2
// * SL (Joy-Con R): BUTTON_INDEX_COUNT + 3
// * SR (Joy-Con R): BUTTON_INDEX_COUNT + 4
const size_t kSwitchCompositeExtraButtonCount = 5;
*mapped = input;
mapped->buttons_length =
BUTTON_INDEX_COUNT + kSwitchCompositeExtraButtonCount;
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperXboxOneBluetooth(const Gamepad& input, Gamepad* mapped) {
*mapped = input;

Expand Down
35 changes: 0 additions & 35 deletions device/gamepad/gamepad_standard_mappings_win.cc
Expand Up @@ -26,13 +26,6 @@ enum StadiaGamepadButtons {
STADIA_GAMEPAD_BUTTON_COUNT
};

// The Switch Pro controller has a Capture button that has no equivalent in the
// Standard Gamepad.
enum SwitchProButtons {
SWITCH_PRO_BUTTON_CAPTURE = BUTTON_INDEX_COUNT,
SWITCH_PRO_BUTTON_COUNT
};

void MapperLogitechDInput(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
Expand Down Expand Up @@ -421,34 +414,6 @@ void MapperBoomN64Psx(const Gamepad& input, Gamepad* mapped) {
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperSwitchJoyCon(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons_length = BUTTON_INDEX_COUNT;
mapped->axes_length = 2;
}

void MapperSwitchPro(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons_length = SWITCH_PRO_BUTTON_COUNT;
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperSwitchComposite(const Gamepad& input, Gamepad* mapped) {
// In composite mode, the inputs from two Joy-Cons are combined to form one
// virtual gamepad. Some buttons do not have equivalents in the Standard
// Gamepad and are exposed as extra buttons:
// * Capture button (Joy-Con L): BUTTON_INDEX_COUNT
// * SL (Joy-Con L): BUTTON_INDEX_COUNT + 1
// * SR (Joy-Con L): BUTTON_INDEX_COUNT + 2
// * SL (Joy-Con R): BUTTON_INDEX_COUNT + 3
// * SR (Joy-Con R): BUTTON_INDEX_COUNT + 4
const size_t kSwitchCompositeExtraButtonCount = 5;
*mapped = input;
mapped->buttons_length =
BUTTON_INDEX_COUNT + kSwitchCompositeExtraButtonCount;
mapped->axes_length = AXIS_INDEX_COUNT;
}

void MapperSnakebyteIDroidCon(const Gamepad& input, Gamepad* mapped) {
*mapped = input;
mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
Expand Down

0 comments on commit bf0785c

Please sign in to comment.