From a8576008f070a35b2b6d1c214b483d884d803a7c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 2 Feb 2022 00:32:29 +0900 Subject: [PATCH] internal/gamepads: refactoring: remove a struct member --- internal/gamepad/gamepad.go | 8 ++------ internal/gamepad/gamepad_darwin.go | 16 +++++++--------- internal/gamepad/gamepad_js.go | 12 +++++------- internal/gamepad/gamepad_windows.go | 24 ++++++++++++------------ 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/internal/gamepad/gamepad.go b/internal/gamepad/gamepad.go index 9b4e992e4..cc13e56da 100644 --- a/internal/gamepad/gamepad.go +++ b/internal/gamepad/gamepad.go @@ -47,10 +47,6 @@ type gamepads struct { var theGamepads gamepads -func init() { - theGamepads.nativeGamepads.gamepads = &theGamepads -} - // AppendGamepadIDs is concurrent-safe. func AppendGamepadIDs(ids []driver.GamepadID) []driver.GamepadID { return theGamepads.appendGamepadIDs(ids) @@ -83,13 +79,13 @@ func (g *gamepads) update() error { defer g.m.Unlock() if !g.inited { - if err := g.nativeGamepads.init(); err != nil { + if err := g.nativeGamepads.init(g); err != nil { return err } g.inited = true } - if err := g.nativeGamepads.update(); err != nil { + if err := g.nativeGamepads.update(g); err != nil { return err } diff --git a/internal/gamepad/gamepad_darwin.go b/internal/gamepad/gamepad_darwin.go index 5e206e0a9..8761f63d7 100644 --- a/internal/gamepad/gamepad_darwin.go +++ b/internal/gamepad/gamepad_darwin.go @@ -60,15 +60,13 @@ import ( import "C" type nativeGamepads struct { - gamepads *gamepads - hidManager C.IOHIDManagerRef devicesToAdd []C.IOHIDDeviceRef devicesToRemove []C.IOHIDDeviceRef devicesM sync.Mutex } -func (g *nativeGamepads) init() error { +func (g *nativeGamepads) init(gamepads *gamepads) error { var dicts []C.CFDictionaryRef page := C.kHIDPage_GenericDesktop @@ -149,15 +147,15 @@ func ebitenGamepadRemovalCallback(ctx unsafe.Pointer, res C.IOReturn, sender uns theGamepads.devicesToRemove = append(theGamepads.devicesToRemove, device) } -func (g *nativeGamepads) update() error { +func (g *nativeGamepads) update(gamepads *gamepads) error { theGamepads.devicesM.Lock() defer theGamepads.devicesM.Unlock() for _, device := range g.devicesToAdd { - g.addDevice(device) + g.addDevice(device, gamepads) } for _, device := range g.devicesToRemove { - g.gamepads.remove(func(g *Gamepad) bool { + gamepads.remove(func(g *Gamepad) bool { return g.device == device }) } @@ -166,8 +164,8 @@ func (g *nativeGamepads) update() error { return nil } -func (g *nativeGamepads) addDevice(device C.IOHIDDeviceRef) { - if g.gamepads.find(func(g *Gamepad) bool { +func (g *nativeGamepads) addDevice(device C.IOHIDDeviceRef, gamepads *gamepads) { + if gamepads.find(func(g *Gamepad) bool { return g.device == device }) != nil { return @@ -213,7 +211,7 @@ func (g *nativeGamepads) addDevice(device C.IOHIDDeviceRef) { elements := C.IOHIDDeviceCopyMatchingElements(device, 0, C.kIOHIDOptionsTypeNone) defer C.CFRelease(C.CFTypeRef(elements)) - gp := g.gamepads.add(name, sdlID) + gp := gamepads.add(name, sdlID) gp.device = device for i := C.CFIndex(0); i < C.CFArrayGetCount(elements); i++ { diff --git a/internal/gamepad/gamepad_js.go b/internal/gamepad/gamepad_js.go index a9809b7ee..4d56308ed 100644 --- a/internal/gamepad/gamepad_js.go +++ b/internal/gamepad/gamepad_js.go @@ -26,16 +26,14 @@ var ( ) type nativeGamepads struct { - gamepads *gamepads - indices map[int]struct{} } -func (g *nativeGamepads) init() error { +func (g *nativeGamepads) init(gamepads *gamepads) error { 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. defer func() { @@ -68,7 +66,7 @@ func (g *nativeGamepads) update() error { g.indices[index] = struct{}{} // 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 }) if gamepad == nil { @@ -79,7 +77,7 @@ func (g *nativeGamepads) update() error { var sdlID [16]byte copy(sdlID[:], []byte(name)) - gamepad = g.gamepads.add(name, hex.EncodeToString(sdlID[:])) + gamepad = gamepads.add(name, hex.EncodeToString(sdlID[:])) gamepad.index = index gamepad.mapping = gp.Get("mapping").String() } @@ -87,7 +85,7 @@ func (g *nativeGamepads) update() error { } // Remove an unused gamepads. - g.gamepads.remove(func(gamepad *Gamepad) bool { + gamepads.remove(func(gamepad *Gamepad) bool { _, ok := g.indices[gamepad.index] return !ok }) diff --git a/internal/gamepad/gamepad_windows.go b/internal/gamepad/gamepad_windows.go index 09118496b..26ca95c03 100644 --- a/internal/gamepad/gamepad_windows.go +++ b/internal/gamepad/gamepad_windows.go @@ -97,8 +97,6 @@ var xinputButtons = []uint16{ } type nativeGamepads struct { - gamepads *gamepads - dinput8 windows.Handle dinput8API *iDirectInput8W xinput windows.Handle @@ -130,7 +128,7 @@ type enumObjectsContext struct { 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. // TODO: Is there a 'system' version of LoadLibrary? if h, err := windows.LoadLibrary("dinput8.dll"); err == nil { @@ -184,7 +182,7 @@ func (g *nativeGamepads) init() error { } g.dinput8API = api - if err := g.detectConnection(); err != nil { + if err := g.detectConnection(gamepads); err != nil { return err } } @@ -220,12 +218,12 @@ func (g *nativeGamepads) xinputGetState(dwUserIndex uint32, pState *xinputState) return nil } -func (g *nativeGamepads) detectConnection() error { +func (g *nativeGamepads) detectConnection(gamepads *gamepads) error { if g.dinput8 != 0 { if g.enumDevicesCallback == 0 { 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 } if g.err != nil { @@ -236,7 +234,7 @@ func (g *nativeGamepads) detectConnection() error { const xuserMaxCount = 4 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 }) != nil { continue @@ -273,7 +271,7 @@ func (g *nativeGamepads) detectConnection() error { name = "XInput Drum Kit" } - gp := g.gamepads.add(name, sdlID) + gp := gamepads.add(name, sdlID) gp.xinputIndex = i } } @@ -281,11 +279,13 @@ func (g *nativeGamepads) detectConnection() error { } func (g *nativeGamepads) dinput8EnumDevicesCallback(lpddi *diDeviceInstanceW, pvRef unsafe.Pointer) uintptr { + gamepads := (*gamepads)(pvRef) + if g.err != nil { return _DIENUM_STOP } - if g.gamepads.find(func(g *Gamepad) bool { + if gamepads.find(func(g *Gamepad) bool { return g.dinputGUID == lpddi.guidInstance }) != nil { 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]) } - gp := g.gamepads.add(name, sdlID) + gp := gamepads.add(name, sdlID) gp.dinputDevice = device gp.dinputObjects = ctx.objects gp.dinputGUID = lpddi.guidInstance @@ -505,7 +505,7 @@ func (g *nativeGamepads) dinputDevice8EnumObjectsCallback(lpddoi *diDeviceObject return _DIENUM_CONTINUE } -func (g *nativeGamepads) update() error { +func (g *nativeGamepads) update(gamepads *gamepads) error { if g.err != nil { return g.err } @@ -521,7 +521,7 @@ func (g *nativeGamepads) update() error { } if atomic.LoadInt32(&g.deviceChanged) != 0 { - if err := g.detectConnection(); err != nil { + if err := g.detectConnection(gamepads); err != nil { g.err = err } atomic.StoreInt32(&g.deviceChanged, 0)