1package core
2
3import (
4 "cmp"
5 "slices"
6
7 "github.com/bnema/neferwl/internal/ports"
8)
9
10// publishState sends the script-facing snapshot when it changed. It is
11// built in the owner's scratch every publish and copied only when sent: an
12// animation frame that changes nothing allocates nothing here.
13func (c *Core) publishState() {
14 if c.ch.State == nil {
15 return
16 }
17 c.scratch.state = c.stateInto(c.scratch.state)
18 // The first snapshot is always sent, even an empty one.
19 if c.sent.state.Outputs != nil && sameState(c.scratch.state, c.sent.state) {
20 return
21 }
22 st := cloneState(c.scratch.state)
23 c.sent.state = st
24 latest(c.ch.State, st)
25}
26
27// state is a fresh snapshot, the caller's to keep.
28func (c *Core) state() ports.State { return cloneState(c.stateInto(ports.State{})) }
29
30// stateInto builds the snapshot in st's storage: the result aliases it and
31// its Window points into its Windows. The placeholder screen is not an
32// output.
33func (c *Core) stateInto(st ports.State) ports.State {
34 st.Output, st.Window = c.cur().name(), nil
35 st.Outputs, st.Windows = st.Outputs[:0], st.Windows[:0]
36 // Workspaces keep their Columns storage across calls (see workspaceInto).
37 nws := 0
38 if c.scratch.shown == nil {
39 c.scratch.shown = map[WindowID]bool{}
40 }
41 shown := c.scratch.shown
42 focus, _ := c.cur().mon.Focused()
43 for _, s := range c.screens {
44 if s.name() == "" {
45 continue
46 }
47 m := s.mon
48 cur := m.Current()
49 o := ports.OutputState{Name: s.name(), Count: numbered(m), Workspace: cur.Name, WorkspaceID: cur.ID}
50 if i := indexOf(m.Workspaces, cur); i >= 0 {
51 o.Active = i + 1
52 }
53 st.Outputs = append(st.Outputs, o)
54 // Visible follows the configures: drawn on the output.
55 clear(shown)
56 // Read at once, into publish's scratch: the snapshot holds no
57 // placement.
58 c.realBuf = m.layoutInto(c.realBuf)
59 view := m.viewport()
60 for _, p := range c.realBuf {
61 shown[p.ID] = view.shows(p)
62 }
63 for w := range m.all() {
64 n := indexOf(m.Workspaces, w) + 1
65 st.Workspaces = c.workspaceInto(st.Workspaces, nws, w, s.name(), n, w == cur)
66 nws++
67 c.scratch.ids = w.appendWindows(c.scratch.ids[:0])
68 for _, id := range c.scratch.ids {
69 rec := c.windows.lookup(id)
70 v := ports.WindowState{ID: id, AppID: rec.client.AppID, PID: rec.client.PID, Output: s.name(), Workspace: n, WorkspaceID: w.ID, WorkspaceName: w.Name, Visible: shown[id], IdleInhibit: rec.idleInhibit, Floating: w.isFloat(id)}
71 if i := w.stashIndex(id); i >= 0 {
72 v.StashIndex, v.StashCount, v.Hidden = i+1, len(w.Stash), w.stashHidden
73 }
74 if !v.Floating {
75 v.Column, v.Row = w.cell(id)
76 }
77 st.Windows = append(st.Windows, v)
78 }
79 }
80 }
81 st.Workspaces = st.Workspaces[:nws]
82 slices.SortFunc(st.Windows, func(a, b ports.WindowState) int { return cmp.Compare(a.ID, b.ID) })
83 for i := range st.Windows {
84 if st.Windows[i].ID == focus && focus != 0 {
85 st.Window = &st.Windows[i]
86 }
87 }
88 return st
89}
90
91// workspaceInto fills ws[i] (appended when ws is shorter) with w's settled
92// geometry, reusing the storage the element held: no slide, fade or zoom,
93// floats and the stash left out. A fullscreen tiled window covers the output.
94func (c *Core) workspaceInto(ws []ports.WorkspaceState, i int, w *Workspace, output string, number int, visible bool) []ports.WorkspaceState {
95 if i < cap(ws) {
96 ws = ws[:i+1]
97 } else {
98 ws = append(ws, ports.WorkspaceState{})
99 }
100 v := &ws[i]
101 cols := v.Columns[:0]
102 *v = ports.WorkspaceState{ID: w.ID, Number: number, Name: w.Name, Output: output, Visible: visible, Overflow: string(w.Overflow)}
103 c.scratch.rects = w.columnRectsAt(c.scratch.rects, false, 0)
104 for ci, col := range w.Columns {
105 if ci < cap(cols) {
106 cols = cols[:ci+1]
107 } else {
108 cols = append(cols, ports.ColumnState{})
109 }
110 cv := &cols[ci]
111 tiles := cv.Windows[:0]
112 r := c.scratch.rects[ci]
113 c.scratch.rows = rowRectsInto(c.scratch.rows[:0], r, col, w.gap())
114 for ri, id := range col.Windows {
115 tr := c.scratch.rows[ri]
116 if w.fullscreen == id {
117 tr = w.Output
118 }
119 tiles = append(tiles, ports.TileState{ID: id, Rect: tr})
120 }
121 *cv = ports.ColumnState{Rect: r, Windows: tiles}
122 }
123 v.Columns = cols
124 return ws
125}
126
127func sameState(a, b ports.State) bool {
128 return a.Output == b.Output && slices.Equal(a.Outputs, b.Outputs) && slices.Equal(a.Windows, b.Windows) &&
129 slices.EqualFunc(a.Workspaces, b.Workspaces, func(x, y ports.WorkspaceState) bool {
130 return x.ID == y.ID && x.Number == y.Number && x.Name == y.Name && x.Output == y.Output && x.Visible == y.Visible && x.Overflow == y.Overflow &&
131 slices.EqualFunc(x.Columns, y.Columns, func(p, q ports.ColumnState) bool {
132 return p.Rect == q.Rect && slices.Equal(p.Windows, q.Windows)
133 })
134 }) &&
135 (a.Window == nil) == (b.Window == nil) && (a.Window == nil || *a.Window == *b.Window)
136}
137
138// cloneState copies st out of the scratch: empty lists stay non-nil (JSON
139// []), and Window is its own copy.
140func cloneState(st ports.State) ports.State {
141 out := ports.State{Output: st.Output, Outputs: append([]ports.OutputState{}, st.Outputs...), Windows: append([]ports.WindowState{}, st.Windows...)}
142 out.Workspaces = make([]ports.WorkspaceState, len(st.Workspaces))
143 for i, w := range st.Workspaces {
144 out.Workspaces[i] = w
145 out.Workspaces[i].Columns = make([]ports.ColumnState, len(w.Columns))
146 for j, col := range w.Columns {
147 out.Workspaces[i].Columns[j] = ports.ColumnState{Rect: col.Rect, Windows: append([]ports.TileState{}, col.Windows...)}
148 }
149 }
150 if st.Window != nil {
151 w := *st.Window
152 out.Window = &w
153 }
154 return out
155}
156
157// numbered counts numbered workspaces, without the trailing spare one
158// unless it is on screen.
159func numbered(m *Monitor) int {
160 n := len(m.Workspaces)
161 if last := m.Workspaces[n-1]; n > 1 && last.empty() && last.Name == "" && m.Current() != last {
162 n--
163 }
164 return n
165}