hypr/check_reachable.go

42 lines
822 B
Go
Raw Permalink Normal View History

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 {
2024-12-12 20:35:28 +01:00
if c.IsReachable() {
return true
}
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if c.IsReachable() {
return true
}
case <-ctx.Done():
return false
}
}
}