feat: split kiosk into fallback canvas and OSD overlay layers (#8)
Build Ground Station / build (push) Successful in 37s

Drive two independent visual layers per display target from one shared
video presence signal: the fullscreen fallback/menu canvas below video
and a transparent, input-transparent OSD overlay above video, reserved
as the future flight OSD rendering surface. WFB and video decode stay
out of the UI process; pilot video keeps rendering priority.
This commit was merged in pull request #12.
This commit is contained in:
2026-10-02 10:02:34 +02:00
parent 056cd5440a
commit 41552b4797
9 changed files with 676 additions and 115 deletions
+291 -55
View File
@@ -11,6 +11,7 @@ import (
"unicode/utf16"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/shape"
"github.com/BurntSushi/xgb/xproto"
)
@@ -26,8 +27,93 @@ const (
maxImageText16Units = 127
// windowTypePropertyName is the EWMH property naming a window's role.
windowTypePropertyName = "_NET_WM_WINDOW_TYPE"
// layerMarkerPropertyName tags Ground Station layer windows so sibling
// layers — and their window-manager frames — never count as video
// output for each other.
layerMarkerPropertyName = "_GROUNDSTATION_LAYER"
)
// osdEventMask selects only repaint bookkeeping for the OSD overlay: no
// pointer, key or motion events, so the server never routes device input to
// the overlay even before its empty input shape is applied.
const osdEventMask = xproto.EventMaskExposure
// deviceInputEventMask are the pointer and key selection bits a layer above
// video must never select: device input belongs to the windows below.
const deviceInputEventMask = xproto.EventMaskKeyPress | xproto.EventMaskKeyRelease |
xproto.EventMaskButtonPress | xproto.EventMaskButtonRelease |
xproto.EventMaskPointerMotion
// LayerRole orders a Ground Station window against presented video output.
// The fallback/menu canvas stays below video; the OSD overlay stays above.
type LayerRole int
const (
// RoleBelow marks the fullscreen fallback/menu canvas under video.
RoleBelow LayerRole = iota
// RoleAbove marks the transparent, input-transparent OSD overlay above
// video.
RoleAbove
)
// layerMarkerValue is the layer marker payload identifying a role's window
// as our own.
func layerMarkerValue(role LayerRole) string {
if role == RoleAbove {
return "osd"
}
return "kiosk"
}
// markerIsGroundstation reports whether a layer marker payload marks one of
// our own windows: those are Ground Station layers, never video output.
func markerIsGroundstation(marker string) bool {
return marker == layerMarkerValue(RoleBelow) || marker == layerMarkerValue(RoleAbove)
}
// ewmhWindowTypeName is the EWMH window role announced for a layer. The OSD
// overlay announces DOCK, the always-above helper role, so window managers
// never decorate or bury it; the fallback canvas keeps announcing nothing,
// as before.
func ewmhWindowTypeName(role LayerRole) string {
if role == RoleAbove {
return "_NET_WM_WINDOW_TYPE_DOCK"
}
return ""
}
// fullscreenState is the wmctrl stacking state for a layer role.
func fullscreenState(role LayerRole) string {
if role == RoleAbove {
return "above"
}
return "below"
}
// stackModeForVideoAppear is where a layer goes relative to the first
// presented video window: the canvas parks below it, the OSD rises above it.
func stackModeForVideoAppear(role LayerRole) uint32 {
if role == RoleAbove {
return xproto.StackModeAbove
}
return xproto.StackModeBelow
}
// raisesOnVideoEnd reports whether a layer raises itself when video ends:
// only the below canvas needs that to make the fallback visible again; the
// OSD overlay is already above everything.
func raisesOnVideoEnd(role LayerRole) bool {
return role == RoleBelow
}
// transparentShapeKinds are the shape regions cleared to empty so the OSD
// overlay is transparent and passes input through: an empty input shape
// never receives pointer or key events, and an empty bounding shape presents
// nothing over the video below — with or without a compositor.
func transparentShapeKinds() []shape.Kind {
return []shape.Kind{shape.SkInput, shape.SkBounding}
}
// ewmhNonVideoTypes are EWMH auxiliary window roles that never count as
// video output: desktop shells, panels, menus, dialogs and notifications
// must not suppress the fallback. Windows without the property — typical
@@ -48,22 +134,27 @@ var ewmhNonVideoTypes = map[string]bool{
}
// X11Opener connects to an X server (selected by DISPLAY in the environment)
// and creates the fullscreen kiosk canvas. It is the display target for the
// Viture screen; future targets such as MIPI/touch provide their own Opener
// and templates.
// and maps one fullscreen kiosk window for the layer role it is built with:
// the fallback/menu canvas below video (RoleBelow) or the transparent,
// input-transparent OSD overlay above video (RoleAbove). It is the display
// target for the Viture screen; future targets such as MIPI/touch provide
// their own Opener and templates.
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
// Fullscreen applies the fullscreen/below window state. It defaults to
// Fullscreen applies the fullscreen window state. It defaults to
// shelling out to wmctrl; tests may replace it. A failure is logged and
// the canvas degrades to a plain window.
// the window degrades to a plain window.
Fullscreen func(args []string) error
// Video is the shared decoded-video presence signal. The session feeds
// it from the video output window's own lifecycle on the root screen;
// templates consume it to show or hide the fallback. When nil the
// opener tracks presence in a private signal.
// templates consume it to show or hide themselves. When nil the opener
// tracks presence in a private signal.
Video *VideoPresence
// Role orders this layer's window against presented video: RoleBelow
// for the fallback/menu canvas, RoleAbove for the OSD overlay.
Role LayerRole
}
func (o X11Opener) presence() *VideoPresence {
@@ -102,11 +193,46 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
return nil, fmt.Errorf("allocate window: %w", err)
}
// The OSD overlay selects no device input even before its empty input
// shape exists; the canvas keeps its legacy exposure-only mask.
eventMask := uint32(xproto.EventMaskExposure)
if o.Role == RoleAbove {
eventMask = osdEventMask
}
values := uint32(xproto.CwBackPixel | xproto.CwEventMask)
xproto.CreateWindow(conn, screen.RootDepth, window, screen.Root,
0, 0, screen.WidthInPixels, screen.HeightInPixels, 0,
xproto.WindowClassInputOutput, screen.RootVisual, values,
[]uint32{screen.BlackPixel, xproto.EventMaskExposure})
[]uint32{screen.BlackPixel, eventMask})
// Tag the window as a Ground Station layer and announce its EWMH role
// before mapping, so no observer or window manager ever sees an
// untagged frame of our own layers.
markerAtom := internAtom(conn, layerMarkerPropertyName)
if markerAtom != 0 {
marker := layerMarkerValue(o.Role)
xproto.ChangeProperty(conn, xproto.PropModeReplace, window, markerAtom,
xproto.AtomString, 8, uint32(len(marker)), []byte(marker))
}
if name := ewmhWindowTypeName(o.Role); name != "" {
typeProp := internAtom(conn, windowTypePropertyName)
roleAtom := internAtom(conn, name)
if typeProp != 0 && roleAtom != 0 {
value := make([]byte, 4)
binary.LittleEndian.PutUint32(value, uint32(roleAtom))
xproto.ChangeProperty(conn, xproto.PropModeReplace, window, typeProp,
xproto.AtomAtom, 32, 1, value)
}
}
// The OSD overlay must be transparent and input-transparent from its
// first mapped frame: the shapes are emptied before mapping, and a
// 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 {
if err := applyTransparentShapes(conn, window); err != nil {
conn.Close()
return nil, err
}
}
xproto.MapWindow(conn, window)
// Watch the root screen so video output announces itself: a video
// window mapping is the first presented video frame, its unmap or
@@ -119,26 +245,27 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
return nil, fmt.Errorf("map kiosk window: %w", err)
}
// A kiosk restart must never cover running video: if video windows are
// already presented, record presence and hand rendering priority to the
// topmost one before the fallback is drawn anywhere.
// 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.
presence := o.presence()
typeAtom := internWindowTypeAtom(conn)
videoWindows := scanVideoWindows(conn, screen.Root, window, typeAtom)
videoWindows := scanVideoWindows(conn, screen.Root, window, typeAtom, markerAtom)
if len(videoWindows) > 0 {
presence.SetActive(true)
stackWindowBelow(conn, window, videoWindows[len(videoWindows)-1])
stackWindowRelative(conn, window, videoWindows[len(videoWindows)-1],
stackModeForVideoAppear(o.Role))
} else {
presence.SetActive(false)
}
// Fullscreen on the root screen, and kept below the video window: pilot
// video owns rendering priority whenever it is available. The kiosk
// raises itself only after video ends, to bring the fallback back; a
// missing wmctrl only degrades to a plain window and must not kill the
// kiosk.
if err := o.applyFullscreen(x11FullscreenArgs(uint32(window))); err != nil {
log.Printf("kiosk fullscreen state: %v (canvas stays a plain window)", err)
// Fullscreen on the root screen, on our side of the video window: pilot
// video owns rendering priority whenever it is available. The canvas
// 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)
}
gc, err := xproto.NewGcontextId(conn)
@@ -171,6 +298,8 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
video: presence,
videoWindows: videoWindowSet(videoWindows),
typeAtom: typeAtom,
markerAtom: markerAtom,
role: o.Role,
unicodeFont: unicodeFont,
}, nil
}
@@ -183,53 +312,152 @@ func videoWindowSet(windows []xproto.Window) map[xproto.Window]struct{} {
return set
}
func x11FullscreenArgs(window uint32) []string {
return []string{"-i", "-r", fmt.Sprintf("0x%08x", window), "-b", "add,fullscreen,below"}
func x11FullscreenArgs(window uint32, role LayerRole) []string {
return []string{"-i", "-r", fmt.Sprintf("0x%08x", window),
"-b", "add,fullscreen," + fullscreenState(role)}
}
// applyTransparentShapes empties the overlay's input and bounding shapes
// before it is mapped: it then never intercepts pointer or key input and
// never presents pixels over pilot video, with or without a compositor.
// Future OSD content will punch its output shape per drawn region.
func applyTransparentShapes(conn *xgb.Conn, window xproto.Window) error {
if err := shape.Init(conn); err != nil {
return fmt.Errorf("shape extension: %w", err)
}
for _, kind := range transparentShapeKinds() {
if err := shape.RectanglesChecked(conn, shape.SoSet, kind, 0,
window, 0, 0, nil).Check(); err != nil {
return fmt.Errorf("empty shape region %d: %w", kind, err)
}
}
return nil
}
// scanVideoWindows lists already-presented video windows on the root screen
// in bottom-to-top stacking order, so a kiosk reconnect can yield priority
// to the topmost one and never cover running video.
func scanVideoWindows(conn *xgb.Conn, root, own xproto.Window, typeAtom xproto.Atom) []xproto.Window {
// in bottom-to-top stacking order, so a layer reconnect can take its side
// relative to the topmost one and never cover running video.
func scanVideoWindows(conn *xgb.Conn, root, own xproto.Window, typeAtom, markerAtom xproto.Atom) []xproto.Window {
tree, err := xproto.QueryTree(conn, root).Reply()
if err != nil {
return nil
}
var windows []xproto.Window
for _, w := range tree.Children {
if w != own && presentedVideoWindow(conn, w, typeAtom) {
if w != own && presentedVideoWindow(conn, w, typeAtom, markerAtom) {
windows = append(windows, w)
}
}
return windows
}
// presentedVideoWindow reports whether w is a visible output window. Only a
// real, presented window counts as video: input-only and override-redirect
// helpers such as menus or notifications never do, and neither do windows
// carrying an EWMH auxiliary window type.
func presentedVideoWindow(conn *xgb.Conn, w xproto.Window, typeAtom xproto.Atom) bool {
// windowFacts is the presentation evidence gathered for one candidate
// window, so the video decision stays a pure function.
type windowFacts struct {
inputOutput bool
overrideRedirect bool
viewable bool
auxiliary bool
groundstation bool
}
// isPresentedVideoWindow decides whether a window is video output. Only a
// real, presented output window counts as video: input-only and
// override-redirect helpers such as menus or notifications never do, and
// neither do windows carrying an EWMH auxiliary window type or a Ground
// Station layer marker.
func isPresentedVideoWindow(f windowFacts) bool {
return f.inputOutput && !f.overrideRedirect && f.viewable &&
!f.auxiliary && !f.groundstation
}
// presentedVideoWindow gathers the presentation facts of w.
func presentedVideoWindow(conn *xgb.Conn, w xproto.Window, typeAtom, markerAtom xproto.Atom) bool {
attrs, err := xproto.GetWindowAttributes(conn, w).Reply()
if err != nil {
return false
}
return attrs.Class == xproto.WindowClassInputOutput &&
!attrs.OverrideRedirect &&
attrs.MapState == xproto.MapStateViewable &&
!auxiliaryWindowType(conn, w, typeAtom)
return isPresentedVideoWindow(windowFacts{
inputOutput: attrs.Class == xproto.WindowClassInputOutput,
overrideRedirect: attrs.OverrideRedirect,
viewable: attrs.MapState == xproto.MapStateViewable,
auxiliary: auxiliaryWindowType(conn, w, typeAtom),
groundstation: groundstationLayerWindow(conn, w, markerAtom),
})
}
// internWindowTypeAtom resolves the EWMH window type property atom, or 0
// when the server refuses, disabling the auxiliary type filter.
func internWindowTypeAtom(conn *xgb.Conn) xproto.Atom {
// markerWalkBounds bound the descendant search for layer markers: window
// managers reparent client windows into frame windows, so a Ground Station
// layer must be recognized through its frame, and the search must stay
// cheap even on a busy desktop screen. depth must cover our window below
// one window-manager frame; nodes bounds the total windows inspected.
func markerWalkBounds() (depth, nodes int) {
return 4, 64
}
// groundstationLayerWindow reports whether w itself or any window below it
// carries our layer marker. Window managers frame our windows, so the
// marker must be found through frame windows too: otherwise a framed
// Ground Station layer could count as video output and permanently
// suppress the fallback. When the search hits its bounds or the server
// refuses, it reports not-found, degrading to the plain window checks.
func groundstationLayerWindow(conn *xgb.Conn, w xproto.Window, markerAtom xproto.Atom) bool {
if markerAtom == 0 {
return false
}
depth, nodes := markerWalkBounds()
visited := 0
var walk func(xproto.Window, int) bool
walk = func(w xproto.Window, depth int) bool {
if visited >= nodes {
return false
}
visited++
if carriesLayerMarker(conn, w, markerAtom) {
return true
}
if depth <= 0 {
return false
}
tree, err := xproto.QueryTree(conn, w).Reply()
if err != nil {
return false
}
for _, child := range tree.Children {
if walk(child, depth-1) {
return true
}
}
return false
}
return walk(w, depth)
}
// carriesLayerMarker reports whether w itself carries our layer marker.
func carriesLayerMarker(conn *xgb.Conn, w xproto.Window, markerAtom xproto.Atom) bool {
reply, err := xproto.GetProperty(conn, false, w, markerAtom,
xproto.GetPropertyTypeAny, 0, 64).Reply()
if err != nil || reply.ValueLen == 0 {
return false
}
return markerIsGroundstation(string(reply.Value))
}
// internAtom resolves an atom by name, or 0 when the server refuses,
// disabling the checks that would use it.
func internAtom(conn *xgb.Conn, name string) xproto.Atom {
reply, err := xproto.InternAtom(conn, false,
uint16(len(windowTypePropertyName)), windowTypePropertyName).Reply()
uint16(len(name)), name).Reply()
if err != nil {
return 0
}
return reply.Atom
}
func internWindowTypeAtom(conn *xgb.Conn) xproto.Atom {
return internAtom(conn, windowTypePropertyName)
}
// auxiliaryWindowType reports whether the window carries an EWMH window
// type naming a non-video helper role. A missing property means the window
// counts as video.
@@ -255,10 +483,11 @@ func auxiliaryWindowType(conn *xgb.Conn, w xproto.Window, typeAtom xproto.Atom)
return false
}
func stackWindowBelow(conn *xgb.Conn, window, sibling xproto.Window) {
// stackWindowRelative moves window to the given side of sibling.
func stackWindowRelative(conn *xgb.Conn, window, sibling xproto.Window, mode uint32) {
xproto.ConfigureWindow(conn, window,
xproto.ConfigWindowSibling|xproto.ConfigWindowStackMode,
[]uint32{uint32(sibling), xproto.StackModeBelow})
[]uint32{uint32(sibling), mode})
}
// videoChange is the video-lifecycle meaning of one root structure event.
@@ -328,6 +557,8 @@ type x11Session struct {
video *VideoPresence
videoWindows map[xproto.Window]struct{}
typeAtom xproto.Atom
markerAtom xproto.Atom
role LayerRole
unicodeFont bool
}
@@ -395,34 +626,39 @@ func (s *x11Session) WaitEvent(ctx context.Context) (bool, error) {
}
// applyVideoChange folds one root structure event into the shared video
// presence and restacks the kiosk so video keeps rendering priority. It
// reports whether the template must be redrawn: video appearing makes the
// fallback disappear and video ending brings it back. Presence follows the
// set of presented video windows, so an unrelated window appearing or
// ending can never hide or resurrect the fallback on its own.
// presence and restacks this layer so video keeps rendering priority: the
// canvas parks below video and the OSD overlay rises above it. It reports
// whether the template must be redrawn: video appearing makes the fallback
// disappear and video ending brings it back. Presence follows the set of
// presented video windows, so an unrelated window appearing or ending can
// never hide or resurrect a template on its own.
func (s *x11Session) applyVideoChange(event xgb.Event) bool {
change, w := classifyVideoChange(event, s.window)
switch change {
case videoAppeared:
// Menus, notifications and other auxiliary windows never count as
// video; only a presented output window does.
if !presentedVideoWindow(s.conn, w, s.typeAtom) {
// Menus, notifications, other auxiliary windows and our own sibling
// layers never count as video; only a presented output window does.
if !presentedVideoWindow(s.conn, w, s.typeAtom, s.markerAtom) {
return false
}
if !addVideoWindow(s.videoWindows, w) {
return false
}
// The first presented video output takes priority: park below it.
stackWindowBelow(s.conn, s.window, w)
// The first presented video output takes priority: park on our side
// of it.
stackWindowRelative(s.conn, s.window, w, stackModeForVideoAppear(s.role))
return s.video.SetActive(true)
case videoDisappeared:
if !removeVideoWindow(s.videoWindows, w) {
return false
}
// Raise the kiosk so the fallback returns visibly even if some
// other window was stacked above it.
xproto.ConfigureWindow(s.conn, s.window,
xproto.ConfigWindowStackMode, []uint32{xproto.StackModeAbove})
// Only the below canvas raises itself so the fallback returns
// visibly even if some other window was stacked above it; the OSD
// overlay is already above video.
if raisesOnVideoEnd(s.role) {
xproto.ConfigureWindow(s.conn, s.window,
xproto.ConfigWindowStackMode, []uint32{xproto.StackModeAbove})
}
return s.video.SetActive(false)
default:
return false