Skip to content

Commit 8a199cc

Browse files
committed
Improve the Hyprland support
1 parent ade5d69 commit 8a199cc

4 files changed

Lines changed: 88 additions & 16 deletions

File tree

backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package wallutils
77
// Some backends may require cgo (sway + x11)
88
var WMs = []WM{
99
&Hyprpaper{},
10+
//&Hyprctl{},
1011
&Sway{},
1112
&Deepin{},
1213
&Xfce4{},

backend_nocgo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package wallutils
77
// Only backends that do not require cgo should be included here.
88
var WMs = []WM{
99
&Hyprpaper{},
10+
//&Hyprctl{},
1011
//&Sway{},
1112
&Deepin{},
1213
&Xfce4{},

hyprctl.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package wallutils
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/xyproto/env/v2"
7+
)
8+
9+
// Hyprctl compatible windowmanager
10+
type Hyprctl struct {
11+
mode string
12+
verbose bool
13+
}
14+
15+
// Name returns the name of this window manager or desktop environment
16+
func (h *Hyprctl) Name() string {
17+
return "Hyprctl"
18+
}
19+
20+
// ExecutablesExists checks if executables associated with this backend exists in the PATH
21+
func (h *Hyprctl) ExecutablesExists() bool {
22+
return which("hyprctl") != "" && which("hyprpaper") != ""
23+
}
24+
25+
// Running examines environment variables to try to figure out if this backend is currently running.
26+
func (h *Hyprctl) Running() bool {
27+
return env.Contains("XDG_SESSION_DESKTOP", "Hyprland") || env.Contains("DESKTOP_SESSION", "hyprland")
28+
}
29+
30+
// SetMode will set the current way to display the wallpaper (stretched, tiled etc)
31+
func (h *Hyprctl) SetMode(mode string) {
32+
h.mode = mode
33+
}
34+
35+
// SetVerbose can be used for setting the verbose field to true or false.
36+
// This will cause this backend to output information about what is is doing on stdout.
37+
func (h *Hyprctl) SetVerbose(verbose bool) {
38+
h.verbose = verbose
39+
}
40+
41+
// SetWallpaper sets the desktop wallpaper, given an image filename.
42+
// The image must exist and be readable.
43+
func (h *Hyprctl) SetWallpaper(imageFilename string) error {
44+
if !exists(imageFilename) {
45+
return fmt.Errorf("no such file: %s", imageFilename)
46+
}
47+
48+
// Set the wallpaper mode
49+
mode := defaultMode
50+
if h.mode != "" {
51+
mode = h.mode
52+
}
53+
54+
switch mode {
55+
case "stretched", "center", "fill", "fit", "scale", "scaled", "stretch", "tile", "zoom", "zoomed":
56+
mode = ""
57+
default:
58+
// Invalid and unrecognized desktop wallpaper mode
59+
return fmt.Errorf("invalid desktop wallpaper mode for swaybg: %s", mode)
60+
}
61+
62+
// preload the wallpaper image using hyprctl
63+
err := run("hyprctl", []string{"hyprpaper", "preload", imageFilename}, h.verbose)
64+
if err != nil {
65+
return err
66+
}
67+
68+
// reload the wallpaper image using hyprctl
69+
return run("hyprctl", []string{"hyprpaper", "reload", ",\"" + imageFilename + "\""}, h.verbose)
70+
}

hyprpaper.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ type Hyprpaper struct {
1717
}
1818

1919
// Name returns the name of this window manager or desktop environment
20-
func (sb *Hyprpaper) Name() string {
20+
func (hp *Hyprpaper) Name() string {
2121
return "Hyprpaper"
2222
}
2323

2424
// ExecutablesExists checks if executables associated with this backend exists in the PATH
25-
func (sb *Hyprpaper) ExecutablesExists() bool {
25+
func (hp *Hyprpaper) ExecutablesExists() bool {
2626
return which("hyprpaper") != "" // && which("hyprctl") != ""
2727
}
2828

2929
// Running examines environment variables to try to figure out if this backend is currently running.
30-
// Also sets sb.sock to an empty string or to a UNIX socket file.
31-
func (sb *Hyprpaper) Running() bool {
32-
sb.sock = ""
30+
// Also sets hp.sock to an empty string or to a UNIX socket file.
31+
func (hp *Hyprpaper) Running() bool {
32+
hp.sock = ""
3333

3434
inst := env.Str("HYPRLAND_INSTANCE_SIGNATURE")
3535
if inst == "" {
@@ -42,38 +42,38 @@ func (sb *Hyprpaper) Running() bool {
4242
}
4343

4444
if sock := path.Join("/run/user/" + currentUser.Uid + "/hypr/" + inst + "/.hyprpaper.sock"); exists(sock) {
45-
sb.sock = sock
45+
hp.sock = sock
4646
return true
4747
}
4848

4949
if sock := path.Join("/tmp/hypr", inst, ".hyprpaper.sock"); exists(sock) {
50-
sb.sock = sock
50+
hp.sock = sock
5151
return true
5252
}
5353

54-
return false // env.Contains("XDG_SESSION_DESKTOP", "Hyprland") || env.Contains("DESKTOP_SESSION", "hyprland")
54+
return false
5555
}
5656

5757
// SetMode will set the current way to display the wallpaper (stretched, tiled etc)
58-
func (sb *Hyprpaper) SetMode(mode string) {
59-
sb.mode = mode
58+
func (hp *Hyprpaper) SetMode(mode string) {
59+
hp.mode = mode
6060
}
6161

6262
// SetVerbose can be used for setting the verbose field to true or false.
6363
// This will cause this backend to output information about what is is doing on stdout.
64-
func (sb *Hyprpaper) SetVerbose(verbose bool) {
65-
sb.verbose = verbose
64+
func (hp *Hyprpaper) SetVerbose(verbose bool) {
65+
hp.verbose = verbose
6666
}
6767

6868
// SetWallpaper sets the desktop wallpaper, given an image filename.
6969
// The image must exist and be readable.
70-
func (sb *Hyprpaper) SetWallpaper(imageFilename string) error {
70+
func (hp *Hyprpaper) SetWallpaper(imageFilename string) error {
7171
if !exists(imageFilename) {
7272
return fmt.Errorf("no such file: %s", imageFilename)
7373
}
7474

7575
// Connect to the Hyprpaper socket
76-
sock, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: sb.sock, Net: "unix"})
76+
sock, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: hp.sock, Net: "unix"})
7777

7878
if err != nil {
7979
return err
@@ -88,8 +88,8 @@ func (sb *Hyprpaper) SetWallpaper(imageFilename string) error {
8888

8989
// Set the wallpaper mode
9090
mode := defaultMode
91-
if sb.mode != "" {
92-
mode = sb.mode
91+
if hp.mode != "" {
92+
mode = hp.mode
9393
}
9494

9595
switch mode {

0 commit comments

Comments
 (0)