-
Notifications
You must be signed in to change notification settings - Fork 4
/
shutdown-dialog
executable file
·307 lines (246 loc) · 7.56 KB
/
shutdown-dialog
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
set -euo pipefail
# shutdown-dialog
# Prompt for shutdown/restart, similar to Gnome 2 poweroff dialog
usage() {
cat >&2 <<EOM
usage: $(basename "$0") OPTIONS
Print an interactive dialog for initiating shut down or reboot of the computer.
DBUS is used to send the shutdown/reboot signal, so sudo is not needed in a
typical desktop setting. This can be run as a normal user.
Options:
-h, --help Print this message
-p, --prompt Show dialog and maybe shutdown computer
-i, --install Install this script in /usr/local/bin/
--install-shortcuts Update keyboard shortcuts
For example:
$(basename "$0") --prompt
If --install-shortcuts is given, update Gnome keyboard shortcuts with the
following:
logout win + shift + q (default ctrl + alt + del)
shutdown ctrl + alt + del (no default)
dialog win + alt + del (no default)
dialog ctrl + win + del (no default)
All three are dialogs with confirmation. logout/shutdown use the built-in
gnome-session-quit, whereas dialog uses this script.
EOM
}
run() {
echo >&2 "+ $*"
"$@"
}
# Prompt for shutdown
# Prints "cancel" | "reboot" | "shutdown" to stdout
prompt_shutdown_actions() {
local ret out
out="$(
zenity --warning \
--icon-name=system-shutdown --title='' --ok-label='Cancel' \
--extra-button='Reboot' --extra-button='Shutdown' \
--text='Shut down this system now?'
)" && ret=$? || ret=$?
if [ "$ret" -eq 0 ]; then
# We relabeled the OK button as Cancel to get button order right
echo "cancel"
return
fi
if [ "$ret" -eq 1 ]; then
case "$out" in
"")
echo "cancel"
return
;;
"Reboot")
echo "reboot"
return
;;
"Shutdown")
echo "shutdown"
return
;;
*)
echo >&2 "Unexpected Zenity stdout: '$out'"
return 2
;;
esac
else
echo >&2 "Unexpected status $ret from zenity"
return 3
fi
}
dbus_reboot() {
run dbus-send --system --print-reply --dest=org.freedesktop.login1 \
/org/freedesktop/login1 "org.freedesktop.login1.Manager.Reboot" boolean:true
}
dbus_poweroff() {
run dbus-send --system --print-reply --dest=org.freedesktop.login1 \
/org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true
}
prompt_and_shutdown() {
local ans
ans="$(prompt_shutdown_actions)"
case "$ans" in
"cancel")
echo "cancel"
return
;;
"reboot")
echo "reboot"
dbus_reboot
;;
"shutdown")
echo "shutdown"
dbus_poweroff
;;
*)
echo >&2 "Unexpected ans from prompt_shutdown_actions: $ans"
return 1
esac
}
install_script() {
set -x
sudo cp -v --preserve=timestamps "$0" /usr/local/bin/
}
# usage: $0 NAME
# Find the custom hotkey in dconf with the given name
#
# Note: NAME must contain literal ' characters, as in
# find_custom_hotkey "'foo'"
#
find_custom_hotkey() {
local target_name
if [ $# -ne 1 ]; then
echo >&2 "Must pass exactly 1 desired hotkey name"
return 1
fi
target_name="$1"
local dconf_dir
dconf_dir=/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings
local id
for id in $(dconf list "$dconf_dir/"); do
id="${id%/}" # strip trailing /
name="$(dconf read "$dconf_dir/$id/name")"
if [ "$name" = "$target_name" ]; then
echo >&2 "Found existing custom hotkey id=$id name=$name"
echo "$id"
return
fi
done
# exit 0 whether we found something or not
}
# usage: dconf_write_if_needed KEY VALUE
# Read current value of KEY. If it differs from VALUE, set it, and echo that we
# did so.
#
# Note that VALUE should contain literal ' characters if it's a string, as in:
#
# dconf_write_if_needed /foo/bar/ "'string'"
#
dconf_write_if_needed() {
if [ $# -ne 2 ]; then
echo >&2 "dconf_write_if_needed takes 2 args"
return 1
fi
local key desired_val current
key="$1"
desired_val="$2"
current="$(dconf read "$key")"
if [ "$current" != "$desired_val" ]; then
run dconf write "$key" "$desired_val"
fi
}
# Usage: set_custom_hotkey NAME DEFAULT_ID BINDING COMMAND
#
# Set or update a custom hotkey using dconf
#
# NAME User-visible label for the hotkey
# DEFAULT_ID Default ID in dconf for creating new hotkey, must be unique
# BINDING Hotkeys to press
# COMMAND Command to run
#
set_custom_hotkey() {
if [ $# -ne 4 ]; then
echo >&2 "set_custom_hotkey: bad usage -- takes 4 args"
return 1
fi
local name default_id binding cmd
name="$1"
default_id="$2"
binding="$3"
cmd="$4"
# name, binding, command all get wrapped in ' since they are strings in
# dconf
name="'$name'"
binding="'$binding'"
cmd="'$cmd'"
local custom_dir=/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings
# find custom hotkey matching our target name, if any
id="$(find_custom_hotkey "$name")"
if [ -z "$id" ]; then
# If ID is empty, we're creating a new custom command
id="$default_id"
run dconf write "$custom_dir/$id/name" "$name"
# add our ID to the list as well
local schema custom_list new_list
schema=org.gnome.settings-daemon.plugins.media-keys
custom_list="$(gsettings get "$schema" custom-keybindings)"
# handle empty list
if [ "$custom_list" = '@as []' ]; then
new_list="['$custom_dir/$id/']"
else
# append
# strip trailing "]" and add ",'/newpath/']" (yes, this is horrendous)
new_list="${custom_list%]},'$custom_dir/$id/']"
fi
run gsettings set "$schema" custom-keybindings "$new_list"
fi
# get/create binding and command
dconf_write_if_needed "$custom_dir/$id/binding" "$binding"
dconf_write_if_needed "$custom_dir/$id/command" "$cmd"
}
install_shortcuts() {
# logout win + shift + q
# shutdown ctrl + alt + del
# dialog win + alt + del
# set logout, as needed
dconf_write_if_needed \
/org/gnome/settings-daemon/plugins/media-keys/logout "['<Shift><Super>q']"
# shutdown ctrl + alt + del
set_custom_hotkey 'Shutdown' 'custom-shutdown' \
'<Primary><Alt>Delete' "gnome-session-quit --power-off"
# dialog win + alt + del
set_custom_hotkey "Custom Shutdown Dialog" 'custom-agb-shutdown' \
'<Alt><Super>Delete' '/usr/local/bin/shutdown-dialog --prompt'
# dialog ctrl + win + del
set_custom_hotkey "Custom Shutdown Dialog 2" 'custom-agb-shutdown-2' \
'<Primary><Super>Delete' '/usr/local/bin/shutdown-dialog --prompt'
echo "All done."
if [ ! -x /usr/local/bin/shutdown-dialog ]; then
echo >&2 "WARNING: /usr/local/bin/shutdown-dialog does not exist or not executable"
echo >&2 "May need to call this script with --install"
fi
}
if [ $# -ne 1 ]; then
usage
exit 1
fi
case "$1" in
-h|--help)
usage
exit
;;
-p|--prompt)
prompt_and_shutdown
;;
-i|--install)
install_script
;;
--install-shortcuts)
install_shortcuts
;;
*)
echo >&2 "Invalid option $1"
usage
exit
;;
esac