use only udev events to detect receiver devices
This commit is contained in:
parent
d3f94ff2fb
commit
c829304e31
|
@ -94,13 +94,29 @@ def monitor(callback, *device_filters):
|
||||||
m = _Monitor.from_netlink(_Context())
|
m = _Monitor.from_netlink(_Context())
|
||||||
m.filter_by(subsystem='hidraw')
|
m.filter_by(subsystem='hidraw')
|
||||||
for action, device in m:
|
for action, device in m:
|
||||||
hid_dev = device.find_parent('hid')
|
if action == 'add':
|
||||||
|
hid_dev = device.find_parent(subsystem='hid')
|
||||||
|
# print ("****", action, device, hid_dev)
|
||||||
if hid_dev:
|
if hid_dev:
|
||||||
for filter in device_filters:
|
for filter in device_filters:
|
||||||
dev_info = _match(device, hid_dev, *filter)
|
d_info = _match(device, hid_dev, *filter)
|
||||||
if dev_info:
|
if d_info:
|
||||||
callback(action, dev_info)
|
callback(action, d_info)
|
||||||
break
|
break
|
||||||
|
elif action == 'remove':
|
||||||
|
# this is ugly, but... well.
|
||||||
|
# signal the callback for each removed device; it will have figure
|
||||||
|
# out for itself if it's a device it should handle
|
||||||
|
d_info = DeviceInfo(path=device.device_node,
|
||||||
|
vendor_id=None,
|
||||||
|
product_id=None,
|
||||||
|
serial=None,
|
||||||
|
release=None,
|
||||||
|
manufacturer=None,
|
||||||
|
product=None,
|
||||||
|
interface=None,
|
||||||
|
driver=None)
|
||||||
|
callback(action, d_info)
|
||||||
|
|
||||||
|
|
||||||
def enumerate(vendor_id=None, product_id=None, interface_number=None, driver=None):
|
def enumerate(vendor_id=None, product_id=None, interface_number=None, driver=None):
|
||||||
|
|
|
@ -74,6 +74,22 @@ def receivers():
|
||||||
# yield d
|
# yield d
|
||||||
|
|
||||||
|
|
||||||
|
def notify_on_receivers(callback):
|
||||||
|
"""Starts a thread that monitors receiver events from udev."""
|
||||||
|
from threading import Thread as _Thread
|
||||||
|
t = _Thread(name='receivers_monitor', target=_hid.monitor,
|
||||||
|
args=(callback,
|
||||||
|
DEVICE_UNIFYING_RECEIVER,
|
||||||
|
DEVICE_UNIFYING_RECEIVER_2,
|
||||||
|
# DEVICE_NANO_RECEIVER,
|
||||||
|
))
|
||||||
|
t.daemon = True
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
# the HID monitor will only send events when devices are added/removed,
|
||||||
|
# so we need to trigger the callback for all currently detected receivers
|
||||||
|
for r in receivers():
|
||||||
|
callback('add', r)
|
||||||
|
|
||||||
|
|
||||||
def open_path(path):
|
def open_path(path):
|
||||||
|
|
|
@ -114,9 +114,6 @@ class EventsListener(_threading.Thread):
|
||||||
def __init__(self, receiver, notifications_callback):
|
def __init__(self, receiver, notifications_callback):
|
||||||
super(EventsListener, self).__init__(name=self.__class__.__name__)
|
super(EventsListener, self).__init__(name=self.__class__.__name__)
|
||||||
|
|
||||||
# replace the handle with a threaded one
|
|
||||||
receiver.handle = _ThreadedHandle(self, receiver.path, receiver.handle)
|
|
||||||
|
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
self._active = False
|
self._active = False
|
||||||
|
|
||||||
|
@ -129,7 +126,9 @@ class EventsListener(_threading.Thread):
|
||||||
def run(self):
|
def run(self):
|
||||||
self._active = True
|
self._active = True
|
||||||
|
|
||||||
|
# replace the handle with a threaded one
|
||||||
ihandle = int(self.receiver.handle)
|
ihandle = int(self.receiver.handle)
|
||||||
|
self.receiver.handle = _ThreadedHandle(self, self.receiver.path, self.receiver.handle)
|
||||||
_log.info("started with %s (%d)", self.receiver, ihandle)
|
_log.info("started with %s (%d)", self.receiver, ihandle)
|
||||||
|
|
||||||
self.has_started()
|
self.has_started()
|
||||||
|
|
|
@ -375,24 +375,16 @@ class Receiver(object):
|
||||||
__bool__ = __nonzero__ = lambda self: self.handle is not None
|
__bool__ = __nonzero__ = lambda self: self.handle is not None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def open(self):
|
def open(self, path):
|
||||||
"""Opens the first Logitech Unifying Receiver found attached to the machine.
|
"""Opens a Logitech Receiver found attached to the machine, by device path.
|
||||||
|
|
||||||
:returns: An open file handle for the found receiver, or ``None``.
|
:returns: An open file handle for the found receiver, or ``None``.
|
||||||
"""
|
"""
|
||||||
exception = None
|
|
||||||
|
|
||||||
for rawdevice in _base.receivers():
|
|
||||||
exception = None
|
|
||||||
try:
|
try:
|
||||||
handle = _base.open_path(rawdevice.path)
|
handle = _base.open_path(path)
|
||||||
if handle:
|
if handle:
|
||||||
return Receiver(handle, rawdevice.path)
|
return Receiver(handle, path)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
_log.exception("open %s", rawdevice.path)
|
_log.exception("open %s", path)
|
||||||
if e.errno == _errno.EACCES:
|
if e.errno == _errno.EACCES:
|
||||||
exception = e
|
raise
|
||||||
|
|
||||||
if exception:
|
|
||||||
# only keep the last exception
|
|
||||||
raise exception
|
|
||||||
|
|
|
@ -45,41 +45,54 @@ def _run(args):
|
||||||
|
|
||||||
ui.notify.init()
|
ui.notify.init()
|
||||||
|
|
||||||
from solaar.listener import DUMMY_RECEIVER, ReceiverListener
|
|
||||||
window = ui.main_window.create(NAME)
|
window = ui.main_window.create(NAME)
|
||||||
assert window
|
assert window
|
||||||
icon = ui.status_icon.create(window)
|
icon = ui.status_icon.create(window)
|
||||||
assert icon
|
assert icon
|
||||||
|
|
||||||
listeners = {}
|
listeners = {}
|
||||||
|
from logitech.unifying_receiver import base as _base
|
||||||
|
|
||||||
# initializes the receiver listener
|
from solaar.listener import ReceiverListener
|
||||||
def check_for_listener(notify=False):
|
|
||||||
# print ("check_for_listener", notify)
|
|
||||||
|
|
||||||
|
def handle_receivers_events(action, device):
|
||||||
|
assert action is not None
|
||||||
|
assert device is not None
|
||||||
|
|
||||||
|
if action == 'add':
|
||||||
|
# a new receiver device was detected
|
||||||
|
if not listeners:
|
||||||
|
# handle only one receiver for now, the rest are ignored
|
||||||
try:
|
try:
|
||||||
l = ReceiverListener.open(status_changed)
|
l = ReceiverListener.open(device.path, status_changed)
|
||||||
|
if l is not None:
|
||||||
|
listeners[device.path] = l
|
||||||
except OSError:
|
except OSError:
|
||||||
l = None
|
# permission error, blacklist this path for now
|
||||||
ui.error_dialog(window, 'Permissions error',
|
listeners.pop(device.path, None)
|
||||||
'Found a possible Unifying Receiver device,\n'
|
import logging
|
||||||
'but did not have permission to open it.')
|
logging.exception("failed to open %s", device.path)
|
||||||
|
# ui.error_dialog(window, 'Permissions error',
|
||||||
|
# 'Found a possible Unifying Receiver device,\n'
|
||||||
|
# 'but did not have permission to open it.')
|
||||||
|
|
||||||
listeners.clear()
|
elif action == 'remove':
|
||||||
if l:
|
# we'll be receiving remove events for any hidraw devices,
|
||||||
listeners[l.receiver.serial] = l
|
# not just Logitech receivers, so it's okay if the device is not
|
||||||
else:
|
# already in our listeners map
|
||||||
if notify:
|
l = listeners.pop(device.path, None)
|
||||||
status_changed(DUMMY_RECEIVER)
|
if l is not None:
|
||||||
else:
|
assert isinstance(l, ReceiverListener)
|
||||||
return True
|
l.stop()
|
||||||
|
|
||||||
from gi.repository import Gtk, GLib
|
# print ("****", action, device, listeners)
|
||||||
from logitech.unifying_receiver.status import ALERT
|
|
||||||
|
|
||||||
# callback delivering status notifications from the receiver/devices to the UI
|
# callback delivering status notifications from the receiver/devices to the UI
|
||||||
|
from gi.repository import GLib
|
||||||
|
from logitech.unifying_receiver.status import ALERT
|
||||||
def status_changed(device, alert=ALERT.NONE, reason=None):
|
def status_changed(device, alert=ALERT.NONE, reason=None):
|
||||||
assert device is not None
|
assert device is not None
|
||||||
|
# print ("status changed", device, reason)
|
||||||
|
|
||||||
if alert & ALERT.SHOW_WINDOW:
|
if alert & ALERT.SHOW_WINDOW:
|
||||||
GLib.idle_add(window.present)
|
GLib.idle_add(window.present)
|
||||||
|
@ -87,14 +100,25 @@ def _run(args):
|
||||||
GLib.idle_add(ui.status_icon.update, icon, device)
|
GLib.idle_add(ui.status_icon.update, icon, device)
|
||||||
|
|
||||||
if ui.notify.available:
|
if ui.notify.available:
|
||||||
# always notify on receiver updates
|
if alert & ALERT.NOTIFICATION:
|
||||||
if device is DUMMY_RECEIVER or alert & ALERT.NOTIFICATION:
|
GLib.idle_add(ui.notify.show, device, reason)
|
||||||
|
elif device.kind is None and not device:
|
||||||
|
# notify when a receiver was removed
|
||||||
GLib.idle_add(ui.notify.show, device, reason)
|
GLib.idle_add(ui.notify.show, device, reason)
|
||||||
|
|
||||||
if device is DUMMY_RECEIVER:
|
# if device.kind is None and not device:
|
||||||
GLib.timeout_add(3000, check_for_listener)
|
# # a receiver was removed
|
||||||
|
# listeners.clear()
|
||||||
|
|
||||||
GLib.timeout_add(10, check_for_listener, True)
|
# ugly...
|
||||||
|
def _startup_check_receiver():
|
||||||
|
from solaar.listener import DUMMY_RECEIVER
|
||||||
|
if not listeners:
|
||||||
|
status_changed(DUMMY_RECEIVER, ALERT.NOTIFICATION)
|
||||||
|
GLib.timeout_add(1000, _startup_check_receiver)
|
||||||
|
|
||||||
|
GLib.timeout_add(10, _base.notify_on_receivers, handle_receivers_events)
|
||||||
|
from gi.repository import Gtk
|
||||||
Gtk.main()
|
Gtk.main()
|
||||||
|
|
||||||
map(ReceiverListener.stop, listeners.values())
|
map(ReceiverListener.stop, listeners.values())
|
||||||
|
|
|
@ -153,10 +153,12 @@ class ReceiverListener(_listener.EventsListener):
|
||||||
__unicode__ = __str__
|
__unicode__ = __str__
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def open(self, status_changed_callback):
|
def open(self, path, status_changed_callback):
|
||||||
assert status_changed_callback
|
assert status_changed_callback
|
||||||
receiver = Receiver.open()
|
receiver = Receiver.open(path)
|
||||||
if receiver:
|
if receiver:
|
||||||
rl = ReceiverListener(receiver, status_changed_callback)
|
rl = ReceiverListener(receiver, status_changed_callback)
|
||||||
rl.start()
|
rl.start()
|
||||||
return rl
|
return rl
|
||||||
|
else:
|
||||||
|
_log.warn("failed to open %s", path)
|
||||||
|
|
Loading…
Reference in New Issue