internal/gamepad, internal/ui: type-cast returning values from Win32APIs just in case

This commit is contained in:
Hajime Hoshi 2022-05-11 00:48:41 +09:00
parent 812607cdfc
commit 3d00bb1fa2
3 changed files with 13 additions and 13 deletions

View File

@ -338,7 +338,7 @@ func (d *iDirectInput8W) CreateDevice(rguid *windows.GUID, lplpDirectInputDevice
uintptr(unsafe.Pointer(d)), uintptr(unsafe.Pointer(d)),
uintptr(unsafe.Pointer(rguid)), uintptr(unsafe.Pointer(lplpDirectInputDevice)), uintptr(pUnkOuter), uintptr(unsafe.Pointer(rguid)), uintptr(unsafe.Pointer(lplpDirectInputDevice)), uintptr(pUnkOuter),
0, 0) 0, 0)
if r != _DI_OK { if windows.Handle(r) != _DI_OK {
return fmt.Errorf("gamepad: IDirectInput8::CreateDevice failed: %w", directInputError(syscall.Errno(r))) return fmt.Errorf("gamepad: IDirectInput8::CreateDevice failed: %w", directInputError(syscall.Errno(r)))
} }
return nil return nil
@ -349,7 +349,7 @@ func (d *iDirectInput8W) EnumDevices(dwDevType uint32, lpCallback uintptr, pvRef
uintptr(unsafe.Pointer(d)), uintptr(unsafe.Pointer(d)),
uintptr(dwDevType), lpCallback, uintptr(pvRef), uintptr(dwFlags), uintptr(dwDevType), lpCallback, uintptr(pvRef), uintptr(dwFlags),
0) 0)
if r != _DI_OK { if windows.Handle(r) != _DI_OK {
return fmt.Errorf("gamepad: IDirectInput8::EnumDevices failed: %w", directInputError(syscall.Errno(r))) return fmt.Errorf("gamepad: IDirectInput8::EnumDevices failed: %w", directInputError(syscall.Errno(r)))
} }
return nil return nil
@ -397,7 +397,7 @@ type iDirectInputDevice8W_Vtbl struct {
func (d *iDirectInputDevice8W) Acquire() error { func (d *iDirectInputDevice8W) Acquire() error {
r, _, _ := syscall.Syscall(d.vtbl.Acquire, 1, uintptr(unsafe.Pointer(d)), 0, 0) r, _, _ := syscall.Syscall(d.vtbl.Acquire, 1, uintptr(unsafe.Pointer(d)), 0, 0)
if r != _DI_OK && r != _SI_FALSE { if windows.Handle(r) != _DI_OK && windows.Handle(r) != _SI_FALSE {
return fmt.Errorf("gamepad: IDirectInputDevice8::Acquire failed: %w", directInputError(syscall.Errno(r))) return fmt.Errorf("gamepad: IDirectInputDevice8::Acquire failed: %w", directInputError(syscall.Errno(r)))
} }
return nil return nil
@ -408,7 +408,7 @@ func (d *iDirectInputDevice8W) EnumObjects(lpCallback uintptr, pvRef unsafe.Poin
uintptr(unsafe.Pointer(d)), uintptr(unsafe.Pointer(d)),
lpCallback, uintptr(pvRef), uintptr(dwFlags), lpCallback, uintptr(pvRef), uintptr(dwFlags),
0, 0) 0, 0)
if r != _DI_OK { if windows.Handle(r) != _DI_OK {
return fmt.Errorf("gamepad: IDirectInputDevice8::EnumObjects failed: %w", directInputError(syscall.Errno(r))) return fmt.Errorf("gamepad: IDirectInputDevice8::EnumObjects failed: %w", directInputError(syscall.Errno(r)))
} }
return nil return nil
@ -416,7 +416,7 @@ func (d *iDirectInputDevice8W) EnumObjects(lpCallback uintptr, pvRef unsafe.Poin
func (d *iDirectInputDevice8W) GetCapabilities(lpDIDevCaps *_DIDEVCAPS) error { func (d *iDirectInputDevice8W) GetCapabilities(lpDIDevCaps *_DIDEVCAPS) error {
r, _, _ := syscall.Syscall(d.vtbl.GetCapabilities, 2, uintptr(unsafe.Pointer(d)), uintptr(unsafe.Pointer(lpDIDevCaps)), 0) r, _, _ := syscall.Syscall(d.vtbl.GetCapabilities, 2, uintptr(unsafe.Pointer(d)), uintptr(unsafe.Pointer(lpDIDevCaps)), 0)
if r != _DI_OK { if windows.Handle(r) != _DI_OK {
return fmt.Errorf("gamepad: IDirectInputDevice8::GetCapabilities failed: %w", directInputError(syscall.Errno(r))) return fmt.Errorf("gamepad: IDirectInputDevice8::GetCapabilities failed: %w", directInputError(syscall.Errno(r)))
} }
return nil return nil
@ -424,7 +424,7 @@ func (d *iDirectInputDevice8W) GetCapabilities(lpDIDevCaps *_DIDEVCAPS) error {
func (d *iDirectInputDevice8W) GetDeviceState(cbData uint32, lpvData unsafe.Pointer) error { func (d *iDirectInputDevice8W) GetDeviceState(cbData uint32, lpvData unsafe.Pointer) error {
r, _, _ := syscall.Syscall(d.vtbl.GetDeviceState, 3, uintptr(unsafe.Pointer(d)), uintptr(cbData), uintptr(lpvData)) r, _, _ := syscall.Syscall(d.vtbl.GetDeviceState, 3, uintptr(unsafe.Pointer(d)), uintptr(cbData), uintptr(lpvData))
if r != _DI_OK { if windows.Handle(r) != _DI_OK {
return fmt.Errorf("gamepad: IDirectInputDevice8::GetDeviceState failed: %w", directInputError(syscall.Errno(r))) return fmt.Errorf("gamepad: IDirectInputDevice8::GetDeviceState failed: %w", directInputError(syscall.Errno(r)))
} }
return nil return nil
@ -432,7 +432,7 @@ func (d *iDirectInputDevice8W) GetDeviceState(cbData uint32, lpvData unsafe.Poin
func (d *iDirectInputDevice8W) Poll() error { func (d *iDirectInputDevice8W) Poll() error {
r, _, _ := syscall.Syscall(d.vtbl.Poll, 1, uintptr(unsafe.Pointer(d)), 0, 0) r, _, _ := syscall.Syscall(d.vtbl.Poll, 1, uintptr(unsafe.Pointer(d)), 0, 0)
if r != _DI_OK && r != _DI_NOEFFECT { if windows.Handle(r) != _DI_OK && windows.Handle(r) != _DI_NOEFFECT {
return fmt.Errorf("gamepad: IDirectInputDevice8::Poll failed: %w", directInputError(syscall.Errno(r))) return fmt.Errorf("gamepad: IDirectInputDevice8::Poll failed: %w", directInputError(syscall.Errno(r)))
} }
return nil return nil
@ -445,7 +445,7 @@ func (d *iDirectInputDevice8W) Release() uint32 {
func (d *iDirectInputDevice8W) SetDataFormat(lpdf *_DIDATAFORMAT) error { func (d *iDirectInputDevice8W) SetDataFormat(lpdf *_DIDATAFORMAT) error {
r, _, _ := syscall.Syscall(d.vtbl.SetDataFormat, 2, uintptr(unsafe.Pointer(d)), uintptr(unsafe.Pointer(lpdf)), 0) r, _, _ := syscall.Syscall(d.vtbl.SetDataFormat, 2, uintptr(unsafe.Pointer(d)), uintptr(unsafe.Pointer(lpdf)), 0)
if r != _DI_OK { if windows.Handle(r) != _DI_OK {
return fmt.Errorf("gamepad: IDirectInputDevice8::SetDataFormat failed: %w", directInputError(syscall.Errno(r))) return fmt.Errorf("gamepad: IDirectInputDevice8::SetDataFormat failed: %w", directInputError(syscall.Errno(r)))
} }
return nil return nil
@ -453,7 +453,7 @@ func (d *iDirectInputDevice8W) SetDataFormat(lpdf *_DIDATAFORMAT) error {
func (d *iDirectInputDevice8W) SetProperty(rguidProp uintptr, pdiph *_DIPROPHEADER) error { func (d *iDirectInputDevice8W) SetProperty(rguidProp uintptr, pdiph *_DIPROPHEADER) error {
r, _, _ := syscall.Syscall(d.vtbl.SetProperty, 3, uintptr(unsafe.Pointer(d)), rguidProp, uintptr(unsafe.Pointer(pdiph))) r, _, _ := syscall.Syscall(d.vtbl.SetProperty, 3, uintptr(unsafe.Pointer(d)), rguidProp, uintptr(unsafe.Pointer(pdiph)))
if r != _DI_OK && r != _DI_PROPNOEFFECT { if windows.Handle(r) != _DI_OK && windows.Handle(r) != _DI_PROPNOEFFECT {
return fmt.Errorf("gamepad: IDirectInputDevice8::SetProperty failed: %w", directInputError(syscall.Errno(r))) return fmt.Errorf("gamepad: IDirectInputDevice8::SetProperty failed: %w", directInputError(syscall.Errno(r)))
} }
return nil return nil

View File

@ -40,7 +40,7 @@ var (
func freeConsole() error { func freeConsole() error {
r, _, e := procFreeConsoleWindow.Call() r, _, e := procFreeConsoleWindow.Call()
if r == 0 { if int32(r) == 0 {
if e != nil && e != windows.ERROR_SUCCESS { if e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("ui: FreeConsole failed: %w", e) return fmt.Errorf("ui: FreeConsole failed: %w", e)
} }

View File

@ -96,7 +96,7 @@ var (
func getSystemMetrics(nIndex int) (int32, error) { func getSystemMetrics(nIndex int) (int32, error) {
r, _, _ := procGetSystemMetrics.Call(uintptr(nIndex)) r, _, _ := procGetSystemMetrics.Call(uintptr(nIndex))
if r == 0 { if int32(r) == 0 {
// GetLastError doesn't provide an extended information. // GetLastError doesn't provide an extended information.
// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics // See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics
return 0, fmt.Errorf("ui: GetSystemMetrics returned 0") return 0, fmt.Errorf("ui: GetSystemMetrics returned 0")
@ -111,7 +111,7 @@ func monitorFromWindow_(hwnd windows.HWND, dwFlags uint32) uintptr {
func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error { func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
r, _, e := procGetMonitorInfoW.Call(hMonitor, uintptr(unsafe.Pointer(lpmi))) r, _, e := procGetMonitorInfoW.Call(hMonitor, uintptr(unsafe.Pointer(lpmi)))
if r == 0 { if int32(r) == 0 {
if e != nil && e != windows.ERROR_SUCCESS { if e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("ui: GetMonitorInfoW failed: error code: %w", e) return fmt.Errorf("ui: GetMonitorInfoW failed: error code: %w", e)
} }
@ -123,7 +123,7 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
func getCursorPos() (int32, int32, error) { func getCursorPos() (int32, int32, error) {
var pt point var pt point
r, _, e := procGetCursorPos.Call(uintptr(unsafe.Pointer(&pt))) r, _, e := procGetCursorPos.Call(uintptr(unsafe.Pointer(&pt)))
if r == 0 { if int32(r) == 0 {
if e != nil && e != windows.ERROR_SUCCESS { if e != nil && e != windows.ERROR_SUCCESS {
return 0, 0, fmt.Errorf("ui: GetCursorPos failed: error code: %w", e) return 0, 0, fmt.Errorf("ui: GetCursorPos failed: error code: %w", e)
} }