internal/glfwwin: remove GetProp and SetProp

GetProp and SetProp forced a risky usage of pointers.
This commit is contained in:
Hajime Hoshi 2022-05-25 02:10:32 +09:00
parent ec2df98c70
commit 0d7062335e
2 changed files with 6 additions and 42 deletions

View File

@ -799,7 +799,6 @@ var (
procGetLayeredWindowAttributes = user32.NewProc("GetLayeredWindowAttributes")
procGetMessageTime = user32.NewProc("GetMessageTime")
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
procGetPropW = user32.NewProc("GetPropW")
procGetRawInputData = user32.NewProc("GetRawInputData")
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
procGetSystemMetricsForDpi = user32.NewProc("GetSystemMetricsForDpi")
@ -835,7 +834,6 @@ var (
procSetLayeredWindowAttributes = user32.NewProc("procSetLayeredWindowAttributes")
procSetProcessDPIAware = user32.NewProc("SetProcessDPIAware")
procSetProcessDpiAwarenessContext = user32.NewProc("SetProcessDpiAwarenessContext")
procSetPropW = user32.NewProc("SetPropW")
procSetWindowLongW = user32.NewProc("SetWindowLongW")
procSetWindowPlacement = user32.NewProc("SetWindowPlacement")
procSetWindowPos = user32.NewProc("SetWindowPos")
@ -1358,22 +1356,6 @@ func _GetDpiForMonitor(hmonitor _HMONITOR, dpiType _MONITOR_DPI_TYPE) (dpiX, dpi
return dpiX, dpiY, nil
}
func _GetPropW(hWnd windows.HWND, str string) windows.Handle {
var lpString *uint16
if str != "" {
var err error
lpString, err = windows.UTF16PtrFromString(str)
if err != nil {
panic("glfwwin: str must not include a NUL character")
}
}
r, _, _ := procGetPropW.Call(uintptr(hWnd), uintptr(unsafe.Pointer(lpString)))
runtime.KeepAlive(lpString)
return windows.Handle(r)
}
func _GetRawInputData(hRawInput _HRAWINPUT, uiCommand uint32, pData unsafe.Pointer, pcbSize *uint32) (uint32, error) {
r, _, e := procGetRawInputData.Call(uintptr(hRawInput), uintptr(uiCommand), uintptr(pData), uintptr(unsafe.Pointer(pcbSize)), unsafe.Sizeof(_RAWINPUTHEADER{}))
if uint32(r) == (1<<32)-1 {
@ -1691,25 +1673,6 @@ func _SetProcessDpiAwarenessContext_Available() bool {
return procSetProcessDpiAwarenessContext.Find() == nil
}
func _SetPropW(hWnd windows.HWND, str string, hData windows.Handle) error {
var lpString *uint16
if str != "" {
var err error
lpString, err = windows.UTF16PtrFromString(str)
if err != nil {
panic("glfwwin: str must not include a NUL character")
}
}
r, _, e := procSetPropW.Call(uintptr(hWnd), uintptr(unsafe.Pointer(lpString)), uintptr(hData))
runtime.KeepAlive(lpString)
if int32(r) == 0 {
return fmt.Errorf("glfwwin: SetPropW failed: %w", e)
}
return nil
}
func _SetThreadExecutionState(esFlags _EXECUTION_STATE) _EXECUTION_STATE {
r, _, _ := procSetThreadExecutionState.Call(uintptr(esFlags))
return _EXECUTION_STATE(r)

View File

@ -580,7 +580,7 @@ func (w *Window) maximizeWindowManually() error {
}
func windowProc(hWnd windows.HWND, uMsg uint32, wParam _WPARAM, lParam _LPARAM) uintptr /*_LRESULT*/ {
window := (*Window)(unsafe.Pointer(_GetPropW(hWnd, "GLFW")))
window := handleToWindow[hWnd]
if window == nil {
// This is the message handling for the hidden helper window
// and for a regular window during its initial creation
@ -1180,6 +1180,8 @@ func windowProc(hWnd windows.HWND, uMsg uint32, wParam _WPARAM, lParam _LPARAM)
var windowProcPtr = windows.NewCallbackCDecl(windowProc)
var handleToWindow = map[windows.HWND]*Window{}
func (w *Window) createNativeWindow(wndconfig *wndconfig, fbconfig *fbconfig) error {
style := w.getWindowStyle()
exStyle := w.getWindowExStyle()
@ -1225,9 +1227,7 @@ func (w *Window) createNativeWindow(wndconfig *wndconfig, fbconfig *fbconfig) er
}
w.win32.handle = h
if err := _SetPropW(w.win32.handle, "GLFW", windows.Handle(unsafe.Pointer(w))); err != nil {
return err
}
handleToWindow[w.win32.handle] = w
if _IsWindows7OrGreater() {
if err := _ChangeWindowMessageFilterEx(w.win32.handle, _WM_DROPFILES, _MSGFLT_ALLOW, nil); err != nil {
@ -1432,6 +1432,7 @@ func (w *Window) platformDestroyWindow() error {
if err := _DestroyWindow(w.win32.handle); err != nil {
return err
}
delete(handleToWindow, w.win32.handle)
w.win32.handle = 0
}
@ -1998,7 +1999,7 @@ func platformPollEvents() error {
// because they change the input focus
// NOTE: The other half of this is in the WM_*KEY* handler in windowProc
if handle := _GetActiveWindow(); handle != 0 {
if window := (*Window)(unsafe.Pointer(_GetPropW(handle, "GLFW"))); window != nil {
if window := handleToWindow[handle]; window != nil {
keys := [...]struct {
VK int
Key Key