From e45f1a9dc71ce9cecc685a981f93247f0126469e Mon Sep 17 00:00:00 2001 From: Filip Parag Date: Mon, 12 Feb 2024 22:00:27 +0100 Subject: [PATCH] charge event daemon --- pkglist.required.txt | 1 + src/HOME/.config/fish/conf.d/env.fish | 2 +- src/HOME/.config/wmrc/libs/notify | 23 ++-- .../.config/wmrc/modules/hardware/battery | 110 +++++++++++++++++- src/HOME/.config/wmrc/rc.conf | 1 + 5 files changed, 124 insertions(+), 13 deletions(-) diff --git a/pkglist.required.txt b/pkglist.required.txt index 74b0a70..e354f31 100644 --- a/pkglist.required.txt +++ b/pkglist.required.txt @@ -88,6 +88,7 @@ ufw unp unrar unzip +upower usbutils vim viper4linux-git diff --git a/src/HOME/.config/fish/conf.d/env.fish b/src/HOME/.config/fish/conf.d/env.fish index 7be47ad..3f26eb2 100644 --- a/src/HOME/.config/fish/conf.d/env.fish +++ b/src/HOME/.config/fish/conf.d/env.fish @@ -39,4 +39,4 @@ set -x DEBUGINFOD_URLS 'https://debuginfod.archlinux.org' set -x MOZ_USE_XINPUT2 1 # Quartus -set -g -x PATH "$PATH:/opt/intelFPGA/21.1/quartus/bin" +set -g -x PATH "$PATH:/opt/intelFPGA/23.1/quartus/bin" diff --git a/src/HOME/.config/wmrc/libs/notify b/src/HOME/.config/wmrc/libs/notify index 615ef2f..08e658b 100644 --- a/src/HOME/.config/wmrc/libs/notify +++ b/src/HOME/.config/wmrc/libs/notify @@ -6,17 +6,18 @@ export _module PROMPT_DEFAULT_EXPIRATION=300000 #milliseconds notify() { - _log_level="" - _actions="" - _app_name="" - _action="" - _urgency="" - _expire_time="" - _icon="" - _category="" - _replace_id="" - _print_id="" - _wait="" + _log_level='' + _actions='' + _app_name='' + _action='' + _urgency='' + _expire_time='' + _icon='' + _category='' + _replace_id='' + _print_id='' + _wait='' + OPTIND='' while getopts l:a:A:u:t:i:c:r:pw arg; do case "$arg" in l) _log_level="$OPTARG";; diff --git a/src/HOME/.config/wmrc/modules/hardware/battery b/src/HOME/.config/wmrc/modules/hardware/battery index 8235038..13df692 100644 --- a/src/HOME/.config/wmrc/modules/hardware/battery +++ b/src/HOME/.config/wmrc/modules/hardware/battery @@ -1,10 +1,11 @@ #!/bin/sh -export WMRC_DEPENDENCIES='sudo' +export WMRC_DEPENDENCIES='sudo awk upower stdbuf' export WMRC_LIBRARIES='notify' WMRC_BATTERY_CHARGE_START="${WMRC_BATTERY_CHARGE_START:-80}" WMRC_BATTERY_CHARGE_STOP="${WMRC_BATTERY_CHARGE_STOP:-85}" +WMRC_BATTERY_STATE_TIMEOUT="${WMRC_BATTERY_STATE_TIMEOUT:-1500}" #milliseconds test_battery() { if ! find /sys/class/power_supply/ -type l -name 'BAT*' | grep -qv '^$'; then @@ -36,3 +37,110 @@ full() { notify -u low -i battery-full-charging 'Full battery charging' \ "Start threshold: 100%\nStop threshold: 100%" } + +start() { + if ! upower --enumerate | grep -qi 'battery'; then + warn "Battery not present, exiting daemon!" + return 1 + fi + if daemon_get_pid; then + error "Charge event daemon is already running!" + return 1 + fi + daemon & + daemon_set_pid "$!" +} + +daemon() { + previous_state='' + previous_online='' + stdbuf -oL upower --monitor | + while IFS= read -r line + do + type="$( + echo "$line" | awk \ + '$2 == "device" { + type = $4; + sub(/.*\//,"",type); + sub(/_[^_]*$/,"",type); + print type; + }' + )" + device="$( + echo "$line" | awk \ + '$2 == "device" { + print $4; + }' + )" + if [ -z "$type" ] || [ -z "$device" ]; then + continue + fi + if [ "$type" = 'battery' ]; then + state="$( + upower --show-info "$device" | awk \ + '$1 == "state:" { print $2; }' + )" + if [ "$previous_state" != "$state" ]; then + info "Battery state changed" "$state" + notify_battery_state "$state" + fi + previous_state="$state" + elif [ "$type" = 'line_power' ]; then + online="$( + upower --show-info "$device" | awk \ + '$1 == "online:" { print $2; }' + )" + if [ "$previous_online" != "$online" ]; then + info "Charger online status changed" "$online" + notify_line_power_state "$online" + fi + previous_online="$online" + fi + done +} + +notify_battery_state() { + case "$1" in + 'unknown') + notify -u low -i battery-caution \ + -t "$WMRC_BATTERY_STATE_TIMEOUT" 'Battery state unknown' + ;; + 'charging') + notify -u low -i battery-medium-charging \ + -t "$WMRC_BATTERY_STATE_TIMEOUT" 'Battery charging' + ;; + 'discharging') + notify -u low -i battery-low \ + -t "$WMRC_BATTERY_STATE_TIMEOUT" 'Battery discharging' + ;; + 'empty') + notify -u low -i battery-empty \ + -t "$WMRC_BATTERY_STATE_TIMEOUT" 'Battery empty' + ;; + 'fully-charged') + notify -u low -i battery-full \ + -t "$WMRC_BATTERY_STATE_TIMEOUT" 'Battery fully charged' + ;; + 'pending-charge') + notify -u low -i battery-good \ + -t "$WMRC_BATTERY_STATE_TIMEOUT" 'Battery pending charge' + ;; + 'pending-discharge') + notify -u low -i battery-good \ + -t "$WMRC_BATTERY_STATE_TIMEOUT" 'Battery pending discharge' + ;; + esac +} + +notify_line_power_state() { + case "$1" in + 'yes') + notify -u low -i ac-adapter \ + -t "$WMRC_BATTERY_STATE_TIMEOUT" 'Charger connected' + ;; + 'no') + notify -u low -i ac-adapter \ + -t "$WMRC_BATTERY_STATE_TIMEOUT" 'Charger disconnected' + ;; + esac +} diff --git a/src/HOME/.config/wmrc/rc.conf b/src/HOME/.config/wmrc/rc.conf index 30cac58..680b289 100644 --- a/src/HOME/.config/wmrc/rc.conf +++ b/src/HOME/.config/wmrc/rc.conf @@ -13,6 +13,7 @@ screen/layout::defined('Default') wait wm/bspwm wait hardware/power hardware/battery::limit +hardware/battery::start hardware/audio screen/backlight screen/wallpaper_dynamic::start