From d08f57e6106eabf50a09f4f91bfca1b566d9dea1 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 3 Jul 2020 22:58:46 +0900 Subject: [PATCH] uidriver/glfw: Bug fix: Skip some special 'joysticks' Apparently, there are some special devices that are recognized as joysticks by GLFW, even though they are not. Such devices can have too many 'buttons'. Skip them as a tentative solution. Updates #1173 --- internal/uidriver/glfw/input.go | 35 ++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/internal/uidriver/glfw/input.go b/internal/uidriver/glfw/input.go index 3630fc1c1..beef9bf75 100644 --- a/internal/uidriver/glfw/input.go +++ b/internal/uidriver/glfw/input.go @@ -348,10 +348,26 @@ func (i *Input) update(window *glfw.Window, context driver.UIContext) { if !id.Present() { continue } + + buttons := id.GetButtons() + + // A gamepad can be detected even though there are not. Apparently, some special devices are + // recognized as gamepads by GLFW. In this case, the number of the 'buttons' can exceeds the + // maximum. Skip such devices as a tentative solution (#1173). + if len(buttons) > driver.GamepadButtonNum { + continue + } + i.gamepads[id].valid = true - // Note that GLFW's gamepad GUID follows SDL's GUID. - i.gamepads[id].guid = id.GetGUID() - i.gamepads[id].name = id.GetName() + + i.gamepads[id].buttonNum = len(buttons) + for b := 0; b < len(i.gamepads[id].buttonPressed); b++ { + if len(buttons) <= b { + i.gamepads[id].buttonPressed[b] = false + continue + } + i.gamepads[id].buttonPressed[b] = glfw.Action(buttons[b]) == glfw.Press + } axes32 := id.GetAxes() i.gamepads[id].axisNum = len(axes32) @@ -362,15 +378,10 @@ func (i *Input) update(window *glfw.Window, context driver.UIContext) { } i.gamepads[id].axes[a] = float64(axes32[a]) } - buttons := id.GetButtons() - i.gamepads[id].buttonNum = len(buttons) - for b := 0; b < len(i.gamepads[id].buttonPressed); b++ { - if len(buttons) <= b { - i.gamepads[id].buttonPressed[b] = false - continue - } - i.gamepads[id].buttonPressed[b] = glfw.Action(buttons[b]) == glfw.Press - } + + // Note that GLFW's gamepad GUID follows SDL's GUID. + i.gamepads[id].guid = id.GetGUID() + i.gamepads[id].name = id.GetName() } return nil })