slstatus-battery-notify-20250731-6eb7887.diff (4004B)
1 From cf9344373a25b61e41a8cde1a6403bf7beeb7bfe Mon Sep 17 00:00:00 2001 2 From: SYLG <2330180633@qq.com> 3 Date: Thu, 31 Jul 2025 11:03:24 +0800 4 Subject: [PATCH] fix: battery notify patch of slstatus 5 6 - snprintf use same size as cmd now 7 - use explicit variable to count size of notifiable_levels instead of 8 calculating in the wrong way 9 --- 10 components/battery.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ 11 config.def.h | 12 +++++++++++ 12 slstatus.h | 1 + 13 3 files changed, 62 insertions(+) 14 15 diff --git a/components/battery.c b/components/battery.c 16 index 1c753f9..5d0063e 100644 17 --- a/components/battery.c 18 +++ b/components/battery.c 19 @@ -1,6 +1,7 @@ 20 /* See LICENSE file for copyright and license details. */ 21 #include <stdio.h> 22 #include <string.h> 23 +#include <stdlib.h> 24 25 #include "../slstatus.h" 26 #include "../util.h" 27 @@ -20,6 +21,13 @@ 28 #define POWER_SUPPLY_CURRENT "/sys/class/power_supply/%s/current_now" 29 #define POWER_SUPPLY_POWER "/sys/class/power_supply/%s/power_now" 30 31 + const char notify_cmd[] = "notify-send"; 32 + const char battery_str[] = "Battery"; 33 + int last_notified_level = 0; 34 + 35 + extern const int notifiable_levels[]; 36 + extern const size_t notifiable_levels_count; 37 + 38 static const char * 39 pick(const char *bat, const char *f1, const char *f2, char *path, 40 size_t length) 41 @@ -49,6 +57,47 @@ 42 return bprintf("%d", cap_perc); 43 } 44 45 + void battery_notify(const char *bat) 46 +{ 47 + int cap_perc; 48 + char state[12]; 49 + char path[PATH_MAX]; 50 + 51 + if (esnprintf(path, sizeof(path), POWER_SUPPLY_CAPACITY, bat) < 0 || pscanf(path, "%d", &cap_perc) != 1) 52 + return; 53 + 54 + if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0 || pscanf(path, "%12[a-zA-Z ]", &state) != 1) 55 + return; 56 + 57 + if (strcmp("Charging", state) == 0) 58 + { 59 + last_notified_level = 0; 60 + 61 + return; 62 + } 63 + 64 + if (strcmp("Discharging", state) != 0) 65 + return; 66 + 67 + char cmd[28]; 68 + 69 + for (size_t i = 0; i < notifiable_levels_count; i++) 70 + { 71 + if (notifiable_levels[i] != cap_perc) 72 + continue; 73 + 74 + if (notifiable_levels[i] != last_notified_level) 75 + { 76 + last_notified_level = notifiable_levels[i]; 77 + 78 + snprintf(cmd, 28, "%s %s %d%%", notify_cmd, battery_str, cap_perc); 79 + system(cmd); 80 + 81 + break; 82 + } 83 + } 84 +} 85 + 86 const char * 87 battery_state(const char *bat) 88 { 89 diff --git a/config.def.h b/config.def.h 90 index 100093e..dc32a96 100644 91 --- a/config.def.h 92 +++ b/config.def.h 93 @@ -9,11 +9,21 @@ static const char unknown_str[] = "n/a"; 94 /* maximum output string length */ 95 #define MAXLEN 2048 96 97 +/* battery levels to notify - add any levels you want to receive notification for (in percent) */ 98 +const int notifiable_levels[] = { 99 + 20, 100 + 10, 101 + 5, 102 +}; 103 +const size_t notifiable_levels_count = sizeof(notifiable_levels) / sizeof(notifiable_levels[0]); 104 + 105 /* 106 * function description argument (example) 107 * 108 * battery_perc battery percentage battery name (BAT0) 109 * NULL on OpenBSD/FreeBSD 110 + * battery_notify linux battery notifications battery name (BAT0) 111 + * OpenBSD/FreeBSD not supported 112 * battery_remaining battery remaining HH:MM battery name (BAT0) 113 * NULL on OpenBSD/FreeBSD 114 * battery_state battery charging state battery name (BAT0) 115 @@ -67,4 +77,6 @@ static const char unknown_str[] = "n/a"; 116 static const struct arg args[] = { 117 /* function format argument */ 118 { datetime, "%s", "%F %T" }, 119 + { battery_notify, "", "BAT0"}, /* There is nothing to print its just a notifications*/ 120 + 121 }; 122 diff --git a/slstatus.h b/slstatus.h 123 index 394281c..76db9f1 100644 124 --- a/slstatus.h 125 +++ b/slstatus.h 126 @@ -2,6 +2,7 @@ 127 128 /* battery */ 129 const char *battery_perc(const char *); 130 +void battery_notify(const char *); 131 const char *battery_remaining(const char *); 132 const char *battery_state(const char *); 133 134 -- 135 2.43.0 136