161 lines
4.5 KiB
Bash
Executable File
161 lines
4.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# This file is part of I38.
|
|
|
|
# I38 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,
|
|
# either version 3 of the License, or (at your option) any later version.
|
|
|
|
# I38 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
# PURPOSE. See the GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License along with I38. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
if ! command -v yad &> /dev/null; then
|
|
echo "yad is required for power options."
|
|
exit 1
|
|
fi
|
|
|
|
action="$(yad --title "I38" --text "Power options" --list \
|
|
--column "Action" --column "Description" \
|
|
"Power off" "Shut down the system" \
|
|
"Reboot" "Restart the system" \
|
|
"Log out" "Exit the current session" \
|
|
--print-column=1 --separator="" --button "Select:0" --button "Cancel:1")"
|
|
|
|
yadResult=$?
|
|
if [[ $yadResult -ne 0 ]]; then
|
|
exit 0
|
|
fi
|
|
|
|
case "$action" in
|
|
"Power off")
|
|
powerAction="poweroff"
|
|
login1Method="PowerOff"
|
|
consolekitMethod="Stop"
|
|
;;
|
|
"Reboot")
|
|
powerAction="reboot"
|
|
login1Method="Reboot"
|
|
consolekitMethod="Restart"
|
|
;;
|
|
"Log out")
|
|
powerAction="logout"
|
|
;;
|
|
*)
|
|
exit 0
|
|
;;
|
|
esac
|
|
|
|
confirmAction() {
|
|
local promptText="$1"
|
|
yad --title "I38" --text "$promptText" --button "Yes:0" --button "No:1"
|
|
return $?
|
|
}
|
|
|
|
case "$powerAction" in
|
|
"poweroff")
|
|
confirmAction "Power off the system now?" || exit 0
|
|
;;
|
|
"reboot")
|
|
confirmAction "Reboot the system now?" || exit 0
|
|
;;
|
|
"logout")
|
|
confirmAction "Log out of the current session now?" || exit 0
|
|
;;
|
|
esac
|
|
|
|
try_logout() {
|
|
if [[ -n "${SWAYSOCK:-}" ]] && command -v swaymsg &> /dev/null; then
|
|
swaymsg -t command exit &> /dev/null
|
|
return $?
|
|
fi
|
|
if [[ -n "${I3SOCK:-}" ]] && command -v i3-msg &> /dev/null; then
|
|
i3-msg -t command exit &> /dev/null
|
|
return $?
|
|
fi
|
|
if command -v swaymsg &> /dev/null; then
|
|
swaymsg -t command exit &> /dev/null
|
|
return $?
|
|
fi
|
|
if command -v i3-msg &> /dev/null; then
|
|
i3-msg -t command exit &> /dev/null
|
|
return $?
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
try_loginctl() {
|
|
command -v loginctl &> /dev/null || return 1
|
|
loginctl "$powerAction" --no-ask-password &> /dev/null
|
|
}
|
|
|
|
try_systemctl() {
|
|
command -v systemctl &> /dev/null || return 1
|
|
systemctl "$powerAction" --no-ask-password &> /dev/null
|
|
}
|
|
|
|
try_gdbus_login1() {
|
|
command -v gdbus &> /dev/null || return 1
|
|
gdbus call --system \
|
|
--dest org.freedesktop.login1 \
|
|
--object-path /org/freedesktop/login1 \
|
|
--method "org.freedesktop.login1.Manager.${login1Method}" false \
|
|
&> /dev/null
|
|
}
|
|
|
|
try_gdbus_consolekit() {
|
|
command -v gdbus &> /dev/null || return 1
|
|
gdbus call --system \
|
|
--dest org.freedesktop.ConsoleKit \
|
|
--object-path /org/freedesktop/ConsoleKit/Manager \
|
|
--method "org.freedesktop.ConsoleKit.Manager.${consolekitMethod}" \
|
|
&> /dev/null
|
|
}
|
|
|
|
try_dbus_send_login1() {
|
|
command -v dbus-send &> /dev/null || return 1
|
|
dbus-send --system --print-reply \
|
|
--dest=org.freedesktop.login1 \
|
|
/org/freedesktop/login1 \
|
|
"org.freedesktop.login1.Manager.${login1Method}" \
|
|
boolean:false \
|
|
&> /dev/null
|
|
}
|
|
|
|
try_dbus_send_consolekit() {
|
|
command -v dbus-send &> /dev/null || return 1
|
|
dbus-send --system --print-reply \
|
|
--dest=org.freedesktop.ConsoleKit \
|
|
/org/freedesktop/ConsoleKit/Manager \
|
|
"org.freedesktop.ConsoleKit.Manager.${consolekitMethod}" \
|
|
&> /dev/null
|
|
}
|
|
|
|
try_shutdown() {
|
|
command -v shutdown &> /dev/null || return 1
|
|
if [[ "$powerAction" == "poweroff" ]]; then
|
|
shutdown -h now &> /dev/null
|
|
else
|
|
shutdown -r now &> /dev/null
|
|
fi
|
|
}
|
|
|
|
try_direct() {
|
|
command -v "$powerAction" &> /dev/null || return 1
|
|
"$powerAction" &> /dev/null
|
|
}
|
|
|
|
if [[ "$powerAction" == "logout" ]]; then
|
|
if try_logout; then
|
|
exit 0
|
|
fi
|
|
else
|
|
if try_loginctl || try_systemctl || try_gdbus_login1 || try_dbus_send_login1 || \
|
|
try_gdbus_consolekit || try_dbus_send_consolekit || try_shutdown || try_direct; then
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
yad --title "I38" --text "Power action failed. You may need permission or a polkit agent to continue." --button "Close:0"
|
|
exit 1
|