From b4bca4670ba7b388159581f7e57a9374b52a9b4e Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Wed, 19 Jun 2013 17:03:01 +0200 Subject: [PATCH] cache notification flags when possible avoids unnecessary reads from devices when the status hasn't changed --- lib/logitech/unifying_receiver/receiver.py | 18 ++++++++---------- lib/logitech/unifying_receiver/status.py | 8 +++++++- lib/solaar/ui/window.py | 14 +++++--------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/logitech/unifying_receiver/receiver.py b/lib/logitech/unifying_receiver/receiver.py index 2e79b047..7f6e7228 100644 --- a/lib/logitech/unifying_receiver/receiver.py +++ b/lib/logitech/unifying_receiver/receiver.py @@ -183,14 +183,13 @@ class PairedDevice(object): ok = _hidpp10.set_notification_flags(self, set_flag_bits) flag_bits = _hidpp10.get_notification_flags(self) - if flag_bits is not None: - flag_bits = tuple(_hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits)) + flag_names = None if flag_bits is None else tuple(_hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits)) if ok: - _log.info("%s: device notifications %s %s", self, 'enabled' if enable else 'disabled', flag_bits) + _log.info("%s: device notifications %s %s", self, 'enabled' if enable else 'disabled', flag_names) else: - _log.warn("%s: failed to %s device notifications %s", self, 'enable' if enable else 'disable', flag_bits) - return ok + _log.warn("%s: failed to %s device notifications %s", self, 'enable' if enable else 'disable', flag_names) + return flag_bits if ok else None def request(self, request_id, *params): return _base.request(self.receiver.handle, self.number, request_id, *params) @@ -289,14 +288,13 @@ class Receiver(object): ok = _hidpp10.set_notification_flags(self, set_flag_bits) flag_bits = _hidpp10.get_notification_flags(self) - if flag_bits is not None: - flag_bits = tuple(_hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits)) + flag_names = None if flag_bits is None else tuple(_hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits)) if ok: - _log.info("%s: receiver notifications %s => %s", self, 'enabled' if enable else 'disabled', flag_bits) + _log.info("%s: receiver notifications %s => %s", self, 'enabled' if enable else 'disabled', flag_names) else: - _log.warn("%s: failed to %s receiver notifications %s", self, 'enable' if enable else 'disable', flag_bits) - return ok + _log.warn("%s: failed to %s receiver notifications %s", self, 'enable' if enable else 'disable', flag_names) + return flag_bits if ok else None def notify_devices(self): """Scan all devices.""" diff --git a/lib/logitech/unifying_receiver/status.py b/lib/logitech/unifying_receiver/status.py index e640b112..c0bff589 100644 --- a/lib/logitech/unifying_receiver/status.py +++ b/lib/logitech/unifying_receiver/status.py @@ -37,6 +37,7 @@ BATTERY_STATUS='battery-status' BATTERY_CHARGING='battery-charging' LIGHT_LEVEL='light-level' ERROR='error' +NOTIFICATIONS='notifications' # If the battery charge is under this percentage, trigger an attention event # (blink systray icon/notification/whatever). @@ -67,6 +68,7 @@ class ReceiverStatus(dict): self.new_device = None self[ERROR] = None + self[NOTIFICATIONS] = _hidpp10.get_notification_flags(receiver) def __str__(self): count = len(self._receiver) @@ -89,6 +91,9 @@ class ReceiverStatus(dict): # make sure to read some stuff that may be read later by the UI r.serial, r.firmware, None + # r.enable_notifications() + self[NOTIFICATIONS] = _hidpp10.get_notification_flags(r) + def process_notification(self, n): if n.sub_id == 0x4A: self.lock_open = bool(n.address & 0x01) @@ -217,7 +222,8 @@ class DeviceStatus(dict): # Make sure to set notification flags on the device, they # get cleared when the device is turned off (but not when the device # goes idle, and we can't tell the difference right now). - d.enable_notifications() + self[NOTIFICATIONS] = d.enable_notifications() + if self.configuration: self.configuration.attach_to(d) else: diff --git a/lib/solaar/ui/window.py b/lib/solaar/ui/window.py index 870f640f..ea971808 100644 --- a/lib/solaar/ui/window.py +++ b/lib/solaar/ui/window.py @@ -20,6 +20,7 @@ from logitech.unifying_receiver.status import ( BATTERY_CHARGING as _BATTERY_CHARGING, LIGHT_LEVEL as _LIGHT_LEVEL, ENCRYPTED as _ENCRYPTED, + NOTIFICATIONS as _NOTFITICATIONS, ) from . import config_panel as _config_panel from . import action as _action, icons as _icons @@ -453,15 +454,10 @@ def _update_details(button): for fw in device.firmware: yield (fw.kind, (fw.name + ' ' + fw.version).strip()) - if device.kind is None or device.status: - # don't show notifications for offline devices - notification_flags = _hidpp10.get_notification_flags(device) - if notification_flags: - notification_flags = _hidpp10.NOTIFICATION_FLAG.flag_names(notification_flags) - else: - notification_flags = ('(none)',) - - yield ('Notifications', ('\n%16s' % ' ').join(notification_flags)) + flag_bits = device.status.get(_NOTFITICATIONS) + if flag_bits is not None: + flag_names = ('(none)',) if flag_bits == 0 else _hidpp10.NOTIFICATION_FLAG.flag_names(flag_bits) + yield ('Notifications', ('\n%16s' % ' ').join(flag_names)) items = _details_items(device) markup_text = '' + '\n'.join('%-14s: %s' % i for i in items if i) + ''