2014-02-11 15:14:15 -06:00
|
|
|
// 11 february 2014
|
2014-03-12 20:55:45 -05:00
|
|
|
|
2014-02-19 10:41:10 -06:00
|
|
|
package ui
|
2014-02-11 15:14:15 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2014-02-19 21:59:48 -06:00
|
|
|
/*
|
|
|
|
problem: messages have to be dispatched on the same thread as system calls, and we can't mux GetMessage() with select, and PeekMessage() every iteration is wasteful (and leads to lag for me (only) with the concurrent garbage collector sweep)
|
|
|
|
solution: use PostThreadMessage() to send uimsgs out to the message loop, which runs on its own goroutine
|
|
|
|
I had come up with this first but wanted to try other things before doing it (and wasn't really sure if user-defined messages were safe, not quite understanding the system); nsf came up with it independently and explained that this was really the only right way to do it, so thanks to him
|
|
|
|
*/
|
|
|
|
|
2014-02-11 15:14:15 -06:00
|
|
|
var uitask chan *uimsg
|
|
|
|
|
|
|
|
type uimsg struct {
|
|
|
|
call *syscall.LazyProc
|
|
|
|
p []uintptr
|
|
|
|
ret chan uiret
|
|
|
|
}
|
|
|
|
|
|
|
|
type uiret struct {
|
|
|
|
ret uintptr
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2014-02-19 21:59:48 -06:00
|
|
|
const (
|
|
|
|
_WM_APP = 0x8000 + iota
|
|
|
|
msgRequested
|
2014-03-05 12:21:45 -06:00
|
|
|
msgQuit
|
2014-02-19 21:59:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_postThreadMessage = user32.NewProc("PostThreadMessageW")
|
|
|
|
)
|
|
|
|
|
2014-03-01 14:18:29 -06:00
|
|
|
func ui(main func()) error {
|
2014-02-11 15:14:15 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
uitask = make(chan *uimsg)
|
2014-03-01 14:18:29 -06:00
|
|
|
err := doWindowsInit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-11 15:14:15 -06:00
|
|
|
|
2014-02-19 21:59:48 -06:00
|
|
|
threadIDReq := make(chan uintptr)
|
|
|
|
msglooperrs := make(chan error)
|
|
|
|
go msgloop(threadIDReq, msglooperrs)
|
|
|
|
threadID := <-threadIDReq
|
|
|
|
|
2014-03-05 12:21:45 -06:00
|
|
|
go func() {
|
|
|
|
main()
|
|
|
|
r1, _, err := _postThreadMessage.Call(
|
|
|
|
threadID,
|
|
|
|
msgQuit,
|
|
|
|
uintptr(0),
|
|
|
|
uintptr(0))
|
|
|
|
if r1 == 0 { // failure
|
|
|
|
panic("error sending quit message to message loop: " + err.Error()) // TODO
|
|
|
|
}
|
|
|
|
}()
|
2014-03-01 14:18:29 -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:
|
2014-02-19 21:59:48 -06:00
|
|
|
r1, _, err := _postThreadMessage.Call(
|
|
|
|
threadID,
|
|
|
|
msgRequested,
|
|
|
|
uintptr(0),
|
|
|
|
uintptr(unsafe.Pointer(m)))
|
|
|
|
if r1 == 0 { // failure
|
|
|
|
panic("error sending message to message loop to call function: " + err.Error()) // TODO
|
|
|
|
}
|
|
|
|
case err := <-msglooperrs:
|
|
|
|
if err == nil { // WM_QUIT; no error
|
|
|
|
quit = true
|
|
|
|
} else {
|
|
|
|
panic("unexpected return from message loop: " + err.Error()) // TODO
|
2014-02-11 21:18:14 -06:00
|
|
|
}
|
2014-02-11 15:14:15 -06:00
|
|
|
}
|
|
|
|
}
|
2014-03-01 14:18:29 -06:00
|
|
|
|
|
|
|
return nil
|
2014-02-11 15:14:15 -06:00
|
|
|
}
|
2014-02-11 17:31:24 -06:00
|
|
|
|
|
|
|
var (
|
|
|
|
_dispatchMessage = user32.NewProc("DispatchMessageW")
|
|
|
|
_getMessage = user32.NewProc("GetMessageW")
|
2014-02-19 21:59:48 -06:00
|
|
|
_getCurrentThreadID = kernel32.NewProc("GetCurrentThreadId")
|
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-19 21:59:48 -06:00
|
|
|
func msgloop(threadID chan uintptr, errors chan error) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
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-19 21:59:48 -06:00
|
|
|
r1, _, _ := _getCurrentThreadID.Call()
|
|
|
|
threadID <- r1
|
|
|
|
for {
|
|
|
|
r1, _, err := _getMessage.Call(
|
|
|
|
uintptr(unsafe.Pointer(&msg)),
|
|
|
|
uintptr(_NULL),
|
|
|
|
uintptr(0),
|
|
|
|
uintptr(0))
|
|
|
|
if r1 == uintptr(getMessageFail) { // error
|
|
|
|
errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r1 == 0 { // WM_QUIT message
|
|
|
|
errors <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if msg.Message == msgRequested {
|
|
|
|
m := (*uimsg)(unsafe.Pointer(msg.LParam))
|
|
|
|
r1, _, err := m.call.Call(m.p...)
|
|
|
|
m.ret <- uiret{
|
|
|
|
ret: r1,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2014-03-05 12:21:45 -06:00
|
|
|
if msg.Message == msgQuit {
|
|
|
|
// does not return a value according to MSDN
|
|
|
|
_postQuitMessage.Call(0)
|
|
|
|
continue
|
|
|
|
}
|
2014-02-19 21:59:48 -06:00
|
|
|
_translateMessage.Call(uintptr(unsafe.Pointer(&msg)))
|
|
|
|
_dispatchMessage.Call(uintptr(unsafe.Pointer(&msg)))
|
2014-02-11 17:31:24 -06:00
|
|
|
}
|
|
|
|
}
|