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) #1

Closed
wants to merge 1 commit 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
42 changes: 40 additions & 2 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/atotto/clipboard"
"github.com/fsnotify/fsnotify"
"github.com/godbus/dbus"
"github.com/muesli/streamdeck"
)
Expand All @@ -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)
Expand Down Expand Up @@ -70,6 +72,42 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
d.Widgets = append(d.Widgets, w)
}

watcher, err := fsnotify.NewWatcher()
if err == nil {
err = watcher.Add(path)
if err == nil {

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
}
case error := <-watcher.Errors:
fmt.Printf("Watcher had an error: %s\n", error)
}
}
}()
} else {
fmt.Printf("Failed to watch deck, automatic reloading diabled: %s\n", err)
}
} else {
fmt.Printf("Failed to initialize fsnotify, automatic reloading diabled: %s\n", err)
}

return &d, nil
}

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/atotto/clipboard v0.1.4
github.com/bendahl/uinput v1.4.1
github.com/flopp/go-findfont v0.1.0
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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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.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/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

var (
deck *Deck
deck *Deck
currentDeck string

dbusConn *dbus.Conn
keyboard uinput.Keyboard
Expand Down