sway/types.go

441 lines
13 KiB
Go
Raw Normal View History

2024-02-14 15:12:39 +01:00
package sway
import (
"encoding/json"
"errors"
)
type NodeID int64
type Rectangle struct {
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
}
func (r Rectangle) String() string {
data, _ := json.MarshalIndent(r, "", "\t")
return string(data)
}
type Workspace struct {
Num int `json:"num"`
Name string `json:"name"`
Visible bool `json:"visible"`
Focused bool `json:"focused"`
Urgent bool `json:"urgent"`
Rect Rectangle `json:"rect"`
Output string `json:"output"`
}
func (w Workspace) String() string {
data, _ := json.MarshalIndent(w, "", "\t")
return string(data)
}
2024-02-14 15:26:04 +01:00
type Output struct {
Name string `json:"name"`
Make string `json:"make"`
Model string `json:"model"`
Serial string `json:"serial"`
Active bool `json:"active"`
DPMS bool `json:"dpms"`
Power bool `json:"power"`
Scale float64 `json:"scale"`
SubPixelHinting SubPixelHinting `json:"subpixel_hinting"`
Transform Transform `json:"transform"`
CurrentWorkspace string `json:"current_workspace"`
Modes []Mode `json:"modes"`
CurrentMode Mode `json:"current_mode"`
Rect Rectangle `json:"rect"`
}
func (o Output) String() string {
data, _ := json.MarshalIndent(o, "", "\t")
return string(data)
}
type BarConfig struct {
ID string `json:"id"`
Mode BarMode `json:"mode"`
Position BarPosition `json:"position"`
StatusCommand string `json:"status_command"`
Font string `json:"font"`
WorkspaceButtons bool `json:"workspace_buttons"`
WorkspaceMinWidth int `json:"workspace_min_width"`
BindingModeIndicator bool `json:"binding_mode_indicator"`
Colors BarColors `json:"colors"`
Gaps BarGaps `json:"gaps"`
BarHeight int `json:"bar_height"`
StatusPadding int `json:"status_padding"`
StatusEdgePadding int `json:"status_edge_padding"`
}
func (bc BarConfig) String() string {
data, _ := json.MarshalIndent(bc, "", "\t")
return string(data)
}
2024-02-14 15:12:39 +01:00
type WindowProperties struct {
Title string `json:"title"`
Instance string `json:"instance"`
Class string `json:"class"`
Role string `json:"window_role"`
Transient NodeID `json:"transient_for"`
}
func (w WindowProperties) String() string {
data, _ := json.MarshalIndent(w, "", "\t")
return string(data)
}
type Node struct {
ID NodeID `json:"id"`
Name string `json:"name"`
Type NodeType `json:"type"`
Border BorderStyle `json:"border"`
CurrentBorderWidth int `json:"current_border_width"`
Layout Layout `json:"layout"`
Orientation Orientation `json:"orientation"`
Percent float64 `json:"percent"`
Rect Rectangle `json:"rect"`
WindowRect Rectangle `json:"window_rect"`
DecoRect Rectangle `json:"deco_rect"`
Geometry Rectangle `json:"geometry"`
Urgent bool `json:"urgent"`
Sticky bool `json:"sticky"`
Marks []string `json:"marks"`
2024-02-14 15:12:39 +01:00
Focused bool `json:"focused"`
Focus []NodeID `json:"focus"`
2024-02-14 15:12:39 +01:00
Nodes []Node `json:"nodes"`
FloatingNodes []Node `json:"floating_nodes"`
Representation string `json:"representation"`
FullscreenMode FullscreenMode `json:"fullscreen_mode"`
AppID string `json:"app_id"`
PID int `json:"pid"`
Visible bool `json:"visible"`
Shell string `json:"shell"`
InhibitIdle bool `json:"inhibit_idle"`
Window int `json:"window"`
WindowProperties WindowProperties `json:"window_properties"`
// The following fields are undocumented. Use with caution!
// They are included in sway as well as in i3 for years though
Num int `json:"num"` // workspace number
Output string `json:"output"` // output port name
2024-02-14 15:12:39 +01:00
}
func (n Node) String() string {
data, _ := json.MarshalIndent(n, "", "\t")
return string(data)
}
2024-02-14 17:21:50 +01:00
// Find recursively searches n and returns the node
// for which condition returned true.
// condition is called in depth-first order:
// 1. for n
// 2. for n.Nodes (in order)
// 3. for n.FloatingNodes (in order)
//
// If no matching node is found, Find returns nil
func (n *Node) Find(condition func(n *Node) bool) *Node {
if condition(n) {
return n
}
for _, child := range n.Nodes {
if node := child.Find(condition); node != nil {
return node
}
}
for _, child := range n.FloatingNodes {
if node := child.Find(condition); node != nil {
return node
}
}
return nil
}
// FindAll behaves like Find but returns all matching nodes
func (n *Node) FindAll(condition func(n *Node) bool) []*Node {
nodes := []*Node{}
if condition(n) {
nodes = append(nodes, n)
}
for _, child := range n.Nodes {
nodes = append(nodes, child.FindAll(condition)...)
}
for _, child := range n.FloatingNodes {
nodes = append(nodes, child.FindAll(condition)...)
}
return nodes
}
2024-02-14 15:12:39 +01:00
type NodeType = string
const (
NodeTypeRoot NodeType = "root"
NodeTypeOutput NodeType = "output"
NodeTypeWorkspace NodeType = "workspace"
NodeTypeContainer NodeType = "con"
NodeTypeFloatingContainer NodeType = "floating_con"
)
type BorderStyle = string
const (
BorderStyleNormal BorderStyle = "normal"
BorderStyleNone BorderStyle = "none"
BorderStylePixel BorderStyle = "pixel"
BorderStyleCSD BorderStyle = "csd"
)
type Layout = string
const (
LayoutSplitH Layout = "splith"
LayoutSplitV Layout = "splitv"
LayoutStacked Layout = "stacked"
LayoutTabbed Layout = "tabbed"
LayoutOutput Layout = "output"
)
type Orientation = string
const (
OrientationVertical Orientation = "vertical"
OrientationHorizontal Orientation = "horizontal"
OrientationNone Orientation = "none"
)
type FullscreenMode = uint8
const (
FullscreenModeNone FullscreenMode = iota
FullscreenModeWorkspace FullscreenMode = iota
FullscreenModeGlobal FullscreenMode = iota
)
type WorkspaceEventChange = string
const (
WorkspaceEventChangeInit WorkspaceEventChange = "init"
WorkspaceEventChangeEmpty WorkspaceEventChange = "empty"
WorkspaceEventChangeFocus WorkspaceEventChange = "focus"
WorkspaceEventChangeMove WorkspaceEventChange = "move"
WorkspaceEventChangeRename WorkspaceEventChange = "rename"
WorkspaceEventChangeUrgent WorkspaceEventChange = "urgent"
WorkspaceEventChangeReload WorkspaceEventChange = "reload"
)
type WindowEventChange = string
const (
WindowEventChangeNew WindowEventChange = "new"
WindowEventChangeClose WindowEventChange = "close"
WindowEventChangeFocus WindowEventChange = "focus"
WindowEventChangeTitle WindowEventChange = "title"
WindowEventChangeFullscreenMode WindowEventChange = "fullscreen_mode"
WindowEventChangeMove WindowEventChange = "move"
WindowEventChangeFloating WindowEventChange = "floating"
WindowEventChangeUrgent WindowEventChange = "urgent"
WindowEventChangeMark WindowEventChange = "mark"
)
type InputEventChange = string
const (
InputEventChangeAdded InputEventChange = "added"
InputEventChangeRemoved InputEventChange = "removed"
InputEventChangeXKBKeymap InputEventChange = "xkb_kemap"
InputEventChangeXKBLayout InputEventChange = "xkb_layout"
InputEventChangeLibInputConfig InputEventChange = "libinput_config"
)
type InputType = string
const (
InputTypeKeyboard InputType = "keyboard"
InputTypeMouse InputType = "mouse"
)
2024-02-14 15:26:04 +01:00
type SubPixelHinting = string
const (
SubPixelHintingRGB SubPixelHinting = "rgb"
SubPixelHintingBGR SubPixelHinting = "bgr"
SubPixelHintingVRGB SubPixelHinting = "vrgb"
SubPixelHintingVBGR SubPixelHinting = "vbgr"
SubPixelHintingNone SubPixelHinting = "none"
)
type Transform = string
const (
Transform90 Transform = "90"
Transform180 Transform = "180"
Transform270 Transform = "270"
TransformFlipped90 Transform = "flipped-90"
TransformFlipped180 Transform = "flipped-180"
TransformFlipped270 Transform = "flipped-270"
)
type BarMode = string
const (
BarModeDock BarMode = "dock"
BarModeHide BarMode = "hide"
barModeInvisible BarMode = "invisible"
)
type BarPosition = string
const (
BarPositionTop BarPosition = "top"
BarPositionBottom BarPosition = "bottom"
)
2024-02-14 16:48:50 +01:00
type InputDeviceType = string
const (
InputDeviceTypeKeyboard InputDeviceType = "keyboard"
InputDeviceTypePointer InputDeviceType = "pointer"
InputDeviceTypeTouch InputDeviceType = "touch"
InputDeviceTypeTabletTool InputDeviceType = "tablet_tool"
InputDeviceTypeTabletPad InputDeviceType = "tablet_pad"
InputDeviceTypeSwitch InputDeviceType = "switch"
)
2024-02-14 15:12:39 +01:00
type commandResult struct {
Success bool `json:"success"`
ParseError bool `json:"parse_error"`
Error string `json:"error"`
}
func (r commandResult) String() string {
data, _ := json.MarshalIndent(r, "", "\t")
return string(data)
}
func (r commandResult) GetError() error {
return errors.New(r.Error)
}
func (r commandResult) HasError() bool {
return !r.Success
}
2024-02-14 15:26:04 +01:00
type Mode struct {
Width int `json:"width"`
Height int `json:"height"`
Refresh int `json:"refresh"`
}
type BarColors struct {
Background string `json:"background"`
Statusline string `json:"statusline"`
Separator string `json:"separator"`
FocusedBackground string `json:"focused_background"`
FocusedStatusline string `json:"focused_statusline"`
FocusedSeparator string `json:"focused_separator"`
FocusedWorkspaceText string `json:"focused_workspace_text"`
FocusedWorkspaceBackground string `json:"focused_workspace_bg"`
FocusedWorkspaceBorder string `json:"focused_workspace_border"`
ActiveWorkspaceText string `json:"active_workspace_text"`
ActiveWorkspaceBackground string `json:"active_workspace_bg"`
ActiveWorkspaceBorder string `json:"active_workspace_border"`
InactiveWorkspaceText string `json:"inactive_workspace_text"`
InactiveWorkspaceBackground string `json:"inactive_workspace_bg"`
InactiveWorkspaceBorder string `json:"inactive_workspace_border"`
UrgentWorkspaceText string `json:"urgent_workspace_text"`
UrgentWorkspaceBackground string `json:"urgent_workspace_bg"`
UrgentWorkspaceBorder string `json:"urgent_workspace_border"`
BindingModeText string `json:"binding_mode_text"`
BindingModeBackground string `json:"binding_mode_bg"`
BindingModeBorder string `json:"binding_mode_border"`
}
type BarGaps struct {
Top int `json:"top"`
Right int `json:"right"`
Bottom int `json:"bottom"`
Left int `json:"left"`
}
type Version struct {
Major int `json:"major"`
Minor int `json:"minor"`
Patch int `json:"patch"`
HumanReadable string `json:"human_readable"`
LoadedConfigFileName string `json:"loaded_config_file_name"`
}
func (v Version) String() string {
data, _ := json.MarshalIndent(v, "", "\t")
return string(data)
}
type config struct {
Config string `json:"config"`
}
type name struct {
Name string `json:"name"`
}
2024-02-14 16:48:50 +01:00
type InputDevice struct {
Identifier string `json:"identifier"`
Name string `json:"name"`
Vendor int `json:"vendor"`
Product int `json:"product"`
Type InputDeviceType `json:"type"`
XKBActiveLayoutName string `json:"xkb_active_layout_name"`
XKBLayoutNames []string `json:"xkb_layout_names"`
XKBActiveLayoutIndex int `json:"xkb_active_layout_index"`
ScrollFactor float64 `json:"scroll_factor"`
LibInput LibInput `json:"libinput"`
}
func (id InputDevice) String() string {
data, _ := json.MarshalIndent(id, "", "\t")
return string(data)
}
type LibInput struct {
SendEvents string `json:"send_events"`
Tap string `json:"tap"`
TapButtonMap string `json:"tap_button_map"`
TapDrag string `json:"tap_drag"`
TapDragLock string `json:"tap_drag_lock"`
AccelSpeed float64 `json:"accel_speed"`
AccelProfile string `json:"accel_profile"`
NaturalScroll string `json:"natural_scroll"`
LeftHanded string `json:"left_handed"`
ClickMethod string `json:"click_method"`
MiddleEmulation string `json:"middle_emulation"`
ScrollMethod string `json:"scroll_method"`
ScrollButton int `json:"scroll_button"`
Dwt string `json:"dwt"`
Dwtp string `json:"dwtp"`
CalibrationMatrix [6]float64 `json:"calibration_matrix"`
}
2024-02-14 16:51:23 +01:00
type Seat struct {
Name string `json:"name"`
Capabilities int `json:"capabilities"`
Focus NodeID `json:"focus"`
Devices []InputDevice `json:"devices"`
}
func (s Seat) String() string {
data, _ := json.MarshalIndent(s, "", "\t")
return string(data)
}