/
harmony.py
88 lines (72 loc) · 2.48 KB
/
harmony.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Chord typing emulation for Linux."""
import sys
import yaml
import evdev
from asyncore import file_dispatcher, loop
from daemon import Daemon
from colorlog import log, LOGLEVEL
from switchboard import SwitchBoard
# pylint: disable=invalid-name
CONFIG = {}
with open('harmony_keymap.yml') as f:
CONFIG = yaml.safe_load(f.read())
LOGLEVEL.set_level(CONFIG['config']['log_level'])
SWITCHBOARD = SwitchBoard(CONFIG)
def process_event(event, config=CONFIG):
"""Take even through translation chain."""
# TODO forward events that aren't relevant to the chord translation
chord_events = SWITCHBOARD.process_event(event)
for event in chord_events:
# TODO: translate chord event into key event
pass
class InputDeviceDispatcher(file_dispatcher):
"""Asynchronous device dispatcher."""
def __init__(self, device):
"""Store device."""
self.device = device
file_dispatcher.__init__(self, device)
def recv(self, _):
"""Read device."""
return self.device.read()
def handle_read(self):
"""Dispatch event."""
for event in self.recv():
process_event(event)
class Harmony(Daemon):
"""Chord typing emulation daemon."""
def run(self, config=CONFIG):
"""Setup and teardown."""
# TODO: see if user indicated which device(s) to grab
# for now, just grab anything that looks remotely like a keyboard
all_devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
self.devices = []
for device in all_devices:
if "Keyboard" in device.name or "HID" in device.name:
self.devices.append(device)
device.grab()
InputDeviceDispatcher(device)
loop()
def stop(self):
"""Teardown."""
for device in self.devices:
device.ungrab()
Daemon.stop(self)
if __name__ == "__main__":
daemon = Harmony(CONFIG['config']['pid_file'])
if len(sys.argv) == 2:
if sys.argv[1] == "start":
log("info", "Starting Harmony")
daemon.start()
elif sys.argv[1] == "stop":
log("info", "Stopping Harmony")
daemon.stop()
elif sys.argv[1] == "restart":
log("info", "Restarting Harmony")
daemon.restart()
else:
log("fail", "Unknown command")
sys.exit(2)
sys.exit(0)
else:
log("info", "Usage %s start|stop|restart" % sys.argv[0])
sys.exit(2)