You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.5 KiB
Python

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 = ""
upper = False
#TODO: this is crap, needs to be multi-threaded and have one
# such loop for each active device
self.active_devices[0].grab()
# TODO: this is a bit messy / complex. break into one more function.
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)
self.send(val[0:2], val[2:])
val = ""
else:
try:
new_key = KEYS[data.scancode]
if new_key == "LSHFT" or new_key == "RSHFT":
upper = True
else:
if upper:
new_key = new_key.upper()
upper = False
val += new_key
except KeyError:
print("Error invalid keycode:", data.scancode)
def main():
evd = TypingPool()
evd.run()
if __name__ == "__main__":
evd = TypingPool()
evd.run()