feat(signage): slides keep their shape on any screen, custom shapes (21:9, 16:10…), Calibrate the picture edge by edge with presets
A slide is drawn at its playlist's shape, as large as fits the screen's picture, the bands in its own background (or stretched, the screen's choice). Playlists and library slides may have a shape of their own (aspect, Alembic 0045). Signage -> Screens -> Calibrate the picture: the TV outlines its area with arrows and its size, each edge moves pixel by pixel or a preset centres a shape; the TV follows live (signage.calibrate) and reports its window (POST /screen/viewport). Plan 6R.13, F-110. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,11 +18,14 @@ Manager side (signage.manage):
|
||||
PUT /api/signage/library/{id} change it (a screen showing it is told)
|
||||
DELETE /api/signage/library/{id} screens that had it as their default show nothing
|
||||
POST /api/signage/library/{id}/duplicate a copy
|
||||
GET /api/signage/screens the paired screens: playlist or slide, orientation, rotation
|
||||
PUT /api/signage/screens/{id} {playlist_id | slide_id, orientation, rotation}
|
||||
GET /api/signage/screens the paired screens: playlist or slide, orientation, rotation,
|
||||
picture area (insets), fit, the window size it reported
|
||||
PUT /api/signage/screens/{id} {playlist_id | slide_id, orientation, rotation, area_*, fit}
|
||||
POST /api/signage/screens/{id}/calibrate {on} the TV outlines its picture area (6R.13)
|
||||
|
||||
The screen (a paired device running `signage`, no sign-in):
|
||||
GET /api/signage/screen its playlist resolved for playing
|
||||
POST /api/signage/screen/viewport {width, height, ratio} its window (for calibrating)
|
||||
|
||||
Any change sends `signage.changed` only to the screens playing that playlist (a screen's own
|
||||
settings: `device.updated` plus `signage.changed` to it). A slide's content is typed per kind
|
||||
@@ -51,10 +54,16 @@ KEY = "signage.manage"
|
||||
MESSAGE = "signage.message"
|
||||
MAX_WIDGETS = "signage.max_widgets" # widgets a slide may hold (owner review 6: Performance; 10 recommended)
|
||||
MAX_WIDGETS_CAP = 50 # never more, whatever the setting
|
||||
AREA_MAX = 4000 # px a screen's picture area may cut off one edge (6R.13)
|
||||
AREA_KEYS = ("area_left", "area_top", "area_right", "area_bottom")
|
||||
CALIBRATE_S = 15 * 60 # a calibration nobody ends stops by itself
|
||||
_viewports: dict = {} # device id → {width, height, ratio} (what the TV reported; memory only)
|
||||
_calibrating: dict = {} # device id → when calibration started (monotonic)
|
||||
HEX = r"^#[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$" # #rrggbb, or #rrggbbaa (see-through, 2026-11-21)
|
||||
TIME = r"^([01]\d|2[0-3]):[0-5]\d$"
|
||||
DATE = r"^\d{4}-\d{2}-\d{2}$"
|
||||
Orientation = Literal["landscape", "portrait"]
|
||||
ASPECT = r"^\d{1,2}:\d{1,2}$" # a custom shape "21:9" (6R.13), from 1:4 to 4:1
|
||||
Transition = Literal["none", "fade", "slide", "zoom"]
|
||||
Font = Literal["sans", "serif", "rounded", "condensed", "display"]
|
||||
|
||||
@@ -353,13 +362,55 @@ Kind = Literal["menu", "image", "video", "text", "blank"]
|
||||
|
||||
# ── requests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
class PlaylistBody(BaseModel):
|
||||
def _aspect(v):
|
||||
"""A custom shape "W:H" (6R.13): None / "" = none; between 1:4 and 4:1."""
|
||||
import re as _re
|
||||
v = (v or "").strip() or None
|
||||
if v is None:
|
||||
return None
|
||||
if not _re.match(ASPECT, v):
|
||||
raise ValueError("a shape is written W:H, e.g. 21:9")
|
||||
w, h = (int(x) for x in v.split(":"))
|
||||
if not w or not h or w / h > 4 or h / w > 4:
|
||||
raise ValueError("a shape between 1:4 and 4:1")
|
||||
return v
|
||||
|
||||
|
||||
class _Shaped(BaseModel):
|
||||
aspect: Optional[str] = None # a custom shape "21:9"; None / "" = landscape / portrait
|
||||
|
||||
@field_validator("aspect")
|
||||
@classmethod
|
||||
def _shape(cls, v):
|
||||
return _aspect(v)
|
||||
|
||||
|
||||
def set_shape(obj, body) -> None:
|
||||
"""A playlist's / library slide's shape from a body: `aspect` sets orientation too (wide or tall;
|
||||
"16:9" / "9:16" are plain landscape / portrait); `orientation` alone drops a custom shape."""
|
||||
sent = body.model_fields_set
|
||||
if "aspect" in sent:
|
||||
a = body.aspect
|
||||
if a is None:
|
||||
obj.aspect = None
|
||||
if body.orientation is not None:
|
||||
obj.orientation = body.orientation
|
||||
return
|
||||
w, h = (int(x) for x in a.split(":"))
|
||||
obj.orientation = "landscape" if w >= h else "portrait"
|
||||
obj.aspect = None if a in ("16:9", "9:16") else a
|
||||
elif body.orientation is not None:
|
||||
obj.orientation = body.orientation
|
||||
obj.aspect = None
|
||||
|
||||
|
||||
class PlaylistBody(_Shaped):
|
||||
name: str = Field(min_length=1, max_length=80)
|
||||
orientation: Orientation = "landscape"
|
||||
transition: Transition = "fade"
|
||||
|
||||
|
||||
class PlaylistUpdate(BaseModel):
|
||||
class PlaylistUpdate(_Shaped):
|
||||
name: Optional[str] = Field(default=None, min_length=1, max_length=80)
|
||||
orientation: Optional[Orientation] = None
|
||||
transition: Optional[Transition] = None
|
||||
@@ -438,6 +489,12 @@ class SavedSlideBody(SlideFields):
|
||||
kind: Optional[Kind] = None
|
||||
description: Optional[str] = Field(default=None, max_length=300)
|
||||
orientation: Optional[Orientation] = None
|
||||
aspect: Optional[str] = None # a custom shape (6R.13)
|
||||
|
||||
@field_validator("aspect")
|
||||
@classmethod
|
||||
def _shape(cls, v):
|
||||
return _aspect(v)
|
||||
|
||||
|
||||
class ScreenBody(BaseModel):
|
||||
@@ -445,6 +502,22 @@ class ScreenBody(BaseModel):
|
||||
slide_id: Optional[int] = None # … or a library slide (one or the other)
|
||||
orientation: Optional[Orientation] = None
|
||||
rotation: Optional[Literal[0, 90, 180, 270]] = None
|
||||
# its picture area (6R.13): px cut off each edge of the TV's window; how a slide of another shape fits
|
||||
area_left: Optional[int] = Field(default=None, ge=0, le=AREA_MAX)
|
||||
area_top: Optional[int] = Field(default=None, ge=0, le=AREA_MAX)
|
||||
area_right: Optional[int] = Field(default=None, ge=0, le=AREA_MAX)
|
||||
area_bottom: Optional[int] = Field(default=None, ge=0, le=AREA_MAX)
|
||||
fit: Optional[Literal["keep", "stretch"]] = None
|
||||
|
||||
|
||||
class CalibrateBody(BaseModel):
|
||||
on: bool
|
||||
|
||||
|
||||
class ViewportBody(BaseModel):
|
||||
width: int = Field(ge=1, le=20000)
|
||||
height: int = Field(ge=1, le=20000)
|
||||
ratio: float = Field(default=1, gt=0, le=10) # device pixels per CSS px
|
||||
|
||||
|
||||
# ── helpers ────────────────────────────────────────────────────────────────────
|
||||
@@ -670,7 +743,7 @@ def _slide_out(s: SignageSlide) -> dict:
|
||||
|
||||
def _playlist_out(db, p: SignagePlaylist, slides: bool = False) -> dict:
|
||||
playing = set(_screens_playing(db, p.id))
|
||||
out = {"id": p.id, "name": p.name, "orientation": p.orientation, "transition": p.transition,
|
||||
out = {"id": p.id, "name": p.name, "orientation": p.orientation, "aspect": p.aspect, "transition": p.transition,
|
||||
"slide_count": len(p.slides), "screens": [{"id": d.id, "name": d.name} for d in _screens(db) if d.id in playing]}
|
||||
if slides:
|
||||
from services.signage_menu import resolve
|
||||
@@ -761,6 +834,7 @@ def list_playlists(db: Session = Depends(get_db), user=Depends(require(KEY))):
|
||||
@router.post("/playlists", status_code=201)
|
||||
def create_playlist(body: PlaylistBody, db: Session = Depends(get_db), user=Depends(require(KEY))):
|
||||
p = SignagePlaylist(name=body.name.strip(), orientation=body.orientation, transition=body.transition)
|
||||
set_shape(p, body)
|
||||
db.add(p)
|
||||
db.commit()
|
||||
db.refresh(p)
|
||||
@@ -777,8 +851,7 @@ def update_playlist(playlist_id: int, body: PlaylistUpdate, db: Session = Depend
|
||||
p = _playlist(db, playlist_id)
|
||||
if body.name is not None:
|
||||
p.name = body.name.strip()
|
||||
if body.orientation is not None:
|
||||
p.orientation = body.orientation
|
||||
set_shape(p, body)
|
||||
if body.transition is not None:
|
||||
p.transition = body.transition
|
||||
db.commit()
|
||||
@@ -804,7 +877,7 @@ def delete_playlist(playlist_id: int, db: Session = Depends(get_db), user=Depend
|
||||
@router.post("/playlists/{playlist_id}/duplicate", status_code=201)
|
||||
def duplicate_playlist(playlist_id: int, db: Session = Depends(get_db), user=Depends(require(KEY))):
|
||||
p = _playlist(db, playlist_id)
|
||||
copy = SignagePlaylist(name=f"{p.name} (2)"[:80], orientation=p.orientation, transition=p.transition)
|
||||
copy = SignagePlaylist(name=f"{p.name} (2)"[:80], orientation=p.orientation, aspect=p.aspect, transition=p.transition)
|
||||
db.add(copy)
|
||||
db.flush()
|
||||
for s in p.slides:
|
||||
@@ -904,7 +977,7 @@ def _saved(db, sid: int) -> SignageSavedSlide:
|
||||
|
||||
|
||||
def _saved_out(s: SignageSavedSlide, db=None) -> dict:
|
||||
out = {"id": s.id, "name": s.name, "description": s.description, "orientation": s.orientation, "kind": s.kind,
|
||||
out = {"id": s.id, "name": s.name, "description": s.description, "orientation": s.orientation, "aspect": s.aspect, "kind": s.kind,
|
||||
"duration_s": s.duration_s, "transition": s.transition, "video_to_end": bool(s.video_to_end), "enabled": bool(s.enabled),
|
||||
"days": s.days or [], "time_from": s.time_from, "time_to": s.time_to, "date_from": s.date_from, "date_to": s.date_to,
|
||||
"content": s.content or {}}
|
||||
@@ -957,6 +1030,7 @@ def create_saved(body: SavedSlideBody, db: Session = Depends(get_db), user=Depen
|
||||
name = _saved_name(body)
|
||||
s = SignageSavedSlide(name=name, kind=body.kind, orientation=body.orientation or "landscape", duration_s=10, enabled=True,
|
||||
video_to_end=body.kind == "video", content=_content(db, body.kind, None))
|
||||
set_shape(s, body)
|
||||
_apply(db, s, body)
|
||||
s.name = name
|
||||
s.description = (body.description or "").strip() or None
|
||||
@@ -984,8 +1058,7 @@ def update_saved(slide_id: int, body: SavedSlideBody, db: Session = Depends(get_
|
||||
s.name = name
|
||||
if "description" in body.model_fields_set:
|
||||
s.description = (body.description or "").strip() or None
|
||||
if body.orientation is not None:
|
||||
s.orientation = body.orientation
|
||||
set_shape(s, body)
|
||||
db.commit()
|
||||
db.refresh(s)
|
||||
_tell(_screens_showing_slide(db, s.id) + _embedding_screens(db, s.id))
|
||||
@@ -1008,7 +1081,7 @@ def delete_saved(slide_id: int, db: Session = Depends(get_db), user=Depends(requ
|
||||
@router.post("/library/{slide_id}/duplicate", status_code=201)
|
||||
def duplicate_saved(slide_id: int, db: Session = Depends(get_db), user=Depends(require(KEY))):
|
||||
s = _saved(db, slide_id)
|
||||
copy = SignageSavedSlide(name=f"{s.name} (2)"[:80], description=s.description, orientation=s.orientation, kind=s.kind,
|
||||
copy = SignageSavedSlide(name=f"{s.name} (2)"[:80], description=s.description, orientation=s.orientation, aspect=s.aspect, kind=s.kind,
|
||||
duration_s=s.duration_s, transition=s.transition, video_to_end=s.video_to_end, enabled=s.enabled,
|
||||
days=s.days, time_from=s.time_from, time_to=s.time_to, date_from=s.date_from, date_to=s.date_to,
|
||||
content=json.loads(json.dumps(s.content or {})))
|
||||
@@ -1039,7 +1112,30 @@ def _screen_out(db, d) -> dict:
|
||||
return {"id": d.id, "name": d.name, "playlist_id": p.id if p else None, "playlist_name": p.name if p else None,
|
||||
"slide_id": sl.id if sl else None, "slide_name": sl.name if sl else None,
|
||||
"orientation": cfg["orientation"], "rotation": cfg["rotation"], "now": now,
|
||||
"orientation_mismatch": any(o != cfg["orientation"] for o in shapes)}
|
||||
"orientation_mismatch": any(o != cfg["orientation"] for o in shapes),
|
||||
**_picture(d, cfg), "viewport": _viewports.get(d.id)}
|
||||
|
||||
|
||||
def _area(cfg: dict) -> dict:
|
||||
return {k[5:]: int(cfg.get(k) or 0) for k in AREA_KEYS}
|
||||
|
||||
|
||||
def calibrating(device_id: int) -> bool:
|
||||
import time
|
||||
at = _calibrating.get(device_id)
|
||||
if at is not None and time.monotonic() - at > CALIBRATE_S:
|
||||
_calibrating.pop(device_id, None)
|
||||
return False
|
||||
return at is not None
|
||||
|
||||
|
||||
def _picture(d, cfg: dict) -> dict:
|
||||
"""A screen's picture (6R.13): the insets of its area, how a slide of another shape fits, calibrating now."""
|
||||
return {"area": _area(cfg), "fit": cfg.get("fit") or "keep", "calibrating": calibrating(d.id)}
|
||||
|
||||
|
||||
def _tell_calibration(d, cfg: dict) -> None:
|
||||
broadcast_sync("signage.calibrate", {"on": calibrating(d.id), "area": _area(cfg)}, device_ids=[d.id])
|
||||
|
||||
|
||||
@router.get("/screens")
|
||||
@@ -1064,11 +1160,36 @@ def update_screen(device_id: int, body: ScreenBody, db: Session = Depends(get_db
|
||||
cfg["playlist"] = None
|
||||
d.config = validate(for_app("signage"), cfg, db)
|
||||
db.commit()
|
||||
if calibrating(d.id):
|
||||
_tell_calibration(d, _screen_config(d)) # the TV moves its outline at once
|
||||
broadcast_sync("device.updated", {}, device_ids=[d.id])
|
||||
_tell([d.id])
|
||||
return _screen_out(db, d)
|
||||
|
||||
|
||||
def _a_screen(db, device_id: int):
|
||||
from models.device import Device
|
||||
d = db.get(Device, device_id)
|
||||
if d is None or d.app_key != "signage" or d.revoked_at is not None:
|
||||
raise XeniaError("signage.not_a_screen", device_id=device_id)
|
||||
return d
|
||||
|
||||
|
||||
@router.post("/screens/{device_id}/calibrate")
|
||||
def calibrate_screen(device_id: int, body: CalibrateBody, db: Session = Depends(get_db), user=Depends(require(KEY))):
|
||||
"""Calibrating a screen's picture (6R.13): on, the TV outlines its picture area with arrows
|
||||
pointing out and its size, and follows every change of the insets at once; off (the manager's
|
||||
dialog closes, or after 15 min) it plays again."""
|
||||
import time
|
||||
d = _a_screen(db, device_id)
|
||||
if body.on:
|
||||
_calibrating[d.id] = time.monotonic()
|
||||
else:
|
||||
_calibrating.pop(d.id, None)
|
||||
_tell_calibration(d, _screen_config(d))
|
||||
return _screen_out(db, d)
|
||||
|
||||
|
||||
# ── the screen itself ──────────────────────────────────────────────────────────
|
||||
|
||||
def _resolved(db, p: SignagePlaylist) -> dict:
|
||||
@@ -1085,7 +1206,7 @@ def _resolved(db, p: SignagePlaylist) -> dict:
|
||||
for w in s["content"].get("widgets") or []:
|
||||
if w["kind"] == "qr":
|
||||
w["link"] = _qr_link(w)
|
||||
return {"id": p.id, "name": p.name, "orientation": p.orientation, "transition": p.transition, "slides": slides,
|
||||
return {"id": p.id, "name": p.name, "orientation": p.orientation, "aspect": p.aspect, "transition": p.transition, "slides": slides,
|
||||
"media": _media_map(db, [i for s in slides for i in media_ids(s["content"])] + more)}
|
||||
|
||||
|
||||
@@ -1101,7 +1222,7 @@ def _resolved_slide(db, s: SignageSavedSlide, orientation: str) -> dict:
|
||||
if w["kind"] == "qr":
|
||||
w["link"] = _qr_link(w)
|
||||
slides = [one] if s.enabled else []
|
||||
return {"id": slide_key(s.id), "name": s.name, "orientation": orientation, "transition": "fade", "slides": slides,
|
||||
return {"id": slide_key(s.id), "name": s.name, "orientation": s.orientation, "aspect": s.aspect, "transition": "fade", "slides": slides,
|
||||
"media": _media_map(db, media_ids(one["content"]) + more)}
|
||||
|
||||
|
||||
@@ -1143,7 +1264,7 @@ def screen(db: Session = Depends(get_db), dev=Depends(require_device("signage"))
|
||||
schedule = {"entries": entries, "fallback_playlist_id": default, "scenes": scenes, "timezone": venue_tz().key}
|
||||
else:
|
||||
playlist = fallback
|
||||
out = {"screen": {"id": dev.id, "name": dev.name, "orientation": cfg["orientation"], "rotation": cfg["rotation"]},
|
||||
out = {"screen": {"id": dev.id, "name": dev.name, "orientation": cfg["orientation"], "rotation": cfg["rotation"], **_picture(dev, cfg)},
|
||||
"playlist": playlist, "schedule": schedule, "playlists": playlists,
|
||||
# for the widgets (5.4c): weather None = hide it (no internet, no place, too old)
|
||||
"venue": _venue(db)}
|
||||
@@ -1151,6 +1272,14 @@ def screen(db: Session = Depends(get_db), dev=Depends(require_device("signage"))
|
||||
return out
|
||||
|
||||
|
||||
@router.post("/screen/viewport")
|
||||
def screen_viewport(body: ViewportBody, dev=Depends(require_device("signage"))):
|
||||
"""The TV's window size (6R.13), sent when it starts and when it changes: the calibration
|
||||
dialog's presets fit a shape into it. Kept in memory only (the TV sends it again after a restart)."""
|
||||
_viewports[dev.id] = {"width": body.width, "height": body.height, "ratio": round(body.ratio, 3)}
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
# ── schedules (5.4d; lines with their screens and scenes, owner 2026-11-21) ─────
|
||||
|
||||
def _line_out(i: SignageScheduleItem, now_ids: dict) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user