diff --git a/ncurses.go b/ncurses.go index c4889be..c857db1 100644 --- a/ncurses.go +++ b/ncurses.go @@ -1,7 +1,6 @@ package ncurses import ( - "fmt" "sync/atomic" "github.com/ebitengine/purego" @@ -9,41 +8,18 @@ import ( var ncurses atomic.Value -func Init() error { +func ncursesFunction[T any](name string) T { if ncurses.Load() == nil { lib, err := purego.Dlopen(LIBRARY_PATH, purego.RTLD_GLOBAL | purego.RTLD_LAZY) if err != nil { - return err + panic(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) + var tmp = new(T) + purego.RegisterLibFunc(tmp, ncurses.Load().(uintptr), name) + return *tmp } type Window uintptr @@ -52,28 +28,28 @@ 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") +var InitScr = ncursesFunction[initscrFP]("initscr") +var EndWin = ncursesFunction[voidFP]("endwin") type newwinFP func(nlines, ncols, begin_x, begin_y int) Window -var NewWin = NewLoadedFunction[newwinFP](&ncurses, "newwin") -var DelWin = NewLoadedFunction[winFP](&ncurses, "delwin") +var NewWin = ncursesFunction[newwinFP]("newwin") +var DelWin = ncursesFunction[winFP]("delwin") type dupwinFP func(window Window) Window -var DupWin = NewLoadedFunction[dupwinFP](&ncurses, "dupwin") +var DupWin = ncursesFunction[dupwinFP]("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") +var Refresh = ncursesFunction[voidFP]("refresh") +var WRefresh = ncursesFunction[winFP]("wrefresh") +var WNOutRefresh = ncursesFunction[winFP]("wnoutrefresh") +var DoUpdate = ncursesFunction[voidFP]("doupdate") type waddstrFP func(window Window, str string) int -var WAddStr = NewLoadedFunction[waddstrFP](&ncurses, "waddstr") +var WAddStr = ncursesFunction[waddstrFP]("waddstr") type waddnstrFP func(window Window, str string, n int) int -var WAddNStr = NewLoadedFunction[waddstrFP](&ncurses, "waddnstr") +var WAddNStr = ncursesFunction[waddstrFP]("waddnstr") -var GetMaxY = NewLoadedFunction[winFP](&ncurses, "getmaxy") -var GetMaxX = NewLoadedFunction[winFP](&ncurses, "getmaxx") +var GetMaxY = ncursesFunction[winFP]("getmaxy") +var GetMaxX = ncursesFunction[winFP]("getmaxx") type mvwaddstrFP func(window Window, x, y int, str string) int -var MvWAddStr = NewLoadedFunction[mvwaddstrFP](&ncurses, "mvwaddstr") +var MvWAddStr = ncursesFunction[mvwaddstrFP]("mvwaddstr")