Skip to content

Commit 509204c

Browse files
committed
Bug 1593841 - Using Gamepad axis array index as its index in default remapper on Mac OS. r=baku
We notice for some unknown gamepads, their axes don't follow the kAxisUsageMin rule, so we should choose to use array index as the axis id. Differential Revision: https://phabricator.services.mozilla.com/D56058 --HG-- extra : moz-landing-system : lando
1 parent 959fb0e commit 509204c

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

dom/gamepad/GamepadRemapping.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,8 +1548,9 @@ class OUYARemapper final : public GamepadRemapper {
15481548
}
15491549
};
15501550

1551-
already_AddRefed<GamepadRemapper> GetGamepadRemapper(
1552-
const uint16_t aVendorId, const uint16_t aProductId) {
1551+
already_AddRefed<GamepadRemapper> GetGamepadRemapper(const uint16_t aVendorId,
1552+
const uint16_t aProductId,
1553+
bool& aUsingDefault) {
15531554
const std::vector<GamepadRemappingData> remappingRules = {
15541555
{GamepadId::kAsusTekProduct4500, new ADT1Remapper()},
15551556
{GamepadId::kDragonRiseProduct0011, new TwoAxesEightKeysRemapper()},
@@ -1580,11 +1581,13 @@ already_AddRefed<GamepadRemapper> GetGamepadRemapper(
15801581

15811582
for (uint32_t i = 0; i < remappingRules.size(); ++i) {
15821583
if (id == remappingRules[i].id) {
1584+
aUsingDefault = false;
15831585
return do_AddRef(remappingRules[i].remapping.get());
15841586
}
15851587
}
15861588

15871589
static RefPtr<GamepadRemapper> defaultRemapper = new DefaultRemapper();
1590+
aUsingDefault = true;
15881591
return do_AddRef(defaultRemapper.get());
15891592
}
15901593

dom/gamepad/GamepadRemapping.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ struct GamepadRemappingData {
102102
};
103103

104104
already_AddRefed<GamepadRemapper> GetGamepadRemapper(const uint16_t aVendorId,
105-
const uint16_t aProductId);
105+
const uint16_t aProductId,
106+
bool& aUsingDefault);
106107

107108
} // namespace dom
108109
} // namespace mozilla

dom/gamepad/cocoa/CocoaGamepad.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Gamepad {
9292
axes.Clear();
9393
mSuperIndex = -1;
9494
}
95-
void init(IOHIDDeviceRef device);
95+
void init(IOHIDDeviceRef device, bool defaultRemapper);
9696
void ReportChanged(uint8_t* report, CFIndex report_length);
9797
size_t WriteOutputReport(const std::vector<uint8_t>& aReport) const;
9898

@@ -119,12 +119,12 @@ class Gamepad {
119119
std::vector<uint8_t> mInputReport;
120120
};
121121

122-
void Gamepad::init(IOHIDDeviceRef device) {
122+
void Gamepad::init(IOHIDDeviceRef aDevice, bool aDefaultRemapper) {
123123
clear();
124-
mDevice = device;
124+
mDevice = aDevice;
125125

126126
CFArrayRef elements =
127-
IOHIDDeviceCopyMatchingElements(device, nullptr, kIOHIDOptionsTypeNone);
127+
IOHIDDeviceCopyMatchingElements(aDevice, nullptr, kIOHIDOptionsTypeNone);
128128
CFIndex n = CFArrayGetCount(elements);
129129
for (CFIndex i = 0; i < n; i++) {
130130
IOHIDElementRef element =
@@ -134,7 +134,8 @@ void Gamepad::init(IOHIDDeviceRef device) {
134134

135135
if (usagePage == kDesktopUsagePage && usage >= kAxisUsageMin &&
136136
usage <= kAxisUsageMax) {
137-
Axis axis = {static_cast<int>(usage - kAxisUsageMin),
137+
Axis axis = {aDefaultRemapper ? int(buttons.Length())
138+
: static_cast<int>(usage - kAxisUsageMin),
138139
element,
139140
usagePage,
140141
usage,
@@ -146,7 +147,8 @@ void Gamepad::init(IOHIDDeviceRef device) {
146147
IOHIDElementGetLogicalMax(element) -
147148
IOHIDElementGetLogicalMin(element) ==
148149
7) {
149-
Axis axis = {static_cast<int>(usage - kAxisUsageMin),
150+
Axis axis = {aDefaultRemapper ? int(buttons.Length())
151+
: static_cast<int>(usage - kAxisUsageMin),
150152
element,
151153
usagePage,
152154
usage,
@@ -273,7 +275,6 @@ void DarwinGamepadService::DeviceAdded(IOHIDDeviceRef device) {
273275
slot = mGamepads.size();
274276
mGamepads.push_back(Gamepad());
275277
}
276-
mGamepads[slot].init(device);
277278

278279
// Gather some identifying information
279280
CFNumberRef vendorIdRef =
@@ -291,8 +292,12 @@ void DarwinGamepadService::DeviceAdded(IOHIDDeviceRef device) {
291292
char buffer[256];
292293
sprintf(buffer, "%x-%x-%s", vendorId, productId, product_name);
293294

294-
RefPtr<GamepadRemapper> remapper = GetGamepadRemapper(vendorId, productId);
295+
bool defaultRemapper = false;
296+
RefPtr<GamepadRemapper> remapper =
297+
GetGamepadRemapper(vendorId, productId, defaultRemapper);
295298
MOZ_ASSERT(remapper);
299+
mGamepads[slot].init(device, defaultRemapper);
300+
296301
remapper->SetAxisCount(mGamepads[slot].numAxes());
297302
remapper->SetButtonCount(mGamepads[slot].numButtons());
298303

dom/gamepad/windows/WindowsGamepad.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,9 @@ bool WindowsGamepadService::GetRawGamepad(HANDLE handle) {
694694
axes.SetLength(kAxesLengthCap);
695695

696696
// Looking for the exisiting ramapping rule.
697-
RefPtr<GamepadRemapper> remapper =
698-
GetGamepadRemapper(rdi.hid.dwVendorId, rdi.hid.dwProductId);
697+
bool defaultRemapper = false;
698+
RefPtr<GamepadRemapper> remapper = GetGamepadRemapper(
699+
rdi.hid.dwVendorId, rdi.hid.dwProductId, defaultRemapper);
699700
MOZ_ASSERT(remapper);
700701

701702
for (size_t i = 0; i < count; i++) {

0 commit comments

Comments
 (0)