Skip to content

Commit

Permalink
Merge pull request #3 from zxcv32/fix/kde
Browse files Browse the repository at this point in the history
feat!: update for KDE
  • Loading branch information
zxcv32 authored Nov 5, 2022
2 parents a7072db + 77fa282 commit 2f80ea0
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Create executable and system pacakge
# Create executable and system package
SHELL = bash

# Create executable
Expand Down
2 changes: 1 addition & 1 deletion build/openrwc-amd64/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: openrwc
Version: 0.0.2
Version: 0.0.3
Architecture: amd64
Essential: no
Priority: optional
Expand Down
2 changes: 1 addition & 1 deletion build/openrwc-amd64/etc/systemd/system/openrwc@.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ StartLimitIntervalSec=0
[Service]
Environment="DISPLAY=:0"
Type=simple
ExecStart=/usr/bin/openrwc
ExecStart=/usr/bin/sh -c "DBUS_SESSION_BUS_ADDRESS=unix:path=$(find /run/user/ -maxdepth 1 -mindepth 1 | head -n 1)/bus /usr/bin/openrwc"
Restart=always
RestartSec=5
User=%i
Expand Down
11 changes: 8 additions & 3 deletions configs/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
title = "OpenRWC Configuration"
version = "0.0.1"
version = "0.0.2"

[reddit]
subreddits = ["wallpaper", "wallpapers", "Animewallpaper", "AnimeWallpapersSFW", "MinimalWallpaper"]
Expand All @@ -19,8 +19,13 @@ max_attempts = 50
# Number of monitors. Same wallpaper will be set on each monitors
monitors=2

# Nitrogen parameter, One of ("set-auto", "set-centered", "set-scaled", "set-tiled", "set-zoom" , "set-zoom-fill")
nitrogen_param = "set-scaled"
# Software used to set the wallpaper. One of ("nitrogen", "kde")
util = "kde"
# Util parameter.
# Examples:
# nitrogen: one of ("set-auto", "set-centered", "set-scaled", "set-tiled", "set-zoom" , "set-zoom-fill")
# KDE: <Not used>
util_param = "set-scaled"

[openrwc.timeout]
# s=seconds,m=minutes,h=hours,d=days
Expand Down
33 changes: 26 additions & 7 deletions internal/changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package internal

import (
"errors"
"fmt"
"strings"
"time"

Expand All @@ -22,14 +23,14 @@ import (
reddit "github.com/zxcv32/openrwc/pkg"
)

// Thrown when no wallpaper is set by nitrogen
// NoWallpaperError Thrown when no wallpaper is set by software
type NoWallpaperError struct{}

func (m *NoWallpaperError) Error() string {
return "No wallpaper to change"
}

// Changes wallpaper periodically
// TimedChanger Changes wallpaper periodically
func TimedChanger() {
fails := 0
for {
Expand All @@ -52,7 +53,7 @@ func TimedChanger() {
}
}

// Fetch and set wallpaper
// Change Fetch and set wallpaper
func Change() (bool, *NoWallpaperError) {
path := LoadConfig() // Load config
for _, subreddit := range getSubreddits() {
Expand Down Expand Up @@ -80,10 +81,8 @@ func Change() (bool, *NoWallpaperError) {
time.Sleep(getRetryDelay())
continue
}
nitrogenError := NitrogenChange(wallpaper)
if nil != nitrogenError {
log.Errorf("nitrogen error: %s", nitrogenError.Error())
// Just wait for next iteration if no wallpaper was set
utilErr := utilSet(path, wallpaper)
if nil == utilErr {
time.Sleep(getRetryDelay())
continue
}
Expand All @@ -93,6 +92,26 @@ func Change() (bool, *NoWallpaperError) {
return false, &NoWallpaperError{}
}

func utilSet(path string, wallpaper string) *NoWallpaperError {
var err error

util := viper.GetString("openrwc.util")
switch util {
case "nitrogen":
err = NitrogenChange(wallpaper)
case "kde":
err = KdeChange(path, wallpaper)
default:
fmt.Println("Util unknown:", util)
}
if nil != err {
log.Errorf("Util error: %s", err.Error())
// Just wait for next iteration if no wallpaper was set
return nil
}
return &NoWallpaperError{}
}

// Get config
func getSubreddits() []string {
subreddits := viper.GetStringSlice("reddit.subreddits")
Expand Down
24 changes: 17 additions & 7 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/spf13/viper"
)

// Load configuration and return the path of config directory
// LoadConfig Load configuration and return the path of config directory
func LoadConfig() string {
// Load config
path := "configs"
Expand All @@ -32,7 +32,12 @@ func LoadConfig() string {
}
return path
} else {
setupViper(path, file, "toml") // Usuall location in project
pwd, pwdErr := os.Getwd()
if pwdErr != nil {
log.Fatal(pwdErr)
}
path = pwd + "/" + path
setupViper(path, file, "toml") // Usually location in project
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err.Error())
Expand All @@ -52,7 +57,7 @@ func createDefaultConfig() (string, string) {

template := `
title = "OpenRWC Configuration"
version = "0.0.1"
version = "0.0.2"
[reddit]
subreddits = ["wallpaper", "wallpapers", "Animewallpaper", "AnimeWallpapersSFW", "MinimalWallpaper"]
Expand All @@ -72,8 +77,13 @@ max_attempts = 10
# Number of monitors. Same wallpaper will be set on each monitors
monitors=1
# Nitrogen parameter, One of ("set-auto", "set-centered", "set-scaled", "set-tiled", "set-zoom" , "set-zoom-fill")
nitrogen_param = "set-scaled"
# Software used to set the wallpaper. One of ("nitrogen", "kde")
util = "kde"
# Util parameter.
# Examples:
# nitrogen: one of ("set-auto", "set-centered", "set-scaled", "set-tiled", "set-zoom" , "set-zoom-fill")
# KDE: <Not used>
util_param = "set-scaled"
[openrwc.timeout]
# s=seconds,m=minutes,h=hours,d=days
Expand All @@ -95,8 +105,8 @@ nitrogen_param = "set-scaled"
log.Fatal(openError)
}
defer f.Close()
_, writeError := f.WriteString(template)

output, writeError := f.WriteString(template)
log.Infof(string(output))
if writeError != nil {
log.Fatal(writeError)
}
Expand Down
59 changes: 59 additions & 0 deletions internal/kde.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2022 OpenRWC.
*
* Licensed under GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*
* @author Ashwani Sharma (https://github.com/zxcV32)
*/

package internal

import (
log "github.com/sirupsen/logrus"
"os"
"os/exec"
)

// KdeChange Set wallpaper on monitor(s)
func KdeChange(path string, wallpaper string) error {
// https://www.reddit.com/r/kde/comments/65pmhj/change_wallpaper_from_terminal/
script := `#!/usr/bin/env sh
qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript "var allDesktops = desktops();
for (i=0;i<allDesktops.length;i++) {{
d = allDesktops[i];
d.wallpaperPlugin = \"org.kde.image\";
d.currentConfigGroup = Array(\"Wallpaper\",
\"org.kde.image\",
\"General\");
d.writeConfig(\"Image\", \"$1\")
}}
"
`
file := "kde.sh"
if _, err := os.Stat(path + "/" + file); os.IsNotExist(err) {
os.MkdirAll(path, 0700)
f, openError := os.Create(path + "/" + file)
if openError != nil {
log.Fatal(openError)
}
defer f.Close()
_, writeError := f.WriteString(script)

if writeError != nil {
log.Fatal(writeError)
}
err = os.Chmod(path+"/"+file, 0775)
if err != nil {
log.Fatal(err)
}
log.Infof("Shell script %s created at %s", file, path)
}
_, err := exec.Command(path+"/"+file, wallpaper).Output()
if nil != err {
return err
}
log.Infof("KDE wallpaper applied to all monitors")
return nil
}
2 changes: 1 addition & 1 deletion internal/nitrogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/spf13/viper"
)

// Set wallpaper on monitor(s)
// NitrogenChange Set wallpaper on monitor(s)
func NitrogenChange(wallpaper string) error {
monitors := viper.GetInt("openrwc.monitors")
for i := 0; i < monitors; i++ {
Expand Down

0 comments on commit 2f80ea0

Please sign in to comment.