Compare commits
2 Commits
34ddba17c1
...
22d8316f12
Author | SHA1 | Date |
---|---|---|
|
22d8316f12 | |
|
7bdef46ab9 |
13
config.go
13
config.go
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/adhocore/jsonc"
|
"github.com/adhocore/jsonc"
|
||||||
|
@ -32,7 +31,7 @@ func (config *Config) load() error {
|
||||||
|
|
||||||
homeDir, err := os.UserHomeDir()
|
homeDir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("could not get home directory!")
|
elog.Println("could not get home directory!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,25 +42,25 @@ func (config *Config) load() error {
|
||||||
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!")
|
elog.Println("could not encode default config!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
//TODO: make parent directories if they don't exist
|
//TODO: make parent directories if they don't exist
|
||||||
err = os.Mkdir(homeDir+"/.config", 755)
|
err = os.Mkdir(homeDir+"/.config", 755)
|
||||||
if err != nil && !os.IsExist(err) {
|
if err != nil && !os.IsExist(err) {
|
||||||
log.Println("could not create .config directory", err)
|
elog.Println("could not create .config directory", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = os.Mkdir(configDir, 755)
|
err = os.Mkdir(configDir, 755)
|
||||||
if err != nil && !os.IsExist(err) {
|
if err != nil && !os.IsExist(err) {
|
||||||
log.Println("could not create .config/gopaper directory")
|
elog.Println("could not create .config/gopaper directory")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.Create(configDir + "config.jsonc")
|
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!")
|
elog.Println("could not create config file!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
file.Write(defaultConfigJSON)
|
file.Write(defaultConfigJSON)
|
||||||
|
@ -72,7 +71,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!")
|
elog.Println("Couldn't unmarshal config!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
5
image.go
5
image.go
|
@ -37,7 +37,6 @@ func hexToHSL(string) [3]int {
|
||||||
func pickRandomImage(dir string) (string, error) {
|
func pickRandomImage(dir string) (string, error) {
|
||||||
|
|
||||||
dir = config.ImagesDir + dir
|
dir = config.ImagesDir + dir
|
||||||
log.Printf("getting random file from %v\n", dir)
|
|
||||||
randomImg, filename, _ := getRandomFile(dir)
|
randomImg, filename, _ := getRandomFile(dir)
|
||||||
filenameNoExt := strings.TrimSuffix(filename, filepath.Ext(filename))
|
filenameNoExt := strings.TrimSuffix(filename, filepath.Ext(filename))
|
||||||
|
|
||||||
|
@ -50,13 +49,13 @@ func pickRandomImage(dir string) (string, error) {
|
||||||
if !fileExists {
|
if !fileExists {
|
||||||
img, err := loadImage(randomImg)
|
img, err := loadImage(randomImg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to load image %v", randomImg)
|
elog.Printf("failed to load image %v", randomImg)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
dst := processImage(img, config)
|
dst := processImage(img, config)
|
||||||
saveImage(curr, dst)
|
saveImage(curr, dst)
|
||||||
} else {
|
} else {
|
||||||
log.Println("file already exists in cache. skipping image processing")
|
log.Printf("file %v already exists in cache. skipping image processing\n", filename)
|
||||||
}
|
}
|
||||||
return curr, nil
|
return curr, nil
|
||||||
}
|
}
|
||||||
|
|
91
main.go
91
main.go
|
@ -1,12 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,43 +14,23 @@ var config Config
|
||||||
const sockfile = "/tmp/gopaper.sock"
|
const sockfile = "/tmp/gopaper.sock"
|
||||||
const pidfilePath = "/tmp/gopaper.pid"
|
const pidfilePath = "/tmp/gopaper.pid"
|
||||||
|
|
||||||
|
var clog = log.New(os.Stdout, "## ", 0)
|
||||||
|
var elog = log.New(os.Stdout, "ERROR: ", 3)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//SETUP FLAGS
|
//SETUP FLAGS
|
||||||
var debugFlag = flag.Bool("debug", false, "help message for flag n")
|
var debugFlag = flag.Bool("debug", false, "help message for flag n")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
//INITIALIZE CONFIG
|
clog.Println("Welcome to GoPaper!")
|
||||||
|
|
||||||
|
//INIT CONFIG
|
||||||
err := config.load()
|
err := config.load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("unable to initialize config", err)
|
elog.Println("unable to initialize config", err)
|
||||||
}
|
}
|
||||||
|
handleProcesses()
|
||||||
//SETUP LOGGER
|
setupLogging(*debugFlag)
|
||||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
||||||
if *debugFlag {
|
|
||||||
logfile, err := openLogfile()
|
|
||||||
if err != nil {
|
|
||||||
log.Println("failed to load config", err)
|
|
||||||
cleanExit(1)
|
|
||||||
}
|
|
||||||
log.SetOutput(logfile)
|
|
||||||
}
|
|
||||||
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
|
|
||||||
log.Println("Welcome to GoPaper!")
|
|
||||||
|
|
||||||
killPrevProc()
|
|
||||||
//create new pidfile
|
|
||||||
pid := os.Getpid()
|
|
||||||
pidfile, err := os.Create(pidfilePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("error trying to create pidfile:", err)
|
|
||||||
cleanExit(1)
|
|
||||||
}
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
err = binary.Write(buf, binary.LittleEndian, pid)
|
|
||||||
if err != nil {
|
|
||||||
}
|
|
||||||
pidfile.Write(buf.Bytes())
|
|
||||||
|
|
||||||
var waitGroup sync.WaitGroup
|
var waitGroup sync.WaitGroup
|
||||||
waitGroup.Add(1)
|
waitGroup.Add(1)
|
||||||
|
@ -98,7 +77,7 @@ func cleanExit(code int) {
|
||||||
|
|
||||||
err := os.Remove(sockfile)
|
err := os.Remove(sockfile)
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
log.Printf("unable to remove sockfile at %v. Error: %v\n", sockfile, err)
|
elog.Printf("unable to remove sockfile at %v. Error: %v\n", sockfile, err)
|
||||||
} else if !os.IsNotExist(err) {
|
} else if !os.IsNotExist(err) {
|
||||||
log.Println("sockfile removed")
|
log.Println("sockfile removed")
|
||||||
} else {
|
} else {
|
||||||
|
@ -107,7 +86,7 @@ func cleanExit(code int) {
|
||||||
|
|
||||||
err = os.Remove(pidfilePath)
|
err = os.Remove(pidfilePath)
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
log.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
|
elog.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
|
||||||
} else if !os.IsNotExist(err) {
|
} else if !os.IsNotExist(err) {
|
||||||
log.Println("pidfile removed")
|
log.Println("pidfile removed")
|
||||||
} else {
|
} else {
|
||||||
|
@ -124,21 +103,51 @@ func killPrevProc() error {
|
||||||
//if it does, kill the process and delete the pidfile
|
//if it does, kill the process and delete the pidfile
|
||||||
prexPid, err := os.ReadFile(pidfilePath)
|
prexPid, err := os.ReadFile(pidfilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("unable to read pre-existing pidfile", err)
|
elog.Println("unable to read pre-existing pidfile", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
//kill process
|
//kill process
|
||||||
//BUG: kill command doesn't work. prexPid == []
|
err = exec.Command("kill", string(prexPid)).Run()
|
||||||
err = exec.Command("kill", "-9", string(prexPid)).Run()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("unable to kill process %v\n", string(prexPid))
|
elog.Printf("Error attempting to kill process %v: %v\n", string(prexPid), err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
//remove pidfile
|
//remove pidfile
|
||||||
err = os.Remove(pidfilePath)
|
err = os.Remove(pidfilePath)
|
||||||
if err != nil {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
log.Printf("unable to remove pidfile %v\n", pidfilePath)
|
elog.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func handleProcesses() {
|
||||||
|
err := killPrevProc()
|
||||||
|
if err != nil {
|
||||||
|
elog.Println("error attempting to kill pre-existing process: ", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
//create new pidfile
|
||||||
|
pid := strconv.FormatInt(int64(os.Getpid()), 10)
|
||||||
|
pidfile, err := os.Create(pidfilePath)
|
||||||
|
if err != nil {
|
||||||
|
elog.Println("error trying to create pidfile:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
pidfile.WriteString(pid)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupLogging(debug bool) {
|
||||||
|
|
||||||
|
//SETUP LOGGERS
|
||||||
|
if debug {
|
||||||
|
logfile, err := openLogfile()
|
||||||
|
if err != nil {
|
||||||
|
elog.Println("failed to load config", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
log.SetOutput(logfile)
|
||||||
|
clog.SetOutput(logfile)
|
||||||
|
elog.SetOutput(logfile)
|
||||||
|
}
|
||||||
|
elog.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
|
log.SetFlags(log.LstdFlags)
|
||||||
|
}
|
||||||
|
|
|
@ -16,11 +16,12 @@ func slideshow(ch <-chan string) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
err := setRandomWallpaper(dir)
|
img, err := setRandomWallpaper(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to set random wallpaper from directory %v.", dir)
|
fmt.Printf("Failed to set random wallpaper from directory %v.", dir)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
log.Printf("set %v as wallpaper\n", img)
|
||||||
select {
|
select {
|
||||||
case dir = <-ch:
|
case dir = <-ch:
|
||||||
log.Println("directory set!")
|
log.Println("directory set!")
|
||||||
|
@ -33,10 +34,10 @@ func slideshow(ch <-chan string) {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func setRandomWallpaper(dir string) error {
|
func setRandomWallpaper(dir string) (string, error) {
|
||||||
img, err := pickRandomImage(dir)
|
img, err := pickRandomImage(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
desktopEnv, bool := os.LookupEnv("DESKTOP_SESSION")
|
desktopEnv, bool := os.LookupEnv("DESKTOP_SESSION")
|
||||||
|
@ -53,5 +54,5 @@ func setRandomWallpaper(dir string) error {
|
||||||
mode = ""
|
mode = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return wallutils.SetWallpaperCustom(img, mode, false)
|
return img, wallutils.SetWallpaperCustom(img, mode, false)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue