From 782257820c15514a4e2f20bc8aeeab437b7ba118 Mon Sep 17 00:00:00 2001 From: coolnsx Date: Sun, 31 May 2026 17:35:29 +0530 Subject: [PATCH] The universe is possible --- hypr/battery | 70 ++++++++++++---------------- hypr/hyprland.conf | 1 - hypr/ntfy_sub | 4 +- hypr/powersupply_notification.rules | 6 +++ readme.md | 3 ++ systemd/user/monitor-battery.service | 9 ---- 6 files changed, 40 insertions(+), 53 deletions(-) create mode 100644 hypr/powersupply_notification.rules delete mode 100644 systemd/user/monitor-battery.service diff --git a/hypr/battery b/hypr/battery index a7f5da5..ecf5b53 100755 --- a/hypr/battery +++ b/hypr/battery @@ -4,37 +4,52 @@ notify() { notify-send -e -t 5000 -i "${icon_dir}${1}" -h "string:x-canonical-private-synchronous:${0##*/}" "$2" -u "${3:-normal}" } -print_sleep() { - [ -n "$4" ] && printf "Status: %s, Charge: %s,Next Check in %s seconds\n" "$3" "$2" "$1" - sleep "$1" -} - switch_to_ac_mode() { - charge_icon=$(printf "%03d" "$((charge / 10))0") hyprctl keyword monitor 'eDP-1',preferred,auto,1.5,bitdepth,10 hyprctl keyword decoration:blur:enabled true hyprctl keyword decoration:shadow:enabled true notify "battery-$charge_icon-charging.svg" "AC Mode ($charge%, Charging)" - exit 0 } switch_to_battery_mode() { - charge_icon=$(printf "%03d" "$((charge / 10))0") hyprctl keyword monitor 'eDP-1',$battery_resolution,auto,1.5,bitdepth,10 hyprctl keyword decoration:blur:enabled false hyprctl keyword decoration:shadow:enabled false notify "battery-$charge_icon.svg" "Battery Mode ($charge%, Discharging)" - exit 0 } + +check_battery_status() { + [ "$(cat "$battery_dir/status")" = "Charging" ] && return 0 + + case "$charge" in + [2-9][0-9] | 100) + status="normal" + ;; + [1-2][0-9]) + status="low" + ;; + [0-9]) + status="critical" + ;; + esac + + case "$status" in + low | critical) + notify "battery-$charge_icon.svg" "$charge% Battery $status, Please Plug In Charger!!" "$status" + ;; + *) ;; + esac +} + #declaration dir="/sys/class/power_supply/" battery="BAT0" #change accordingly battery_dir="${dir}${battery}/" icon_dir="/usr/share/icons/Papirus-Dark/22x22/panel/" #change accordingly -i=0 status="normal" charge=$(cat "$battery_dir"/capacity) battery_resolution="2880x1800@48" # widthxheight@refresh_rate +charge_icon=$(printf "%03d" "$((charge / 10))0") case "$1" in ac) @@ -48,38 +63,11 @@ case "$1" in if [ "$(cat "$dir/ADP1/online")" -eq 0 ]; then switch_to_battery_mode fi - exit 0 + ;; + + status) + check_battery_status ;; *) ;; esac - -#infinite loop -while charge=$(cat "$battery_dir"/capacity); do - if [ "$(cat "$battery_dir"/status)" = "Charging" ]; then - i=300 #default polling time, it's 5 minutes - else - case $charge in - [2-9][0-9] | 100) - i=300 # 5 minutes - status="normal" - ;; - [1-2][0-9]) - i=150 # 2.5 minutes - status="low" - ;; - [5-9]) - i=60 # every minute - status="critical" - ;; - [0-4]) - i=10 # every 10 seconds - status="critical" - ;; - esac - - # remind appropriately - [ "$status" != "normal" ] && notify "battery-010.svg" "$charge % Battery $status, Please Plugin Charger!!" "$status" - fi - print_sleep "$i" "$charge" "$status" "$2" #pass 'log' as argument to print logs -done diff --git a/hypr/hyprland.conf b/hypr/hyprland.conf index f975c3a..10b0edd 100644 --- a/hypr/hyprland.conf +++ b/hypr/hyprland.conf @@ -306,7 +306,6 @@ exec-once = systemctl --user start hypridle # power exec-once = systemctl --user start power-mode@check -exec-once = systemctl --user start monitor-battery # UI exec-once = systemctl --user start theme diff --git a/hypr/ntfy_sub b/hypr/ntfy_sub index 25d715c..d33d079 100755 --- a/hypr/ntfy_sub +++ b/hypr/ntfy_sub @@ -1,6 +1,6 @@ #!/bin/sh -notify () { +notify() { notify-send -i "$icon_path" -e -h "string:x-canonical-private-synchronous:${0##*/}" "[$1] ${2}" "$3" } @@ -15,7 +15,7 @@ icon_path="$(dirname "$0")/ntfy.png" trap "ntfy_creds unload" EXIT trap "ntfy_creds unload; exit 1" INT HUP -websocat --no-close "wss://${NTFY_URL#https://}/$NTFY_TOPIC_LISTEN/ws?auth=$token" | while IFS= read -r line; do +websocat --no-close "wss://${NTFY_URL#https://}/$NTFY_TOPIC_LISTEN/ws?auth=$token" | while IFS= read -r line; do title=$(printf '%s' "$line" | sed -nE 's|.*,"title":"([^"]*)",.*|\1|p') topic=$(printf '%s' "$line" | sed -nE 's|.*,"topic":"([^"]*)",?.*|\1|p') message=$(printf '%s' "$line" | sed -nE 's|.*,"message":"([^"]*)",.*|\1|p') diff --git a/hypr/powersupply_notification.rules b/hypr/powersupply_notification.rules new file mode 100644 index 0000000..3e1beed --- /dev/null +++ b/hypr/powersupply_notification.rules @@ -0,0 +1,6 @@ +# Rule for when switching to battery +ACTION=="change", SUBSYSTEM=="power_supply", ATTRS{type}=="Mains", ATTRS{online}=="0", TAG+="systemd", ENV{SYSTEMD_USER_WANTS}+="power-mode@battery.service" +# Rule for when switching to AC +ACTION=="change", SUBSYSTEM=="power_supply", ATTRS{type}=="Mains", ATTRS{online}=="1", TAG+="systemd", ENV{SYSTEMD_USER_WANTS}+="power-mode@ac.service" +# Rule for battery percentage/status changes +ACTION=="change", SUBSYSTEM=="power_supply", KERNEL=="BAT0", TAG+="systemd", ENV{SYSTEMD_USER_WANTS}+="power-mode@status.service" diff --git a/readme.md b/readme.md index d62e6e8..9d05149 100644 --- a/readme.md +++ b/readme.md @@ -2,3 +2,6 @@ ## Note for backlight - Copy the file from .config/hypr/backlight.rules to /etc/udev/rules.d/ directory + +## Note for Battery Monitoring +- Copy the file from .config/hypr/powersupply_notification.rules to /etc/udev/rules.d/ directory diff --git a/systemd/user/monitor-battery.service b/systemd/user/monitor-battery.service deleted file mode 100644 index e2bf7c7..0000000 --- a/systemd/user/monitor-battery.service +++ /dev/null @@ -1,9 +0,0 @@ -[Unit] -Description=Monitors the battery Power and State. - -[Service] -Type=simple -ExecStart=%h/.config/hypr/battery -Restart=on-failure -RestartSec=5 -