summaryrefslogtreecommitdiff
path: root/toolkit/gocui/main.go
blob: 6214fcbe7e9291d174826610ca32966d625cfcdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"os"
	"git.wit.org/wit/gui/toolkit"
)

// sets defaults and establishes communication
// to this toolkit from the wit/gui golang package
func init() {
	log(logInfo, "Init() of awesome-gocui")

	// init the config struct default values
	Set(&me, "default")

	me.pluginChan = make(chan toolkit.Action)

	log(logNow, "Init() start pluginChan")
	go catchActionChannel()
	sleep(.1) // probably not needed, but in here for now under development
	go main()
	sleep(.1) // probably not needed, but in here for now under development
}

// this sets the channel to send user events back from the plugin
func Callback(guiCallback chan toolkit.Action) {
	me.callback = guiCallback
}

func PluginChannel() chan toolkit.Action {
	return me.pluginChan
}

/*
recieves requests from the program to do things like:
* add new widgets
* change the text of a label
* etc..
*/
func catchActionChannel() {
	log(logInfo, "catchActionChannel() START")
	for {
		log(logInfo, "catchActionChannel() infinite for() loop restarted select on channel")
	    	select {
		case a := <-me.pluginChan:
			if (me.baseGui == nil) {
				// something went wrong initializing the gocui
				log(logError,"ERROR: console did not initialize")
				continue
			}
			log(logNow, "catchActionChannel()", a.WidgetId, a.ActionType, a.WidgetType, a.Name)
			action(&a)
		}
	}
}

func Exit() {
	// TODO: what should actually happen here?
	standardExit()
}

func standardExit() {
	me.baseGui.Close()
	outf.Close()
	setOutput(os.Stdout)
	sendBackQuit()
	sleep(.5)
	exit()
}
func sendBackQuit() {
	// send 'Quit' back to the program (?)
	var a toolkit.Action
	a.ActionType = toolkit.UserQuit
	me.callback <- a
}

var outf *os.File

func main() {
	var err error
	log(logInfo, "main() start Init()")

	outf, err = os.OpenFile("/tmp/witgui.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
	if err != nil {
		exit("error opening file: %v", err)
	}
	os.Stdout = outf
	defer outf.Close()

	// setOutput(outf)
	// log("This is a test log entry")

	ferr, _ := os.OpenFile("/tmp/witgui.err", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
	os.Stderr = ferr
	gocuiMain()

	log(true, "MouseMain() closed")
	standardExit()
}