diff options
Diffstat (limited to 'dot_config/i3/scripts/executable_powermenu')
-rw-r--r-- | dot_config/i3/scripts/executable_powermenu | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/dot_config/i3/scripts/executable_powermenu b/dot_config/i3/scripts/executable_powermenu new file mode 100644 index 0000000..e72f0a2 --- /dev/null +++ b/dot_config/i3/scripts/executable_powermenu | |||
@@ -0,0 +1,186 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | # | ||
3 | # Use rofi/zenity to change system runstate thanks to systemd. | ||
4 | # | ||
5 | # Note: this currently relies on associative array support in the shell. | ||
6 | # | ||
7 | # Inspired from i3pystatus wiki: | ||
8 | # https://github.com/enkore/i3pystatus/wiki/Shutdown-Menu | ||
9 | # | ||
10 | # Copyright 2015 Benjamin Chrétien <chretien at lirmm dot fr> | ||
11 | # | ||
12 | # This program is free software: you can redistribute it and/or modify | ||
13 | # it under the terms of the GNU General Public License as published by | ||
14 | # the Free Software Foundation, either version 3 of the License, or | ||
15 | # (at your option) any later version. | ||
16 | |||
17 | # This program is distributed in the hope that it will be useful, | ||
18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
20 | # GNU General Public License for more details. | ||
21 | |||
22 | # You should have received a copy of the GNU General Public License | ||
23 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
24 | |||
25 | # modified to work with latest rofi update by joekamprad <[email protected]> | ||
26 | |||
27 | ####################################################################### | ||
28 | # BEGIN CONFIG # | ||
29 | ####################################################################### | ||
30 | |||
31 | # Use a custom lock script | ||
32 | #LOCKSCRIPT="i3lock-extra -m pixelize" | ||
33 | |||
34 | # Colors: FG (foreground), BG (background), HL (highlighted) | ||
35 | FG_COLOR="#bbbbbb" | ||
36 | BG_COLOR="#111111" | ||
37 | HLFG_COLOR="#111111" | ||
38 | HLBG_COLOR="#bbbbbb" | ||
39 | BORDER_COLOR="#222222" | ||
40 | |||
41 | # Options not related to colors (most rofi options do not work anymore) | ||
42 | ROFI_OPTIONS=(-theme ~/.config/rofi/powermenu.rasi) | ||
43 | # Zenity options | ||
44 | ZENITY_TITLE="Power Menu" | ||
45 | ZENITY_TEXT="Action:" | ||
46 | ZENITY_OPTIONS=(--column= --hide-header) | ||
47 | |||
48 | ####################################################################### | ||
49 | # END CONFIG # | ||
50 | ####################################################################### | ||
51 | |||
52 | # Whether to ask for user's confirmation | ||
53 | enable_confirmation=false | ||
54 | |||
55 | # Preferred launcher if both are available | ||
56 | preferred_launcher="rofi" | ||
57 | |||
58 | usage="$(basename "$0") [-h] [-c] [-p name] -- display a menu for shutdown, reboot, lock etc. | ||
59 | |||
60 | where: | ||
61 | -h show this help text | ||
62 | -c ask for user confirmation | ||
63 | -p preferred launcher (rofi or zenity) | ||
64 | |||
65 | This script depends on: | ||
66 | - systemd, | ||
67 | - i3, | ||
68 | - rofi or zenity." | ||
69 | |||
70 | # Check whether the user-defined launcher is valid | ||
71 | launcher_list=(rofi zenity) | ||
72 | function check_launcher() { | ||
73 | if [[ ! "${launcher_list[@]}" =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then | ||
74 | echo "Supported launchers: ${launcher_list[*]}" | ||
75 | exit 1 | ||
76 | else | ||
77 | # Get array with unique elements and preferred launcher first | ||
78 | # Note: uniq expects a sorted list, so we cannot use it | ||
79 | i=1 | ||
80 | launcher_list=($(for l in "$1" "${launcher_list[@]}"; do printf "%i %s\n" "$i" "$l"; let i+=1; done \ | ||
81 | | sort -uk2 | sort -nk1 | cut -d' ' -f2- | tr '\n' ' ')) | ||
82 | fi | ||
83 | } | ||
84 | |||
85 | # Parse CLI arguments | ||
86 | while getopts "hcp:" option; do | ||
87 | case "${option}" in | ||
88 | h) echo "${usage}" | ||
89 | exit 0 | ||
90 | ;; | ||
91 | c) enable_confirmation=true | ||
92 | ;; | ||
93 | p) preferred_launcher="${OPTARG}" | ||
94 | check_launcher "${preferred_launcher}" | ||
95 | ;; | ||
96 | *) exit 1 | ||
97 | ;; | ||
98 | esac | ||
99 | done | ||
100 | |||
101 | # Check whether a command exists | ||
102 | function command_exists() { | ||
103 | command -v "$1" &> /dev/null 2>&1 | ||
104 | } | ||
105 | |||
106 | # systemctl required | ||
107 | if ! command_exists systemctl ; then | ||
108 | exit 1 | ||
109 | fi | ||
110 | |||
111 | # menu defined as an associative array | ||
112 | typeset -A menu | ||
113 | |||
114 | # Menu with keys/commands | ||
115 | |||
116 | menu=( | ||
117 | [ Shutdown]="systemctl poweroff" | ||
118 | [ Reboot]="systemctl reboot" | ||
119 | [ Suspend]="systemctl suspend" | ||
120 | [ Hibernate]="systemctl hibernate" | ||
121 | [ Lock]="~/.config/i3/scripts/blur-lock" | ||
122 | [ Logout]="i3-msg exit" | ||
123 | [ Cancel]="" | ||
124 | ) | ||
125 | |||
126 | menu_nrows=${#menu[@]} | ||
127 | |||
128 | # Menu entries that may trigger a confirmation message | ||
129 | menu_confirm="Shutdown Reboot Hibernate Suspend Halt Logout" | ||
130 | |||
131 | launcher_exe="" | ||
132 | launcher_options="" | ||
133 | rofi_colors="" | ||
134 | |||
135 | function prepare_launcher() { | ||
136 | if [[ "$1" == "rofi" ]]; then | ||
137 | rofi_colors=(-bc "${BORDER_COLOR}" -bg "${BG_COLOR}" -fg "${FG_COLOR}" \ | ||
138 | -hlfg "${HLFG_COLOR}" -hlbg "${HLBG_COLOR}") | ||
139 | launcher_exe="rofi" | ||
140 | launcher_options=(-dmenu -i -lines "${menu_nrows}" -p "${ROFI_TEXT}" \ | ||
141 | "${rofi_colors}" "${ROFI_OPTIONS[@]}") | ||
142 | elif [[ "$1" == "zenity" ]]; then | ||
143 | launcher_exe="zenity" | ||
144 | launcher_options=(--list --title="${ZENITY_TITLE}" --text="${ZENITY_TEXT}" \ | ||
145 | "${ZENITY_OPTIONS[@]}") | ||
146 | fi | ||
147 | } | ||
148 | |||
149 | for l in "${launcher_list[@]}"; do | ||
150 | if command_exists "${l}" ; then | ||
151 | prepare_launcher "${l}" | ||
152 | break | ||
153 | fi | ||
154 | done | ||
155 | |||
156 | # No launcher available | ||
157 | if [[ -z "${launcher_exe}" ]]; then | ||
158 | exit 1 | ||
159 | fi | ||
160 | |||
161 | launcher=(${launcher_exe} "${launcher_options[@]}") | ||
162 | selection="$(printf '%s\n' "${!menu[@]}" | sort | "${launcher[@]}")" | ||
163 | |||
164 | function ask_confirmation() { | ||
165 | if [ "${launcher_exe}" == "rofi" ]; then | ||
166 | confirmed=$(echo -e "Yes\nNo" | rofi -dmenu -i -lines 2 -p "${selection}?" \ | ||
167 | "${rofi_colors}" "${ROFI_OPTIONS[@]}") | ||
168 | [ "${confirmed}" == "Yes" ] && confirmed=0 | ||
169 | elif [ "${launcher_exe}" == "zenity" ]; then | ||
170 | zenity --question --text "Are you sure you want to ${selection,,}?" | ||
171 | confirmed=$? | ||
172 | fi | ||
173 | |||
174 | if [ "${confirmed}" == 0 ]; then | ||
175 | i3-msg -q "exec ${menu[${selection}]}" | ||
176 | fi | ||
177 | } | ||
178 | |||
179 | if [[ $? -eq 0 && ! -z ${selection} ]]; then | ||
180 | if [[ "${enable_confirmation}" = true && \ | ||
181 | ${menu_confirm} =~ (^|[[:space:]])"${selection}"($|[[:space:]]) ]]; then | ||
182 | ask_confirmation | ||
183 | else | ||
184 | i3-msg -q "exec ${menu[${selection}]}" | ||
185 | fi | ||
186 | fi | ||