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

hidpp10_constants.py: Remove dependency to NamedInts #25

Open
wants to merge 29 commits into
base: refactor_settings
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7e62bb0
Split up huge settings module
MattHag Nov 3, 2024
5959d6a
Refactor: Convert Kind to IntEnum
MattHag Nov 3, 2024
22fdae2
type hints: Introduce settings protocol
MattHag Nov 3, 2024
c633bda
Refactor: Remove diversion alias
MattHag Nov 3, 2024
49aa35e
Simplify settings UI class
MattHag Nov 3, 2024
6b44119
Enforce rules on RuleComponentUI subclasses
MattHag Nov 3, 2024
8f1d25f
settings: Add docstrings and type hint
MattHag Nov 3, 2024
5085084
Remove NamedInts: Convert Column to enum
MattHag Nov 4, 2024
ec588a5
Remove NamedInts: Convert Task to enum
MattHag Nov 4, 2024
649bcde
Add type hints
MattHag Nov 4, 2024
fd1aa2f
key flags: Move to module of use
MattHag Nov 4, 2024
f1c3e7c
Add type hints
MattHag Nov 4, 2024
5be1e10
Remove NamedInts: Convert KeyFlag to Flag
MattHag Nov 5, 2024
e04d17e
Remove NamedInts: Convert Spec to enum
MattHag Nov 5, 2024
e92e13f
Remove NamedInts: Convert ActionId to enum
MattHag Nov 5, 2024
61ef9b5
mapping flag: Move to module of use
MattHag Nov 5, 2024
cdf4b80
Remove NamedInts: Convert MappingFlag to flag
MattHag Nov 5, 2024
04f7957
Remove NamedInts: Convert PowerSwitchLocation to flag
MattHag Nov 5, 2024
32e0158
Remove NamedInts: Convert HorizontalScroll to enum
MattHag Nov 5, 2024
cf46f80
Remove NamedInts: Convert LedRampChoice to flag
MattHag Nov 5, 2024
c5a7de5
charge status: Refactor to enum and move to module of use
MattHag Nov 5, 2024
5300877
Remove NamedInts: Convert LedFormChoices to enum
MattHag Nov 5, 2024
65abb53
Fix KeyFlag conversion
MattHag Nov 5, 2024
c8d3040
Fixes on top of refactoring
MattHag Nov 5, 2024
c728e1d
Prepare refactoring of NotificationFlag
MattHag Nov 16, 2024
8eee66e
Remove NamedInts: Convert NotificationFlag to flag
MattHag Nov 16, 2024
5b48ffd
Remove NamedInts: Convert DeviceFeature to flag
MattHag Nov 16, 2024
5548897
refactor(logitech_receiver/hidpp10_constants.py): use InfoSubRegister…
Nov 20, 2024
d79346f
refactor(logitech_receiver/hidpp10_constants.py): remove dependency t…
Nov 21, 2024
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
6 changes: 3 additions & 3 deletions lib/logitech_receiver/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import dataclasses
import typing

from enum import Flag
from enum import IntEnum
from typing import Generator
from typing import Iterable
Expand Down Expand Up @@ -589,7 +590,7 @@ class FirmwareInfo:
extras: str | None


class BatteryStatus(IntEnum):
class BatteryStatus(Flag):
DISCHARGING = 0x00
RECHARGING = 0x01
ALMOST_FULL = 0x02
Expand Down Expand Up @@ -649,8 +650,7 @@ def to_str(self) -> str:
elif isinstance(self.level, int):
status = self.status.name.lower().replace("_", " ") if self.status is not None else "Unknown"
return _("Battery: %(percent)d%% (%(status)s)") % {"percent": self.level, "status": _(status)}
else:
return ""
return ""


class Alert(IntEnum):
Expand Down
44 changes: 21 additions & 23 deletions lib/logitech_receiver/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
- the name or codename should be different from what the device reports
"""

from .hidpp10_constants import DEVICE_KIND
from .hidpp10_constants import DeviceKind
from .hidpp10_constants import Registers as Reg


Expand Down Expand Up @@ -71,15 +71,15 @@ def _D(
):
if kind is None:
kind = (
DEVICE_KIND.mouse
DeviceKind.MOUSE
if "Mouse" in name
else DEVICE_KIND.keyboard
else DeviceKind.KEYBOARD
if "Keyboard" in name
else DEVICE_KIND.numpad
else DeviceKind.NUMPAD
if "Number Pad" in name
else DEVICE_KIND.touchpad
else DeviceKind.TOUCHPAD
if "Touchpad" in name
else DEVICE_KIND.trackball
else DeviceKind.TRACKBALL
if "Trackball" in name
else None
)
Expand All @@ -92,11 +92,11 @@ def _D(
assert w[0:1] == "4", f"{name} has protocol {protocol:0.1f}, wpid {w}"
else:
if w[0:1] == "1":
assert kind == DEVICE_KIND.mouse, f"{name} has protocol {protocol:0.1f}, wpid {w}"
assert kind == DeviceKind.MOUSE, f"{name} has protocol {protocol:0.1f}, wpid {w}"
elif w[0:1] == "2":
assert kind in (
DEVICE_KIND.keyboard,
DEVICE_KIND.numpad,
DeviceKind.KEYBOARD,
DeviceKind.NUMPAD,
), f"{name} has protocol {protocol:0.1f}, wpid {w}"

device_descriptor = _DeviceDescriptor(
Expand Down Expand Up @@ -254,7 +254,7 @@ def get_btid(btid):
_D(
"VX Revolution",
codename="VX Revolution",
kind=DEVICE_KIND.mouse,
kind=DeviceKind.MOUSE,
protocol=1.0,
wpid=("1006", "100D", "0612"),
registers=(Reg.BATTERY_CHARGE,),
Expand All @@ -263,15 +263,15 @@ def get_btid(btid):
"MX Air",
codename="MX Air",
protocol=1.0,
kind=DEVICE_KIND.mouse,
kind=DeviceKind.MOUSE,
wpid=("1007", "100E"),
registers=Reg.BATTERY_CHARGE,
)
_D(
"MX Revolution",
codename="MX Revolution",
protocol=1.0,
kind=DEVICE_KIND.mouse,
kind=DeviceKind.MOUSE,
wpid=("1008", "100C"),
registers=(Reg.BATTERY_CHARGE,),
)
Expand Down Expand Up @@ -307,7 +307,7 @@ def get_btid(btid):
"MX 1100 Cordless Laser Mouse",
codename="MX 1100",
protocol=1.0,
kind=DEVICE_KIND.mouse,
kind=DeviceKind.MOUSE,
wpid="1014",
registers=(Reg.BATTERY_CHARGE,),
)
Expand Down Expand Up @@ -421,7 +421,7 @@ def get_btid(btid):
_D("MX518 Gaming Mouse", codename="MX518", usbid=0xC08E, interface=1)
_D("G703 Hero Gaming Mouse", codename="G703 Hero", usbid=0xC090)
_D("G903 Hero Gaming Mouse", codename="G903 Hero", usbid=0xC091)
_D(None, kind=DEVICE_KIND.mouse, usbid=0xC092, interface=1) # two mice share this ID
_D(None, kind=DeviceKind.MOUSE, usbid=0xC092, interface=1) # two mice share this ID
_D("M500S Mouse", codename="M500S", usbid=0xC093, interface=1)
# _D('G600 Gaming Mouse', codename='G600 Gaming', usbid=0xc24a, interface=1) # not an HID++ device
_D("G500s Gaming Mouse", codename="G500s Gaming", usbid=0xC24E, interface=1, protocol=1.0)
Expand All @@ -438,29 +438,27 @@ def get_btid(btid):

_D("Wireless Touchpad", codename="Wireless Touch", protocol=2.0, wpid="4011")
_D("Wireless Rechargeable Touchpad T650", codename="T650", protocol=2.0, wpid="4101")
_D(
"G Powerplay", codename="Powerplay", protocol=2.0, kind=DEVICE_KIND.touchpad, wpid="405F"
) # To override self-identification
_D("G Powerplay", codename="Powerplay", protocol=2.0, kind=DeviceKind.TOUCHPAD, wpid="405F") # To override self-identification

# Headset

_D("G533 Gaming Headset", codename="G533 Headset", protocol=2.0, interface=3, kind=DEVICE_KIND.headset, usbid=0x0A66)
_D("G535 Gaming Headset", codename="G535 Headset", protocol=2.0, interface=3, kind=DEVICE_KIND.headset, usbid=0x0AC4)
_D("G935 Gaming Headset", codename="G935 Headset", protocol=2.0, interface=3, kind=DEVICE_KIND.headset, usbid=0x0A87)
_D("G733 Gaming Headset", codename="G733 Headset", protocol=2.0, interface=3, kind=DEVICE_KIND.headset, usbid=0x0AB5)
_D("G533 Gaming Headset", codename="G533 Headset", protocol=2.0, interface=3, kind=DeviceKind.HEADSET, usbid=0x0A66)
_D("G535 Gaming Headset", codename="G535 Headset", protocol=2.0, interface=3, kind=DeviceKind.HEADSET, usbid=0x0AC4)
_D("G935 Gaming Headset", codename="G935 Headset", protocol=2.0, interface=3, kind=DeviceKind.HEADSET, usbid=0x0A87)
_D("G733 Gaming Headset", codename="G733 Headset", protocol=2.0, interface=3, kind=DeviceKind.HEADSET, usbid=0x0AB5)
_D(
"G733 Gaming Headset",
codename="G733 Headset New",
protocol=2.0,
interface=3,
kind=DEVICE_KIND.headset,
kind=DeviceKind.HEADSET,
usbid=0x0AFE,
)
_D(
"PRO X Wireless Gaming Headset",
codename="PRO Headset",
protocol=2.0,
interface=3,
kind=DEVICE_KIND.headset,
kind=DeviceKind.HEADSET,
usbid=0x0ABA,
)
15 changes: 9 additions & 6 deletions lib/logitech_receiver/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from . import settings_templates
from .common import Alert
from .common import Battery
from .hidpp10_constants import NotificationFlag
from .hidpp20_constants import SupportedFeature

if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -468,10 +469,8 @@ def enable_connection_notifications(self, enable=True):

if enable:
set_flag_bits = (
hidpp10_constants.NOTIFICATION_FLAG.battery_status
| hidpp10_constants.NOTIFICATION_FLAG.ui
| hidpp10_constants.NOTIFICATION_FLAG.configuration_complete
)
NotificationFlag.BATTERY_STATUS | NotificationFlag.UI | NotificationFlag.CONFIGURATION_COMPLETE
).value
else:
set_flag_bits = 0
ok = _hidpp10.set_notification_flags(self, set_flag_bits)
Expand All @@ -480,8 +479,12 @@ def enable_connection_notifications(self, enable=True):

flag_bits = _hidpp10.get_notification_flags(self)
if logger.isEnabledFor(logging.INFO):
flag_names = None if flag_bits is None else tuple(hidpp10_constants.NOTIFICATION_FLAG.flag_names(flag_bits))
logger.info("%s: device notifications %s %s", self, "enabled" if enable else "disabled", flag_names)
if flag_bits is None:
flag_names = None
else:
flag_names = hidpp10_constants.NotificationFlag.flag_names(flag_bits)
is_enabled = "enabled" if enable else "disabled"
logger.info(f"{self}: device notifications {is_enabled} {flag_names}")
return flag_bits if ok else None

def add_notification_handler(self, id: str, fn):
Expand Down
5 changes: 3 additions & 2 deletions lib/logitech_receiver/hidpp10.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .common import BatteryLevelApproximation
from .common import BatteryStatus
from .common import FirmwareKind
from .hidpp10_constants import NotificationFlag
from .hidpp10_constants import Registers

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -190,7 +191,7 @@ def set_3leds(self, device: Device, battery_level=None, charging=None, warning=N
def get_notification_flags(self, device: Device):
return self._get_register(device, Registers.NOTIFICATIONS)

def set_notification_flags(self, device: Device, *flag_bits):
def set_notification_flags(self, device: Device, *flag_bits: NotificationFlag):
assert device is not None

# Avoid a call if the device is not online,
Expand All @@ -200,7 +201,7 @@ def set_notification_flags(self, device: Device, *flag_bits):
if device.protocol and device.protocol >= 2.0:
return

flag_bits = sum(int(b) for b in flag_bits)
flag_bits = sum(int(b.value) for b in flag_bits)
assert flag_bits & 0x00FFFFFF == flag_bits
result = write_register(device, Registers.NOTIFICATIONS, common.int2bytes(flag_bits, 3))
return result is not None
Expand Down
Loading