[wiki] [sites] add battery notify patch for slstatus || keroles97

From: <git_AT_suckless.org>
Date: Fri, 19 Jan 2024 15:07:24 +0100

commit c76ee9b66b67be37442a4ca3e2ec9ce446766b73
Author: keroles97 <keroles97_AT_gmail.com>
Date: Fri Jan 19 16:02:11 2024 +0200

    add battery notify patch for slstatus

diff --git a/tools.suckless.org/slstatus/patches/battery-notify/index.md b/tools.suckless.org/slstatus/patches/battery-notify/index.md
new file mode 100644
index 00000000..2442e7d2
--- /dev/null
+++ b/tools.suckless.org/slstatus/patches/battery-notify/index.md
_AT_@ -0,0 +1,22 @@
+battery notify
+=========
+
+Description
+-----------
+This diff adds a battery notifications for specific levels you defined to slstatus.
+It sends notification using "notify-send" command.
+In config.h file there is array called "notifiable_levels" add any levels you want.
+
+Important
+---------
+* "libnotify" is required to be installed
+* Add ({battery_notify, "", "BAT1"},) to config file in args array - replace BAT1 with your battery name
+* FreeBSD and OpenBSD are not supported
+
+Download
+--------
+* [slstatus-battery-notify-20240119-a9ebd00.diff](slstatus-battery-notify-20240119-a9ebd00.diff)
+
+Authors
+-------
+* keroles <krls97_AT_proton.me>
diff --git a/tools.suckless.org/slstatus/patches/battery-notify/slstatus-battery-notify-20240119-a9ebd00.diff b/tools.suckless.org/slstatus/patches/battery-notify/slstatus-battery-notify-20240119-a9ebd00.diff
new file mode 100644
index 00000000..8cfd3da9
--- /dev/null
+++ b/tools.suckless.org/slstatus/patches/battery-notify/slstatus-battery-notify-20240119-a9ebd00.diff
_AT_@ -0,0 +1,111 @@
+From: keroles <krls97_AT_proton.me>
+Date: Fri, 19 Jan 2024 14:01:34 +0200
+Subject: [PATCH] add battery notify feature
+
+---
+ components/battery.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
+ config.def.h | 7 +++++++
+ slstatus.h | 3 +++
+ 3 files changed, 59 insertions(+)
+
+diff --git a/components/battery.c b/components/battery.c
+index 1c753f9..908ea7b 100644
+--- a/components/battery.c
++++ b/components/battery.c
+_AT_@ -20,6 +20,12 @@
+ #define POWER_SUPPLY_CURRENT "/sys/class/power_supply/%s/current_now"
+ #define POWER_SUPPLY_POWER "/sys/class/power_supply/%s/power_now"
+
++ const char notify_cmd[] = "notify-send";
++ const char battery_str[] = "Battery";
++ int last_notified_level = 0;
++
++ extern const int notifiable_levels[];
++
+ static const char *
+ pick(const char *bat, const char *f1, const char *f2, char *path,
+ size_t length)
+_AT_@ -49,6 +55,49 @@
+ return bprintf("%d", cap_perc);
+ }
+
++ void battery_notify(const char *bat)
++ {
++ int cap_perc;
++ char state[12];
++ char path[PATH_MAX];
++
++ if (esnprintf(path, sizeof(path), POWER_SUPPLY_CAPACITY, bat) < 0 || pscanf(path, "%d", &cap_perc) != 1)
++ return;
++
++ if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0 || pscanf(path, "%12[a-zA-Z ]", &state) != 1)
++ return;
++
++ if (strcmp("Charging", state) == 0)
++ {
++ last_notified_level = 0;
++
++ return;
++ }
++
++ if (strcmp("Discharging", state) != 0)
++ return;
++
++ size_t i;
++ const int size = sizeof(*notifiable_levels);
++ char cmd[28];
++
++ for (i = 0; i < size; i++)
++ {
++ if (notifiable_levels[i] != cap_perc)
++ continue;
++
++ if (notifiable_levels[i] != last_notified_level)
++ {
++ last_notified_level = notifiable_levels[i];
++
++ snprintf(cmd, 100, "%s %s %d%%", notify_cmd, battery_str, cap_perc);
++ system(cmd);
++
++ break;
++ }
++ }
++ }
++
+ const char *
+ battery_state(const char *bat)
+ {
+diff --git a/config.def.h b/config.def.h
+index d805331..67e6f16 100644
+--- a/config.def.h
++++ b/config.def.h
+_AT_@ -1,5 +1,12 @@
+ /* See LICENSE file for copyright and license details. */
+
++/* battery levels to notify - add any levels you want to receive notification for (in percent) */
++const int notifiable_levels[] = {
++ 20,
++ 10,
++ 5,
++};
++
+ /* interval between updates (in ms) */
+ const unsigned int interval = 1000;
+
+diff --git a/slstatus.h b/slstatus.h
+index 8ef5874..5642dda 100644
+--- a/slstatus.h
++++ b/slstatus.h
+_AT_@ -2,6 +2,9 @@
+
+ /* battery */
+ const char *battery_perc(const char *);
++
++void battery_notify(const char *);
++
+ const char *battery_remaining(const char *);
+ const char *battery_state(const char *);
+
+--
+2.43.0
+
Received on Fri Jan 19 2024 - 15:07:24 CET

This archive was generated by hypermail 2.3.0 : Fri Jan 19 2024 - 15:12:46 CET