internal/gamepads: refactoring: remove a struct member

This commit is contained in:
Hajime Hoshi 2022-02-02 00:32:29 +09:00
parent 96ddec1753
commit a8576008f0
4 changed files with 26 additions and 34 deletions

View File

@ -47,10 +47,6 @@ type gamepads struct {
var theGamepads gamepads var theGamepads gamepads
func init() {
theGamepads.nativeGamepads.gamepads = &theGamepads
}
// AppendGamepadIDs is concurrent-safe. // AppendGamepadIDs is concurrent-safe.
func AppendGamepadIDs(ids []driver.GamepadID) []driver.GamepadID { func AppendGamepadIDs(ids []driver.GamepadID) []driver.GamepadID {
return theGamepads.appendGamepadIDs(ids) return theGamepads.appendGamepadIDs(ids)
@ -83,13 +79,13 @@ func (g *gamepads) update() error {
defer g.m.Unlock() defer g.m.Unlock()
if !g.inited { if !g.inited {
if err := g.nativeGamepads.init(); err != nil { if err := g.nativeGamepads.init(g); err != nil {
return err return err
} }
g.inited = true g.inited = true
} }
if err := g.nativeGamepads.update(); err != nil { if err := g.nativeGamepads.update(g); err != nil {
return err return err
} }

View File

@ -60,15 +60,13 @@ import (
import "C" import "C"
type nativeGamepads struct { type nativeGamepads struct {
gamepads *gamepads
hidManager C.IOHIDManagerRef hidManager C.IOHIDManagerRef
devicesToAdd []C.IOHIDDeviceRef devicesToAdd []C.IOHIDDeviceRef
devicesToRemove []C.IOHIDDeviceRef devicesToRemove []C.IOHIDDeviceRef
devicesM sync.Mutex devicesM sync.Mutex
} }
func (g *nativeGamepads) init() error { func (g *nativeGamepads) init(gamepads *gamepads) error {
var dicts []C.CFDictionaryRef var dicts []C.CFDictionaryRef
page := C.kHIDPage_GenericDesktop page := C.kHIDPage_GenericDesktop
@ -149,15 +147,15 @@ func ebitenGamepadRemovalCallback(ctx unsafe.Pointer, res C.IOReturn, sender uns
theGamepads.devicesToRemove = append(theGamepads.devicesToRemove, device) theGamepads.devicesToRemove = append(theGamepads.devicesToRemove, device)
} }
func (g *nativeGamepads) update() error { func (g *nativeGamepads) update(gamepads *gamepads) error {
theGamepads.devicesM.Lock() theGamepads.devicesM.Lock()
defer theGamepads.devicesM.Unlock() defer theGamepads.devicesM.Unlock()
for _, device := range g.devicesToAdd { for _, device := range g.devicesToAdd {
g.addDevice(device) g.addDevice(device, gamepads)
} }
for _, device := range g.devicesToRemove { for _, device := range g.devicesToRemove {
g.gamepads.remove(func(g *Gamepad) bool { gamepads.remove(func(g *Gamepad) bool {
return g.device == device return g.device == device
}) })
} }
@ -166,8 +164,8 @@ func (g *nativeGamepads) update() error {
return nil return nil
} }
func (g *nativeGamepads) addDevice(device C.IOHIDDeviceRef) { func (g *nativeGamepads) addDevice(device C.IOHIDDeviceRef, gamepads *gamepads) {
if g.gamepads.find(func(g *Gamepad) bool { if gamepads.find(func(g *Gamepad) bool {
return g.device == device return g.device == device
}) != nil { }) != nil {
return return
@ -213,7 +211,7 @@ func (g *nativeGamepads) addDevice(device C.IOHIDDeviceRef) {
elements := C.IOHIDDeviceCopyMatchingElements(device, 0, C.kIOHIDOptionsTypeNone) elements := C.IOHIDDeviceCopyMatchingElements(device, 0, C.kIOHIDOptionsTypeNone)
defer C.CFRelease(C.CFTypeRef(elements)) defer C.CFRelease(C.CFTypeRef(elements))
gp := g.gamepads.add(name, sdlID) gp := gamepads.add(name, sdlID)
gp.device = device gp.device = device
for i := C.CFIndex(0); i < C.CFArrayGetCount(elements); i++ { for i := C.CFIndex(0); i < C.CFArrayGetCount(elements); i++ {

View File

@ -26,16 +26,14 @@ var (
) )
type nativeGamepads struct { type nativeGamepads struct {
gamepads *gamepads
indices map[int]struct{} indices map[int]struct{}
} }
func (g *nativeGamepads) init() error { func (g *nativeGamepads) init(gamepads *gamepads) error {
return nil return nil
} }
func (g *nativeGamepads) update() error { func (g *nativeGamepads) update(gamepads *gamepads) error {
// TODO: Use the gamepad events instead of navigator.getGamepads after go2cpp is removed. // TODO: Use the gamepad events instead of navigator.getGamepads after go2cpp is removed.
defer func() { defer func() {
@ -68,7 +66,7 @@ func (g *nativeGamepads) update() error {
g.indices[index] = struct{}{} g.indices[index] = struct{}{}
// The gamepad is not registered yet, register this. // The gamepad is not registered yet, register this.
gamepad := g.gamepads.find(func(gamepad *Gamepad) bool { gamepad := gamepads.find(func(gamepad *Gamepad) bool {
return index == gamepad.index return index == gamepad.index
}) })
if gamepad == nil { if gamepad == nil {
@ -79,7 +77,7 @@ func (g *nativeGamepads) update() error {
var sdlID [16]byte var sdlID [16]byte
copy(sdlID[:], []byte(name)) copy(sdlID[:], []byte(name))
gamepad = g.gamepads.add(name, hex.EncodeToString(sdlID[:])) gamepad = gamepads.add(name, hex.EncodeToString(sdlID[:]))
gamepad.index = index gamepad.index = index
gamepad.mapping = gp.Get("mapping").String() gamepad.mapping = gp.Get("mapping").String()
} }
@ -87,7 +85,7 @@ func (g *nativeGamepads) update() error {
} }
// Remove an unused gamepads. // Remove an unused gamepads.
g.gamepads.remove(func(gamepad *Gamepad) bool { gamepads.remove(func(gamepad *Gamepad) bool {
_, ok := g.indices[gamepad.index] _, ok := g.indices[gamepad.index]
return !ok return !ok
}) })

View File

@ -97,8 +97,6 @@ var xinputButtons = []uint16{
} }
type nativeGamepads struct { type nativeGamepads struct {
gamepads *gamepads
dinput8 windows.Handle dinput8 windows.Handle
dinput8API *iDirectInput8W dinput8API *iDirectInput8W
xinput windows.Handle xinput windows.Handle
@ -130,7 +128,7 @@ type enumObjectsContext struct {
povCount int povCount int
} }
func (g *nativeGamepads) init() error { func (g *nativeGamepads) init(gamepads *gamepads) error {
// As there is no guarantee that the DLL exists, NewLazySystemDLL is not available. // As there is no guarantee that the DLL exists, NewLazySystemDLL is not available.
// TODO: Is there a 'system' version of LoadLibrary? // TODO: Is there a 'system' version of LoadLibrary?
if h, err := windows.LoadLibrary("dinput8.dll"); err == nil { if h, err := windows.LoadLibrary("dinput8.dll"); err == nil {
@ -184,7 +182,7 @@ func (g *nativeGamepads) init() error {
} }
g.dinput8API = api g.dinput8API = api
if err := g.detectConnection(); err != nil { if err := g.detectConnection(gamepads); err != nil {
return err return err
} }
} }
@ -220,12 +218,12 @@ func (g *nativeGamepads) xinputGetState(dwUserIndex uint32, pState *xinputState)
return nil return nil
} }
func (g *nativeGamepads) detectConnection() error { func (g *nativeGamepads) detectConnection(gamepads *gamepads) error {
if g.dinput8 != 0 { if g.dinput8 != 0 {
if g.enumDevicesCallback == 0 { if g.enumDevicesCallback == 0 {
g.enumDevicesCallback = windows.NewCallback(g.dinput8EnumDevicesCallback) g.enumDevicesCallback = windows.NewCallback(g.dinput8EnumDevicesCallback)
} }
if err := g.dinput8API.EnumDevices(_DI8DEVCLASS_GAMECTRL, g.enumDevicesCallback, nil, _DIEDFL_ALLDEVICES); err != nil { if err := g.dinput8API.EnumDevices(_DI8DEVCLASS_GAMECTRL, g.enumDevicesCallback, unsafe.Pointer(gamepads), _DIEDFL_ALLDEVICES); err != nil {
return err return err
} }
if g.err != nil { if g.err != nil {
@ -236,7 +234,7 @@ func (g *nativeGamepads) detectConnection() error {
const xuserMaxCount = 4 const xuserMaxCount = 4
for i := 0; i < xuserMaxCount; i++ { for i := 0; i < xuserMaxCount; i++ {
if g.gamepads.find(func(g *Gamepad) bool { if gamepads.find(func(g *Gamepad) bool {
return g.dinputDevice == nil && g.xinputIndex == i return g.dinputDevice == nil && g.xinputIndex == i
}) != nil { }) != nil {
continue continue
@ -273,7 +271,7 @@ func (g *nativeGamepads) detectConnection() error {
name = "XInput Drum Kit" name = "XInput Drum Kit"
} }
gp := g.gamepads.add(name, sdlID) gp := gamepads.add(name, sdlID)
gp.xinputIndex = i gp.xinputIndex = i
} }
} }
@ -281,11 +279,13 @@ func (g *nativeGamepads) detectConnection() error {
} }
func (g *nativeGamepads) dinput8EnumDevicesCallback(lpddi *diDeviceInstanceW, pvRef unsafe.Pointer) uintptr { func (g *nativeGamepads) dinput8EnumDevicesCallback(lpddi *diDeviceInstanceW, pvRef unsafe.Pointer) uintptr {
gamepads := (*gamepads)(pvRef)
if g.err != nil { if g.err != nil {
return _DIENUM_STOP return _DIENUM_STOP
} }
if g.gamepads.find(func(g *Gamepad) bool { if gamepads.find(func(g *Gamepad) bool {
return g.dinputGUID == lpddi.guidInstance return g.dinputGUID == lpddi.guidInstance
}) != nil { }) != nil {
return _DIENUM_CONTINUE return _DIENUM_CONTINUE
@ -382,7 +382,7 @@ func (g *nativeGamepads) dinput8EnumDevicesCallback(lpddi *diDeviceInstanceW, pv
bs[0], bs[1], bs[2], bs[3], bs[4], bs[5], bs[6], bs[7], bs[8], bs[9], bs[10], bs[11]) bs[0], bs[1], bs[2], bs[3], bs[4], bs[5], bs[6], bs[7], bs[8], bs[9], bs[10], bs[11])
} }
gp := g.gamepads.add(name, sdlID) gp := gamepads.add(name, sdlID)
gp.dinputDevice = device gp.dinputDevice = device
gp.dinputObjects = ctx.objects gp.dinputObjects = ctx.objects
gp.dinputGUID = lpddi.guidInstance gp.dinputGUID = lpddi.guidInstance
@ -505,7 +505,7 @@ func (g *nativeGamepads) dinputDevice8EnumObjectsCallback(lpddoi *diDeviceObject
return _DIENUM_CONTINUE return _DIENUM_CONTINUE
} }
func (g *nativeGamepads) update() error { func (g *nativeGamepads) update(gamepads *gamepads) error {
if g.err != nil { if g.err != nil {
return g.err return g.err
} }
@ -521,7 +521,7 @@ func (g *nativeGamepads) update() error {
} }
if atomic.LoadInt32(&g.deviceChanged) != 0 { if atomic.LoadInt32(&g.deviceChanged) != 0 {
if err := g.detectConnection(); err != nil { if err := g.detectConnection(gamepads); err != nil {
g.err = err g.err = err
} }
atomic.StoreInt32(&g.deviceChanged, 0) atomic.StoreInt32(&g.deviceChanged, 0)