diff --git a/internal/gamepad/gamepad.go b/internal/gamepad/gamepad.go index ac3476fd5..87a533f47 100644 --- a/internal/gamepad/gamepad.go +++ b/internal/gamepad/gamepad.go @@ -60,6 +60,10 @@ func Get(id ID) *Gamepad { return theGamepads.get(id) } +func SetNativeWindow(nativeWindow uintptr) { + theGamepads.setNativeWindow(nativeWindow) +} + func (g *gamepads) appendGamepadIDs(ids []ID) []ID { g.m.Lock() defer g.m.Unlock() @@ -158,6 +162,16 @@ func (g *gamepads) remove(cond func(*Gamepad) bool) { } } +func (g *gamepads) setNativeWindow(nativeWindow uintptr) { + g.m.Lock() + defer g.m.Unlock() + + var n interface{} = &g.nativeGamepads + if n, ok := n.(interface{ setNativeWindow(uintptr) }); ok { + n.setNativeWindow(nativeWindow) + } +} + type Gamepad struct { name string sdlID string diff --git a/internal/gamepad/gamepad_windows.go b/internal/gamepad/gamepad_windows.go index 987a3da1d..e81f891c9 100644 --- a/internal/gamepad/gamepad_windows.go +++ b/internal/gamepad/gamepad_windows.go @@ -113,6 +113,7 @@ type nativeGamepads struct { enumDevicesCallback uintptr enumObjectsCallback uintptr + nativeWindow windows.HWND deviceChanged int32 err error } @@ -518,7 +519,8 @@ func (g *nativeGamepads) update(gamepads *gamepads) error { if g.wndProcCallback == 0 { g.wndProcCallback = windows.NewCallback(g.wndProc) } - h, err := _SetWindowLongPtrW(_GetActiveWindow(), _GWL_WNDPROC, g.wndProcCallback) + // Note that a Win32API GetActiveWindow doesn't work on Xbox. + h, err := _SetWindowLongPtrW(g.nativeWindow, _GWL_WNDPROC, g.wndProcCallback) if err != nil { return err } @@ -543,6 +545,10 @@ func (g *nativeGamepads) wndProc(hWnd uintptr, uMsg uint32, wParam, lParam uintp return _CallWindowProcW(g.origWndProc, hWnd, uMsg, wParam, lParam) } +func (g *nativeGamepads) setNativeWindow(nativeWindow uintptr) { + g.nativeWindow = windows.HWND(nativeWindow) +} + type nativeGamepad struct { dinputDevice *_IDirectInputDevice8W dinputObjects []dinputObject diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 7078e3cce..72f6af9c6 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -28,6 +28,7 @@ import ( "time" "github.com/hajimehoshi/ebiten/v2/internal/devicescale" + "github.com/hajimehoshi/ebiten/v2/internal/gamepad" "github.com/hajimehoshi/ebiten/v2/internal/glfw" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/hooks" @@ -921,6 +922,8 @@ func (u *userInterfaceImpl) init() error { g.SetWindow(u.nativeWindow()) } + gamepad.SetNativeWindow(u.nativeWindow()) + return nil }