| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# This file is formatted with ruff format |
| 4 |
|
| 5 |
from configparser import ConfigParser |
| 6 |
from dataclasses import dataclass, field |
| 7 |
|
| 8 |
import ctypes |
| 9 |
import logging |
| 10 |
import pytest |
| 11 |
import string |
| 12 |
|
| 13 |
from . import ( |
| 14 |
WacomAxisType, |
| 15 |
WacomBuilder, |
| 16 |
WacomBustype, |
| 17 |
WacomDatabase, |
| 18 |
WacomDevice, |
| 19 |
WacomEraserType, |
| 20 |
WacomStatusLed, |
| 21 |
WacomStylus, |
| 22 |
WacomStylusType, |
| 23 |
) |
| 24 |
|
| 25 |
logger = logging.getLogger(__name__) |
| 26 |
|
| 27 |
|
| 28 |
@dataclass |
| 29 |
class StylusEntry: |
| 30 |
id: str |
| 31 |
name: str |
| 32 |
group: str = "generic-with-eraser" |
| 33 |
paired_stylus_ids: list[str] = field(default_factory=list) |
| 34 |
buttons: int = 2 |
| 35 |
axes: str = "Tilt;Pressure;Distance;" |
| 36 |
stylus_type: str = "General" |
| 37 |
eraser_type: str | None = None |
| 38 |
alias_of: str | None = None |
| 39 |
has_wheel: str | None = None |
| 40 |
has_lens: str | None = None |
| 41 |
|
| 42 |
def add_to_config(self, config: ConfigParser): |
| 43 |
c = { |
| 44 |
"Name": self.name, |
| 45 |
"Group": self.group, |
| 46 |
"PairedStylusIds": ";".join(self.paired_stylus_ids), |
| 47 |
"Buttons": f"{self.buttons}", |
| 48 |
"Axes": self.axes, |
| 49 |
"Type": self.stylus_type, |
| 50 |
} |
| 51 |
for attr in ["eraser_type", "alias_of", "has_wheel", "has_lens"]: |
| 52 |
if (val := getattr(self, attr, None)) is not None: |
| 53 |
key = attr.title().replace("_", "") |
| 54 |
# We need lowercase true/false |
| 55 |
c[key] = val if not isinstance(val, bool) else str(val).lower() |
| 56 |
|
| 57 |
config[self.id] = c |
| 58 |
|
| 59 |
@classmethod |
| 60 |
def generic_pen(cls) -> "StylusEntry": |
| 61 |
return cls( |
| 62 |
id="0x0:0xaffff", name="General Pen", paired_stylus_ids=["0x0:0xafffe"] |
| 63 |
) |
| 64 |
|
| 65 |
@classmethod |
| 66 |
def generic_eraser(cls) -> "StylusEntry": |
| 67 |
return StylusEntry( |
| 68 |
id="0x0:0xafffe", |
| 69 |
name="General Pen Eraser", |
| 70 |
paired_stylus_ids=["0x0:0xaffff"], |
| 71 |
eraser_type="Invert", |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
@dataclass |
| 76 |
class StylusFile: |
| 77 |
entries: list[StylusEntry] |
| 78 |
|
| 79 |
@classmethod |
| 80 |
def default(cls) -> "StylusFile": |
| 81 |
return cls( |
| 82 |
entries=[ |
| 83 |
StylusEntry.generic_pen(), |
| 84 |
StylusEntry.generic_eraser(), |
| 85 |
] |
| 86 |
) |
| 87 |
|
| 88 |
def write_to_dir(self, dir, filename="libwacom.stylus"): |
| 89 |
config = ConfigParser() |
| 90 |
config.optionxform = lambda option: option |
| 91 |
|
| 92 |
for s in self.entries: |
| 93 |
s.add_to_config(config) |
| 94 |
|
| 95 |
with open(dir / filename, "w") as fd: |
| 96 |
config.write(fd, space_around_delimiters=False) |
| 97 |
|
| 98 |
if logger.getEffectiveLevel() == logging.DEBUG: |
| 99 |
logger.debug(f"{dir}/{filename}:") |
| 100 |
for line in open(dir / filename).readlines(): |
| 101 |
logger.debug(f" {line.rstrip()}") |
| 102 |
|
| 103 |
|
| 104 |
@dataclass |
| 105 |
class TabletFile: |
| 106 |
name: str |
| 107 |
matches: list[str] |
| 108 |
width: int = 9 |
| 109 |
height: int = 6 |
| 110 |
integrated_in: str = "" |
| 111 |
klass: str = "Bamboo" |
| 112 |
layout: str = "" |
| 113 |
has_stylus: bool = True |
| 114 |
is_reversible: bool = False |
| 115 |
styli: list[str] = field(default_factory=list) |
| 116 |
# extra additions to the tablet file, e.g. { "Buttons": { "Left" : "A;B;" } |
| 117 |
extra: dict[str, dict[str, str]] = field(default_factory=dict) |
| 118 |
|
| 119 |
def write_to(self, filename): |
| 120 |
config = ConfigParser() |
| 121 |
config.optionxform = lambda option: option |
| 122 |
|
| 123 |
cfg = {} |
| 124 |
cfg["Device"] = { |
| 125 |
"Name": self.name, |
| 126 |
"DeviceMatch": ";".join(self.matches), |
| 127 |
"Width": self.width, |
| 128 |
"Height": self.height, |
| 129 |
"IntegratedIn": self.integrated_in, |
| 130 |
"Class": self.klass, |
| 131 |
"Layout": self.layout, |
| 132 |
} |
| 133 |
if self.styli: |
| 134 |
cfg["Device"]["Styli"] = ";".join(self.styli) |
| 135 |
|
| 136 |
cfg["Features"] = { |
| 137 |
"Stylus": self.has_stylus, |
| 138 |
"Reversible": self.is_reversible, |
| 139 |
} |
| 140 |
|
| 141 |
cfg = cfg | self.extra |
| 142 |
|
| 143 |
for k, v in cfg.items(): |
| 144 |
config[k] = v |
| 145 |
|
| 146 |
with open(filename, "w") as fd: |
| 147 |
config.write(fd, space_around_delimiters=False) |
| 148 |
|
| 149 |
if logger.getEffectiveLevel() == logging.DEBUG: |
| 150 |
logger.debug(f"{filename}:") |
| 151 |
for line in open(filename).readlines(): |
| 152 |
logger.debug(f" {line.rstrip()}") |
| 153 |
|
| 154 |
|
| 155 |
@pytest.fixture() |
| 156 |
def custom_datadir(tmp_path): |
| 157 |
StylusFile.default().write_to_dir(tmp_path) |
| 158 |
TabletFile(name="Generic", matches=["generic"]).write_to( |
| 159 |
tmp_path / "generic.tablet" |
| 160 |
) |
| 161 |
return tmp_path |
| 162 |
|
| 163 |
|
| 164 |
def test_database_init(db): |
| 165 |
"""Just a test to make sure it doesn't crash""" |
| 166 |
assert db is not None |
| 167 |
|
| 168 |
|
| 169 |
def test_invalid_device(db): |
| 170 |
device = db.new_from_usbid(0x0, 0x0) |
| 171 |
assert device is None |
| 172 |
|
| 173 |
|
| 174 |
def test_intuos4(db): |
| 175 |
device = db.new_from_usbid(0x056A, 0x00BC) |
| 176 |
assert device is not None |
| 177 |
|
| 178 |
assert device.name == "Wacom Intuos4 WL" |
| 179 |
assert device.get_class() == device.CLASS_INTUOS4 |
| 180 |
assert device.vendor_id == 0x56A |
| 181 |
assert device.product_id == 0xBC |
| 182 |
assert device.bustype == WacomBustype.USB |
| 183 |
assert device.num_buttons == 9 |
| 184 |
assert device.has_stylus() |
| 185 |
assert device.is_reversible() |
| 186 |
assert not device.has_touch() |
| 187 |
assert device.has_ring() |
| 188 |
assert not device.has_ring2() |
| 189 |
assert device.num_rings == 1 |
| 190 |
assert not device.has_touchswitch() |
| 191 |
assert device.num_strips == 0 |
| 192 |
assert device.num_dials == 0 |
| 193 |
assert device.integration_flags == [] |
| 194 |
assert device.width == 8 |
| 195 |
assert device.height == 5 |
| 196 |
|
| 197 |
matches = device.matches |
| 198 |
assert len(matches) == 2 |
| 199 |
assert any(match.bustype == device.bustype for match in matches) |
| 200 |
assert any(match.vendor_id == device.vendor_id for match in matches) |
| 201 |
assert any(match.product_id == device.product_id for match in matches) |
| 202 |
|
| 203 |
|
| 204 |
def test_intuos4_wl(db): |
| 205 |
device = db.new_from_usbid(0x056A, 0x00B9) |
| 206 |
assert device is not None |
| 207 |
|
| 208 |
assert WacomDevice.ButtonFlags.RING_MODESWITCH in device.button_flags("A") |
| 209 |
assert WacomDevice.ButtonFlags.OLED in device.button_flags("I") |
| 210 |
assert device.ring_num_modes == 4 |
| 211 |
|
| 212 |
|
| 213 |
def test_cintiq24hd(db): |
| 214 |
device = db.new_from_usbid(0x056A, 0x00F4) |
| 215 |
assert device is not None |
| 216 |
|
| 217 |
assert device.ring_num_modes == 3 |
| 218 |
assert device.ring2_num_modes == 3 |
| 219 |
|
| 220 |
|
| 221 |
def test_cintiq21ux(db): |
| 222 |
device = db.new_from_usbid(0x056A, 0x00CC) |
| 223 |
assert device is not None |
| 224 |
|
| 225 |
assert device.num_strips == 2 |
| 226 |
assert device.num_dials == 0 |
| 227 |
|
| 228 |
|
| 229 |
def test_wacf004(db): |
| 230 |
device = db.new_from_name("Wacom Serial Tablet WACf004") |
| 231 |
assert device is not None |
| 232 |
assert device.model_name is None |
| 233 |
assert device.integration_flags == [ |
| 234 |
WacomDevice.IntegrationFlags.DISPLAY, |
| 235 |
WacomDevice.IntegrationFlags.SYSTEM, |
| 236 |
] |
| 237 |
|
| 238 |
|
| 239 |
def test_cintiq24hdt(db): |
| 240 |
device = db.new_from_usbid(0x056A, 0x00F8) |
| 241 |
assert device is not None |
| 242 |
|
| 243 |
match = device.paired_device |
| 244 |
assert match is not None |
| 245 |
assert match.vendor_id == 0x56A |
| 246 |
assert match.product_id == 0xF6 |
| 247 |
assert match.bustype == device.BUSTYPE_USB |
| 248 |
|
| 249 |
modes = {"A": 0, "B": 1, "C": 2, "I": 0, "J": 1, "K": 2} |
| 250 |
for btn in string.ascii_uppercase[: device.num_buttons]: |
| 251 |
expected_mode = modes.get(btn, WacomDevice.ModeSwitch.NEXT) |
| 252 |
assert device.button_modeswitch_mode(btn) == expected_mode |
| 253 |
|
| 254 |
|
| 255 |
def test_cintiq13hd(db): |
| 256 |
libevdev = pytest.importorskip("libevdev") |
| 257 |
device = db.new_from_name("Wacom Cintiq 13HD") |
| 258 |
assert device is not None |
| 259 |
|
| 260 |
assert device.button_evdev_code("A") == libevdev.EV_KEY.BTN_0.value |
| 261 |
assert device.button_evdev_code("B") == libevdev.EV_KEY.BTN_1.value |
| 262 |
assert device.button_evdev_code("C") == libevdev.EV_KEY.BTN_2.value |
| 263 |
assert device.button_evdev_code("D") == libevdev.EV_KEY.BTN_3.value |
| 264 |
assert device.button_evdev_code("E") == libevdev.EV_KEY.BTN_4.value |
| 265 |
assert device.button_evdev_code("F") == libevdev.EV_KEY.BTN_5.value |
| 266 |
assert device.button_evdev_code("G") == libevdev.EV_KEY.BTN_6.value |
| 267 |
assert device.button_evdev_code("H") == libevdev.EV_KEY.BTN_7.value |
| 268 |
assert device.button_evdev_code("I") == libevdev.EV_KEY.BTN_8.value |
| 269 |
assert device.model_name == "DTK-1300" |
| 270 |
|
| 271 |
|
| 272 |
def test_cintiqpro13(db): |
| 273 |
device = db.new_from_name("Wacom Cintiq Pro 13") |
| 274 |
assert device is not None |
| 275 |
assert device.num_keys == 5 |
| 276 |
|
| 277 |
for btn in string.ascii_uppercase[: device.num_buttons]: |
| 278 |
assert device.button_modeswitch_mode(btn) == WacomDevice.ModeSwitch.NEXT |
| 279 |
|
| 280 |
|
| 281 |
def test_dell_canvas(db): |
| 282 |
device = db.new_from_name("Dell Canvas 27") |
| 283 |
assert device is not None |
| 284 |
assert device.integration_flags == [WacomDevice.IntegrationFlags.DISPLAY] |
| 285 |
|
| 286 |
|
| 287 |
def test_bamboo_pen(db): |
| 288 |
libevdev = pytest.importorskip("libevdev") |
| 289 |
|
| 290 |
device = db.new_from_name("Wacom Bamboo Pen") |
| 291 |
assert device is not None |
| 292 |
assert device.button_evdev_code("A") == libevdev.EV_KEY.BTN_BACK.value |
| 293 |
assert device.button_evdev_code("B") == libevdev.EV_KEY.BTN_FORWARD.value |
| 294 |
assert device.button_evdev_code("C") == libevdev.EV_KEY.BTN_LEFT.value |
| 295 |
assert device.button_evdev_code("D") == libevdev.EV_KEY.BTN_RIGHT.value |
| 296 |
assert device.model_name == "MTE-450" |
| 297 |
|
| 298 |
|
| 299 |
def test_isdv4_4800(db): |
| 300 |
device = db.new_from_usbid(0x56A, 0x4800) |
| 301 |
assert device is not None |
| 302 |
|
| 303 |
assert device.integration_flags == [ |
| 304 |
WacomDevice.IntegrationFlags.DISPLAY, |
| 305 |
WacomDevice.IntegrationFlags.SYSTEM, |
| 306 |
] |
| 307 |
assert device.model_name is None |
| 308 |
|
| 309 |
assert device.vendor_id == 0x56A |
| 310 |
assert device.product_id == 0x4800 |
| 311 |
assert device.num_buttons == 0 |
| 312 |
|
| 313 |
|
| 314 |
def test_mobilestudio_pro_modeswitch(db): |
| 315 |
device = db.new_from_name("Wacom MobileStudio Pro 13") |
| 316 |
assert device is not None |
| 317 |
|
| 318 |
modes = {"H": 0, "I": 1, "J": 2, "K": 3} |
| 319 |
for btn in string.ascii_uppercase[: device.num_buttons]: |
| 320 |
expected_mode = modes.get(btn, WacomDevice.ModeSwitch.NEXT) |
| 321 |
assert device.button_modeswitch_mode(btn) == expected_mode |
| 322 |
|
| 323 |
|
| 324 |
@pytest.mark.parametrize( |
| 325 |
"usbid,expected", |
| 326 |
[ |
| 327 |
[(0x256C, 0x0067), [WacomStylus.Generic.PEN_NO_ERASER]], |
| 328 |
[ |
| 329 |
(0x04F3, 0x264C), |
| 330 |
[ |
| 331 |
WacomStylus.Generic.PEN_WITH_ERASER, |
| 332 |
WacomStylus.Generic.ERASER, |
| 333 |
WacomStylus.Generic.PEN_NO_ERASER, |
| 334 |
], |
| 335 |
], |
| 336 |
], |
| 337 |
) |
| 338 |
def test_generic_pens(db, usbid, expected): |
| 339 |
# Inspiroy 2 has a generic-pen-no-eraser |
| 340 |
device = db.new_from_usbid(*usbid) |
| 341 |
assert device is not None |
| 342 |
|
| 343 |
nstyli = ctypes.c_int() |
| 344 |
styli = device.get_supported_styli(ctypes.byref(nstyli)) |
| 345 |
s = [WacomStylus.Generic(id) for id in styli[: nstyli.value]] |
| 346 |
|
| 347 |
assert sorted(s) == sorted(expected) |
| 348 |
|
| 349 |
|
| 350 |
@pytest.mark.parametrize( |
| 351 |
"bus,vid,pid", |
| 352 |
[ |
| 353 |
(WacomBustype.USB, 0x56A, 0xBC), |
| 354 |
(WacomBustype.BLUETOOTH, 0x56A, 0xBD), |
| 355 |
(WacomBustype.UNKNOWN, 0x56A, 0xBD), |
| 356 |
], |
| 357 |
) |
| 358 |
def test_new_from_builder_ids(db, bus, vid, pid): |
| 359 |
match = WacomBuilder.create(bus=bus, usbid=(vid, pid)) |
| 360 |
device = db.new_from_builder(match) |
| 361 |
|
| 362 |
assert device is not None |
| 363 |
assert device.vendor_id == vid |
| 364 |
assert device.product_id == pid |
| 365 |
if bus != WacomBustype.UNKNOWN: |
| 366 |
assert device.bustype == bus |
| 367 |
else: |
| 368 |
# unkonwn bustype means "search for it" and |
| 369 |
# for this test that's bluetooth: |
| 370 |
# 0x56a/0bd is a bluetooth tablet |
| 371 |
assert device.bustype == WacomBustype.BLUETOOTH |
| 372 |
|
| 373 |
|
| 374 |
def test_new_from_builder_empty(db): |
| 375 |
builder = WacomBuilder.create() |
| 376 |
device = db.new_from_builder(builder) |
| 377 |
assert device is None, f"Unexpected device: {device.name}" |
| 378 |
|
| 379 |
|
| 380 |
def test_new_from_builder_device_name(db): |
| 381 |
builder = WacomBuilder.create(device_name="Wacom Bamboo Pen") |
| 382 |
device = db.new_from_builder(builder) |
| 383 |
assert device is not None |
| 384 |
|
| 385 |
# Fallback device with name override |
| 386 |
builder.device_name = "does not exist" |
| 387 |
device = db.new_from_builder(builder, fallback=WacomDatabase.Fallback.GENERIC) |
| 388 |
assert device is not None |
| 389 |
assert device.name == "does not exist" |
| 390 |
|
| 391 |
|
| 392 |
def test_new_from_builder_uniq(db): |
| 393 |
builder = WacomBuilder.create(uniq="OEM02_T18e") |
| 394 |
device = db.new_from_builder(builder) |
| 395 |
assert device is not None |
| 396 |
assert device.name == "GAOMON S620" |
| 397 |
|
| 398 |
# uniq + match name triggers normal builder |
| 399 |
# but since vid/pid isn't set this does not find a match |
| 400 |
builder = WacomBuilder.create(uniq="OEM02_T18e") |
| 401 |
builder.match_name = "GAOMON Gaomon Tablet Pen" |
| 402 |
device = db.new_from_builder(builder) |
| 403 |
assert device is None |
| 404 |
|
| 405 |
# Once we set the vid/pid we get a match |
| 406 |
builder.usbid = (0x256C, 0x6D) |
| 407 |
device = db.new_from_builder(builder) |
| 408 |
assert device is not None |
| 409 |
assert device.name == "GAOMON S620" |
| 410 |
|
| 411 |
# uniq + name triggers normal builder but we don't have a code path |
| 412 |
# for that, it's a normal nameless match and |
| 413 |
# since vid/pid isn't set this does not find a match |
| 414 |
builder = WacomBuilder.create(uniq="OEM02_T18e") |
| 415 |
builder.device_name = "GAOMON S620" |
| 416 |
device = db.new_from_builder(builder) |
| 417 |
assert device is None |
| 418 |
|
| 419 |
|
| 420 |
def test_exact_matches(custom_datadir): |
| 421 |
USBID = (0x1234, 0x5678) |
| 422 |
UNIQ = "uniqval" |
| 423 |
NAME = "nameval" |
| 424 |
|
| 425 |
# A device match with uniq but no name |
| 426 |
matches = ["usb|1234|5678||uniqval"] |
| 427 |
TabletFile(name="UniqOnly", matches=matches).write_to( |
| 428 |
custom_datadir / "uniq.tablet" |
| 429 |
) |
| 430 |
|
| 431 |
# A device match with a name but no uniq |
| 432 |
matches = ["usb|1234|5678|nameval"] |
| 433 |
TabletFile(name="NameOnly", matches=matches).write_to( |
| 434 |
custom_datadir / "name.tablet" |
| 435 |
) |
| 436 |
|
| 437 |
# A device match with both |
| 438 |
matches = ["usb|1234|5678|nameval|uniqval"] |
| 439 |
TabletFile(name="Both", matches=matches).write_to(custom_datadir / "both.tablet") |
| 440 |
|
| 441 |
db = WacomDatabase(path=custom_datadir) |
| 442 |
|
| 443 |
builder = WacomBuilder.create(usbid=USBID, uniq=UNIQ) |
| 444 |
device = db.new_from_builder(builder) |
| 445 |
assert device is not None |
| 446 |
assert device.name == "UniqOnly" |
| 447 |
|
| 448 |
builder = WacomBuilder.create(usbid=USBID, match_name=NAME) |
| 449 |
device = db.new_from_builder(builder) |
| 450 |
assert device is not None |
| 451 |
assert device.name == "NameOnly" |
| 452 |
|
| 453 |
builder = WacomBuilder.create(usbid=USBID, uniq=UNIQ, match_name=NAME) |
| 454 |
device = db.new_from_builder(builder) |
| 455 |
assert device is not None |
| 456 |
assert device.name == "Both" |
| 457 |
|
| 458 |
|
| 459 |
def test_prefer_uniq_over_name(custom_datadir): |
| 460 |
USBID = (0x1234, 0x5678) |
| 461 |
UNIQ = "uniqval" |
| 462 |
NAME = "nameval" |
| 463 |
|
| 464 |
# A device match with uniq but no name |
| 465 |
matches = ["usb|1234|5678||uniqval"] |
| 466 |
TabletFile(name="UniqOnly", matches=matches).write_to( |
| 467 |
custom_datadir / "uniq.tablet" |
| 468 |
) |
| 469 |
|
| 470 |
# A device match with a name but no uniq |
| 471 |
matches = ["usb|1234|5678|nameval"] |
| 472 |
TabletFile(name="NameOnly", matches=matches).write_to( |
| 473 |
custom_datadir / "name.tablet" |
| 474 |
) |
| 475 |
|
| 476 |
db = WacomDatabase(path=custom_datadir) |
| 477 |
|
| 478 |
# name and uniq set in our match but we don't have a device with both. |
| 479 |
# Prefer the uniq match over the name match |
| 480 |
builder = WacomBuilder.create(usbid=USBID, uniq=UNIQ, match_name=NAME) |
| 481 |
device = db.new_from_builder(builder) |
| 482 |
assert device is not None |
| 483 |
assert device.name == "UniqOnly" |
| 484 |
|
| 485 |
# If we have a uniq in our match but none of the DeviceMatches |
| 486 |
# have that, fall back to name only |
| 487 |
builder = WacomBuilder.create(usbid=USBID, uniq="whatever", match_name=NAME) |
| 488 |
device = db.new_from_builder(builder) |
| 489 |
assert device is not None |
| 490 |
assert device.name == "NameOnly" |
| 491 |
|
| 492 |
# If we have a name in our match but none of the DeviceMatches |
| 493 |
# have that, fall back to uniq only |
| 494 |
builder = WacomBuilder.create(usbid=USBID, uniq=UNIQ, match_name="whatever") |
| 495 |
device = db.new_from_builder(builder) |
| 496 |
assert device is not None |
| 497 |
assert device.name == "UniqOnly" |
| 498 |
|
| 499 |
|
| 500 |
def test_dont_ignore_exact_matches(custom_datadir): |
| 501 |
USBID = (0x1234, 0x5678) |
| 502 |
UNIQ = "uniqval" |
| 503 |
NAME = "nameval" |
| 504 |
|
| 505 |
# A device match with both |
| 506 |
matches = ["usb|1234|5678|nameval|uniqval"] |
| 507 |
TabletFile(name="Both", matches=matches).write_to(custom_datadir / "both.tablet") |
| 508 |
|
| 509 |
db = WacomDatabase(path=custom_datadir) |
| 510 |
|
| 511 |
builder = WacomBuilder.create(usbid=USBID, uniq=UNIQ, match_name=NAME) |
| 512 |
device = db.new_from_builder(builder) |
| 513 |
assert device is not None |
| 514 |
assert device.name == "Both" |
| 515 |
|
| 516 |
# Our DeviceMatch has both uniq and name set, so only match |
| 517 |
# when *both* match |
| 518 |
builder = WacomBuilder.create(usbid=USBID, uniq=UNIQ, match_name="whatever") |
| 519 |
device = db.new_from_builder(builder) |
| 520 |
assert device is None |
| 521 |
|
| 522 |
builder = WacomBuilder.create(usbid=USBID, uniq="whatever", match_name=NAME) |
| 523 |
device = db.new_from_builder(builder) |
| 524 |
assert device is None |
| 525 |
|
| 526 |
|
| 527 |
# Emulates the behavior of new_from_path for an unknown device but without |
| 528 |
# uinput devices |
| 529 |
@pytest.mark.parametrize( |
| 530 |
"fallback", (WacomDatabase.Fallback.NONE, WacomDatabase.Fallback.GENERIC) |
| 531 |
) |
| 532 |
@pytest.mark.parametrize("bustype", (WacomBustype.USB, WacomBustype.BLUETOOTH)) |
| 533 |
def test_new_unknown_device_with_fallback(custom_datadir, fallback, bustype): |
| 534 |
USBID = (0x1234, 0x5678) |
| 535 |
NAME = "nameval" |
| 536 |
db = WacomDatabase(path=custom_datadir) |
| 537 |
builder = WacomBuilder.create( |
| 538 |
usbid=USBID, bus=bustype, match_name=NAME, device_name=NAME |
| 539 |
) |
| 540 |
|
| 541 |
device = db.new_from_builder(builder, fallback=fallback) |
| 542 |
if fallback: |
| 543 |
assert device is not None |
| 544 |
match = device.match |
| 545 |
assert match.decode("utf-8") == "generic" |
| 546 |
# Generic device always has 0, 0, 0 triple for bus/vid/pid |
| 547 |
assert device.bustype == WacomBustype.UNKNOWN |
| 548 |
assert device.vendor_id == 0 |
| 549 |
assert device.product_id == 0 |
| 550 |
assert device.name == NAME |
| 551 |
else: |
| 552 |
assert device is None |
| 553 |
|
| 554 |
|
| 555 |
def create_uinput(name, vid, pid, bustype=0x3): |
| 556 |
libevdev = pytest.importorskip("libevdev") |
| 557 |
dev = libevdev.Device() |
| 558 |
dev.name = name |
| 559 |
dev.id = {"bustype": bustype, "vendor": vid, "product": pid} |
| 560 |
dev.enable( |
| 561 |
libevdev.EV_ABS.ABS_X, |
| 562 |
libevdev.InputAbsInfo(minimum=0, maximum=10000, resolution=200), |
| 563 |
) |
| 564 |
dev.enable( |
| 565 |
libevdev.EV_ABS.ABS_Y, |
| 566 |
libevdev.InputAbsInfo(minimum=0, maximum=10000, resolution=200), |
| 567 |
) |
| 568 |
dev.enable(libevdev.EV_KEY.BTN_STYLUS) |
| 569 |
dev.enable(libevdev.EV_KEY.BTN_TOOL_PEN) |
| 570 |
try: |
| 571 |
return dev.create_uinput_device() |
| 572 |
except OSError as e: |
| 573 |
pytest.skip(f"Failed to create uinput device: {e}") |
| 574 |
|
| 575 |
|
| 576 |
@pytest.mark.parametrize( |
| 577 |
"fallback", (WacomDatabase.Fallback.NONE, WacomDatabase.Fallback.GENERIC) |
| 578 |
) |
| 579 |
def test_new_from_path_known_device(db, fallback): |
| 580 |
name = "Wacom Intuos4 WL" |
| 581 |
vid = 0x056A |
| 582 |
pid = 0x00BC |
| 583 |
uinput = create_uinput(name, vid, pid) |
| 584 |
|
| 585 |
dev = db.new_from_path( |
| 586 |
uinput.devnode, fallback=fallback |
| 587 |
) # fallback has no effect here |
| 588 |
assert dev is not None |
| 589 |
assert dev.name == name |
| 590 |
assert dev.vendor_id == vid |
| 591 |
assert dev.product_id == pid |
| 592 |
|
| 593 |
|
| 594 |
@pytest.mark.parametrize("bustype", WacomBustype) |
| 595 |
@pytest.mark.parametrize( |
| 596 |
"fallback", (WacomDatabase.Fallback.NONE, WacomDatabase.Fallback.GENERIC) |
| 597 |
) |
| 598 |
def test_new_from_path_unknown_device(db, fallback, bustype): |
| 599 |
bus = { |
| 600 |
WacomBustype.UNKNOWN: 0, |
| 601 |
WacomBustype.USB: 0x3, |
| 602 |
WacomBustype.BLUETOOTH: 0x5, |
| 603 |
WacomBustype.I2C: 0x18, |
| 604 |
}[bustype] |
| 605 |
|
| 606 |
name = "Unknown device" |
| 607 |
vid = 0x1234 |
| 608 |
pid = 0xABAC |
| 609 |
uinput = create_uinput(name, vid, pid, bustype=bus) |
| 610 |
|
| 611 |
dev = db.new_from_path( |
| 612 |
uinput.devnode, fallback=fallback |
| 613 |
) # fallback has no effect here |
| 614 |
if fallback == WacomDatabase.Fallback.NONE: |
| 615 |
assert dev is None |
| 616 |
else: |
| 617 |
assert dev is not None |
| 618 |
assert dev.name == name |
| 619 |
assert dev.vendor_id == 0 |
| 620 |
assert dev.product_id == 0 |
| 621 |
assert dev.bustype == 0 # fallback device is always bustype 0 |
| 622 |
|
| 623 |
|
| 624 |
@pytest.mark.parametrize( |
| 625 |
"feature", |
| 626 |
("Ring", "Strip", "Dial"), |
| 627 |
) |
| 628 |
@pytest.mark.parametrize("count", (1, 2)) |
| 629 |
def test_button_modeswitch(custom_datadir, feature, count): |
| 630 |
USBID = (0x1234, 0x5678) |
| 631 |
|
| 632 |
extra = { |
| 633 |
"Buttons": { |
| 634 |
"Left": "A;B;C;", |
| 635 |
"Right": "D;E;F;", |
| 636 |
feature: "A", |
| 637 |
}, |
| 638 |
"Features": { |
| 639 |
f"{feature}s": 4, |
| 640 |
}, |
| 641 |
} |
| 642 |
if count > 1: |
| 643 |
extra["Buttons"][f"{feature}2"] = "D" |
| 644 |
|
| 645 |
TabletFile( |
| 646 |
name="some tablet", |
| 647 |
matches=[f"usb|{USBID[0]:04x}|{USBID[1]:04x}"], |
| 648 |
extra=extra, |
| 649 |
).write_to(custom_datadir / "led.tablet") |
| 650 |
|
| 651 |
db = WacomDatabase(path=custom_datadir) |
| 652 |
builder = WacomBuilder.create(usbid=USBID) |
| 653 |
device = db.new_from_builder(builder) |
| 654 |
assert device is not None |
| 655 |
|
| 656 |
expected_flag = { |
| 657 |
"Ring": WacomDevice.ButtonFlags.RING_MODESWITCH, |
| 658 |
"Strip": WacomDevice.ButtonFlags.TOUCHSTRIP_MODESWITCH, |
| 659 |
"Dial": WacomDevice.ButtonFlags.DIAL_MODESWITCH, |
| 660 |
}[feature] |
| 661 |
|
| 662 |
flags = device.button_flags("A") |
| 663 |
assert expected_flag in flags |
| 664 |
|
| 665 |
for b in "BCDEF": |
| 666 |
flags = device.button_flags(b) |
| 667 |
assert expected_flag not in flags |
| 668 |
|
| 669 |
expected_flag = { |
| 670 |
"Ring": WacomDevice.ButtonFlags.RING2_MODESWITCH, |
| 671 |
"Strip": WacomDevice.ButtonFlags.TOUCHSTRIP2_MODESWITCH, |
| 672 |
"Dial": WacomDevice.ButtonFlags.DIAL2_MODESWITCH, |
| 673 |
}[feature] |
| 674 |
|
| 675 |
flags = device.button_flags("D") |
| 676 |
if count > 1: |
| 677 |
assert expected_flag in flags |
| 678 |
else: |
| 679 |
assert expected_flag not in flags |
| 680 |
|
| 681 |
for b in "ABCEF": |
| 682 |
flags = device.button_flags(b) |
| 683 |
assert expected_flag not in flags |
| 684 |
|
| 685 |
|
| 686 |
@pytest.mark.parametrize( |
| 687 |
"feature", |
| 688 |
("Ring", "Strip", "Dial"), |
| 689 |
) |
| 690 |
@pytest.mark.parametrize("count", (1, 2)) |
| 691 |
def test_status_leds(custom_datadir, feature, count): |
| 692 |
USBID = (0x1234, 0x5678) |
| 693 |
|
| 694 |
extra = { |
| 695 |
"Buttons": { |
| 696 |
"Left": "A;B;C;", |
| 697 |
"Right": "D;E;F;", |
| 698 |
feature: "A", |
| 699 |
}, |
| 700 |
"Features": { |
| 701 |
"StatusLEDs": f"{feature};{feature}2" if count > 1 else f"{feature}", |
| 702 |
f"{feature}s": 4, |
| 703 |
}, |
| 704 |
} |
| 705 |
if count > 1: |
| 706 |
extra["Buttons"][f"{feature}2"] = "D" |
| 707 |
|
| 708 |
TabletFile( |
| 709 |
name="some tablet", |
| 710 |
matches=[f"usb|{USBID[0]:04x}|{USBID[1]:04x}"], |
| 711 |
extra=extra, |
| 712 |
).write_to(custom_datadir / "led.tablet") |
| 713 |
|
| 714 |
db = WacomDatabase(path=custom_datadir) |
| 715 |
builder = WacomBuilder.create(usbid=USBID) |
| 716 |
device = db.new_from_builder(builder) |
| 717 |
assert device is not None |
| 718 |
|
| 719 |
expected = [ |
| 720 |
{ |
| 721 |
"Ring": WacomStatusLed.RING, |
| 722 |
"Strip": WacomStatusLed.TOUCHSTRIP, |
| 723 |
"Dial": WacomStatusLed.DIAL, |
| 724 |
}[feature] |
| 725 |
] |
| 726 |
|
| 727 |
if count > 1: |
| 728 |
expected.append( |
| 729 |
{ |
| 730 |
"Ring": WacomStatusLed.RING2, |
| 731 |
"Strip": WacomStatusLed.TOUCHSTRIP2, |
| 732 |
"Dial": WacomStatusLed.DIAL2, |
| 733 |
}[feature] |
| 734 |
) |
| 735 |
|
| 736 |
leds = device.status_leds |
| 737 |
assert sorted(leds) == sorted(expected) |
| 738 |
|
| 739 |
led_group = device.button_led_group("A") |
| 740 |
assert led_group == 0 |
| 741 |
|
| 742 |
led_group = device.button_led_group("D") |
| 743 |
if count > 1: |
| 744 |
assert led_group == 1 |
| 745 |
else: |
| 746 |
assert led_group == -1 |
| 747 |
|
| 748 |
for b in "BCEF": |
| 749 |
led_group = device.button_led_group(b) |
| 750 |
assert led_group == -1 |
| 751 |
|
| 752 |
|
| 753 |
def test_nonwacom_stylus_ids(tmp_path): |
| 754 |
styli = StylusFile.default() |
| 755 |
s1 = StylusEntry( |
| 756 |
id="0x1234:0xabcd", |
| 757 |
name="ABC Pen", |
| 758 |
group="notwacom", |
| 759 |
paired_stylus_ids=["0x1234:0x9876"], |
| 760 |
) |
| 761 |
s2 = StylusEntry( |
| 762 |
id="0x1234:0x9876", |
| 763 |
name="9876 Pen", |
| 764 |
group="notwacom", |
| 765 |
paired_stylus_ids=["0x1234:0xabcd"], |
| 766 |
eraser_type="Invert", |
| 767 |
) |
| 768 |
styli.entries.append(s1) |
| 769 |
styli.entries.append(s2) |
| 770 |
styli.write_to_dir(tmp_path) |
| 771 |
|
| 772 |
# matches our nonwacom group |
| 773 |
TabletFile( |
| 774 |
name="ABC Group Tablet", |
| 775 |
matches=["usb|9999|abcd"], |
| 776 |
styli=["@notwacom"], |
| 777 |
).write_to(tmp_path / "group.tablet") |
| 778 |
|
| 779 |
# matches one nonwacom styli and the default generic ones |
| 780 |
TabletFile( |
| 781 |
name="ABC Tablet", |
| 782 |
matches=["usb|8888|abcd"], |
| 783 |
styli=[s2.id, "@generic-with-eraser"], |
| 784 |
).write_to(tmp_path / "abc.tablet") |
| 785 |
|
| 786 |
db = WacomDatabase(path=tmp_path) |
| 787 |
|
| 788 |
builder = WacomBuilder.create(usbid=(0x9999, 0xABCD)) |
| 789 |
device = db.new_from_builder(builder) |
| 790 |
assert device is not None |
| 791 |
assert device.name == "ABC Group Tablet" |
| 792 |
styli = device.get_styli() |
| 793 |
assert len(styli) == 2 |
| 794 |
assert styli[0].vendor_id == 0x1234 |
| 795 |
assert styli[1].vendor_id == 0x1234 |
| 796 |
# Order of styli is undefined |
| 797 |
assert styli[0].tool_id == 0xABCD or styli[1].tool_id == 0xABCD |
| 798 |
assert styli[0].tool_id == 0x9876 or styli[1].tool_id == 0x9876 |
| 799 |
assert sum(s.is_eraser for s in styli) == 1 |
| 800 |
for s in filter(lambda s: s.is_eraser, styli): |
| 801 |
assert s.eraser_type == WacomEraserType.INVERT |
| 802 |
|
| 803 |
assert sum(s.is_eraser is True for s in styli) == 1 |
| 804 |
|
| 805 |
paired0 = styli[0].get_paired_styli() |
| 806 |
paired1 = styli[1].get_paired_styli() |
| 807 |
assert len(paired0) == 1 |
| 808 |
assert len(paired1) == 1 |
| 809 |
assert paired0[0].vendor_id == styli[1].vendor_id |
| 810 |
assert paired0[0].tool_id != styli[1].vendor_id |
| 811 |
assert paired1[0].vendor_id == styli[0].vendor_id |
| 812 |
assert paired1[0].tool_id != styli[0].vendor_id |
| 813 |
|
| 814 |
builder = WacomBuilder.create(usbid=(0x8888, 0xABCD)) |
| 815 |
device = db.new_from_builder(builder) |
| 816 |
assert device is not None |
| 817 |
assert device.name == "ABC Tablet" |
| 818 |
styli = device.get_styli() |
| 819 |
assert len(styli) == 3 # 1 non-wacom, 2 generic ones |
| 820 |
# Order of styli is undefined |
| 821 |
assert sum(s.vendor_id == 0x1234 and s.tool_id == 0x9876 for s in styli) == 1 |
| 822 |
assert sum(s.vendor_id == 0 and s.tool_id == 0xAFFFE for s in styli) == 1 |
| 823 |
assert sum(s.vendor_id == 0 and s.tool_id == 0xAFFFF for s in styli) == 1 |
| 824 |
|
| 825 |
|
| 826 |
def test_load_xdg_config_home(monkeypatch, tmp_path, custom_datadir): |
| 827 |
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path.absolute())) |
| 828 |
|
| 829 |
xdg = tmp_path / "libwacom" |
| 830 |
xdg.mkdir() |
| 831 |
|
| 832 |
usbid = (0x1234, 0x5678) |
| 833 |
matches = [f"usb|{usbid[0]:04x}|{usbid[1]:04x}"] |
| 834 |
TabletFile(name="XDGTablet", matches=matches).write_to(xdg / "uniq.tablet") |
| 835 |
|
| 836 |
StylusFile.default().write_to_dir(xdg) |
| 837 |
|
| 838 |
# This should load from system *and* XDG. system files could |
| 839 |
# interfere with our test or may not exist but unfortunately we can't |
| 840 |
# chroot for the test. it should be good enough this way anyway. |
| 841 |
db = WacomDatabase() |
| 842 |
builder = WacomBuilder.create(usbid=usbid) |
| 843 |
device = db.new_from_builder(builder) |
| 844 |
assert device is not None and device.name == "XDGTablet" |
| 845 |
|
| 846 |
|
| 847 |
@pytest.mark.parametrize( |
| 848 |
"override,value", |
| 849 |
( |
| 850 |
("axes", "Tilt;"), |
| 851 |
("buttons", 3), |
| 852 |
("has_lens", True), |
| 853 |
("has_wheel", True), |
| 854 |
("stylus_type", WacomStylusType.AIRBRUSH), |
| 855 |
("eraser_type", WacomEraserType.INVERT), |
| 856 |
), |
| 857 |
) |
| 858 |
def test_alias_of(custom_datadir, override, value): |
| 859 |
usbid = (0x1234, 0x5678) |
| 860 |
matches = [f"usb|{usbid[0]:04x}|{usbid[1]:04x}"] |
| 861 |
TabletFile(name="XDGTablet", matches=matches, styli=["@happy-aliases"]).write_to( |
| 862 |
custom_datadir / "uniq.tablet" |
| 863 |
) |
| 864 |
|
| 865 |
stylusfile = StylusFile.default() |
| 866 |
stylusfile.entries.append( |
| 867 |
StylusEntry( |
| 868 |
id="0x1234:0xabcd", |
| 869 |
name="To be aliased", |
| 870 |
group="happy-aliases", |
| 871 |
) |
| 872 |
) |
| 873 |
|
| 874 |
alias = StylusEntry( |
| 875 |
id="0x1234:0xffff", |
| 876 |
name="AliasTester", |
| 877 |
alias_of="0x1234:0xabcd", |
| 878 |
group="happy-aliases", |
| 879 |
) |
| 880 |
if override == "stylus_type": |
| 881 |
setattr(alias, override, "Airbrush") |
| 882 |
elif override == "eraser_type": |
| 883 |
setattr(alias, override, "Invert") |
| 884 |
else: |
| 885 |
setattr(alias, override, value) |
| 886 |
|
| 887 |
stylusfile.entries.append(alias) |
| 888 |
stylusfile.write_to_dir(custom_datadir) |
| 889 |
|
| 890 |
db = WacomDatabase(path=custom_datadir) |
| 891 |
builder = WacomBuilder.create(usbid=usbid) |
| 892 |
device = db.new_from_builder(builder) |
| 893 |
assert device is not None and device.name == "XDGTablet" |
| 894 |
|
| 895 |
styli = device.get_styli() |
| 896 |
assert len(styli) == 2 |
| 897 |
|
| 898 |
original = styli[0] |
| 899 |
aliasing = styli[1] |
| 900 |
|
| 901 |
assert original.vendor_id == 0x1234 |
| 902 |
assert original.tool_id == 0xABCD |
| 903 |
assert original.num_buttons == 2 |
| 904 |
assert original.axes == [ |
| 905 |
WacomAxisType.TILT, |
| 906 |
WacomAxisType.DISTANCE, |
| 907 |
WacomAxisType.PRESSURE, |
| 908 |
] |
| 909 |
assert not original.has_lens |
| 910 |
assert not original.has_wheel |
| 911 |
assert original.eraser_type == WacomEraserType.NONE |
| 912 |
|
| 913 |
assert aliasing.vendor_id == 0x1234 |
| 914 |
assert aliasing.tool_id == 0xFFFF |
| 915 |
if override == "buttons": |
| 916 |
assert aliasing.num_buttons == value |
| 917 |
else: |
| 918 |
assert aliasing.num_buttons == 2 |
| 919 |
|
| 920 |
if override == "axes": |
| 921 |
assert aliasing.axes == [WacomAxisType.TILT] |
| 922 |
else: |
| 923 |
assert aliasing.axes == [ |
| 924 |
WacomAxisType.TILT, |
| 925 |
WacomAxisType.DISTANCE, |
| 926 |
WacomAxisType.PRESSURE, |
| 927 |
] |
| 928 |
|
| 929 |
if override == "stylus_type": |
| 930 |
assert aliasing.stylus_type == value |
| 931 |
else: |
| 932 |
assert aliasing.stylus_type == WacomStylusType.GENERAL |
| 933 |
|
| 934 |
if override == "eraser_type": |
| 935 |
assert aliasing.eraser_type == value |
| 936 |
else: |
| 937 |
assert aliasing.eraser_type == WacomEraserType.NONE |
| 938 |
|
| 939 |
if override == "has_lens": |
| 940 |
assert aliasing.has_lens is value |
| 941 |
else: |
| 942 |
assert aliasing.has_lens is False |
| 943 |
|
| 944 |
if override == "has_wheel": |
| 945 |
assert aliasing.has_wheel is value |
| 946 |
else: |
| 947 |
assert aliasing.has_wheel is False |
| 948 |
|
| 949 |
|
| 950 |
@pytest.mark.parametrize("alias", ("0x1234:0xaaaa", "0xabcd", "banana")) |
| 951 |
def test_alias_of_invalid(custom_datadir, alias): |
| 952 |
usbid = (0x1234, 0x5678) |
| 953 |
matches = [f"usb|{usbid[0]:04x}|{usbid[1]:04x}"] |
| 954 |
TabletFile(name="XDGTablet", matches=matches, styli=["@happy-aliases"]).write_to( |
| 955 |
custom_datadir / "uniq.tablet" |
| 956 |
) |
| 957 |
|
| 958 |
stylusfile = StylusFile.default() |
| 959 |
stylusfile.entries = [] |
| 960 |
stylusfile.entries.append( |
| 961 |
StylusEntry( |
| 962 |
id="0x1234:0xabcd", |
| 963 |
name="To be aliased", |
| 964 |
group="happy-aliases", |
| 965 |
) |
| 966 |
) |
| 967 |
|
| 968 |
alias = StylusEntry( |
| 969 |
id="0x1234:0xffff", |
| 970 |
name="AliasTester", |
| 971 |
alias_of=alias, |
| 972 |
group="happy-aliases", |
| 973 |
) |
| 974 |
stylusfile.entries.append(alias) |
| 975 |
stylusfile.write_to_dir(custom_datadir) |
| 976 |
|
| 977 |
db = WacomDatabase(path=custom_datadir) |
| 978 |
builder = WacomBuilder.create(usbid=usbid) |
| 979 |
device = db.new_from_builder(builder) |
| 980 |
assert device is not None and device.name == "XDGTablet" |
| 981 |
|
| 982 |
styli = device.get_styli() |
| 983 |
assert len(styli) == 1 |
| 984 |
|
| 985 |
original = styli[0] |
| 986 |
assert original.vendor_id == 0x1234 |
| 987 |
assert original.tool_id == 0xABCD |
| 988 |
assert original.num_buttons == 2 |
| 989 |
assert original.axes == [ |
| 990 |
WacomAxisType.TILT, |
| 991 |
WacomAxisType.DISTANCE, |
| 992 |
WacomAxisType.PRESSURE, |
| 993 |
] |
| 994 |
assert not original.has_lens |
| 995 |
assert not original.has_wheel |
| 996 |
assert original.eraser_type == WacomEraserType.NONE |