Skip to content

Commit

Permalink
charge event daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
filiparag committed Feb 12, 2024
1 parent 46a4b90 commit e45f1a9
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 13 deletions.
1 change: 1 addition & 0 deletions pkglist.required.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ ufw
unp
unrar
unzip
upower
usbutils
vim
viper4linux-git
Expand Down
2 changes: 1 addition & 1 deletion src/HOME/.config/fish/conf.d/env.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 12 additions & 11 deletions src/HOME/.config/wmrc/libs/notify
Original file line number Diff line number Diff line change
Expand Up @@ -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";;
Expand Down
110 changes: 109 additions & 1 deletion src/HOME/.config/wmrc/modules/hardware/battery
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions src/HOME/.config/wmrc/rc.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e45f1a9

Please sign in to comment.