receiver: fix crash when turning notification flags into strings

This commit is contained in:
Peter F. Patel-Schneider
2025-12-11 15:17:47 -05:00
parent 93e90f4894
commit 8298db0891
3 changed files with 14 additions and 26 deletions

View File

@@ -89,23 +89,9 @@ class NotificationFlag(Flag):
"""
@classmethod
def flag_names(cls, flag_bits: int) -> List[str]:
def flag_names(cls, flags) -> List[str]:
"""Extract the names of the flags from the integer."""
indexed = {item.value: item.name for item in cls}
flag_names = []
unknown_bits = flag_bits
for k in indexed:
# Ensure that the key (flag value) is a power of 2 (a single bit flag)
assert bin(k).count("1") == 1
if k & flag_bits == k:
unknown_bits &= ~k
flag_names.append(indexed[k].replace("_", " ").lower())
# Yield any remaining unknown bits
if unknown_bits != 0:
flag_names.append(f"unknown:{unknown_bits:06X}")
return flag_names
return flags.name.replace("_", " ").lower().split("|")
NUMPAD_NUMERICAL_KEYS = 0x800000
F_LOCK_STATUS = 0x400000
@@ -125,13 +111,13 @@ class NotificationFlag(Flag):
THREED_GESTURE = 0x000001
def flags_to_str(flag_bits: int | None, fallback: str) -> str:
def flags_to_str(flags, fallback: str) -> str:
flag_names = []
if flag_bits is not None:
if flag_bits == 0:
if flags is not None:
if flags.value == 0:
flag_names = (fallback,)
else:
flag_names = NotificationFlag.flag_names(flag_bits)
flag_names = NotificationFlag.flag_names(flags)
return f"\n{' ':15}".join(sorted(flag_names))

View File

@@ -77,7 +77,7 @@ class SolaarListener(listener.EventsListener):
def has_started(self):
logger.info("%s: notifications listener has started (%s)", self.receiver, self.receiver.handle)
nfs = self.receiver.enable_connection_notifications()
if not self.receiver.isDevice and not ((nfs if nfs else 0) & hidpp10_constants.NotificationFlag.WIRELESS.value):
if not self.receiver.isDevice and (not nfs or not (nfs & hidpp10_constants.NotificationFlag.WIRELESS)):
logger.warning(
"Receiver on %s might not support connection notifications, GUI might not show its devices",
self.receiver.path,

View File

@@ -279,17 +279,19 @@ def test_set_notification_flags_bad(mocker):
"flag_bits, expected_names",
[
(None, ""),
(0x0, "none"),
(0x009020, "multi touch\n unknown:008020"),
(0x080000, "mouse extra buttons"),
(hidpp10_constants.NotificationFlag(0x0), "none"),
(hidpp10_constants.NotificationFlag(0x001000), "multi touch"),
(hidpp10_constants.NotificationFlag(0x080000), "mouse extra buttons"),
(
0x080000 + 0x000400,
hidpp10_constants.NotificationFlag(0x080400),
("link quality\n mouse extra buttons"),
),
],
)
def test_notification_flag_str(flag_bits, expected_names):
flag_names = hidpp10_constants.flags_to_str(flag_bits, fallback="none")
flag_names = hidpp10_constants.flags_to_str(
hidpp10_constants.NotificationFlag(flag_bits) if flag_bits is not None else None, fallback="none"
)
assert flag_names == expected_names