Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
13a1346c39 | |||
5e1ba06ebe | |||
2b5b3f84cb | |||
f358112c7e | |||
8c1df2310c | |||
9fad501f41 | |||
62341d584a | |||
3503da411f | |||
f7662457a9 | |||
ce9bb69859 | |||
faf807cb86 | |||
075a8441cd | |||
49708ed503 | |||
9bea148d40 | |||
cfc682a36f | |||
2b2f1648c1 |
41
check_reachable.go
Normal file
41
check_reachable.go
Normal file
@ -0,0 +1,41 @@
|
||||
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 {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
@ -6,12 +6,12 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (i *Instance) Dispatch(cmd string) (io.ReadCloser, error) {
|
||||
return readSocketRaw(i.SocketPath(), strings.NewReader(cmd))
|
||||
func (c *Client) Dispatch(cmd string) (io.ReadCloser, error) {
|
||||
return readSocketRaw(c.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd)))
|
||||
}
|
||||
|
||||
func (i *Instance) DispatchExpectOK(cmd string) error {
|
||||
str, err := readSocketString(i.SocketPath(), strings.NewReader(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
|
||||
}
|
||||
|
104
get_clients.go
Normal file
104
get_clients.go
Normal file
@ -0,0 +1,104 @@
|
||||
package hypr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.milar.in/milarin/slices"
|
||||
)
|
||||
|
||||
func GetClient(signature string) (*Client, error) {
|
||||
lockFilePath := fmt.Sprintf("/run/user/1000/hypr/%s/hyprland.lock", signature)
|
||||
|
||||
file, err := os.Open(lockFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lines := strings.Split(string(data), "\n")
|
||||
|
||||
pid, err := strconv.Atoi(lines[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
Signature: signature,
|
||||
PID: pid,
|
||||
WaylandSocket: lines[1],
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetDefaultClient() (*Client, error) {
|
||||
signature, ok := os.LookupEnv("HYPRLAND_INSTANCE_SIGNATURE")
|
||||
if !ok {
|
||||
return nil, errors.New("default instance not found because HYPRLAND_INSTANCE_SIGNATURE is not set")
|
||||
}
|
||||
|
||||
return GetClient(signature)
|
||||
}
|
||||
|
||||
func GetClients() ([]*Client, error) {
|
||||
entries, err := os.ReadDir("/run/user/1000/hypr")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entries = slices.Filter(entries, fs.DirEntry.IsDir)
|
||||
clients := make([]*Client, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
client, err := GetClient(entry.Name())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
clients = append(clients, client)
|
||||
}
|
||||
|
||||
return clients, nil
|
||||
}
|
||||
|
||||
func WaitForDefaultClient(ctx context.Context) (*Client, error) {
|
||||
signature, ok := os.LookupEnv("HYPRLAND_INSTANCE_SIGNATURE")
|
||||
if !ok {
|
||||
return nil, errors.New("default instance not found because HYPRLAND_INSTANCE_SIGNATURE is not set")
|
||||
}
|
||||
|
||||
return WaitForClient(ctx, signature)
|
||||
}
|
||||
|
||||
func WaitForClient(ctx context.Context, signature string) (*Client, error) {
|
||||
lockFilePath := fmt.Sprintf("/run/user/1000/hypr/%s/hyprland.lock", signature)
|
||||
|
||||
lockFileExists := waitFor(ctx, func() bool {
|
||||
_, err := os.Stat(lockFilePath)
|
||||
return !errors.Is(err, os.ErrNotExist)
|
||||
})
|
||||
|
||||
if !lockFileExists {
|
||||
return nil, errors.New("hyprland lock file not found")
|
||||
}
|
||||
|
||||
client, err := GetClient(signature)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !client.WaitUntilReachable(context.Background()) {
|
||||
return nil, errors.New("hyprland not reachable")
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
package hypr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.milar.in/milarin/slices"
|
||||
)
|
||||
|
||||
func GetInstance(signature string) (*Instance, error) {
|
||||
lockFilePath := fmt.Sprintf("/tmp/hypr/%s.lock", signature)
|
||||
|
||||
file, err := os.Open(lockFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lines := strings.Split(string(data), "\n")
|
||||
|
||||
pid, err := strconv.Atoi(lines[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Instance{
|
||||
Signature: signature,
|
||||
PID: pid,
|
||||
WaylandSocket: lines[1],
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetDefaultInstance() (*Instance, error) {
|
||||
signature, ok := os.LookupEnv("HYPRLAND_INSTANCE_SIGNATURE")
|
||||
if !ok {
|
||||
return nil, errors.New("default instance not found because HYPRLAND_INSTANCE_SIGNATURE is not set")
|
||||
}
|
||||
|
||||
return GetInstance(signature)
|
||||
}
|
||||
|
||||
func GetInstances() ([]*Instance, error) {
|
||||
entries, err := os.ReadDir("/tmp/hypr")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entries = slices.Filter(entries, fs.DirEntry.IsDir)
|
||||
instances := make([]*Instance, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
instance, err := GetInstance(entry.Name())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
instances = append(instances, instance)
|
||||
}
|
||||
|
||||
return instances, nil
|
||||
}
|
14
getters.go
14
getters.go
@ -4,30 +4,30 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (i *Instance) GetActiveWindow() (*Window, error) {
|
||||
func (i *Client) GetActiveWindow() (*Window, error) {
|
||||
return readSocket[*Window](i.SocketPath(), strings.NewReader("j/activewindow"))
|
||||
}
|
||||
|
||||
func (i *Instance) GetActiveWorkspace() (*Workspace, error) {
|
||||
func (i *Client) GetActiveWorkspace() (*Workspace, error) {
|
||||
return readSocket[*Workspace](i.SocketPath(), strings.NewReader("j/activeworkspace"))
|
||||
}
|
||||
|
||||
func (i *Instance) GetBinds() ([]*Bind, error) {
|
||||
func (i *Client) GetBinds() ([]*Bind, error) {
|
||||
return readSocket[[]*Bind](i.SocketPath(), strings.NewReader("j/binds"))
|
||||
}
|
||||
|
||||
func (i *Instance) GetWindows() ([]*Window, error) {
|
||||
func (i *Client) GetWindows() ([]*Window, error) {
|
||||
return readSocket[[]*Window](i.SocketPath(), strings.NewReader("j/clients"))
|
||||
}
|
||||
|
||||
func (i *Instance) GetCursorPos() (Point, error) {
|
||||
func (i *Client) GetCursorPos() (Point, error) {
|
||||
return readSocket[Point](i.SocketPath(), strings.NewReader("j/cursorpos"))
|
||||
}
|
||||
|
||||
func (i *Instance) GetMonitors() ([]*Monitor, error) {
|
||||
func (i *Client) GetMonitors() ([]*Monitor, error) {
|
||||
return readSocket[[]*Monitor](i.SocketPath(), strings.NewReader("j/monitors"))
|
||||
}
|
||||
|
||||
func (i *Instance) GetWorkspaces() ([]*Workspace, error) {
|
||||
func (i *Client) GetWorkspaces() ([]*Workspace, error) {
|
||||
return readSocket[[]*Workspace](i.SocketPath(), strings.NewReader("j/workspaces"))
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -1,4 +1,4 @@
|
||||
module git.milar.in/milarin/hypr
|
||||
module git.milar.in/hyprland-tools/hypr
|
||||
|
||||
go 1.21.6
|
||||
|
||||
|
@ -31,8 +31,10 @@ const (
|
||||
EventTypeActiveWindowV2 EventType = "activewindowv2"
|
||||
EventTypeKeyboardFocus EventType = "keyboardfocus"
|
||||
EventTypeMoveWorkspace EventType = "moveworkspace"
|
||||
EventTypeMoveWorkspaceV2 EventType = "moveworkspacev2"
|
||||
EventTypeFocusedMon EventType = "focusedmon"
|
||||
EventTypeMoveWindow EventType = "movewindow"
|
||||
EventTypeMoveWindowV2 EventType = "movewindowv2"
|
||||
EventTypeOpenLayer EventType = "openlayer"
|
||||
EventTypeCloseLayer EventType = "closelayer"
|
||||
EventTypeOpenWindow EventType = "openwindow"
|
||||
@ -40,12 +42,16 @@ const (
|
||||
EventTypeUrgent EventType = "urgent"
|
||||
EventTypeMinimize EventType = "minimize"
|
||||
EventTypeMonitorAdded EventType = "monitoradded"
|
||||
EventTypeMonitorAddedV2 EventType = "monitoraddedv2"
|
||||
EventTypeMonitorRemoved EventType = "monitorremoved"
|
||||
EventTypeCreateWorkspace EventType = "createworkspace"
|
||||
EventTypeCreateWorkspaceV2 EventType = "createworkspacev2"
|
||||
EventTypeDestroyWorkspace EventType = "destroyworkspace"
|
||||
EventTypeDestroyWorkspaceV2 EventType = "destroyworkspacev2"
|
||||
EventTypeFullscreen EventType = "fullscreen"
|
||||
EventTypeChangeFloatingMode EventType = "changefloatingmode"
|
||||
EventTypeWorkspace EventType = "workspace"
|
||||
EventTypeWorkspaceV2 EventType = "workspacev2"
|
||||
EventTypeSubmap EventType = "submap"
|
||||
EventTypeMouseMove EventType = "mousemove"
|
||||
EventTypeMouseButton EventType = "mousebutton"
|
||||
|
@ -5,21 +5,21 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
type Client struct {
|
||||
Signature string `json:"instance"`
|
||||
PID int `json:"pid"`
|
||||
WaylandSocket string `json:"wl_socket"`
|
||||
}
|
||||
|
||||
func (i Instance) String() string {
|
||||
data, _ := json.MarshalIndent(i, "", "\t")
|
||||
func (c Client) String() string {
|
||||
data, _ := json.MarshalIndent(c, "", "\t")
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func (i Instance) SocketPath() string {
|
||||
return fmt.Sprintf("/tmp/hypr/%s/.socket.sock", i.Signature)
|
||||
func (c Client) SocketPath() string {
|
||||
return fmt.Sprintf("/run/user/1000/hypr/%s/.socket.sock", c.Signature)
|
||||
}
|
||||
|
||||
func (i Instance) EventSocketPath() string {
|
||||
return fmt.Sprintf("/tmp/hypr/%s/.socket2.sock", i.Signature)
|
||||
func (c Client) EventSocketPath() string {
|
||||
return fmt.Sprintf("/run/user/1000/hypr/%s/.socket2.sock", c.Signature)
|
||||
}
|
||||
|
@ -3,30 +3,39 @@ package hypr
|
||||
import "encoding/json"
|
||||
|
||||
type Window struct {
|
||||
Address string `json:"address"`
|
||||
Mapped bool `json:"mapped"`
|
||||
Hidden bool `json:"hidden"`
|
||||
At [2]int `json:"at"`
|
||||
Size [2]int `json:"size"`
|
||||
Workspace WorkspaceIdent `json:"workspace"`
|
||||
Floating bool `json:"floating"`
|
||||
MonitorID int `json:"monitor"`
|
||||
Class string `json:"class"`
|
||||
Title string `json:"title"`
|
||||
InitialClass string `json:"initialClass"`
|
||||
InitialTitle string `json:"initialTitle"`
|
||||
PID int `json:"pid"`
|
||||
Xwayland bool `json:"xwayland"`
|
||||
Pinned bool `json:"pinned"`
|
||||
Fullscreen bool `json:"fullscreen"`
|
||||
FullscreenMode int `json:"fullscreenMode"`
|
||||
FakeFullscreen bool `json:"fakeFullscreen"`
|
||||
Grouped []interface{} `json:"grouped"` // TODO
|
||||
Swallowing string `json:"swallowing"`
|
||||
FocusHistoryID int `json:"focusHistoryID"`
|
||||
Address string `json:"address"`
|
||||
Mapped bool `json:"mapped"`
|
||||
Hidden bool `json:"hidden"`
|
||||
At [2]int `json:"at"`
|
||||
Size [2]int `json:"size"`
|
||||
Workspace WorkspaceIdent `json:"workspace"`
|
||||
Floating bool `json:"floating"`
|
||||
MonitorID int `json:"monitor"`
|
||||
Class string `json:"class"`
|
||||
Title string `json:"title"`
|
||||
InitialClass string `json:"initialClass"`
|
||||
InitialTitle string `json:"initialTitle"`
|
||||
PID int `json:"pid"`
|
||||
Xwayland bool `json:"xwayland"`
|
||||
Pinned bool `json:"pinned"`
|
||||
Fullscreen FullscreenState `json:"fullscreen"`
|
||||
FullscreenClient FullscreenState `json:"fullscreenClient"`
|
||||
Grouped []interface{} `json:"grouped"` // TODO
|
||||
Swallowing string `json:"swallowing"`
|
||||
FocusHistoryID int `json:"focusHistoryID"`
|
||||
}
|
||||
|
||||
func (w Window) String() string {
|
||||
data, _ := json.MarshalIndent(w, "", "\t")
|
||||
return string(data)
|
||||
}
|
||||
|
||||
type FullscreenState int
|
||||
|
||||
const (
|
||||
FullscreenCurrent FullscreenState = -1
|
||||
FullscreenNone FullscreenState = 0
|
||||
FullscreenMaximize FullscreenState = 1
|
||||
FullscreenFullscreen FullscreenState = 2
|
||||
FullscreenMaximizeFullscreen FullscreenState = 3
|
||||
)
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"git.milar.in/milarin/slices"
|
||||
)
|
||||
|
||||
func (i *Instance) Subscribe(ctx context.Context, events ...EventType) (<-chan Event, error) {
|
||||
func (i *Client) Subscribe(ctx context.Context, events ...EventType) (<-chan Event, error) {
|
||||
r, err := readSocketRaw(i.EventSocketPath(), strings.NewReader(""))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -16,6 +16,7 @@ func (i *Instance) Subscribe(ctx context.Context, events ...EventType) (<-chan E
|
||||
|
||||
out := make(chan Event, 10)
|
||||
eventMap := slices.ToStructMap(events)
|
||||
allEvents := len(events) == 0
|
||||
|
||||
go func() {
|
||||
defer r.Close()
|
||||
@ -25,7 +26,7 @@ func (i *Instance) Subscribe(ctx context.Context, events ...EventType) (<-chan E
|
||||
|
||||
for ctx.Err() == nil && sc.Scan() {
|
||||
event := parseEvent(sc.Text())
|
||||
if _, ok := eventMap[event.Type]; ok {
|
||||
if _, ok := eventMap[event.Type]; allEvents || ok {
|
||||
out <- event
|
||||
}
|
||||
}
|
||||
|
22
utils.go
22
utils.go
@ -1,9 +1,11 @@
|
||||
package hypr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
func readSocketRaw(socket string, body io.Reader) (io.ReadCloser, error) {
|
||||
@ -49,3 +51,23 @@ func readSocket[T any](socket string, body io.Reader) (T, error) {
|
||||
|
||||
return *value, nil
|
||||
}
|
||||
|
||||
func waitFor(ctx context.Context, condition func() bool) bool {
|
||||
if condition() {
|
||||
return true
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if condition() {
|
||||
return true
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user