diff --git a/assets/battery_notify.mp3 b/assets/battery_notify.mp3 new file mode 100644 index 0000000..90e3256 Binary files /dev/null and b/assets/battery_notify.mp3 differ diff --git a/hosts/laptop/configuration.nix b/hosts/laptop/configuration.nix index 7db19f6..66cec8e 100755 --- a/hosts/laptop/configuration.nix +++ b/hosts/laptop/configuration.nix @@ -183,6 +183,7 @@ pavucontrol # PulseAudio Volume Control hyprshot hyprcursor vial # Open-source GUI and QMK fork for configuring your keyboard in real time +mplayer # Movie player that supports many video formats ]; # fonts diff --git a/hosts/laptop/home.nix b/hosts/laptop/home.nix index 39f75dd..dc2056d 100755 --- a/hosts/laptop/home.nix +++ b/hosts/laptop/home.nix @@ -55,6 +55,7 @@ home.packages = [ #scritps (pkgs.callPackage ../../modules/scripts/dontkillsteam.nix {}) # kill app (if it is steam put it in some background (pkgs.callPackage ../../modules/scripts/batterynotify.nix {}) +(pkgs.callPackage ../../modules/scripts/batterywarning.nix {}) (pkgs.callPackage ../../modules/scripts/weather.nix {}) (pkgs.callPackage ../../modules/scripts/cowsay.nix {}) (pkgs.callPackage ../../modules/scripts/wallpaper.nix {}) @@ -65,6 +66,7 @@ home.packages = [ (pkgs.callPackage ../../modules/scripts/launch.nix {}) # necessary for the apps i launch with hyprland at every start (pkgs.callPackage ../../modules/scripts/gitnotify.nix {}) (pkgs.callPackage ../../modules/scripts/tomato.nix {}) + #pkgs pkgs.gruvbox-gtk-theme # # Adds the 'hello' command to your environment. It prints a friendly diff --git a/modules/home-manager/hyprland.nix b/modules/home-manager/hyprland.nix index 29bf3d7..709698c 100644 --- a/modules/home-manager/hyprland.nix +++ b/modules/home-manager/hyprland.nix @@ -190,7 +190,8 @@ in "nm-applet --indicator" # systray app for Network/wifi "wl-paste --type text --watch cliphist store" # clipboard store text data "wl-paste --type image --watch cliphist store" # clipboard store image data - "custom-batterynotify" + "custom-batterynotify" + "custom-batterywarning" #wallpapers/b "swww img ${wallpaper}" "swww-daemon" diff --git a/modules/scripts/OldDeprecatedbatterynotify.nix b/modules/scripts/OldDeprecatedbatterynotify.nix new file mode 100644 index 0000000..3504fe0 --- /dev/null +++ b/modules/scripts/OldDeprecatedbatterynotify.nix @@ -0,0 +1,71 @@ +{ writeShellApplication +, libnotify +, systemd +}: + +writeShellApplication { + name = "custom-olddeprecatedbatterynotify"; + + runtimeInputs = [ + libnotify # provides notify-send + systemd # provides systemctl + ]; + + text = '' + set -euo pipefail + + LOW="''${BATTERY_LOW_THRESHOLD:-20}" + CRITICAL="''${BATTERY_CRITICAL_THRESHOLD:-5}" + FULL="''${BATTERY_FULL_THRESHOLD:-95}" + INTERVAL="''${BATTERY_INTERVAL:-30}" + ACTION="''${BATTERY_CRITICAL_ACTION:-systemctl suspend}" + + notify() { + notify-send -a "Battery" -u "$1" "$2" "$3" + } + + get_info() { + total=0 + count=0 + + for bat in /sys/class/power_supply/BAT*; do + [ -f "$bat/capacity" ] || continue + capacity=$(<"$bat/capacity") + status=$(<"$bat/status") + total=$((total + capacity)) + count=$((count + 1)) + done + + [ "$count" -gt 0 ] || exit 0 + percentage=$((total / count)) + } + + last_state="" + + while true; do + get_info + + if [[ "$status" == "Discharging" && "$percentage" -le "$CRITICAL" ]]; then + notify critical "Battery Critically Low" "$percentage% — suspending." + $ACTION + + elif [[ "$status" == "Discharging" && "$percentage" -le "$LOW" ]]; then + if [[ "$last_state" != "low" ]]; then + notify normal "Battery Low" "$percentage% remaining." + last_state="low" + fi + + elif [[ "$status" != "Discharging" && "$percentage" -ge "$FULL" ]]; then + if [[ "$last_state" != "full" ]]; then + notify normal "Battery Full" "$percentage% — unplug charger." + last_state="full" + fi + + else + last_state="" + fi + + sleep "$INTERVAL" + done + ''; +} diff --git a/modules/scripts/batterynotify.nix b/modules/scripts/batterynotify.nix index 3ebab02..cc2268f 100644 --- a/modules/scripts/batterynotify.nix +++ b/modules/scripts/batterynotify.nix @@ -1,71 +1,69 @@ -{ writeShellApplication -, libnotify -, systemd -}: - +# taken and adapted https://github.com/miniMinn24/Battery_Notify with MIT License +{ writeShellApplication, mplayer, brightnessctl, ... }: +let + NotifySound = ../../assets/battery_notify.mp3; +in writeShellApplication { - name = "custom-batterynotify"; + name = "custom-batterynotify"; + runtimeInputs = [ + mplayer + brightnessctl + ]; + text = '' - runtimeInputs = [ - libnotify # provides notify-send - systemd # provides systemctl - ]; +# Delay for startup +sleep 5 - text = '' - set -euo pipefail +# Change your sound path here - LOW="''${BATTERY_LOW_THRESHOLD:-20}" - CRITICAL="''${BATTERY_CRITICAL_THRESHOLD:-5}" - FULL="''${BATTERY_FULL_THRESHOLD:-95}" - INTERVAL="''${BATTERY_INTERVAL:-30}" - ACTION="''${BATTERY_CRITICAL_ACTION:-systemctl suspend}" - - notify() { - notify-send -a "Battery" -u "$1" "$2" "$3" - } - - get_info() { - total=0 - count=0 - - for bat in /sys/class/power_supply/BAT*; do - [ -f "$bat/capacity" ] || continue - capacity=$(<"$bat/capacity") - status=$(<"$bat/status") - total=$((total + capacity)) - count=$((count + 1)) - done - - [ "$count" -gt 0 ] || exit 0 - percentage=$((total / count)) - } - - last_state="" - - while true; do - get_info - - if [[ "$status" == "Discharging" && "$percentage" -le "$CRITICAL" ]]; then - notify critical "Battery Critically Low" "$percentage% — suspending." - $ACTION - - elif [[ "$status" == "Discharging" && "$percentage" -le "$LOW" ]]; then - if [[ "$last_state" != "low" ]]; then - notify normal "Battery Low" "$percentage% remaining." - last_state="low" - fi - - elif [[ "$status" != "Discharging" && "$percentage" -ge "$FULL" ]]; then - if [[ "$last_state" != "full" ]]; then - notify normal "Battery Full" "$percentage% — unplug charger." - last_state="full" - fi - - else - last_state="" - fi - - sleep "$INTERVAL" - done - ''; +playsound() { + mplayer ${NotifySound} +} + +adjustBrightness() { + local adjustment="$1" + brightnessctl set "$adjustment" +} + +sendNotification() { + local title="$1" + local message="$2" + notify-send -h string:x-canonical-private-synchronous:sys-notify -e -u low "$title" "$message" +} + +# Runs full time in background +x=0 +while true; do + Battery_Status="$(cat /sys/class/power_supply/BAT*/status)" + Battery_Capacity=$(cat /sys/class/power_supply/BAT*/capacity) + + case "$Battery_Status" in + "Discharging") + if [ $x -eq 1 ]; then + sendNotification "Discharging" "Remains: ''${Battery_Capacity}%" + adjustBrightness "15%-" + playsound + x=0 + fi + ;; + "Charging") + if [ $x -eq 0 ]; then + sendNotification "Charging" "Current: ''${Battery_Capacity}%" + adjustBrightness "15%+" + playsound + x=1 + fi + ;; + "Full") + if [ $x -eq 0 ]; then + sendNotification "Fully Charged" "Noice: ''${Battery_Capacity}%" + playsound + x=1 + fi + ;; + esac + + sleep 1 +done + ''; } diff --git a/modules/scripts/batterywarning.nix b/modules/scripts/batterywarning.nix new file mode 100644 index 0000000..14d64d1 --- /dev/null +++ b/modules/scripts/batterywarning.nix @@ -0,0 +1,53 @@ +# taken and adapted https://github.com/miniMinn24/Battery_Notify with MIT License +{ writeShellApplication, mplayer, brightnessctl, ... }: +let + NotifySound = ../../assets/battery_notify.mp3; +in +writeShellApplication { + name = "custom-batterywarning"; + runtimeInputs = [ + mplayer + brightnessctl + ]; + text = '' + +# Delay for startup +sleep 5 + +# Change your sound path here + +playsound() { + mplayer ${NotifySound} +} + +sendNotification() { + local message="$1" + notify-send -h string:x-canonical-private-synchronous:sys-notify -e -u critical "Battery Low!" "$message" +} + +notify_count=0 + +while true; do + Battery_Status="$(cat /sys/class/power_supply/BAT*/status)" + Battery_Capacity=$(cat /sys/class/power_supply/BAT*/capacity) + + if [ "$Battery_Status" == "Discharging" ] && [ "$Battery_Capacity" -le 14 ]; then + if [ "$notify_count" -eq 0 ]; then + sendNotification "Power running out: ''${Battery_Capacity}%" + playsound + notify_count=$((notify_count + 1)) + sleep 180 + + elif [ "$notify_count" -eq 1 ]; then + sendNotification "Save your works before immediate shutdown." + playsound + notify_count=$((notify_count - 1)) + sleep 180 + fi + else + notify_count=0 + sleep 120 + fi +done + ''; +}