#!/usr/bin/env python3
#
# Requires swaylock > 1.7.2.
import os
import sys
import subprocess
(r, w) = os.pipe()
os.set_inheritable(r, False)
swaylock = subprocess.Popen(["swaylock", "--ready-fd", str(w)], pass_fds=(w,))
os.close(w)
# Wait for swaylock to be ready.
with os.fdopen(r, "rb") as ready_pipe:
while True:
pipe_data = ready_pipe.read(1)
if pipe_data == b"":
print("fatal: swaylock closed fd before locking")
sys.exit(1)
if pipe_data == b"\n":
break
while True:
subprocess.call(["powerctl", "mem"]) # blocks until wakeup from sleep.
wayidle = subprocess.Popen(["wayidle", "--timeout", "10"])
(pid, returncode) = os.wait()
if pid == swaylock.pid:
wayidle.kill()
sys.exit(returncode)
if pid == wayidle.pid and returncode != 0:
print("fatal: wayidle exited non-zero")
sys.exit(returncode) # swaylock will continue running