sway: structure scripts, add helpers in separate file

This commit is contained in:
Konstantin Koslowski 2021-01-04 18:27:42 +01:00
parent 307d1dd03d
commit 9a4b96932a
5 changed files with 86 additions and 70 deletions

View file

@ -1,5 +1,11 @@
#!/bin/bash #!/bin/bash
source $HOME/.config/sway/bin/helpers
DEVICE=sysfs/backlight/intel_backlight DEVICE=sysfs/backlight/intel_backlight
APP=light
## main
_log BRIGHTNESS INFO args: $*
_check_app $APP
case $1 in case $1 in
d*) d*)
@ -9,6 +15,7 @@ case $1 in
light -s $DEVICE -A 10 light -s $DEVICE -A 10
;; ;;
*) *)
echo "ERROR: invalid command: \"$1\"" echo "invalid argument: \"$1\""
echo "arguments: [decrease|increase]"
;; ;;
esac esac

38
bin/helpers Normal file
View file

@ -0,0 +1,38 @@
#!/bin/bash
LOGFILE=$HOME/log/sway.log
## helpers
function _log() {
mkdir -p $(dirname $LOGFILE)
echo $(date) $* | tee -a $LOGFILE
}
function _check_app() {
which $1 &> /dev/null
if [ $? -ne 0 ]; then
_log _check_app: ERROR missing app \"$1\"
exit 1
fi
}
function _check_inhibit() {
if [ -e $1 ]; then
_log _check_inhibit: INFO found inhibit file $1
exit 1
fi
}
function _set_inhibit() {
file=$1
action=$2
case "$action" in
0|off)
rm -f $file
_log _set_inhibit: INFO inhibit disabled
;;
*)
touch $file
_log _set_inhibit: INFO inhibit enabled
;;
esac
}

View file

@ -1,50 +1,18 @@
#!/bin/bash #!/bin/bash
source $HOME/.config/sway/bin/helpers
IMAGE=$HOME/.lockscreen.jpg IMAGE=$HOME/.lockscreen.jpg
SCROT_APP=grim SCROT_APP=grim
SCROT_ARGS=" -t jpeg" SCROT_ARGS=" -t jpeg"
LOCK_APP=swaylock LOCK_APP=swaylock
LOCK_ARGS=" -f -c 000000 -i $IMAGE" LOCK_ARGS=" -f -c 000000 -i $IMAGE"
INHIBIT_FILE=$HOME/.inhibit INHIBIT_FILE=$HOME/.inhibit_lock
function _check_app() {
which $1 &> /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: missing app \"$1\""
exit 1
fi
}
function _check_inhibit() {
if [ -e $INHIBIT_FILE ]; then
echo "ERROR: found inhibit file $INHIBIT_FILE"
exit 1
fi
}
function _inhibit() {
case "$1" in
0|off)
rm -f $INHIBIT_FILE
echo "inhibit disabled"
;;
1|on)
touch $INHIBIT_FILE
echo "inhibit enabled"
;;
*)
echo "ERROR: unknown argument: $1"
;;
esac
}
function _clean() { function _clean() {
rm -vf $IMAGE rm -vf $IMAGE
} }
function _lock() { function _lock() {
_check_inhibit _check_inhibit $INHIBIT_FILE
_check_app $LOCK_APP
$LOCK_APP $LOCK_ARGS $LOCK_APP $LOCK_ARGS
} }
@ -57,22 +25,18 @@ function _on() {
} }
function _screenshot() { function _screenshot() {
_check_app $SCROT_APP
$SCROT_APP $SCROT_ARGS $IMAGE $SCROT_APP $SCROT_ARGS $IMAGE
convert -blur 4x4 $IMAGE $IMAGE convert -blur 4x4 $IMAGE $IMAGE
} }
function _suspend() { ## main
_check_inhibit _log LOCK INFO args: $*
systemctl suspend-then-hibernate _check_app $LOCK_APP
} _check_app $SCROT_APP
mkdir -p $HOME/log
echo "lock: $(date) args: $*" >> $HOME/log/sway.log
case "$1" in case "$1" in
inhibit) inhibit)
_inhibit $2 _set_inhibit $INHIBIT_FILE $2
;; ;;
lock) lock)
_on _on
@ -96,12 +60,9 @@ case "$1" in
_on _on
_screenshot _screenshot
;; ;;
suspend)
_suspend
;;
*) *)
echo "invalid argument \"$1\"." echo "invalid argument \"$1\""
echo "arguments: [lock|lockonly|off|on|screenshot]" echo "arguments: [inhibit [on|off]|lock|lockonly|off|on|screenshot]"
esac esac

View file

@ -1,8 +1,11 @@
#!/bin/bash #!/bin/bash
source $HOME/.config/sway/bin/helpers
BAT_PATH=/sys/class/power_supply BAT_PATH=/sys/class/power_supply
BAT=BAT0 BAT=BAT0
MODE=suspend-then-hibernate MODE=suspend-then-hibernate
INHIBIT_FILE=$HOME/.inhibit_suspend
## helpers
# 0: charging # 0: charging
# 1: discharging/unknown # 1: discharging/unknown
function _state() { function _state() {
@ -15,20 +18,23 @@ function _state() {
} }
function _suspend() { function _suspend() {
_check_inhibit $INHIBIT_FILE
state=$(_state) state=$(_state)
force=${1:-0} force=${1:-0}
echo "INFO: state: $state, force: $force"
if [ $state -eq 1 -o $force -eq 1 ]; then if [ $state -eq 1 -o $force -eq 1 ]; then
systemctl $MODE systemctl $MODE
else else
echo "ERROR: state: $state, force: $force" _log SUSPEND INFO aborting. state: $state, force: $force
fi fi
} }
mkdir -p $HOME/log ## main
echo "suspend: $(date) args: $*" >> $HOME/log/sway.log _log SUSPEND INFO args: $*
case $1 in case $1 in
inhibit)
_set_inhibit $INHIBIT_FILE $2
;;
suspend) suspend)
_suspend 0 _suspend 0
;; ;;
@ -36,7 +42,7 @@ case $1 in
_suspend 1 _suspend 1
;; ;;
*) *)
echo "ERROR: invalid command: \"$1\"" echo "invalid argument: \"$1\""
echo "possible options: [suspend|force]" echo "arguments: [inhibit [on|off]|suspend|force]"
;; ;;
esac esac

View file

@ -1,13 +1,9 @@
#!/bin/bash #!/bin/bash
sink=$(pacmd list-sinks | grep "* index" | grep -oE '[0-9]*') source $HOME/.config/sway/bin/helpers
cmd="" SINK=$(pacmd list-sinks | grep "* index" | grep -oE '[0-9]*')
app="pavucontrol" APP="pavucontrol"
if [ -z "$sink" ]; then
echo "ERROR: invalid sink: \"$sink\""
exit 1
fi
## helpers
function _toggle() { function _toggle() {
a=$1 a=$1
if [ $(pgrep $a) ]; then if [ $(pgrep $a) ]; then
@ -17,20 +13,28 @@ function _toggle() {
fi fi
} }
## main
if [ -z "$SINK" ]; then
_log VOLUME ERROR args: $*
exit 1
fi
case $1 in case $1 in
d*) d*)
pactl set-sink-volume $sink -5% pactl set-sink-volume $SINK -5%
;; ;;
i*) i*)
pactl set-sink-volume $sink +5% pactl set-sink-volume $SINK +5%
;; ;;
m*) m*)
pactl set-sink-mute $sink toggle pactl set-sink-mute $SINK toggle
;; ;;
gui) gui)
_toggle $app _check_app $APP
_toggle $APP
;; ;;
*) *)
echo "ERROR: invalid command: \"$1\"" echo "invalid command: \"$1\""
echo "arguments: [decrease|increase|mute|gui]"
;; ;;
esac esac