~whynothugo/dotfiles

dotfiles/home/.local/bin/lock-and-sleep -rwxr-xr-x 952 bytes
f40afb31Hugo Osvaldo Barrera waybar: open bluetuith for bluetooth control 11 days ago
                                                                                
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
#!/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