diff --git a/internal/radio/playlist.go b/internal/radio/playlist.go index e4791fc..173badc 100644 --- a/internal/radio/playlist.go +++ b/internal/radio/playlist.go @@ -1,9 +1,12 @@ package radio import ( + "dwelling-radio/pkg/watcher" + "log" "os" "strings" "sync" + "syscall" ) // Playlist holds a list of paths to a song files. @@ -13,6 +16,8 @@ type Playlist struct { playlist []string cur int repeat bool + watcher *watcher.InotifyWatcher + changed chan uint32 } // NewPlaylist returns an instance of a Playlist struct with a loaded playlist. @@ -58,3 +63,34 @@ func (p *Playlist) load() error { func (p *Playlist) Reload() error { return p.load() } + +func (p *Playlist) Watch() (err error) { + p.watcher, err = watcher.NewInotifyWatcher() + if err != nil { + return err + } + + err = p.watcher.AddWatch(p.filePath, watcher.ModIgnMask) + if err != nil { + return err + } + + p.watcher.WatchForMask(p.changed, watcher.ModIgnMask) + + go func() { + for { + mask := <-p.changed + + if mask&syscall.IN_MODIFY > 0 { + if err := p.load(); err != nil { + log.Fatalln("cannot reload a changed playlist:", err) + } + } else if mask&syscall.IN_IGNORED > 0 { + p.watcher.Close() + p.Watch() + } + } + }() + + return nil +}