internal/gamepad: implement gamepad vibrations for Xbox

Updates #2084
This commit is contained in:
Hajime Hoshi 2022-07-15 21:30:35 +09:00
parent a7beddbc94
commit d6547f12c6
2 changed files with 37 additions and 0 deletions

View File

@ -109,6 +109,13 @@ type _GameInputGamepadState struct {
rightThumbstickY float32 rightThumbstickY float32
} }
type _GameInputRumbleParams struct {
lowFrequency float32
highFrequency float32
leftTrigger float32
rightTrigger float32
}
func _GameInputCreate() (*_IGameInput, error) { func _GameInputCreate() (*_IGameInput, error) {
var gameInput *_IGameInput var gameInput *_IGameInput
r, _, _ := procGameInputCreate.Call(uintptr(unsafe.Pointer(&gameInput))) r, _, _ := procGameInputCreate.Call(uintptr(unsafe.Pointer(&gameInput)))
@ -250,3 +257,8 @@ func (i *_IGameInputReading) GetGamepadState() (_GameInputGamepadState, bool) {
func (i *_IGameInputReading) Release() { func (i *_IGameInputReading) Release() {
syscall.Syscall(i.vtbl.Release, 1, uintptr(unsafe.Pointer(i)), 0, 0) syscall.Syscall(i.vtbl.Release, 1, uintptr(unsafe.Pointer(i)), 0, 0)
} }
func (i *_IGameInputDevice) SetRumbleState(params *_GameInputRumbleParams, timestamp uint64) {
syscall.Syscall(i.vtbl.SetRumbleState, 3, uintptr(unsafe.Pointer(i)), uintptr(unsafe.Pointer(params)), uintptr(timestamp))
runtime.KeepAlive(params)
}

View File

@ -123,6 +123,9 @@ func (n *nativeGamepadsXbox) deviceCallback(callbackToken _GameInputCallbackToke
type nativeGamepadXbox struct { type nativeGamepadXbox struct {
gameInputDevice *_IGameInputDevice gameInputDevice *_IGameInputDevice
state _GameInputGamepadState state _GameInputGamepadState
vib bool
vibEnd time.Time
} }
func (n *nativeGamepadXbox) update(gamepads *gamepads) error { func (n *nativeGamepadXbox) update(gamepads *gamepads) error {
@ -139,6 +142,15 @@ func (n *nativeGamepadXbox) update(gamepads *gamepads) error {
return nil return nil
} }
n.state = state n.state = state
if n.vib && time.Now().Sub(n.vibEnd) >= 0 {
n.gameInputDevice.SetRumbleState(&_GameInputRumbleParams{
lowFrequency: 0,
highFrequency: 0,
}, 0)
n.vib = false
}
return nil return nil
} }
@ -216,4 +228,17 @@ func (n *nativeGamepadXbox) hatState(hat int) int {
} }
func (n *nativeGamepadXbox) vibrate(duration time.Duration, strongMagnitude float64, weakMagnitude float64) { func (n *nativeGamepadXbox) vibrate(duration time.Duration, strongMagnitude float64, weakMagnitude float64) {
if strongMagnitude <= 0 && weakMagnitude <= 0 {
n.gameInputDevice.SetRumbleState(&_GameInputRumbleParams{
lowFrequency: 0,
highFrequency: 0,
}, 0)
return
}
n.vib = true
n.vibEnd = time.Now().Add(duration)
n.gameInputDevice.SetRumbleState(&_GameInputRumbleParams{
lowFrequency: float32(strongMagnitude),
highFrequency: float32(weakMagnitude),
}, 0)
} }