Skip to content

Commit

Permalink
Version 1.0 release
Browse files Browse the repository at this point in the history
Finally done
  • Loading branch information
thelinuxpirate authored Jun 17, 2024
1 parent fa826b5 commit ea3fe32
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 86 deletions.
238 changes: 175 additions & 63 deletions dynamic-paper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"sort"
"strconv"
"strings"
Expand All @@ -22,7 +24,7 @@ type LocalTimes struct {
night int
}

type localPapers struct {
type LocalPapers struct {
sunrise string
day string
sunset string
Expand All @@ -31,6 +33,8 @@ type localPapers struct {

var DesktopSession string = os.Getenv("XDG_SESSION_TYPE")
var localTimes LocalTimes
var localPapers LocalPapers
var wallPath string

var defaultTimes = LocalTimes{
sunrise: 6,
Expand All @@ -40,20 +44,89 @@ var defaultTimes = LocalTimes{
}

func detectDefaultWallpapers() {
var wallPath string = os.Getenv("DP_WALLPATH")
wallPath = os.Getenv("DP_WALLPATH")
if wallPath == "" {
setEnv(0)
fmt.Println("$DP_WALLPATH not detected, please set this variable or use the \"load\" command")
fmt.Println("$ export DP_WALLPATH=\"$HOME/YOURPATH/\"\nOr set via your shellrc file")
errors.New("ABORTING PROGRAM")
} else {
fmt.Println("W")
log.Print("Using wallpaper path from $DP_WALLPATH:", wallPath)
fmt.Println("Using wallpaper path from $DP_WALLPATH:", wallPath)
loadDefaultWallpapers()
setWallpaper()
}
}

func setEnv(x int) {
if x == 0 {
fmt.Println("DP_WALLPATH not detected, would you like to set it?")
} else {
fmt.Println("Error handeling")
func loadDefaultWallpapers() {
err := filepath.Walk(wallPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() {
lowerName := strings.ToLower(info.Name())
switch {
case strings.Contains(lowerName, "sunrise"):
localPapers.sunrise = path
case strings.Contains(lowerName, "day"):
localPapers.day = path
case strings.Contains(lowerName, "sunset"):
localPapers.sunset = path
case strings.Contains(lowerName, "night"):
localPapers.night = path
}
}
return nil
})

if err != nil {
log.Fatalf("Error loading default wallpapers: %v", err)
}
log.Print("Loaded wallpapers: %+v\n", localPapers)
fmt.Printf("Loaded wallpapers: %+v\n", localPapers)
}


func expandPath(path string) string {
if path[:2] == "~/" {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return filepath.Join(usr.HomeDir, path[2:])
}
return path
}

func processWallpapers(wallPaths string) {
paths := strings.Split(wallPaths, ",")
if len(paths) != 4 {
fmt.Println("Please provide exactly 4 wallpaper paths")
return
}

for i := range paths {
paths[i] = strings.TrimSpace(paths[i])
}

if paths[0] != "" {
localPapers.sunrise = paths[0]
}
if paths[1] != "" {
localPapers.day = paths[1]
}
if paths[2] != "" {
localPapers.sunset = paths[2]
}
if paths[3] != "" {
localPapers.night = paths[3]
}

for i, wallPath := range paths {
fmt.Println("Loaded wallpaper:", wallPath)
log.Print("Loaded wallpaper:", i, "", wallPath)
}
setWallpaper()
}

func finalizeTime(usrTimes string) error {
Expand Down Expand Up @@ -83,79 +156,87 @@ func finalizeTime(usrTimes string) error {
return nil
}

func processWallpapers(wallPaths string) {
paths := strings.Split(wallPaths, ",")
if len(paths) != 4 {
fmt.Println("Please provide exactly 4 wallpaper paths")
return
}

for _, wallPath := range paths {
fmt.Println("Loaded wallpaper:", wallPath)
log.Print("Loaded wallpaper:", wallPath)
}
}

func setWallpaper() bool {
currentHour := time.Now().Hour()

var wallprogram, wallpaper string
var wallprogram, args, wallpaper string

if DesktopSession == "x11" {
wallprogram = "feh"
switch {
case currentHour >= localTimes.night:
wallpaper = "~/Pictures/Wallpapers/etc/outset-island/night.jpg"
exec.Command(wallprogram, "--bg-fill", wallpaper)
fmt.Println("Wallpaper set to:", wallpaper)
wallpaper = expandPath(localPapers.night)
case currentHour >= localTimes.sunset:
wallpaper = "~/Pictures/Wallpapers/etc/outset-island/beforeYafter.png"
exec.Command(wallprogram, "--bg-fill", wallpaper)
fmt.Println("Wallpaper set to:", wallpaper)
wallpaper = expandPath(localPapers.sunset)
case currentHour >= localTimes.day:
wallpaper = "~/Pictures/Wallpapers/etc/outset-island/day.png"
exec.Command(wallprogram, "--bg-fill", wallpaper)
fmt.Println("Wallpaper set to:", wallpaper)
wallpaper = expandPath(localPapers.day)
case currentHour >= localTimes.sunrise:
wallpaper = "~/Pictures/Wallpapers/etc/outset-island/beforeYafter.png"
exec.Command(wallprogram, "--bg-fill", wallpaper)
fmt.Println("Wallpaper set to:", wallpaper)
wallpaper = expandPath(localPapers.sunrise)
}
} else if DesktopSession == "wayland" {
wallprogram = "swaybg"
switch {
case currentHour >= localTimes.night:
wallpaper = "~/Pictures/Wallpapers/etc/outset-island/night.jpg"
exec.Command(wallprogram, "--bg-fill", wallpaper)
fmt.Println("Wallpaper set to:", wallpaper)
wallpaper = expandPath(localPapers.night)
case currentHour >= localTimes.sunset:
wallpaper = "~/Pictures/Wallpapers/etc/outset-island/beforeYafter.png"
exec.Command(wallprogram, "--bg-fill", wallpaper)
fmt.Println("Wallpaper set to:", wallpaper)
wallpaper = expandPath(localPapers.sunset)
case currentHour >= localTimes.day:
wallpaper = "~/Pictures/Wallpapers/etc/outset-island/day.png"
exec.Command(wallprogram, "--bg-fill", wallpaper)
fmt.Println("Wallpaper set to:", wallpaper)
wallpaper = expandPath(localPapers.day)
case currentHour >= localTimes.sunrise:
wallpaper = "~/Pictures/Wallpapers/etc/outset-island/beforeYafter.png"
exec.Command(wallprogram, "--bg-fill", wallpaper)
fmt.Println("Wallpaper set to:", wallpaper)
wallpaper = expandPath(localPapers.sunrise)
}
} else {
errors.New("Unable to determine $XDG_SESSION_TYPE...\n(Do you have a 'xdg-desktop-portal' installed?)")
return false
}

if wallprogram == "feh" {
args = "--bg-fill"
} else if wallprogram == "swaybg" {
args = "-i"
} else {
errors.New("Unable to detect your wallpaper program")
log.Println("Honesly dont know what happened... UNEXPECTED ERROR")
}

cmd := exec.Command(wallprogram, args, wallpaper)
cmdOutput, err := cmd.CombinedOutput()
if err != nil {
log.Println("Error executing command:", err)
log.Println("Command output:", string(cmdOutput))
fmt.Println("Error executing command:", err)
fmt.Println("Command output:", string(cmdOutput))
return false
}
log.Print("Wallpaper set to:", wallpaper)
fmt.Println("Wallpaper set to:", wallpaper)
return true
}

func activateDaemon() {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal("Unable to get user home directory: ", err)
}

daemonDir := filepath.Join(homeDir, ".local", "share", "dynamic-paper")

if _, err := os.Stat(daemonDir); os.IsNotExist(err) {
err := os.MkdirAll(daemonDir, 0755)
if err != nil {
log.Fatal("Unable to create daemon directory: ", err)
}
}

logFile := filepath.Join(daemonDir, "dynamic-paper.log")
pidFile := filepath.Join(daemonDir, "dynamic-paper.pid")

cntxt := &daemon.Context{
PidFileName: "dynamic-paper.pid",
PidFileName: pidFile,
PidFilePerm: 0644,
LogFileName: "dynamic-paper.log",
LogFileName: logFile,
LogFilePerm: 0640,
WorkDir: "./",
WorkDir: daemonDir,
Umask: 027,
}

Expand All @@ -180,17 +261,39 @@ func activateDaemon() {
for {
select {
case <-ticker.C:
setWallpaper()
err := setWallpaper()
if err == false {
errors.New("Error executing command...")
}
detectDefaultWallpapers()
}
}
}

// make sure to echo PID & make a function that kills the program using the current PID
// develop an environment variable for Wallpaper path (DP_WALLPATH)
func killDaemon() {
homeDir, err := os.UserHomeDir()
daemonDir := filepath.Join(homeDir, ".local", "share", "dynamic-paper")
pidFile := filepath.Join(daemonDir, "dynamic-paper.pid")
data, err := os.ReadFile(pidFile)
if err != nil {
log.Fatalf("Unable to read PID file: %v", err)
}

pid, err := strconv.Atoi(string(data))
if err != nil {
log.Fatalf("Invalid PID found in file: %v", err)
}

proc, err := os.FindProcess(pid)
if err != nil {
log.Fatalf("Unable to find process: %v", err)
}

err = proc.Kill()
if err != nil {
log.Fatalf("Failed to kill process: %v", err)
}

fmt.Printf("Killed daemon with PID %d\n", pid)
os.Remove(pidFile)
}

func main() {
localTimes = defaultTimes
app := &cli.App{
Expand All @@ -202,17 +305,26 @@ func main() {
Commands: []*cli.Command{
{
Name: "daemon",
Aliases: []string{"dm"},
Usage: "Activates the Dynamic-Paper Daemon",
Aliases: []string{"ad"},
Usage: "Activates the daemon",
Action: func(*cli.Context) error {
activateDaemon()
return nil
},
},
{
Name: "kill-daemon",
Aliases: []string{"kd"},
Usage: "Kills the running daemon",
Action: func(*cli.Context) error {
killDaemon()
return nil
},
},
{
Name: "load",
Aliases: []string{"l"},
Usage: "Provide a List of 4 Wallpaper Paths for Usage",
Usage: "Loads 4 wallpaers for usage (Order: Sunrise, Day, Sunset, Night)",
Action: func(cCtx *cli.Context) error {
if cCtx.NArg() != 1 {
return errors.New("Please provide a comma-separated list of 4 wallpaper paths")
Expand All @@ -226,7 +338,7 @@ func main() {
{
Name: "set-time",
Aliases: []string{"st"},
Usage: "Provide a List of 4 Hours (Order: Sunrise, Day, Sunset, Night)",
Usage: "Sets a custom time for each time of day (Order: Sunrise, Day, Sunset, Night)",
Action: func(cCtx *cli.Context) error {
if cCtx.NArg() != 1 {
return errors.New("Please provide a comma-separated list of 4 time values")
Expand All @@ -240,9 +352,9 @@ func main() {
},
},
{
Name: "run-me",
Name: "run",
Aliases: []string{"r"},
Usage: "Im a demo",
Usage: "Sets the wallpaper for the current time of day",
Action: func(cCtx *cli.Context) error {
detectDefaultWallpapers()
return nil
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ go 1.21.11

require (
github.com/sevlyar/go-daemon v0.1.6
github.com/urfave/cli v1.22.15
github.com/urfave/cli/v2 v2.27.2
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/sys v0.21.0 // indirect
)
Loading

0 comments on commit ea3fe32

Please sign in to comment.