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 if c.scratch.shown == nil {
37 c.scratch.shown = map[WindowID]bool{}
38 }
39 shown := c.scratch.shown
40 focus, _ := c.cur().mon.Focused()
41 for _, s := range c.screens {
42 if s.name() == "" {
43 continue
44 }
45 m := s.mon
46 cur := m.Current()
47 o := ports.OutputState{Name: s.name(), Count: numbered(m), Workspace: cur.Name, WorkspaceID: cur.ID}
48 if i := indexOf(m.Workspaces, cur); i >= 0 {
49 o.Active = i + 1
50 }
51 st.Outputs = append(st.Outputs, o)
52 // Visible follows the configures: drawn on the output.
53 clear(shown)
54 // Read at once, into publish's scratch: the snapshot holds no
55 // placement.
56 c.realBuf = m.layoutInto(c.realBuf)
57 view := m.viewport()
58 for _, p := range c.realBuf {
59 shown[p.ID] = view.shows(p)
60 }
61 for w := range m.all() {
62 n := indexOf(m.Workspaces, w) + 1
63 c.scratch.ids = w.appendWindows(c.scratch.ids[:0])
64 for _, id := range c.scratch.ids {
65 rec := c.windows.lookup(id)
66 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)}
67 if i := w.stashIndex(id); i >= 0 {
68 v.StashIndex, v.StashCount, v.Hidden = i+1, len(w.Stash), w.stashHidden
69 }
70 if !v.Floating {
71 v.Column, v.Row = w.cell(id)
72 }
73 st.Windows = append(st.Windows, v)
74 }
75 }
76 }
77 slices.SortFunc(st.Windows, func(a, b ports.WindowState) int { return cmp.Compare(a.ID, b.ID) })
78 for i := range st.Windows {
79 if st.Windows[i].ID == focus && focus != 0 {
80 st.Window = &st.Windows[i]
81 }
82 }
83 return st
84}
85
86func sameState(a, b ports.State) bool {
87 return a.Output == b.Output && slices.Equal(a.Outputs, b.Outputs) && slices.Equal(a.Windows, b.Windows) &&
88 (a.Window == nil) == (b.Window == nil) && (a.Window == nil || *a.Window == *b.Window)
89}
90
91// cloneState copies st out of the scratch: empty lists stay non-nil (JSON
92// []), and Window is its own copy.
93func cloneState(st ports.State) ports.State {
94 out := ports.State{Output: st.Output, Outputs: append([]ports.OutputState{}, st.Outputs...), Windows: append([]ports.WindowState{}, st.Windows...)}
95 if st.Window != nil {
96 w := *st.Window
97 out.Window = &w
98 }
99 return out
100}
101
102// numbered counts numbered workspaces, without the trailing spare one
103// unless it is on screen.
104func numbered(m *Monitor) int {
105 n := len(m.Workspaces)
106 if last := m.Workspaces[n-1]; n > 1 && last.empty() && last.Name == "" && m.Current() != last {
107 n--
108 }
109 return n
110}