tools: add a tool to update our Razer quirks for internal keyboards
authorPeter Hutterer <peter.hutterer@who-t.net>
Tue, 11 Apr 2023 04:01:03 +0000 (14:01 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Tue, 11 Apr 2023 04:48:41 +0000 (14:48 +1000)
openrazer keeps a convenient list of keyboard devices that belong to the
RazerBlade line and thus should be marked as internal by us. Let's
use that one.

This script git clones the current openrazer repo, imports the file we
need and then overwrites our current quirks file with the sorted list of
devices.

For the second part of this to work reliable we need a marker in the
quirks file that marks the start of autogenerated entries.

Heavily influenced by @danryu in !887.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
quirks/30-vendor-razer.quirks
tools/razer-quirks-lister.py [new file with mode: 0755]

index 8b87a6bc72739a726b90d5481d91b301c09e7d4f..6be49990e4b919cc794e7191a7c99237b6f03290 100644 (file)
@@ -5,6 +5,10 @@ MatchName=*Lid Switch*
 MatchDMIModalias=dmi:*svnRazer:pnBlade*
 AttrLidSwitchReliability=write_open
 
+# Manually added entries must go above this line.
+# Entries below this line are autogenerated
+# AUTOGENERATED
+
 [RazerBladeStealth Keyboard]
 MatchUdevType=keyboard
 MatchBus=usb
diff --git a/tools/razer-quirks-lister.py b/tools/razer-quirks-lister.py
new file mode 100755 (executable)
index 0000000..6069ade
--- /dev/null
@@ -0,0 +1,83 @@
+#!/usr/bin/env python3
+#
+# SPDX-License-Identifier: MIT
+#
+# This utility clones the openrazer repository, extracts the known VID/PID combos
+# for "RazerBlade" keyboards and spits out a libinput-compatible quirks file
+# with those keyboards listed as "Internal".
+#
+# Usage:
+# $ cd path/to/libinput.git/
+# $ ./tools/razer-quirks-list.py
+
+from pathlib import Path
+from collections import namedtuple
+import tempfile
+import shutil
+import subprocess
+import sys
+
+
+KeyboardDescriptor = namedtuple("KeyboardDescriptor", ["vid", "pid", "name"])
+
+with tempfile.TemporaryDirectory() as tmpdir:
+    # Clone the openrazer directory and add its Python modyle basedir
+    # to our sys.path so we can import it
+    subprocess.run(
+        [
+            "git",
+            "clone",
+            "--quiet",
+            "--depth=1",
+            "https://github.com/openrazer/openrazer",
+        ],
+        cwd=tmpdir,
+        check=True,
+    )
+    sys.path.append(str(Path(tmpdir) / "openrazer" / "daemon"))
+
+    import openrazer_daemon.hardware.keyboards as razer_keyboards  # type: ignore
+
+    keyboards: list[KeyboardDescriptor] = []
+
+    # All classnames for the keyboards we care about start with RazerBlade
+    for clsname in filter(lambda c: c.startswith("RazerBlade"), dir(razer_keyboards)):
+        kbd = getattr(razer_keyboards, clsname)
+        try:
+            k = KeyboardDescriptor(name=clsname, vid=kbd.USB_VID, pid=kbd.USB_PID)
+            keyboards.append(k)
+        except AttributeError:
+            continue
+
+    tmpfile = Path(tmpdir) / "30-vendor-razer.quirks"
+    try:
+        quirksfile = next(Path("quirks").glob("30-vendor-razer.quirks"))
+    except StopIteration:
+        print("Unable to find the razer quirks file.")
+        raise SystemExit(1)
+
+    with open(tmpfile, "w") as output:
+        with open(quirksfile) as input:
+            for line in input:
+                output.write(line)
+                if line.startswith("# AUTOGENERATED"):
+                    output.write("\n")
+                    break
+
+        for kbd in sorted(keyboards, key=lambda k: k.vid << 16 | k.pid):
+            print(
+                "\n".join(
+                    (
+                        f"[{kbd.name} Keyboard]",
+                        "MatchUdevType=keyboard",
+                        "MatchBus=usb",
+                        f"MatchVendor=0x{kbd.vid:04X}",
+                        f"MatchProduct=0x{kbd.pid:04X}",
+                        "AttrKeyboardIntegration=internal",
+                    )
+                ),
+                file=output,
+            )
+            print(file=output)
+
+    shutil.copy(tmpfile, quirksfile)