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