clean up swaybg processes
This commit is contained in:
		
							parent
							
								
									d87664a9f4
								
							
						
					
					
						commit
						3d5d38de51
					
				
							
								
								
									
										49
									
								
								config.go
								
								
								
								
							
							
						
						
									
										49
									
								
								config.go
								
								
								
								
							|  | @ -2,36 +2,73 @@ package main | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"github.com/adhocore/jsonc" | 	"fmt" | ||||||
| 	"log" | 	"log" | ||||||
| 	"os" | 	"os" | ||||||
|  | 
 | ||||||
|  | 	"github.com/adhocore/jsonc" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type Config struct { | type Config struct { | ||||||
| 	ImageFilters | 	ImageFilters ImageFilters | ||||||
| 	ImagesDir    string | 	ImagesDir    string | ||||||
| 	Duration     int | 	Duration     int | ||||||
| 	Cache        string | 	Cache        string | ||||||
|  | 	Default      string | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func makeDefaultConfig() Config { | ||||||
|  | 	ImageFilters := makeDefaultImageFilters() | ||||||
|  | 	return Config{ | ||||||
|  | 		ImageFilters: ImageFilters, | ||||||
|  | 		ImagesDir:    "bgs", | ||||||
|  | 		Duration:     1, | ||||||
|  | 		Cache:        ".gopaper", | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (config *Config) load() { | func (config *Config) load() { | ||||||
|  | 	j := jsonc.New() | ||||||
|  | 
 | ||||||
| 	homeDir, err := os.UserHomeDir() | 	homeDir, err := os.UserHomeDir() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatal("could not get home directory!", err) | 		log.Fatal("could not get home directory!", err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	configRaw, err := os.ReadFile(homeDir + "/.config/gopaper/config.jsonc") | 	configPath := homeDir + "/.config/gopaper/config.jsonc" | ||||||
|  | 	configRaw, err := os.ReadFile(configPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatal("Couldn't open config file!\n", err) | 		if !os.IsExist(err) { | ||||||
|  | 			defaultConfig := makeDefaultConfig() | ||||||
|  | 			defaultConfigJSON, err := json.MarshalIndent(defaultConfig, "", "") | ||||||
|  | 			if err != nil { | ||||||
|  | 				log.Fatal("could not encode default config!", err) | ||||||
| 			} | 			} | ||||||
| 	j := jsonc.New() | 			file, err := os.Create(configPath) | ||||||
|  | 			defer file.Close() | ||||||
|  | 			if err != nil { | ||||||
|  | 				log.Fatal("could not create config file!", err) | ||||||
|  | 			} | ||||||
|  | 			file.Write(defaultConfigJSON) | ||||||
|  | 			*config = defaultConfig | ||||||
|  | 		} | ||||||
|  | 	} else { | ||||||
|  | 
 | ||||||
| 		configRaw = j.Strip(configRaw) | 		configRaw = j.Strip(configRaw) | ||||||
| 		err = json.Unmarshal(configRaw, &config) | 		err = json.Unmarshal(configRaw, &config) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			log.Fatal("Couldn't unmarshal config!\n", err) | 			log.Fatal("Couldn't unmarshal config!\n", err) | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	config.Cache = homeDir + "/" + config.Cache + "/" | 	config.Cache = homeDir + "/" + config.Cache + "/" | ||||||
| 	//TODO: make directories if they don't
 | 	//make directory if it doesn't exist
 | ||||||
|  | 	_, err = os.Stat(config.Cache) | ||||||
|  | 	if os.IsNotExist(err) { | ||||||
|  | 		fmt.Printf("Cache directory %v not found. Making one.\n", config.Cache) | ||||||
|  | 		os.Mkdir(config.Cache, 0777) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	config.ImagesDir = homeDir + "/" + config.ImagesDir + "/" | 	config.ImagesDir = homeDir + "/" + config.ImagesDir + "/" | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -0,0 +1,10 @@ | ||||||
|  | //All directories are relative 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":1, | ||||||
|  | "cache":"slideshow", | ||||||
|  | "default":"default.jpg" | ||||||
|  | } | ||||||
							
								
								
									
										2
									
								
								files.go
								
								
								
								
							
							
						
						
									
										2
									
								
								files.go
								
								
								
								
							|  | @ -20,7 +20,7 @@ func getRandomFile(dir string) (path string, filename string, error error) { | ||||||
| 		randomImg = files[randomIndex] | 		randomImg = files[randomIndex] | ||||||
| 		//re-roll if you get a directory
 | 		//re-roll if you get a directory
 | ||||||
| 		if files[randomIndex].IsDir() { | 		if files[randomIndex].IsDir() { | ||||||
| 			fmt.Printf("%v is a directory\n", randomImg.Name()) | 			fmt.Printf("%v is a directory. Trying again... \n", randomImg.Name()) | ||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
| 		//re-roll if not a recognised image type
 | 		//re-roll if not a recognised image type
 | ||||||
|  |  | ||||||
							
								
								
									
										7
									
								
								go.mod
								
								
								
								
							
							
						
						
									
										7
									
								
								go.mod
								
								
								
								
							|  | @ -4,13 +4,12 @@ go 1.23.2 | ||||||
| 
 | 
 | ||||||
| require ( | require ( | ||||||
| 	github.com/disintegration/gift v1.2.1 | 	github.com/disintegration/gift v1.2.1 | ||||||
| 	github.com/reujab/wallpaper v0.0.0-20210630195606-5f9f655b3740 |  | ||||||
| 	golang.org/x/image v0.21.0 | 	golang.org/x/image v0.21.0 | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| require ( | require ( | ||||||
| 	github.com/adhocore/jsonc v0.10.0 | 	github.com/adhocore/jsonc v0.10.0 | ||||||
| 	golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a // indirect | 	github.com/crazy3lf/colorconv v1.2.0 | ||||||
| 	gopkg.in/ini.v1 v1.62.0 // indirect |  | ||||||
| 	gopkg.in/yaml.v2 v2.4.0 // indirect |  | ||||||
| ) | ) | ||||||
|  | 
 | ||||||
|  | require github.com/swaywm/swaybg v1.2.1 // indirect | ||||||
|  |  | ||||||
							
								
								
									
										21
									
								
								go.sum
								
								
								
								
							
							
						
						
									
										21
									
								
								go.sum
								
								
								
								
							|  | @ -1,23 +1,10 @@ | ||||||
| github.com/adhocore/jsonc v0.10.0 h1:YjNX9TojBfxQJ4kuoiNqVR5SFqu1YBEMsm+HxWnxbOI= | 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/adhocore/jsonc v0.10.0/go.mod h1:Ar4gd3i83+1Z+5M5SG6Vrfw9q3TO544OwLXH4+ZhWTE= | ||||||
|  | github.com/crazy3lf/colorconv v1.2.0 h1:UM7kSZWnwFMGiC+PpYrjxQSOd6sEyWb+dRKKTd3KslA= | ||||||
|  | github.com/crazy3lf/colorconv v1.2.0/go.mod h1:2jTJ7QCWCj2sSLOhF4Gzi0J5/hoX8/VY8VzNvXAlD1I= | ||||||
| github.com/disintegration/gift v1.2.1 h1:Y005a1X4Z7Uc+0gLpSAsKhWi4qLtsdEcMIbbdvdZ6pc= | github.com/disintegration/gift v1.2.1 h1:Y005a1X4Z7Uc+0gLpSAsKhWi4qLtsdEcMIbbdvdZ6pc= | ||||||
| github.com/disintegration/gift v1.2.1/go.mod h1:Jh2i7f7Q2BM7Ezno3PhfezbR1xpUg9dUg3/RlKGr4HI= | 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/swaywm/swaybg v1.2.1 h1:TvQYiifR9JW4dANxxb7HMbEaZdGnZ4CWynevENocIWU= | ||||||
| github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= | github.com/swaywm/swaybg v1.2.1/go.mod h1:Og8Sb6ebG/J9hqY2nL3xnhKY5sL30mQL+JOrY9owcJM= | ||||||
| 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 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s= | ||||||
| golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78= | 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= |  | ||||||
|  |  | ||||||
							
								
								
									
										24
									
								
								image.go
								
								
								
								
							
							
						
						
									
										24
									
								
								image.go
								
								
								
								
							|  | @ -17,9 +17,17 @@ import ( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type ImageFilters struct { | type ImageFilters struct { | ||||||
| 	Colorize [3]float32 | 	Colorize [3]float64 | ||||||
| 	Contrast int | 	Contrast float32 | ||||||
| 	Gamma    float64 | 	Gamma    float32 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func makeDefaultImageFilters() ImageFilters { | ||||||
|  | 	return ImageFilters{ | ||||||
|  | 		Colorize: [3]float64{247, 40, 60}, | ||||||
|  | 		Contrast: -35, | ||||||
|  | 		Gamma:    0.8, | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func pickRandomImage(dir string) (string, error) { | func pickRandomImage(dir string) (string, error) { | ||||||
|  | @ -77,12 +85,14 @@ func saveImage(filename string, img image.Image) error { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func processImage(img image.Image, config Config) image.Image { | func processImage(img image.Image, config Config) image.Image { | ||||||
|  | 	h32 := float32(config.ImageFilters.Colorize[0]) | ||||||
|  | 	s32 := float32(config.ImageFilters.Colorize[1]) | ||||||
|  | 	l32 := float32(config.ImageFilters.Colorize[2]) | ||||||
| 
 | 
 | ||||||
| 	cArgs := config.Colorize |  | ||||||
| 	g := gift.New( | 	g := gift.New( | ||||||
| 		gift.Colorize(cArgs[0], cArgs[1], cArgs[2]), | 		gift.Colorize(h32, s32, l32), | ||||||
| 		gift.Contrast(float32(config.Contrast)), | 		gift.Contrast(config.ImageFilters.Contrast), | ||||||
| 		gift.Gamma(float32(config.Gamma)), | 		gift.Gamma(config.ImageFilters.Gamma), | ||||||
| 	) | 	) | ||||||
| 
 | 
 | ||||||
| 	dst := image.NewRGBA(g.Bounds(img.Bounds())) | 	dst := image.NewRGBA(g.Bounds(img.Bounds())) | ||||||
|  |  | ||||||
							
								
								
									
										32
									
								
								slideshow.go
								
								
								
								
							
							
						
						
									
										32
									
								
								slideshow.go
								
								
								
								
							|  | @ -3,25 +3,25 @@ package main | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"log" | 	"log" | ||||||
|  | 	"os/exec" | ||||||
| 	"time" | 	"time" | ||||||
| 
 |  | ||||||
| 	"github.com/reujab/wallpaper" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| 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.Second) | ||||||
|  | 	//set a flat background color based on config defaults
 | ||||||
|  | 	// err = exec.Command("swaybg", "-i", img, "--mode", "fit").Run()
 | ||||||
|  | 	var cmd *exec.Cmd | ||||||
| 	go func() { | 	go func() { | ||||||
| 		for { | 		for { | ||||||
| 			img, err := pickRandomImage(dir) | 			if cmd != nil { | ||||||
|  | 				err := cmd.Process.Kill() | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| 				panic(err) | 					log.Fatal("could not kill process", err) | ||||||
| 				} | 				} | ||||||
| 			fmt.Printf("setting wallpaper: %v\n", img) |  | ||||||
| 			err = wallpaper.SetFromFile(img) |  | ||||||
| 			if err != nil { |  | ||||||
| 				log.Fatal("failed to set wallpaper!", err) |  | ||||||
| 			} | 			} | ||||||
|  | 			cmd = setRandomWallpaper(dir) | ||||||
| 			select { | 			select { | ||||||
| 			case dir = <-ch: | 			case dir = <-ch: | ||||||
| 				log.Println("directory set!") | 				log.Println("directory set!") | ||||||
|  | @ -32,3 +32,19 @@ func slideshow(ch <-chan string) { | ||||||
| 		} | 		} | ||||||
| 	}() | 	}() | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func setRandomWallpaper(dir string) *exec.Cmd { | ||||||
|  | 	img, err := pickRandomImage(dir) | ||||||
|  | 	if err != nil { | ||||||
|  | 		panic(err) | ||||||
|  | 	} | ||||||
|  | 	fmt.Printf("setting wallpaper: %v\n", img) | ||||||
|  | 
 | ||||||
|  | 	cmd := exec.Command("swaybg", "-i", img, "--mode", "fit") | ||||||
|  | 	err = cmd.Start() | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatal("failed to set wallpaper!", err) | ||||||
|  | 	} | ||||||
|  | 	go cmd.Wait() | ||||||
|  | 	return cmd | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue