From bc004e5cadcd68f5a90075399c8125430921862a Mon Sep 17 00:00:00 2001 From: Peter Fern Date: Sat, 11 May 2024 13:15:49 +1000 Subject: [PATCH] fix(compat): Update for new hyprland socket path, with fallback to /tmp --- internal/hypripc/hypripc.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/internal/hypripc/hypripc.go b/internal/hypripc/hypripc.go index e515baf..449b8c6 100644 --- a/internal/hypripc/hypripc.go +++ b/internal/hypripc/hypripc.go @@ -8,6 +8,7 @@ import ( "io" "net" "os" + "path" "regexp" "strconv" "strings" @@ -193,7 +194,11 @@ func (h *HyprIPC) Dispatch(args ...string) error { } func (h *HyprIPC) send(args ...string) ([]byte, error) { - ctrl, err := net.Dial(`unix`, fmt.Sprintf("/tmp/hypr/%s/.socket.sock", os.Getenv(`HYPRLAND_INSTANCE_SIGNATURE`))) + sock, err := socketPath(`.socket.sock`) + if err != nil { + return nil, err + } + ctrl, err := net.Dial(`unix`, sock) if err != nil { return nil, err } @@ -314,7 +319,11 @@ func (h *HyprIPC) readloop() { // New instantiates a new HyprIPC client func New(log hclog.Logger) (*HyprIPC, error) { - evtConn, err := net.Dial(`unix`, fmt.Sprintf("/tmp/hypr/%s/.socket2.sock", os.Getenv(`HYPRLAND_INSTANCE_SIGNATURE`))) + sock, err := socketPath(`.socket2.sock`) + if err != nil { + return nil, err + } + evtConn, err := net.Dial(`unix`, sock) if err != nil { return nil, err } @@ -546,3 +555,19 @@ func hyprToEvent(name Event, value string) (*eventv1.Event, error) { return eventv1.NewString(eventv1.EventKind_EVENT_KIND_UNSPECIFIED, value) } } + +func socketPath(sock string) (string, error) { + s := path.Join(os.Getenv(`XDG_RUNTIME_DIR`), `hypr`, os.Getenv(`HYPRLAND_INSTANCE_SIGNATURE`), sock) + _, err := os.Stat(s) + if err == nil { + return s, nil + } + + s = path.Join(`/tmp`, `hypr`, os.Getenv(`HYPRLAND_INSTANCE_SIGNATURE`), sock) + _, err = os.Stat(s) + if err != nil { + return ``, fmt.Errorf("hyprland socket not found: %w", err) + } + + return s, nil +}