-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhyprpaper.go
More file actions
139 lines (112 loc) · 3.2 KB
/
Copy pathhyprpaper.go
File metadata and controls
139 lines (112 loc) · 3.2 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package wallutils
import (
"errors"
"fmt"
"io"
"net"
"os/user"
"path"
"strings"
"time"
"github.com/xyproto/env/v2"
)
// Hyprpaper compatible windowmanager
type Hyprpaper struct {
mode string
sock string
verbose bool
}
// Name returns the name of this window manager or desktop environment
func (hp *Hyprpaper) Name() string {
return "Hyprpaper"
}
// ExecutablesExists checks if executables associated with this backend exists in the PATH
func (hp *Hyprpaper) ExecutablesExists() bool {
return which("hyprpaper") != "" // && which("hyprctl") != ""
}
// Running examines environment variables to try to figure out if this backend is currently running.
// Also sets hp.sock to an empty string or to a UNIX socket file.
func (hp *Hyprpaper) Running() bool {
hp.sock = ""
inst := env.Str("HYPRLAND_INSTANCE_SIGNATURE")
if inst == "" {
return false // HYPRLAND_INSTANCE_SIGNATURE is not set
}
currentUser, err := user.Current()
if err != nil || currentUser.Uid == "" {
return false // could not get the user ID of the current user
}
if sock := path.Join("/run/user/" + currentUser.Uid + "/hypr/" + inst + "/.hyprpaper.sock"); exists(sock) {
hp.sock = sock
return true
}
if sock := path.Join("/tmp/hypr", inst, ".hyprpaper.sock"); exists(sock) {
hp.sock = sock
return true
}
return false
}
// SetMode will set the current way to display the wallpaper (stretched, tiled etc)
func (hp *Hyprpaper) SetMode(mode string) {
hp.mode = mode
}
// SetVerbose can be used for setting the verbose field to true or false.
// This will cause this backend to output information about what is is doing on stdout.
func (hp *Hyprpaper) SetVerbose(verbose bool) {
hp.verbose = verbose
}
// SetWallpaper sets the desktop wallpaper, given an image filename.
// The image must exist and be readable.
func (hp *Hyprpaper) SetWallpaper(imageFilename string) error {
if !exists(imageFilename) {
return fmt.Errorf("no such file: %s", imageFilename)
}
// Connect to the Hyprpaper socket
sock, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: hp.sock, Net: "unix"})
if err != nil {
return err
}
defer sock.Close()
// Preload the image
if err = runHyprCmd(sock, "preload "+imageFilename); err != nil {
return err
}
// Set the wallpaper mode
mode := defaultMode
if hp.mode != "" {
mode = hp.mode
}
switch mode {
case "center", "tile", "fill", "stretch", "scale", "scaled", "zoom", "zoomed", "stretched":
mode = ""
case "fit":
mode = "contain:"
default:
// Invalid and unrecognized desktop wallpaper mode
return fmt.Errorf("invalid desktop wallpaper mode for swaybg: %s", mode)
}
// Set the wallpaper
if err = runHyprCmd(sock, "wallpaper ,"+mode+imageFilename); err != nil {
return err
}
// Unload unused images
return runHyprCmd(sock, "unload unused")
}
func runHyprCmd(sock *net.UnixConn, cmd string) error {
if _, err := sock.Write([]byte(cmd)); err != nil {
return err
}
sock.SetReadDeadline(time.Now().Add(2 * time.Second))
data, err := io.ReadAll(sock)
if err != nil {
var ne net.Error
if !(errors.As(err, &ne) && ne.Timeout()) {
return err
}
}
res := strings.TrimSpace(string(data))
if res != "ok" {
return fmt.Errorf("cmd %q failed: %s", cmd, res)
}
return nil
}