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