slstatus-dyn_battery-20250414-f68f492.diff - sites - public wiki contents of su… | |
git clone git://git.suckless.org/sites | |
Log | |
Files | |
Refs | |
--- | |
slstatus-dyn_battery-20250414-f68f492.diff (4376B) | |
--- | |
1 From b96a72c277a72c77feaf94fac679f3ea87f78ea6 Mon Sep 17 00:00:00 2001 | |
2 From: Madison Lynch <[email protected]> | |
3 Date: Mon, 14 Apr 2025 21:04:23 -0700 | |
4 Subject: [PATCH] Created dyn-battery patch, for displaying status of mul… | |
5 batteries | |
6 | |
7 --- | |
8 Makefile | 1 + | |
9 components/dyn_battery.c | 77 ++++++++++++++++++++++++++++++++++++++++ | |
10 config.def.h | 4 +++ | |
11 slstatus.h | 3 ++ | |
12 4 files changed, 85 insertions(+) | |
13 create mode 100644 components/dyn_battery.c | |
14 | |
15 diff --git a/Makefile b/Makefile | |
16 index 7a18274..d1b24dd 100644 | |
17 --- a/Makefile | |
18 +++ b/Makefile | |
19 @@ -11,6 +11,7 @@ COM =\ | |
20 components/cpu\ | |
21 components/datetime\ | |
22 components/disk\ | |
23 + components/dyn_battery\ | |
24 components/entropy\ | |
25 components/hostname\ | |
26 components/ip\ | |
27 diff --git a/components/dyn_battery.c b/components/dyn_battery.c | |
28 new file mode 100644 | |
29 index 0000000..2e58351 | |
30 --- /dev/null | |
31 +++ b/components/dyn_battery.c | |
32 @@ -0,0 +1,77 @@ | |
33 +/* Written by Madison Lynch <[email protected]> */ | |
34 +/* Only Linux is supported */ | |
35 +#include <stdio.h> | |
36 +#include <stdlib.h> | |
37 +#include <string.h> | |
38 +#include <dirent.h> | |
39 + | |
40 +#include "../slstatus.h" | |
41 + | |
42 +#define BAT_PREFIX "BAT" | |
43 +#define BAT_DIR "/sys/class/power_supply" | |
44 + | |
45 +/** | |
46 +* Counts number of batteries detected by system. | |
47 +* | |
48 +* @return unsigned integer denoting the number of detected batteries. | |
49 +* @author Madison Lynch | |
50 +*/ | |
51 +static unsigned int | |
52 +battery_count(void) { | |
53 + DIR *dir = opendir(BAT_DIR); | |
54 + unsigned int bat_c = 0; | |
55 + | |
56 + struct dirent *entry; | |
57 + while((entry = readdir(dir))) | |
58 + if(strlen(entry->d_name) > 3) | |
59 + if(strncmp(entry->d_name, BAT_PREFIX, 3) == 0) | |
60 + bat_c++; | |
61 + | |
62 + (void) closedir(dir); | |
63 + return bat_c; | |
64 +} | |
65 + | |
66 +/** | |
67 +* Displays the status and capacity of a dynamic amount of batteries (i.… | |
68 +* laptop may have secondary external battery). | |
69 +* | |
70 +* @param fmt format string to use for each battery display. ordered ke… | |
71 +* %u: battery number || %s: battery state || %s battery cap… | |
72 +* @return string containing the status and capacities of all detected b… | |
73 +* @author Madison Lynch | |
74 +*/ | |
75 +const char * | |
76 +dyn_battery(const char *fmt) { | |
77 + const size_t fmt_s = strlen(fmt); | |
78 + const unsigned int bat_c = battery_count(); | |
79 + | |
80 + // Extra byte in calloc() for null byte | |
81 + char *output = (char *)calloc(fmt_s * bat_c + 1, sizeof(char)); | |
82 + unsigned int displacement = 0; // For appending battery displays | |
83 + for(unsigned int i=0; i<bat_c; i++) { | |
84 + char bat[7]; // "BAT" = 3 + <=3 digit number + null byte | |
85 + sprintf(bat, "BAT%u", i); | |
86 + | |
87 + // Add battery display to final string to be returned | |
88 + sprintf( | |
89 + output + displacement, | |
90 + fmt, | |
91 + i, | |
92 + battery_state(bat), | |
93 + battery_perc(bat) | |
94 + ); | |
95 + displacement = strlen(output); | |
96 + | |
97 + // Add space between battery displays | |
98 + *(output + displacement) = ' '; | |
99 + displacement++; | |
100 + } | |
101 + | |
102 + // Remove extra space after last battery display | |
103 + output[--displacement] = 0x00; | |
104 + | |
105 + return output; | |
106 +} | |
107 + | |
108 +#undef BAT_DIR | |
109 +#undef BAT_PREFIX | |
110 diff --git a/config.def.h b/config.def.h | |
111 index d805331..967d63d 100644 | |
112 --- a/config.def.h | |
113 +++ b/config.def.h | |
114 @@ -26,6 +26,10 @@ static const char unknown_str[] = "n/a"; | |
115 * disk_perc disk usage in percent mountpoint path … | |
116 * disk_total total disk space in GB mountpoint path … | |
117 * disk_used used disk space in GB mountpoint path … | |
118 + * dyn_battery displays the name, state and format string (%… | |
119 + * capacity of all detected %s). order is im… | |
120 + * batteries and matches desc… | |
121 + * (Linux only) | |
122 * entropy available entropy NULL | |
123 * gid GID of current user NULL | |
124 * hostname hostname NULL | |
125 diff --git a/slstatus.h b/slstatus.h | |
126 index 8ef5874..44a4b61 100644 | |
127 --- a/slstatus.h | |
128 +++ b/slstatus.h | |
129 @@ -21,6 +21,9 @@ const char *disk_perc(const char *path); | |
130 const char *disk_total(const char *path); | |
131 const char *disk_used(const char *path); | |
132 | |
133 +/* dyn_battery */ | |
134 +const char *dyn_battery(const char *fmt); | |
135 + | |
136 /* entropy */ | |
137 const char *entropy(const char *unused); | |
138 | |
139 -- | |
140 2.49.0 | |
141 |