2026-09-27 12:28:46 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""kanshi-autoconfig - generiert kanshi-Profile aus live erkannten Outputs.
|
|
|
|
|
|
|
|
|
|
Liest die angeschlossenen Monitore via `wlr-randr --json` aus (EDID-Namen,
|
|
|
|
|
bevorzugte Modi, aktuelle Skalierung) und schreibt daraus eine
|
|
|
|
|
funktionsfaehige ~/.config/kanshi/config mit den Profilen
|
|
|
|
|
docked / undocked / fallback.
|
|
|
|
|
|
|
|
|
|
Verhalten:
|
|
|
|
|
--print Vorschlag auf stdout, schreibt nichts.
|
|
|
|
|
--apply Schreibt (mit Backup *.bak), fragt nicht.
|
|
|
|
|
--apply-if-managed Schreibt nur, wenn die Config fehlt oder den Marker
|
2026-09-27 20:02:45 +02:00
|
|
|
'# kanshi-autoconfig: managed' enthaelt. Bei
|
|
|
|
|
unveraendertem Output-Set bleibt eine verwaltete Config
|
|
|
|
|
unangetastet (Modus-Tweaks inklusive). Wird vom
|
2026-09-27 12:28:46 +02:00
|
|
|
labwc-Autostart bei jedem Login aufgerufen.
|
2026-10-07 19:20:04 +02:00
|
|
|
--watch Dauerlauf (Autostart): erkennt Docking mitten in der
|
|
|
|
|
Session, schreibt bei neuem Output-Set neu (Merge,
|
|
|
|
|
siehe unten), startet kanshi bei Bedarf (neu) und
|
|
|
|
|
meldet Profilwechsel per notify-send (kanshi selbst
|
|
|
|
|
hat kein OSD).
|
|
|
|
|
|
|
|
|
|
MERGE statt Ueberschreiben: Eine Neugenerierung ersetzt nur Profile
|
|
|
|
|
gleichen Namens; Profile anderer Setups (z.B. docked bei gerade
|
|
|
|
|
ungedocktem Rechner) bleiben erhalten. Sonst wuerde ein ungedockter
|
|
|
|
|
Login das docked-Profil loeschen und der Daemon koennte spaeter nicht
|
|
|
|
|
mehr umschalten (interner Bildschirm bliebe an).
|
2026-09-27 12:28:46 +02:00
|
|
|
|
2026-09-27 20:02:45 +02:00
|
|
|
Moduswahl: Auf echter Hardware der EDID-preferred-Modus (meist nativ =
|
|
|
|
|
richtig). In VMs meldet die Firmware oft einen Kleinstmodus als preferred
|
|
|
|
|
(QEMU: 1280x800) - dort wird 1920x1080@60 genommen, wenn angeboten, sonst
|
|
|
|
|
preferred. Einmal geschrieben bleibt die Config bei gleichem Output-Set
|
|
|
|
|
erhalten (Modus-Tweaks inklusive); neu generiert wird nur bei geaendertem
|
|
|
|
|
Output-Set (Docking) oder per --apply.
|
|
|
|
|
|
2026-09-27 12:28:46 +02:00
|
|
|
Faellt die Erkennung fehl (kein Wayland, kein wlr-randr), wird eine
|
|
|
|
|
reine Fallback-Config geschrieben, damit nie ein schwarzer Bildschirm
|
|
|
|
|
ohne Layout dasteht.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import json
|
2026-10-07 19:20:04 +02:00
|
|
|
import os
|
|
|
|
|
import re
|
2026-09-27 12:28:46 +02:00
|
|
|
import shutil
|
2026-10-07 19:20:04 +02:00
|
|
|
import signal
|
2026-09-27 12:28:46 +02:00
|
|
|
import subprocess
|
|
|
|
|
import sys
|
2026-10-07 19:20:04 +02:00
|
|
|
import time
|
2026-09-27 12:28:46 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
MARKER = "# kanshi-autoconfig: managed"
|
|
|
|
|
CONFIG_PATH = Path.home() / ".config" / "kanshi" / "config"
|
2026-10-07 19:20:04 +02:00
|
|
|
POLL_INTERVAL = 5
|
2026-09-27 12:28:46 +02:00
|
|
|
|
|
|
|
|
|
2026-09-27 20:02:45 +02:00
|
|
|
def detect_vm():
|
|
|
|
|
"""True, wenn wir virtualisiert laufen (QEMU/Boxes-Test)."""
|
|
|
|
|
try:
|
|
|
|
|
proc = subprocess.run(
|
|
|
|
|
["systemd-detect-virt", "--vm"], capture_output=True, timeout=10
|
|
|
|
|
)
|
|
|
|
|
except (FileNotFoundError, subprocess.TimeoutExpired):
|
|
|
|
|
return False
|
|
|
|
|
return proc.returncode == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def norm_rate(value):
|
|
|
|
|
if isinstance(value, (int, float)) and value > 1000:
|
|
|
|
|
return value / 1000 # mHz -> Hz
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def choose_mode(modes, current, in_vm):
|
|
|
|
|
"""Modus waehlen: VM-Heuristik, sonst preferred, sonst current/erster."""
|
|
|
|
|
if in_vm:
|
|
|
|
|
for m in modes:
|
|
|
|
|
rate = norm_rate(m.get("refresh") or m.get("rate")) or 0
|
|
|
|
|
if m.get("width") == 1920 and m.get("height") == 1080 and round(rate) == 60:
|
|
|
|
|
return m
|
|
|
|
|
for m in modes:
|
|
|
|
|
if m.get("preferred"):
|
|
|
|
|
return m
|
|
|
|
|
return current or (modes[0] if modes else None)
|
|
|
|
|
|
|
|
|
|
|
2026-09-27 12:28:46 +02:00
|
|
|
def detect_outputs():
|
|
|
|
|
"""Liste von Dicts: name, internal, width, height, rate, scale."""
|
2026-09-27 20:02:45 +02:00
|
|
|
in_vm = detect_vm()
|
2026-09-27 12:28:46 +02:00
|
|
|
try:
|
|
|
|
|
proc = subprocess.run(
|
|
|
|
|
["wlr-randr", "--json"], capture_output=True, text=True, timeout=10
|
|
|
|
|
)
|
|
|
|
|
except (FileNotFoundError, subprocess.TimeoutExpired):
|
|
|
|
|
return []
|
|
|
|
|
if proc.returncode != 0:
|
|
|
|
|
return []
|
|
|
|
|
try:
|
|
|
|
|
data = json.loads(proc.stdout)
|
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
|
return []
|
|
|
|
|
outputs = []
|
|
|
|
|
for out in data:
|
|
|
|
|
name = out.get("name", "?")
|
|
|
|
|
# Nur verbundene Outputs betrachten (kanshi matcht nur angesteckte)
|
|
|
|
|
if out.get("connected") is False:
|
|
|
|
|
continue
|
|
|
|
|
modes = out.get("modes", []) or []
|
|
|
|
|
current = out.get("current_mode") or out.get("mode")
|
2026-09-27 20:02:45 +02:00
|
|
|
chosen = choose_mode(modes, current, in_vm)
|
2026-09-27 12:28:46 +02:00
|
|
|
if not chosen:
|
|
|
|
|
continue
|
|
|
|
|
outputs.append(
|
|
|
|
|
{
|
|
|
|
|
"name": name,
|
2026-10-10 13:52:16 +02:00
|
|
|
"internal": is_internal_name(name),
|
2026-09-27 20:02:45 +02:00
|
|
|
"width": chosen.get("width"),
|
|
|
|
|
"height": chosen.get("height"),
|
|
|
|
|
"rate": norm_rate(chosen.get("refresh") or chosen.get("rate")),
|
2026-10-03 17:05:37 +02:00
|
|
|
"scale": hidpi_default_scale(
|
|
|
|
|
name, out.get("scale", 1) or 1, chosen.get("width")
|
|
|
|
|
),
|
2026-09-27 12:28:46 +02:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return outputs
|
|
|
|
|
|
|
|
|
|
|
2026-10-03 17:05:37 +02:00
|
|
|
def hidpi_default_scale(name, detected, width):
|
|
|
|
|
"""HiDPI-Default fuer interne Panels (z.B. Framework 13: 2256x1504).
|
|
|
|
|
|
|
|
|
|
Der Compositor meldet frisch 1.0 - auf >=2000px breiten Laptop-Panels
|
|
|
|
|
ist das unbedienbar klein. Dann 1.5 als Startwert; explizit gesetzte
|
|
|
|
|
Scales (ungleich 1.0, z.B. via wdisplays) und externe/VM-Outputs bleiben
|
|
|
|
|
unangetastet. Hand-Tweaks in einer verwalteten Config bleiben ohnehin
|
|
|
|
|
erhalten (nur geaendertes Output-Set generiert neu).
|
|
|
|
|
"""
|
|
|
|
|
if (
|
|
|
|
|
name.startswith(("eDP-", "LVDS-", "DSI-"))
|
|
|
|
|
and detected == 1
|
|
|
|
|
and (width or 0) >= 2000
|
|
|
|
|
):
|
|
|
|
|
return 1.5
|
|
|
|
|
return detected
|
|
|
|
|
|
|
|
|
|
|
2026-09-27 12:28:46 +02:00
|
|
|
def mode_str(o):
|
|
|
|
|
s = f"{o['width']}x{o['height']}"
|
|
|
|
|
if o["rate"]:
|
|
|
|
|
s += f"@{o['rate']:.2f}" if isinstance(o["rate"], float) else f"@{o['rate']}"
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def render(outputs):
|
|
|
|
|
lines = [
|
|
|
|
|
MARKER,
|
|
|
|
|
f"# generiert am {datetime.now().isoformat(timespec='seconds')}",
|
2026-09-27 18:48:10 +02:00
|
|
|
"# Quelle: wlr-randr (live). Feintuning: wdisplays oder Modus direkt",
|
|
|
|
|
"# in dieser Datei anpassen - bei unveraendertem Output-Set bleibt",
|
|
|
|
|
"# die Datei (inkl. deiner Modus-Tweaks) bei jedem Login erhalten.",
|
|
|
|
|
"# Neu generiert wird nur bei geaendertem Output-Set (Docking) oder",
|
|
|
|
|
"# per --apply. Marker-Zeile loeschen = ganz hand-gepflegt.",
|
2026-09-27 12:28:46 +02:00
|
|
|
"",
|
|
|
|
|
]
|
|
|
|
|
externals = [o for o in outputs if not o["internal"]]
|
|
|
|
|
internals = [o for o in outputs if o["internal"]]
|
|
|
|
|
|
|
|
|
|
if externals:
|
|
|
|
|
lines.append("profile docked {")
|
|
|
|
|
for i in internals:
|
|
|
|
|
lines.append(f" output {i['name']} disable")
|
|
|
|
|
x = 0
|
|
|
|
|
for o in externals:
|
|
|
|
|
lines.append(
|
|
|
|
|
f" output {o['name']} mode {mode_str(o)} position {x},0 scale {o['scale']}"
|
|
|
|
|
)
|
|
|
|
|
x += o["width"] or 0
|
|
|
|
|
lines.append("}")
|
|
|
|
|
lines.append("")
|
|
|
|
|
|
|
|
|
|
if internals:
|
|
|
|
|
lines.append("profile undocked {")
|
|
|
|
|
for o in internals:
|
|
|
|
|
lines.append(
|
|
|
|
|
f" output {o['name']} mode {mode_str(o)} position 0,0 scale {o['scale']}"
|
|
|
|
|
)
|
|
|
|
|
lines.append("}")
|
|
|
|
|
lines.append("")
|
|
|
|
|
elif not externals:
|
|
|
|
|
# Gar nichts erkannt -> reiner Fallback, nie leer lassen
|
|
|
|
|
lines.append("# Keine Outputs erkannt (ausserhalb einer Wayland-Session")
|
|
|
|
|
lines.append("# aufgerufen?) - nur Fallback geschrieben.")
|
|
|
|
|
lines.append("")
|
|
|
|
|
|
|
|
|
|
lines.append("profile fallback {")
|
|
|
|
|
lines.append(" output * enable")
|
|
|
|
|
lines.append("}")
|
|
|
|
|
lines.append("")
|
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_managed(path: Path):
|
|
|
|
|
if not path.exists():
|
|
|
|
|
return True
|
|
|
|
|
try:
|
|
|
|
|
return MARKER in path.read_text()
|
|
|
|
|
except OSError:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply(text):
|
|
|
|
|
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
if CONFIG_PATH.exists():
|
|
|
|
|
backup = CONFIG_PATH.with_suffix(".bak")
|
|
|
|
|
shutil.copy2(CONFIG_PATH, backup)
|
|
|
|
|
print(f"Backup: {backup}")
|
|
|
|
|
CONFIG_PATH.write_text(text)
|
|
|
|
|
print(f"Geschrieben: {CONFIG_PATH}")
|
|
|
|
|
|
|
|
|
|
|
2026-09-27 18:48:10 +02:00
|
|
|
def config_output_names(path: Path):
|
|
|
|
|
"""Output-Namen aus einer bestehenden kanshi-Config (ohne '*' Fallback)."""
|
|
|
|
|
names = set()
|
|
|
|
|
try:
|
|
|
|
|
for line in path.read_text().splitlines():
|
|
|
|
|
parts = line.strip().split()
|
|
|
|
|
if len(parts) >= 2 and parts[0] == "output" and parts[1] != "*":
|
|
|
|
|
names.add(parts[1].strip("'\""))
|
|
|
|
|
except OSError:
|
|
|
|
|
pass
|
|
|
|
|
return names
|
|
|
|
|
|
|
|
|
|
|
2026-10-07 19:20:04 +02:00
|
|
|
def safe_read(path: Path):
|
|
|
|
|
try:
|
|
|
|
|
return path.read_text()
|
|
|
|
|
except OSError:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def split_profiles(text):
|
|
|
|
|
"""kanshi-Config zerlegen in (Header, {Name: Zeilen}, Reihenfolge)."""
|
|
|
|
|
header, sections, order = [], {}, []
|
|
|
|
|
cur = None
|
|
|
|
|
for line in text.splitlines():
|
|
|
|
|
m = re.match(r"^\s*profile\s+(\S+)\s*\{", line)
|
|
|
|
|
if m:
|
|
|
|
|
cur = m.group(1)
|
|
|
|
|
sections[cur] = [line]
|
|
|
|
|
order.append(cur)
|
|
|
|
|
continue
|
|
|
|
|
if cur is None:
|
|
|
|
|
header.append(line)
|
|
|
|
|
else:
|
|
|
|
|
sections[cur].append(line)
|
|
|
|
|
if line.strip() == "}":
|
|
|
|
|
cur = None
|
|
|
|
|
return header, sections, order
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def merge_configs(old_text, new_text):
|
|
|
|
|
"""Profile mergen: Neue gewinnen bei Namensgleichheit; alte
|
|
|
|
|
nicht-fallback Profile (z.B. docked bei ungedocktem Rechner) bleiben
|
|
|
|
|
erhalten. Fallback kommt immer frisch aus dem neuen Render.
|
|
|
|
|
Reihenfolge ist wesentlich (kanshi nimmt das erste passende Profil):
|
|
|
|
|
docked vor undocked, fallback zuletzt."""
|
|
|
|
|
new_h, new_s, new_o = split_profiles(new_text)
|
|
|
|
|
_, old_s, old_o = split_profiles(old_text or "")
|
|
|
|
|
combined = list(new_o)
|
|
|
|
|
for name in old_o:
|
|
|
|
|
if name not in new_s and name != "fallback" and name not in combined:
|
|
|
|
|
combined.append(name)
|
|
|
|
|
|
|
|
|
|
def rank(name):
|
|
|
|
|
if name == "fallback":
|
|
|
|
|
return 99
|
|
|
|
|
return {"docked": 0, "undocked": 1}.get(name, 50)
|
|
|
|
|
|
|
|
|
|
final_o = sorted(combined, key=rank) # stabil: Rest behält Reihenfolge
|
|
|
|
|
final_s = dict(new_s)
|
|
|
|
|
for name in final_o:
|
|
|
|
|
if name not in final_s:
|
|
|
|
|
final_s[name] = old_s[name]
|
|
|
|
|
kept = [n for n in final_o if n not in new_o]
|
|
|
|
|
lines = list(new_h)
|
|
|
|
|
if kept:
|
|
|
|
|
lines.append(f"# Beibehaltene Profile anderer Setups: {', '.join(kept)} "
|
|
|
|
|
"(Bestaende, werden ignoriert wenn unpassend).")
|
|
|
|
|
lines.append("")
|
|
|
|
|
for name in final_o:
|
|
|
|
|
lines.extend(final_s[name])
|
|
|
|
|
lines.append("")
|
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def matching_profile_name(connected, path=CONFIG_PATH):
|
|
|
|
|
"""Erstes Profil, dessen Outputs alle angeschlossen sind (kanshi-Logik)."""
|
|
|
|
|
try:
|
|
|
|
|
text = path.read_text()
|
|
|
|
|
except OSError:
|
|
|
|
|
return None
|
|
|
|
|
profiles = []
|
|
|
|
|
cur, outs = None, []
|
|
|
|
|
for line in text.splitlines():
|
|
|
|
|
s = line.strip()
|
|
|
|
|
if s.startswith("profile"):
|
|
|
|
|
parts = s.split()
|
|
|
|
|
if cur is not None:
|
|
|
|
|
profiles.append((cur, outs))
|
|
|
|
|
cur = parts[1].rstrip("{").strip() if len(parts) >= 2 else None
|
|
|
|
|
outs = []
|
|
|
|
|
continue
|
|
|
|
|
if cur is not None:
|
|
|
|
|
if s == "}":
|
|
|
|
|
profiles.append((cur, outs))
|
|
|
|
|
cur, outs = None, []
|
|
|
|
|
elif s.startswith("output"):
|
|
|
|
|
p = s.split()
|
|
|
|
|
if len(p) >= 2 and p[1] != "*":
|
|
|
|
|
outs.append(p[1].strip("'\""))
|
|
|
|
|
if cur is not None:
|
|
|
|
|
profiles.append((cur, outs))
|
|
|
|
|
for name, outs in profiles:
|
|
|
|
|
if outs and all(o in connected for o in outs):
|
|
|
|
|
return name
|
|
|
|
|
if any(n == "fallback" for n, _ in profiles):
|
|
|
|
|
return "fallback"
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def notify_send(summary, body=""):
|
|
|
|
|
if shutil.which("notify-send") is None:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
subprocess.run(
|
|
|
|
|
["notify-send", summary, body, "--app-name=kanshi-autoconfig",
|
|
|
|
|
"--icon=preferences-desktop-display"],
|
|
|
|
|
timeout=10, check=False,
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2026-10-10 13:52:16 +02:00
|
|
|
def is_internal_name(name):
|
|
|
|
|
return name.startswith(("eDP-", "LVDS-", "DSI-"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def enable_outputs(names):
|
|
|
|
|
"""Ausgaenge per wlr-randr einschalten (stiller Vorab-Schritt)."""
|
|
|
|
|
if shutil.which("wlr-randr") is None:
|
|
|
|
|
return
|
|
|
|
|
for n in sorted(names):
|
|
|
|
|
try:
|
|
|
|
|
subprocess.run(
|
|
|
|
|
["wlr-randr", "--output", n, "--on"],
|
|
|
|
|
timeout=15, check=False,
|
|
|
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2026-10-07 19:20:04 +02:00
|
|
|
def find_kanshi_pids():
|
|
|
|
|
"""PIDs laufender kanshi-Prozesse (ohne uns selbst), ohne pkill-Abhaengigkeit."""
|
|
|
|
|
pids, me = [], os.getpid()
|
|
|
|
|
try:
|
|
|
|
|
entries = os.listdir("/proc")
|
|
|
|
|
except OSError:
|
|
|
|
|
return []
|
|
|
|
|
for e in entries:
|
|
|
|
|
if not e.isdigit() or int(e) == me:
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
with open(f"/proc/{e}/comm") as f:
|
|
|
|
|
if f.read().strip() == "kanshi":
|
|
|
|
|
pids.append(int(e))
|
|
|
|
|
except OSError:
|
|
|
|
|
continue
|
|
|
|
|
return pids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_kanshi(child):
|
|
|
|
|
"""kanshi sicherstellen: eigener Child bevorzugt, fremde Instanz respektiert."""
|
|
|
|
|
if child is not None and child.poll() is None:
|
|
|
|
|
return child
|
|
|
|
|
if find_kanshi_pids():
|
|
|
|
|
return None
|
|
|
|
|
try:
|
|
|
|
|
return subprocess.Popen(
|
|
|
|
|
["kanshi"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
|
|
|
|
)
|
|
|
|
|
except (FileNotFoundError, OSError):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def restart_kanshi(child):
|
|
|
|
|
"""kanshi neu starten (Config wurde umgeschrieben und wird sonst nicht neu gelesen)."""
|
|
|
|
|
if child is not None and child.poll() is None:
|
|
|
|
|
child.terminate()
|
|
|
|
|
try:
|
|
|
|
|
child.wait(timeout=5)
|
|
|
|
|
except Exception:
|
|
|
|
|
child.kill()
|
|
|
|
|
for pid in find_kanshi_pids():
|
|
|
|
|
try:
|
|
|
|
|
os.kill(pid, signal.SIGTERM)
|
|
|
|
|
except OSError:
|
|
|
|
|
pass
|
|
|
|
|
return ensure_kanshi(None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_watch():
|
|
|
|
|
"""Dauerlauf: Output-Wechsel erkennen, Config mergen, kanshi (neu)
|
|
|
|
|
starten, Profilwechsel per notify-send melden."""
|
|
|
|
|
child, last_set = None, None
|
|
|
|
|
print("kanshi-autoconfig --watch: gestartet (Intervall 5s)", flush=True)
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
outputs = detect_outputs()
|
|
|
|
|
except Exception as e: # noqa: BLE001 - Watcher darf nie sterben
|
|
|
|
|
print(f"WARN: Erkennung fehlgeschlagen: {e}", file=sys.stderr, flush=True)
|
|
|
|
|
outputs = []
|
|
|
|
|
if outputs:
|
|
|
|
|
names = {o["name"] for o in outputs}
|
|
|
|
|
if last_set is None:
|
|
|
|
|
# Erstlauf: wie --apply-if-managed sicherstellen, kanshi starten.
|
|
|
|
|
if is_managed(CONFIG_PATH) and names != config_output_names(CONFIG_PATH):
|
|
|
|
|
apply(merge_configs(safe_read(CONFIG_PATH), render(outputs)))
|
|
|
|
|
elif names != last_set:
|
2026-10-10 13:52:16 +02:00
|
|
|
# Abdock-Sequenz: Waren Externe da und ist jetzt (auch) ein
|
|
|
|
|
# internes Panel da, erst das interne anzünden und kurz
|
|
|
|
|
# warten, BEVOR kanshi das volle Profil anwendet. Der
|
|
|
|
|
# kombinierte Commit (MST-Abbau + eDP-An in einem) hängt
|
|
|
|
|
# sonst die amdgpu-Pipeline (selbst die VT bleibt schwarz).
|
|
|
|
|
prev_extern = {o for o in last_set if not is_internal_name(o)}
|
|
|
|
|
new_intern = {o["name"] for o in outputs if o["internal"]}
|
|
|
|
|
if prev_extern and new_intern:
|
|
|
|
|
enable_outputs(new_intern)
|
|
|
|
|
time.sleep(2)
|
2026-10-07 19:20:04 +02:00
|
|
|
profile = None
|
|
|
|
|
if is_managed(CONFIG_PATH):
|
|
|
|
|
if names != config_output_names(CONFIG_PATH):
|
|
|
|
|
apply(merge_configs(safe_read(CONFIG_PATH), render(outputs)))
|
|
|
|
|
child = restart_kanshi(child)
|
|
|
|
|
profile = matching_profile_name(names)
|
|
|
|
|
notify_send("Monitorprofil gewechselt",
|
|
|
|
|
f"{profile or 'unbekannt'}: {', '.join(sorted(names))}")
|
|
|
|
|
last_set = names
|
|
|
|
|
child = ensure_kanshi(child)
|
|
|
|
|
time.sleep(POLL_INTERVAL)
|
|
|
|
|
|
|
|
|
|
|
2026-09-27 12:28:46 +02:00
|
|
|
def main(argv):
|
2026-10-07 19:20:04 +02:00
|
|
|
if "--watch" in argv:
|
|
|
|
|
run_watch()
|
|
|
|
|
return 0
|
2026-09-27 12:28:46 +02:00
|
|
|
outputs = detect_outputs()
|
|
|
|
|
if not outputs:
|
2026-09-27 18:48:10 +02:00
|
|
|
print("WARN: keine Outputs erkannt", file=sys.stderr)
|
2026-09-27 12:28:46 +02:00
|
|
|
text = render(outputs)
|
|
|
|
|
# Default ohne Schreib-Flag: nur anzeigen, nichts schreiben (sicher).
|
|
|
|
|
if "--print" in argv or ("--apply" not in argv and "--apply-if-managed" not in argv):
|
2026-09-27 18:48:10 +02:00
|
|
|
if not outputs:
|
|
|
|
|
print("# (Fallback-Anteil, da keine Outputs erkannt)", file=sys.stderr)
|
2026-09-27 12:28:46 +02:00
|
|
|
print(text)
|
|
|
|
|
if outputs:
|
|
|
|
|
print(
|
|
|
|
|
f"# Erkannt: {', '.join(o['name'] for o in outputs)}",
|
|
|
|
|
file=sys.stderr,
|
|
|
|
|
)
|
|
|
|
|
return 0
|
2026-09-27 18:48:10 +02:00
|
|
|
if "--apply-if-managed" in argv:
|
|
|
|
|
if not CONFIG_PATH.exists():
|
2026-10-07 19:20:04 +02:00
|
|
|
apply(merge_configs("", text)) # Erst-Login: anlegen
|
2026-09-27 18:48:10 +02:00
|
|
|
return 0
|
|
|
|
|
if not is_managed(CONFIG_PATH):
|
|
|
|
|
print(f"{CONFIG_PATH} ist hand-gepflegt - lasse sie unangetastet.")
|
|
|
|
|
return 0
|
|
|
|
|
if not outputs:
|
|
|
|
|
# Erkennung fehlgeschlagen -> niemals eine gute Config clobbern.
|
|
|
|
|
print("WARN: Erkennung fehlgeschlagen - lasse verwaltete Config unangetastet.",
|
|
|
|
|
file=sys.stderr)
|
|
|
|
|
return 0
|
|
|
|
|
if {o["name"] for o in outputs} == config_output_names(CONFIG_PATH):
|
|
|
|
|
# Gleiches Output-Set (z.B. jeden Boot dieselbe VM-Anzeige):
|
|
|
|
|
# Modus-Tweaks des Users bleiben erhalten, kein Rewrite.
|
|
|
|
|
print(f"Outputs unveraendert ({', '.join(sorted(config_output_names(CONFIG_PATH)))})"
|
|
|
|
|
" - behalte Config (inkl. Modus-Tweaks) bei.")
|
|
|
|
|
return 0
|
2026-10-07 19:20:04 +02:00
|
|
|
# Output-Set geaendert (angedockt/abgedockt): mergen (andere Profile bleiben).
|
|
|
|
|
apply(merge_configs(safe_read(CONFIG_PATH), text))
|
2026-09-27 12:28:46 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
sys.exit(main(sys.argv))
|