157 lines
5.4 KiB
Python
Executable File
157 lines
5.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# FWS — affiche les raccourcis clavier RÉELS de la session (bouton ⌨ de la
|
|
# barre ou Super+K). Universel :
|
|
# • Hyprland : « hyprctl binds -j » = les binds effectivement chargés
|
|
# (descriptions des bindd) — affichage wofi (Wayland).
|
|
# • i3 : parse ~/.config/i3/config (bindsym, variables résolues)
|
|
# — affichage rofi (X11).
|
|
# Aucune dépendance hors python3 + wofi/rofi (déjà installés avec le bureau).
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
|
|
sys.path.insert(0, "/usr/local/lib/fws")
|
|
from fws_i18n import catalog # noqa: E402
|
|
|
|
_catalog = catalog("system")
|
|
t = _catalog.t
|
|
|
|
_WOFI_STYLE = "/usr/local/share/fws/wofi-keys.css"
|
|
|
|
# Bits de modmask Hyprland (masques X11) dans l'ordre d'affichage voulu.
|
|
_MODS = [(64, "SUPER"), (4, "CTRL"), (8, "ALT"), (1, "SHIFT")]
|
|
|
|
# Key names translated for display (keys of system-translations.toml).
|
|
_KEY_LABELS = {
|
|
"mouse:272": "keys.left_click",
|
|
"mouse:273": "keys.right_click",
|
|
"mouse:274": "keys.middle_click",
|
|
"mouse_down": "keys.wheel_down",
|
|
"mouse_up": "keys.wheel_up",
|
|
"button4": "keys.wheel_up",
|
|
"button5": "keys.wheel_down",
|
|
"return": "keys.enter",
|
|
"space": "keys.space",
|
|
"escape": "keys.escape",
|
|
"tab": "keys.tab",
|
|
"print": "keys.print_screen",
|
|
}
|
|
_KEY_SYMBOLS = {
|
|
"left": "←",
|
|
"right": "→",
|
|
"up": "↑",
|
|
"down": "↓",
|
|
}
|
|
|
|
|
|
def _label(key):
|
|
name = key.lower()
|
|
if name in _KEY_LABELS:
|
|
return t(_KEY_LABELS[name])
|
|
return _KEY_SYMBOLS.get(name, key)
|
|
|
|
|
|
def _description(bind):
|
|
"""Description of a Hyprland bind, translated when it is an FWS key.
|
|
|
|
The FWS config writes keys (binds.open_terminal) as bindd descriptions:
|
|
a translated text with a comma in it would break the bind line, so they
|
|
are translated here instead of in the file. A user's own plain-text
|
|
description is shown as written.
|
|
"""
|
|
desc = (bind.get("description") or "").strip()
|
|
if desc.startswith("binds.") and _catalog.text(desc) is not None:
|
|
# The workspace binds share one text: their argument is the number.
|
|
return t(desc, number=(bind.get("arg") or "").strip())
|
|
return desc
|
|
|
|
|
|
# --- Hyprland : binds réels via hyprctl ------------------------------------
|
|
def hyprland_lines():
|
|
raw = subprocess.check_output(["hyprctl", "binds", "-j"], text=True)
|
|
lines, seen = [], set()
|
|
for bind in json.loads(raw):
|
|
parts = [name for bit, name in _MODS if bind.get("modmask", 0) & bit]
|
|
key = bind.get("key") or (f"code {bind['keycode']}" if bind.get("keycode") else "")
|
|
parts.append(_label(key))
|
|
combo = " + ".join(p for p in parts if p)
|
|
if not combo or combo in seen:
|
|
continue
|
|
seen.add(combo)
|
|
desc = _description(bind) \
|
|
or f"{bind.get('dispatcher', '?')} {(bind.get('arg') or '').strip()}".strip()
|
|
lines.append(f"{combo:<24} {desc}")
|
|
return lines
|
|
|
|
|
|
# --- i3 : parse de la config (variables résolues) ----------------------------
|
|
def i3_lines():
|
|
path = os.path.expanduser("~/.config/i3/config")
|
|
variables = {}
|
|
lines, seen = [], set()
|
|
with open(path, encoding="utf-8", errors="replace") as fh:
|
|
content = fh.readlines()
|
|
for line in content:
|
|
m = re.match(r"\s*set\s+(\$\S+)\s+(.+)$", line)
|
|
if m:
|
|
variables[m.group(1)] = m.group(2).strip()
|
|
def resolve(text):
|
|
# $var les plus longs d'abord ($filemanager avant $file…).
|
|
for var in sorted(variables, key=len, reverse=True):
|
|
text = text.replace(var, variables[var])
|
|
return text
|
|
for line in content:
|
|
m = re.match(r"\s*bindsym\s+(?:--\S+\s+)*(\S+)\s+(.+)$", line)
|
|
if not m:
|
|
continue
|
|
combo_raw, action = m.groups()
|
|
combo_raw = resolve(combo_raw).replace("Mod4", "Super").replace("Mod1", "Alt")
|
|
combo = " + ".join(_label(k) for k in combo_raw.split("+"))
|
|
if combo in seen:
|
|
continue
|
|
seen.add(combo)
|
|
action = resolve(action)
|
|
action = re.sub(r"^exec\s+(--no-startup-id\s+)?", "", action).strip().strip('"')
|
|
lines.append(f"{combo:<24} {action}")
|
|
return lines
|
|
|
|
|
|
def show(lines):
|
|
if not lines:
|
|
lines = [t("keys.none_found")]
|
|
text = "\n".join(lines)
|
|
if os.environ.get("HYPRLAND_INSTANCE_SIGNATURE"):
|
|
cmd = ["wofi", "--dmenu", "--prompt", t("keys.prompt_filter"),
|
|
"--width", "820", "--height", "600", "--insensitive",
|
|
"--cache-file", "/dev/null"]
|
|
if os.path.exists(_WOFI_STYLE):
|
|
cmd += ["--style", _WOFI_STYLE]
|
|
else:
|
|
# X11/i3 : rofi en dmenu, style assorti (mono pour l'alignement).
|
|
cmd = ["rofi", "-dmenu", "-i", "-p", t("keys.prompt"),
|
|
"-no-custom",
|
|
"-theme-str", 'window { width: 880px; } listview { lines: 18; }',
|
|
"-theme-str", '* { font: "Noto Sans Mono 11"; }']
|
|
subprocess.run(cmd, input=text, text=True, check=False)
|
|
|
|
|
|
def main():
|
|
try:
|
|
if os.environ.get("HYPRLAND_INSTANCE_SIGNATURE"):
|
|
lines = hyprland_lines()
|
|
else:
|
|
lines = i3_lines()
|
|
except Exception as exc: # noqa: BLE001 — best effort, jamais de crash muet
|
|
subprocess.run(["notify-send", t("keys.error_title"),
|
|
t("keys.error", error=exc)], check=False)
|
|
return 1
|
|
show(lines)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|