diff --git a/deck.go b/deck.go index a260afb..28e7db0 100644 --- a/deck.go +++ b/deck.go @@ -13,6 +13,7 @@ import ( "time" "github.com/atotto/clipboard" + "github.com/fsnotify/fsnotify" "github.com/godbus/dbus" "github.com/muesli/streamdeck" ) @@ -22,11 +23,12 @@ type Deck struct { File string Background image.Image Widgets []Widget + Watcher *fsnotify.Watcher } // LoadDeck loads a deck configuration. -func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) { - path, err := expandPath(base, deck) +func LoadDeck(dev *streamdeck.Device, base string, deckName string) (*Deck, error) { + path, err := expandPath(base, deckName) if err != nil { return nil, err } @@ -71,6 +73,48 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) { d.Widgets = append(d.Widgets, w) } + d.Watcher, err = fsnotify.NewWatcher() + if err == nil { + err = d.Watcher.Add(path) + if err == nil { + go func() { + for { + select { + case event, ok := <-d.Watcher.Events: + if !ok { + return + } + if d.File == path { + fmt.Printf("Change: %s: %s\n", event.Op, event.Name) + err = d.Watcher.Close() + if err != nil { + fatal(err) + } + d, err := LoadDeck(dev, base, deckName) + if err != nil { + fatal(err) + } + err = dev.Clear() + if err != nil { + fatal(err) + } + + deck = d + deck.updateWidgets() + return + } + case err := <-d.Watcher.Errors: + fmt.Fprintf(os.Stderr, "Watcher had an error: %s\n", err) + } + } + }() + } else { + fmt.Fprintf(os.Stderr, "Failed to watch deck, automatic reloading diabled: %s\n", err) + } + } else { + fmt.Fprintf(os.Stderr, "Failed to initialize fsnotify, automatic reloading diabled: %s\n", err) + } + return &d, nil } @@ -225,6 +269,10 @@ func (d *Deck) triggerAction(dev *streamdeck.Device, index uint8, hold bool) { } if a.Deck != "" { + err := d.Watcher.Close() + if err != nil { + fatal(err) + } d, err := LoadDeck(dev, filepath.Dir(d.File), a.Deck) if err != nil { fmt.Fprintln(os.Stderr, "Can't load deck:", err) diff --git a/go.mod b/go.mod index cea47cc..318bd7a 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/bendahl/uinput v1.5.0 github.com/davecgh/go-spew v1.1.1 // indirect github.com/flopp/go-findfont v0.1.0 + github.com/fsnotify/fsnotify v1.4.7 github.com/godbus/dbus v4.1.0+incompatible github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 diff --git a/go.sum b/go.sum index 31b48f1..7873e18 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU= github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=