sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

slstatus-battery-notify-20240119-a9ebd00.diff (2644B)


      1 From: keroles <krls97@proton.me>
      2 Date: Fri, 19 Jan 2024 14:01:34 +0200
      3 Subject: [PATCH] add battery notify feature
      4 
      5 ---
      6  components/battery.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
      7  config.def.h         |  7 +++++++
      8  slstatus.h           |  3 +++
      9  3 files changed, 59 insertions(+)
     10 
     11 diff --git a/components/battery.c b/components/battery.c
     12 index 1c753f9..908ea7b 100644
     13 --- a/components/battery.c
     14 +++ b/components/battery.c
     15 @@ -20,6 +20,12 @@
     16  	#define POWER_SUPPLY_CURRENT  "/sys/class/power_supply/%s/current_now"
     17  	#define POWER_SUPPLY_POWER    "/sys/class/power_supply/%s/power_now"
     18  
     19 +	const char notify_cmd[] = "notify-send";
     20 +	const char battery_str[] = "Battery";
     21 +	int last_notified_level = 0;
     22 +
     23 +	extern const int notifiable_levels[];
     24 +
     25  	static const char *
     26  	pick(const char *bat, const char *f1, const char *f2, char *path,
     27  	     size_t length)
     28 @@ -49,6 +55,49 @@
     29  		return bprintf("%d", cap_perc);
     30  	}
     31  
     32 +	void battery_notify(const char *bat)
     33 +	{
     34 +		int cap_perc;
     35 +		char state[12];
     36 +		char path[PATH_MAX];
     37 +
     38 +		if (esnprintf(path, sizeof(path), POWER_SUPPLY_CAPACITY, bat) < 0 || pscanf(path, "%d", &cap_perc) != 1)
     39 +			return;
     40 +
     41 +		if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0 || pscanf(path, "%12[a-zA-Z ]", &state) != 1)
     42 +			return;
     43 +
     44 +		if (strcmp("Charging", state) == 0)
     45 +		{
     46 +			last_notified_level = 0;
     47 +
     48 +			return;
     49 +		}
     50 +
     51 +		if (strcmp("Discharging", state) != 0)
     52 +			return;
     53 +
     54 +		size_t i;
     55 +		const int size = sizeof(*notifiable_levels);
     56 +		char cmd[28];
     57 +
     58 +		for (i = 0; i < size; i++)
     59 +		{
     60 +			if (notifiable_levels[i] != cap_perc)
     61 +				continue;
     62 +
     63 +			if (notifiable_levels[i] != last_notified_level)
     64 +			{
     65 +				last_notified_level = notifiable_levels[i];
     66 +
     67 +				snprintf(cmd, 100, "%s %s %d%%", notify_cmd, battery_str, cap_perc);
     68 +				system(cmd);
     69 +
     70 +				break;
     71 +			}	
     72 +		}
     73 +	}	
     74 +
     75  	const char *
     76  	battery_state(const char *bat)
     77  	{
     78 diff --git a/config.def.h b/config.def.h
     79 index d805331..67e6f16 100644
     80 --- a/config.def.h
     81 +++ b/config.def.h
     82 @@ -1,5 +1,12 @@
     83  /* See LICENSE file for copyright and license details. */
     84  
     85 +/* battery levels to notify - add any levels you want to receive notification for (in percent) */
     86 +const int notifiable_levels[] = {
     87 +    20,
     88 +    10,
     89 +    5,
     90 +};
     91 +
     92  /* interval between updates (in ms) */
     93  const unsigned int interval = 1000;
     94  
     95 diff --git a/slstatus.h b/slstatus.h
     96 index 8ef5874..5642dda 100644
     97 --- a/slstatus.h
     98 +++ b/slstatus.h
     99 @@ -2,6 +2,9 @@
    100  
    101  /* battery */
    102  const char *battery_perc(const char *);
    103 +
    104 +void battery_notify(const char *);
    105 +
    106  const char *battery_remaining(const char *);
    107  const char *battery_state(const char *);
    108  
    109 -- 
    110 2.43.0
    111