2024-02-19 10:02:33 +01:00
|
|
|
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()
|
|
|
|
|
2024-02-22 00:21:14 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if c.IsReachable() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return false
|
2024-02-19 10:02:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|