summaryrefslogtreecommitdiff
path: root/.local/bin/sway-power
blob: c4b2bbcd3873608c08bdd600b25d3ec865c5c63b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/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