aboutsummaryrefslogtreecommitdiffstats
path: root/hyprsunset_qt/daemon.py
blob: 7bf0e47b951a5ba6aa38ee85877faccd6e909489 (plain)
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
38
39
40
41
42
43
44
45
"""Control the hyprsunset daemon: status, restart, live preview via hyprctl."""

from __future__ import annotations

import shlex
import subprocess
from typing import Optional


def is_running() -> bool:
    return subprocess.run(
        ["pgrep", "-x", "hyprsunset"],
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
    ).returncode == 0


def live_preview(
    temperature: Optional[int] = None,
    gamma: Optional[float] = None,
    identity: bool = False,
) -> None:
    """Push values to the running daemon over IPC. No file write, no restart."""
    if identity:
        subprocess.run(["hyprctl", "hyprsunset", "identity"], check=False)
        return
    if temperature is not None:
        subprocess.run(
            ["hyprctl", "hyprsunset", "temperature", str(temperature)],
            check=False,
        )
    if gamma is not None:
        # IPC gamma is integer percent; profile gamma is a 0-2 float.
        pct = str(round(gamma * 100))
        subprocess.run(["hyprctl", "hyprsunset", "gamma", pct], check=False)


def restart(command: str = "hyprsunset") -> None:
    subprocess.run(["pkill", "hyprsunset"], check=False)
    subprocess.Popen(
        shlex.split(command),
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
        start_new_session=True,
    )