commit 6b50cd0f1aed0d48ea4f57b4a11beb1249c5a282 parent a1a9dcb1617f595f3f5ddfe728bb5cf03f172153 Author: ryukamish <[email protected]> Date: Sun, 19 Jul 2026 23:54:44 +0530 support for different distros which are systemd & other init systems Diffstat:
| M | .local/bin/sway-power | | | 106 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- |
1 file changed, 87 insertions(+), 19 deletions(-)
diff --git a/.local/bin/sway-power b/.local/bin/sway-power @@ -1,28 +1,96 @@ #!/bin/sh +# Power menu with systemd / non-systemd (e.g. Gentoo+OpenRC) support. _have() { - command -v "$1" + command -v "$1" >/dev/null 2>&1 } -PWR_OPTS=$(printf '%b' 'Lock\nLogout\nReboot\nShutdown\nSuspend\nHibernate' | fuzzel -d -p 'Power menu: ') +# Canonical check: systemd sets this up when it's running as PID 1, +# regardless of distro. Works even if systemctl happens to be installed +# but isn't actually managing the session (e.g. some Gentoo setups). +_is_systemd() { + [ -d /run/systemd/system ] +} -case "$PWR_OPTS" in - Hibernate) - loginctl hibernate - ;; - Lock) - _have swaylock && swaylock --daemonize - ;; - Logout) - loginctl terminate-session self - ;; - Reboot) - loginctl reboot - ;; - Shutdown) +# elogind provides a logind-compatible loginctl on non-systemd distros +# (common on Gentoo/OpenRC boxes running Wayland compositors). +_have_elogind() { + _have loginctl && [ -d /run/systemd/seats ] 2>/dev/null || _have elogind-loginctl +} + +do_lock() { + _have swaylock && swaylock --daemonize +} + +do_logout() { + if _is_systemd; then + loginctl terminate-session self + elif _have loginctl; then + # elogind case + loginctl terminate-session self + elif [ -n "$SWAYSOCK" ] && _have swaymsg; then + swaymsg exit + else + # last resort: kill the user's session + pkill -KILL -u "$(whoami)" + fi +} + +do_reboot() { + if _is_systemd; then + systemctl reboot + elif _have loginctl; then + loginctl reboot + elif _have doas; then + doas reboot + else + sudo reboot + fi +} + +do_shutdown() { + if _is_systemd; then + systemctl poweroff + elif _have loginctl; then loginctl poweroff - ;; - Suspend) + elif _have doas; then + doas poweroff + else + sudo poweroff + fi +} + +do_suspend() { + if _is_systemd; then + systemctl suspend + elif _have loginctl; then loginctl suspend - ;; + elif _have doas; then + doas zzz + else + sudo zzz + fi +} + +do_hibernate() { + if _is_systemd; then + systemctl hibernate + elif _have loginctl; then + loginctl hibernate + elif _have doas; then + doas ZZZ + else + sudo ZZZ + fi +} + +PWR_OPTS=$(printf '%b' 'Lock\nLogout\nReboot\nShutdown\nSuspend\nHibernate' | fuzzel -d -p 'Power menu: ') + +case "$PWR_OPTS" in + Lock) do_lock ;; + Logout) do_logout ;; + Reboot) do_reboot ;; + Shutdown) do_shutdown ;; + Suspend) do_suspend ;; + Hibernate) do_hibernate ;; esac