handle sleep in udev monitoring, fixes #35

This commit is contained in:
Daniel Pavel 2013-05-04 12:18:49 +02:00
parent 5e68094e87
commit ad577d22d0
3 changed files with 41 additions and 22 deletions

View File

@ -121,27 +121,44 @@ def monitor_async(callback, *device_filters):
def monitor(callback, *device_filters):
c = _Context()
def _monitor():
c = _Context()
for device in c.list_devices(subsystem='hidraw'):
# print (device, dict(device), dict(device.attributes))
for filter in device_filters:
d_info = _match('add', device, *filter)
if d_info:
callback('add', d_info)
break
m = _Monitor.from_netlink(c)
m.filter_by(subsystem='hidraw')
for action, device in m:
# print ('----', action, device)
if action in ('add', 'remove'):
for device in c.list_devices(subsystem='hidraw'):
# print (device, dict(device), dict(device.attributes))
for filter in device_filters:
d_info = _match(action, device, *filter)
d_info = _match('add', device, *filter)
if d_info:
callback(action, d_info)
callback('add', d_info)
break
m = _Monitor.from_netlink(c)
del c
m.filter_by(subsystem='hidraw')
try:
for action, device in m:
# print ('----', action, device)
if action in ('add', 'remove'):
for filter in device_filters:
d_info = _match(action, device, *filter)
if d_info:
callback(action, d_info)
break
finally:
del m
while True:
try:
_monitor()
except IOError as e:
print ("monitor IOError", e)
if e.errno == _errno.EINTR:
# raised when the computer wakes from sleep
# in this case, just restart the monitor
continue
raise
def enumerate(vendor_id=None, product_id=None, interface_number=None, driver=None):
"""Enumerate the HID Devices.

View File

@ -388,3 +388,5 @@ class Receiver(object):
_log.exception("open %s", path)
if e.errno == _errno.EACCES:
raise
except:
_log.exception("open %s", path)

View File

@ -66,12 +66,12 @@ class ReceiverListener(_listener.EventsListener):
if _log.isEnabledFor(_DEBUG):
_log.debug("%s: polling status: %s", self.receiver, list(iter(self.receiver)))
if self._last_tick > 0 and timestamp - self._last_tick > _POLL_TICK * 3:
# if we missed a couple of polls, most likely the computer went into
# sleep, and we have to reinitialize the receiver again
_log.warn("%s: possible sleep detected, closing this listener", self.receiver)
self.stop()
return
# if self._last_tick > 0 and timestamp - self._last_tick > _POLL_TICK * 2:
# # if we missed a couple of polls, most likely the computer went into
# # sleep, and we have to reinitialize the receiver again
# _log.warn("%s: possible sleep detected, closing this listener", self.receiver)
# self.stop()
# return
self._last_tick = timestamp