2022-01-22 20:20:54 +01:00
// Copyright 2022 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2022-02-05 10:30:17 +01:00
//go:build !ebitencbackend
// +build !ebitencbackend
2022-01-22 20:20:54 +01:00
package gamepad
import (
"errors"
"fmt"
"sort"
"strings"
"sync/atomic"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/windows"
)
type dinputObjectType int
const (
dinputObjectTypeAxis dinputObjectType = iota
dinputObjectTypeSlider
dinputObjectTypeButton
dinputObjectTypePOV
)
2022-02-14 20:07:11 +01:00
var dinputObjectDataFormats = [ ] _DIOBJECTDATAFORMAT {
{ & _GUID_XAxis , _DIJOFS_X , _DIDFT_AXIS | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , _DIDOI_ASPECTPOSITION } ,
{ & _GUID_YAxis , _DIJOFS_Y , _DIDFT_AXIS | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , _DIDOI_ASPECTPOSITION } ,
{ & _GUID_ZAxis , _DIJOFS_Z , _DIDFT_AXIS | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , _DIDOI_ASPECTPOSITION } ,
{ & _GUID_RxAxis , _DIJOFS_RX , _DIDFT_AXIS | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , _DIDOI_ASPECTPOSITION } ,
{ & _GUID_RyAxis , _DIJOFS_RY , _DIDFT_AXIS | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , _DIDOI_ASPECTPOSITION } ,
{ & _GUID_RzAxis , _DIJOFS_RZ , _DIDFT_AXIS | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , _DIDOI_ASPECTPOSITION } ,
{ & _GUID_Slider , _DIJOFS_SLIDER ( 0 ) , _DIDFT_AXIS | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , _DIDOI_ASPECTPOSITION } ,
{ & _GUID_Slider , _DIJOFS_SLIDER ( 1 ) , _DIDFT_AXIS | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , _DIDOI_ASPECTPOSITION } ,
{ & _GUID_POV , _DIJOFS_POV ( 0 ) , _DIDFT_POV | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ & _GUID_POV , _DIJOFS_POV ( 1 ) , _DIDFT_POV | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ & _GUID_POV , _DIJOFS_POV ( 2 ) , _DIDFT_POV | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ & _GUID_POV , _DIJOFS_POV ( 3 ) , _DIDFT_POV | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 0 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 1 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 2 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 3 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 4 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 5 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 6 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 7 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 8 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 9 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 10 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 11 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 12 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 13 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 14 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 15 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 16 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 17 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 18 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 19 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 20 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 21 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 22 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 23 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 24 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 25 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 26 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 27 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 28 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 29 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 30 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
{ nil , _DIJOFS_BUTTON ( 31 ) , _DIDFT_BUTTON | _DIDFT_OPTIONAL | _DIDFT_ANYINSTANCE , 0 } ,
2022-01-22 20:20:54 +01:00
}
var xinputButtons = [ ] uint16 {
_XINPUT_GAMEPAD_A ,
_XINPUT_GAMEPAD_B ,
_XINPUT_GAMEPAD_X ,
_XINPUT_GAMEPAD_Y ,
_XINPUT_GAMEPAD_LEFT_SHOULDER ,
_XINPUT_GAMEPAD_RIGHT_SHOULDER ,
_XINPUT_GAMEPAD_BACK ,
_XINPUT_GAMEPAD_START ,
_XINPUT_GAMEPAD_LEFT_THUMB ,
_XINPUT_GAMEPAD_RIGHT_THUMB ,
}
type nativeGamepads struct {
dinput8 windows . Handle
2022-06-03 09:59:44 +02:00
dinput8API * _IDirectInput8W
2022-01-22 20:20:54 +01:00
xinput windows . Handle
procDirectInput8Create uintptr
procXInputGetCapabilities uintptr
procXInputGetState uintptr
origWndProc uintptr
wndProcCallback uintptr
enumDevicesCallback uintptr
enumObjectsCallback uintptr
2022-06-03 11:42:35 +02:00
nativeWindow windows . HWND
2022-01-22 20:20:54 +01:00
deviceChanged int32
err error
}
type dinputObject struct {
objectType dinputObjectType
index int
}
type enumObjectsContext struct {
2022-06-03 09:59:44 +02:00
device * _IDirectInputDevice8W
2022-01-22 20:20:54 +01:00
objects [ ] dinputObject
axisCount int
sliderCount int
buttonCount int
povCount int
}
2022-02-01 16:32:29 +01:00
func ( g * nativeGamepads ) init ( gamepads * gamepads ) error {
2022-01-22 20:20:54 +01:00
// 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 {
g . dinput8 = h
p , err := windows . GetProcAddress ( h , "DirectInput8Create" )
if err != nil {
return err
}
g . procDirectInput8Create = p
}
// TODO: Loading xinput1_4.dll or xinput9_1_0.dll should be enough.
// See https://source.chromium.org/chromium/chromium/src/+/main:device/gamepad/xinput_data_fetcher_win.cc;l=75-84;drc=643cdf61903e99f27c3d80daee67e217e9d280e0
for _ , dll := range [ ] string {
"xinput1_4.dll" ,
"xinput1_3.dll" ,
"xinput9_1_0.dll" ,
"xinput1_2.dll" ,
"xinput1_1.dll" ,
} {
if h , err := windows . LoadLibrary ( dll ) ; err == nil {
g . xinput = h
{
p , err := windows . GetProcAddress ( h , "XInputGetCapabilities" )
if err != nil {
return err
}
g . procXInputGetCapabilities = p
}
{
p , err := windows . GetProcAddress ( h , "XInputGetState" )
if err != nil {
return err
}
g . procXInputGetState = p
}
break
}
}
if g . dinput8 != 0 {
2022-06-03 09:59:44 +02:00
m , err := _GetModuleHandleW ( )
2022-01-22 20:20:54 +01:00
if err != nil {
return err
}
2022-06-03 09:59:44 +02:00
var api * _IDirectInput8W
2022-02-14 20:07:11 +01:00
if err := g . directInput8Create ( m , _DIRECTINPUT_VERSION , & _IID_IDirectInput8W , & api , nil ) ; err != nil {
2022-01-22 20:20:54 +01:00
return err
}
g . dinput8API = api
2022-02-01 16:32:29 +01:00
if err := g . detectConnection ( gamepads ) ; err != nil {
2022-01-22 20:20:54 +01:00
return err
}
}
return nil
}
2022-06-03 09:59:44 +02:00
func ( g * nativeGamepads ) directInput8Create ( hinst uintptr , dwVersion uint32 , riidltf * windows . GUID , ppvOut * * _IDirectInput8W , punkOuter unsafe . Pointer ) error {
2022-01-22 20:20:54 +01:00
r , _ , _ := syscall . Syscall6 ( g . procDirectInput8Create , 5 ,
2022-02-11 19:49:09 +01:00
hinst , uintptr ( dwVersion ) , uintptr ( unsafe . Pointer ( riidltf ) ) , uintptr ( unsafe . Pointer ( ppvOut ) ) , uintptr ( punkOuter ) ,
2022-01-22 20:20:54 +01:00
0 )
2022-06-03 09:52:42 +02:00
if uint32 ( r ) != _DI_OK {
return fmt . Errorf ( "gamepad: DirectInput8Create failed: %w" , directInputError ( r ) )
2022-01-22 20:20:54 +01:00
}
return nil
}
2022-06-03 09:59:44 +02:00
func ( g * nativeGamepads ) xinputGetCapabilities ( dwUserIndex uint32 , dwFlags uint32 , pCapabilities * _XINPUT_CAPABILITIES ) error {
2022-06-03 09:52:42 +02:00
// XInputGetCapabilities doesn't call SetLastError and returns an error code directly.
2022-01-22 20:20:54 +01:00
r , _ , _ := syscall . Syscall ( g . procXInputGetCapabilities , 3 ,
uintptr ( dwUserIndex ) , uintptr ( dwFlags ) , uintptr ( unsafe . Pointer ( pCapabilities ) ) )
2022-06-03 09:52:42 +02:00
if e := syscall . Errno ( uint32 ( r ) ) ; e != windows . ERROR_SUCCESS {
2022-01-22 20:20:54 +01:00
return fmt . Errorf ( "gamepad: XInputGetCapabilities failed: %w" , e )
}
return nil
}
2022-06-03 09:59:44 +02:00
func ( g * nativeGamepads ) xinputGetState ( dwUserIndex uint32 , pState * _XINPUT_STATE ) error {
2022-06-03 09:52:42 +02:00
// XInputGetState doesn't call SetLastError and returns an error code directly.
2022-01-22 20:20:54 +01:00
r , _ , _ := syscall . Syscall ( g . procXInputGetState , 2 ,
uintptr ( dwUserIndex ) , uintptr ( unsafe . Pointer ( pState ) ) , 0 )
2022-06-03 09:52:42 +02:00
if e := syscall . Errno ( uint32 ( r ) ) ; e != windows . ERROR_SUCCESS {
2022-01-22 20:20:54 +01:00
return fmt . Errorf ( "gamepad: XInputGetCapabilities failed: %w" , e )
}
return nil
}
2022-02-01 16:32:29 +01:00
func ( g * nativeGamepads ) detectConnection ( gamepads * gamepads ) error {
2022-01-22 20:20:54 +01:00
if g . dinput8 != 0 {
if g . enumDevicesCallback == 0 {
g . enumDevicesCallback = windows . NewCallback ( g . dinput8EnumDevicesCallback )
}
2022-02-01 16:32:29 +01:00
if err := g . dinput8API . EnumDevices ( _DI8DEVCLASS_GAMECTRL , g . enumDevicesCallback , unsafe . Pointer ( gamepads ) , _DIEDFL_ALLDEVICES ) ; err != nil {
2022-01-22 20:20:54 +01:00
return err
}
if g . err != nil {
return g . err
}
}
if g . xinput != 0 {
const xuserMaxCount = 4
for i := 0 ; i < xuserMaxCount ; i ++ {
2022-02-01 16:32:29 +01:00
if gamepads . find ( func ( g * Gamepad ) bool {
2022-01-22 20:20:54 +01:00
return g . dinputDevice == nil && g . xinputIndex == i
} ) != nil {
continue
}
2022-06-03 09:59:44 +02:00
var xic _XINPUT_CAPABILITIES
2022-01-22 20:20:54 +01:00
if err := g . xinputGetCapabilities ( uint32 ( i ) , 0 , & xic ) ; err != nil {
if ! errors . Is ( err , windows . ERROR_DEVICE_NOT_CONNECTED ) {
return err
}
continue
}
sdlID := fmt . Sprintf ( "78696e707574%02x000000000000000000" , xic . subType & 0xff )
name := "Unknown XInput Device"
switch xic . subType {
case _XINPUT_DEVSUBTYPE_GAMEPAD :
if xic . flags & _XINPUT_CAPS_WIRELESS != 0 {
name = "Wireless Xbox Controller"
} else {
name = "Xbox Controller"
}
case _XINPUT_DEVSUBTYPE_WHEEL :
name = "XInput Wheel"
case _XINPUT_DEVSUBTYPE_ARCADE_STICK :
name = "XInput Arcade Stick"
case _XINPUT_DEVSUBTYPE_FLIGHT_STICK :
name = "XInput Flight Stick"
case _XINPUT_DEVSUBTYPE_DANCE_PAD :
name = "XInput Dance Pad"
case _XINPUT_DEVSUBTYPE_GUITAR :
name = "XInput Guitar"
case _XINPUT_DEVSUBTYPE_DRUM_KIT :
name = "XInput Drum Kit"
}
2022-02-01 16:32:29 +01:00
gp := gamepads . add ( name , sdlID )
2022-01-22 20:20:54 +01:00
gp . xinputIndex = i
}
}
return nil
}
2022-02-14 20:07:11 +01:00
func ( g * nativeGamepads ) dinput8EnumDevicesCallback ( lpddi * _DIDEVICEINSTANCEW , pvRef unsafe . Pointer ) uintptr {
2022-02-01 16:32:29 +01:00
gamepads := ( * gamepads ) ( pvRef )
2022-01-22 20:20:54 +01:00
if g . err != nil {
return _DIENUM_STOP
}
2022-02-01 16:32:29 +01:00
if gamepads . find ( func ( g * Gamepad ) bool {
2022-01-22 20:20:54 +01:00
return g . dinputGUID == lpddi . guidInstance
} ) != nil {
return _DIENUM_CONTINUE
}
s , err := supportsXInput ( lpddi . guidProduct )
if err != nil {
g . err = err
return _DIENUM_STOP
}
if s {
return _DIENUM_CONTINUE
}
2022-06-03 09:59:44 +02:00
var device * _IDirectInputDevice8W
2022-01-22 20:20:54 +01:00
if err := g . dinput8API . CreateDevice ( & lpddi . guidInstance , & device , nil ) ; err != nil {
g . err = err
return _DIENUM_STOP
}
2022-02-14 20:07:11 +01:00
dataFormat := _DIDATAFORMAT {
dwSize : uint32 ( unsafe . Sizeof ( _DIDATAFORMAT { } ) ) ,
dwObjSize : uint32 ( unsafe . Sizeof ( _DIOBJECTDATAFORMAT { } ) ) ,
2022-01-22 20:20:54 +01:00
dwFlags : _DIDFT_ABSAXIS ,
2022-02-14 20:07:11 +01:00
dwDataSize : uint32 ( unsafe . Sizeof ( _DIJOYSTATE { } ) ) ,
2022-01-22 20:20:54 +01:00
dwNumObjs : uint32 ( len ( dinputObjectDataFormats ) ) ,
rgodf : & dinputObjectDataFormats [ 0 ] ,
}
if err := device . SetDataFormat ( & dataFormat ) ; err != nil {
g . err = err
device . Release ( )
return _DIENUM_STOP
}
2022-02-14 20:07:11 +01:00
dc := _DIDEVCAPS {
dwSize : uint32 ( unsafe . Sizeof ( _DIDEVCAPS { } ) ) ,
2022-01-22 20:20:54 +01:00
}
if err := device . GetCapabilities ( & dc ) ; err != nil {
g . err = err
device . Release ( )
return _DIENUM_STOP
}
2022-02-14 20:07:11 +01:00
dipd := _DIPROPDWORD {
diph : _DIPROPHEADER {
dwSize : uint32 ( unsafe . Sizeof ( _DIPROPDWORD { } ) ) ,
dwHeaderSize : uint32 ( unsafe . Sizeof ( _DIPROPHEADER { } ) ) ,
2022-01-22 20:20:54 +01:00
dwHow : _DIPH_DEVICE ,
} ,
dwData : _DIPROPAXISMODE_ABS ,
}
if err := device . SetProperty ( _DIPROP_AXISMODE , & dipd . diph ) ; err != nil {
g . err = err
device . Release ( )
return _DIENUM_STOP
}
ctx := enumObjectsContext {
device : device ,
}
if g . enumObjectsCallback == 0 {
g . enumObjectsCallback = windows . NewCallback ( g . dinputDevice8EnumObjectsCallback )
}
if err := device . EnumObjects ( g . enumObjectsCallback , unsafe . Pointer ( & ctx ) , _DIDFT_AXIS | _DIDFT_BUTTON | _DIDFT_POV ) ; err != nil {
g . err = err
device . Release ( )
return _DIENUM_STOP
}
sort . Slice ( ctx . objects , func ( i , j int ) bool {
if ctx . objects [ i ] . objectType != ctx . objects [ j ] . objectType {
return ctx . objects [ i ] . objectType < ctx . objects [ j ] . objectType
}
return ctx . objects [ i ] . index < ctx . objects [ j ] . index
} )
name := windows . UTF16ToString ( lpddi . tszInstanceName [ : ] )
var sdlID string
if string ( lpddi . guidProduct . Data4 [ 2 : 8 ] ) == "PIDVID" {
// This seems different from the current SDL implementation.
// Probably guidProduct includes the vendor and the product information, but this works.
// From the game controller database, the 'version' part seems always 0.
sdlID = fmt . Sprintf ( "03000000%02x%02x0000%02x%02x000000000000" ,
byte ( lpddi . guidProduct . Data1 ) ,
byte ( lpddi . guidProduct . Data1 >> 8 ) ,
byte ( lpddi . guidProduct . Data1 >> 16 ) ,
byte ( lpddi . guidProduct . Data1 >> 24 ) )
} else {
bs := [ ] byte ( name )
if len ( bs ) < 12 {
bs = append ( bs , make ( [ ] byte , 12 - len ( bs ) ) ... )
}
sdlID = fmt . Sprintf ( "05000000%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" ,
bs [ 0 ] , bs [ 1 ] , bs [ 2 ] , bs [ 3 ] , bs [ 4 ] , bs [ 5 ] , bs [ 6 ] , bs [ 7 ] , bs [ 8 ] , bs [ 9 ] , bs [ 10 ] , bs [ 11 ] )
}
2022-02-01 16:32:29 +01:00
gp := gamepads . add ( name , sdlID )
2022-01-22 20:20:54 +01:00
gp . dinputDevice = device
gp . dinputObjects = ctx . objects
gp . dinputGUID = lpddi . guidInstance
gp . dinputAxes = make ( [ ] float64 , ctx . axisCount + ctx . sliderCount )
gp . dinputButtons = make ( [ ] bool , ctx . buttonCount )
gp . dinputHats = make ( [ ] int , ctx . povCount )
return _DIENUM_CONTINUE
}
func supportsXInput ( guid windows . GUID ) ( bool , error ) {
var count uint32
2022-06-03 09:59:44 +02:00
if r , err := _GetRawInputDeviceList ( nil , & count ) ; err != nil {
2022-01-22 20:20:54 +01:00
return false , err
} else if r != 0 {
return false , nil
}
2022-02-14 20:07:11 +01:00
ridl := make ( [ ] _RAWINPUTDEVICELIST , count )
2022-06-03 09:59:44 +02:00
if _ , err := _GetRawInputDeviceList ( & ridl [ 0 ] , & count ) ; err != nil {
2022-01-22 20:20:54 +01:00
return false , err
}
for i := 0 ; i < int ( count ) ; i ++ {
if ridl [ i ] . dwType != _RIM_TYPEHID {
continue
}
2022-02-14 20:07:11 +01:00
rdi := _RID_DEVICE_INFO {
cbSize : uint32 ( unsafe . Sizeof ( _RID_DEVICE_INFO { } ) ) ,
2022-01-22 20:20:54 +01:00
}
size := uint32 ( unsafe . Sizeof ( rdi ) )
2022-06-03 09:59:44 +02:00
if _ , err := _GetRawInputDeviceInfoW ( ridl [ i ] . hDevice , _RIDI_DEVICEINFO , unsafe . Pointer ( & rdi ) , & size ) ; err != nil {
2022-01-22 20:20:54 +01:00
return false , err
}
if uint32 ( rdi . hid . dwVendorId ) | ( uint32 ( rdi . hid . dwProductId ) << 16 ) != guid . Data1 {
continue
}
var name [ 256 ] uint16
size = uint32 ( unsafe . Sizeof ( name ) )
2022-06-03 09:59:44 +02:00
if _ , err := _GetRawInputDeviceInfoW ( ridl [ i ] . hDevice , _RIDI_DEVICENAME , unsafe . Pointer ( & name [ 0 ] ) , & size ) ; err != nil {
2022-01-22 20:20:54 +01:00
return false , err
}
if strings . Contains ( windows . UTF16ToString ( name [ : ] ) , "IG_" ) {
return true , nil
}
}
return false , nil
}
2022-02-14 20:07:11 +01:00
func ( g * nativeGamepads ) dinputDevice8EnumObjectsCallback ( lpddoi * _DIDEVICEOBJECTINSTANCEW , pvRef unsafe . Pointer ) uintptr {
2022-01-22 20:20:54 +01:00
ctx := ( * enumObjectsContext ) ( pvRef )
switch {
2022-02-14 20:07:11 +01:00
case _DIDFT_GETTYPE ( lpddoi . dwType ) & _DIDFT_AXIS != 0 :
2022-01-22 20:20:54 +01:00
var index int
switch lpddoi . guidType {
2022-02-14 20:07:11 +01:00
case _GUID_Slider :
2022-01-22 20:20:54 +01:00
index = ctx . sliderCount
2022-02-14 20:07:11 +01:00
case _GUID_XAxis :
2022-01-22 20:20:54 +01:00
index = 0
2022-02-14 20:07:11 +01:00
case _GUID_YAxis :
2022-01-22 20:20:54 +01:00
index = 1
2022-02-14 20:07:11 +01:00
case _GUID_ZAxis :
2022-01-22 20:20:54 +01:00
index = 2
2022-02-14 20:07:11 +01:00
case _GUID_RxAxis :
2022-01-22 20:20:54 +01:00
index = 3
2022-02-14 20:07:11 +01:00
case _GUID_RyAxis :
2022-01-22 20:20:54 +01:00
index = 4
2022-02-14 20:07:11 +01:00
case _GUID_RzAxis :
2022-01-22 20:20:54 +01:00
index = 5
default :
return _DIENUM_CONTINUE
}
2022-02-14 20:07:11 +01:00
dipr := _DIPROPRANGE {
diph : _DIPROPHEADER {
dwSize : uint32 ( unsafe . Sizeof ( _DIPROPRANGE { } ) ) ,
dwHeaderSize : uint32 ( unsafe . Sizeof ( _DIPROPHEADER { } ) ) ,
2022-01-22 20:20:54 +01:00
dwObj : lpddoi . dwType ,
dwHow : _DIPH_BYID ,
} ,
lMin : - 32768 ,
lMax : 32767 ,
}
if err := ctx . device . SetProperty ( _DIPROP_RANGE , & dipr . diph ) ; err != nil {
return _DIENUM_CONTINUE
}
var objectType dinputObjectType
2022-02-14 20:07:11 +01:00
if lpddoi . guidType == _GUID_Slider {
2022-01-22 20:20:54 +01:00
objectType = dinputObjectTypeSlider
ctx . sliderCount ++
} else {
objectType = dinputObjectTypeAxis
ctx . axisCount ++
}
ctx . objects = append ( ctx . objects , dinputObject {
objectType : objectType ,
index : index ,
} )
2022-02-14 20:07:11 +01:00
case _DIDFT_GETTYPE ( lpddoi . dwType ) & _DIDFT_BUTTON != 0 :
2022-01-22 20:20:54 +01:00
ctx . objects = append ( ctx . objects , dinputObject {
objectType : dinputObjectTypeButton ,
index : ctx . buttonCount ,
} )
ctx . buttonCount ++
2022-02-14 20:07:11 +01:00
case _DIDFT_GETTYPE ( lpddoi . dwType ) & _DIDFT_POV != 0 :
2022-01-22 20:20:54 +01:00
ctx . objects = append ( ctx . objects , dinputObject {
objectType : dinputObjectTypePOV ,
index : ctx . povCount ,
} )
ctx . povCount ++
}
return _DIENUM_CONTINUE
}
2022-02-01 16:32:29 +01:00
func ( g * nativeGamepads ) update ( gamepads * gamepads ) error {
2022-01-22 20:20:54 +01:00
if g . err != nil {
return g . err
}
if g . origWndProc == 0 {
if g . wndProcCallback == 0 {
g . wndProcCallback = windows . NewCallback ( g . wndProc )
}
2022-06-03 11:42:35 +02:00
// Note that a Win32API GetActiveWindow doesn't work on Xbox.
h , err := _SetWindowLongPtrW ( g . nativeWindow , _GWL_WNDPROC , g . wndProcCallback )
2022-01-22 20:20:54 +01:00
if err != nil {
return err
}
g . origWndProc = h
}
if atomic . LoadInt32 ( & g . deviceChanged ) != 0 {
2022-02-01 16:32:29 +01:00
if err := g . detectConnection ( gamepads ) ; err != nil {
2022-01-22 20:20:54 +01:00
g . err = err
}
atomic . StoreInt32 ( & g . deviceChanged , 0 )
}
return nil
}
func ( g * nativeGamepads ) wndProc ( hWnd uintptr , uMsg uint32 , wParam , lParam uintptr ) uintptr {
switch uMsg {
case _WM_DEVICECHANGE :
atomic . StoreInt32 ( & g . deviceChanged , 1 )
}
2022-06-03 09:59:44 +02:00
return _CallWindowProcW ( g . origWndProc , hWnd , uMsg , wParam , lParam )
2022-01-22 20:20:54 +01:00
}
2022-06-03 11:42:35 +02:00
func ( g * nativeGamepads ) setNativeWindow ( nativeWindow uintptr ) {
g . nativeWindow = windows . HWND ( nativeWindow )
}
2022-02-01 05:52:23 +01:00
type nativeGamepad struct {
2022-06-03 09:59:44 +02:00
dinputDevice * _IDirectInputDevice8W
2022-02-01 05:52:23 +01:00
dinputObjects [ ] dinputObject
dinputGUID windows . GUID
dinputAxes [ ] float64
dinputButtons [ ] bool
dinputHats [ ] int
xinputIndex int
2022-06-03 09:59:44 +02:00
xinputState _XINPUT_STATE
2022-02-01 05:52:23 +01:00
}
2022-01-22 20:20:54 +01:00
func ( * nativeGamepad ) hasOwnStandardLayoutMapping ( ) bool {
return false
}
func ( g * nativeGamepad ) usesDInput ( ) bool {
return g . dinputDevice != nil
}
func ( g * nativeGamepad ) update ( gamepads * gamepads ) ( err error ) {
var disconnected bool
defer func ( ) {
if ! disconnected && err == nil {
return
}
gamepads . remove ( func ( gamepad * Gamepad ) bool {
return & gamepad . nativeGamepad == g
} )
} ( )
if g . usesDInput ( ) {
if err := g . dinputDevice . Poll ( ) ; err != nil {
if ! errors . Is ( err , directInputError ( _DIERR_NOTACQUIRED ) ) && ! errors . Is ( err , directInputError ( _DIERR_INPUTLOST ) ) {
return err
}
}
2022-02-14 20:07:11 +01:00
var state _DIJOYSTATE
2022-01-22 20:20:54 +01:00
if err := g . dinputDevice . GetDeviceState ( uint32 ( unsafe . Sizeof ( state ) ) , unsafe . Pointer ( & state ) ) ; err != nil {
if ! errors . Is ( err , directInputError ( _DIERR_NOTACQUIRED ) ) && ! errors . Is ( err , directInputError ( _DIERR_INPUTLOST ) ) {
return err
}
// Acquire can return an error just after a gamepad is disconnected. Ignore the error.
g . dinputDevice . Acquire ( )
if err := g . dinputDevice . Poll ( ) ; err != nil {
if ! errors . Is ( err , directInputError ( _DIERR_NOTACQUIRED ) ) && ! errors . Is ( err , directInputError ( _DIERR_INPUTLOST ) ) {
return err
}
}
if err := g . dinputDevice . GetDeviceState ( uint32 ( unsafe . Sizeof ( state ) ) , unsafe . Pointer ( & state ) ) ; err != nil {
if ! errors . Is ( err , directInputError ( _DIERR_NOTACQUIRED ) ) && ! errors . Is ( err , directInputError ( _DIERR_INPUTLOST ) ) {
return err
}
disconnected = true
return nil
}
}
var ai , bi , hi int
for _ , obj := range g . dinputObjects {
switch obj . objectType {
case dinputObjectTypeAxis :
var v int32
switch obj . index {
case 0 :
v = state . lX
case 1 :
v = state . lY
case 2 :
v = state . lZ
case 3 :
v = state . lRx
case 4 :
v = state . lRy
case 5 :
v = state . lRz
}
g . dinputAxes [ ai ] = ( float64 ( v ) + 0.5 ) / 32767.5
ai ++
case dinputObjectTypeSlider :
v := state . rglSlider [ obj . index ]
g . dinputAxes [ ai ] = ( float64 ( v ) + 0.5 ) / 32767.5
ai ++
case dinputObjectTypeButton :
v := ( state . rgbButtons [ obj . index ] & 0x80 ) != 0
g . dinputButtons [ bi ] = v
bi ++
case dinputObjectTypePOV :
stateIndex := state . rgdwPOV [ obj . index ] / ( 45 * _DI_DEGREES )
v := hatCentered
switch stateIndex {
case 0 :
v = hatUp
case 1 :
v = hatRightUp
case 2 :
v = hatRight
case 3 :
v = hatRightDown
case 4 :
v = hatDown
case 5 :
v = hatLeftDown
case 6 :
v = hatLeft
case 7 :
v = hatLeftUp
}
g . dinputHats [ hi ] = v
hi ++
}
}
return nil
}
2022-06-03 09:59:44 +02:00
var state _XINPUT_STATE
2022-01-22 20:20:54 +01:00
if err := gamepads . xinputGetState ( uint32 ( g . xinputIndex ) , & state ) ; err != nil {
if ! errors . Is ( err , windows . ERROR_DEVICE_NOT_CONNECTED ) {
return err
}
disconnected = true
return nil
}
g . xinputState = state
return nil
}
2022-02-01 18:01:46 +01:00
func ( g * nativeGamepad ) axisCount ( ) int {
2022-01-22 20:20:54 +01:00
if g . usesDInput ( ) {
return len ( g . dinputAxes )
}
return 6
}
2022-02-01 18:01:46 +01:00
func ( g * nativeGamepad ) buttonCount ( ) int {
2022-01-22 20:20:54 +01:00
if g . usesDInput ( ) {
return len ( g . dinputButtons )
}
return len ( xinputButtons )
}
2022-02-01 18:01:46 +01:00
func ( g * nativeGamepad ) hatCount ( ) int {
2022-01-22 20:20:54 +01:00
if g . usesDInput ( ) {
return len ( g . dinputHats )
}
return 1
}
func ( g * nativeGamepad ) axisValue ( axis int ) float64 {
if g . usesDInput ( ) {
if axis < 0 || axis >= len ( g . dinputAxes ) {
return 0
}
return g . dinputAxes [ axis ]
}
var v float64
switch axis {
case 0 :
v = ( float64 ( g . xinputState . Gamepad . sThumbLX ) + 0.5 ) / 32767.5
case 1 :
v = - ( float64 ( g . xinputState . Gamepad . sThumbLY ) + 0.5 ) / 32767.5
case 2 :
v = ( float64 ( g . xinputState . Gamepad . sThumbRX ) + 0.5 ) / 32767.5
case 3 :
v = - ( float64 ( g . xinputState . Gamepad . sThumbRY ) + 0.5 ) / 32767.5
case 4 :
v = float64 ( g . xinputState . Gamepad . bLeftTrigger ) / 127.5 - 1.0
case 5 :
v = float64 ( g . xinputState . Gamepad . bRightTrigger ) / 127.5 - 1.0
}
return v
}
func ( g * nativeGamepad ) isButtonPressed ( button int ) bool {
if g . usesDInput ( ) {
if button < 0 || button >= len ( g . dinputButtons ) {
return false
}
return g . dinputButtons [ button ]
}
if button < 0 || button >= len ( xinputButtons ) {
return false
}
return g . xinputState . Gamepad . wButtons & xinputButtons [ button ] != 0
}
func ( g * nativeGamepad ) buttonValue ( button int ) float64 {
panic ( "gamepad: buttonValue is not implemented" )
}
func ( g * nativeGamepad ) hatState ( hat int ) int {
if g . usesDInput ( ) {
2022-05-25 04:44:43 +02:00
if hat < 0 || hat >= len ( g . dinputHats ) {
return 0
}
2022-01-22 20:20:54 +01:00
return g . dinputHats [ hat ]
}
if hat != 0 {
return 0
}
var v int
if g . xinputState . Gamepad . wButtons & _XINPUT_GAMEPAD_DPAD_UP != 0 {
v |= hatUp
}
if g . xinputState . Gamepad . wButtons & _XINPUT_GAMEPAD_DPAD_RIGHT != 0 {
v |= hatRight
}
if g . xinputState . Gamepad . wButtons & _XINPUT_GAMEPAD_DPAD_DOWN != 0 {
v |= hatDown
}
if g . xinputState . Gamepad . wButtons & _XINPUT_GAMEPAD_DPAD_LEFT != 0 {
v |= hatLeft
}
return v
}
func ( g * nativeGamepad ) vibrate ( duration time . Duration , strongMagnitude float64 , weakMagnitude float64 ) {
// TODO: Implement this (#1452)
}