From f327e3ad38e6e5adcf34b53b6d62eb04fc336e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Thu, 10 Mar 2022 17:09:59 -0300 Subject: [PATCH] logitech_receiver: split Test condition --- docs/rules.md | 10 +++++----- lib/logitech_receiver/diversion.py | 31 +++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/docs/rules.md b/docs/rules.md index 66caed4d..fe58dc65 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -83,22 +83,22 @@ string argument. Alternatively, if the argument is a list `[name, action]` wher is either `'pressed'` or `'released'`, the key down or key up events of `name` argument are matched, respectively. Logitech key and button names are shown in the `Key/Button Diversion` setting. Some keyboards have Gn, Mn, or MR keys, which are diverted using the 'Divert G Keys' setting. -`Test` conditions are true if their test evaluates to true on the feature, +`Test` and `TestBytes` conditions are true if their test evaluates to true on the feature, report, and data of the current notification. Test conditions can return a number instead of a boolean. -Test conditions consisting of a sequence of three or four integers use the first +`TestBytes` conditions consist of a sequence of three or four integers and use the first two to select bytes of the notification data. Writing this kind of test condition is not trivial. -Three-element test conditions are true if the selected bytes bit-wise anded +Three-element `TestBytes` conditions are true if the selected bytes bit-wise anded with its third element is non-zero. The value of these test conditions is the result of the and. -Four-element test conditions are true if the selected bytes form a signed +Four-element `TestBytes` conditions are true if the selected bytes form a signed integer between the third and fourth elements. The value of these test condition is the signed value of the selected bytes if that is non-zero otherwise True. -The other test conditions are mnemonic shorthands for meaningful feature, +`Test` conditions are mnemonic shorthands for meaningful feature, report, and data combinations in notifications. A `crown_right` test is the rotation amount of a `CROWN` right rotation notification. A `crown_left` test is the rotation amount of a `CROWN` left rotation notification. diff --git a/lib/logitech_receiver/diversion.py b/lib/logitech_receiver/diversion.py index ed2701d5..3f7f4f55 100644 --- a/lib/logitech_receiver/diversion.py +++ b/lib/logitech_receiver/diversion.py @@ -553,11 +553,10 @@ class Test(Condition): else: _log.warn('rule Test string argument not name of a test: %s', test) self.function = TESTS['False'] - elif ( - isinstance(test, list) and 2 < len(test) <= 4 and all(isinstance(t, int) for t in test) and test[0] >= 0 - and test[0] <= 16 and test[1] >= 0 and test[1] <= 16 and test[0] < test[1] - ): - self.function = bit_test(*test) if len(test) == 3 else range_test(*test) + elif isinstance(test, list) and all(isinstance(t, int) for t in test): + _log.warn('Test rules consisting of numbers are deprecated, converting to a TestBytes condition') + self.__class__ = TestBytes + self.__init__(test) else: _log.warn('rule Test argument not valid %s', test) @@ -571,6 +570,27 @@ class Test(Condition): return {'Test': str(self.test)} +class TestBytes(Condition): + def __init__(self, test): + self.test = test + if ( + isinstance(test, list) and 2 < len(test) <= 4 and all(isinstance(t, int) for t in test) and test[0] >= 0 + and test[0] <= 16 and test[1] >= 0 and test[1] <= 16 and test[0] < test[1] + ): + self.function = bit_test(*test) if len(test) == 3 else range_test(*test) + else: + _log.warn('rule TestBytes argument not valid %s', test) + + def __str__(self): + return 'TestBytes: ' + str(self.test) + + def evaluate(self, feature, notification, device, status, last_result): + return self.function(feature, notification.address, notification.data) + + def data(self): + return {'TestBytes': str(self.test)} + + class MouseGesture(Condition): MOVEMENTS = [ 'Mouse Up', 'Mouse Down', 'Mouse Left', 'Mouse Right', 'Mouse Up-left', 'Mouse Up-right', 'Mouse Down-left', @@ -860,6 +880,7 @@ COMPONENTS = { 'Modifiers': Modifiers, 'Key': Key, 'Test': Test, + 'TestBytes': TestBytes, 'MouseGesture': MouseGesture, 'KeyPress': KeyPress, 'MouseScroll': MouseScroll,