1package wallutils
2
3// This is for Pekwm 0.2.0 or later
4
5import (
6	"errors"
7	"fmt"
8
9	"github.com/xyproto/env"
10)
11
12// Pekwm is a structure containing settings for running the "pekwm_bg" executable
13type Pekwm struct {
14	mode    string
15	verbose bool
16}
17
18// Name returns the name of this method of setting a wallpaper
19func (f *Pekwm) Name() string {
20	return "Pekwm"
21}
22
23// ExecutablesExists checks if the "pekwm_bg" executable exists in the PATH
24// (comes with pekwm 0.2.0 or later)
25func (f *Pekwm) ExecutablesExists() bool {
26	return which("pekwm_bg") != ""
27}
28
29// Running checks if $PEKWM_CONFIG_FILE is set
30func (f *Pekwm) Running() bool {
31	return env.Has("PEKWM_CONFIG_FILE")
32}
33
34// SetMode will set the current way to display the wallpaper (stretched, tiled etc).
35// The selected mode must be compatible with pekwm_bg.
36func (f *Pekwm) SetMode(mode string) {
37	f.mode = mode
38}
39
40// SetVerbose can be used for setting the verbose field to true or false.
41// This will cause this backend to output information about what is is doing on stdout.
42func (f *Pekwm) SetVerbose(verbose bool) {
43	f.verbose = verbose
44}
45
46// SetWallpaper sets the desktop wallpaper, given an image filename.
47// The image must exist and be readable.
48func (f *Pekwm) SetWallpaper(imageFilename string) error {
49	if !exists(imageFilename) {
50		return fmt.Errorf("no such file: %s", imageFilename)
51	}
52	mode := defaultMode
53	if f.mode != "" {
54		mode = f.mode
55	}
56
57	// Images are set with ie. "pekwm_bg Image /somewhere/image.png#scaled"
58
59	var tag string
60
61	// check if the mode is valid
62	switch mode {
63	case "stretch", "stretched", "scaled", "fill", "scale", "max":
64		tag = "#scaled"
65	case "tile", "tiled":
66		// pekwm_bg tiles by default
67		break
68	default:
69		// Invalid and unrecognized desktop wallpaper mode
70		return fmt.Errorf("invalid desktop wallpaper mode for Pekwm: %s", mode)
71	}
72
73	// set the wallpaper with pekwm_bg
74	if err := run("pekwm_bg", []string{"-D", "Image", imageFilename + tag}, f.verbose); err != nil {
75		return errors.New("pekwm_bg -D Image \"" + imageFilename + tag + "\" failed to run")
76	}
77	return nil
78}
79