diff --git a/cmd/ui/ui.go b/cmd/ui/ui.go index c340748..f80fd42 100644 --- a/cmd/ui/ui.go +++ b/cmd/ui/ui.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io" "log" "os/exec" @@ -22,34 +21,6 @@ func main() { // fim placeholder so we can operate on it when a exit slideshow is received var fim *exec.Cmd = nil - // Start watching for key strokes and echo them back to stdout - keysEvents, err := keyboard.GetKeys(10) - if err != nil { - panic(err) - } - defer func() { - _ = keyboard.Close() - }() - - fmt.Println("Press ESC to kill slideshow") - go func() { - for { - event := <-keysEvents - if event.Err != nil { - panic(event.Err) - } - fmt.Printf("You pressed: rune %q, key %X\r\n", event.Rune, event.Key) - if event.Key == keyboard.KeyEsc { - if fim != nil { // Just in case someone lays on exit key or similar during startup - if err := fim.Process.Kill(); err != nil { - log.Fatalf("failed to kill fim : %s", err) - } - } - break - } - } - }() - // Run slideshow fim = exec.Command(CMD_FIM) @@ -78,6 +49,55 @@ func main() { } }() + // Start watching for key strokes and echo them back to stdout + keysEvents, err := keyboard.GetKeys(10) + if err != nil { + panic(err) + } + defer func() { + _ = keyboard.Close() + }() + + // Goroutine for tracking which keys are pressed and controlling fim if appropriate + go func() { + for { + event := <-keysEvents + if event.Err != nil { + panic(event.Err) + } + log.Printf("You pressed: key %X\n", event.Key) + + // Keys for fim event management (previous/next in particular) + fimKey := "" + if event.Key == keyboard.KeyArrowLeft || event.Key == keyboard.KeyArrowDown { + fimKey = "p" + } + if event.Key == keyboard.KeyArrowRight || event.Key == keyboard.KeyArrowUp { + fimKey = "n" + } + + // Exit fim and move to the config UI + if event.Key == keyboard.KeyEsc || event.Key == keyboard.KeyEnter || event.Key == keyboard.KeySpace { + if fim != nil { // Just in case someone lays on exit key or similar during startup + if err := fim.Process.Kill(); err != nil { + log.Fatalf("failed to kill fim : %s", err) + } + } + break + } + + // Control fim if we received a valid key for next/previous slide + if fimKey != "" { + log.Printf("Sending key to fim : %s", fimKey) + _, err = io.WriteString(stdin, fimKey) + if err != nil { + log.Fatalf("Error controlling fim : %s", err) + } + ticker.Reset(SLIDESHOW_INTERVAL) + } + } + }() + // Run fim if err := fim.Run(); err != nil { // Unwrap the error a bit so we can find out if a signal killed fim or something else