Skip to content

Commit 8905acb

Browse files
Nikos Verschoremarcusramberg
authored andcommitted
Add support for automatic reloading the config when it has been changed
1 parent 7e11327 commit 8905acb

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

deck.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/atotto/clipboard"
15+
"github.com/fsnotify/fsnotify"
1516
"github.com/godbus/dbus"
1617
"github.com/muesli/streamdeck"
1718
)
@@ -24,11 +25,12 @@ type Deck struct {
2425
}
2526

2627
// LoadDeck loads a deck configuration.
27-
func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
28-
path, err := expandPath(base, deck)
28+
func LoadDeck(dev *streamdeck.Device, base string, deckName string) (*Deck, error) {
29+
path, err := expandPath(base, deckName)
2930
if err != nil {
3031
return nil, err
3132
}
33+
currentDeck = path
3234
fmt.Println("Loading deck:", path)
3335

3436
dc, err := LoadConfig(path)
@@ -70,6 +72,42 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
7072
d.Widgets = append(d.Widgets, w)
7173
}
7274

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+
73111
return &d, nil
74112
}
75113

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/atotto/clipboard v0.1.4
1111
github.com/bendahl/uinput v1.4.1
1212
github.com/flopp/go-findfont v0.1.0
13+
github.com/fsnotify/fsnotify v1.4.7
1314
github.com/go-ole/go-ole v1.2.4 // indirect
1415
github.com/godbus/dbus v4.1.0+incompatible
1516
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
3232
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
3333
github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU=
3434
github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
35+
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
3536
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
3637
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
3738
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
)
1919

2020
var (
21-
deck *Deck
21+
deck *Deck
22+
currentDeck string
2223

2324
dbusConn *dbus.Conn
2425
keyboard uinput.Keyboard

0 commit comments

Comments
 (0)