From 468e30ce66e2e0201524bdf9fc79950b912d6689 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 8 Feb 2020 13:56:52 +0100 Subject: [PATCH] [main] Add input thread logic and logging To the main application, add an input thread that "simply" reads sys.stdin events and transmits them via core.input. Additionally, set up some initial logging (yeah, for threading, this is needed immediately) --- bumblebee-status | 38 +++++++++++++++++++++++++++++++++++++- core/input.py | 11 +++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/bumblebee-status b/bumblebee-status index bca1a01..4d8254f 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -1,14 +1,51 @@ #!/usr/bin/env python import sys +import json +import select +import logging +import threading + import core.config import core.output import core.module +import core.input + +def handle_input(): + poll = select.poll() + poll.register(sys.stdin.fileno(), select.POLLIN) + + while True: + events = poll.poll() + + for fileno, event in events: + line = '[' + while line.startswith('['): + line = sys.stdin.readline().strip(',').strip() + logging.info('input event: {}'.format(line)) + try: + event = json.loads(line) + core.input.trigger(event) + except ValueError: + pass + + poll.unregister(sys.stdin.fileno()) def main(): + logging.basicConfig( + level=logging.DEBUG, + format="[%(asctime)s] %(module)-16s %(levelname)-8s %(message)s", + stream=sys.stderr + ) + config = core.config.Config(sys.argv[1:]) output = core.output.i3() modules = [] + + input_thread = threading.Thread(target=handle_input) + input_thread.daemon = True + input_thread.start() + for module in config.modules(): modules.append(core.module.load(module, config)) output.modules(modules) @@ -20,7 +57,6 @@ def main(): output.draw('statusline') output.wait(config.interval()) output.draw('stop') - if __name__ == "__main__": main() diff --git a/core/input.py b/core/input.py index dde22a8..18042e8 100644 --- a/core/input.py +++ b/core/input.py @@ -1,4 +1,6 @@ import uuid +import logging + import util.cli LEFT_MOUSE = 1 @@ -7,6 +9,14 @@ RIGHT_MOUSE = 3 WHEEL_UP = 4 WHEEL_DOWN = 5 +def button_name(button): + if button == LEFT_MOUSE: return 'left-mouse' + if button == RIGHT_MOUSE: return 'right-mouse' + if button == MIDDLE_MOUSE: return 'middle-mouse' + if button == WHEEL_UP: return 'wheel-up' + if button == WHEEL_DOWN: return 'wheel-down' + return 'n/a' + callbacks = {} class Object(object): @@ -18,6 +28,7 @@ class Object(object): return self._id def register(obj, button=None, cmd=None): + logging.debug('registering callback {} {}'.format(obj.id(), button)) callbacks.setdefault(obj.id(), {}).setdefault(button, []).append(cmd) def trigger(event):