49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
"""Wait for release of the physical Super+D chord that launched this service."""
|
|
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
from contextlib import closing
|
|
|
|
from evdev import InputDevice, ecodes as key, list_devices
|
|
|
|
|
|
def held(keys):
|
|
return key.KEY_D in keys and bool({key.KEY_LEFTMETA, key.KEY_RIGHTMETA} & set(keys))
|
|
|
|
|
|
def interrupted(signum, frame):
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
def main(handy):
|
|
signal.signal(signal.SIGTERM, interrupted)
|
|
for path in list_devices():
|
|
try:
|
|
device = InputDevice(path)
|
|
except OSError:
|
|
continue # Devices can disappear between enumeration and opening.
|
|
with closing(device):
|
|
if not held(device.active_keys()):
|
|
continue
|
|
action = "--cancel"
|
|
try:
|
|
subprocess.run([handy, "--toggle-transcription"], check=True)
|
|
# A quick release may already be queued while the CLI starts.
|
|
if held(device.active_keys()):
|
|
for event in device.read_loop():
|
|
if not held(device.active_keys()):
|
|
break
|
|
action = "--toggle-transcription"
|
|
finally:
|
|
# Disconnects, cancellation and service shutdown discard the recording.
|
|
subprocess.run([handy, action], check=True)
|
|
return
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main(sys.argv[1])
|
|
except KeyboardInterrupt:
|
|
pass
|