utils/gatelock/gatelock/_vt.py
Krzysztof kuhy Rudnicki 6154dc09bf Pin pre-commit's ruff to 0.16.7 and satisfy the CPY001 it brings
The three Python packages pinned ruff twice at different versions:
requirements.txt at 0.16.7, .pre-commit-config.yaml at v0.15.2. Only the
older one ever ran, so `select = ["ALL"]` was enforced two minor versions
behind. Measured before aligning: v0.15.2 reports 0 findings, every 0.16.x
reports CPY001 on each file -- 70 crdt-sync, 23 freedays, 71 gatelock, and
no other rule.

Resolved by adding the notice rather than ignoring the rule, so the gate
keeps its teeth.

Two gatelock modules sat at exactly the 250-line cap, so a one-line header
tipped them to 251. Both are split along a real seam rather than shaved:

  _widget_fills.py    colour arithmetic over LockConfig, no tkinter import
  _surfaces_focus.py  selects among existing surfaces, never mutates them

The focus split keeps _surfaces.py's documented containment (it stays the
only module that creates, moves or destroys windows), and the static guard
in test_recovery_invariants.py globs _surfaces*.py, so the new sibling is
covered with no list to extend. widgets.py re-exports the fills helpers,
which keeps gatelock.widgets the single public surface its tests import.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019kMvSMtsVMB3d2uoZ56pbp
2026-09-12 18:11:24 +02:00

39 lines
1.3 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""VT-switch (Ctrl+Alt+Fn) disable/restore via setxkbmap.
Disabling VT switching is what stops a locked window from being bypassed by
dropping to a TTY. Best-effort: silently does nothing if setxkbmap isn't
installed, matching the behavior of every implementation this was ported from.
"""
from __future__ import annotations
import logging
import shutil
import subprocess
_logger = logging.getLogger(__name__)
def disable_vt_switching() -> bool:
"""Best-effort disable of Ctrl+Alt+Fn VT switching.
Returns:
True if setxkbmap was found and the disable command ran (the caller
should later call :func:`restore_vt_switching`); False if setxkbmap
is unavailable and VT switching could not be touched.
"""
setxkbmap = shutil.which("setxkbmap")
if setxkbmap is None:
_logger.warning("setxkbmap not found; VT switching stays enabled")
return False
subprocess.run([setxkbmap, "-option", "srvrkeys:none"], check=False)
return True
def restore_vt_switching() -> None:
"""Re-enable VT switching. Safe to call even if it was never disabled."""
setxkbmap = shutil.which("setxkbmap")
if setxkbmap is not None:
subprocess.run([setxkbmap, "-option", ""], check=False)