Compare commits

..

No commits in common. "9bea148d409935fdb2a9f39fde1a9a0ed356dc62" and "2b2f1648c1bf6437fe743eef1a91683a2f6b5cc4" have entirely different histories.

6 changed files with 28 additions and 65 deletions

View File

@ -1,37 +0,0 @@
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
}

View File

@ -6,12 +6,12 @@ import (
"strings" "strings"
) )
func (c *Client) Dispatch(cmd string) (io.ReadCloser, error) { func (i *Instance) Dispatch(cmd string) (io.ReadCloser, error) {
return readSocketRaw(c.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd))) return readSocketRaw(i.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd)))
} }
func (c *Client) DispatchExpectOK(cmd string) error { func (i *Instance) DispatchExpectOK(cmd string) error {
str, err := readSocketString(c.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd))) str, err := readSocketString(i.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd)))
if err != nil { if err != nil {
return err return err
} }

View File

@ -12,7 +12,7 @@ import (
"git.milar.in/milarin/slices" "git.milar.in/milarin/slices"
) )
func GetClient(signature string) (*Client, error) { func GetInstance(signature string) (*Instance, error) {
lockFilePath := fmt.Sprintf("/tmp/hypr/%s.lock", signature) lockFilePath := fmt.Sprintf("/tmp/hypr/%s.lock", signature)
file, err := os.Open(lockFilePath) file, err := os.Open(lockFilePath)
@ -33,38 +33,38 @@ func GetClient(signature string) (*Client, error) {
return nil, err return nil, err
} }
return &Client{ return &Instance{
Signature: signature, Signature: signature,
PID: pid, PID: pid,
WaylandSocket: lines[1], WaylandSocket: lines[1],
}, nil }, nil
} }
func GetDefaultClient() (*Client, error) { func GetDefaultInstance() (*Instance, error) {
signature, ok := os.LookupEnv("HYPRLAND_INSTANCE_SIGNATURE") signature, ok := os.LookupEnv("HYPRLAND_INSTANCE_SIGNATURE")
if !ok { if !ok {
return nil, errors.New("default instance not found because HYPRLAND_INSTANCE_SIGNATURE is not set") return nil, errors.New("default instance not found because HYPRLAND_INSTANCE_SIGNATURE is not set")
} }
return GetClient(signature) return GetInstance(signature)
} }
func GetClients() ([]*Client, error) { func GetInstances() ([]*Instance, error) {
entries, err := os.ReadDir("/tmp/hypr") entries, err := os.ReadDir("/tmp/hypr")
if err != nil { if err != nil {
return nil, err return nil, err
} }
entries = slices.Filter(entries, fs.DirEntry.IsDir) entries = slices.Filter(entries, fs.DirEntry.IsDir)
clients := make([]*Client, 0, len(entries)) instances := make([]*Instance, 0, len(entries))
for _, entry := range entries { for _, entry := range entries {
client, err := GetClient(entry.Name()) instance, err := GetInstance(entry.Name())
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
continue continue
} }
clients = append(clients, client) instances = append(instances, instance)
} }
return clients, nil return instances, nil
} }

View File

@ -4,30 +4,30 @@ import (
"strings" "strings"
) )
func (i *Client) GetActiveWindow() (*Window, error) { func (i *Instance) GetActiveWindow() (*Window, error) {
return readSocket[*Window](i.SocketPath(), strings.NewReader("j/activewindow")) return readSocket[*Window](i.SocketPath(), strings.NewReader("j/activewindow"))
} }
func (i *Client) GetActiveWorkspace() (*Workspace, error) { func (i *Instance) GetActiveWorkspace() (*Workspace, error) {
return readSocket[*Workspace](i.SocketPath(), strings.NewReader("j/activeworkspace")) return readSocket[*Workspace](i.SocketPath(), strings.NewReader("j/activeworkspace"))
} }
func (i *Client) GetBinds() ([]*Bind, error) { func (i *Instance) GetBinds() ([]*Bind, error) {
return readSocket[[]*Bind](i.SocketPath(), strings.NewReader("j/binds")) return readSocket[[]*Bind](i.SocketPath(), strings.NewReader("j/binds"))
} }
func (i *Client) GetWindows() ([]*Window, error) { func (i *Instance) GetWindows() ([]*Window, error) {
return readSocket[[]*Window](i.SocketPath(), strings.NewReader("j/clients")) return readSocket[[]*Window](i.SocketPath(), strings.NewReader("j/clients"))
} }
func (i *Client) GetCursorPos() (Point, error) { func (i *Instance) GetCursorPos() (Point, error) {
return readSocket[Point](i.SocketPath(), strings.NewReader("j/cursorpos")) return readSocket[Point](i.SocketPath(), strings.NewReader("j/cursorpos"))
} }
func (i *Client) GetMonitors() ([]*Monitor, error) { func (i *Instance) GetMonitors() ([]*Monitor, error) {
return readSocket[[]*Monitor](i.SocketPath(), strings.NewReader("j/monitors")) return readSocket[[]*Monitor](i.SocketPath(), strings.NewReader("j/monitors"))
} }
func (i *Client) GetWorkspaces() ([]*Workspace, error) { func (i *Instance) GetWorkspaces() ([]*Workspace, error) {
return readSocket[[]*Workspace](i.SocketPath(), strings.NewReader("j/workspaces")) return readSocket[[]*Workspace](i.SocketPath(), strings.NewReader("j/workspaces"))
} }

View File

@ -5,21 +5,21 @@ import (
"fmt" "fmt"
) )
type Client struct { type Instance struct {
Signature string `json:"instance"` Signature string `json:"instance"`
PID int `json:"pid"` PID int `json:"pid"`
WaylandSocket string `json:"wl_socket"` WaylandSocket string `json:"wl_socket"`
} }
func (c Client) String() string { func (i Instance) String() string {
data, _ := json.MarshalIndent(c, "", "\t") data, _ := json.MarshalIndent(i, "", "\t")
return string(data) return string(data)
} }
func (c Client) SocketPath() string { func (i Instance) SocketPath() string {
return fmt.Sprintf("/tmp/hypr/%s/.socket.sock", c.Signature) return fmt.Sprintf("/tmp/hypr/%s/.socket.sock", i.Signature)
} }
func (c Client) EventSocketPath() string { func (i Instance) EventSocketPath() string {
return fmt.Sprintf("/tmp/hypr/%s/.socket2.sock", c.Signature) return fmt.Sprintf("/tmp/hypr/%s/.socket2.sock", i.Signature)
} }

View File

@ -8,7 +8,7 @@ import (
"git.milar.in/milarin/slices" "git.milar.in/milarin/slices"
) )
func (i *Client) Subscribe(ctx context.Context, events ...EventType) (<-chan Event, error) { func (i *Instance) Subscribe(ctx context.Context, events ...EventType) (<-chan Event, error) {
r, err := readSocketRaw(i.EventSocketPath(), strings.NewReader("")) r, err := readSocketRaw(i.EventSocketPath(), strings.NewReader(""))
if err != nil { if err != nil {
return nil, err return nil, err