[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:
parent
fca364554e
commit
468e30ce66
2 changed files with 48 additions and 1 deletions
|
@ -1,14 +1,51 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import json
|
||||||
|
import select
|
||||||
|
import logging
|
||||||
|
import threading
|
||||||
|
|
||||||
import core.config
|
import core.config
|
||||||
import core.output
|
import core.output
|
||||||
import core.module
|
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():
|
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:])
|
config = core.config.Config(sys.argv[1:])
|
||||||
output = core.output.i3()
|
output = core.output.i3()
|
||||||
modules = []
|
modules = []
|
||||||
|
|
||||||
|
input_thread = threading.Thread(target=handle_input)
|
||||||
|
input_thread.daemon = True
|
||||||
|
input_thread.start()
|
||||||
|
|
||||||
for module in config.modules():
|
for module in config.modules():
|
||||||
modules.append(core.module.load(module, config))
|
modules.append(core.module.load(module, config))
|
||||||
output.modules(modules)
|
output.modules(modules)
|
||||||
|
@ -20,7 +57,6 @@ def main():
|
||||||
output.draw('statusline')
|
output.draw('statusline')
|
||||||
output.wait(config.interval())
|
output.wait(config.interval())
|
||||||
output.draw('stop')
|
output.draw('stop')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
import uuid
|
import uuid
|
||||||
|
import logging
|
||||||
|
|
||||||
import util.cli
|
import util.cli
|
||||||
|
|
||||||
LEFT_MOUSE = 1
|
LEFT_MOUSE = 1
|
||||||
|
@ -7,6 +9,14 @@ RIGHT_MOUSE = 3
|
||||||
WHEEL_UP = 4
|
WHEEL_UP = 4
|
||||||
WHEEL_DOWN = 5
|
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 = {}
|
callbacks = {}
|
||||||
|
|
||||||
class Object(object):
|
class Object(object):
|
||||||
|
@ -18,6 +28,7 @@ class Object(object):
|
||||||
return self._id
|
return self._id
|
||||||
|
|
||||||
def register(obj, button=None, cmd=None):
|
def register(obj, button=None, cmd=None):
|
||||||
|
logging.debug('registering callback {} {}'.format(obj.id(), button))
|
||||||
callbacks.setdefault(obj.id(), {}).setdefault(button, []).append(cmd)
|
callbacks.setdefault(obj.id(), {}).setdefault(button, []).append(cmd)
|
||||||
|
|
||||||
def trigger(event):
|
def trigger(event):
|
||||||
|
|
Loading…
Reference in a new issue