test-keysym.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. #
  3. # This script creates a custom layout, overriding the TDLE key with the first
  4. # argument given.
  5. import argparse
  6. import os
  7. import re
  8. import subprocess
  9. import sys
  10. import tempfile
  11. from pathlib import Path
  12. # Template to force our key to TLDE
  13. template = """
  14. default
  15. xkb_symbols "basic" {{
  16. include "us(basic)"
  17. replace key <TLDE> {{ [ {} ] }};
  18. }};
  19. """
  20. parser = argparse.ArgumentParser(
  21. description="Tool to verify whether a keysym is resolved"
  22. )
  23. parser.add_argument("keysym", type=str, help="XKB keysym")
  24. parser.add_argument(
  25. "--tool",
  26. type=str,
  27. nargs=1,
  28. default=["xkbcli", "compile-keymap"],
  29. help="Full path to the xkbcli-compile-keymap tool",
  30. )
  31. args = parser.parse_args()
  32. with tempfile.TemporaryDirectory() as tmpdir:
  33. symfile = Path(tmpdir) / "symbols" / "keytest"
  34. symfile.parent.mkdir()
  35. with symfile.open(mode="w") as f:
  36. f.write(template.format(args.keysym))
  37. try:
  38. cmd = [
  39. *args.tool,
  40. "--layout",
  41. "keytest",
  42. ]
  43. env = os.environ.copy()
  44. env["XKB_CONFIG_EXTRA_PATH"] = tmpdir
  45. result = subprocess.run(
  46. cmd, env=env, capture_output=True, universal_newlines=True
  47. )
  48. if result.returncode != 0:
  49. print("ERROR: Failed to compile:")
  50. print(result.stderr)
  51. sys.exit(1)
  52. # grep for TLDE actually being remapped
  53. for l in result.stdout.split("\n"):
  54. match = re.match(r"\s+key \<TLDE\>\s+{\s+\[\s+(?P<keysym>\w+)\s+\]\s+}", l)
  55. if match:
  56. if args.keysym == match.group("keysym"):
  57. sys.exit(0)
  58. elif match.group("keysym") == "NoSymbol":
  59. print("ERROR: key {} not resolved:".format(args.keysym), l)
  60. else:
  61. print("ERROR: key {} mapped to wrong key:".format(args.keysym), l)
  62. sys.exit(1)
  63. print(result.stdout)
  64. print("ERROR: above keymap is missing key mapping for {}".format(args.keysym))
  65. sys.exit(1)
  66. except FileNotFoundError as err:
  67. print("ERROR: invalid or missing tool: {}".format(err))
  68. sys.exit(1)