1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
#!/usr/bin/env python3
#
# Copyright © 2012 Red Hat, Inc.
#
# Permission to use, copy, modify, distribute, and sell this software
# and its documentation for any purpose is hereby granted without
# fee, provided that the above copyright notice appear in all copies
# and that both that copyright notice and this permission notice
# appear in supporting documentation, and that the name of Red Hat
# not be used in advertising or publicity pertaining to distribution
# of the software without specific, written prior permission. Red
# Hat makes no representations about the suitability of this software
# for any purpose. It is provided "as is" without express or implied
# warranty.
#
# THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import argparse
import configparser
import os
import sys
import subprocess
import tempfile
from pathlib import Path
def xdg_dir():
return Path(os.getenv("XDG_CONFIG_HOME", Path.home() / ".config")) / "libwacom"
class Tablet(object):
def __init__(self, name, bus, vid, pid):
self.name = name
self.bus = bus
self.vid = vid # Note: this is a string
self.pid = pid # Note: this is a string
self.has_touch = False
self.has_pad = False
self.is_touchscreen = False
# We have everything in strings so let's use that for sorting later
# This will sort bluetooth before usb but meh
self.cmpstr = ":".join((bus, vid, pid, name))
def __lt__(self, other):
return self.cmpstr < other.cmpstr
def __str__(self):
return f"{self.bus}:{self.vid}:{self.pid}:{self.name}"
class HWDBFile:
def __init__(self):
self.tablets = []
def _tablet_entry(self, tablet):
vid = tablet.vid.upper()
pid = tablet.pid.upper()
bustypes = {
"usb": "0003",
"bluetooth": "0005",
}
# serial devices have their own rules, so we skip anything that
# doesn't have straight conversion
try:
bus = bustypes[tablet.bus]
except KeyError:
return
match = f"b{bus}v{vid}p{pid}"
entries = {"*": ["ID_INPUT=1", "ID_INPUT_TABLET=1", "ID_INPUT_JOYSTICK=0"]}
if tablet.has_touch:
if tablet.is_touchscreen:
entries["* Finger"] = ["ID_INPUT_TOUCHSCREEN=1"]
else:
entries["* Finger"] = ["ID_INPUT_TOUCHPAD=1"]
if tablet.has_pad:
entries["* Pad"] = ["ID_INPUT_TABLET_PAD=1"]
# Non-Wacom devices often have a Keyboard node instead of a Pad
# device. If they share the USB ID with the tablet, we likely just
# assigned ID_INPUT_TABLET to a keyboard device - and libinput refuses
# to accept those.
# Let's add a generic exclusion rule for anything we know of with a
# Keyboard device name.
entries["* Keyboard"] = ["ID_INPUT_TABLET=0", "ID_INPUT_TABLET_PAD=0"]
entries["* Mouse"] = ["ID_INPUT_TABLET=0", "ID_INPUT_TABLET_PAD=0"]
lines = [f"# {tablet.name}"]
for name, props in entries.items():
lines.append(f"libwacom:name:{name}:input:{match}*")
lines.extend([f" {p}" for p in props])
lines.append("")
return "\n".join(lines)
def print(self, file=sys.stdout):
header = (
"# hwdb entries for libwacom supported devices",
"# This file is generated by libwacom, do not edit",
"#",
"# The lookup key is a contract between the udev rules and the hwdb entries.",
"# It is not considered public API and may change.",
"",
)
print("\n".join(header), file=file)
for t in self.tablets:
entry = self._tablet_entry(t)
if entry:
print(entry, file=file)
class TabletDatabase:
def __init__(self, path):
self.path = path
self.tablets = sorted(self._load(path))
def _load(self, path):
for file in Path(path).glob("*.tablet"):
config = configparser.ConfigParser()
config.read(file)
for match in config["Device"]["DeviceMatch"].split(";"):
# ignore trailing semicolons
if not match or match == "generic":
continue
# For hwdb entries we don't care about name matches,
# it'll just result in duplicate ID_INPUT_TABLET assignments
# for tablets with re-used usbids and that doesn't matter
try:
bus, vid, pid, *_ = match.split("|")
except ValueError as e:
print(f"Failed to process match {match} in {file}", file=sys.stderr)
raise e
name = config["Device"]["Name"]
t = Tablet(name, bus, vid, pid)
try:
t.has_touch = config["Features"]["Touch"].lower() == "true"
if t.has_touch:
integration = config["Device"]["IntegratedIn"]
t.is_touchscreen = (
"Display" in integration or "System" in integration
)
except KeyError:
pass
t.has_pad = any(config.has_section(s) for s in ["Buttons", "Keys"])
yield t
# Guess the udev directory based on path. For the case of /usr/share, the
# udev directory is probably in /usr/lib so let's fallback to that.
def find_udev_base_dir(paths):
for path in paths:
for parent in path.parents:
d = Path(parent / "udev" / "rules.d")
if d.exists():
return d.parent
# /usr/share but also any custom prefixes
for parent in path.parents:
d = Path(parent / "lib" / "udev" / "rules.d")
if d.exists():
return d.parent
raise FileNotFoundError(paths)
# udev's behaviour is that where a file X exists in two locations,
# only the highest-precedence one is read. Our files are supposed to be
# complimentary to the system-installed ones (which default to
# 65-libwacom.hwdb) so we bump the filename number.
def guess_hwdb_filename(basedir):
hwdbdir = Path(basedir) / "hwdb.d"
if not hwdbdir.exists():
raise FileNotFoundError(hwdbdir)
fname = hwdbdir / "66-libwacom.hwdb"
return fname
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Update the system according to the current set of tablet data files"
)
parser.add_argument(
"path",
nargs="?",
type=Path,
default=None,
help="Directory to load .tablet files from. Default: $XDG_CONFIG_HOME/libwacom and /etc/libwacom",
)
# buildsystem-mode is what we use from meson, it changes the
# the behavior to just generate the file and print it
parser.add_argument(
"--buildsystem-mode",
action="store_true",
default=False,
help="be used by the build system only",
)
parser.add_argument(
"--skip-systemd-hwdb-update",
action="store_true",
default=False,
help="Do not run systemd-hwdb --update (Note: updates to tablet files will not be reflected in udev)",
)
parser.add_argument(
"--udev-base-dir",
type=Path,
default=None,
help="The udev base directory (default: guessed based on the path)",
)
ns = parser.parse_args()
if ns.path is None:
paths = [xdg_dir(), Path("/etc/libwacom")]
else:
paths = [ns.path]
# Reverse the list so the most important one is last and takes precedence
paths.reverse()
dbs = [TabletDatabase(p) for p in paths]
hwdb = HWDBFile()
# Bamboo and Intuos devices connected to the system via Wacom's
# Wireless Accessory Kit appear to udev as having the PID of the
# dongle rather than the actual tablet. Make sure we properly tag
# such devices.
#
# We only really care about this in the official hwdb files
if ns.buildsystem_mode:
wwak = Tablet("Wacom Wireless Accessory Kit", "usb", "056A", "0084")
wwak.has_pad = True
wwak.has_touch = True
hwdb.tablets.append(wwak)
for db in dbs:
hwdb.tablets.extend(db.tablets)
if ns.buildsystem_mode:
hwdb.print()
else:
if os.geteuid() == 0:
print(
"WARNING: Running this command as root will not pick up .tablet files in $XDG_CONFIG_HOME/libwacom"
)
try:
udevdir = ns.udev_base_dir or find_udev_base_dir(paths)
hwdbfile = guess_hwdb_filename(udevdir)
with tempfile.NamedTemporaryFile(
mode="w+", prefix=f"{hwdbfile.name}-XXXXXX", encoding="utf-8"
) as fd:
hwdb.print(fd)
print(f"Using sudo to copy hwdb file to {hwdbfile}")
subprocess.run(["sudo", "cp", f"{fd.name}", hwdbfile.absolute()])
if not ns.skip_systemd_hwdb_update:
print("Using sudo to run systemd-hwdb update")
subprocess.run(
["sudo", "systemd-hwdb", "update"],
capture_output=True,
check=True,
text=True,
)
print("Finished, please unplug and replug your device")
except PermissionError as e:
print(f"{e}, please run me as root")
except FileNotFoundError as e:
print(f"Unable to find udev base directory: {e}")
except subprocess.CalledProcessError as e:
print(f"hwdb update failed: {e.stderr}")
except KeyboardInterrupt:
pass
|