Skip to content

Commit

Permalink
internal/gamepad: give a window from another package instead of GetAc…
Browse files Browse the repository at this point in the history
…tiveWindow

GetActiveWindow doesn't work on Xbox.

Updates #2084
  • Loading branch information
hajimehoshi committed Jun 3, 2022
1 parent 1580e92 commit 2eb4ef1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
14 changes: 14 additions & 0 deletions internal/gamepad/gamepad.go
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion internal/gamepad/gamepad_windows.go
Expand Up @@ -113,6 +113,7 @@ type nativeGamepads struct {
enumDevicesCallback uintptr
enumObjectsCallback uintptr

nativeWindow windows.HWND
deviceChanged int32
err error
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions internal/ui/ui_glfw.go
Expand Up @@ -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"
Expand Down Expand Up @@ -921,6 +922,8 @@ func (u *userInterfaceImpl) init() error {
g.SetWindow(u.nativeWindow())
}

gamepad.SetNativeWindow(u.nativeWindow())

return nil
}

Expand Down

0 comments on commit 2eb4ef1

Please sign in to comment.