fix: hide cursor natively, keep OSD on top, pin codec extension contract (#3)

This commit is contained in:
2026-11-03 22:49:36 +01:00
parent 3d6d146007
commit 296569232e
4 changed files with 271 additions and 37 deletions
+81 -23
View File
@@ -117,21 +117,27 @@ func stackModeForVideoAppear(role LayerRole) uint32 {
return xproto.StackModeBelow
}
// stackReassertTarget reports which video window the OSD overlay must sit
// above after an external restack, and whether one exists. Only the OSD
// overlay re-asserts its place: window managers promote fullscreened video
// to the top of their stacking order after the video-appear restack, which
// would bury the overlay — and its text — for the rest of the flight. The
// fallback canvas keeps its event-driven lifecycle, and the video window is
// never moved.
func stackReassertTarget(role LayerRole, videoActive bool, topVideo xproto.Window, videoWindows map[xproto.Window]struct{}) (xproto.Window, bool) {
if role != RoleAbove || !videoActive || topVideo == 0 {
return 0, false
// stackReassert decides the OSD overlay's stacking correction on its
// refresh: above the tracked video window while video is live, and raised
// to the top while video is absent — the fallback canvas raises itself to
// the top when video ends and would otherwise bury the overlay's text under
// the fullscreen presentation indefinitely. Only the OSD overlay ever
// re-asserts; the canvas and video windows are never moved by this.
func stackReassert(role LayerRole, videoActive bool, topVideo xproto.Window, videoWindows map[xproto.Window]struct{}) (sibling xproto.Window, mode uint32, ok bool) {
if role != RoleAbove {
return 0, 0, false
}
if !videoActive {
// No sibling: raise the overlay to the top of the stack.
return 0, xproto.StackModeAbove, true
}
if topVideo == 0 {
return 0, 0, false
}
if _, tracked := videoWindows[topVideo]; !tracked {
return 0, false
return 0, 0, false
}
return topVideo, true
return topVideo, xproto.StackModeAbove, true
}
// raisesOnVideoEnd reports whether a layer raises itself when video ends:
@@ -180,6 +186,43 @@ func osdFallbackFontMetrics() (ascent, descent, cell int16) {
return 11, 3, 6
}
// createWindowValues builds the CreateWindow value mask and values; the
// order must follow the mask bit order (back pixel, event mask, cursor) so
// the server reads each value correctly. The blank cursor hides the pointer
// over the layer from its first frame, independently of video, wfb-ng or
// any cursor-hiding helper process.
func createWindowValues(backPixel uint32, eventMask uint32, cursor xproto.Cursor) (uint32, []uint32) {
values := []uint32{backPixel, eventMask, uint32(cursor)}
return xproto.CwBackPixel | xproto.CwEventMask | xproto.CwCursor, values
}
// newBlankCursor builds the invisible cursor from zeroed 1x1 source and
// mask pixmaps: an all-zero mask displays nothing regardless of the source
// contents, so the pointer disappears over the layer without any shell
// polling or helper process. The pixmaps and graphics context are freed
// immediately; the cursor itself lives until the session closes.
func newBlankCursor(conn *xgb.Conn, screen *xproto.ScreenInfo) xproto.Cursor {
pixmap, _ := xproto.NewPixmapId(conn)
xproto.CreatePixmap(conn, screen.RootDepth, pixmap,
xproto.Drawable(screen.Root), 1, 1)
mask, _ := xproto.NewPixmapId(conn)
xproto.CreatePixmap(conn, screen.RootDepth, mask,
xproto.Drawable(screen.Root), 1, 1)
gc, _ := xproto.NewGcontextId(conn)
xproto.CreateGC(conn, gc, xproto.Drawable(pixmap), 0, nil)
unit := []xproto.Rectangle{{Width: 1, Height: 1}}
xproto.PolyFillRectangle(conn, xproto.Drawable(pixmap), gc, unit)
xproto.PolyFillRectangle(conn, xproto.Drawable(mask), gc, unit)
xproto.FreeGC(conn, gc)
cursor, _ := xproto.NewCursorId(conn)
// Zeroed colors and hotspot: with the all-zero mask nothing is ever
// displayed, so the fore/back colors never reach the screen.
xproto.CreateCursor(conn, cursor, pixmap, mask, 0, 0, 0, 0, 0, 0, 0, 0)
xproto.FreePixmap(conn, pixmap)
xproto.FreePixmap(conn, mask)
return cursor
}
// 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 {
@@ -302,11 +345,15 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
if o.Role == RoleAbove {
eventMask = osdEventMask
}
values := uint32(xproto.CwBackPixel | xproto.CwEventMask)
// The blank cursor hides the pointer over this layer from its first
// frame and stays on the layer across no-video/video transitions; the
// server reclaims it when the session's connection closes.
cursor := newBlankCursor(conn, screen)
valueMask, values := createWindowValues(screen.BlackPixel, eventMask, cursor)
xproto.CreateWindow(conn, screen.RootDepth, window, screen.Root,
0, 0, screen.WidthInPixels, screen.HeightInPixels, 0,
xproto.WindowClassInputOutput, screen.RootVisual, values,
[]uint32{screen.BlackPixel, eventMask})
xproto.WindowClassInputOutput, screen.RootVisual, valueMask,
values)
// 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.
@@ -442,6 +489,7 @@ func (o X11Opener) Open(ctx context.Context) (Session, error) {
markerAtom: markerAtom,
role: o.Role,
unicodeFont: unicodeFont,
cursor: cursor,
fontAscent: ascent,
fontDescent: descent,
fontCell: cell,
@@ -711,6 +759,9 @@ type x11Session struct {
markerAtom xproto.Atom
role LayerRole
unicodeFont bool
// cursor is the blank cursor applied to the layer window at creation;
// it is freed when the session closes.
cursor xproto.Cursor
// 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
@@ -849,17 +900,22 @@ func (s *x11Session) WaitEvent(ctx context.Context) (bool, error) {
}
}
// reassertStacking re-claims the overlay's place above the presented video
// window; a no-op for the canvas and whenever video is absent.
// reassertStacking re-claims the overlay's stacking place on the OSD's
// slower refresh cadence: above the live video window, or on top of the
// stack while video is absent so the fallback canvas can never bury it. A
// no-op for the canvas layer.
func (s *x11Session) reassertStacking() {
if s.video == nil || !s.video.Active() {
return
}
target, ok := stackReassertTarget(s.role, s.video.Active(), s.topVideo, s.videoWindows)
videoActive := s.video != nil && s.video.Active()
sibling, mode, ok := stackReassert(s.role, videoActive, s.topVideo, s.videoWindows)
if !ok {
return
}
stackWindowRelative(s.conn, s.window, target, xproto.StackModeAbove)
if sibling != 0 {
stackWindowRelative(s.conn, s.window, sibling, mode)
return
}
xproto.ConfigureWindow(s.conn, s.window,
xproto.ConfigWindowStackMode, []uint32{mode})
}
// applyVideoChange folds one root structure event into the shared video
@@ -983,7 +1039,9 @@ func foldToASCII(message string) string {
}
func (s *x11Session) Close() error {
// Closing the connection makes the X server destroy the kiosk window.
// Free the blank cursor explicitly; closing the connection then makes
// the X server reclaim everything else, including the kiosk window.
xproto.FreeCursor(s.conn, s.cursor)
s.conn.Close()
return nil
}
+28 -14
View File
@@ -286,27 +286,41 @@ func TestOSDEventMaskNeverSelectsDeviceInput(t *testing.T) {
}
}
func TestStackReassertTargetOnlyAboveLiveTrackedVideo(t *testing.T) {
// Window managers promote fullscreened video to the top of their
// stacking order after the video-appear restack, burying the OSD
// overlay; the overlay's refresh must re-assert its place above the
// tracked video window — and only the OSD overlay may do so.
func TestStackReassertKeepsTheOSDOnTop(t *testing.T) {
// While video is live the overlay must sit above the tracked video
// window; while video is absent the overlay must rise to the top,
// because the fallback canvas raises itself to the top when video ends
// and would otherwise bury the overlay's text. Only the OSD overlay
// re-asserts; the canvas and video windows are never moved by this.
windows := map[xproto.Window]struct{}{0x20: {}}
if target, ok := stackReassertTarget(RoleAbove, true, 0x20, windows); !ok || target != 0x20 {
t.Fatalf("target=%v ok=%v want the tracked video window", target, ok)
if sibling, mode, ok := stackReassert(RoleAbove, true, 0x20, windows); !ok || sibling != 0x20 || mode != xproto.StackModeAbove {
t.Fatalf("sibling=%v mode=%v ok=%v want above the tracked video window", sibling, mode, ok)
}
if _, ok := stackReassertTarget(RoleBelow, true, 0x20, windows); ok {
if sibling, mode, ok := stackReassert(RoleAbove, false, 0, nil); !ok || sibling != 0 || mode != xproto.StackModeAbove {
t.Fatalf("sibling=%v mode=%v ok=%v want a top raise while video is absent", sibling, mode, ok)
}
if _, _, ok := stackReassert(RoleBelow, true, 0x20, windows); ok {
t.Fatal("the fallback canvas must never re-assert stacking")
}
if _, ok := stackReassertTarget(RoleAbove, false, 0x20, windows); ok {
t.Fatal("no re-assert while video is absent")
}
if _, ok := stackReassertTarget(RoleAbove, true, 0x30, windows); ok {
if _, _, ok := stackReassert(RoleAbove, true, 0x30, windows); ok {
t.Fatal("a stale top video window must not be restacked against")
}
if _, ok := stackReassertTarget(RoleAbove, true, 0, windows); ok {
t.Fatal("no tracked video window means nothing to re-assert")
if _, _, ok := stackReassert(RoleAbove, true, 0, windows); ok {
t.Fatal("no tracked video window means no video-relative restack")
}
}
func TestCreateWindowValuesOrderMatchesTheValueMask(t *testing.T) {
// The CreateWindow values must follow the mask bit order (back pixel,
// event mask, cursor) so the server reads each value correctly; the
// blank cursor hides the pointer over the layer from its first frame.
mask, values := createWindowValues(0x100, 0x200, 0xc0de)
if mask != xproto.CwBackPixel|xproto.CwEventMask|xproto.CwCursor {
t.Fatalf("mask=%b want back pixel, event mask and cursor bits", mask)
}
if len(values) != 3 || values[0] != 0x100 || values[1] != 0x200 || values[2] != 0xc0de {
t.Fatalf("values=%v want [backPixel eventMask cursor]", values)
}
}