Files
desktop/modules/hdmi-relink/default.nix
T
2026-08-21 11:26:12 +05:30

53 lines
2.5 KiB
Nix

# Re-initialise the HDMI-0 TMDS link after every wake.
#
# A DPMS off/on cycle on its own leaves the LG's link untrained: the CRTC stays
# programmed and the panel keeps HPD asserted, so the driver never retries and
# the panel sits at "no signal" until the cable is replugged. Nothing queryable
# distinguishes that from a healthy link -- xrandr, the driver's display masks
# and /sys/class/drm all report fine either way, and X reports the target mode
# whether or not the panel actually locked -- so the link is re-initialised
# unconditionally on wake, and the only real check is looking at the panel.
#
# Recovery is an ascending pixel-clock ladder, not a single hop. The transmitter
# will not train straight to 4K60 (594MHz, against this port's 600MHz maximum);
# the panel lights up and drops again. It will climb: disable the CRTC, then
# 1080p @148.5MHz, then 4K30 @297MHz, then 4K60 @594MHz. The middle rung must be
# the slowest mode at the target resolution -- picking the fastest one below the
# target selects 593.41MHz, the 59.94Hz near-twin, and the climb does nothing.
#
# Wake is detected from the X screensaver idle counter, NOT a DPMS Off->On edge:
# user input resets the idle counter but not the power level, and whoever blanked
# the screen may restore the level before the next poll, so testing it on the
# edge races and never matches. The level is latched instead, as evidence that
# the panels slept at some point during the idle.
#
# Implemented as a binary speaking X11 directly rather than a shell loop around
# xset(1)/xrandr(1): the poll loop then spawns no processes at all, which keeps
# it out of execsnoop, and it never issues an xrandr --query, which on this
# driver forces a full DFP re-probe.
{ config, lib, pkgs, ... }:
let
hdmi-relink = pkgs.callPackage ./package.nix {};
in
{
# also exposed for running by hand after a manual layout change
environment.systemPackages = [ hdmi-relink ];
systemd.user.services.hdmi-relink = {
description = "Re-initialise HDMI-0 TMDS link on wake";
partOf = [ "graphical-session.target" ];
after = [ "graphical-session.target" ];
wantedBy = [ "graphical-session.target" ];
# XAUTHORITY is already exported to the user manager by the session
environment.DISPLAY = ":0";
serviceConfig = {
# ExecStart, not `script`, so systemd execs the binary directly
# instead of wrapping it in a generated shell script
ExecStart = "${lib.getExe hdmi-relink} --watch";
Restart = "on-failure";
RestartSec = 5;
};
};
}