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
|
|
|
|
|
|
|
"git.wit.org/wit/gui/toolkit"
|
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
|
|
|
|
//
|
|
|
|
Callback func(chan toolkit.Action)
|
|
|
|
|
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
|
|
|
//
|
2023-04-07 21:22:51 -05:00
|
|
|
pluginChan chan toolkit.Action
|
2023-04-07 21:54:57 -05:00
|
|
|
PluginChannel func() chan toolkit.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 {
|
|
|
|
log(logInfo, "initPlugin() START")
|
2022-11-13 08:53:03 -06:00
|
|
|
|
|
|
|
for _, aplug := range allPlugins {
|
2023-04-23 09:47:54 -05:00
|
|
|
log(debugGui, "initPlugin() already loaded toolkit plugin =", aplug.name)
|
2022-11-13 08:53:03 -06:00
|
|
|
if (aplug.name == name) {
|
2023-04-23 09:47:54 -05:00
|
|
|
log(debugError, "initPlugin() SKIPPING", name, "as you can't load it twice")
|
|
|
|
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")
|
|
|
|
func getPluginChannel(p *aplug, funcName string) func() chan toolkit.Action {
|
|
|
|
var newfunc func() chan toolkit.Action
|
|
|
|
var ok bool
|
|
|
|
var test plugin.Symbol
|
|
|
|
|
|
|
|
test, err = p.plug.Lookup(funcName)
|
|
|
|
if err != nil {
|
|
|
|
log(debugGui, "DID NOT FIND: name =", test, "err =", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newfunc, ok = test.(func() chan toolkit.Action)
|
|
|
|
if !ok {
|
|
|
|
log(debugGui, "function name =", funcName, "names didn't map correctly. Fix the plugin name =", p.name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return newfunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendCallback(p *aplug, funcName string) func(chan toolkit.Action) {
|
2023-04-06 18:00:18 -05:00
|
|
|
var newfunc func(chan toolkit.Action)
|
|
|
|
var ok bool
|
|
|
|
var test plugin.Symbol
|
|
|
|
|
|
|
|
test, err = p.plug.Lookup(funcName)
|
|
|
|
if err != nil {
|
|
|
|
log(debugGui, "DID NOT FIND: name =", test, "err =", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newfunc, ok = test.(func(chan toolkit.Action))
|
|
|
|
if !ok {
|
|
|
|
log(debugGui, "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/
|
|
|
|
~/go/src/go.wit.org/gui/toolkit/
|
|
|
|
/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"
|
2023-04-28 08:14:51 -05:00
|
|
|
log(logError, "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 {
|
|
|
|
log(logError, filename, "was not embedded. Error:", err)
|
|
|
|
}
|
2022-11-13 08:53:03 -06:00
|
|
|
|
|
|
|
// attempt to write out the file from the internal resource
|
2023-04-23 16:00:38 -05:00
|
|
|
filename = "toolkit/" + 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 {
|
|
|
|
log(logError, "searchPaths() error. exiting here?")
|
|
|
|
} else {
|
|
|
|
filename = homeDir + "/go/src/git.wit.org/wit/gui/toolkit/" + 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
|
|
|
}
|
2022-11-06 19:57:20 -06:00
|
|
|
|
2023-04-23 16:00:38 -05:00
|
|
|
filename = "/usr/lib/go-gui/" + 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
|
|
|
|
|
|
|
filename = "/usr/local/lib/" + 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 {
|
2022-11-13 08:53:03 -06:00
|
|
|
plug, err := plugin.Open(filename)
|
|
|
|
if err != nil {
|
2023-02-25 14:05:25 -06:00
|
|
|
log(debugGui, "plugin FAILED =", filename, err)
|
2022-11-13 08:53:03 -06:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-28 07:29:46 -05:00
|
|
|
log(debugGui, "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) {
|
2023-04-28 07:29:46 -05:00
|
|
|
log(debugError, "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
|
|
|
|
2023-04-28 07:29:46 -05:00
|
|
|
log(debugPlugin, "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-05-09 19:24:37 -05:00
|
|
|
// 2023/05/09 pretty clean
|
|
|
|
// 2023/04/06 Queue() is also being used and channels are being used. memcopy() only
|
2023-05-09 08:25:10 -05:00
|
|
|
func newAction(n *Node, atype toolkit.ActionType) *toolkit.Action {
|
|
|
|
var a toolkit.Action
|
|
|
|
a.ActionType = atype
|
|
|
|
if (n == nil) {
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
a.Name = n.Name
|
|
|
|
a.Text = n.Text
|
|
|
|
a.WidgetId = n.id
|
2023-05-09 19:04:39 -05:00
|
|
|
|
2023-05-09 18:50:16 -05:00
|
|
|
a.B = n.B
|
2023-05-09 19:04:39 -05:00
|
|
|
a.I = n.I
|
|
|
|
a.S = n.S
|
|
|
|
|
2023-05-09 18:50:16 -05:00
|
|
|
a.X = n.X
|
|
|
|
a.Y = n.Y
|
2023-05-09 19:04:39 -05:00
|
|
|
|
|
|
|
a.AtW = n.AtW
|
|
|
|
a.AtH = n.AtH
|
|
|
|
|
2023-05-09 18:34:09 -05:00
|
|
|
if (n.parent != nil) {
|
|
|
|
a.ParentId = n.parent.id
|
|
|
|
}
|
2023-05-09 08:25:10 -05:00
|
|
|
a.WidgetType = n.WidgetType
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
2023-05-09 19:04:39 -05:00
|
|
|
// sends the action/event to each toolkit via a golang plugin channel
|
2023-05-09 18:53:31 -05:00
|
|
|
func sendAction(a *toolkit.Action) {
|
2023-05-09 18:50:16 -05:00
|
|
|
for _, aplug := range allPlugins {
|
|
|
|
log(debugPlugin, "Action() aplug =", aplug.name, "Action type=", a.ActionType)
|
|
|
|
if (aplug.pluginChan == nil) {
|
|
|
|
log(logInfo, "Action() retrieving the aplug.PluginChannel()", aplug.name)
|
|
|
|
aplug.pluginChan = aplug.PluginChannel()
|
|
|
|
log(logInfo, "Action() retrieved", aplug.pluginChan)
|
|
|
|
}
|
|
|
|
log(logInfo, "Action() SEND to pluginChan", aplug.name)
|
|
|
|
aplug.pluginChan <- *a
|
|
|
|
// added during debugging. might be a good idea in general for a tactile experience
|
|
|
|
sleep(.02)
|
|
|
|
}
|
2023-05-09 08:25:10 -05: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 {
|
|
|
|
log(logInfo, "LoadToolkitEmbed() already loaded toolkit plugin =", aplug.name)
|
|
|
|
if (aplug.name == name) {
|
|
|
|
log(logError, "LoadToolkitEmbed() SKIPPING", name, "as you can't load it twice")
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.CreateTemp("", "sample." + name + ".so")
|
|
|
|
if (err != nil) {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
f.Write(b)
|
|
|
|
|
|
|
|
p := initToolkit(name, f.Name())
|
|
|
|
if (p == nil) {
|
|
|
|
log(logError, "LoadToolkitEmbed() embedded go file failed", name)
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) ListToolkits() {
|
|
|
|
for _, aplug := range allPlugins {
|
|
|
|
log(logNow, "ListToolkits() already loaded toolkit plugin =", aplug.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) LoadToolkit(name string) *Node {
|
|
|
|
log(logInfo, "LoadToolkit() START for name =", name)
|
|
|
|
plug := initPlugin(name)
|
|
|
|
if (plug == nil) {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
log(logInfo, "LoadToolkit() sending InitToolkit action to the plugin channel")
|
|
|
|
var a toolkit.Action
|
|
|
|
a.ActionType = toolkit.InitToolkit
|
|
|
|
plug.pluginChan <- a
|
|
|
|
sleep(.5) // temp hack until chan communication is setup
|
|
|
|
|
|
|
|
// TODO: find a new way to do this that is locking, safe and accurate
|
|
|
|
me.rootNode.redraw(plug)
|
|
|
|
log(logInfo, "LoadToolkit() END for name =", name)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) CloseToolkit(name string) bool {
|
|
|
|
log(logInfo, "CloseToolkit() for name =", name)
|
|
|
|
for _, plug := range allPlugins {
|
|
|
|
log(debugGui, "CloseToolkit() found", plug.name)
|
|
|
|
if (plug.name == name) {
|
|
|
|
log(debugNow, "CloseToolkit() sending close", name)
|
|
|
|
var a toolkit.Action
|
|
|
|
a.ActionType = toolkit.CloseToolkit
|
|
|
|
plug.pluginChan <- a
|
|
|
|
sleep(.5)
|
|
|
|
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
|
|
|
}
|