Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for automatic reloading the config when it has been changed (based on #50) #69

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/atotto/clipboard"
"github.com/fsnotify/fsnotify"
"github.com/godbus/dbus"
"github.com/muesli/streamdeck"
)
Expand All @@ -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
}
Expand Down Expand Up @@ -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() {
marcusramberg marked this conversation as resolved.
Show resolved Hide resolved
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
}

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down