From 381371c899b4294754589894211312899bba814b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Fri, 13 Nov 2020 20:48:09 -0300 Subject: [PATCH] receiver: add MouseClick diversion rule --- docs/rules.md | 1 + lib/logitech_receiver/diversion.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/docs/rules.md b/docs/rules.md index 04f4d653..a0359e3c 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -91,6 +91,7 @@ A `KeyPress` action takes a sequence of X11 key symbols and simulates a chorded Any key symbols that correspond to modifier keys that are in the current keyboard modifiers are ignored. A `MouseScroll` action takes a sequence of two numbers and simulates a horizontal and vertical mouse scroll of these amounts. If the previous condition in the parent rule returns a number the scroll amounts are multiplied by this number. +A `MouseClick` action takes a mouse button name (`left`, `middle` or `right`) and a positive number, and simulates that number of clicks of the specified button. An `Execute` actions takes a program and arguments and executes it asynchronously. Solaar has several built-in rules, which are run after user-created rules and so can be overridden by user-created rules. diff --git a/lib/logitech_receiver/diversion.py b/lib/logitech_receiver/diversion.py index 79ed5a6e..ea7c8ed8 100644 --- a/lib/logitech_receiver/diversion.py +++ b/lib/logitech_receiver/diversion.py @@ -444,6 +444,33 @@ class MouseScroll(Action): return None +class MouseClick(Action): + def __init__(self, args): + if len(args) == 1 and isinstance(args[0], list): + args = args[0] + if not isinstance(args, list): + args = [args] + self.button = str(args[0]) if len(args) >= 0 else 'unknown' + if not hasattr(_mouse.Button, self.button): + _log.warn('rule MouseClick action: button %s not known', self.button) + self.button = 'unknown' + count = args[1] if len(args) >= 1 else 1 + try: + self.count = int(count) + except ValueError | TypeError: + _log.warn('rule MouseClick action: count %s should be an integer', count) + self.count = 1 + + def __str__(self): + return 'MouseClick: %s (%d)' % (self.button, self.count) + + def evaluate(self, feature, notification, device, status, last_result): + if _log.isEnabledFor(_INFO): + _log.info('MouseClick action: %d %s' % (self.count, self.button)) + mouse.click(getattr(_mouse.Button, self.button), self.count) + return None + + class Execute(Action): def __init__(self, args): if isinstance(args, str): @@ -478,6 +505,7 @@ COMPONENTS = { 'Test': Test, 'KeyPress': KeyPress, 'MouseScroll': MouseScroll, + 'MouseClick': MouseClick, 'Execute': Execute, }