-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdwmst.c
314 lines (274 loc) · 6.97 KB
/
dwmst.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include <sys/utsname.h>
#include "config.h"
// controls how fast dwmst will update in seconds
// change this to set default, otherwise use 'dwmst -i <seconds>'
double interval = 1;
// runs this amount of times and exits. If you want to change the default behavior
// change this here, otherwise use 'dwmst -r <int>'
// set to a negative int to run infinitely (default)
long int run_times = -1;
// opens the display to set root window name
Display *display;
// used to safely return strings in a formatted manner
char *
smprintf(char *fmt, ...)
{
va_list fmtargs;
char *ret;
int len;
va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);
ret = malloc(++len);
if(ret == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
va_start(fmtargs, fmt);
vsnprintf(ret, len, fmt, fmtargs);
va_end(fmtargs);
return ret;
}
// display program usage
void
display_usage()
{
printf("usage: dwmst [OPTION]\n");
printf(" -h, display this help and exit\n");
printf(" -v, display version information and exit\n");
printf(" -r <int>, run int amount of times and exit, turned off by default\n");
printf(" this argument must be an int > 0 (useful for debug)\n");
printf(" -i <int>, set the interval (in seconds) of how often dwmst should refresh\n");
printf(" by default there is 1 second between updates, must be an int >= 0\n");
exit(0);
}
// display general program info & version
void
display_version()
{
printf("dwmst 1.31 - A utility to feed info to a status bar\n");
printf("2015, Tyler C-W\n");
exit(0);
}
/* safely spawns a shell command and returns it's output.
currently only used for volume (for getting mute state),
can be used like so:
char *curtime = SHCMD("date +'%I:%M%P'");
char *kernel = SHCMD("uname -r");
char *whoami = SHCMD("whoami");
*/
char *
SHCMD(char *cmd)
{
FILE* process = popen(cmd, "r");
char buffer[1024];
fscanf(process, "%[^\n]", buffer);
pclose(process);
return smprintf(buffer);
}
/* reads respective files for battery state (charging/discharging),
remaining percent, and if the battery is installed or not
*/
char *
get_battery()
{
// is the battery installed?
int installed;
FILE* infile = fopen("/sys/devices/platform/smapi/BAT0/installed", "r");
fscanf(infile, "%i", &installed);
fclose(infile);
printf("%i", installed);
if (installed == 1)
{
// get the remaining percentage
int percent;
infile = fopen("/sys/devices/platform/smapi/BAT0/remaining_percent", "r");
fscanf(infile, "%i", &percent);
fclose(infile);
// get battery status (charging/discharging)
char state[16];
infile = fopen("/sys/devices/platform/smapi/BAT0/state", "r");
fscanf(infile, "%s", state);
fclose(infile);
if ( strncmp(state, "discharging", 11) == 0)
{
if (percent < 25)
return smprintf(BAT_25, percent);
else if ((percent >= 25) && (percent < 50))
return smprintf(BAT_50, percent);
else if ((percent >= 50) && (percent < 75))
return smprintf(BAT_75, percent);
else if (percent >= 75)
return smprintf(BAT_100, percent);
}
else
{
if (percent < 25)
return smprintf(CRG_25, percent);
else if ((percent >= 25) && (percent < 50))
return smprintf(CRG_50, percent);
else if ((percent >= 50) && (percent < 75))
return smprintf(CRG_75, percent);
else if (percent >= 75)
return smprintf(CRG_100, percent);
}
}
else
{
return smprintf(AC_PWR);
}
return smprintf("error");
}
/* Reads UPDATE_FILE to retrieve number of pacman updates.
UPDATE_FILE is written to by a shell script executed every
10 minutes by a cron job. the script has the following in it:
pacman -Sy
pacman -Qu | wc -l > $HOME/log/updates.log
*/
char *
get_updates()
{
FILE* infile = fopen(UPDATE_FILE, "r");
int number;
fscanf(infile, "%i", &number);
fclose(infile);
return smprintf(number == 0 ? PAC_NONE : PAC_UPDS, number);
}
/* reads current system volume from acpi,
configured to work with tp_smapi for ThinkPads.
only tested on an IBM ThinkPad T60
*/
char *
get_vol()
{
char *mute = SHCMD("awk '/^mute/{print $2}' /proc/acpi/ibm/volume");
FILE* infile = fopen("/proc/acpi/ibm/volume", "r");
int number;
fscanf(infile, "level:%i", &number);
fclose(infile);
int volume = round((number*100)/14);
if( strncmp(mute, "off", 3) == 0 )
{
if (volume < 25)
return smprintf(VOL_25, volume);
else if ((volume >= 25) && (volume < 50))
return smprintf(VOL_50, volume);
else if ((volume >= 50) && (volume < 75))
return smprintf(VOL_75, volume);
else if (volume >= 75)
return smprintf(VOL_100, volume);
}
else // volume is muted
{
return smprintf(VOL_MUTE);
}
return "Unable"; // unable to get volume level from /proc
}
// returns current system time in the format specified by CLK
char *
get_time()
{
char clock[38];
time_t current;
time(¤t);
if(!strftime(clock, sizeof(clock) - 1, CLK, localtime(¤t)))
return NO_CLK;
return smprintf(clock);
}
// returns kernel release number, same as 'uname -r'
char *
get_kernel()
{
struct utsname retval;
uname(&retval);
return smprintf(KERNEL, retval.release);
}
// does the actual printing of the status
void
print_status()
{
int times_ran = 0;
FILE* bar = popen(STATUS_BAR, "w");
if (!(display = XOpenDisplay(NULL))) {
fprintf(stderr, "dwmst: cannot open display.\n");
}
// status loop, executed [interval] seconds
while (true)
{
//print status
fprintf(bar, KERNEL_STRING, get_kernel());
fprintf(bar, SEPCHR_STRING);
fprintf(bar, BATTRY_STRING, get_battery());
fprintf(bar, SEPCHR_STRING);
fprintf(bar, UPDATE_STRING, get_updates());
fprintf(bar, SEPCHR_STRING);
fprintf(bar, VOLUME_STRING, get_vol());
fprintf(bar, SEPCHR_STRING);
fprintf(bar, PCTIME_STRING, get_time());
fprintf(bar, SEPCHR_STRING);
fprintf(bar, CMDBTN_STRING);
fprintf(bar, " \n");
fflush(bar);
/* increment the number of times the loop has ran by 1, if
this value is exactly equal to the specified number of times to run
(default is -1) then break the loop. If it is not, then
sleep for the specified interval, and run the loop again
*/
times_ran++;
if (times_ran == run_times)
{
pclose(bar);
break;
}
else
{
sleep(interval);
}
}
pclose(bar);
}
// main function, all argument handling is done here
int
main(int argc, char *argv[])
{
int c;
if ((c = getopt(argc, argv, "hvr:i:t")) != 4)
{
switch( c )
{
case 'h':
display_usage();
break;
case 'v':
display_version();
break;
case 'i':
interval = strtod(optarg, NULL);
print_status();
break;
case 'r':
run_times = strtol(optarg, NULL, 10);
if( run_times > 0)
print_status();
else
display_usage();
break;
case '?':
display_usage();
break;
default:
print_status();
break;
}
}
return 0;
}