Skip to content

Commit

Permalink
Improve base module
Browse files Browse the repository at this point in the history
Use clearer names and type hints.
  • Loading branch information
MattHag committed May 28, 2024
1 parent e9f129c commit 378d6c8
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/logitech_receiver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from contextlib import contextmanager
from random import getrandbits
from time import time
from typing import Any

import hidapi

Expand Down Expand Up @@ -56,34 +57,41 @@ def __str__(self):
return f"Notification({self.report_id:02x},{self.devnumber},{self.sub_id:02X},{self.address:02X},{text_as_hex})"


def _wired_device(product_id, interface):
return {"vendor_id": 1133, "product_id": product_id, "bus_id": 3, "usb_interface": interface, "isDevice": True}
def _usb_device(product_id: int, usb_interface: int) -> dict[str, Any]:
return {
"vendor_id": 1133,
"product_id": product_id,
"bus_id": 3,
"usb_interface": usb_interface,
"isDevice": True,
}


def _bt_device(product_id):
def _bluetooth_device(product_id: int) -> dict[str, Any]:
return {"vendor_id": 1133, "product_id": product_id, "bus_id": 5, "isDevice": True}


DEVICE_IDS = []
KNOWN_DEVICE_IDS = []

for _ignore, d in descriptors.DEVICES.items():
if d.usbid:
DEVICE_IDS.append(_wired_device(d.usbid, d.interface if d.interface else 2))
usb_interface = d.interface if d.interface else 2
KNOWN_DEVICE_IDS.append(_usb_device(d.usbid, usb_interface))
if d.btid:
DEVICE_IDS.append(_bt_device(d.btid))
KNOWN_DEVICE_IDS.append(_bluetooth_device(d.btid))


def other_device_check(bus_id, vendor_id, product_id):
def other_device_check(bus_id: int, vendor_id: int, product_id: int):
"""Check whether product is a Logitech USB-connected or Bluetooth device based on bus, vendor, and product IDs
This allows Solaar to support receiverless HID++ 2.0 devices that it knows nothing about"""
if vendor_id != 0x46D: # Logitech
return
if bus_id == 0x3: # USB
if product_id >= 0xC07D and product_id <= 0xC094 or product_id >= 0xC32B and product_id <= 0xC344:
return _wired_device(product_id, 2)
return _usb_device(product_id, 2)
elif bus_id == 0x5: # Bluetooth
if product_id >= 0xB012 and product_id <= 0xB0FF or product_id >= 0xB317 and product_id <= 0xB3FF:
return _bt_device(product_id)
return _bluetooth_device(product_id)


def product_information(usb_id: int | str) -> dict:
Expand Down Expand Up @@ -157,7 +165,7 @@ def filter(bus_id, vendor_id, product_id, hidpp_short=False, hidpp_long=False):
record = filter_receivers(bus_id, vendor_id, product_id, hidpp_short, hidpp_long)
if record: # known or unknown receiver
return record
for record in DEVICE_IDS: # known devices
for record in KNOWN_DEVICE_IDS:
if match(record, bus_id, vendor_id, product_id):
return record
if hidpp_short or hidpp_long: # unknown devices that use HID++
Expand Down Expand Up @@ -358,7 +366,7 @@ def _skip_incoming(handle, ihandle, notifications_hook):

def make_notification(report_id, devnumber, data) -> HIDPPNotification | None:
"""Guess if this is a notification (and not just a request reply), and
return a Notification tuple if it is."""
return a Notification if it is."""

sub_id = ord(data[:1])
if sub_id & 0x80 == 0x80:
Expand Down

0 comments on commit 378d6c8

Please sign in to comment.