fix: add open progress staging and forced key evidence to display_diag (#3)

This commit is contained in:
2026-11-28 14:43:32 +01:00
parent 903c5c69b6
commit acc721e7e7
5 changed files with 496 additions and 49 deletions
+117 -35
View File
@@ -430,6 +430,30 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
conn.Close()
return nil, fmt.Errorf("allocate window: %w", err)
}
// The open_begin marker is deduplicated by the emitter, so it is
// bounded to once per process and role no matter how often the layer
// reconnects.
if o.Diag != nil {
o.Diag.Emit("open_begin", roleName(o.Role))
}
// Stage markers run through the same emitter; the session is not built
// until Open completes, so the pre-session stages close over the
// opener's emitter directly.
stage := func(name string) {
if o.Diag != nil {
o.Diag.Emit("open_stage", roleName(o.Role), "stage="+name)
}
}
stageDone := func(name string, err error) {
if o.Diag == nil {
return
}
if err != nil {
o.Diag.Emit("open_stage_done", roleName(o.Role), "stage="+name, "err="+diagValue(err.Error()))
return
}
o.Diag.Emit("open_stage_done", roleName(o.Role), "stage="+name, "ok")
}
// The OSD overlay selects no device input even before its empty input
// shape exists; the canvas keeps its legacy exposure-only mask.
@@ -443,19 +467,24 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
// and the window still maps with the server default cursor — an
// always-visible main window takes priority over cursor hiding, and a
// cursor failure must never destroy or reconnect the display.
stage("cursor")
cursor, cursorErr := blankCursor(conn, screen)
if cursorErr != nil {
stageDone("cursor", cursorErr)
log.Printf("blank cursor unavailable (%v); mapping with the default cursor", cursorErr)
cursor = 0
} else {
}
valueMask, values := createWindowValues(screen.BlackPixel, eventMask, cursor)
// The window creation is checked at its own operation: a failure here
// is reported exactly, and no dependent request — properties, shapes,
// map, liveness — ever runs against an invalid window.
stage("window")
if err := xproto.CreateWindowChecked(conn, screen.RootDepth, window, screen.Root,
0, 0, screen.WidthInPixels, screen.HeightInPixels, 0,
xproto.WindowClassInputOutput, screen.RootVisual, valueMask,
values).Check(); err != nil {
stageDone("window", err)
// The window was never created: reclaim the connection (and the
// live cursor with a best-effort free) exactly like the other
// open failures, so reconnect attempts cannot leak either.
@@ -509,7 +538,9 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
// server without the shape extension never gets the overlay at all, so
// it can never present an unshaped frame over pilot video.
if o.Role == RoleAbove {
stage("shapes")
if err := applyTransparentShapes(conn, window); err != nil {
stageDone("shapes", err)
conn.Close()
return nil, err
}
@@ -520,10 +551,19 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
// destroy is video end. WFB and launcher processes are never consulted.
xproto.ChangeWindowAttributes(conn, screen.Root,
xproto.CwEventMask, []uint32{xproto.EventMaskSubstructureNotify})
// The open record is emitted the moment the core window is created and
// mapped, from already-known identifiers and configured geometry: the
// actual client geometry and shape/parent/stack evidence follow in the
// first-flush query bundle. Emitting here guarantees the open evidence
// survives even if a later reply round trip stalls the open.
emitOpenRecord(o.Diag, o.Role, window, screen.Root,
screen.WidthInPixels, screen.HeightInPixels, o.RefreshInterval)
// Round trip confirms the window is mapped and the server is alive;
// the reply-bearing cookie is resolved with Reply (see
// connLivenessProbe).
stage("map_confirm")
if err := confirmMapped(connLivenessProbe(conn)); err != nil {
stageDone("map_confirm", err)
conn.Close()
return nil, err
}
@@ -531,6 +571,7 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
// A layer reconnect must never cover running video: if video windows
// are already presented, record presence and take our side relative to
// the topmost one before anything is drawn.
stage("presence")
presence := o.presence()
typeAtom := internWindowTypeAtom(conn)
videoWindows := scanVideoWindows(conn, screen.Root, window, typeAtom, markerAtom)
@@ -549,8 +590,11 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
// raises itself only after video ends, to bring the fallback back; the
// OSD overlay is already above video. A missing wmctrl only degrades to
// a plain window and must not kill the layer.
if err := o.applyFullscreen(x11FullscreenArgs(uint32(window), o.Role)); err != nil {
log.Printf("kiosk fullscreen state: %v (window stays a plain window)", err)
stage("wmctrl")
fullscreenErr := o.applyFullscreen(x11FullscreenArgs(uint32(window), o.Role))
stageDone("wmctrl", fullscreenErr)
if fullscreenErr != nil {
log.Printf("kiosk fullscreen state: %v (window stays a plain window)", fullscreenErr)
}
gc, err := xproto.NewGcontextId(conn)
@@ -565,6 +609,7 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
}
// Prefer a Unicode font so the exact fallback text renders with its
// diacritics; without one, drawn text degrades to ASCII.
stage("font_list")
fontName, unicodeFont := selectFontName(conn)
xproto.OpenFont(conn, font, uint16(len(fontName)), fontName)
xproto.CreateGC(conn, gc, xproto.Drawable(window),
@@ -575,11 +620,14 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
// needs the real glyph metrics; the canvas never punches shapes.
ascent, descent, cell := osdFallbackFontMetrics()
if o.Role == RoleAbove {
stage("font_query")
if info, err := xproto.QueryFont(conn, xproto.Fontable(font)).Reply(); err == nil {
ascent, descent = int16(info.FontAscent), int16(info.FontDescent)
if width := int16(info.MaxBounds.CharacterWidth); width > 0 {
cell = width
}
} else {
stageDone("font_query", err)
}
}
@@ -611,39 +659,36 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
pollEvent: conn.PollForEvent,
probe: connLivenessProbe(conn),
}
s.recordOpen(o.RefreshInterval)
return s, nil
}
// recordOpen emits the one per-role open record: role, window and root
// identifiers, configured/root/client geometry, refresh interval, the EWMH
// type/state intent, and the initial shape intent. The one synchronous
// geometry round trip runs here, where first-open queries are allowed.
func (s *x11Session) recordOpen(refresh time.Duration) {
if s.diag == nil {
// emitOpenRecord emits the one per-role open record the moment the core
// window is created and mapped, from already-known identifiers, configured
// geometry and state intent. The actual client geometry and the
// shape/parent/stack evidence are deferred to the first-flush query
// bundle, so no extra blocking round trip is needed here and the open
// evidence survives even if a later reply round trip stalls the open.
func emitOpenRecord(diag *Diag, role LayerRole, window, root xproto.Window, width, height uint16, refresh time.Duration) {
if diag == nil {
return
}
clientGeom := "unknown"
if reply, err := xproto.GetGeometry(s.conn, xproto.Drawable(s.window)).Reply(); err == nil {
clientGeom = fmt.Sprintf("%dx%d+%d+%d", reply.Width, reply.Height, reply.X, reply.Y)
}
wmType := ewmhWindowTypeName(s.role)
wmType := ewmhWindowTypeName(role)
if wmType == "" {
wmType = "none"
}
shapeInit := "unshaped"
if s.role == RoleAbove {
if role == RoleAbove {
shapeInit = "empty/empty"
}
s.diag.Emit("open", roleName(s.role),
fmt.Sprintf("window=0x%x", s.window),
fmt.Sprintf("root=0x%x", s.root),
fmt.Sprintf("cfg=%dx%d", s.width, s.height),
fmt.Sprintf("root_geom=%dx%d", s.width, s.height),
"client_geom="+clientGeom,
diag.Emit("open", roleName(role),
fmt.Sprintf("window=0x%x", window),
fmt.Sprintf("root=0x%x", root),
fmt.Sprintf("cfg=%dx%d", width, height),
fmt.Sprintf("root_geom=%dx%d", width, height),
"client_geom=deferred",
"refresh="+refresh.String(),
"type="+wmType,
"wm_state="+strings.Join(fullscreenStateAtoms(s.role), "+"),
"wm_state="+strings.Join(fullscreenStateAtoms(role), "+"),
"shape_init="+shapeInit,
)
}
@@ -918,16 +963,17 @@ type x11Session struct {
// the snapshot state reported by the template, per-pass template
// operation counts, bounded counters, and the last announced stack
// target. All fields are touched only on the layer's own goroutine.
diag *Diag
queries xquery
lastTrigger string
diagSnap string
passOps int
flushes int
redraws int
errors int
firstFlush bool
lastStack string
diag *Diag
queries xquery
lastTrigger string
diagSnap string
passOps int
flushes int
redraws int
errors int
firstFlush bool
queryBundleDone bool
lastStack string
// fontAscent, fontDescent and fontCell describe the drawn font so the
// OSD overlay can punch its output shape around each text's glyph cell.
fontAscent int16
@@ -1048,7 +1094,7 @@ func (s *x11Session) recordFlush(shapeErr error) {
} else {
fields = append(fields, "ok")
}
s.diag.Emit("first_redraw", roleName(s.role), fields...)
s.diag.EmitForced("first_redraw", roleName(s.role), fields...)
s.emitQueryBundle(roleName(s.role))
}
@@ -1057,34 +1103,43 @@ func (s *x11Session) recordFlush(shapeErr error) {
// records them with any errors attached. Best-effort: failures become
// diagnostic fields, never layer failures.
func (s *x11Session) emitQueryBundle(role string) {
if s.queries == nil {
if s.queries == nil || s.queryBundleDone {
return
}
s.queryBundleDone = true
var fields, errs []string
s.stage("query_shape")
if v, err := s.queries.ShapeExtents(s.window); err != nil {
errs = append(errs, err.Error())
s.stageDone("query_shape", err)
} else {
fields = append(fields, "shape="+v)
}
s.stage("query_geom")
if v, err := s.queries.Geometry(s.window); err != nil {
errs = append(errs, err.Error())
s.stageDone("query_geom", err)
} else {
fields = append(fields, "geom="+v)
}
s.stage("query_parent")
if v, err := s.queries.Parent(s.window); err != nil {
errs = append(errs, err.Error())
s.stageDone("query_parent", err)
} else {
fields = append(fields, "parent="+v)
}
s.stage("query_stack")
if pos, of, err := s.queries.StackPosition(s.window); err != nil {
errs = append(errs, err.Error())
s.stageDone("query_stack", err)
} else {
fields = append(fields, "stack="+pos+"/"+of)
}
if len(errs) > 0 {
fields = append(fields, "err="+diagValue(strings.Join(errs, "; ")))
}
s.diag.Emit("query", role, fields...)
s.diag.EmitForced("query", role, fields...)
}
// osdRegionBounds returns the union bounds of the drawn regions in
@@ -1209,6 +1264,33 @@ func (s *x11Session) stackTarget() string {
return fmt.Sprintf("video/0x%x", sibling)
}
// openBegin announces a new open attempt for the role; the emitter
// deduplicates it, so it is bounded to once per process per role.
func (s *x11Session) openBegin() {
if s.diag != nil {
s.diag.Emit("open_begin", roleName(s.role))
}
}
// stage marks the start of one potentially blocking open operation, so a
// stalled open is identifiable from the journal by its last stage marker.
func (s *x11Session) stage(name string) {
if s.diag != nil {
s.diag.Emit("open_stage", roleName(s.role), "stage="+name)
}
}
// stageDone records a FAILED stage completion; a stage that returns
// without error needs no completion record — success is implied by the
// next stage marker (or the final record). The error value is sanitized
// and bounded by the emitter.
func (s *x11Session) stageDone(name string, err error) {
if s.diag == nil || err == nil {
return
}
s.diag.Emit("open_stage_done", roleName(s.role), "stage="+name, "err="+diagValue(err.Error()))
}
// roleName names a layer role in diagnostics.
func roleName(role LayerRole) string {
if role == RoleAbove {