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 }