forked from auracaster/bumble_mirror
Load libusb-1.0 shared library from libusb_package wheel
It would be nice to pip install bumble without having to first install the libusb system dependency. Expecially on platforms like Windows and Mac, without a default package manager. The libusb_package Python package distributes prebuilt libusb-1.0 shared libraries for each OS and architecture as binary wheels for the pyusb project. Add this package as a dependency for bumble. For the pyusb transport, the libusb_package.find() function is a drop-in replacement for pyusb.core.find(). It searches the libusb_package site-path before system paths and creates a pyusb backend. For the usb transport, use libusb_package.get_library_path() to return a path to the libusb-1.0 library in site-packages. If this path exists, create a ctypes DLL and init the usb1 backend. This only needs to be done once. All future calls to usb1 will use this opened library. If the library path does not exist, do nothing, and usb1 will search default system paths when the usb1.USBContext object is created. This commit pins the libusb_package dependency at 1.0.26.0 to ensure every bumble install uses the exact same version of the libusb library.
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
# -----------------------------------------------------------------------------
|
||||
import asyncio
|
||||
import logging
|
||||
import libusb_package
|
||||
import usb.core
|
||||
import usb.util
|
||||
import threading
|
||||
@@ -203,13 +204,13 @@ async def open_pyusb_transport(spec):
|
||||
# Find the device according to the spec moniker
|
||||
if ':' in spec:
|
||||
vendor_id, product_id = spec.split(':')
|
||||
device = usb.core.find(
|
||||
device = libusb_package.find(
|
||||
idVendor=int(vendor_id, 16), idProduct=int(product_id, 16)
|
||||
)
|
||||
else:
|
||||
device_index = int(spec)
|
||||
devices = list(
|
||||
usb.core.find(
|
||||
libusb_package.find(
|
||||
find_all=1,
|
||||
bDeviceClass=USB_DEVICE_CLASS_WIRELESS_CONTROLLER,
|
||||
bDeviceSubClass=USB_DEVICE_SUBCLASS_RF_CONTROLLER,
|
||||
|
||||
@@ -17,9 +17,12 @@
|
||||
# -----------------------------------------------------------------------------
|
||||
import asyncio
|
||||
import logging
|
||||
import libusb_package
|
||||
import usb1
|
||||
import threading
|
||||
import collections
|
||||
import ctypes
|
||||
import platform
|
||||
from colors import color
|
||||
|
||||
from .common import Transport, ParserSource
|
||||
@@ -33,6 +36,19 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def load_libusb():
|
||||
'''
|
||||
Attempt to load the libusb-1.0 C library from libusb_package in site-packages.
|
||||
If library exists, we create a DLL object and initialize the usb1 backend.
|
||||
This only needs to be done once, but bufore a usb1.USBContext is created.
|
||||
If library does not exists, do nothing and usb1 will search default system paths
|
||||
when usb1.USBContext is created.
|
||||
'''
|
||||
if libusb_path := libusb_package.get_library_path():
|
||||
dll_loader = ctypes.WinDLL if platform.system() == 'Windows' else ctypes.CDLL
|
||||
libusb_dll = dll_loader(libusb_path, use_errno=True, use_last_error=True)
|
||||
usb1.loadLibrary(libusb_dll)
|
||||
|
||||
async def open_usb_transport(spec):
|
||||
'''
|
||||
Open a USB transport.
|
||||
@@ -305,6 +321,7 @@ async def open_usb_transport(spec):
|
||||
self.context.close()
|
||||
|
||||
# Find the device according to the spec moniker
|
||||
load_libusb()
|
||||
context = usb1.USBContext()
|
||||
context.open()
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user