diff --git a/deck.go b/deck.go index ea04e63..c4a3f98 100644 --- a/deck.go +++ b/deck.go @@ -12,6 +12,7 @@ import ( "time" "github.com/atotto/clipboard" + "github.com/fsnotify/fsnotify" "github.com/godbus/dbus" "github.com/muesli/streamdeck" ) @@ -24,11 +25,12 @@ type Deck struct { } // 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 } + currentDeck = path fmt.Println("Loading deck:", path) dc, err := LoadConfig(path) @@ -70,6 +72,32 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) { d.Widgets = append(d.Widgets, w) } + watcher, err := fsnotify.NewWatcher() + err = watcher.Add(path) + + go func() { + for { + select { + case event := <-watcher.Events: + if currentDeck == path { + fmt.Printf("Change: %s: %s\n", event.Op, event.Name) + d, err := LoadDeck(dev, base, deckName) + if err != nil { + fatal(err) + } + err = dev.Clear() + if err != nil { + fatal(err) + } + + deck = d + deck.updateWidgets() + return + } + } + } + }() + return &d, nil } diff --git a/go.mod b/go.mod index f16fd49..019e535 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/atotto/clipboard v0.1.4 github.com/bendahl/uinput v1.4.1 github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b + github.com/fsnotify/fsnotify v1.4.7 github.com/go-ole/go-ole v1.2.4 // indirect github.com/godbus/dbus v4.1.0+incompatible github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 diff --git a/go.sum b/go.sum index 3f3578c..bbf994f 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b h1:/wqXgpZNTP8qV1dPEApjJXlDQd5N/F9U/WEvy5SawUI= github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b/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/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= diff --git a/main.go b/main.go index 0a1facf..597aabc 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,8 @@ import ( ) var ( - deck *Deck + deck *Deck + currentDeck string dbusConn *dbus.Conn keyboard uinput.Keyboard