[slstatus][patches][battery-notify] patch bug fix - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
 (DIR) commit f63d7fe609bb4ecdd7f164f7353774d6d8f45446
 (DIR) parent a7f46eed7ad54c3afc47ca7b130020dd80898d58
 (HTM) Author: SYLG <2330180633@qq.com>
       Date:   Thu, 31 Jul 2025 22:32:06 +0800
       
       [slstatus][patches][battery-notify] patch bug fix
       
       - fix buffer length of snprintf that cause core dump
       - fix wrong way of calculating length of notifiable_levels
       
       Diffstat:
         M tools.suckless.org/slstatus/patche… |       1 +
         A tools.suckless.org/slstatus/patche… |     136 +++++++++++++++++++++++++++++++
       
       2 files changed, 137 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/tools.suckless.org/slstatus/patches/battery-notify/index.md b/tools.suckless.org/slstatus/patches/battery-notify/index.md
       @@ -15,6 +15,7 @@ Important
        
        Download
        --------
       +* [slstatus-battery-notify-20250731-6eb7887.diff](slstatus-battery-notify-20250731-6eb7887.diff)
        * [slstatus-battery-notify-20240127-a56a0a5.diff](slstatus-battery-notify-20240127-a56a0a5.diff)
        
        Authors
 (DIR) diff --git a/tools.suckless.org/slstatus/patches/battery-notify/slstatus-battery-notify-20250731-6eb7887.diff b/tools.suckless.org/slstatus/patches/battery-notify/slstatus-battery-notify-20250731-6eb7887.diff
       @@ -0,0 +1,136 @@
       +From cf9344373a25b61e41a8cde1a6403bf7beeb7bfe Mon Sep 17 00:00:00 2001
       +From: SYLG <2330180633@qq.com>
       +Date: Thu, 31 Jul 2025 11:03:24 +0800
       +Subject: [PATCH] fix: battery notify patch of slstatus
       +
       +- snprintf use same size as cmd now
       +- use explicit variable to count size of notifiable_levels instead of
       +  calculating in the wrong way
       +---
       + components/battery.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
       + config.def.h         | 12 +++++++++++
       + slstatus.h           |  1 +
       + 3 files changed, 62 insertions(+)
       +
       +diff --git a/components/battery.c b/components/battery.c
       +index 1c753f9..5d0063e 100644
       +--- a/components/battery.c
       ++++ b/components/battery.c
       +@@ -1,6 +1,7 @@
       + /* See LICENSE file for copyright and license details. */
       + #include <stdio.h>
       + #include <string.h>
       ++#include <stdlib.h>
       + 
       + #include "../slstatus.h"
       + #include "../util.h"
       +@@ -20,6 +21,13 @@
       +         #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[];
       ++        extern const size_t notifiable_levels_count;
       ++
       +         static const char *
       +         pick(const char *bat, const char *f1, const char *f2, char *path,
       +              size_t length)
       +@@ -49,6 +57,47 @@
       +                 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;
       ++
       ++        char cmd[28];
       ++
       ++        for (size_t i = 0; i < notifiable_levels_count; i++)
       ++        {
       ++                if (notifiable_levels[i] != cap_perc)
       ++                        continue;
       ++
       ++                if (notifiable_levels[i] != last_notified_level)
       ++                {
       ++                        last_notified_level = notifiable_levels[i];
       ++
       ++                        snprintf(cmd, 28, "%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 100093e..dc32a96 100644
       +--- a/config.def.h
       ++++ b/config.def.h
       +@@ -9,11 +9,21 @@ static const char unknown_str[] = "n/a";
       + /* maximum output string length */
       + #define MAXLEN 2048
       + 
       ++/* battery levels to notify - add any levels you want to receive notification for (in percent) */
       ++const int notifiable_levels[] = {
       ++    20,
       ++    10,
       ++    5,
       ++};
       ++const size_t notifiable_levels_count = sizeof(notifiable_levels) / sizeof(notifiable_levels[0]);
       ++
       + /*
       +  * function            description                     argument (example)
       +  *
       +  * battery_perc        battery percentage              battery name (BAT0)
       +  *                                                     NULL on OpenBSD/FreeBSD
       ++ * battery_notify      linux battery notifications     battery name (BAT0)
       ++ *                                                     OpenBSD/FreeBSD not supported
       +  * battery_remaining   battery remaining HH:MM         battery name (BAT0)
       +  *                                                     NULL on OpenBSD/FreeBSD
       +  * battery_state       battery charging state          battery name (BAT0)
       +@@ -67,4 +77,6 @@ static const char unknown_str[] = "n/a";
       + static const struct arg args[] = {
       +         /* function format          argument */
       +         { datetime, "%s",           "%F %T" },
       ++        { battery_notify, "",       "BAT0"}, /* There is nothing to print its just a notifications*/
       ++
       + };
       +diff --git a/slstatus.h b/slstatus.h
       +index 394281c..76db9f1 100644
       +--- a/slstatus.h
       ++++ b/slstatus.h
       +@@ -2,6 +2,7 @@
       + 
       + /* 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
       +