simplewhispr/auto.py
2026-06-04 16:25:22 -05:00

42 lines
998 B
Python

import os
import signal
import sys
import main
def auto_toggle():
pid_file = "/tmp/simplewhispr.pid"
if os.path.exists(pid_file):
with open(pid_file, "r") as f:
try:
pid = int(f.read().strip())
os.kill(pid, signal.SIGUSR1)
print("info: stopped existing recording.")
except ProcessLookupError, ValueError:
print("info: stale pid file found, starting new recording.")
os.remove(pid_file)
_fork_and_start()
else:
print("info: starting new recording.")
_fork_and_start()
def _fork_and_start():
# perform a single fork to detach the process
try:
pid = os.fork()
if pid > 0:
# parent process exits
sys.exit(0)
except OSError as e:
print(f"fatal: fork failed: {e}")
sys.exit(1)
# start the main application
main.main()
if __name__ == "__main__":
auto_toggle()