make platform agnostic

This commit is contained in:
andrzej 2024-10-30 19:22:03 +01:00
parent 80a49ca59a
commit 3d8197d7ae
12 changed files with 77 additions and 89 deletions

View File

@ -2,24 +2,35 @@ package main
import (
"encoding/json"
"github.com/adhocore/jsonc"
"log"
"os"
)
type Config struct {
ImageFilters
Root string
ImagesDir string
Duration int
Cache string
}
func (config *Config) load() {
configRaw, err := os.ReadFile("./config.json")
configRaw, err := os.ReadFile("./config.jsonc")
if err != nil {
log.Fatal("Couldn't open config file!", err)
log.Fatal("Couldn't open config file!\n", err)
}
j := jsonc.New()
configRaw = j.Strip(configRaw)
err = json.Unmarshal(configRaw, &config)
if err != nil {
log.Fatal("Couldn't unmarshal config!", err)
log.Fatal("Couldn't unmarshal config!\n", err)
}
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal("could not get home directory!", err)
}
config.Cache = homeDir + "/" + config.Cache + "/"
//TODO: make directories if they don't
config.ImagesDir = homeDir + "/" + config.ImagesDir + "/"
}

View File

@ -1,8 +0,0 @@
{
"colorize":[ 247,40,60 ],
"contrast":-35,
"gamma":0.8,
"root":"/home/andrzej/bgs",
"duration":10
}

9
config.jsonc Normal file
View File

@ -0,0 +1,9 @@
//All directories are relevant to the home folder, and will be created if they do not exist
{
"colorize":[ 247,40,60 ],
"contrast":-35,
"gamma":0.8,
"imagesDir":"bgs",
"duration":10,
"cache":"slideshow" //this is where
}

View File

@ -29,12 +29,13 @@ func getRandomFile(dir string) (path string, filename string, error error) {
fmt.Printf("%v is not a recognised image format\n", randomImg.Name())
continue
}
//TODO: re-roll if you get the same image
return dir + "/" + randomImg.Name(), randomImg.Name(), nil
}
}
func fileExistsInCache(filename string) (bool, error) {
files, err := os.ReadDir("images")
files, err := os.ReadDir(config.Cache)
if err != nil {
return false, err
}

BIN
go-img

Binary file not shown.

8
go.mod
View File

@ -4,5 +4,13 @@ go 1.23.2
require (
github.com/disintegration/gift v1.2.1
github.com/reujab/wallpaper v0.0.0-20210630195606-5f9f655b3740
golang.org/x/image v0.21.0
)
require (
github.com/adhocore/jsonc v0.10.0
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

19
go.sum
View File

@ -1,4 +1,23 @@
github.com/adhocore/jsonc v0.10.0 h1:YjNX9TojBfxQJ4kuoiNqVR5SFqu1YBEMsm+HxWnxbOI=
github.com/adhocore/jsonc v0.10.0/go.mod h1:Ar4gd3i83+1Z+5M5SG6Vrfw9q3TO544OwLXH4+ZhWTE=
github.com/disintegration/gift v1.2.1 h1:Y005a1X4Z7Uc+0gLpSAsKhWi4qLtsdEcMIbbdvdZ6pc=
github.com/disintegration/gift v1.2.1/go.mod h1:Jh2i7f7Q2BM7Ezno3PhfezbR1xpUg9dUg3/RlKGr4HI=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/reujab/wallpaper v0.0.0-20210630195606-5f9f655b3740 h1:X6IDPPN+zrSClp0Q+JiERA//d8L0WcU5MqcGeulCW1A=
github.com/reujab/wallpaper v0.0.0-20210630195606-5f9f655b3740/go.mod h1:WYwPVmM/8szeItLeWkwZSLRvQgrvsvstRzgznR8+E4Q=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

View File

@ -1,55 +0,0 @@
package main
import (
"errors"
"log"
"os/exec"
)
func hyprpaperSet(path string) {
err := hyprpaperPreload(path)
if err != nil {
log.Fatal("preload failed!", err)
}
err = hyprpaperWallpaper(path)
if err != nil {
log.Fatal("set wallpaper failed!", err)
}
err = hyprpaperUnloadAll()
if err != nil {
log.Fatal("unload all failed!", err)
}
}
func hyprpaperPreload(path string) error {
out, err := exec.Command("hyprctl", "hyprpaper", "preload", path).Output()
if err != nil {
return err
}
return checkHyprctlError(out)
}
func hyprpaperWallpaper(path string) error {
out, err := exec.Command("hyprctl", "hyprpaper", "wallpaper", ",contain:"+path).Output()
if err != nil {
return err
}
return checkHyprctlError(out)
}
func hyprpaperUnloadAll() error {
out, err := exec.Command("hyprctl", "hyprpaper", "unload", "unused").Output()
if err != nil {
return err
}
return checkHyprctlError(out)
}
func checkHyprctlError(out []byte) error {
str := string(out)
if str != "ok\n" {
return errors.New(str)
}
return nil
}

View File

@ -23,11 +23,9 @@ type ImageFilters struct {
}
func pickRandomImage(dir string) (string, error) {
dir = config.Root + "/" + dir
wd, err := os.Getwd()
if err != nil {
return "", err
}
dir = config.ImagesDir + dir
fmt.Printf("getting random file from %v\n", dir)
randomImg, filename, _ := getRandomFile(dir)
filenameNoExt := strings.TrimSuffix(filename, filepath.Ext(filename))
@ -36,11 +34,11 @@ func pickRandomImage(dir string) (string, error) {
if err != nil {
return "", err
}
curr := wd + "/images/" + filenameNoExt
curr := config.Cache + filenameNoExt
if !fileExists {
img, err := loadImage(randomImg)
fmt.Printf("failed to load image %v", randomImg)
if err != nil {
fmt.Printf("failed to load image %v", randomImg)
return "", err
}
dst := processImage(img, config)

14
main.go
View File

@ -14,7 +14,12 @@ func main() {
config.load()
fmt.Printf("%+v\n", config)
//TODO: make this logic interface more neatly with the slideshow channel
var waitGroup sync.WaitGroup
waitGroup.Add(1)
slideshowDir := make(chan string)
go server(slideshowDir)
go slideshow(slideshowDir)
var dir string
args := os.Args[1:]
if len(args) > 0 {
@ -22,12 +27,7 @@ func main() {
} else {
dir = ""
}
var waitGroup sync.WaitGroup
waitGroup.Add(1)
slideshowDir := make(chan string)
go server(slideshowDir)
go slideshow(slideshowDir)
slideshowDir <- dir
waitGroup.Wait()
}

View File

@ -20,7 +20,6 @@ func server(slideshowDir chan string) {
log.Println("listening on unix domain socket...")
//cleanup sockfile
//TODO: this only works when the program is killed from outside. Make it work via defer() too?
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
@ -28,6 +27,7 @@ func server(slideshowDir chan string) {
os.Remove(sockfile)
os.Exit(1)
}()
defer os.Remove(sockfile)
for {
//Accept incoming
@ -36,8 +36,6 @@ func server(slideshowDir chan string) {
log.Fatal(err)
}
//create store for incoming bytes
//handle connection in separate goroutine
go func(conn net.Conn) {
defer conn.Close()

View File

@ -1,20 +1,27 @@
package main
import (
"fmt"
"log"
"time"
"github.com/reujab/wallpaper"
)
func slideshow(ch <-chan string) {
dir := <-ch
ticker := time.NewTicker(time.Duration(config.Duration) * time.Minute)
ticker := time.NewTicker(time.Duration(config.Duration) * time.Second)
go func() {
for {
img, err := pickRandomImage(dir)
if err != nil {
panic(err)
}
hyprpaperSet(img)
fmt.Printf("setting wallpaper: %v\n", img)
err = wallpaper.SetFromFile(img)
if err != nil {
log.Fatal("failed to set wallpaper!", err)
}
select {
case dir = <-ch:
log.Println("directory set!")