aboutsummaryrefslogtreecommitdiff
path: root/extras/HOME/.local/bin/rdectl
blob: 7c594f75f7cb15850022ca4619c774140151148b (plain) (blame)
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
#!/usr/bin/env python3

import os
import sys
import socket


def get_socket_path() -> str:
    display = os.environ.get("DISPLAY")
    if not display:
        raise RuntimeError("DISPLAY is not set")

    display_suffix = display.replace(":", "", 1)

    runtime_dir = os.environ.get("XDG_RUNTIME_DIR")
    base_dir = runtime_dir if runtime_dir else "/run/rde"

    return os.path.join(base_dir, f"rde-{display_suffix}.sock")


def main() -> int:
    if len(sys.argv) < 2:
        print(f"usage: {sys.argv[0]} <command...>", file=sys.stderr)
        return 1

    path = get_socket_path()
    message = " ".join(sys.argv[1:]) + "\n"

    with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
        sock.connect(path)
        sock.sendall(message.encode("utf-8"))

    return 0


if __name__ == "__main__":
    raise SystemExit(main())