diff --git a/hosts/pinwheel/modules/bluetooth/default.nix b/hosts/pinwheel/modules/bluetooth/default.nix index 4faf5c1..46571f8 100644 --- a/hosts/pinwheel/modules/bluetooth/default.nix +++ b/hosts/pinwheel/modules/bluetooth/default.nix @@ -1,4 +1,4 @@ -{ lib, config, ... }: +{ pkgs, lib, config, ... }: let enabled = config.mod.bluetooth.enable; in @@ -21,5 +21,93 @@ in }; services.blueman.enable = true; + + # Low battery notification for bluetooth devices + systemd.user = + let + trackpad = { + id = "battery_hid_a8o91o3doe5ofeo38_battery"; + name = "trackpad"; + threshold = "20"; + }; + + headphones = { + id = "headset_dev_38_18_4C_18_A4_6E"; + name = "headphones"; + threshold = "30"; + }; + in + { + timers = + let + mkTimer = device: { + name = "notify-low-battery-for-${device.name}"; + + value = { + unitConfig = { + Description = "notify-battery-low timer for '${device.name}'"; + }; + + wantedBy = [ "timers.target" ]; + + timerConfig = { + Unit = "notify-low-battery-for-${device.name}.service"; + OnCalendar = "*-*-* *:00:00"; # Every hour + Persistent = true; + }; + }; + }; + in + builtins.listToAttrs (builtins.map mkTimer [ trackpad headphones ]); + + services = + let + mkService = device: { + name = "notify-low-battery-for-${device.name}"; + + value = { + unitConfig = { + Description = "check battery level of '${device.name}'"; + }; + + wantedBy = [ "default.target" ]; + serviceConfig = { + Type = "exec"; + }; + + path = [ + pkgs.upower + pkgs.gawk + pkgs.bc + pkgs.libnotify + ]; + + script = '' + CONNECTED=$(upower --show-info /org/freedesktop/UPower/devices/${device.id} | grep native-path | awk '{print $2}') + [ "$CONNECTED" == "(null)" ] && exit 0 + + CHECKING="/tmp/checking-dismiss-low-battery-${device.id}" + [ ! -f "$CHECKING" ] && touch $CHECKING || exit 0 + + DISMISSED="/tmp/dismiss-low-battery-${device.id}" + PERCENT=$(upower --show-info /org/freedesktop/UPower/devices/${device.id} | grep percentage | grep -o '[0-9]*') + if (( $(echo "$PERCENT < ${device.threshold}" | bc) )); then + echo "'${device.name}' is under threshold. battery = $PERCENT% - threshold = ${device.threshold}%" + if [ ! -f "$DISMISSED" ]; then + DISMISS=$(notify-send --expire-time 0 "Low battery" "${device.name} has $PERCENT% battery" --action=dismiss=Dismiss) + [ "$DISMISS" == "dismiss" ] && touch $DISMISSED && echo "'${device.name}' dismissed" + fi + else + echo "'${device.name}' is over threshold. battery = $PERCENT% - threshold = ${device.threshold}%" + [ -f "$DISMISSED" ] && rm $DISMISSED && echo "'${device.name}' undismissed" + fi + + rm $CHECKING + ''; + }; + }; + in + builtins.listToAttrs (builtins.map mkService [ trackpad headphones ]); + }; }; } diff --git a/hosts/pinwheel/modules/dunst/default.nix b/hosts/pinwheel/modules/dunst/default.nix index 3eada2a..1ff319e 100644 --- a/hosts/pinwheel/modules/dunst/default.nix +++ b/hosts/pinwheel/modules/dunst/default.nix @@ -14,6 +14,7 @@ transparency = 10; frame_color = "#${config.lib.colors.foreground-dim}"; font = "JetBrainsMono Nerd Font Mono 14"; + mouse_middle_click = "do_action, close_current"; }; urgency_low = {