v0.9.0 458251f

security.go RawHistoryFolder

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

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 23

Tags 10

↑↓ moveenter openesc close