This commit is contained in:
sfigato 2022-10-21 15:52:43 +02:00
commit 47fa94eaa5
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
6 changed files with 123 additions and 0 deletions

43
Makefile Normal file
View File

@ -0,0 +1,43 @@
SUDO ?= sudo
LIB ?= /usr/lib/battery-status
SYSTEMD_PATH ?= /usr/lib/systemd/system
LIB_FILES := battery-common-lib.sh battery-low-alert.sh battery-status-toggle.sh
LIB_TARGETS := $(addprefix $(LIB)/,$(LIB_FILES))
SYSTEMD_FILES := battery-status-toggle.service battery-status-toggle.path
SYSTEMD_TARGETS := $(addprefix $(SYSTEMD_PATH)/,$(SYSTEMD_FILES))
$(LIB):
mkdir $@
$(LIB)/battery-common-lib.sh:
$(SUDO) cp ./battery-common-lib.sh $@
$(LIB)/battery-low-alert.sh:
$(SUDO) cp ./battery-low-alert.sh $@
$(LIB)/battery-status-toggle.sh:
$(SUDO) cp ./battery-status-toggle.sh $@
./battery-status-toggle.service:
PREFIX=$(SYSTEMD_PATH) ./battery-status-toggle.service.template
$(SYSTEMD_PATH)/battery-status-toggle.service: ./battery-status-toggle.service
$(SUDO) cp ./battery-status-toggle.service $@
$(SYSTEMD_PATH)/battery-status-toggle.path:
$(SUDO) cp ./battery-status-toggle.path $@
install: $(LIB_TARGETS) $(SYSTEMD_TARGETS)
$(SUDO) systemctl daemon-reload
enable: install
$(SUDO) systemctl enable --now battery-status-toggle.path
clean:
$(SUDO) rm -f $(SYSTEMD_TARGETS) $(LIB_TARGETS)
rm -f ./battery-status-toggle.service
.PHONY: install enable clean

29
battery-common-lib.sh Normal file
View File

@ -0,0 +1,29 @@
bat_files="/sys/class/power_supply/BAT0"
bat_status=$(cat ${bat_files}/status)
capacity=$(cat "${bat_files}/capacity")
function set_capacity() {
if [[ ${capacity} -lt 10 ]]; then
cap=0
elif [[ ${capacity} -lt 20 ]]; then
cap=10
elif [[ ${capacity} -lt 30 ]]; then
cap=20
elif [[ ${capacity} -lt 40 ]]; then
cap=30
elif [[ ${capacity} -lt 50 ]]; then
cap=40
elif [[ ${capacity} -lt 60 ]]; then
cap=50
elif [[ ${capacity} -lt 70 ]]; then
cap=60
elif [[ ${capacity} -lt 80 ]]; then
cap=70
elif [[ ${capacity} -lt 90 ]]; then
cap=80
elif [[ ${capacity} -lt 100 ]]; then
cap=90
else
cap=100
fi
}

17
battery-low-alert.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
source ./battery-common-lib.sh
set_capacity
if [[ "${bat_status}"=="Discharging" ]]; then
notify-send \
--icon="/usr/share/icons/Adwaita/64x64/status/battery-level-${cap}-symbolic.symbolic.png" \
"Discharging" \
"AC disconnected"
elif [[ "${bat_status}"=="Charging" ]]; then
notify-send \
--icon="/usr/share/icons/Adwaita/64x64/status/battery-level-${cap}-charged-symbolic.symbolic.png" \
"Discharging" \
"AC disconnected"
fi

View File

@ -0,0 +1,5 @@
[Unit]
Description=React to battery status change
[Path]
PathModified=/sys/class/power_supply/BAT0

View File

@ -0,0 +1,10 @@
#!/bin/bash
cat > battery-status-toggle.service << EOU
[Unit]
Description=Notify when battery status changes
[Service]
Type=oneshot
ExecStart=${PREFIX}/battery-status-toggle.sh
EOU

19
battery-status-toggle.sh Executable file
View File

@ -0,0 +1,19 @@
#!/bin/bash
source ./battery-common-lib.sh
set_capacity
if [[ ${bat_status}=="Discharging" && ${capacity} -le 10 ]]; then
echo "Battery alert - ${capacity}%"
notify-send \
--icon="/usr/share/icons/Adwaita/64x64/status/battery-level-${cap}-symbolic.symbolic.png" \
"Critical battery" \
"Only ${capacity}% battery remaining"
elif [[ ${bat_status}=="Discharging" && ${capacity} -le 20 ]]; then
echo "Battery alert - ${capacity}%"
notify-send \
--icon="/usr/share/icons/Adwaita/64x64/status/battery-level-${cap}-symbolic.symbolic.png" \
"Low battery" \
"Only ${capacity}% battery remaining"
fi