Skip to content

Commit

Permalink
Refactor: Remove all GDK dependencies from hidapi package
Browse files Browse the repository at this point in the history
The hidapi hardware layer must not know or depend on any UI libraries.
Removes all GDK dependencies from the hidapi packages, which makes
testing of these modules easier and removes unwanted cross-dependencies.

Related pwr-Solaar#2480
  • Loading branch information
MattHag committed Sep 15, 2024
1 parent 94deef8 commit 67822dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
20 changes: 16 additions & 4 deletions lib/hidapi/hidapi_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
which is MIT licensed.
"""

from __future__ import annotations

import atexit
import ctypes
import logging
import platform
import typing

from threading import Thread
from time import sleep
Expand All @@ -35,8 +38,9 @@

from hidapi.common import DeviceInfo

gi.require_version("Gdk", "3.0")
from gi.repository import GLib # NOQA: E402
if typing.TYPE_CHECKING:
gi.require_version("Gdk", "3.0")
from gi.repository import GLib # NOQA: E402

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -311,13 +315,21 @@ def find_paired_node_wpid(receiver_path, index):
return None


def monitor_glib(callback, filterfn):
def monitor_glib(glib: GLib, callback, filterfn):
"""Monitor GLib.
Parameters
----------
glib
GLib instance.
"""

def device_callback(action, device):
# print(f"device_callback({action}): {device}")
if action == "add":
d_info = _match(action, device, filterfn)
if d_info:
GLib.idle_add(callback, action, d_info)
glib.idle_add(callback, action, d_info)
elif action == "remove":
# Removed devices will be detected by Solaar directly
pass
Expand Down
27 changes: 19 additions & 8 deletions lib/hidapi/udev_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
necessary.
"""

from __future__ import annotations

import errno
import logging
import os
import typing
import warnings


Expand All @@ -39,8 +42,9 @@

from hidapi.common import DeviceInfo

gi.require_version("Gdk", "3.0")
from gi.repository import GLib # NOQA: E402
if typing.TYPE_CHECKING:
gi.require_version("Gdk", "3.0")
from gi.repository import GLib # NOQA: E402

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -213,35 +217,42 @@ def find_paired_node_wpid(receiver_path, index):
return None


def monitor_glib(callback, filterfn):
def monitor_glib(glib: GLib, callback, filterfn):
"""Monitor GLib.
Parameters
----------
glib
GLib instance.
"""
c = pyudev.Context()
m = pyudev.Monitor.from_netlink(c)
m.filter_by(subsystem="hidraw")

def _process_udev_event(monitor, condition, cb, filterfn):
if condition == GLib.IO_IN:
if condition == glib.IO_IN:
event = monitor.receive_device()
if event:
action, device = event
# print ("***", action, device)
if action == "add":
d_info = _match(action, device, filterfn)
if d_info:
GLib.idle_add(cb, action, d_info)
glib.idle_add(cb, action, d_info)
elif action == "remove":
# the GLib notification does _not_ match!
pass
return True

try:
# io_add_watch_full may not be available...
GLib.io_add_watch_full(m, GLib.PRIORITY_LOW, GLib.IO_IN, _process_udev_event, callback, filterfn)
glib.io_add_watch_full(m, glib.PRIORITY_LOW, glib.IO_IN, _process_udev_event, callback, filterfn)
except AttributeError:
try:
# and the priority parameter appeared later in the API
GLib.io_add_watch(m, GLib.PRIORITY_LOW, GLib.IO_IN, _process_udev_event, callback, filterfn)
glib.io_add_watch(m, glib.PRIORITY_LOW, glib.IO_IN, _process_udev_event, callback, filterfn)
except Exception:
GLib.io_add_watch(m, GLib.IO_IN, _process_udev_event, callback, filterfn)
glib.io_add_watch(m, glib.IO_IN, _process_udev_event, callback, filterfn)

if logger.isEnabledFor(logging.DEBUG):
logger.debug("Starting dbus monitoring")
Expand Down
6 changes: 5 additions & 1 deletion lib/logitech_receiver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from time import time
from typing import Any

import gi
import hidapi

from . import base_usb
Expand All @@ -41,6 +42,9 @@
from .common import LOGITECH_VENDOR_ID
from .common import BusID

gi.require_version("Gdk", "3.0")
from gi.repository import GLib # NOQA: E402

logger = logging.getLogger(__name__)

_hidpp20 = hidpp20.Hidpp20()
Expand Down Expand Up @@ -176,7 +180,7 @@ def receivers_and_devices():

def notify_on_receivers_glib(callback):
"""Watch for matching devices and notifies the callback on the GLib thread."""
return hidapi.monitor_glib(callback, filter)
return hidapi.monitor_glib(GLib, callback, filter)


def open_path(path):
Expand Down

0 comments on commit 67822dd

Please sign in to comment.