Files
swayipc/node.go

117 lines
3.4 KiB
Go
Raw Normal View History

2025-08-16 19:50:30 +02:00
package swayipc
2025-08-14 14:16:05 +02:00
import (
"encoding/json"
"fmt"
)
const (
NodeTypeRoot = iota + 1 // A root node
NodeTypeOutput // An output node
NodeTypeWorkspace // A workspace
NodeTypeCon // A container node (containing more nodes)
NodeTypeFloating // A floating node
)
// Node can be an output, a workspace, a container or a container
// containing other containers.
2025-08-14 14:16:05 +02:00
type Node struct {
ID int `json:"id"`
2025-08-14 14:16:05 +02:00
Type string `json:"type"` // output, workspace or container
Name string `json:"name"` // workspace number or app name
Output string `json:"output"`
2025-08-14 14:16:05 +02:00
Nodes []*Node `json:"nodes"`
FloatingNodes []*Node `json:"floating_nodes"`
Focused bool `json:"focused"`
Visible bool `json:"visible"`
2025-08-14 14:16:05 +02:00
Urgent bool `json:"urgent"`
Sticky bool `json:"sticky"`
Border string `json:"border"`
Layout string `json:"layout"`
Orientation string `json:"orientation"`
CurrentBorderWidth int `json:"current_border_width"`
Percent float32 `json:"percent"`
Focus []int `json:"focus"`
Window int `json:"window"` // wayland native
X11Window string `json:"app_id"` // x11 compat
CurrentWorkspace string `json:"current_workspace"`
2025-08-14 14:16:05 +02:00
Rect Rect `json:"rect"`
WindowRect Rect `json:"window_rect"`
DecoRect Rect `json:"deco_rect"`
Geometry Rect `json:"geometry"`
}
// GetTree returns the whole information tree, which contains
// everything from output to containers as a tree of nodes. Each node
// has a field 'Nodes' which points to a list subnodes. Some nodes
// also have a field 'FloatingNodes' which points to a list of
// floating containers.
//
// The top level node is the "root" node.
//
// Use the returned node oject to further investigate the wm setup.
2025-08-16 19:50:30 +02:00
func (ipc *SwayIPC) GetTree() (*Node, error) {
err := ipc.sendHeader(MsgGettTree, 0)
if err != nil {
return nil, err
}
payload, err := ipc.readResponse()
if err != nil {
return nil, err
}
node := &Node{}
if err := json.Unmarshal(payload.Payload, &node); err != nil {
return nil, fmt.Errorf("failed to unmarshal json: %w", err)
}
return node, nil
}
// FindFocused returns the container which has currently the
// focus. Usually called on the root node.
2025-08-14 14:16:05 +02:00
func (node *Node) FindFocused() *Node {
focused := searchFocused(node.Nodes)
if focused == nil {
return searchFocused(node.FloatingNodes)
2025-08-14 14:16:05 +02:00
}
return nil
2025-08-14 14:16:05 +02:00
}
// FindCurrentWorkspace returns the current active
// workspace name. Usually called on the root node.
func (node *Node) FindCurrentWorkspace() string {
return searchCurrentWorkspace(node.Nodes)
}
// searchCurrentWorkspace search for current workspace
func searchCurrentWorkspace(nodes []*Node) string {
2025-08-14 14:16:05 +02:00
for _, node := range nodes {
if node.CurrentWorkspace != "" {
return node.CurrentWorkspace
2025-08-14 14:16:05 +02:00
} else {
return searchCurrentWorkspace(node.Nodes)
2025-08-14 14:16:05 +02:00
}
}
return ""
2025-08-14 14:16:05 +02:00
}
// searchFocused recursively search focused node
func searchFocused(nodes []*Node) *Node {
2025-08-14 14:16:05 +02:00
for _, node := range nodes {
if node.Focused {
return node
2025-08-14 14:16:05 +02:00
} else {
focused := searchFocused(node.Nodes)
if focused == nil {
return searchFocused(node.FloatingNodes)
}
2025-08-14 14:16:05 +02:00
}
}
return nil
2025-08-14 14:16:05 +02:00
}