internal/graphicsdriver/opengl, internal/uidriver/glfw: treat Win32 API errors correctly

The returned errors from syscall.Syscall* and windows.LazyProc.Call come
from GetLastError. The value of GetLastError is not reliable when the
function succeeds.

This change fixes the usages of error values. The error value is now
used only when the API explicitly fails.
This commit is contained in:
Hajime Hoshi 2022-01-30 15:36:09 +09:00
parent b3e8d5d7ae
commit 84e53d4c61
3 changed files with 62 additions and 94 deletions

View File

@ -10,8 +10,8 @@ import (
) )
var ( var (
opengl32 = windows.NewLazySystemDLL("opengl32") opengl32 = windows.NewLazySystemDLL("opengl32")
wglGetProcAddress = opengl32.NewProc("wglGetProcAddress") procWglGetProcAddress = opengl32.NewProc("wglGetProcAddress")
) )
func getProcAddress(namea string) uintptr { func getProcAddress(namea string) uintptr {
@ -20,13 +20,13 @@ func getProcAddress(namea string) uintptr {
panic(err) panic(err)
} }
r, _, err := wglGetProcAddress.Call(uintptr(unsafe.Pointer(cname))) r, _, err := procWglGetProcAddress.Call(uintptr(unsafe.Pointer(cname)))
if err != nil && err != windows.ERROR_SUCCESS && err != windows.ERROR_PROC_NOT_FOUND {
panic(fmt.Sprintf("gl: wglGetProcAddress failed: %s", err.Error()))
}
if r != 0 { if r != 0 {
return r return r
} }
if err != nil && err != windows.ERROR_SUCCESS && err != windows.ERROR_PROC_NOT_FOUND {
panic(fmt.Sprintf("gl: wglGetProcAddress failed: %s", err.Error()))
}
p := opengl32.NewProc(namea) p := opengl32.NewProc(namea)
if err := p.Find(); err != nil { if err := p.Find(); err != nil {

View File

@ -29,71 +29,48 @@ var (
kernel32 = windows.NewLazySystemDLL("kernel32.dll") kernel32 = windows.NewLazySystemDLL("kernel32.dll")
user32 = windows.NewLazySystemDLL("user32.dll") user32 = windows.NewLazySystemDLL("user32.dll")
getCurrentProcessIdProc = kernel32.NewProc("GetCurrentProcessId") procFreeConsoleWindow = kernel32.NewProc("FreeConsole")
getConsoleWindowProc = kernel32.NewProc("GetConsoleWindow") procGetCurrentProcessId = kernel32.NewProc("GetCurrentProcessId")
freeConsoleWindowProc = kernel32.NewProc("FreeConsole") procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow")
getWindowThreadProcessIdProc = user32.NewProc("GetWindowThreadProcessId") procGetWindowThreadProcessId = user32.NewProc("GetWindowThreadProcessId")
) )
func getCurrentProcessId() (uint32, error) {
r, _, e := getCurrentProcessIdProc.Call()
if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: GetCurrentProcessId failed: %w", e)
}
return uint32(r), nil
}
func getWindowThreadProcessId(hwnd uintptr) (uint32, error) {
pid := uint32(0)
r, _, e := getWindowThreadProcessIdProc.Call(hwnd, uintptr(unsafe.Pointer(&pid)))
if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: GetWindowThreadProcessId failed: %w", e)
}
if r == 0 {
return 0, fmt.Errorf("ui: GetWindowThreadProcessId returned 0")
}
return pid, nil
}
func getConsoleWindow() (uintptr, error) {
r, _, e := getConsoleWindowProc.Call()
if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: GetConsoleWindow failed: %w", e)
}
return r, nil
}
func freeConsole() error { func freeConsole() error {
if _, _, e := freeConsoleWindowProc.Call(); e != nil && e != windows.ERROR_SUCCESS { r, _, e := procFreeConsoleWindow.Call()
return fmt.Errorf("ui: FreeConsole failed: %w", e) if r == 0 {
if e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("glfw: FreeConsole failed: %w", e)
}
return fmt.Errorf("glfw: FreeConsole returned 0")
} }
return nil return nil
} }
func getCurrentProcessId() uint32 {
r, _, _ := procGetCurrentProcessId.Call()
return uint32(r)
}
func getConsoleWindow() windows.HWND {
r, _, _ := procGetConsoleWindow.Call()
return windows.HWND(r)
}
func getWindowThreadProcessId(hwnd windows.HWND) (tid, pid uint32) {
r, _, _ := procGetWindowThreadProcessId.Call(uintptr(hwnd), uintptr(unsafe.Pointer(&pid)))
tid = uint32(r)
return
}
// hideConsoleWindowOnWindows will hide the console window that is showing when // hideConsoleWindowOnWindows will hide the console window that is showing when
// compiling on Windows without specifying the '-ldflags "-Hwindowsgui"' flag. // compiling on Windows without specifying the '-ldflags "-Hwindowsgui"' flag.
func hideConsoleWindowOnWindows() { func hideConsoleWindowOnWindows() {
pid, err := getCurrentProcessId() pid := getCurrentProcessId()
if err != nil {
// Ignore errors because:
// 1. It is not critical if the console can't be hid.
// 2. There is nothing to do when errors happen.
return
}
w, err := getConsoleWindow()
if err != nil {
// Ignore errors
return
}
// Get the process ID of the console's creator. // Get the process ID of the console's creator.
cpid, err := getWindowThreadProcessId(w) _, cpid := getWindowThreadProcessId(getConsoleWindow())
if err != nil {
// Ignore errors
return
}
if pid == cpid { if pid == cpid {
// The current process created its own console. Hide this. // The current process created its own console. Hide this.
// Ignore error // Ignore error.
freeConsole() freeConsole()
} }
} }

View File

@ -52,40 +52,34 @@ var (
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW") procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
) )
func getSystemMetrics(nIndex int) (int, error) { func getSystemMetrics(nIndex int) (int32, error) {
r, _, e := procGetSystemMetrics.Call(uintptr(nIndex)) r, _, e := procGetSystemMetrics.Call(uintptr(nIndex))
if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: GetSystemMetrics failed: error code: %w", e)
}
return int(r), nil
}
func getForegroundWindow() (uintptr, error) {
r, _, e := procGetForegroundWindow.Call()
if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: GetForegroundWindow failed: error code: %w", e)
}
return r, nil
}
func monitorFromWindow(hwnd uintptr, dwFlags uint32) (uintptr, error) {
r, _, e := procMonitorFromWindow.Call(hwnd, uintptr(dwFlags))
if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: MonitorFromWindow failed: error code: %w", e)
}
if r == 0 { if r == 0 {
return 0, fmt.Errorf("ui: MonitorFromWindow failed: returned value: %d", r) if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("glfw: GetSystemMetrics failed: error code: %w", e)
}
return 0, fmt.Errorf("glfw: GetSystemMetrics returned 0")
} }
return r, nil return int32(r), nil
}
func getForegroundWindow() windows.HWND {
r, _, _ := procGetForegroundWindow.Call()
return windows.HWND(r)
}
func monitorFromWindow(hwnd windows.HWND, dwFlags uint32) uintptr {
r, _, _ := procMonitorFromWindow.Call(uintptr(hwnd), uintptr(dwFlags))
return r
} }
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 e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("ui: GetMonitorInfoW failed: error code: %w", e)
}
if r == 0 { if r == 0 {
return fmt.Errorf("ui: GetMonitorInfoW failed: returned value: %d", r) if e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("glfw: GetMonitorInfoW failed: error code: %w", e)
}
return fmt.Errorf("glfw: GetMonitorInfoW failed: returned 0")
} }
return nil return nil
} }
@ -119,18 +113,15 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
if y < my+t { if y < my+int(t) {
y = my + t y = my + int(t)
} }
return x, y return x, y
} }
func initialMonitorByOS() *glfw.Monitor { func initialMonitorByOS() *glfw.Monitor {
// Get the foreground window, that is common among multiple processes. // Get the foreground window, that is common among multiple processes.
w, err := getForegroundWindow() w := getForegroundWindow()
if err != nil {
panic(err)
}
if w == 0 { if w == 0 {
// GetForegroundWindow can return null according to the document. // GetForegroundWindow can return null according to the document.
return nil return nil
@ -139,15 +130,15 @@ func initialMonitorByOS() *glfw.Monitor {
} }
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor { func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
return monitorFromWin32Window(w.GetWin32Window()) return monitorFromWin32Window(windows.HWND(w.GetWin32Window()))
} }
func monitorFromWin32Window(w uintptr) *glfw.Monitor { func monitorFromWin32Window(w windows.HWND) *glfw.Monitor {
// Get the current monitor by the window handle instead of the window position. It is because the window // Get the current monitor by the window handle instead of the window position. It is because the window
// position is not relaiable in some cases e.g. when the window is put across multiple monitors. // position is not relaiable in some cases e.g. when the window is put across multiple monitors.
m, err := monitorFromWindow(w, monitorDefaultToNearest) m := monitorFromWindow(w, monitorDefaultToNearest)
if err != nil { if m == 0 {
// monitorFromWindow can return error on Wine. Ignore this. // monitorFromWindow can return error on Wine. Ignore this.
return nil return nil
} }