hidapi: Explicitly load hidapi/udev implementation

Linux uses udev, other platforms use the cross-platform hidapi
implementation. Remove implicit loading of hidapi in hidapi/__init__.py.
This commit is contained in:
MattHag 2024-09-26 23:27:03 +02:00 committed by Peter F. Patel-Schneider
parent 99fc9c6fcb
commit 46fafa0e68
7 changed files with 43 additions and 58 deletions

View File

@ -1,47 +0,0 @@
## Copyright (C) 2012-2013 Daniel Pavel
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""Generic Human Interface Device API."""
import platform
if platform.system() in ("Darwin", "Windows"):
from hidapi.hidapi_impl import close # noqa: F401
from hidapi.hidapi_impl import enumerate # noqa: F401
from hidapi.hidapi_impl import find_paired_node # noqa: F401
from hidapi.hidapi_impl import find_paired_node_wpid # noqa: F401
from hidapi.hidapi_impl import get_manufacturer # noqa: F401
from hidapi.hidapi_impl import get_product # noqa: F401
from hidapi.hidapi_impl import get_serial # noqa: F401
from hidapi.hidapi_impl import monitor_glib # noqa: F401
from hidapi.hidapi_impl import open # noqa: F401
from hidapi.hidapi_impl import open_path # noqa: F401
from hidapi.hidapi_impl import read # noqa: F401
from hidapi.hidapi_impl import write # noqa: F401
else:
from hidapi.udev_impl import close # noqa: F401
from hidapi.udev_impl import enumerate # noqa: F401
from hidapi.udev_impl import find_paired_node # noqa: F401
from hidapi.udev_impl import find_paired_node_wpid # noqa: F401
from hidapi.udev_impl import get_manufacturer # noqa: F401
from hidapi.udev_impl import get_product # noqa: F401
from hidapi.udev_impl import get_serial # noqa: F401
from hidapi.udev_impl import monitor_glib # noqa: F401
from hidapi.udev_impl import open # noqa: F401
from hidapi.udev_impl import open_path # noqa: F401
from hidapi.udev_impl import read # noqa: F401
from hidapi.udev_impl import write # noqa: F401
__version__ = "0.9"

View File

@ -17,6 +17,7 @@
import argparse
import os
import os.path
import platform
import readline
import sys
import time
@ -27,7 +28,10 @@ from select import select
from threading import Lock
from threading import Thread
import hidapi
if platform.system() == "Linux":
import hidapi.udev_impl as hidapi
else:
import hidapi.hidapi_impl as hidapi
LOGITECH_VENDOR_ID = 0x046D

View File

@ -21,6 +21,7 @@ from __future__ import annotations
import dataclasses
import logging
import platform
import struct
import threading
import typing
@ -31,7 +32,6 @@ from time import time
from typing import Any
import gi
import hidapi
from . import base_usb
from . import common
@ -47,6 +47,11 @@ if typing.TYPE_CHECKING:
gi.require_version("Gdk", "3.0")
from gi.repository import GLib # NOQA: E402
if platform.system() == "Linux":
import hidapi.udev_impl as hidapi
else:
import hidapi.hidapi_impl as hidapi
logger = logging.getLogger(__name__)
_hidpp20 = hidpp20.Hidpp20()

View File

@ -16,6 +16,7 @@
import argparse
import logging
import platform
import sys
from importlib import import_module
@ -28,6 +29,11 @@ from logitech_receiver import receiver
from solaar import NAME
if platform.system() == "Linux":
import hidapi.udev_impl as hidapi
else:
import hidapi.hidapi_impl as hidapi
logger = logging.getLogger(__name__)
@ -106,7 +112,7 @@ def _receivers(dev_path=None):
if dev_path is not None and dev_path != dev_info.path:
continue
try:
r = receiver.ReceiverFactory.create_receiver(dev_info)
r = receiver.ReceiverFactory.create_receiver(hidapi.find_paired_node_wpid, dev_info)
if logger.isEnabledFor(logging.DEBUG):
logger.debug("[%s] => %s", dev_info.path, r)
if r:
@ -122,9 +128,9 @@ def _receivers_and_devices(dev_path=None):
continue
try:
if dev_info.isDevice:
d = device.DeviceFactory.create_device(base, dev_info)
d = device.DeviceFactory.create_device(hidapi.find_paired_node, base, dev_info)
else:
d = receiver.ReceiverFactory.create_receiver(dev_info)
d = receiver.ReceiverFactory.create_receiver(hidapi.find_paired_node_wpid, dev_info)
if logger.isEnabledFor(logging.DEBUG):
logger.debug("[%s] => %s", dev_info.path, d)

View File

@ -17,6 +17,7 @@
import errno
import logging
import platform
import subprocess
import time
@ -24,7 +25,6 @@ from collections import namedtuple
from functools import partial
import gi
import hidapi
import logitech_receiver
from logitech_receiver import base
@ -40,6 +40,11 @@ from . import i18n
gi.require_version("Gtk", "3.0") # NOQA: E402
from gi.repository import GLib # NOQA: E402 # isort:skip
if platform.system() == "Linux":
import hidapi.udev_impl as hidapi
else:
import hidapi.hidapi_impl as hidapi
logger = logging.getLogger(__name__)
_GHOST_DEVICE = namedtuple("_GHOST_DEVICE", ("receiver", "number", "name", "kind", "online"))

View File

@ -1,6 +1,11 @@
import platform
from unittest import mock
import hidapi
if platform.system() == "Linux":
import hidapi.udev_impl as hidapi
else:
import hidapi.hidapi_impl as hidapi
def test_find_paired_node():

View File

@ -13,6 +13,11 @@ from logitech_receiver import receiver
from . import fake_hidpp
if platform.system() == "Linux":
import hidapi.udev_impl as hidapi
else:
import hidapi.hidapi_impl as hidapi
@pytest.mark.parametrize(
"index, expected_kind",
@ -113,7 +118,7 @@ c534_info = {"kind": common.NamedInt(0, "unknown"), "polling": "", "power_switch
(DeviceInfo("14"), responses_lacking, 0x14, None, 1),
],
)
def test_ReceiverFactory_create_receiver(device_info, responses, handle, serial, max_devices, mock_base):
def test_receiver_factory_create_receiver(device_info, responses, handle, serial, max_devices, mock_base):
mock_base[0].side_effect = fake_hidpp.open_path
mock_base[1].side_effect = partial(fake_hidpp.request, responses)
find_paired_node_wpid_func = hidapi.find_paired_node_wpid
@ -140,7 +145,9 @@ def test_ReceiverFactory_create_receiver(device_info, responses, handle, serial,
(DeviceInfo("13", product_id=0xCCCC), responses_unusual, None, None, -1, c534_info, 3),
],
)
def test_ReceiverFactory_props(device_info, responses, firmware, codename, remaining_pairings, pairing_info, count, mock_base):
def test_receiver_factory_props(
device_info, responses, firmware, codename, remaining_pairings, pairing_info, count, mock_base
):
mock_base[0].side_effect = fake_hidpp.open_path
mock_base[1].side_effect = partial(fake_hidpp.request, responses)
@ -162,7 +169,7 @@ def test_ReceiverFactory_props(device_info, responses, firmware, codename, remai
(DeviceInfo("13", product_id=0xCCCC), responses_unusual, "No paired devices.", "<Receiver(13,19)>"),
],
)
def test_ReceiverFactory_string(device_info, responses, status_str, strng, mock_base):
def test_receiver_factory_string(device_info, responses, status_str, strng, mock_base):
mock_base[0].side_effect = fake_hidpp.open_path
mock_base[1].side_effect = partial(fake_hidpp.request, responses)
@ -180,7 +187,7 @@ def test_ReceiverFactory_string(device_info, responses, status_str, strng, mock_
(DeviceInfo("14", product_id="C534"), responses_lacking),
],
)
def test_ReceiverFactory_nodevice(device_info, responses, mock_base):
def test_receiver_factory_nodevice(device_info, responses, mock_base):
mock_base[0].side_effect = fake_hidpp.open_path
mock_base[1].side_effect = partial(fake_hidpp.request, responses)