dotfiles

dotfiles - ryukamish edition
Log | Files | Refs | README

sway-power (2002B)


      1 #!/bin/sh
      2 # Power menu with systemd / non-systemd (e.g. Gentoo+OpenRC) support.
      3 
      4 _have() {
      5   command -v "$1" >/dev/null 2>&1
      6 }
      7 
      8 # Canonical check: systemd sets this up when it's running as PID 1,
      9 # regardless of distro. Works even if systemctl happens to be installed
     10 # but isn't actually managing the session (e.g. some Gentoo setups).
     11 _is_systemd() {
     12   [ -d /run/systemd/system ]
     13 }
     14 
     15 # elogind provides a logind-compatible loginctl on non-systemd distros
     16 # (common on Gentoo/OpenRC boxes running Wayland compositors).
     17 _have_elogind() {
     18   _have loginctl && [ -d /run/systemd/seats ] 2>/dev/null || _have elogind-loginctl
     19 }
     20 
     21 do_lock() {
     22   _have swaylock && swaylock --daemonize
     23 }
     24 
     25 do_logout() {
     26   if _is_systemd; then
     27     loginctl terminate-session self
     28   elif _have loginctl; then
     29     # elogind case
     30     loginctl terminate-session self
     31   elif [ -n "$SWAYSOCK" ] && _have swaymsg; then
     32     swaymsg exit
     33   else
     34     # last resort: kill the user's session
     35     pkill -KILL -u "$(whoami)"
     36   fi
     37 }
     38 
     39 do_reboot() {
     40   if _is_systemd; then
     41     systemctl reboot
     42   elif _have loginctl; then
     43     loginctl reboot
     44   elif _have doas; then
     45     doas reboot
     46   else
     47     sudo reboot
     48   fi
     49 }
     50 
     51 do_shutdown() {
     52   if _is_systemd; then
     53     systemctl poweroff
     54   elif _have loginctl; then
     55     loginctl poweroff
     56   elif _have doas; then
     57     doas poweroff
     58   else
     59     sudo poweroff
     60   fi
     61 }
     62 
     63 do_suspend() {
     64   if _is_systemd; then
     65     systemctl suspend
     66   elif _have loginctl; then
     67     loginctl suspend
     68   elif _have doas; then
     69     doas zzz
     70   else
     71     sudo zzz
     72   fi
     73 }
     74 
     75 do_hibernate() {
     76   if _is_systemd; then
     77     systemctl hibernate
     78   elif _have loginctl; then
     79     loginctl hibernate
     80   elif _have doas; then
     81     doas ZZZ
     82   else
     83     sudo ZZZ
     84   fi
     85 }
     86 
     87 PWR_OPTS=$(printf '%b' 'Lock\nLogout\nReboot\nShutdown\nSuspend\nHibernate' | fuzzel -d -p 'Power menu: ')
     88 
     89 case "$PWR_OPTS" in
     90   Lock)      do_lock ;;
     91   Logout)    do_logout ;;
     92   Reboot)    do_reboot ;;
     93   Shutdown)  do_shutdown ;;
     94   Suspend)   do_suspend ;;
     95   Hibernate) do_hibernate ;;
     96 esac