From 619223e8beb9235e1858f4c8395cd9f076ac462d Mon Sep 17 00:00:00 2001 From: "Peter F. Patel-Schneider" Date: Mon, 19 Sep 2022 08:15:18 -0400 Subject: [PATCH] rules: add Later action --- docs/rules.md | 4 ++++ lib/logitech_receiver/diversion.py | 33 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/docs/rules.md b/docs/rules.md index b45b265d..fb2811dc 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -216,6 +216,10 @@ For settings that use gestures as an argument the internal name of the gesture i which can be found in the GESTURE2_GESTURES_LABELS structure in lib/logitech_receiver/settings_templates. All settings are supported. +A `Sleep` action executes rule components later. +`Set` actions take an integer delay between 1 and 100 followed by a list of rule components that will be executed later. +Processing of the rest of the rule continues immediately. + Solaar has several built-in rules, which are run after user-created rules and so can be overridden by user-created rules. One rule turns `Brightness Down` key press notifications into `XF86_MonBrightnessDown` key taps diff --git a/lib/logitech_receiver/diversion.py b/lib/logitech_receiver/diversion.py index d1b5a7d1..45d054a9 100644 --- a/lib/logitech_receiver/diversion.py +++ b/lib/logitech_receiver/diversion.py @@ -1092,6 +1092,38 @@ class Execute(Action): return {'Execute': self.args[:]} +class Later(Action): + def __init__(self, args, warn=True): + self.delay = 0 + self.rule = Rule([]) + self.components = [] + if not (isinstance(args, list)): + args = [args] + if not (isinstance(args, list) and len(args) >= 1): + if warn: + _log.warn('rule Later argument not list with minimum length 1: %s', args) + elif not (isinstance(args[0], int)) or not 0 < args[0] < 101: + if warn: + _log.warn('rule Later argument delay not integer between 1 and 100: %s', args) + else: + self.delay = args[0] + self.rule = Rule(args[1:], warn=warn) + self.components = self.rule.components + + def __str__(self): + return 'Later: [' + str(self.delay) + ', ' + ', '.join(str(c) for c in self.components) + ']' + + def evaluate(self, feature, notification, device, status, last_result): + if self.delay and self.rule: + GLib.timeout_add_seconds(self.delay, Rule.evaluate, self.rule, feature, notification, device, status, last_result) + return None + + def data(self): + data = [c.data() for c in self.components] + data.insert(0, self.delay) + return {'Later': data} + + COMPONENTS = { 'Rule': Rule, 'Not': Not, @@ -1113,6 +1145,7 @@ COMPONENTS = { 'MouseClick': MouseClick, 'Set': Set, 'Execute': Execute, + 'Later': Later, } built_in_rules = Rule([])