Start building out robust UI code ; this update moves the fim slideshow advancing into the go code and keeps fim OFF stdin/out so keyboard input shouldn't affect it
All checks were successful
continuous-integration/drone/tag Build is passing
All checks were successful
continuous-integration/drone/tag Build is passing
This commit is contained in:
parent
255e647c5e
commit
5212949fb0
71
cmd/ui/ui.go
71
cmd/ui/ui.go
|
@ -1,17 +1,23 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CMD_FIM = "/usr/local/bin/pf-fim.sh"
|
||||||
|
SLIDESHOW_INTERVAL = 300 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// fim placeholder so we can operate on it when a signal is received
|
// fim placeholder so we can operate on it when a signal is received
|
||||||
var fim *exec.Cmd = nil
|
var fim *exec.Cmd = nil
|
||||||
|
@ -26,32 +32,59 @@ func main() {
|
||||||
for range sigs {
|
for range sigs {
|
||||||
if fim != nil { // Just in case someone lays on ctrl-c or similar during startup
|
if fim != nil { // Just in case someone lays on ctrl-c or similar during startup
|
||||||
if err := fim.Process.Kill(); err != nil {
|
if err := fim.Process.Kill(); err != nil {
|
||||||
log.Fatalf("failed to kill process: %s", err)
|
log.Fatalf("failed to kill fim : %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Forever run slideshow / show ui as the main program executed on a PiFrame
|
// Run slideshow
|
||||||
for {
|
fim = exec.Command(CMD_FIM)
|
||||||
// Run slideshow
|
|
||||||
fim = exec.Command("/usr/local/bin/pf-fim.sh")
|
// Setup stdin for fim to control slideshow
|
||||||
if err := fim.Run(); err != nil {
|
stdin, err := fim.StdinPipe()
|
||||||
// Unwrap the error a bit so we can find out if a signal killed fim or something else
|
if err != nil {
|
||||||
// An exit code of -1 means the program didn't exit in time or was terminated by a signal (per the docs)
|
log.Fatalf("Error getting fim stdin : %s", err)
|
||||||
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() != -1 {
|
}
|
||||||
log.Fatalf("Error : %s", err)
|
|
||||||
|
// Advance slideshow every interval as defined in const()
|
||||||
|
ticker := time.NewTicker(SLIDESHOW_INTERVAL)
|
||||||
|
stop_ticker := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
_, err = io.WriteString(stdin, "n")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error advancing slides : %s", err)
|
||||||
|
}
|
||||||
|
case <-stop_ticker:
|
||||||
|
ticker.Stop()
|
||||||
|
stdin.Close()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Run config UI when slideshow stops
|
// Run fim
|
||||||
app := tview.NewApplication()
|
if err := fim.Run(); err != nil {
|
||||||
frame := tview.NewFrame(tview.NewBox().SetBackgroundColor(tcell.ColorBlack)).
|
// Unwrap the error a bit so we can find out if a signal killed fim or something else
|
||||||
SetBorders(2, 2, 2, 2, 4, 4).
|
// An exit code of -1 means the program didn't exit in time or was terminated by a signal (per the docs)
|
||||||
AddText("PiFrame", true, tview.AlignCenter, tcell.ColorWhite).
|
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() != -1 {
|
||||||
AddText("Configuration Utility", true, tview.AlignCenter, tcell.ColorRed)
|
log.Fatalf("Error running fim : %s", err)
|
||||||
if err := app.SetRoot(frame, true).EnableMouse(true).Run(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop fim slideshow advancing go routine
|
||||||
|
close(stop_ticker)
|
||||||
|
|
||||||
|
// Run config UI when slideshow stops
|
||||||
|
app := tview.NewApplication()
|
||||||
|
frame := tview.NewFrame(tview.NewBox().SetBackgroundColor(tcell.ColorBlack)).
|
||||||
|
SetBorders(2, 2, 2, 2, 4, 4).
|
||||||
|
AddText("PiFrame", true, tview.AlignCenter, tcell.ColorWhite).
|
||||||
|
AddText("Configuration Utility", true, tview.AlignCenter, tcell.ColorRed)
|
||||||
|
if err := app.SetRoot(frame, true).EnableMouse(true).Run(); err != nil {
|
||||||
|
log.Fatalf("Error running UI : %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue