changeset 7:dc00601519cd

power: rework using dmenu.subr
author David Demelier <markand@malikania.fr>
date Tue, 07 Nov 2017 11:54:31 +0100
parents c92f87d3d3d7
children e1e0929d3189
files dmenu_power dmenu_shutdown
diffstat 2 files changed, 174 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dmenu_power	Tue Nov 07 11:54:31 2017 +0100
@@ -0,0 +1,174 @@
+#!/bin/sh
+#
+# dmenu_power -- dmenu prompt for system shutdown
+#
+# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+if [ -r dmenu.subr ]; then
+    . ./dmenu.subr
+fi
+if [ -r /usr/local/libexec/dmenutools/dmenu.subr ]; then
+    . /usr/local/libexec/dmenutools/dmenu.subr
+fi
+
+# Default user values.
+: "${power_hibernate_enable:=1}"
+: "${power_reboot_enable:=1}"
+: "${power_shutdown_enable:=1}"
+: "${power_suspend_enable:=1}"
+
+os=$(uname -s)
+
+# Check for systemctl on Linux.
+if command -v systemctl > /dev/null 2>&1; then
+    has_systemctl=1
+fi
+
+#
+# Create a list of commands.
+# -------------------------------------------------------------------
+#
+dmt_power_list()
+{
+    for i in hibernate reboot shutdown suspend; do
+        eval enabled="\${power_${i}_enable}"
+
+        if [ ${enabled} -eq 1 ]; then
+            list="${list}$i\n"
+        fi
+    done
+
+    echo -e $(echo ${list} | sed -e s'/\\n$//')
+}
+
+#
+# Do hibernation
+# -------------------------------------------------------------------
+#
+# Supports: Linux, OpenBSD
+#
+dmt_power_hibernate()
+{
+    if [ -n "${power_hibernate_cmd}" ]; then
+        ${power_hibernate_cmd}
+    elif [ "${has_systemctl}" -eq 1 ]; then
+        systemctl hibernate
+    else
+        case "${os}" in
+        Linux)
+            echo 'disk' | sudo -A tee -a /sys/power/state
+            ;;
+        OpenBSD)
+            ZZZ
+            ;;
+        *)
+            dmt_fatal "hibernation not supported on this system"
+            ;;
+        esac
+    fi
+}
+
+#
+# Do reboot
+# -------------------------------------------------------------------
+#
+# Supports: all
+#
+dmt_power_reboot()
+{
+    if [ -n "${power_reboot_cmd}" ]; then
+        ${power_reboot_cmd}
+    elif [ "${has_systemctl}" -eq 1 ]; then
+        systemctl reboot
+    else
+        sudo -A shutdown -r now
+    fi
+}
+
+#
+# Do shutdown
+# -------------------------------------------------------------------
+#
+# Supports: all
+#
+dmt_power_shutdown()
+{
+    if [ -n "${power_shutdown_cmd}" ]; then
+        ${power_shutdown_cmd}
+    elif [ "${has_systemctl}" -eq 1 ]; then
+        systemctl poweroff
+    else
+        case ${os} in
+        Linux)
+            sudo -A shutdown -h now
+            ;;
+        *)
+            sudo -A shutdown -p now
+            ;;
+        esac
+    fi
+}
+
+#
+# Do suspend
+# -------------------------------------------------------------------
+#
+# Supports: Linux, OpenBSD, FreeBSD (if has not been broken in a new release).
+#
+dmt_power_suspend()
+{
+    if [ -n "${power_suspend_cmd}" ]; then
+        ${power_suspend_cmd}
+    elif [ "${has_systemctl}" -eq 1 ]; then
+        systemctl suspend
+    else
+        case "${os}" in
+        Linux)
+            echo 'mem' | sudo -A tee -a /sys/power/state
+            ;;
+        FreeBSD)
+            # Good luck.
+            sudo -A acpiconf -s 3
+            ;;
+        OpenBSD)
+            zzz
+            ;;
+        *)
+            dmt_fatal "suspend not supported on this system"
+            ;;
+        esac
+    fi
+}
+
+cmd=$(dmt_power_list | dmenu -p "power")
+
+# TODO: change this with a substitution.
+case "${cmd}" in
+hibernate)
+    dmt_power_hibernate
+    ;;
+reboot)
+    dmt_power_reboot
+    ;;
+shutdown)
+    dmt_power_shutdown
+    ;;
+suspend)
+    dmt_power_suspend
+    ;;
+esac
+
+# vim: syntax=sh:
--- a/dmenu_shutdown	Tue Nov 07 11:07:27 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-#!/bin/sh
-#
-# dmenu_shutdown -- dmenu prompt for system shutdown
-#
-# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
-#
-# Permission to use, copy, modify, and/or distribute this software for any
-# purpose with or without fee is hereby granted, provided that the above
-# copyright notice and this permission notice appear in all copies.
-#
-# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-#
-
-if ! which dmenu > /dev/null 2>&1; then
-    echo "dmenu is missing" 1>&2;
-    exit 1;
-fi
-
-os=$(uname -s)
-cmd=$(echo -e "shutdown\nsuspend\nreboot" | dmenu -p "do")
-
-if [ "${cmd}" = "shutdown" ]; then
-    case "${os}" in
-    Linux)
-        sudo shutdown -h now
-        ;;
-    FreeBSD)
-        sudo shutdown -p now
-        ;;
-    esac
-elif [ "${cmd}" = "suspend" ]; then
-    case "${os}" in
-    Linux)
-        sudo pm-suspend
-        ;;
-    FreeBSD)
-        sudo acpiconf -s 3
-        ;;
-    esac
-elif [ "${cmd}" = "reboot" ]; then
-    sudo shutdown -r now
-fi