[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)
This commit is contained in:
Tobias Witek 2020-02-08 13:56:52 +01:00
parent fca364554e
commit 468e30ce66
2 changed files with 48 additions and 1 deletions

View file

@ -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)
@ -21,7 +58,6 @@ def main():
output.wait(config.interval())
output.draw('stop')
if __name__ == "__main__":
main()

View file

@ -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):