#!/usr/bin/env python3

"""Apply the room's UI scale to the whole classroom session.

UI_SCALE is a property of the television, not of one program, so it is
applied where it reaches everything: sway's output scale. Chrome's tabs
and omnibox, its menus, VLC, and the classroom's own dock and
notifications are then all measured in the same scaled logical pixels.

The display mode is untouched — the panel stays at its native
resolution. Only the logical coordinate space shrinks, which is what
makes everything drawn in it larger.

Run from the sway config before anything else starts, so surfaces come
up at the right size rather than being resized underneath themselves.
"""

import os
import subprocess
import sys

sys.path.insert(0, "/usr/local/lib/ekklesia-classroom")
sys.path.insert(
    0,
    os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "lib"),
)

from classroom_config import read_device_config, ui_scale  # noqa: E402


def main():
    scale = ui_scale(read_device_config())

    try:
        result = subprocess.run(
            ["swaymsg", "output", "*", "scale", f"{scale:g}"],
            capture_output=True,
            text=True,
        )
    except OSError as error:
        print(f"classroom-scale: {error}", file=sys.stderr)
        return 1

    if result.returncode != 0:
        print(
            f"classroom-scale: {result.stderr.strip() or 'sway refused the scale'}",
            file=sys.stderr,
        )
        return 1

    print(f"classroom-scale: session scale {scale:g}")

    return 0


if __name__ == "__main__":
    sys.exit(main())
