summaryrefslogtreecommitdiff
path: root/.local/bin/sway-power
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin/sway-power')
-rwxr-xr-x.local/bin/sway-power106
1 files changed, 87 insertions, 19 deletions
diff --git a/.local/bin/sway-power b/.local/bin/sway-power
index 31e5013..c4b2bbc 100755
--- 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