andlabs-ui/main.go

96 lines
2.4 KiB
Go
Raw Normal View History

2015-12-11 20:03:38 -06:00
// 11 december 2015
package ui
import (
2015-12-11 21:48:25 -06:00
"runtime"
2015-12-11 20:03:38 -06:00
"errors"
2015-12-11 21:48:25 -06:00
"sync"
"unsafe"
2015-12-11 20:03:38 -06:00
)
2015-12-11 21:48:25 -06:00
// #include "ui.h"
// extern void doQueued(void *);
// /* I forgot how dumb cgo is... ./main.go:73: cannot use _Cgo_ptr(_Cfpvar_fp_doQueued) (type unsafe.Pointer) as type *[0]byte in argument to _Cfunc_uiQueueMain */
// /* I'm pretty sure this worked before... */
// static inline void realQueueMain(void *x)
// {
// uiQueueMain(doQueued, x);
// }
2015-12-11 20:03:38 -06:00
import "C"
// Main initializes package ui, runs f to set up the program,
// and executes the GUI main loop. f should set up the program's
// initial state: open the main window, create controls, and set up
// events. It should then return, at which point Main will
// process events until Quit is called, at which point Main will return
// nil. If package ui fails to initialize, Main returns an appropriate
// error.
func Main(f func()) error {
errchan := make(chan error)
go start(errchan, f)
return <-errchan
}
func start(errchan chan error, f func()) {
2015-12-11 21:48:25 -06:00
runtime.LockOSThread()
// TODO HEAP SAFETY
opts := C.uiInitOptions{}
estr := C.uiInit(&opts)
if estr != nil {
2015-12-11 20:03:38 -06:00
errchan <- errors.New(C.GoString(estr))
2015-12-11 21:48:25 -06:00
C.uiFreeInitError(estr)
2015-12-11 20:03:38 -06:00
return
}
QueueMain(f)
2015-12-11 21:48:25 -06:00
C.uiMain()
2015-12-11 20:03:38 -06:00
errchan <- nil
}
// Quit queues an exit from the GUI thread. It does not exit the
// program. Quit must be called from the GUI thread.
func Quit() {
2015-12-11 21:48:25 -06:00
C.uiQuit()
2015-12-11 20:03:38 -06:00
}
2015-12-11 21:48:25 -06:00
// These prevent the passing of Go functions into C land.
// TODO make an actual sparse list instead of this monotonic map thingy
var (
qmmap = make(map[uintptr]func())
qmcurrent = uintptr(0)
qmlock sync.Mutex
)
2015-12-11 20:03:38 -06:00
// QueueMain queues f to be executed on the GUI thread when
// next possible. It returns immediately; that is, it does not wait
// for the function to actually be executed. QueueMain is the only
// function that can be called from other goroutines, and its
// primary purpose is to allow communication between other
// goroutines and the GUI thread. Calling QueueMain after Quit
// has been called results in undefined behavior.
func QueueMain(f func()) {
2015-12-11 21:48:25 -06:00
qmlock.Lock()
defer qmlock.Unlock()
n := qmcurrent
qmcurrent++
qmmap[n] = f
C.realQueueMain(unsafe.Pointer(n))
2015-12-11 20:03:38 -06:00
}
2015-12-11 21:48:25 -06:00
//export doQueued
func doQueued(nn unsafe.Pointer) {
qmlock.Lock()
n := uintptr(nn)
f := qmmap[n]
delete(qmmap, n)
// allow uiQueueMain() to be called by a queued function
// TODO explicitly allow this in libui too
qmlock.Unlock()
2015-12-11 20:03:38 -06:00
f()
}