Files
dotfiles/desktop/waybar/scripts/logout-menu.py
bullshar-k 0a88ca8787 upd
2026-10-18 21:57:19 -04:00

62 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Waybar power button: a small session menu driven by fuzzel."""
import shutil
import subprocess
import sys
def _shutdown_cmd() -> str:
"""Clean logout command for this session.
The session runs under UWSM (hyprland-uwsm). There, `hyprctl dispatch exit`
can leave SDDM on a black screen instead of logging out, while `uwsm stop`
tears the session down cleanly. Fall back to the compositor dispatcher when
UWSM is not in use.
"""
if shutil.which("uwsm"):
return "uwsm stop"
return "hyprctl dispatch exit"
ENTRIES = [
("Lock", "hyprlock"),
("Log out", _shutdown_cmd()),
("Suspend", "systemctl suspend"),
("Reboot", "systemctl reboot"),
("Shut down", "systemctl poweroff"),
]
def _notify(message: str) -> None:
if shutil.which("notify-send"):
subprocess.Popen(["notify-send", "Session menu", message])
def main() -> int:
if not shutil.which("fuzzel"):
print("fuzzel not found", file=sys.stderr)
return 1
menu = "\n".join(name for name, _ in ENTRIES) + "\n"
result = subprocess.run(
["fuzzel", "--dmenu", "--prompt", "Session: "],
input=menu,
capture_output=True,
text=True,
)
choice = result.stdout.strip()
for name, command in ENTRIES:
if name != choice:
continue
program = command.split()[0]
if not shutil.which(program):
_notify(f"{program} not found")
return 1
subprocess.Popen(["sh", "-c", command])
break
return 0
if __name__ == "__main__":
raise SystemExit(main())