2024-10-28 23:29:20 +00:00
|
|
|
package main
|
|
|
|
|
2024-10-29 15:18:21 +00:00
|
|
|
import (
|
2024-11-03 08:34:45 +00:00
|
|
|
// "bufio"
|
2024-10-30 18:22:03 +00:00
|
|
|
"fmt"
|
2024-10-29 15:18:21 +00:00
|
|
|
"log"
|
2024-10-31 11:54:39 +00:00
|
|
|
"os/exec"
|
2024-10-29 15:18:21 +00:00
|
|
|
"time"
|
|
|
|
)
|
2024-10-28 23:29:20 +00:00
|
|
|
|
2024-10-29 15:18:21 +00:00
|
|
|
func slideshow(ch <-chan string) {
|
|
|
|
dir := <-ch
|
2024-10-31 14:18:04 +00:00
|
|
|
ticker := time.NewTicker(time.Duration(config.Duration) * time.Minute)
|
|
|
|
|
2024-10-31 11:54:39 +00:00
|
|
|
//set a flat background color based on config defaults
|
2024-10-31 15:09:18 +00:00
|
|
|
fillCmd := exec.Command("swaybg", "-c", "#232136")
|
2024-11-03 08:34:45 +00:00
|
|
|
// stdout, _ := fillCmd.StdoutPipe()
|
|
|
|
// scanner := bufio.NewScanner(stdout)
|
|
|
|
// out := make(chan string)
|
|
|
|
// go func() {
|
|
|
|
// for scanner.Scan() {
|
|
|
|
// fmt.Println("scanner: ", scanner.Text())
|
|
|
|
// out <- scanner.Text()
|
|
|
|
// }
|
|
|
|
// }()
|
2024-10-31 15:09:18 +00:00
|
|
|
|
|
|
|
ready := make(chan bool)
|
2024-10-31 14:18:04 +00:00
|
|
|
go func() {
|
|
|
|
err := fillCmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("could not set background color", err)
|
|
|
|
}
|
2024-11-03 08:34:45 +00:00
|
|
|
//NOTE: this is *very* hacky, but it is difficult to know when swaybg has initiated
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2024-10-31 15:09:18 +00:00
|
|
|
ready <- true
|
2024-10-31 14:18:04 +00:00
|
|
|
fmt.Println("bg color set!")
|
|
|
|
fillCmd.Wait()
|
|
|
|
}()
|
|
|
|
|
2024-10-31 11:54:39 +00:00
|
|
|
var cmd *exec.Cmd
|
2024-10-29 15:18:21 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2024-11-03 08:34:45 +00:00
|
|
|
<-ready
|
2024-10-31 14:18:04 +00:00
|
|
|
newCmd := setRandomWallpaper(dir)
|
2024-10-31 11:54:39 +00:00
|
|
|
if cmd != nil {
|
|
|
|
err := cmd.Process.Kill()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("could not kill process", err)
|
|
|
|
}
|
2024-10-30 18:22:03 +00:00
|
|
|
}
|
2024-10-31 14:18:04 +00:00
|
|
|
cmd = newCmd
|
2024-10-29 15:18:21 +00:00
|
|
|
select {
|
|
|
|
case dir = <-ch:
|
|
|
|
log.Println("directory set!")
|
|
|
|
continue
|
|
|
|
case <-ticker.C:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2024-10-28 23:29:20 +00:00
|
|
|
}
|
2024-10-31 11:54:39 +00:00
|
|
|
|
|
|
|
func setRandomWallpaper(dir string) *exec.Cmd {
|
|
|
|
img, err := pickRandomImage(dir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("swaybg", "-i", img, "--mode", "fit")
|
2024-10-31 14:18:04 +00:00
|
|
|
fmt.Printf("setting wallpaper: %v\n", img)
|
2024-10-31 11:54:39 +00:00
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("failed to set wallpaper!", err)
|
|
|
|
}
|
|
|
|
go cmd.Wait()
|
|
|
|
return cmd
|
|
|
|
}
|