refactor: refactored battery bash scripts into a C++ daemon

This commit is contained in:
devaine 2025-07-29 15:05:52 -05:00
commit 16690836d6
Signed by untrusted user who does not match committer: devaine
GPG key ID: 954B1DCAC6FF84EE
10 changed files with 215 additions and 2 deletions

View file

@ -1,75 +0,0 @@
#!/bin/bash
# Constants (access to display)
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
# Find out which battery is the laptop's battery
BATTERY=$(acpi | grep -vwE "(unavailable)" | grep -o '[0-9]' | head -n 1)
# Daemon Constants
WARNING_PERCENT=35
FULL_PERCENT=98
BAT_PERCENT=$(acpi -b | grep "Battery $BATTERY" | head -n 1 | grep -P -o "[0-9]+(?=%)")
DISCHARGING_COUNT=$(acpi -b | grep "Battery $BATTERY" | grep -c "Discharging")
NOTCHARGING_COUNT=$(acpi -b | grep "Battery $BATTERY" | grep "until" | grep -c "Not Charging")
## Conditioning Files
LOW_BAT_FILE=/tmp/battery_low
FULL_BAT_FILE=/tmp/battery_full
# Functions
battery-full() {
notify-send "Battery Full" "Battery is fully charged!" -i "battery-full-charged" -r 9991 -t 5000
touch $FULL_BAT_FILE
rm -rf $LOW_BAT_FILE
}
battery-low() {
notify-send "Low Battery" "${BAT_PERCENT}% of battery remaining." -u critical -i "battery-caution" -r 9991
touch $LOW_BAT_FILE
rm -rf $FULL_BAT_FILE
}
battery-normal() {
if [ "$1" == "Normal" ]; then
rm $FULL_BAT_FILE
elif [ "$1" == "Charging" ]; then
rm $LOW_BAT_FILE
fi
}
# IF the batttery is discharging AND there is a FULL_BAT_FILE exists AND battery is less than 99%
if [ "$BAT_PERCENT" -lt $FULL_PERCENT ] && [ "$DISCHARGING_COUNT" -eq 1 ] && [ -f $FULL_BAT_FILE ]; then
#echo "hit normal + full file exists + less than full %"
battery-normal "Normal"
# If charging, AND less than full percentage AND LOW_BAT_FILE exists:
elif [ "$BAT_PERCENT" -lt $FULL_PERCENT ] && [ "$DISCHARGING_COUNT" -eq 0 ] && [ -f "$EMPTY_BAT_FILE" ]; then
#echo "hit normal + warning file exists + more than warning %"
battery-normal "Charging"
fi
# IF the battery is charging AND is full (+ hasn't show any notif yet):
if [ "$BAT_PERCENT" -ge $FULL_PERCENT ] && [ ! -f $FULL_BAT_FILE ] && [ "$DISCHARGING_COUNT" = 0 ]; then
#echo "hit full + charging"
battery-full
# Another condition, battery could be full, yet it won't be charging:
elif [ "$BAT_PERCENT" -ge $FULL_PERCENT ] && [ "$NOTCHARGING_COUNT" = 1 ] && [ ! -f $LOW_BAT_FILE ]; then
#echo "hit full + not charging"
battery-full
# IF the battery is low and it's discharging (+ hasn't shown any notif yet):
elif [ "$BAT_PERCENT" -le $WARNING_PERCENT ] && [ "$DISCHARGING_COUNT" -eq 1 ] && [ ! -f $LOW_BAT_FILE ]; then
#echo "hit low + discharging"
battery-low
# If LOW_BAT_FILE Exists, Spam Until It's Charging
elif [ "$BAT_PERCENT" -le $WARNING_PERCENT ] && [ "$DISCHARGING_COUNT" -eq 1 ] && [ -f $LOW_BAT_FILE ]; then
notify-send "Low Battery" "${BAT_PERCENT}% of battery remaining." -u critical -i "battery-caution" -r 9991
fi
# Debug commands
#echo "$BAT_PERCENT is the battery percent"
#echo "$DISCHARGING_COUNT -- 1 = discharging, 0 = charging"

View file

@ -1,11 +0,0 @@
#!/bin/bash
BATTERY=$(acpi | grep -vwE "(unavailable)" | grep -o '[0-9]' | head -n 1)
BAT_PERCENT=$(acpi -b | grep "Battery $BATTERY" | grep -P -o "[0-9]+(?=%)")
DISCHARGING_COUNT=$(acpi -b | grep "Battery $BATTERY" | grep -E "until|remaining" | grep -c "Discharging")
NOTCHARGING_COUNT=$(acpi -b | grep "Battery $BATTERY" | grep -c "Not Charging")
if [[ $DISCHARGING_COUNT -eq 0 ]] || [[ $NOTCHARGING_COUNT -eq 1 ]]; then
dunstify "Battery Status" "Battery is currently charging at ${BAT_PERCENT}%" -i "battery-level-50-charging-symbolic" -r 9991 -t 5000
else
dunstify "Battery Status" "Battery is currently discharging at ${BAT_PERCENT}%" -i "battery-level-50-symbolic" -r 9991 -t 5000
fi

View file

@ -11,6 +11,10 @@ bright_dec() {
}
keybind-func() {
echo "$CURRENT_BRIGHTNESS"
}
while test $# -gt 0; do
case "$1" in
inc)
@ -20,5 +24,10 @@ while test $# -gt 0; do
dec)
bright_dec && exit 0
;;
keybind)
keybind-func && exit 0
;;
esac
done

View file

@ -1,28 +0,0 @@
#!/bin/bash
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
# Find out which battery is the laptop's battery
BATTERY=$(acpi | grep -vwE "(unavailable)" | grep -o '[0-9]' | head -n 1)
# 1 = Charging
# 0 = Discharging
BAT_CHARGING=$1
# Filtering one more time to find the percentage of the laptop's battery level
BAT_LEVEL=$(acpi -b | grep "Battery $BATTERY" | grep -P -o '[0-9]+(?=%)')
CHARGE_FILE=/tmp/laptop-charging
DISCHARGE_FILE=/tmp/laptop-discharging
# Notification handling
if [ "$BAT_CHARGING" -eq 1 ] && [ ! -f $CHARGE_FILE ]; then
rm $DISCHARGE_FILE
touch $CHARGE_FILE
/usr/bin/notify-send "Charging" "Charging battery at ${BAT_LEVEL}%" -u low -i "battery-level-50-charging-symbolic" -t 5000 -r 9991
elif [ "$BAT_CHARGING" -eq 0 ] && [ ! -f $DISCHARGE_FILE ]; then
rm $CHARGE_FILE
touch $DISCHARGE_FILE
/usr/bin/notify-send "Discharging" "${BAT_LEVEL}% remaining" -u low -i "battery-level-70-symbolic" -t 5000 -r 9991
fi

15
notifs/info Executable file
View file

@ -0,0 +1,15 @@
#!/bin/bash
# Enter scripts directory
cd ~/.scripts/notifs || exit
# Constants
CURRENT_VOL=$(./volume keybind | grep -o '^[^ ]*')
CURRENT_MIC_VOL=$(./volume keybind | awk '{ print $2 }')
BRIGHTNESS=$(./brightness keybind)
# Variables for display, basically multiplies floats to integers
DISPLAY_VOL=$(awk '{ print $1 * $2 }' <<<"${CURRENT_VOL} 100")
DISPLAY_MIC_VOL=$(awk '{ print $1 * $2 }' <<<"${CURRENT_MIC_VOL} 100")
dunstify "All other info:" "Brightness: $BRIGHTNESS%\nCurrent Volume: $DISPLAY_VOL%\nCurrent Mic. Volume: $DISPLAY_MIC_VOL%"

View file

@ -92,6 +92,10 @@ mic_mute_toggle() {
fi
}
keybind-func() {
echo "$CURRENT_VOL" "$CURRENT_MIC_VOL"
}
while test $# -gt 0; do
case "$1" in
inc)
@ -118,5 +122,9 @@ while test $# -gt 0; do
mic_mute_toggle && exit 0
;;
keybind)
keybind-func && exit 0
;;
esac
done

3
notifs/window Executable file
View file

@ -0,0 +1,3 @@
#!/bin/bash
dunstify "Switching Window" "Current Window: $1" -t 1000 -r 5000
swaymsg workspace number "$1"