Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Dial status LEDs #805

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion libwacom/libwacom-database.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,9 @@ static const struct {
{ "Ring", WACOM_STATUS_LED_RING },
{ "Ring2", WACOM_STATUS_LED_RING2 },
{ "Touchstrip", WACOM_STATUS_LED_TOUCHSTRIP },
{ "Touchstrip2", WACOM_STATUS_LED_TOUCHSTRIP2 }
{ "Touchstrip2", WACOM_STATUS_LED_TOUCHSTRIP2 },
{ "Dial", WACOM_STATUS_LED_DIAL },
{ "Dial2", WACOM_STATUS_LED_DIAL2 },
};

static const struct {
Expand Down
6 changes: 4 additions & 2 deletions libwacom/libwacom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,9 @@ static const struct {
{ WACOM_BUTTON_RING_MODESWITCH, WACOM_STATUS_LED_RING },
{ WACOM_BUTTON_RING2_MODESWITCH, WACOM_STATUS_LED_RING2 },
{ WACOM_BUTTON_TOUCHSTRIP_MODESWITCH, WACOM_STATUS_LED_TOUCHSTRIP },
{ WACOM_BUTTON_TOUCHSTRIP2_MODESWITCH, WACOM_STATUS_LED_TOUCHSTRIP2 }
{ WACOM_BUTTON_TOUCHSTRIP2_MODESWITCH, WACOM_STATUS_LED_TOUCHSTRIP2 },
{ WACOM_BUTTON_DIAL_MODESWITCH, WACOM_STATUS_LED_DIAL },
{ WACOM_BUTTON_DIAL2_MODESWITCH, WACOM_STATUS_LED_DIAL2 },
};

LIBWACOM_EXPORT int
Expand All @@ -1526,7 +1528,7 @@ libwacom_get_button_led_group (const WacomDevice *device, char button)
}
}

return WACOM_STATUS_LED_UNAVAILABLE;
return -1;
}

LIBWACOM_EXPORT int
Expand Down
4 changes: 3 additions & 1 deletion libwacom/libwacom.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ typedef enum {
WACOM_STATUS_LED_RING = 0,
WACOM_STATUS_LED_RING2 = 1,
WACOM_STATUS_LED_TOUCHSTRIP = 2,
WACOM_STATUS_LED_TOUCHSTRIP2 = 3
WACOM_STATUS_LED_TOUCHSTRIP2 = 3,
WACOM_STATUS_LED_DIAL = 4,
WACOM_STATUS_LED_DIAL2 = 5,
} WacomStatusLEDs;

/**
Expand Down
24 changes: 23 additions & 1 deletion test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def instance(cls):
_Api(
name="libwacom_get_status_leds",
args=(c_void_p, c_void_p),
return_type=c_void_p,
return_type=ctypes.POINTER(ctypes.c_int),
),
_Api(
name="libwacom_get_button_led_group",
Expand Down Expand Up @@ -394,6 +394,8 @@ def instance(cls):
_Enum(name="WACOM_STATUS_LED_RING2", value=2),
_Enum(name="WACOM_STATUS_LED_TOUCHSTRIP", value=3),
_Enum(name="WACOM_STATUS_LED_TOUCHSTRIP2", value=4),
_Enum(name="WACOM_STATUS_LED_DIAL", value=1),
_Enum(name="WACOM_STATUS_LED_DIAL2", value=2),
]


Expand Down Expand Up @@ -616,6 +618,16 @@ def get_paired_styli(self) -> List["WacomStylus"]:
return styli


class WacomStatusLed(enum.IntEnum):
UNAVAILABLE = -1
RING = 0
RING2 = 1
TOUCHSTRIP = 2
TOUCHSTRIP2 = 3
DIAL = 4
DIAL2 = 5


class WacomDevice:
"""
Convenience wrapper to make using libwacom a bit more pythonic.
Expand Down Expand Up @@ -795,6 +807,16 @@ def button_flags(self, button: str) -> List[ButtonFlags]:
def button_evdev_code(self, button: str) -> int:
return self.get_button_evdev_code(button.encode("utf-8"))

def button_led_group(self, button: str) -> List[ButtonFlags]:
return self.get_button_led_group(button.encode("utf-8"))

@property
def status_leds(self) -> List["WacomStatusLed"]:
nleds = c_int()
leds = self.get_status_leds(ctypes.byref(nleds))

return [WacomStatusLed(l) for l in leds[: nleds.value]]


class WacomDatabase:
"""
Expand Down
160 changes: 156 additions & 4 deletions test/test_libwacom.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
import logging
import pytest

from . import WacomBuilder, WacomBustype, WacomDatabase, WacomDevice, WacomEraserType
from . import (
WacomBuilder,
WacomBustype,
WacomDatabase,
WacomDevice,
WacomEraserType,
WacomStatusLed,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -89,11 +96,15 @@ class TabletFile:
has_stylus: bool = True
is_reversible: bool = False
styli: list[str] = field(default_factory=list)
# extra additions to the tablet file, e.g. { "Buttons": { "Left" : "A;B;" }
extra: dict[str, dict[str, str]] = field(default_factory=dict)

def write_to(self, filename):
config = ConfigParser()
config.optionxform = lambda option: option
config["Device"] = {

cfg = {}
cfg["Device"] = {
"Name": self.name,
"DeviceMatch": ";".join(self.matches),
"Width": self.width,
Expand All @@ -103,12 +114,18 @@ def write_to(self, filename):
"Layout": self.layout,
}
if self.styli:
config["Device"]["Styli"] = ";".join(self.styli)
cfg["Device"]["Styli"] = ";".join(self.styli)

config["Features"] = {
cfg["Features"] = {
"Stylus": self.has_stylus,
"Reversible": self.is_reversible,
}

cfg = cfg | self.extra

for k, v in cfg.items():
config[k] = v

with open(filename, "w") as fd:
config.write(fd, space_around_delimiters=False)

Expand Down Expand Up @@ -529,6 +546,141 @@ def test_new_from_path_unknown_device(db, fallback):
assert dev.product_id == 0


@pytest.mark.parametrize(
"feature",
("Ring", "Touchstrip", "Dial"),
)
@pytest.mark.parametrize("count", (1, 2))
def test_button_modeswitch(custom_datadir, feature, count):
USBID = (0x1234, 0x5678)

# sigh, Touchstrip but StripsNumModes...
num_mode_key = f"{feature}s" if feature != "Touchstrip" else "Strip"

extra = {
"Buttons": {
"Left": "A;B;C;",
"Right": "D;E;F;",
feature: "A",
},
"Features": {
num_mode_key: 4,
},
}
if count > 1:
extra["Buttons"][f"{feature}2"] = "D"

TabletFile(
name="some tablet",
matches=[f"usb|{USBID[0]:04x}|{USBID[1]:04x}"],
extra=extra,
).write_to(custom_datadir / "led.tablet")

db = WacomDatabase(path=custom_datadir)
builder = WacomBuilder.create(usbid=USBID)
device = db.new_from_builder(builder)
assert device is not None

expected_flag = {
"Ring": WacomDevice.ButtonFlags.RING_MODESWITCH,
"Touchstrip": WacomDevice.ButtonFlags.TOUCHSTRIP_MODESWITCH,
"Dial": WacomDevice.ButtonFlags.DIAL_MODESWITCH,
}[feature]

flags = device.button_flags("A")
assert expected_flag in flags

for b in "BCDEF":
flags = device.button_flags(b)
assert expected_flag not in flags

expected_flag = {
"Ring": WacomDevice.ButtonFlags.RING2_MODESWITCH,
"Touchstrip": WacomDevice.ButtonFlags.TOUCHSTRIP2_MODESWITCH,
"Dial": WacomDevice.ButtonFlags.DIAL2_MODESWITCH,
}[feature]

flags = device.button_flags("D")
if count > 1:
assert expected_flag in flags
else:
assert expected_flag not in flags

for b in "ABCEF":
flags = device.button_flags(b)
assert expected_flag not in flags


@pytest.mark.parametrize(
"feature",
("Ring", "Touchstrip", "Dial"),
)
@pytest.mark.parametrize("count", (1, 2))
def test_status_leds(custom_datadir, feature, count):
USBID = (0x1234, 0x5678)

# sigh, Touchstrip but StripsNumModes...
num_mode_key = f"{feature}s" if feature != "Touchstrip" else "Strip"

extra = {
"Buttons": {
"Left": "A;B;C;",
"Right": "D;E;F;",
feature: "A",
},
"Features": {
"StatusLEDs": f"{feature};{feature}2" if count > 1 else f"{feature}",
num_mode_key: 4,
},
}
if count > 1:
extra["Buttons"][f"{feature}2"] = "D"

TabletFile(
name="some tablet",
matches=[f"usb|{USBID[0]:04x}|{USBID[1]:04x}"],
extra=extra,
).write_to(custom_datadir / "led.tablet")

db = WacomDatabase(path=custom_datadir)
builder = WacomBuilder.create(usbid=USBID)
device = db.new_from_builder(builder)
assert device is not None

expected = [
{
"Ring": WacomStatusLed.RING,
"Touchstrip": WacomStatusLed.TOUCHSTRIP,
"Dial": WacomStatusLed.DIAL,
}[feature]
]

if count > 1:
expected.append(
{
"Ring": WacomStatusLed.RING2,
"Touchstrip": WacomStatusLed.TOUCHSTRIP2,
"Dial": WacomStatusLed.DIAL2,
}[feature]
)

leds = device.status_leds
assert sorted(leds) == sorted(expected)

led_group = device.button_led_group("A")
assert led_group == 0

led_group = device.button_led_group("D")
if count > 1:
assert led_group == 1
else:
assert led_group == -1

for b in "BCEF":
led_group = device.button_led_group(b)
assert led_group == -1


def test_nonwacom_stylus_ids(tmp_path):
styli = StylusFile.default()
s1 = StylusEntry(
Expand Down
2 changes: 2 additions & 0 deletions tools/debug-device.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ handle_device(WacomDeviceDatabase *db, const char *path)
case WACOM_STATUS_LED_RING2: ledstr = "RING2"; break;
case WACOM_STATUS_LED_TOUCHSTRIP: ledstr = "TOUCHSTRIP"; break;
case WACOM_STATUS_LED_TOUCHSTRIP2: ledstr = "TOUCHSTRIP2"; break;
case WACOM_STATUS_LED_DIAL: ledstr = "DIAL"; break;
case WACOM_STATUS_LED_DIAL2: ledstr = "DIAL2"; break;
}
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s%s", i > 0 ? ", " : "", ledstr);
}
Expand Down