From d27f7285e0dfe20f1011bb257176d1fd473bbec4 Mon Sep 17 00:00:00 2001 From: MattHag <16444067+MattHag@users.noreply.github.com> Date: Tue, 5 Nov 2024 02:41:21 +0100 Subject: [PATCH] Remove NamedInts: Convert MappingFlag to flag Related #2273 --- lib/logitech_receiver/hidpp20.py | 47 ++++++++++++++++---------------- lib/solaar/cli/show.py | 12 +++++--- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/logitech_receiver/hidpp20.py b/lib/logitech_receiver/hidpp20.py index 796e7909..777b8312 100644 --- a/lib/logitech_receiver/hidpp20.py +++ b/lib/logitech_receiver/hidpp20.py @@ -44,7 +44,6 @@ from .common import BatteryLevelApproximation from .common import BatteryStatus from .common import FirmwareKind from .common import NamedInt -from .common import NamedInts from .hidpp20_constants import CHARGE_STATUS from .hidpp20_constants import DEVICE_KIND from .hidpp20_constants import ChargeLevel @@ -105,15 +104,17 @@ class KeyFlag(Flag): return self.name.replace("_", " ") -# Flags describing the reporting method of a control -# We treat bytes 2 and 5 of `get/setCidReporting` as a single bitfield -MAPPING_FLAG = NamedInts( - analytics_key_events_reporting=0x100, - force_raw_XY_diverted=0x40, - raw_XY_diverted=0x10, - persistently_diverted=0x04, - diverted=0x01, -) +class MappingFlag(Flag): + """Flags describing the reporting method of a control. + + We treat bytes 2 and 5 of `get/setCidReporting` as a single bitfield + """ + + ANALYTICS_KEY_EVENTS_REPORTING = 0x100 + FORCE_RAW_XY_DIVERTED = 0x40 + RAW_XY_DIVERTED = 0x10 + PERSISTENTLY_DIVERTED = 0x04 + DIVERTED = 0x01 class FeaturesArray(dict): @@ -317,21 +318,21 @@ class ReprogrammableKeyV4(ReprogrammableKey): def mapping_flags(self) -> List[str]: if self._mapping_flags is None: self._getCidReporting() - return MAPPING_FLAG.flag_names(self._mapping_flags) + return list(common.flag_names(MappingFlag, self._mapping_flags)) def set_diverted(self, value: bool): """If set, the control is diverted temporarily and reports presses as HID++ events.""" - flags = {MAPPING_FLAG.diverted: value} + flags = {MappingFlag.DIVERTED: value} self._setCidReporting(flags=flags) def set_persistently_diverted(self, value: bool): """If set, the control is diverted permanently and reports presses as HID++ events.""" - flags = {MAPPING_FLAG.persistently_diverted: value} + flags = {MappingFlag.PERSISTENTLY_DIVERTED: value} self._setCidReporting(flags=flags) def set_rawXY_reporting(self, value: bool): """If set, the mouse temporarily reports all its raw XY events while this control is pressed as HID++ events.""" - flags = {MAPPING_FLAG.raw_XY_diverted: value} + flags = {MappingFlag.RAW_XY_DIVERTED: value} self._setCidReporting(flags=flags) def remap(self, to: NamedInt): @@ -383,19 +384,19 @@ class ReprogrammableKeyV4(ReprogrammableKey): """ flags = flags if flags else {} # See flake8 B006 - # if MAPPING_FLAG.raw_XY_diverted in flags and flags[MAPPING_FLAG.raw_XY_diverted]: + # if MappingFlag.RAW_XY_DIVERTED in flags and flags[MappingFlag.RAW_XY_DIVERTED]: # We need diversion to report raw XY, so divert temporarily (since XY reporting is also temporary) - # flags[MAPPING_FLAG.diverted] = True - # if MAPPING_FLAG.diverted in flags and not flags[MAPPING_FLAG.diverted]: - # flags[MAPPING_FLAG.raw_XY_diverted] = False + # flags[MappingFlag.DIVERTED] = True + # if MappingFlag.DIVERTED in flags and not flags[MappingFlag.DIVERTED]: + # flags[MappingFlag.RAW_XY_DIVERTED] = False # The capability required to set a given reporting flag. FLAG_TO_CAPABILITY = { - MAPPING_FLAG.diverted: KeyFlag.DIVERTABLE, - MAPPING_FLAG.persistently_diverted: KeyFlag.PERSISTENTLY_DIVERTABLE, - MAPPING_FLAG.analytics_key_events_reporting: KeyFlag.ANALYTICS_KEY_EVENTS, - MAPPING_FLAG.force_raw_XY_diverted: KeyFlag.FORCE_RAW_XY, - MAPPING_FLAG.raw_XY_diverted: KeyFlag.RAW_XY, + MappingFlag.DIVERTED: KeyFlag.DIVERTABLE, + MappingFlag.PERSISTENTLY_DIVERTED: KeyFlag.PERSISTENTLY_DIVERTABLE, + MappingFlag.ANALYTICS_KEY_EVENTS_REPORTING: KeyFlag.ANALYTICS_KEY_EVENTS, + MappingFlag.FORCE_RAW_XY_DIVERTED: KeyFlag.FORCE_RAW_XY, + MappingFlag.RAW_XY_DIVERTED: KeyFlag.RAW_XY, } bfield = 0 diff --git a/lib/solaar/cli/show.py b/lib/solaar/cli/show.py index f9c2310d..99a91413 100644 --- a/lib/solaar/cli/show.py +++ b/lib/solaar/cli/show.py @@ -55,7 +55,7 @@ def _print_receiver(receiver): notification_flags = _hidpp10.get_notification_flags(receiver) if notification_flags is not None: if notification_flags: - notification_names = hidpp10_constants.NOTIFICATION_FLAG.flag_names(notification_flags) + notification_names = hidpp10_constants.NotificationFlag.flag_names(notification_flags) print(f" Notifications: {', '.join(notification_names)} (0x{notification_flags:06X})") else: print(" Notifications: (none)") @@ -151,8 +151,8 @@ def _print_device(dev, num=None): if isinstance(feature, str): feature_bytes = bytes.fromhex(feature[-4:]) else: - feature_bytes = feature.to_bytes(2) - feature_int = int.from_bytes(feature_bytes) + feature_bytes = feature.to_bytes(2, byteorder="big") + feature_int = int.from_bytes(feature_bytes, byteorder="big") flags = dev.request(0x0000, feature_bytes) flags = 0 if flags is None else ord(flags[1:2]) flags = common.flag_names(hidpp20_constants.FeatureFlag, flags) @@ -278,7 +278,11 @@ def _print_device(dev, num=None): gmask_fmt = ",".join(k.group_mask) gmask_fmt = gmask_fmt if gmask_fmt else "empty" print(f" {', '.join(k.flags)}, pos:{int(k.pos)}, group:{int(k.group):1}, group mask:{gmask_fmt}") - report_fmt = ", ".join(k.mapping_flags) + flag_names = list(common.flag_names(hidpp20.KeyFlag, k.flags.value)) + print( + f" {', '.join(flag_names)}, pos:{int(k.pos)}, group:{int(k.group):1}, group mask:{gmask_fmt}" + ) + report_fmt = list(common.flag_names(hidpp20.MappingFlag, k.mapping_flags.value)) report_fmt = report_fmt if report_fmt else "default" print(f" reporting: {report_fmt}") if dev.online and dev.remap_keys: