180 lines
4.2 KiB
Go
180 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/adrg/xdg"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"periph.io/x/host/v3"
|
|
|
|
"git.maze.io/go/harmon/config"
|
|
"git.maze.io/go/harmon/display"
|
|
_ "git.maze.io/go/harmon/display/all" // Load all display drivers
|
|
)
|
|
|
|
var (
|
|
configurationPath = append(makeConfigurationPath(), ".")
|
|
configurationFile string
|
|
configuration *config.Structure
|
|
logFile string
|
|
logDebug bool
|
|
logTrace bool
|
|
)
|
|
|
|
func main() {
|
|
var entrypoint = &cobra.Command{
|
|
Use: "harmon",
|
|
Short: "Harmon is a music player frontend for graphic displays",
|
|
Args: entrypointArgs,
|
|
Run: entrypoint,
|
|
}
|
|
|
|
flag := entrypoint.PersistentFlags()
|
|
flag.StringVarP(&configurationFile, "config", "c", "", "configuration file (default is look in "+strings.Join(configurationPath, ",")+")")
|
|
flag.BoolVarP(&logDebug, "debug", "D", false, "enable debug log messages")
|
|
flag.BoolVarP(&logTrace, "trace", "T", false, "enable trace log messages")
|
|
flag.StringVarP(&logFile, "log", "l", "", "log file (default: use standard output)")
|
|
entrypoint.MarkFlagRequired("config")
|
|
|
|
if err := entrypoint.Execute(); err != nil {
|
|
fatal(err)
|
|
}
|
|
}
|
|
|
|
func fatal(err error) {
|
|
fmt.Fprintf(os.Stderr, "Fatal error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func setup() {
|
|
viper.SetConfigFile("harmon.conf")
|
|
viper.AddConfigPath(".")
|
|
viper.SetConfigName(".harmon")
|
|
viper.SetDefault("license", "MIT")
|
|
}
|
|
|
|
func makeConfigurationPath() []string {
|
|
dirs := make([]string, len(xdg.ConfigDirs))
|
|
for i, dir := range xdg.ConfigDirs {
|
|
dirs[i] = filepath.Join(dir, "harmon")
|
|
}
|
|
return dirs
|
|
}
|
|
|
|
func entrypointArgs(cmd *cobra.Command, args []string) (err error) {
|
|
// Setup logger
|
|
if logFile != "" {
|
|
logrus.WithField("path", logFile).Info("logging to file")
|
|
var w io.Writer
|
|
if w, err = os.OpenFile(logFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0640); err != nil {
|
|
return
|
|
}
|
|
logrus.SetOutput(w)
|
|
}
|
|
if logTrace {
|
|
logrus.SetLevel(logrus.TraceLevel)
|
|
} else if logDebug {
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
}
|
|
|
|
// Load configuration
|
|
if configurationFile == "" {
|
|
for _, dir := range configurationPath {
|
|
if dir, err = filepath.Abs(dir); err != nil {
|
|
return
|
|
}
|
|
logrus.WithField("path", dir).Trace("looking for harmon.conf")
|
|
if i, ferr := os.Stat(filepath.Join(dir, "harmon.conf")); ferr == nil && !i.IsDir() {
|
|
if configurationFile, err = filepath.Abs(i.Name()); err != nil {
|
|
return
|
|
}
|
|
logrus.WithField("path", configurationFile).Trace("using for harmon.conf")
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if configuration, err = config.Load(configurationFile); err != nil {
|
|
if config.ExplainError(configurationFile, err) {
|
|
return errors.New("configuration failed")
|
|
}
|
|
return err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func entrypoint(cmd *cobra.Command, args []string) {
|
|
if len(configuration.Display) == 0 {
|
|
logrus.Fatal("no displays configured")
|
|
}
|
|
|
|
var (
|
|
displays = make(map[string]display.Display)
|
|
err error
|
|
)
|
|
|
|
teardown := func() {
|
|
for _, d := range displays {
|
|
logrus.WithField("display", d.String()).Debug("halting...")
|
|
if d != nil {
|
|
_ = d.Halt()
|
|
}
|
|
}
|
|
}
|
|
defer teardown()
|
|
|
|
// Configure host
|
|
if _, err = host.Init(); err != nil {
|
|
logrus.WithError(err).Fatal("host setup failed")
|
|
}
|
|
|
|
// Configure displays
|
|
for _, config := range configuration.Display {
|
|
var (
|
|
log = logrus.WithFields(logrus.Fields{
|
|
"name": config.Name,
|
|
"type": config.Type,
|
|
})
|
|
output display.Display
|
|
)
|
|
log.Info("setting up display")
|
|
if output, err = display.New(config); err != nil {
|
|
log.WithError(err).Fatal("display setup failed")
|
|
}
|
|
|
|
if err = display.DrawImageFile(output, "harmon-color.png"); err != nil {
|
|
logrus.
|
|
WithField("display", config.Name).
|
|
WithError(err).
|
|
Fatal("display splash failed")
|
|
}
|
|
if err = output.Refresh(); err != nil {
|
|
logrus.
|
|
WithField("display", config.Name).
|
|
WithError(err).
|
|
Fatal("display refresh failed")
|
|
}
|
|
|
|
displays[config.Name] = output
|
|
}
|
|
|
|
signals := make(chan os.Signal)
|
|
signal.Notify(signals, os.Interrupt)
|
|
signal.Notify(signals, os.Kill)
|
|
|
|
fmt.Println("idling...")
|
|
for {
|
|
s := <-signals
|
|
logrus.WithField("signal", s.String()).Info("terminating")
|
|
return
|
|
}
|
|
}
|