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