Skip to content

Commit 85ab5e9

Browse files
Nikos Verschoremarcusramberg
authored andcommitted
Add support for automatic reloading the config when it has been changed
1 parent f3b14b4 commit 85ab5e9

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

deck.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/atotto/clipboard"
16+
"github.com/fsnotify/fsnotify"
1617
"github.com/godbus/dbus"
1718
"github.com/muesli/streamdeck"
1819
)
@@ -25,8 +26,8 @@ type Deck struct {
2526
}
2627

2728
// LoadDeck loads a deck configuration.
28-
func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
29-
path, err := expandPath(base, deck)
29+
func LoadDeck(dev *streamdeck.Device, base string, deckName string) (*Deck, error) {
30+
path, err := expandPath(base, deckName)
3031
if err != nil {
3132
return nil, err
3233
}
@@ -71,6 +72,42 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
7172
d.Widgets = append(d.Widgets, w)
7273
}
7374

75+
watcher, err := fsnotify.NewWatcher()
76+
if err == nil {
77+
err = watcher.Add(path)
78+
if err == nil {
79+
80+
go func() {
81+
for {
82+
select {
83+
case event := <-watcher.Events:
84+
if currentDeck == path {
85+
fmt.Printf("Change: %s: %s\n", event.Op, event.Name)
86+
d, err := LoadDeck(dev, base, deckName)
87+
if err != nil {
88+
fatal(err)
89+
}
90+
err = dev.Clear()
91+
if err != nil {
92+
fatal(err)
93+
}
94+
95+
deck = d
96+
deck.updateWidgets()
97+
return
98+
}
99+
case error := <-watcher.Errors:
100+
fmt.Printf("Watcher had an error: %s\n", error)
101+
}
102+
}
103+
}()
104+
} else {
105+
fmt.Printf("Failed to watch deck, automatic reloading diabled: %s\n", err)
106+
}
107+
} else {
108+
fmt.Printf("Failed to initialize fsnotify, automatic reloading diabled: %s\n", err)
109+
}
110+
74111
return &d, nil
75112
}
76113

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/bendahl/uinput v1.5.0
1111
github.com/davecgh/go-spew v1.1.1 // indirect
1212
github.com/flopp/go-findfont v0.1.0
13+
github.com/fsnotify/fsnotify v1.4.7
1314
github.com/godbus/dbus v4.1.0+incompatible
1415
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
1516
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1313
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1414
github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU=
1515
github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
16+
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
17+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
1618
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
1719
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
1820
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=

0 commit comments

Comments
 (0)