summary refs log tree commit diff stats
path: root/background/set_unix.go
blob: e782a92be26cd78051ab63562ddfb02f6fbb898e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// +build linux netbsd openbsd freebsd dragonfly

package background

import (
	"fmt"
	"os"
	"os/exec"
)

// SetFromFile takes a string as an input, it must be absolute path to
// the background. Checks are not made to check if the path exists or
// it is actually an image, that must be verified before passing it to
// SetFromFile. SetFromFile will exit returning in error if there is
// any.
func SetFromFile(path string) error {
	var err error
	switch os.Getenv("XDG_CURRENT_DESKTOP") {
	case "GNOME", "Unity", "Pantheon":
		// GNOME, Unity & Pantheon support setting background
		// from gsettings & have the same key.

		// gsettings takes path in format of a uri
		path = fmt.Sprintf("%s%s", "file://", path)

		err = exec.Command("gsettings",
			"set org.gnome.desktop.background picture-uri", path).Run()
		if err != nil {
			err = fmt.Errorf("%s\n%s",
				"set_unix.go: failed to set background with gsettings",
				err.Error())
		}
		return err

	case "LXDE":
		// Background on LXDE can be set with pcmanfm (default
		// file manager).
		err = exec.Command("pcmanfm", "-w", path).Run()
		if err != nil {
			err = fmt.Errorf("%s\n%s",
				"set_unix.go: failed to set background with pcmanfm",
				err.Error())
		}
		return err

	default:
		// If WM/DE doesn't have a case then feh is used to
		// set the background. This is tested to work on WMs
		// similar to i3wm.
		feh, err := exec.LookPath("feh")
		if err != nil {
			err = fmt.Errorf("%s\n%s",
				"set_unix.go: feh not found in $PATH",
				err.Error())
			return err
		}

		err = exec.Command(feh, "--bg-fill", path).Run()
		if err != nil {
			err = fmt.Errorf("%s\n%s",
				"set_unix.go: failed to set background with feh",
				err.Error())
		}
		return err
	}
}