diff --git a/examples/gamepad/main.go b/examples/gamepad/main.go index f68e25ff4..f692c77f7 100644 --- a/examples/gamepad/main.go +++ b/examples/gamepad/main.go @@ -83,7 +83,7 @@ func update(screen *ebiten.Image) error { str := "" if len(ids) > 0 { for _, id := range ids { - str += fmt.Sprintf("Gamepad (ID: %d):\n", id) + str += fmt.Sprintf("Gamepad (ID: %d, SDL ID: %s):\n", id, ebiten.GamepadSDLID(id)) str += fmt.Sprintf(" Axes: %s\n", strings.Join(axes[id], ", ")) str += fmt.Sprintf(" Buttons: %s\n", strings.Join(pressedButtons[id], ", ")) str += "\n" diff --git a/internal/uidriver/js/input.go b/internal/uidriver/js/input.go index 7be939567..edf5a82b2 100644 --- a/internal/uidriver/js/input.go +++ b/internal/uidriver/js/input.go @@ -17,6 +17,7 @@ package js import ( + "encoding/hex" "syscall/js" "unicode" @@ -58,9 +59,14 @@ func (i *Input) CursorPosition() (x, y int) { } func (i *Input) GamepadSDLID(id int) string { - // TODO: Implement this. See the implementation of SDL: - // https://github.com/spurious/SDL-mirror/blob/master/src/joystick/emscripten/SDL_sysjoystick.c - return "" + // This emulates the implementation of EMSCRIPTEN_JoystickGetDeviceGUID. + // https://hg.libsdl.org/SDL/file/bc90ce38f1e2/src/joystick/emscripten/SDL_sysjoystick.c#l385 + if len(i.gamepads) <= id { + return "" + } + var sdlid [16]byte + copy(sdlid[:], []byte(i.gamepads[id].name)) + return hex.EncodeToString(sdlid[:]) } // GamepadName returns a string containing some information about the controller.