2022-11-06 19:57:20 -06:00
|
|
|
package gui
|
|
|
|
|
2022-11-09 08:38:50 -06:00
|
|
|
// This is based off of the excellent example and documentation here:
|
|
|
|
// https://github.com/vladimirvivien/go-plugin-example
|
|
|
|
// There truly are great people in this world.
|
|
|
|
// It's a pleasure to be here with all of you
|
|
|
|
|
2022-11-06 19:57:20 -06:00
|
|
|
import (
|
|
|
|
"os"
|
2023-04-28 15:34:55 -05:00
|
|
|
"embed"
|
2022-11-06 19:57:20 -06:00
|
|
|
"plugin"
|
2022-11-13 08:53:03 -06:00
|
|
|
|
2024-01-03 18:15:54 -06:00
|
|
|
"go.wit.com/log"
|
2024-01-05 13:18:44 -06:00
|
|
|
"go.wit.com/gui/widget"
|
2022-11-06 19:57:20 -06:00
|
|
|
)
|
|
|
|
|
2022-11-13 08:53:03 -06:00
|
|
|
var err error
|
|
|
|
type Symbol any
|
|
|
|
|
|
|
|
type aplug struct {
|
|
|
|
name string
|
|
|
|
filename string
|
|
|
|
plug *plugin.Plugin
|
|
|
|
|
2023-05-09 19:39:56 -05:00
|
|
|
// this tells the toolkit plugin how to send events
|
|
|
|
// back here
|
|
|
|
//
|
|
|
|
// This is how we are passed information like a user clicking a button
|
2023-04-07 21:22:51 -05:00
|
|
|
// or a user changing a dropdown menu or a checkbox
|
|
|
|
//
|
2023-05-09 19:39:56 -05:00
|
|
|
// From this channel, the information is then passed into the main program
|
2023-04-07 21:22:51 -05:00
|
|
|
// Custom() function
|
|
|
|
//
|
2024-01-05 13:18:44 -06:00
|
|
|
Callback func(chan widget.Action)
|
2023-04-07 21:22:51 -05:00
|
|
|
|
2023-05-09 19:39:56 -05:00
|
|
|
// This is how actions are sent to the toolkit.
|
|
|
|
// For example:
|
|
|
|
// If a program is using GTK, when a program tries to make a new button
|
|
|
|
// "Open GIMP", then it would pass an action via this channel into the toolkit
|
|
|
|
// plugin and the toolkit plugin would add a button to the parent widget
|
2023-04-07 21:22:51 -05:00
|
|
|
//
|
|
|
|
// each toolkit has it's own goroutine and each one is sent this
|
|
|
|
// add button request
|
2023-05-09 19:39:56 -05:00
|
|
|
//
|
2024-01-05 13:18:44 -06:00
|
|
|
pluginChan chan widget.Action
|
|
|
|
PluginChannel func() chan widget.Action
|
2022-11-06 19:57:20 -06:00
|
|
|
}
|
|
|
|
|
2022-11-13 08:53:03 -06:00
|
|
|
var allPlugins []*aplug
|
|
|
|
|
|
|
|
// loads and initializes a toolkit (andlabs/ui, gocui, etc)
|
2023-04-12 13:00:29 -05:00
|
|
|
// attempts to locate the .so file
|
2023-04-23 09:47:54 -05:00
|
|
|
func initPlugin(name string) *aplug {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "initPlugin() START")
|
2022-11-13 08:53:03 -06:00
|
|
|
|
|
|
|
for _, aplug := range allPlugins {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "initPlugin() already loaded toolkit plugin =", aplug.name)
|
2022-11-13 08:53:03 -06:00
|
|
|
if (aplug.name == name) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("initPlugin() SKIPPING", name, "as you can't load it twice")
|
2023-04-23 09:47:54 -05:00
|
|
|
return nil
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:00:38 -05:00
|
|
|
return searchPaths(name)
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
|
|
|
|
2023-04-07 21:54:57 -05:00
|
|
|
// newPlug.PluginChannel = getPluginChannel(newPlug, "PluginChannel")
|
2024-01-05 13:18:44 -06:00
|
|
|
func getPluginChannel(p *aplug, funcName string) func() chan widget.Action {
|
|
|
|
var newfunc func() chan widget.Action
|
2023-04-07 21:54:57 -05:00
|
|
|
var ok bool
|
|
|
|
var test plugin.Symbol
|
|
|
|
|
|
|
|
test, err = p.plug.Lookup(funcName)
|
|
|
|
if err != nil {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Error(err, "DID NOT FIND: name =", test)
|
2023-04-07 21:54:57 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 13:18:44 -06:00
|
|
|
newfunc, ok = test.(func() chan widget.Action)
|
2023-04-07 21:54:57 -05:00
|
|
|
if !ok {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "function name =", funcName, "names didn't map correctly. Fix the plugin name =", p.name)
|
2023-04-07 21:54:57 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return newfunc
|
|
|
|
}
|
|
|
|
|
2024-01-05 13:18:44 -06:00
|
|
|
func sendCallback(p *aplug, funcName string) func(chan widget.Action) {
|
|
|
|
var newfunc func(chan widget.Action)
|
2023-04-06 18:00:18 -05:00
|
|
|
var ok bool
|
|
|
|
var test plugin.Symbol
|
|
|
|
|
|
|
|
test, err = p.plug.Lookup(funcName)
|
|
|
|
if err != nil {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Error(err, "DID NOT FIND: name =", test)
|
2023-04-06 18:00:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 13:18:44 -06:00
|
|
|
newfunc, ok = test.(func(chan widget.Action))
|
2023-04-06 18:00:18 -05:00
|
|
|
if !ok {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "function name =", funcName, "names didn't map correctly. Fix the plugin name =", p.name)
|
2022-11-13 08:53:03 -06:00
|
|
|
return nil
|
2022-11-06 19:57:20 -06:00
|
|
|
}
|
2022-11-13 08:53:03 -06:00
|
|
|
return newfunc
|
2022-11-06 19:57:20 -06:00
|
|
|
}
|
|
|
|
|
2023-02-25 14:05:25 -06:00
|
|
|
/*
|
2023-04-23 16:00:38 -05:00
|
|
|
TODO: clean this up. use command args?
|
2023-04-24 06:22:14 -05:00
|
|
|
TODO: use LD_LIBRARY_PATH ?
|
2023-02-25 14:05:25 -06:00
|
|
|
This searches in the following order for the plugin .so files:
|
|
|
|
./toolkit/
|
2023-12-20 14:15:00 -06:00
|
|
|
~/go/src/go.wit.com/gui/toolkit/
|
2023-02-25 14:05:25 -06:00
|
|
|
/usr/lib/go-gui/
|
|
|
|
*/
|
2023-04-23 16:00:38 -05:00
|
|
|
func searchPaths(name string) *aplug {
|
2022-11-13 08:53:03 -06:00
|
|
|
var filename string
|
2023-04-28 08:14:51 -05:00
|
|
|
var pfile []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// first try to load the embedded plugin file
|
|
|
|
filename = "plugins/" + name + ".so"
|
2023-04-28 10:35:57 -05:00
|
|
|
pfile, err = me.resFS.ReadFile(filename)
|
2023-04-28 08:14:51 -05:00
|
|
|
if (err == nil) {
|
2023-12-02 19:02:51 -06:00
|
|
|
filename = "/tmp/" + name + ".so"
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Error(err, "write out file here", name, filename, len(pfile))
|
2023-12-02 19:02:51 -06:00
|
|
|
f, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
f.Write(pfile)
|
|
|
|
f.Close()
|
|
|
|
p := initToolkit(name, filename)
|
|
|
|
if (p != nil) {
|
|
|
|
return p
|
|
|
|
}
|
2023-04-28 08:14:51 -05:00
|
|
|
} else {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Error(err, filename, "was not embedded in the binary")
|
2023-04-28 08:14:51 -05:00
|
|
|
}
|
2022-11-13 08:53:03 -06:00
|
|
|
|
|
|
|
// attempt to write out the file from the internal resource
|
2024-01-03 20:34:10 -06:00
|
|
|
filename = "toolkits/" + name + ".so"
|
2023-04-28 07:29:46 -05:00
|
|
|
p := initToolkit(name, filename)
|
2023-04-23 16:00:38 -05:00
|
|
|
if (p != nil) {
|
|
|
|
return p
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
|
|
|
|
2023-04-23 16:00:38 -05:00
|
|
|
homeDir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
2024-01-03 20:34:10 -06:00
|
|
|
log.Error(err, "os.UserHomeDir() error. giving up")
|
|
|
|
return nil
|
2024-01-01 15:43:50 -06:00
|
|
|
}
|
|
|
|
|
2024-01-03 20:34:10 -06:00
|
|
|
filename = homeDir + "/go/src/go.wit.com/gui/toolkits/" + name + ".so"
|
|
|
|
p = initToolkit(name, filename)
|
|
|
|
if (p != nil) {
|
|
|
|
return p
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
2022-11-06 19:57:20 -06:00
|
|
|
|
2024-01-03 20:34:10 -06:00
|
|
|
filename = "/usr/lib/go-gui/latest/" + name + ".so"
|
2023-04-28 07:29:46 -05:00
|
|
|
p = initToolkit(name, filename)
|
2023-04-23 16:00:38 -05:00
|
|
|
if (p != nil) {
|
|
|
|
return p
|
2022-11-06 19:57:20 -06:00
|
|
|
}
|
2023-04-24 06:22:14 -05:00
|
|
|
|
2024-01-03 20:34:10 -06:00
|
|
|
filename = "/usr/local/lib/gui/toolkits/" + name + ".so"
|
2023-04-28 07:29:46 -05:00
|
|
|
p = initToolkit(name, filename)
|
2023-04-24 06:22:14 -05:00
|
|
|
if (p != nil) {
|
|
|
|
return p
|
|
|
|
}
|
2023-04-23 16:00:38 -05:00
|
|
|
return nil
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// load module
|
|
|
|
// 1. open the shared object file to load the symbols
|
2023-04-28 07:29:46 -05:00
|
|
|
func initToolkit(name string, filename string) *aplug {
|
2023-12-03 16:08:39 -06:00
|
|
|
if _, err := os.Stat(filename); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2024-01-08 22:27:17 -06:00
|
|
|
log.Log(PLUG, "missing plugin", name, "as filename", filename)
|
2023-12-03 16:08:39 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-01-08 22:27:17 -06:00
|
|
|
log.Log(PLUG, "Found plugin", name, "as filename", filename)
|
2023-12-03 16:08:39 -06:00
|
|
|
|
2022-11-13 08:53:03 -06:00
|
|
|
plug, err := plugin.Open(filename)
|
|
|
|
if err != nil {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Error(err, "plugin FAILED =", filename)
|
2022-11-13 08:53:03 -06:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "initToolkit() loading plugin =", filename)
|
2023-04-23 16:00:38 -05:00
|
|
|
|
|
|
|
var newPlug *aplug
|
|
|
|
newPlug = new(aplug)
|
|
|
|
newPlug.name = name
|
|
|
|
newPlug.filename = filename
|
|
|
|
newPlug.plug = plug
|
|
|
|
|
|
|
|
// this tells the toolkit plugin how to send user events back to us
|
|
|
|
// for things like: the user clicked on the 'Check IPv6'
|
|
|
|
newPlug.Callback = sendCallback(newPlug, "Callback")
|
|
|
|
|
|
|
|
// this let's us know where to send requests to the toolkit
|
|
|
|
// for things like: add a new button called 'Check IPv6'
|
|
|
|
newPlug.PluginChannel = getPluginChannel(newPlug, "PluginChannel")
|
|
|
|
|
2023-04-24 06:22:14 -05:00
|
|
|
// add it to the list of plugins
|
2023-04-23 16:00:38 -05:00
|
|
|
allPlugins = append(allPlugins, newPlug)
|
|
|
|
|
|
|
|
|
|
|
|
// set the communication to the plugins
|
|
|
|
newPlug.pluginChan = newPlug.PluginChannel()
|
2023-04-24 06:22:14 -05:00
|
|
|
if (newPlug.pluginChan == nil) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("initToolkit() ERROR PluginChannel() returned nil for plugin:", newPlug.name, filename)
|
2023-04-24 06:22:14 -05:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-28 10:35:57 -05:00
|
|
|
newPlug.Callback(me.guiChan)
|
2023-04-23 16:00:38 -05:00
|
|
|
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "initToolkit() END", newPlug.name, filename)
|
2023-04-23 16:00:38 -05:00
|
|
|
return newPlug
|
2022-11-06 19:57:20 -06:00
|
|
|
}
|
2023-03-01 11:35:36 -06:00
|
|
|
|
2023-04-28 15:34:55 -05:00
|
|
|
func (n *Node) InitEmbed(resFS embed.FS) *Node {
|
|
|
|
me.resFS = resFS
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) LoadToolkitEmbed(name string, b []byte) *Node {
|
|
|
|
for _, aplug := range allPlugins {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("LoadToolkitEmbed() already loaded toolkit plugin =", aplug.name)
|
2023-04-28 15:34:55 -05:00
|
|
|
if (aplug.name == name) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("LoadToolkitEmbed() SKIPPING", name, "as you can't load it twice")
|
2023-04-28 15:34:55 -05:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.CreateTemp("", "sample." + name + ".so")
|
|
|
|
if (err != nil) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Error(err, "LoadToolkitEmbed() SKIPPING", name, "as you can't load it twice")
|
2023-04-28 15:34:55 -05:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
f.Write(b)
|
|
|
|
|
|
|
|
p := initToolkit(name, f.Name())
|
|
|
|
if (p == nil) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("LoadToolkitEmbed() embedded go file failed", name)
|
2023-04-28 15:34:55 -05:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) ListToolkits() {
|
|
|
|
for _, aplug := range allPlugins {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "ListToolkits() already loaded toolkit plugin =", aplug.name)
|
2023-04-28 15:34:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) LoadToolkit(name string) *Node {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "LoadToolkit() START for name =", name)
|
2023-04-28 15:34:55 -05:00
|
|
|
plug := initPlugin(name)
|
|
|
|
if (plug == nil) {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "LoadToolkit() sending InitToolkit action to the plugin channel")
|
2024-01-05 13:18:44 -06:00
|
|
|
var a widget.Action
|
|
|
|
a.ActionType = widget.InitToolkit
|
2023-04-28 15:34:55 -05:00
|
|
|
plug.pluginChan <- a
|
2023-12-14 10:36:56 -06:00
|
|
|
// sleep(.5) // temp hack until chan communication is setup
|
2023-04-28 15:34:55 -05:00
|
|
|
|
|
|
|
// TODO: find a new way to do this that is locking, safe and accurate
|
|
|
|
me.rootNode.redraw(plug)
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "LoadToolkit() END for name =", name)
|
2023-04-28 15:34:55 -05:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) CloseToolkit(name string) bool {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "CloseToolkit() for name =", name)
|
2023-04-28 15:34:55 -05:00
|
|
|
for _, plug := range allPlugins {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "CloseToolkit() found", plug.name)
|
2023-04-28 15:34:55 -05:00
|
|
|
if (plug.name == name) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(PLUG, "CloseToolkit() sending close", name)
|
2024-01-05 13:18:44 -06:00
|
|
|
var a widget.Action
|
|
|
|
a.ActionType = widget.CloseToolkit
|
2023-04-28 15:34:55 -05:00
|
|
|
plug.pluginChan <- a
|
2023-12-14 10:36:56 -06:00
|
|
|
// sleep(.5) // is this needed? TODO: properly close channel
|
2023-04-28 15:34:55 -05:00
|
|
|
return true
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|
|
|
|
}
|
2023-04-28 15:34:55 -05:00
|
|
|
return false
|
2023-03-23 12:35:12 -05:00
|
|
|
}
|