summaryrefslogtreecommitdiff
path: root/init.go
diff options
context:
space:
mode:
Diffstat (limited to 'init.go')
-rw-r--r--init.go170
1 files changed, 75 insertions, 95 deletions
diff --git a/init.go b/init.go
index 3adaf1b..646aee0 100644
--- a/init.go
+++ b/init.go
@@ -9,6 +9,7 @@ package main
import (
"errors"
+ "fmt"
"os"
"runtime"
"runtime/debug"
@@ -31,6 +32,21 @@ func toolkitClose() {
// sets defaults and establishes communication
// to this toolkit from the wit/gui golang package
func initPlugin() {
+ defer func() {
+ if r := recover(); r != nil {
+ fmt.Fprintf(outf, "PANIC: initPlugin() recovered %v\n", r)
+ return
+ }
+ }()
+
+ var err error
+ // todo: make this a tmp file that goes away
+ outf, err = os.OpenFile("/tmp/captureMode.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
+ if err != nil {
+ log.Info("error opening file:", err)
+ os.Exit(0)
+ }
+ me.starttime = time.Now()
log.Log(INFO, "Init() of awesome-gocui")
// init the config struct default values
@@ -86,16 +102,59 @@ func initPlugin() {
}
log.Log(NOW, "Init() start pluginChan")
- // log.Sleep(.1) // probably not needed, but in here for now under development
- go mainGogui()
- // log.Sleep(.1) // probably not needed, but in here for now under development
+
+ os.Stdout = outf
+
+ log.CaptureMode(outf)
+
+ // init gocui
+ g, err := gocui.NewGui(gocui.OutputNormal, true)
+ if err != nil {
+ return
+ }
+ me.baseGui = g
+ g.Cursor = true
+ g.Mouse = true
+
+ // this sets the function that is run on every event. For example:
+ // When you click the mouse, move the mouse, or press a key on the keyboard
+ // This is equivalent to xev or similar to cat /dev/input on linux
+ g.SetManagerFunc(gocuiEvent)
+
+ // register how the 'gocui' will work as a GO toolkit plugin
+ // all applications will use these keys. they are universal.
+ // registered event handlers still have the events sent to gocuiEvent() above
+ registerHandlers(g)
+
+ go gocuiMain()
+}
+
+// This goroutine sits in gocui's MainLoop()
+func gocuiMain() {
+ defer func() {
+ if r := recover(); r != nil {
+ log.Warn("PANIC ecovered in gocuiMain()", r)
+ fmt.Fprintf(outf, "PANIC recovered in r = %v", r)
+ os.Stderr = outf
+ os.Stdout = outf
+ debug.PrintStack()
+ panic(outf)
+ }
+ }()
+
+ // me.stdout.Write([]byte("begin gogui.MainLoop()\n"))
+ if err := me.baseGui.MainLoop(); err != nil && !errors.Is(err, gocui.ErrQuit) {
+ log.Log(NOW, "g.MainLoop() panic err =", err)
+ // normally panic here
+ panic("gocuiTKmainloop OOPS")
+ }
}
func standardExit() {
log.Log(NOW, "standardExit() doing baseGui.Close()")
me.baseGui.Close()
log.Log(NOW, "standardExit() doing outf.Close()")
- me.outf.Close()
+ outf.Close()
// log(true, "standardExit() setOutput(os.Stdout)")
// setOutput(os.Stdout)
log.Log(NOW, "standardExit() send back Quit()")
@@ -110,7 +169,7 @@ func standardClose() {
log.Log(NOW, "standardExit() doing baseGui.Close()")
me.baseGui.Close()
log.Log(NOW, "standardExit() doing outf.Close()")
- me.outf.Close()
+ outf.Close()
// os.Stdin = os.Stdin
// os.Stdout = os.Stdout
// os.Stderr = os.Stderr
@@ -120,101 +179,15 @@ func standardClose() {
func main() {
}
-var origStdout *os.File
-var origStderr *os.File
-
-func mainGogui() {
- defer func() {
- if r := recover(); r != nil {
- log.Warn("YAHOOOO Recovered in guiMain application:", r)
- log.Warn("Recovered from panic:", r)
- me.baseGui.Close()
- log.CaptureMode(nil)
- log.Warn("YAHOOOO Recovered in guiMain application:", r)
- log.Warn("Recovered from panic:", r)
- me.myTree.SendToolkitPanic()
-
- return
- }
- }()
-
- var err error
-
- // todo: make this a tmp file that goes away
- me.outf, err = os.OpenFile("/tmp/captureMode.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
- if err != nil {
- log.Info("error opening file:", err)
- os.Exit(0)
- }
- origStdout = os.Stdout
- os.Stdout = me.outf
- defer me.outf.Close()
-
- log.CaptureMode(me.outf)
-
- gocuiMain()
-}
-
-// This initializes the gocui package
-// it runs SetManagerFunc which passes every input
-// event (keyboard, mouse, etc) to the function "gocuiEvent()"
-func gocuiMain() {
+// this hack is to wait for the application to send something
+// before trying to do anything. todo: rethink this someday
+func waitOK() {
defer func() {
if r := recover(); r != nil {
- log.Warn("YAHOOOO Recovered in gocuiMain()", r)
- log.Warn("Recovered from panic:", r)
- me.baseGui.Close()
-
- // allow gocui to close if possible, then print stack
- log.Sleep(1)
- os.Stdout = origStdout
- os.Stderr = origStderr
- me.myTree.SendToolkitPanic()
- log.Warn("Stack trace:")
- debug.PrintStack()
- // panic("BUMMER 2")
-
- // attempt to switch to the nocui toolkit
- log.Sleep(1)
- me.myTree.SendToolkitLoad("nocui")
- log.Sleep(3)
- me.myTree.SendToolkitLoad("nocui")
- // panic("BUMMER")
+ fmt.Fprintln(outf, "INIT PLUGIN recovered in r", r)
return
}
}()
- g, err := gocui.NewGui(gocui.OutputNormal, true)
- if err != nil {
- return
- }
- defer g.Close()
-
- me.baseGui = g
-
- g.Cursor = true
- g.Mouse = true
-
- // this sets the function that is run on every event. For example:
- // When you click the mouse, move the mouse, or press a key on the keyboard
- // This is equivalent to xev or similar to cat /dev/input on linux
- g.SetManagerFunc(gocuiEvent)
-
- // register how the 'gocui' will work as a GO toolkit plugin
- // all applications will use these keys. they are universal.
- // registered event handlers still have the events sent to gocuiEvent() above
- registerHandlers(g)
-
- // me.stdout.Write([]byte("begin gogui.MainLoop()\n"))
- if err := g.MainLoop(); err != nil && !errors.Is(err, gocui.ErrQuit) {
- log.Log(NOW, "g.MainLoop() panic err =", err)
- // normally panic here
- panic("gocuiTKmainloop OOPS")
- }
-}
-
-// this hack is to wait for the application to send something
-// before trying to do anything. todo: rethink this someday
-func waitOK() {
for {
if me.ok {
return
@@ -231,6 +204,12 @@ func testRefresh(*gocui.Gui) error {
// refresh the screen 10 times a second
func refreshGocui() {
+ defer func() {
+ if r := recover(); r != nil {
+ fmt.Fprintln(outf, "INIT PLUGIN recovered in r", r)
+ return
+ }
+ }()
var lastRefresh time.Time
lastRefresh = time.Now()
for {
@@ -286,6 +265,7 @@ func newWindowTrigger() {
// log.Log(NOW, "newWindowTrigger() got new window", tk.cuiName)
time.Sleep(200 * time.Millisecond)
waitOK()
+ time.Sleep(200 * time.Millisecond)
redoWindows(3, 3)
if !me.stdout.init {
me.stdout.init = true