• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# SPDX-License-Identifier: MIT
4#
5# This utility clones the openrazer repository, extracts the known VID/PID combos
6# for "RazerBlade" keyboards and spits out a libinput-compatible quirks file
7# with those keyboards listed as "Internal".
8#
9# Usage:
10# $ cd path/to/libinput.git/
11# $ ./tools/razer-quirks-list.py
12
13from pathlib import Path
14from collections import namedtuple
15import tempfile
16import shutil
17import subprocess
18import sys
19
20
21KeyboardDescriptor = namedtuple("KeyboardDescriptor", ["vid", "pid", "name"])
22
23with tempfile.TemporaryDirectory() as tmpdir:
24    # Clone the openrazer directory and add its Python modyle basedir
25    # to our sys.path so we can import it
26    subprocess.run(
27        [
28            "git",
29            "clone",
30            "--quiet",
31            "--depth=1",
32            "https://github.com/openrazer/openrazer",
33        ],
34        cwd=tmpdir,
35        check=True,
36    )
37    sys.path.append(str(Path(tmpdir) / "openrazer" / "daemon"))
38
39    import openrazer_daemon.hardware.keyboards as razer_keyboards  # type: ignore
40
41    keyboards: list[KeyboardDescriptor] = []
42
43    # All classnames for the keyboards we care about start with RazerBlade
44    for clsname in filter(lambda c: c.startswith("RazerBlade"), dir(razer_keyboards)):
45        kbd = getattr(razer_keyboards, clsname)
46        try:
47            k = KeyboardDescriptor(name=clsname, vid=kbd.USB_VID, pid=kbd.USB_PID)
48            keyboards.append(k)
49        except AttributeError:
50            continue
51
52    tmpfile = Path(tmpdir) / "30-vendor-razer.quirks"
53    try:
54        quirksfile = next(Path("quirks").glob("30-vendor-razer.quirks"))
55    except StopIteration:
56        print("Unable to find the razer quirks file.")
57        raise SystemExit(1)
58
59    with open(tmpfile, "w") as output:
60        with open(quirksfile) as input:
61            for line in input:
62                output.write(line)
63                if line.startswith("# AUTOGENERATED"):
64                    output.write("\n")
65                    break
66
67        for kbd in sorted(keyboards, key=lambda k: k.vid << 16 | k.pid):
68            print(
69                "\n".join(
70                    (
71                        f"[{kbd.name} Keyboard]",
72                        "MatchUdevType=keyboard",
73                        "MatchBus=usb",
74                        f"MatchVendor=0x{kbd.vid:04X}",
75                        f"MatchProduct=0x{kbd.pid:04X}",
76                        "AttrKeyboardIntegration=internal",
77                    )
78                ),
79                file=output,
80            )
81            print(file=output)
82
83    shutil.copy(tmpfile, quirksfile)
84