1package core
2
3import (
4 "context"
5 "slices"
6
7 "github.com/bnema/neferwl/internal/ports"
8)
9
10// lockState is the session lock's surfaces and keyboard focus. A new
11// security epoch resets it whole.
12type lockState struct {
13 surfaces []ports.LockSurfacePlacement
14 focus WindowID
15 pinned bool // focus was clicked or typed into: it keeps focus
16}
17
18// syncSecurity runs only on the owner. The gate, not event selection order,
19// establishes the epoch; surface events can only fill in that exact epoch.
20func (c *Core) syncSecurity() bool {
21 if c.inputActive {
22 return false
23 }
24 if c.opts.Security == nil {
25 return false
26 }
27 state := c.opts.Security.Snapshot()
28 if state == c.security {
29 return false
30 }
31 c.security = state
32 c.lock = lockState{}
33 c.pressed, c.buttons, c.inputKeys = map[string]bool{}, map[uint32]bool{}, map[string]bool{}
34 c.pointer, c.grab, c.pointerOutput = 0, 0, ""
35 c.drag, c.swallow, c.mods, c.lastButton = nil, map[uint32]bool{}, 0, 0
36 c.pointerAt, c.motionTime = [2]float64{}, 0
37 c.keyboard = keyboard{}
38 c.constrained, c.constraint = ports.PointerConstrained{}, ports.PointerConstraint{}
39 if c.ch.Constraints != nil {
40 latest(c.ch.Constraints, c.constraint)
41 }
42 c.dropSwipe()
43 c.swipe = nil
44 // The gesture ends with the pointer focus: wayland cancels it when the
45 // protected epoch clears that focus.
46 c.gesture = clientGesture{}
47 c.stopAnimations()
48 c.stopPulse()
49 c.cancelSwitcher()
50 if state.Protected {
51 c.dropCaptureSessions()
52 }
53 return true
54}
55
56// Check spawns without adopting a new epoch midway through processing an
57// event: commands emitted by that event must retain its original owner epoch.
58func (c *Core) inputEpochChanged() bool {
59 return c.inputActive && c.opts.Security != nil && c.opts.Security.Snapshot() != c.security
60}
61
62func (c *Core) protectionRequested() bool {
63 return c.security.Protected || c.opts.Security != nil && c.opts.Security.Snapshot() != c.security
64}
65
66func (c *Core) securityCheckpoint(ctx context.Context) error {
67 if !c.syncSecurity() {
68 return nil
69 }
70 // Clear display focus even if the new epoch has no mapped lock surfaces.
71 if err := c.pointerFocus(ctx, ports.PointerFocus{}); err != nil {
72 return err
73 }
74 if err := c.command(ctx, ports.FocusWindow{}); err != nil {
75 return err
76 }
77 return c.publish(ctx)
78}
79
80// blockProtected reports whether a protected session drops ev. A dropped
81// fullscreen request is still answered, at the first publish after unlock.
82func (c *Core) blockProtected(ev ports.ClientEvent) bool {
83 if !c.security.Protected || !blockedProtectedEvent(ev) {
84 return false
85 }
86 if v, ok := ev.(ports.WindowFullscreenRequest); ok {
87 c.configures.answer[v.ID] = true
88 }
89 return true
90}
91
92func blockedProtectedEvent(ev ports.ClientEvent) bool {
93 switch ev.(type) {
94 case ports.WindowActivate, ports.WorkspaceActivate, ports.PointerWarp, ports.PointerConstrained,
95 ports.PopupRequest, ports.ShortcutsInhibit, ports.WindowFullscreenRequest,
96 ports.CaptureSessionOpen, ports.CaptureFrameTaken, ports.CaptureExclusionBegin, ports.CaptureExclusionLayer,
97 ports.WindowMoveRequest:
98 return true
99 }
100 return false
101}
102
103func (c *Core) applyLockChanged(v ports.SessionLockChanged) bool {
104 if c.opts.Security == nil || v.State != c.security || v.State != c.opts.Security.Snapshot() {
105 return false
106 }
107 if !c.security.Protected {
108 return true
109 }
110 c.lock.surfaces = slices.Clone(v.Surfaces)
111 c.lockKeyboardFocus()
112 // A destroyed or resized role must not retain a pointer grab.
113 if !c.validLockID(c.pointer) {
114 c.pointer = 0
115 }
116 if !c.validLockID(c.grab) {
117 c.grab = 0
118 clear(c.buttons)
119 }
120 return true
121}
122
123func (c *Core) lockSurface(sc *screen) (ports.LockSurfacePlacement, bool) {
124 o := sc.mon.Output()
125 for _, s := range c.lock.surfaces {
126 if s.ID != 0 && sc.name() != "" && s.Output == sc.name() && s.Width == o.W && s.Height == o.H && o.W > 0 && o.H > 0 {
127 return s, true
128 }
129 }
130 return ports.LockSurfacePlacement{}, false
131}
132
133func (c *Core) validLockID(id WindowID) bool {
134 if id == 0 {
135 return false
136 }
137 for _, sc := range c.screens {
138 if s, ok := c.lockSurface(sc); ok && s.ID == id {
139 return true
140 }
141 }
142 return false
143}
144
145// lockKeyboardFocus prefers the lock surface of the focused output, where the
146// user is looking, then any other. A surface the user clicked or typed into is
147// pinned and keeps focus. Until then, focus moves to the focused output's
148// surface once it maps: lock clients map their surfaces one by one, in any
149// order.
150func (c *Core) lockKeyboardFocus() WindowID {
151 valid := c.validLockID(c.lock.focus)
152 if valid && c.lock.pinned {
153 return c.lock.focus
154 }
155 if c.focusScreen >= 0 && c.focusScreen < len(c.screens) {
156 if s, ok := c.lockSurface(c.screens[c.focusScreen]); ok {
157 c.lock.focus, c.lock.pinned = s.ID, false
158 return c.lock.focus
159 }
160 }
161 if valid {
162 return c.lock.focus
163 }
164 c.lock.focus, c.lock.pinned = 0, false
165 for _, sc := range c.screens {
166 if s, ok := c.lockSurface(sc); ok {
167 c.lock.focus = s.ID
168 break
169 }
170 }
171 return c.lock.focus
172}
173
174func (c *Core) lockHit(x, y float64) (WindowID, float64, float64) {
175 i := c.screenAt(x, y)
176 if i < 0 {
177 return 0, 0, 0
178 }
179 sc := c.screens[i]
180 if s, ok := c.lockSurface(sc); ok {
181 return s.ID, x - float64(sc.x), y - float64(sc.y)
182 }
183 return 0, 0, 0
184}
185
186func (c *Core) publishProtected(ctx context.Context) error {
187 if err := c.syncOutputs(ctx); err != nil {
188 return err
189 }
190 focus := c.lockKeyboardFocus()
191 scenes := make([]ports.Scene, 0, len(c.screens))
192 for _, sc := range c.screens {
193 c.seq++
194 // The desktop scene that follows must not reuse a Seq from before.
195 sc.last = ports.Scene{}
196 sc.settledLayout, sc.shown = nil, nil
197 o := sc.mon.Output()
198 scene := ports.Scene{Security: c.security, Output: sc.name(), Seq: c.seq, OutputWidth: o.W, OutputHeight: o.H, Scale: sc.scale, Transform: sc.transform, Off: sc.off, Background: "#000000"}
199 if s, ok := c.lockSurface(sc); ok {
200 scene.Windows = []ports.SceneWindow{{ID: s.ID, Rect: Rect{W: o.W, H: o.H}, Fullscreen: true, Focused: s.ID == focus}}
201 }
202 scenes = append(scenes, scene)
203 }
204 if err := c.syncFocus(ctx, focus); err != nil {
205 return err
206 }
207 // Surface updates may invalidate the role under a stationary pointer.
208 if !c.validLockID(c.pointer) {
209 c.pointer = 0
210 }
211 if err := c.pointerFocus(ctx, ports.PointerFocus{ID: c.pointer, X: c.pointerAt[0], Y: c.pointerAt[1]}); err != nil {
212 return err
213 }
214 latest(c.ch.Scenes, scenes)
215 c.publishState()
216 c.publishWorkspaces()
217 return nil
218}
219
220func (c *Core) admitInput(ev ports.InputEvent) (ports.InputEvent, bool) {
221 if v, ok := ev.(ports.SecurityInput); ok {
222 if v.State != c.security || v.Event == nil {
223 return nil, false
224 }
225 ev = v.Event
226 } else if c.opts.Security != nil || c.security != (ports.SecurityState{}) {
227 return nil, false
228 }
229 // The producer resets native keymap state and quarantines physically held
230 // keys on transitions. Core preserves that keymap-aware modifier state;
231 // it must not infer modifier meaning from evdev codes or XKB bit positions.
232 // No release without a press in this epoch (password keys must not reach
233 // the desktop after unlock). Standalone legacy channel tests remain raw.
234 if c.opts.Security != nil {
235 if b, ok := ev.(ports.PointerButton); ok && !b.Pressed && !c.buttons[b.Button] {
236 return nil, false
237 }
238 if k, ok := ev.(ports.KeyEvent); ok {
239 held := heldKey(k)
240 if k.Pressed {
241 c.inputKeys[held] = true
242 } else {
243 if !c.inputKeys[held] {
244 return nil, false
245 }
246 delete(c.inputKeys, held)
247 }
248 }
249 }
250 return ev, true
251}
252
253func (c *Core) protectedInput(ctx context.Context, ev ports.InputEvent) error {
254 switch v := ev.(type) {
255 case ports.KeyEvent:
256 if id := c.lockKeyboardFocus(); id != 0 {
257 c.lock.pinned = true // typing must not move focus mid-secret
258 return c.command(ctx, ports.ForwardKey{ID: id, Key: v})
259 }
260 case ports.PointerMotion:
261 c.motionTime = v.Time
262 c.cursorX, c.cursorY = c.clampPointer(c.cursorX, c.cursorY, v.X, v.Y)
263 id, x, y := c.lockHit(c.cursorX, c.cursorY)
264 if id != c.pointer {
265 c.pointer = id
266 if err := c.pointerFocus(ctx, ports.PointerFocus{ID: id, X: x, Y: y}); err != nil {
267 return err
268 }
269 }
270 c.pointerAt = [2]float64{x, y}
271 if id != 0 {
272 return c.command(ctx, ports.PointerMotionTo{ID: id, X: x, Y: y, DX: v.DX, DY: v.DY, UnaccelDX: v.UnaccelDX, UnaccelDY: v.UnaccelDY, Time: v.Time})
273 }
274 case ports.PointerButton:
275 id := c.pointer
276 if c.grab != 0 {
277 id = c.grab
278 }
279 if !v.Pressed && !c.buttons[v.Button] {
280 return nil
281 }
282 if v.Pressed {
283 if len(c.buttons) == 0 {
284 c.grab = id
285 }
286 c.buttons[v.Button] = true
287 } else {
288 delete(c.buttons, v.Button)
289 }
290 if c.validLockID(id) {
291 if v.Pressed {
292 changed := c.lock.focus != id
293 c.lock.focus, c.lock.pinned = id, true
294 if changed {
295 if err := c.publishProtected(ctx); err != nil {
296 return err
297 }
298 }
299 }
300 if err := c.command(ctx, ports.PointerButtonTo{ID: id, Button: v.Button, Pressed: v.Pressed, Time: v.Time}); err != nil {
301 return err
302 }
303 }
304 if len(c.buttons) == 0 {
305 c.grab = 0
306 }
307 case ports.PointerAxis:
308 if c.validLockID(c.pointer) {
309 return c.command(ctx, ports.PointerAxisTo{ID: c.pointer, Axis: v})
310 }
311 }
312 return nil
313}