summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2023-04-11 14:01:03 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2023-04-11 14:48:41 +1000
commit2432b6eeff73778e6c3761f8a50129403a8b4e79 (patch)
treee7f4b788c050b266952d76106ffb310bcf1cfe2d
parent8a01c3423d4bf46c800c388b7f22ac475dcee25e (diff)
downloadlibinput-2432b6eeff73778e6c3761f8a50129403a8b4e79.tar.gz
tools: add a tool to update our Razer quirks for internal keyboards
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>
-rw-r--r--quirks/30-vendor-razer.quirks4
-rwxr-xr-xtools/razer-quirks-lister.py83
2 files changed, 87 insertions, 0 deletions
diff --git a/quirks/30-vendor-razer.quirks b/quirks/30-vendor-razer.quirks
index 8b87a6bc..6be49990 100644
--- a/quirks/30-vendor-razer.quirks
+++ b/quirks/30-vendor-razer.quirks
@@ -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
index 00000000..6069ade7
--- /dev/null
+++ b/tools/razer-quirks-lister.py
@@ -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)