rofi/scripts/rofi-power 3.0 K raw
1
#!/bin/sh
2
# Requires: rofi, and either xfce4-session or elogind (loginctl)
3
# Power menu, shared between the XFCE session and oxwm. Under XFCE the
4
# session manager owns these actions: it asks open applications to close and
5
# saves the session before handing the request on, which is what Ctrl+Alt+Del
6
# does. Under oxwm there is no session manager, so elogind is asked directly
7
# and doas is the backstop for a session it does not own. Every action asks
8
# for confirmation first, defaulting to no — this menu sits a keystroke away
9
# from the launcher.
10
set -u
11
12
# xfce4-session-logout's per-action flags skip its own dialog on their own;
13
# --fast is deliberately not passed, because that means "do not save the
14
# session" rather than "do not ask".
15
session=""
16
if [ "${XDG_CURRENT_DESKTOP:-}" = "XFCE" ] && command -v xfce4-session-logout >/dev/null 2>&1; then
17
    session=xfce4-session-logout
18
fi
19
20
# Only the lockers that are actually installed get offered. None of these
21
# ship with the system: `doas apk add slock` (or i3lock) to get a lock entry.
22
# xflock4 is not a locker itself — it asks xfce4-session to lock, which needs
23
# one of these installed all the same — but going through it means the
24
# session's own lock settings apply.
25
locker=""
26
for candidate in slock i3lock xsecurelock physlock xscreensaver light-locker; do
27
    if command -v "$candidate" >/dev/null 2>&1; then
28
        locker=$candidate
29
        break
30
    fi
31
done
32
if [ -n "$locker" ] && [ -n "$session" ] && command -v xflock4 >/dev/null 2>&1; then
33
    locker=xflock4
34
fi
35
36
menu() { rofi -dmenu -i -no-custom "$@"; }
37
38
confirm() {
39
    answer=$(printf 'no\nyes\n' | menu -p "$1?" -mesg "This will $2." -l 2) || exit 0
40
    [ "$answer" = "yes" ]
41
}
42
43
# The session manager when there is one, then loginctl, then doas.
44
#   $1 xfce4-session-logout flag   $2 loginctl verb   $3 doas fallback
45
power() {
46
    if [ -n "$session" ]; then
47
        "$session" "$1"
48
    else
49
        loginctl "$2" 2>/dev/null || doas "$3"
50
    fi
51
}
52
53
entries="󰤄  suspend
54
󰜉  reboot
55
󰐥  shutdown
56
󰍃  log out"
57
[ -n "$locker" ] && entries="󰌾  lock
58
$entries"
59
60
CHOICE=$(printf '%s\n' "$entries" | menu -p "power" -l 5) || exit 0
61
[ -n "$CHOICE" ] || exit 0
62
63
case ${CHOICE##*  } in
64
    lock)     exec "$locker" ;;
65
    suspend)  confirm "Suspend" "put the machine to sleep" && power --suspend suspend zzz ;;
66
    reboot)   confirm "Reboot" "close every open window and restart" && power --reboot reboot reboot ;;
67
    shutdown) confirm "Shut down" "close every open window and power off" && power --halt poweroff poweroff ;;
68
    "log out")
69
        confirm "Log out" "end the session and close every window" || exit 0
70
        if [ -n "$session" ]; then
71
            "$session" --logout
72
        else
73
            # Ending the session takes the whole X server with it, which is
74
            # what logging out means here; killing oxwm alone would leave X
75
            # running.
76
            loginctl terminate-session "${XDG_SESSION_ID:-}" 2>/dev/null ||
77
                pkill -u "$USER" -x oxwm
78
        fi
79
        ;;
80
esac