background fill (introduces race condition!)

This commit is contained in:
andrzej 2024-10-31 15:18:04 +01:00
parent 3d5d38de51
commit 91f87397b0
1 changed files with 18 additions and 4 deletions

View File

@ -9,19 +9,33 @@ import (
func slideshow(ch <-chan string) { func slideshow(ch <-chan string) {
dir := <-ch dir := <-ch
ticker := time.NewTicker(time.Duration(config.Duration) * time.Second) ticker := time.NewTicker(time.Duration(config.Duration) * time.Minute)
//set a flat background color based on config defaults //set a flat background color based on config defaults
// err = exec.Command("swaybg", "-i", img, "--mode", "fit").Run() fillReady := make(chan bool)
go func() {
fillCmd := exec.Command("swaybg", "-c", "#232136")
err := fillCmd.Start()
if err != nil {
fmt.Println("could not set background color", err)
}
fmt.Println("bg color set!")
fillReady <- true
fillCmd.Wait()
}()
var cmd *exec.Cmd var cmd *exec.Cmd
go func() { go func() {
for { for {
<-fillReady
newCmd := setRandomWallpaper(dir)
if cmd != nil { if cmd != nil {
err := cmd.Process.Kill() err := cmd.Process.Kill()
if err != nil { if err != nil {
log.Fatal("could not kill process", err) log.Fatal("could not kill process", err)
} }
} }
cmd = setRandomWallpaper(dir) cmd = newCmd
select { select {
case dir = <-ch: case dir = <-ch:
log.Println("directory set!") log.Println("directory set!")
@ -38,9 +52,9 @@ func setRandomWallpaper(dir string) *exec.Cmd {
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Printf("setting wallpaper: %v\n", img)
cmd := exec.Command("swaybg", "-i", img, "--mode", "fit") cmd := exec.Command("swaybg", "-i", img, "--mode", "fit")
fmt.Printf("setting wallpaper: %v\n", img)
err = cmd.Start() err = cmd.Start()
if err != nil { if err != nil {
log.Fatal("failed to set wallpaper!", err) log.Fatal("failed to set wallpaper!", err)