feat: render minimal flight OSD from supervisor snapshots (#3)
This commit is contained in:
@@ -133,6 +133,30 @@ var ewmhNonVideoTypes = map[string]bool{
|
||||
"POPUP_MENU": true,
|
||||
}
|
||||
|
||||
// osdShapePadding widens each punched output region by this many pixels on
|
||||
// every side, so glyph antialiasing artifacts at fixed-font edges never
|
||||
// clip visibly.
|
||||
const osdShapePadding = 2
|
||||
|
||||
// osdFallbackFontMetrics are the font metrics used when the font query
|
||||
// fails: close to the common 6x13 fixed cell so a punched region still
|
||||
// covers the glyphs. The OSD only ever draws terse ASCII telemetry.
|
||||
func osdFallbackFontMetrics() (ascent, descent, cell int16) {
|
||||
return 11, 3, 6
|
||||
}
|
||||
|
||||
// osdTextRect is the output region covering one drawn text: its glyph cell
|
||||
// around the baseline, widened by the shape padding.
|
||||
func osdTextRect(x, y, units int, cell, ascent, descent int16) xproto.Rectangle {
|
||||
pad := int16(osdShapePadding)
|
||||
return xproto.Rectangle{
|
||||
X: int16(x) - pad,
|
||||
Y: int16(y) - ascent - pad,
|
||||
Width: uint16(units*int(cell) + 2*int(pad)),
|
||||
Height: uint16(int(ascent+descent) + 2*int(pad)),
|
||||
}
|
||||
}
|
||||
|
||||
// X11Opener connects to an X server (selected by DISPLAY in the environment)
|
||||
// and maps one fullscreen kiosk window for the layer role it is built with:
|
||||
// the fallback/menu canvas below video (RoleBelow) or the transparent,
|
||||
@@ -143,6 +167,12 @@ type X11Opener struct {
|
||||
// PollInterval is how often X events are drained while waiting, so that
|
||||
// cancellation stays responsive. Zero selects 100 ms.
|
||||
PollInterval time.Duration
|
||||
// RefreshInterval is how often the layer redraws without an X event,
|
||||
// so template data that changes independently of the display server —
|
||||
// the OSD overlay's published snapshots — appears at the OSD's own
|
||||
// slower cadence. Zero keeps the purely event-driven redraws of the
|
||||
// fallback canvas.
|
||||
RefreshInterval time.Duration
|
||||
// Fullscreen applies the fullscreen window state. It defaults to
|
||||
// shelling out to wmctrl; tests may replace it. A failure is logged and
|
||||
// the window degrades to a plain window.
|
||||
@@ -326,6 +356,18 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
|
||||
xproto.GcForeground|xproto.GcBackground|xproto.GcFont,
|
||||
[]uint32{screen.WhitePixel, screen.BlackPixel, uint32(font)})
|
||||
|
||||
// The OSD overlay punches its output shape per drawn region, so it
|
||||
// needs the real glyph metrics; the canvas never punches shapes.
|
||||
ascent, descent, cell := osdFallbackFontMetrics()
|
||||
if o.Role == RoleAbove {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &x11Session{
|
||||
conn: conn,
|
||||
window: window,
|
||||
@@ -335,12 +377,16 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
|
||||
black: screen.BlackPixel,
|
||||
white: screen.WhitePixel,
|
||||
poll: o.pollInterval(),
|
||||
refresh: o.RefreshInterval,
|
||||
video: presence,
|
||||
videoWindows: videoWindowSet(videoWindows),
|
||||
typeAtom: typeAtom,
|
||||
markerAtom: markerAtom,
|
||||
role: o.Role,
|
||||
unicodeFont: unicodeFont,
|
||||
fontAscent: ascent,
|
||||
fontDescent: descent,
|
||||
fontCell: cell,
|
||||
pollEvent: conn.PollForEvent,
|
||||
probe: connLivenessProbe(conn),
|
||||
}, nil
|
||||
@@ -596,12 +642,21 @@ type x11Session struct {
|
||||
black uint32
|
||||
white uint32
|
||||
poll time.Duration
|
||||
refresh time.Duration
|
||||
video *VideoPresence
|
||||
videoWindows map[xproto.Window]struct{}
|
||||
typeAtom xproto.Atom
|
||||
markerAtom xproto.Atom
|
||||
role LayerRole
|
||||
unicodeFont bool
|
||||
// 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
|
||||
fontDescent int16
|
||||
fontCell int16
|
||||
// drawn accumulates the output regions of texts drawn since the last
|
||||
// clear; the OSD overlay's flush punches its bounding shape over it.
|
||||
drawn []xproto.Rectangle
|
||||
// pollEvent drains one pending X event; conn.PollForEvent in
|
||||
// production. Tests script it.
|
||||
pollEvent func() (xgb.Event, xgb.Error)
|
||||
@@ -615,18 +670,27 @@ func (s *x11Session) Canvas() Canvas { return s }
|
||||
func (s *x11Session) Size() (int, int) { return s.width, s.height }
|
||||
|
||||
func (s *x11Session) Clear() {
|
||||
// A fresh draw pass starts with no output regions: the OSD overlay's
|
||||
// next flush re-punches its shape over exactly what this pass draws.
|
||||
s.resetOSDRegions()
|
||||
// ClearArea tiles the window background (black, set at creation); it
|
||||
// involves no graphics context.
|
||||
xproto.ClearArea(s.conn, false, s.window, 0, 0,
|
||||
uint16(s.width), uint16(s.height))
|
||||
}
|
||||
|
||||
// resetOSDRegions forgets the output regions of the previous draw pass.
|
||||
func (s *x11Session) resetOSDRegions() {
|
||||
s.drawn = s.drawn[:0]
|
||||
}
|
||||
|
||||
func (s *x11Session) Text(x, y int, message string) {
|
||||
xproto.ChangeGC(s.conn, s.gc, xproto.GcForeground, []uint32{s.white})
|
||||
if s.unicodeFont {
|
||||
chars := utf16Char2b(message, maxImageText16Units)
|
||||
xproto.ImageText16(s.conn, byte(len(chars)), xproto.Drawable(s.window), s.gc,
|
||||
int16(x), int16(y), chars)
|
||||
s.recordOSDText(x, y, len(chars))
|
||||
return
|
||||
}
|
||||
message = foldToASCII(message)
|
||||
@@ -635,15 +699,52 @@ func (s *x11Session) Text(x, y int, message string) {
|
||||
}
|
||||
xproto.ImageText8(s.conn, byte(len(message)), xproto.Drawable(s.window), s.gc,
|
||||
int16(x), int16(y), message)
|
||||
s.recordOSDText(x, y, len(message))
|
||||
}
|
||||
|
||||
// recordOSDText remembers one drawn text's output region so the overlay's
|
||||
// flush can punch its bounding shape over exactly what was drawn. Only the
|
||||
// OSD overlay punches shapes; the fallback canvas always stays plain.
|
||||
func (s *x11Session) recordOSDText(x, y, units int) {
|
||||
if s.role != RoleAbove || units <= 0 {
|
||||
return
|
||||
}
|
||||
s.drawn = append(s.drawn,
|
||||
osdTextRect(x, y, units, s.fontCell, s.fontAscent, s.fontDescent))
|
||||
}
|
||||
|
||||
func (s *x11Session) Flush() {
|
||||
// The OSD overlay presents pixels only where this draw pass put text:
|
||||
// its bounding shape is re-punched over the recorded regions while its
|
||||
// input shape stays empty, so it never covers video elsewhere and
|
||||
// never intercepts input. A failure here is a broken connection; the
|
||||
// liveness probe below ends the session and the layer reconnects.
|
||||
if s.role == RoleAbove {
|
||||
if err := applyOSDShapes(s.conn, s.window, s.drawn); err != nil {
|
||||
log.Printf("osd overlay shapes: %v", err)
|
||||
}
|
||||
}
|
||||
s.conn.Sync()
|
||||
}
|
||||
|
||||
// applyOSDShapes sets the overlay's bounding shape to the drawn regions
|
||||
// (possibly none) and keeps its input shape empty.
|
||||
func applyOSDShapes(conn *xgb.Conn, window xproto.Window, rects []xproto.Rectangle) error {
|
||||
if err := shape.RectanglesChecked(conn, shape.SoSet, shape.SkBounding, 0,
|
||||
window, 0, 0, rects).Check(); err != nil {
|
||||
return fmt.Errorf("bounding shape: %w", err)
|
||||
}
|
||||
if err := shape.RectanglesChecked(conn, shape.SoSet, shape.SkInput, 0,
|
||||
window, 0, 0, nil).Check(); err != nil {
|
||||
return fmt.Errorf("empty input shape: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *x11Session) WaitEvent(ctx context.Context) (bool, error) {
|
||||
ticker := time.NewTicker(s.poll)
|
||||
defer ticker.Stop()
|
||||
lastRefresh := time.Now()
|
||||
for {
|
||||
for {
|
||||
event, err := s.pollEvent()
|
||||
@@ -660,6 +761,14 @@ func (s *x11Session) WaitEvent(ctx context.Context) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
// The OSD overlay redraws on its own slower cadence than video so
|
||||
// newly published snapshots appear without waiting for X events.
|
||||
// A zero interval disables it, keeping the fallback canvas purely
|
||||
// event-driven.
|
||||
if s.refresh > 0 && time.Since(lastRefresh) >= s.refresh {
|
||||
lastRefresh = time.Now()
|
||||
return true, nil
|
||||
}
|
||||
// Round trip keeps the connection liveness visible even without
|
||||
// incoming events; the reply-bearing cookie is resolved with Reply
|
||||
// (see connLivenessProbe).
|
||||
|
||||
Reference in New Issue
Block a user