#!/bin/sh # Power menu with systemd / non-systemd (e.g. Gentoo+OpenRC) support. _have() { command -v "$1" >/dev/null 2>&1 } # 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 ] } # 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 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