From 5e305bf3b6b9f7a6c07f65767c49ca182e0dec40 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Thu, 15 Dec 2016 19:41:50 +0100 Subject: [PATCH] [engine/input] Re-enable mouse button parameter bindings Re-enable the possibility to define custom mouse actions by binding commands to ".". These commands are then executed as shell commands. fixes #30 --- bumblebee/engine.py | 20 +++++++++++++++++++- bumblebee/input.py | 1 + runtests.sh | 6 ++---- tests/test_engine.py | 28 ++++++++++++++++++++++++++++ tests/util.py | 12 +++++++++++- 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/bumblebee/engine.py b/bumblebee/engine.py index 0698171..9a9c35f 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -86,12 +86,30 @@ class Engine(object): self.input.start() + def modules(self): + return self._modules + def load_modules(self, modules): """Load specified modules and return them as list""" for module in modules: - self._modules.append(self._load_module(module["module"], module["name"])) + mod = self._load_module(module["module"], module["name"]) + self._modules.append(mod) + self._register_module_callbacks(mod) return self._modules + def _register_module_callbacks(self, module): + buttons = [ + { "name": "left-click", "id": bumblebee.input.LEFT_MOUSE }, + { "name": "middle-click", "id": bumblebee.input.MIDDLE_MOUSE }, + { "name": "right-click", "id": bumblebee.input.RIGHT_MOUSE }, + { "name": "wheel-up", "id": bumblebee.input.WHEEL_UP }, + { "name": "wheel-down", "id": bumblebee.input.WHEEL_DOWN }, + ] + for button in buttons: + if module.parameter(button["name"], None): + self.input.register_callback(obj=module, + button=button["id"], cmd=module.parameter(button["name"])) + def _read_aliases(self): result = {} for module in all_modules(): diff --git a/bumblebee/input.py b/bumblebee/input.py index 7dea7d2..dc2e723 100644 --- a/bumblebee/input.py +++ b/bumblebee/input.py @@ -9,6 +9,7 @@ import threading import bumblebee.util LEFT_MOUSE = 1 +MIDDLE_MOUSE = 2 RIGHT_MOUSE = 3 WHEEL_UP = 4 WHEEL_DOWN = 5 diff --git a/runtests.sh b/runtests.sh index cbc5a2a..a267458 100755 --- a/runtests.sh +++ b/runtests.sh @@ -1,13 +1,11 @@ #!/bin/sh -test=$(which nosetests) - echo "testing with $(python2 -V 2>&1)" -python2 $test --rednose -v tests/ +python2 $(which nosetests) --rednose -v tests/ if [ $? == 0 ]; then echo echo "testing with $(python3 -V 2>&1)" - python3 $test --rednose -v tests/ + python3 $(which nosetests-3) --rednose -v tests/ fi diff --git a/tests/test_engine.py b/tests/test_engine.py index 069a71d..0b71fd4 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -1,10 +1,12 @@ # pylint: disable=C0103,C0111,W0703,W0212 +import shlex import unittest from bumblebee.error import ModuleLoadError from bumblebee.engine import Engine from bumblebee.config import Config +import bumblebee.input from tests.util import MockOutput, MockInput @@ -52,4 +54,30 @@ class TestEngine(unittest.TestCase): except Exception as e: self.fail(e) + def test_custom_cmd(self): + testmodules = [ + { "name": "test", "button": "test.left-click", "action": "echo" }, + { "name": "test:alias", "button": "alias.right-click", "action": "echo2" }, + ] + cmd = "-m" + for test in testmodules: + cmd += " " + test["name"] + cmd += " -p" + for test in testmodules: + cmd += " " + test["button"] + "=" + test["action"] + cfg = Config(shlex.split(cmd)) + inp = MockInput() + engine = Engine(config=cfg, output=MockOutput(), inp=inp) + + i = 0 + for module in engine.modules(): + callback = inp.get_callback(module.id) + self.assertTrue(callback is not None) + self.assertEquals(callback["command"], testmodules[i]["action"]) + if "left" in testmodules[i]["button"]: + self.assertTrue(callback["button"], bumblebee.input.LEFT_MOUSE) + if "right" in testmodules[i]["button"]: + self.assertTrue(callback["button"], bumblebee.input.RIGHT_MOUSE) + i += 1 + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py index 0b46d62..4b599b7 100644 --- a/tests/util.py +++ b/tests/util.py @@ -37,14 +37,24 @@ def assertMouseEvent(mock_input, mock_output, mock_select, engine, module, butto assertPopen(mock_output, cmd) class MockInput(object): + def __init__(self): + self._callbacks = {} def start(self): pass def stop(self): pass + def get_callback(self, uid): + return self._callbacks.get(uid, None) + def register_callback(self, obj, button, cmd): - pass + if not obj: + return + self._callbacks[obj.id] = { + "button": button, + "command": cmd, + } class MockEngine(object): def __init__(self):