commit 028fc629b711849707cb7617b31d2b055e5875f8
parent a196d0835522cb12713588a46f3f04191463b523
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date: Fri, 18 Dec 2020 09:25:14 +0100
Implement abduco and backlight brightness commands.
Diffstat:
2 files changed, 104 insertions(+), 4 deletions(-)
diff --git a/bin/handle-nsx11-message b/bin/handle-nsx11-message
@@ -1,5 +1,5 @@
#!/bin/zsh
-setopt no_unset warn_create_global
+setopt no_unset warn_create_global extended_glob
typeset -g data terminal_exec_socket terminal_el x_container_service x_container_tmpfs
typeset -ga terminal_cmd
@@ -10,10 +10,95 @@ terminal_el="redirfd -w 1 /dev/null redirfd -w 2 /dev/null s6-sudo ${(qqq)termin
terminal_cmd=( redirfd -w 1 /dev/null redirfd -w 2 /dev/null s6-sudo $terminal_exec_socket )
terminal_env=rxvt-unicode # the TERM variable used
+nag() {
+ s6-sudo $x_container_tmpfs/run/exec/exec i3-nagbar -f fixed "$@"
+}
+
in_terminal() {
spawn-pty.py "$terminal_env" " $^@" "" "$terminal_cmd[@]"
}
+adjust-brightness() {
+ local backlight=/sys/class/backlight/intel_backlight
+ awk >$backlight/brightness \
+ -v adjustment=$1 \
+ -v max=$(<$backlight/max_brightness) \
+ -v current=$(<$backlight/brightness) \
+ 'BEGIN {
+ if(match(adjustment, /^\+[0-9]+%$/)) {
+ adjustment = substr(adjustment, 2, RLENGTH - 2)
+ adjustment = current + adjustment * 0.01 * max
+ } else if(match(adjustment, /^-[0-9]+%$/)) {
+ adjustment = substr(adjustment, 2, RLENGTH - 2)
+ adjustment = current - adjustment * 0.01 * max
+ } else if(match(adjustment, /^[0-9]+%$/)) {
+ adjustment = substr(adjustment, 1, RLENGTH - 1)
+ adjustment = adjustment * 0.01 * max
+ } else if(match(adjustment, /^\+[0-9]+$/)) {
+ adjustment = substr(adjustment, 2)
+ adjustment = current + adjustment
+ } else if(match(adjustment, /^-[0-9]+$/)) {
+ adjustment = substr(adjustment, 2)
+ adjustment = current - adjustment
+ }
+ adjustment = adjustment < 0 ? 0 : ( adjustment > max ? max : adjustment )
+ printf ("%d\n", adjustment)
+ }'
+}
+
+abduco-run() {
+ local container session cmd rest out
+ local -a socket
+ container=${1%%!*}
+ rest=${1#*!}
+
+ session=${rest%%!*}
+ if [[ $rest == *!* ]]; then
+ cmd=${rest#*!}
+ fi
+
+ if [[ $container == */* ]]; then
+ nag -m "Invalid container name: contains slashes"
+ return 1
+ fi
+
+ if [[ $session == */* ]]; then
+ nag -m "Invalid session name: contains slashes"
+ return 1
+ fi
+
+
+ socket=( /run/containers/$container/run/abduco/$USER/$session@*(=N) )
+ if (( $#socket == 0 )); then
+ if [[ -n $cmd ]]; then
+ if out=$( 2>&1 s6-sudo /run/containers/$container/run/exec/exec execlineb -c "abduco -n ${(qqq)session} $cmd" ); then
+ socket=( /run/containers/$container/run/abduco/$USER/$session@*(=N) )
+ if (( $#socket == 0 )); then
+ nag -m "Session spawned but no socket found."
+ return 1
+ fi
+ else
+ nag -m "Error spawning session (exit $?): ${(qqq)out)}"
+ return 1
+ fi
+ else
+ nag -m "Session ${(qqq)session} does not exist in ${(qqq)container} and no command provided."
+ return 1
+ fi
+ fi
+ if (( $#socket > 1 )); then
+ nag -m "More than one socket found for session ${(qqq)session} in ${(qqq)container}: ${socket:t}"
+ return 1
+ fi
+ if ! link-to-container-inbox $container xsession.$X.$USER ${socket#/run/containers/${container}}; then
+ nag -m "Failed to link abduco socket."
+ return 1
+ fi
+ if ! s6-sudo $x_container_tmpfs/run/exec/exec urxvt -e abduco -a /run/inbox/${socket#/run/containers/}; then
+ nag -m "Failed to spawn terminal for abduco session: /run/inbox/${socket#/run/containers/}"
+ fi
+}
+
# read full data from stdin, will handle binary payloads correctly unlike env
IFS= read -ru 0 -k $plumb_ndata data || return $?
@@ -23,6 +108,21 @@ case $data in
#in_terminal watch -n 1 'tree /run/containers/*.ccx/inbox'
;;
- (*) s6-sudo $x_container_tmpfs/run/exec/exec \
- i3-nagbar -t warning -f fixed -m "Powerbox got message: ${(qqq)plumb_data}"
+ (adjust-brightness!*)
+ adjust-brightness "${data#*!}"
+ case adjustment in
+ ([-+][0-9]*%)
+ ;;
+ ([0-9]*%)
+ ([-+][0-9]*%)
+ esac
+ ;;
+
+ (abduco!*!*)
+ abduco-run "${data#*!}"
+ ;;
+
+ (*)
+ nag -t warning -m "Powerbox got message: ${(qqq)plumb_data}"
+ ;;
esac
diff --git a/sbin/logincaps b/sbin/logincaps
@@ -77,7 +77,7 @@ terminal_spawn_common() {
local term_cmd term_env
term_cmd="${3#* }"
term_env=${term_cmd%% *}
- if ! [[ $term_env =~ [-.0-9a-zA-Z]* ]]; then
+ if ! [[ $term_env =~ ^[-.0-9a-zA-Z]*$ ]]; then
printf 'ERR: invalid TERM'
return 1
fi