[engine/input] Re-enable mouse button parameter bindings

Re-enable the possibility to define custom mouse actions by binding
commands to "<alias|module>.<left-click|right-click|...>". These
commands are then executed as shell commands.

fixes #30
This commit is contained in:
Tobi-wan Kenobi 2016-12-15 19:41:50 +01:00
parent 41b61d05d5
commit 5e305bf3b6
5 changed files with 61 additions and 6 deletions

View file

@ -86,12 +86,30 @@ class Engine(object):
self.input.start() self.input.start()
def modules(self):
return self._modules
def load_modules(self, modules): def load_modules(self, modules):
"""Load specified modules and return them as list""" """Load specified modules and return them as list"""
for module in modules: 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 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): def _read_aliases(self):
result = {} result = {}
for module in all_modules(): for module in all_modules():

View file

@ -9,6 +9,7 @@ import threading
import bumblebee.util import bumblebee.util
LEFT_MOUSE = 1 LEFT_MOUSE = 1
MIDDLE_MOUSE = 2
RIGHT_MOUSE = 3 RIGHT_MOUSE = 3
WHEEL_UP = 4 WHEEL_UP = 4
WHEEL_DOWN = 5 WHEEL_DOWN = 5

View file

@ -1,13 +1,11 @@
#!/bin/sh #!/bin/sh
test=$(which nosetests)
echo "testing with $(python2 -V 2>&1)" echo "testing with $(python2 -V 2>&1)"
python2 $test --rednose -v tests/ python2 $(which nosetests) --rednose -v tests/
if [ $? == 0 ]; then if [ $? == 0 ]; then
echo echo
echo "testing with $(python3 -V 2>&1)" echo "testing with $(python3 -V 2>&1)"
python3 $test --rednose -v tests/ python3 $(which nosetests-3) --rednose -v tests/
fi fi

View file

@ -1,10 +1,12 @@
# pylint: disable=C0103,C0111,W0703,W0212 # pylint: disable=C0103,C0111,W0703,W0212
import shlex
import unittest import unittest
from bumblebee.error import ModuleLoadError from bumblebee.error import ModuleLoadError
from bumblebee.engine import Engine from bumblebee.engine import Engine
from bumblebee.config import Config from bumblebee.config import Config
import bumblebee.input
from tests.util import MockOutput, MockInput from tests.util import MockOutput, MockInput
@ -52,4 +54,30 @@ class TestEngine(unittest.TestCase):
except Exception as e: except Exception as e:
self.fail(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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -37,14 +37,24 @@ def assertMouseEvent(mock_input, mock_output, mock_select, engine, module, butto
assertPopen(mock_output, cmd) assertPopen(mock_output, cmd)
class MockInput(object): class MockInput(object):
def __init__(self):
self._callbacks = {}
def start(self): def start(self):
pass pass
def stop(self): def stop(self):
pass pass
def get_callback(self, uid):
return self._callbacks.get(uid, None)
def register_callback(self, obj, button, cmd): def register_callback(self, obj, button, cmd):
pass if not obj:
return
self._callbacks[obj.id] = {
"button": button,
"command": cmd,
}
class MockEngine(object): class MockEngine(object):
def __init__(self): def __init__(self):