forked from xyproto/wallutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallpaper.go
97 lines (89 loc) · 2.39 KB
/
wallpaper.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Package wallpaper provides a way to set the desktop wallpaper, for any windowmanager
package wallutils
import (
"errors"
"fmt"
"os"
)
type WM interface {
Name() string
ExecutablesExists() bool
Running() bool
SetWallpaper(string) error
SetVerbose(bool)
}
type Wallpaper struct {
CollectionName string // the name of the directory containing this wallpaper, if it's not "pixmaps", "images" or "contents". May use the parent of the parent.
Path string // full path to the image filename
Width uint // width of the image
Height uint // height of the image
PartOfCollection bool // likely to be part of a wallpaper collection
}
// Default mode when setting the wallpaper for Gnome / Mate / Cinnamon
const defaultMode = "fill"
// All available backends for changing the wallpaper
var WMs = []WM{
&Sway{},
&Deepin{},
&Xfce4{},
&Mate{},
&Cinnamon{},
&Plasma{},
&Gnome3{},
&Gnome2{},
&Weston{},
&Feh{}, // using feh
&X11{},
}
func SetWallpaper(imageFilename string) error {
return SetWallpaperVerbose(imageFilename, false)
}
// Set the desktop wallpaper (filled/stretched), for any supported windowmanager.
// The fallback is to use `feh`.
func SetWallpaperVerbose(imageFilename string, verbose bool) error {
var lastErr error
// Loop through all available WM structs
for _, wm := range WMs {
if wm.Running() && wm.ExecutablesExists() {
if verbose {
fmt.Printf("Using the %s backend.\n", wm.Name())
}
wm.SetVerbose(verbose)
if err := wm.SetWallpaper(imageFilename); err != nil {
lastErr = err
switch wm.Name() {
case "Weston":
// If the current windowmanager is Weston, no method is currently available
return err
default:
if verbose {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
}
// Try the next one
continue
}
} else {
return nil
}
}
}
if lastErr != nil {
return fmt.Errorf("Found no working method for setting the desktop wallpaper:\n%v", lastErr)
}
// Flush stderr
if err := os.Stderr.Sync(); err != nil {
return err
}
// Flush stdout
if err := os.Stdout.Sync(); err != nil {
return err
}
return errors.New("Found no working method for setting the desktop wallpaper")
}
func (wp *Wallpaper) String() string {
star := " "
if wp.PartOfCollection {
star = "*"
}
return fmt.Sprintf("(%s) %dx%d\t%16s\t%s", star, wp.Width, wp.Height, wp.CollectionName, wp.Path)
}