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 })