2014-02-11 15:14:15 -06:00
|
|
|
// 11 february 2014
|
|
|
|
//package ui
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
var uitask chan *uimsg
|
|
|
|
|
|
|
|
type uimsg struct {
|
|
|
|
call *syscall.LazyProc
|
|
|
|
p []uintptr
|
|
|
|
ret chan uiret
|
|
|
|
}
|
|
|
|
|
|
|
|
type uiret struct {
|
|
|
|
ret uintptr
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func ui(initDone chan error) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
uitask = make(chan *uimsg)
|
2014-02-11 15:25:27 -06:00
|
|
|
initDone <- doWindowsInit()
|
2014-02-11 15:14:15 -06:00
|
|
|
|
2014-02-11 21:38:46 -06:00
|
|
|
quit := false
|
|
|
|
for !quit {
|
2014-02-11 21:18:14 -06:00
|
|
|
select {
|
|
|
|
case m := <-uitask:
|
|
|
|
r1, _, err := m.call.Call(m.p...)
|
|
|
|
m.ret <- uiret{
|
|
|
|
ret: r1,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
default:
|
2014-02-11 21:38:46 -06:00
|
|
|
quit = msgloopstep()
|
2014-02-11 15:14:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-11 17:31:24 -06:00
|
|
|
|
2014-02-11 21:38:46 -06:00
|
|
|
const (
|
|
|
|
_PM_REMOVE = 0x0001
|
|
|
|
)
|
|
|
|
|
2014-02-11 17:31:24 -06:00
|
|
|
var (
|
|
|
|
_dispatchMessage = user32.NewProc("DispatchMessageW")
|
|
|
|
_getMessage = user32.NewProc("GetMessageW")
|
2014-02-11 21:38:46 -06:00
|
|
|
_peekMessage = user32.NewProc("PeekMessageW")
|
2014-02-11 17:31:24 -06:00
|
|
|
_postQuitMessage = user32.NewProc("PostQuitMessage")
|
|
|
|
_sendMessage = user32.NewProc("SendMessageW")
|
|
|
|
_translateMessage = user32.NewProc("TranslateMessage")
|
|
|
|
)
|
|
|
|
|
2014-02-11 18:18:03 -06:00
|
|
|
var getMessageFail = -1 // because Go doesn't let me
|
|
|
|
|
2014-02-11 21:38:46 -06:00
|
|
|
func msgloopstep() (quit bool) {
|
2014-02-11 17:31:24 -06:00
|
|
|
var msg struct {
|
|
|
|
Hwnd _HWND
|
|
|
|
Message uint32
|
|
|
|
WParam _WPARAM
|
|
|
|
LParam _LPARAM
|
|
|
|
Time uint32
|
|
|
|
Pt _POINT
|
|
|
|
}
|
|
|
|
|
2014-02-11 21:38:46 -06:00
|
|
|
// TODO figure out how to handle errors
|
|
|
|
r1, _, _ := _peekMessage.Call(
|
|
|
|
uintptr(unsafe.Pointer(&msg)),
|
|
|
|
uintptr(_NULL),
|
|
|
|
uintptr(0),
|
|
|
|
uintptr(0),
|
|
|
|
uintptr(_PM_REMOVE))
|
|
|
|
if r1 == 0 { // no message available
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if msg.Message == _WM_QUIT {
|
|
|
|
return true
|
2014-02-11 17:31:24 -06:00
|
|
|
}
|
2014-02-11 21:38:46 -06:00
|
|
|
// TODO handle potential errors in TranslateMessage() and DispatchMessage()
|
|
|
|
_translateMessage.Call(uintptr(unsafe.Pointer(&msg)))
|
|
|
|
_dispatchMessage.Call(uintptr(unsafe.Pointer(&msg)))
|
|
|
|
return false
|
2014-02-11 17:31:24 -06:00
|
|
|
}
|