From 576e21319352b3fce6bcce59d37908e29b57ed72 Mon Sep 17 00:00:00 2001 From: Brendan Howell Date: Thu, 18 Feb 2016 23:21:53 +0100 Subject: [PATCH] basic barcode/KB I/O stuff with Typing Pool bureau --- screenless/bureau/typing.py | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 screenless/bureau/typing.py diff --git a/screenless/bureau/typing.py b/screenless/bureau/typing.py new file mode 100644 index 0000000..42c5d29 --- /dev/null +++ b/screenless/bureau/typing.py @@ -0,0 +1,76 @@ +import evdev + +from bureau import Bureau + +KEYS = { + 0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8', + 10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'\b', 15: u'\t', 16: u'q', 17: u'w', 18: u'e', 19: u'r', + 20: u't', 21: u'y', 22: u'u', 23: u'i', 24: u'o', 25: u'p', 26: u'[', 27: u']', 28: u'\n', 29: u'LCTRL', + 30: u'a', 31: u's', 32: u'd', 33: u'f', 34: u'g', 35: u'h', 36: u'j', 37: u'k', 38: u'l', 39: u';', + 40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'z', 45: u'x', 46: u'c', 47: u'v', 48: u'b', 49: u'n', + 50: u'm', 51: u',', 52: u'', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT' +} + + +class TypingPool(Bureau): + """ + The typing pool is the bureau that converts keyboard, barcode scanner, etc. + input to messages and sends them to the responsible bureau. + """ + name = "Typing Pool" + prefix = "TY" + version = 0 + + def __init__(self): + Bureau.__init__(self) + + devices = map(evdev.InputDevice, evdev.list_devices()) + + self.devices = [] + self.active_devices = [] + + # add anything that smells like a keyboard and grab focus + for dev in devices: + try: + if evdev.ecodes.KEY_A in dev.capabilities()[evdev.ecodes.EV_KEY]: + print(dir(dev)) + print(dev.fn) + # dev.grab() + self.devices.append(dev) + except KeyError as err: + print("ignoring non-keyboard device: " + str(dev)) + + # if we don't have any active KB so tell the user to config one + if "active" in self.config: + # TODO: better error handling here + self.active_devices = list(map(evdev.InputDevice, + self.config["active"]["devices"].split())) + else: + print("you need to configure input hardware!") + print("add one of the following devices to TY.ini under [active]") + print(" devices = /dev/usb/kb1 /dev/input/whatever") + print("available keyboard devices:") + for dev in self.devices: + print(dev.fn, ":", dev.name, dev.info) + + def run_io(self): + val = "" + #TODO: this is crap, needs to be multi-threaded and have one + # such loop for each active device + self.active_devices[0].grab() + for ev in self.active_devices[0].read_loop(): + if ev.type == evdev.ecodes.EV_KEY: + data = evdev.categorize(ev) + if data.keystate == 1: + if data.scancode == 28: + print("sending barcode:", val) + val = "" + else: + try: + val += KEYS[data.scancode] + except KeyError: + print("Error invalid keycode:", data.scancode) + +if __name__ == "__main__": + evd = TypingPool() + evd.run()