diff --git a/examples/gamepad/main.go b/examples/gamepad/main.go index 2c57d38de..795737d73 100644 --- a/examples/gamepad/main.go +++ b/examples/gamepad/main.go @@ -163,6 +163,7 @@ func (g *Game) Draw(screen *ebiten.Image) { standard = " (Standard Layout)" } str += fmt.Sprintf("Gamepad (ID: %d, SDL ID: %s)%s:\n", id, ebiten.GamepadSDLID(id), standard) + str += fmt.Sprintf(" Name: %s\n", ebiten.GamepadName(id)) str += fmt.Sprintf(" Axes: %s\n", strings.Join(g.axes[id], ", ")) str += fmt.Sprintf(" Buttons: %s\n", strings.Join(g.pressedButtons[id], ", ")) if ebiten.IsStandardGamepadLayoutAvailable(id) { diff --git a/internal/gamepad/gamepad.go b/internal/gamepad/gamepad.go index b0cff3d72..95a57cdc4 100644 --- a/internal/gamepad/gamepad.go +++ b/internal/gamepad/gamepad.go @@ -163,6 +163,9 @@ func (g *Gamepad) update() { func (g *Gamepad) Name() string { // This is immutable and doesn't have to be protected by a mutex. + if name := gamepaddb.Name(g.sdlID); name != "" { + return name + } return g.name } diff --git a/internal/gamepaddb/gamepaddb.go b/internal/gamepaddb/gamepaddb.go index f6db4b9d8..8d7042562 100644 --- a/internal/gamepaddb/gamepaddb.go +++ b/internal/gamepaddb/gamepaddb.go @@ -126,6 +126,7 @@ type mapping struct { } var ( + gamepadNames = map[string]string{} gamepadButtonMappings = map[string]map[driver.StandardGamepadButton]*mapping{} gamepadAxisMappings = map[string]map[driver.StandardGamepadAxis]*mapping{} mappingsM sync.RWMutex @@ -203,6 +204,8 @@ func processLine(line string, platform platform) error { // There is no corresponding button in the Web standard gamepad layout. } + gamepadNames[id] = tokens[1] + return nil } @@ -378,6 +381,13 @@ type GamepadState interface { Hat(index int) int } +func Name(id string) string { + mappingsM.RLock() + defer mappingsM.RUnlock() + + return gamepadNames[id] +} + func AxisValue(id string, axis driver.StandardGamepadAxis, state GamepadState) float64 { mappingsM.RLock() defer mappingsM.RUnlock() diff --git a/internal/uidriver/glfw/gamepad_glfw.go b/internal/uidriver/glfw/gamepad_glfw.go index 96b59f1ae..b45d76e12 100644 --- a/internal/uidriver/glfw/gamepad_glfw.go +++ b/internal/uidriver/glfw/gamepad_glfw.go @@ -119,6 +119,9 @@ func (i *Input) GamepadName(id driver.GamepadID) string { if len(i.gamepads) <= int(id) { return "" } + if name := gamepaddb.Name(i.gamepads[id].guid); name != "" { + return name + } return i.gamepads[id].name } diff --git a/internal/uidriver/mobile/input.go b/internal/uidriver/mobile/input.go index 3ce82552e..32f87f79c 100644 --- a/internal/uidriver/mobile/input.go +++ b/internal/uidriver/mobile/input.go @@ -67,6 +67,9 @@ func (i *Input) GamepadName(id driver.GamepadID) string { if g.ID != id { continue } + if name := gamepaddb.Name(g.SDLID); name != "" { + return name + } return g.Name } return ""