From a659c330d2864c795f7848fd39f397f43e624bae Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 9 Apr 2022 14:42:14 +0900 Subject: [PATCH] internal/gamepad: bug fix: ignore a device that doesn't have an appropriate button --- internal/gamepad/api_linux.go | 10 ++++++++++ internal/gamepad/gamepad_linux.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/internal/gamepad/api_linux.go b/internal/gamepad/api_linux.go index e751b2182..6fd4344c3 100644 --- a/internal/gamepad/api_linux.go +++ b/internal/gamepad/api_linux.go @@ -30,6 +30,16 @@ const ( _ABS_CNT = _ABS_MAX + 1 _BTN_MISC = 0x100 + _BTN_0 = 0x100 + _BTN_1 = 0x101 + _BTN_2 = 0x102 + _BTN_3 = 0x103 + _BTN_4 = 0x104 + _BTN_5 = 0x105 + _BTN_6 = 0x106 + _BTN_7 = 0x107 + _BTN_8 = 0x108 + _BTN_9 = 0x109 _IOC_NONE = 0 _IOC_WRITE = 1 diff --git a/internal/gamepad/gamepad_linux.go b/internal/gamepad/gamepad_linux.go index 95f2cf558..1d6d882ee 100644 --- a/internal/gamepad/gamepad_linux.go +++ b/internal/gamepad/gamepad_linux.go @@ -181,13 +181,27 @@ func (*nativeGamepadsImpl) openGamepad(gamepads *gamepads, path string) (err err var axisCount int var buttonCount int var hatCount int + var necessaryButtonExist bool for code := _BTN_MISC; code < _KEY_CNT; code++ { if !isBitSet(keyBits, code) { continue } n.keyMap[code-_BTN_MISC] = buttonCount + if _BTN_0 <= code && code <= _BTN_9 { + necessaryButtonExist = true + } buttonCount++ } + + // If the device doesn't have any necessary bottons, this might not be a gamepad (#2039). + // Remove this. + if !necessaryButtonExist { + n.close() + gamepads.remove(func(gamepad *Gamepad) bool { + return gamepad == gp + }) + } + for code := 0; code < _ABS_CNT; code++ { n.absMap[code] = -1 if !isBitSet(absBits, code) {