Skip to content

Commit

Permalink
gamepad: add GamepadGUID and GamepadName functions
Browse files Browse the repository at this point in the history
  • Loading branch information
silbinarywolf committed Dec 28, 2019
1 parent ff2159f commit 0cb6a77
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
23 changes: 23 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ func IsMouseButtonPressed(mouseButton MouseButton) bool {
return uiDriver().Input().IsMouseButtonPressed(driver.MouseButton(mouseButton))
}

// GamepadGUID returns a string with the uuid.
//
// GamepadGUID is concurrent-safe.
//
// GamepadGUID always returns an empty string on browsers and mobiles.
func GamepadGUID(id int) string {
return uiDriver().Input().GamepadGUID(id)
}

// GamepadName returns a string with the name.
// This function may vary in how it returns descriptions for the same device across platforms
// for example the following drivers/platforms see a Xbox One controller as the following:
// - Windows: "Xbox Controller"
// - Chrome: "Xbox 360 Controller (XInput STANDARD GAMEPAD)"
// - Firefox: "xinput"
//
// GamepadName is concurrent-safe.
//
// GamepadName always returns an empty string on mobiles.
func GamepadName(id int) string {
return uiDriver().Input().GamepadName(id)
}

// GamepadIDs returns a slice indicating available gamepad IDs.
//
// GamepadIDs is concurrent-safe.
Expand Down
2 changes: 2 additions & 0 deletions internal/driver/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package driver

type Input interface {
CursorPosition() (x, y int)
GamepadGUID(id int) string
GamepadName(id int) string
GamepadAxis(id int, axis int) float64
GamepadAxisNum(id int) int
GamepadButtonNum(id int) int
Expand Down
3 changes: 1 addition & 2 deletions internal/glfw/glfw_notwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ func CreateWindow(width, height int, title string, monitor *Monitor, share *Wind
return theWindows.add(w), nil
}

func GetJoystickAxes(joy Joystick) []float32 {
return glfw.GetJoystickAxes(glfw.Joystick(joy))

}

func GetJoystickButtons(joy Joystick) []byte {
Expand Down
21 changes: 21 additions & 0 deletions internal/glfw/glfw_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,27 @@ func CreateWindow(width, height int, title string, monitor *Monitor, share *Wind
return theGLFWWindows.add(w), nil
}

func GetJoystickGUID(j Joystick) string {
return ""
}

func GetJoystickName(j Joystick) string {
ptr := glfwDLL.call("glfwGetJoystickName", uintptr(j))
panicError()
var backed [256]byte
as := backed[:0]
for {
b := *(*byte)(unsafe.Pointer(ptr))
ptr += unsafe.Sizeof(byte(0))
if b == 0 {
break
}
as = append(as, b)
}
r := string(as)
return r
}

func GetJoystickAxes(joy Joystick) []float32 {
var l int32
ptr := glfwDLL.call("glfwGetJoystickAxes", uintptr(joy), uintptr(unsafe.Pointer(&l)))
Expand Down
22 changes: 22 additions & 0 deletions internal/uidriver/glfw/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (

type gamePad struct {
valid bool
guid string
name string
axisNum int
axes [16]float64
buttonNum int
Expand Down Expand Up @@ -76,6 +78,24 @@ func (i *Input) GamepadIDs() []int {
return r
}

func (i *Input) GamepadGUID(id int) string {
i.ui.m.RLock()
defer i.ui.m.RUnlock()
if len(i.gamepads) <= id {
return ""
}
return i.gamepads[id].guid
}

func (i *Input) GamepadName(id int) string {
i.ui.m.RLock()
defer i.ui.m.RUnlock()
if len(i.gamepads) <= id {
return ""
}
return i.gamepads[id].name
}

func (i *Input) GamepadAxisNum(id int) int {
i.ui.m.RLock()
defer i.ui.m.RUnlock()
Expand Down Expand Up @@ -254,6 +274,8 @@ func (i *Input) update(window *glfw.Window, scale float64) {
continue
}
i.gamepads[id].valid = true
i.gamepads[id].guid = glfw.GetJoystickGUID(id)
i.gamepads[id].name = glfw.GetJoystickName(id)

axes32 := glfw.GetJoystickAxes(id)
i.gamepads[id].axisNum = len(axes32)
Expand Down
16 changes: 16 additions & 0 deletions internal/uidriver/js/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type pos struct {

type gamePad struct {
valid bool
name string
axisNum int
axes [16]float64
buttonNum int
Expand All @@ -55,6 +56,20 @@ func (i *Input) CursorPosition() (x, y int) {
return i.ui.adjustPosition(i.cursorX, i.cursorY)
}

func (i *Input) GamepadGUID(id int) string {
return ""
}

// GamepadName returns a string containing some information about the controller.
// A PS2 controller returned "810-3-USB Gamepad" on Firefox
// A Xbox 360 controller returned "xinput" on Firefox and "Xbox 360 Controller (XInput STANDARD GAMEPAD)" on Chrome
func (i *Input) GamepadName(id int) string {
if len(i.gamepads) <= id {
return ""
}
return i.gamepads[id].name
}

func (i *Input) GamepadIDs() []int {
if len(i.gamepads) == 0 {
return nil
Expand Down Expand Up @@ -231,6 +246,7 @@ func (i *Input) UpdateGamepads() {
continue
}
i.gamepads[id].valid = true
i.gamepads[id].name = gamepad.Get("id").String()

axes := gamepad.Get("axes")
axesNum := axes.Get("length").Int()
Expand Down
8 changes: 8 additions & 0 deletions internal/uidriver/mobile/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func (i *Input) GamepadIDs() []int {
return nil
}

func (i *Input) GamepadGUID(id int) string {
return ""
}

func (i *Input) GamepadName(id int) string {
return ""
}

func (i *Input) GamepadAxisNum(id int) int {
return 0
}
Expand Down

0 comments on commit 0cb6a77

Please sign in to comment.