From 9bea148d409935fdb2a9f39fde1a9a0ed356dc62 Mon Sep 17 00:00:00 2001 From: Milarin Date: Mon, 19 Feb 2024 10:02:33 +0100 Subject: [PATCH] IsReachable and WaitUntilReachable implemented --- check_reachable.go | 37 +++++++++++++++++++++++++++++++++++++ dispatch.go | 8 ++++---- 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 check_reachable.go diff --git a/check_reachable.go b/check_reachable.go new file mode 100644 index 0000000..4ccd021 --- /dev/null +++ b/check_reachable.go @@ -0,0 +1,37 @@ +package hypr + +import ( + "context" + "strings" + "time" +) + +// IsReachable pings the unix socket to check +// if Hyprland is ready to accept incoming requests +func (c *Client) IsReachable() bool { + str, err := readSocketString(c.SocketPath(), strings.NewReader("dispatch exec echo")) + if err != nil { + return false + } + + return str == "ok" +} + +// WaitUntilReachable periodically calls IsReachable +// and returns true as soon as IsReachable returns true. +// When ctx is closed before Hyprland is reachable, false is returned +func (c *Client) WaitUntilReachable(ctx context.Context) bool { + ticker := time.NewTicker(100 * time.Millisecond) + defer ticker.Stop() + + select { + case <-ticker.C: + if c.IsReachable() { + return true + } + case <-ctx.Done(): + return false + } + + return false +} diff --git a/dispatch.go b/dispatch.go index 816eb76..c3af557 100644 --- a/dispatch.go +++ b/dispatch.go @@ -6,12 +6,12 @@ import ( "strings" ) -func (i *Client) Dispatch(cmd string) (io.ReadCloser, error) { - return readSocketRaw(i.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd))) +func (c *Client) Dispatch(cmd string) (io.ReadCloser, error) { + return readSocketRaw(c.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd))) } -func (i *Client) DispatchExpectOK(cmd string) error { - str, err := readSocketString(i.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd))) +func (c *Client) DispatchExpectOK(cmd string) error { + str, err := readSocketString(c.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd))) if err != nil { return err }