Compare commits
No commits in common. "22d8316f12bc2050f1a6464af21bb1003caf26b2" and "34ddba17c17801705e47db0c98f9f2800d9580a8" have entirely different histories.
22d8316f12
...
34ddba17c1
13
config.go
13
config.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/adhocore/jsonc"
|
"github.com/adhocore/jsonc"
|
||||||
|
@ -31,7 +32,7 @@ func (config *Config) load() error {
|
||||||
|
|
||||||
homeDir, err := os.UserHomeDir()
|
homeDir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
elog.Println("could not get home directory!")
|
log.Println("could not get home directory!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,25 +43,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 {
|
||||||
elog.Println("could not encode default config!")
|
log.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) {
|
||||||
elog.Println("could not create .config directory", err)
|
log.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) {
|
||||||
elog.Println("could not create .config/gopaper directory")
|
log.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 {
|
||||||
elog.Println("could not create config file!")
|
log.Println("could not create config file!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
file.Write(defaultConfigJSON)
|
file.Write(defaultConfigJSON)
|
||||||
|
@ -71,7 +72,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 {
|
||||||
elog.Println("Couldn't unmarshal config!")
|
log.Println("Couldn't unmarshal config!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
5
image.go
5
image.go
|
@ -37,6 +37,7 @@ 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))
|
||||||
|
|
||||||
|
@ -49,13 +50,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 {
|
||||||
elog.Printf("failed to load image %v", randomImg)
|
log.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.Printf("file %v already exists in cache. skipping image processing\n", filename)
|
log.Println("file already exists in cache. skipping image processing")
|
||||||
}
|
}
|
||||||
return curr, nil
|
return curr, nil
|
||||||
}
|
}
|
||||||
|
|
91
main.go
91
main.go
|
@ -1,11 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,23 +15,43 @@ 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()
|
||||||
|
|
||||||
clog.Println("Welcome to GoPaper!")
|
//INITIALIZE CONFIG
|
||||||
|
|
||||||
//INIT CONFIG
|
|
||||||
err := config.load()
|
err := config.load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
elog.Println("unable to initialize config", err)
|
log.Println("unable to initialize config", err)
|
||||||
}
|
}
|
||||||
handleProcesses()
|
|
||||||
setupLogging(*debugFlag)
|
//SETUP LOGGER
|
||||||
|
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)
|
||||||
|
@ -77,7 +98,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) {
|
||||||
elog.Printf("unable to remove sockfile at %v. Error: %v\n", sockfile, err)
|
log.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 {
|
||||||
|
@ -86,7 +107,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) {
|
||||||
elog.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
|
log.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 {
|
||||||
|
@ -103,51 +124,21 @@ 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 {
|
||||||
elog.Println("unable to read pre-existing pidfile", err)
|
log.Println("unable to read pre-existing pidfile", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
//kill process
|
//kill process
|
||||||
err = exec.Command("kill", string(prexPid)).Run()
|
//BUG: kill command doesn't work. prexPid == []
|
||||||
|
err = exec.Command("kill", "-9", string(prexPid)).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
elog.Printf("Error attempting to kill process %v: %v\n", string(prexPid), err)
|
log.Printf("unable to kill process %v\n", string(prexPid))
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
//remove pidfile
|
//remove pidfile
|
||||||
err = os.Remove(pidfilePath)
|
err = os.Remove(pidfilePath)
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil {
|
||||||
elog.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
|
log.Printf("unable to remove pidfile %v\n", pidfilePath)
|
||||||
|
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,12 +16,11 @@ func slideshow(ch <-chan string) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
img, err := setRandomWallpaper(dir)
|
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!")
|
||||||
|
@ -34,10 +33,10 @@ func slideshow(ch <-chan string) {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func setRandomWallpaper(dir string) (string, error) {
|
func setRandomWallpaper(dir 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")
|
||||||
|
@ -54,5 +53,5 @@ func setRandomWallpaper(dir string) (string, error) {
|
||||||
mode = ""
|
mode = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return img, wallutils.SetWallpaperCustom(img, mode, false)
|
return wallutils.SetWallpaperCustom(img, mode, false)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue