go-ncurses/ncurses.go

80 lines
2.2 KiB
Go

package ncurses
import (
"fmt"
"sync/atomic"
"github.com/ebitengine/purego"
)
var ncurses atomic.Value
func Init() error {
if ncurses.Load() == nil {
lib, err := purego.Dlopen(LIBRARY_PATH, purego.RTLD_GLOBAL | purego.RTLD_LAZY)
if err != nil {
return err
}
ncurses.CompareAndSwap(nil, lib)
}
return nil
}
type loaded_function[T any] struct {
name string
value atomic.Value
library *atomic.Value
}
func NewLoadedFunction[T any](library *atomic.Value, name string) loaded_function[T] {
return loaded_function[T]{
name: name,
value: atomic.Value{},
library: library,
}
}
func(f *loaded_function[T]) Load() T {
if f.library.Load() == nil {
panic(fmt.Sprintf("Attempted to call library function %s before loading library", f.name))
} else if f.value.Load() == nil {
var tmp T
purego.RegisterLibFunc(&tmp, f.library.Load().(uintptr), f.name)
f.value.CompareAndSwap(nil, tmp)
}
return f.value.Load().(T)
}
type Window uintptr
type voidFP func() int
type winFP func(window Window) int
type initscrFP func() Window
var InitScr = NewLoadedFunction[initscrFP](&ncurses, "initscr")
var EndWin = NewLoadedFunction[voidFP](&ncurses, "endwin")
type newwinFP func(nlines, ncols, begin_x, begin_y int) Window
var NewWin = NewLoadedFunction[newwinFP](&ncurses, "newwin")
var DelWin = NewLoadedFunction[winFP](&ncurses, "delwin")
type dupwinFP func(window Window) Window
var DupWin = NewLoadedFunction[dupwinFP](&ncurses, "dupwin")
var Refresh = NewLoadedFunction[voidFP](&ncurses, "refresh")
var WRefresh = NewLoadedFunction[winFP](&ncurses, "wrefresh")
var WNOutRefresh = NewLoadedFunction[winFP](&ncurses, "wnoutrefresh")
var DoUpdate = NewLoadedFunction[voidFP](&ncurses, "doupdate")
type waddstrFP func(window Window, str string) int
var WAddStr = NewLoadedFunction[waddstrFP](&ncurses, "waddstr")
type waddnstrFP func(window Window, str string, n int) int
var WAddNStr = NewLoadedFunction[waddstrFP](&ncurses, "waddnstr")
var GetMaxY = NewLoadedFunction[winFP](&ncurses, "getmaxy")
var GetMaxX = NewLoadedFunction[winFP](&ncurses, "getmaxx")
type mvwaddstrFP func(window Window, x, y int, str string) int
var MvWAddStr = NewLoadedFunction[mvwaddstrFP](&ncurses, "mvwaddstr")