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
)
2018-08-26 09:19:10 -05:00
// #include "pkgui.h"
2015-12-11 20:03:38 -06:00
import "C"
2018-08-11 15:10:47 -05:00
// make sure main() runs on the first thread created by the OS
// if main() calls Main(), things will just work on macOS, where the first thread created by the OS is the only thread allowed to be the main GUI thread
// we might as well lock the OS thread for the other platforms here too (though on those it doesn't matter *which* thread we lock to)
2018-08-11 15:12:54 -05:00
// TODO describe the source of this trick
2018-08-11 15:10:47 -05:00
func init ( ) {
runtime . LockOSThread ( )
}
2015-12-11 20:03:38 -06:00
// 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 {
2018-08-26 09:19:10 -05:00
opts := C . pkguiAllocInitOptions ( )
estr := C . uiInit ( opts )
C . pkguiFreeInitOptions ( opts )
2015-12-11 21:48:25 -06:00
if estr != nil {
2018-08-11 15:10:47 -05:00
err := errors . New ( C . GoString ( estr ) )
2015-12-11 21:48:25 -06:00
C . uiFreeInitError ( estr )
2018-08-11 15:10:47 -05:00
return err
2015-12-11 20:03:38 -06:00
}
2018-08-26 09:19:10 -05:00
C . pkguiOnShouldQuit ( )
2015-12-11 20:03:38 -06:00
QueueMain ( f )
2015-12-11 21:48:25 -06:00
C . uiMain ( )
2018-08-11 15:10:47 -05:00
return nil
2015-12-11 20:03:38 -06:00
}
2015-12-12 12:37:36 -06:00
// Quit queues a return from Main. It does not exit the program.
// It also does not immediately cause Main to return; Main will
// return when it next can. Quit must be called from the GUI thread.
2015-12-11 20:03:38 -06:00
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.
2018-03-27 19:19:27 -05:00
//
// If you start a goroutine in f, it also cannot call package ui
// functions. So for instance, the following will result in
// undefined behavior:
//
// ui.QueueMain(func() {
// go ui.MsgBox(...)
// })
2015-12-11 20:03:38 -06:00
func QueueMain ( f func ( ) ) {
2015-12-11 21:48:25 -06:00
qmlock . Lock ( )
defer qmlock . Unlock ( )
2015-12-21 13:26:09 -06:00
n := uintptr ( 0 )
for {
n = qmcurrent
qmcurrent ++
if qmmap [ n ] == nil {
break
}
}
2015-12-11 21:48:25 -06:00
qmmap [ n ] = f
2018-08-26 09:19:10 -05:00
C . pkguiQueueMain ( C . uintptr_t ( n ) )
2015-12-11 20:03:38 -06:00
}
2018-08-26 09:19:10 -05:00
//export pkguiDoQueueMain
func pkguiDoQueueMain ( nn unsafe . Pointer ) {
2015-12-11 21:48:25 -06:00
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 ( )
}
2015-12-12 11:01:31 -06:00
// no need to lock this; this API is only safe on the main thread
var shouldQuitFunc func ( ) bool
// OnShouldQuit schedules f to be exeucted when the OS wants
// the program to quit or when a Quit menu item has been clicked.
// Only one function may be registered at a time. If the function
// returns true, Quit will be called. If the function returns false, or
// if OnShouldQuit is never called. Quit will not be called and the
// OS will be told that the program needs to continue running.
func OnShouldQuit ( f func ( ) bool ) {
shouldQuitFunc = f
}
2018-08-26 09:19:10 -05:00
//export pkguiDoOnShouldQuit
func pkguiDoOnShouldQuit ( unused unsafe . Pointer ) C . int {
2015-12-12 11:01:31 -06:00
if shouldQuitFunc == nil {
return 0
}
2015-12-12 12:07:57 -06:00
return frombool ( shouldQuitFunc ( ) )
2015-12-12 11:01:31 -06:00
}
2018-08-12 08:17:01 -05:00
// TODO Timer?