Compare commits
	
		
			No commits in common. "9b4acc1dca6bdb89449b6dbe48377788c89db42c" and "568dc2b8d4ddeeaa1422c3aa28eb5688dc922b8e" have entirely different histories.
		
	
	
		
			9b4acc1dca
			...
			568dc2b8d4
		
	
		
							
								
								
									
										33
									
								
								config.go
								
								
								
								
							
							
						
						
									
										33
									
								
								config.go
								
								
								
								
							| 
						 | 
					@ -27,42 +27,27 @@ func makeDefaultConfig() Config {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (config *Config) load() error {
 | 
					func (config *Config) load() {
 | 
				
			||||||
	j := jsonc.New()
 | 
						j := jsonc.New()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	homeDir, err := os.UserHomeDir()
 | 
						homeDir, err := os.UserHomeDir()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Println("could not get home directory!")
 | 
							log.Fatal("could not get home directory!", err)
 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	configDir := homeDir + "/.config/gopaper/"
 | 
						configPath := homeDir + "/.config/gopaper/config.jsonc"
 | 
				
			||||||
	configRaw, err := os.ReadFile(configDir)
 | 
						configRaw, err := os.ReadFile(configPath)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		if !os.IsExist(err) {
 | 
							if !os.IsExist(err) {
 | 
				
			||||||
			defaultConfig := makeDefaultConfig()
 | 
								defaultConfig := makeDefaultConfig()
 | 
				
			||||||
			defaultConfigJSON, err := json.MarshalIndent(defaultConfig, "", "")
 | 
								defaultConfigJSON, err := json.MarshalIndent(defaultConfig, "", "")
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Println("could not encode default config!")
 | 
									log.Fatal("could not encode default config!", err)
 | 
				
			||||||
				return err
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			//TODO: make parent directories if they don't exist
 | 
								file, err := os.Create(configPath)
 | 
				
			||||||
			err = os.Mkdir(homeDir+".config", 755)
 | 
					 | 
				
			||||||
			if err != nil && !os.IsExist(err) {
 | 
					 | 
				
			||||||
				log.Println("could not create .config directory")
 | 
					 | 
				
			||||||
				return err
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			err = os.Mkdir(configDir, 755)
 | 
					 | 
				
			||||||
			if err != nil && !os.IsExist(err) {
 | 
					 | 
				
			||||||
				log.Println("could not create .config/gopaper directory")
 | 
					 | 
				
			||||||
				return err
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			file, err := os.Create(configDir + "config.jsonc")
 | 
					 | 
				
			||||||
			defer file.Close()
 | 
								defer file.Close()
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Println("could not create config file!")
 | 
									log.Fatal("could not create config file!", err)
 | 
				
			||||||
				return err
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			file.Write(defaultConfigJSON)
 | 
								file.Write(defaultConfigJSON)
 | 
				
			||||||
			*config = defaultConfig
 | 
								*config = defaultConfig
 | 
				
			||||||
| 
						 | 
					@ -72,8 +57,7 @@ func (config *Config) load() error {
 | 
				
			||||||
		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.Println("Couldn't unmarshal config!")
 | 
								log.Fatal("Couldn't unmarshal config!\n", err)
 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -87,5 +71,4 @@ func (config *Config) load() error {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	config.ImagesDir = homeDir + "/" + config.ImagesDir + "/"
 | 
						config.ImagesDir = homeDir + "/" + config.ImagesDir + "/"
 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										23
									
								
								main.go
								
								
								
								
							
							
						
						
									
										23
									
								
								main.go
								
								
								
								
							| 
						 | 
					@ -1,32 +1,22 @@
 | 
				
			||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"log"
 | 
						"fmt"
 | 
				
			||||||
 | 
						// "log"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var config Config
 | 
					var config Config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const sockfile = "/tmp/gopaper.sock"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	//TODO:kill existing processes
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	log.SetFlags(log.LstdFlags | log.Lshortfile)
 | 
						config.load()
 | 
				
			||||||
 | 
						fmt.Printf("%+v\n", config)
 | 
				
			||||||
	err := config.load()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		log.Println("failed to load config", err)
 | 
					 | 
				
			||||||
		cleanExit(1)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	log.Printf("%+v\n", config)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var waitGroup sync.WaitGroup
 | 
						var waitGroup sync.WaitGroup
 | 
				
			||||||
	waitGroup.Add(1)
 | 
						waitGroup.Add(1)
 | 
				
			||||||
	slideshowDir := make(chan string)
 | 
						slideshowDir := make(chan string)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	go server(slideshowDir)
 | 
						go server(slideshowDir)
 | 
				
			||||||
	go slideshow(slideshowDir)
 | 
						go slideshow(slideshowDir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -41,8 +31,3 @@ func main() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	waitGroup.Wait()
 | 
						waitGroup.Wait()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
func cleanExit(code int) {
 | 
					 | 
				
			||||||
	os.Remove(sockfile)
 | 
					 | 
				
			||||||
	os.Exit(code)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,8 +11,7 @@ import (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func server(slideshowDir chan string) {
 | 
					func server(slideshowDir chan string) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	defer os.Remove(sockfile)
 | 
						sockfile := "/tmp/bg-go.sock"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	//Create a Unix domain socket and listen for incoming connections.
 | 
						//Create a Unix domain socket and listen for incoming connections.
 | 
				
			||||||
	socket, err := net.Listen("unix", sockfile)
 | 
						socket, err := net.Listen("unix", sockfile)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
| 
						 | 
					@ -28,6 +27,7 @@ func server(slideshowDir chan string) {
 | 
				
			||||||
		os.Remove(sockfile)
 | 
							os.Remove(sockfile)
 | 
				
			||||||
		os.Exit(1)
 | 
							os.Exit(1)
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
						defer os.Remove(sockfile)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
		//Accept incoming
 | 
							//Accept incoming
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										20
									
								
								slideshow.go
								
								
								
								
							
							
						
						
									
										20
									
								
								slideshow.go
								
								
								
								
							| 
						 | 
					@ -3,7 +3,6 @@ package main
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"os"
 | 
					 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/xyproto/wallutils"
 | 
						"github.com/xyproto/wallutils"
 | 
				
			||||||
| 
						 | 
					@ -11,7 +10,7 @@ import (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func slideshow(ch <-chan string) {
 | 
					func slideshow(ch <-chan string) {
 | 
				
			||||||
	dir := <-ch
 | 
						dir := <-ch
 | 
				
			||||||
	ticker := time.NewTicker(time.Duration(config.Duration) * time.Minute)
 | 
						ticker := time.NewTicker(time.Duration(config.Duration) * time.Second)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		for {
 | 
							for {
 | 
				
			||||||
| 
						 | 
					@ -36,20 +35,5 @@ func setRandomWallpaper(dir string) error {
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						return wallutils.SetWallpaperCustom(img, "fit", false)
 | 
				
			||||||
	desktopEnv, bool := os.LookupEnv("DESKTOP_SESSION")
 | 
					 | 
				
			||||||
	if !bool {
 | 
					 | 
				
			||||||
		panic("cannot determine desktop environment")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	var mode string
 | 
					 | 
				
			||||||
	switch desktopEnv {
 | 
					 | 
				
			||||||
	case "cinnamon":
 | 
					 | 
				
			||||||
		mode = "span"
 | 
					 | 
				
			||||||
	case "hyprland":
 | 
					 | 
				
			||||||
		mode = "fit"
 | 
					 | 
				
			||||||
	default:
 | 
					 | 
				
			||||||
		mode = ""
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return wallutils.SetWallpaperCustom(img, mode, false)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue