-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwmstatus.c
206 lines (150 loc) · 5.02 KB
/
dwmstatus.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
#include <alloca.h>
#include <alsa/asoundlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#define STRING_MAX 256
static const char *draw_b(char*, size_t);
static const char *draw_t(char*, size_t);
static const char *draw_v(char*, size_t);
static const char *errstr(const char*);
static char errbuf[STRING_MAX];
static const char*
errstr(const char *str)
{
snprintf(errbuf, sizeof(errbuf), "err: %s", str);
return errbuf;
}
static const char*
draw_b(char *str, size_t n)
{
const char *err = NULL;
FILE *fd_0 = fopen("/sys/class/power_supply/BAT0/status", "r"),
*fd_1 = fopen("/sys/class/power_supply/BAT0/charge_now", "r"),
*fd_2 = fopen("/sys/class/power_supply/BAT0/charge_full_design", "r");
if (fd_0 && fd_1 && fd_2) {
const char *s = "";
int c0 = 0,
c1 = 0;
switch (fgetc(fd_0)) {
case 'C': s = "+"; break;
case 'D': s = "-"; break;
case 'F': break;
default:
err = errstr("s");
}
if (!err && EOF == fscanf(fd_1, "%d\n", &c0))
err = errstr("c0");
if (!err && EOF == fscanf(fd_2, "%d\n", &c1))
err = errstr("c1");
if (!err && (c0 <= 0 || c1 <= 0))
err = errstr("invalid");
if (!err && 0 > snprintf(str, n, "%s%d%%", s, (int)(c0 * 100.0 / c1)))
err = errstr("snprintf");
}
if (fd_0) fclose(fd_0); else err = errstr("fd_0");
if (fd_1) fclose(fd_1); else err = errstr("fd_1");
if (fd_2) fclose(fd_2); else err = errstr("fd_2");
return err ? err : str;
}
static const char*
draw_t(char *str, size_t n)
{
const char *err = NULL;
time_t now = time(0);
if (0 == strftime(str, n, "%a %e %R", localtime(&now)))
err = errstr("snprintf");
return err ? err : str;
}
static const char*
draw_v(char *str, size_t n)
{
const char *err = NULL;
/* See `amixer -c N scontrols` */
const char *card_name = "hw:0";
int enabled_master = 0,
enabled_headphone = 0,
enabled_speaker = 0;
long volcur = 0, /* current volume */
volmin = 0, /* minimum volume */
volmax = 0, /* maximum volume */
volrange = 0; /* volume range */
snd_mixer_t *handle = NULL;
snd_mixer_elem_t *elem_master = NULL;
snd_mixer_elem_t *elem_headphone = NULL;
snd_mixer_elem_t *elem_speaker = NULL;
snd_mixer_selem_id_t *sid_master = NULL;
snd_mixer_selem_id_t *sid_headphone = NULL;
snd_mixer_selem_id_t *sid_speaker = NULL;
snd_mixer_selem_id_alloca(&sid_master);
snd_mixer_selem_id_alloca(&sid_headphone);
snd_mixer_selem_id_alloca(&sid_speaker);
snd_mixer_selem_id_set_index(sid_master, 0);
snd_mixer_selem_id_set_index(sid_headphone, 0);
snd_mixer_selem_id_set_index(sid_speaker, 0);
snd_mixer_selem_id_set_name(sid_master, "Master");
snd_mixer_selem_id_set_name(sid_headphone, "Headphone");
snd_mixer_selem_id_set_name(sid_speaker, "Speaker");
if (0 > snd_mixer_open(&handle, 0))
err = errstr("open");
else if (0 > snd_mixer_attach(handle, card_name))
err = errstr("attach");
else if (0 > snd_mixer_selem_register(handle, NULL, NULL))
err = errstr("selem register");
else if (0 > snd_mixer_load(handle))
err = errstr("load");
else if ((NULL == (elem_master = snd_mixer_find_selem(handle, sid_master)))
|| (NULL == (elem_headphone = snd_mixer_find_selem(handle, sid_headphone)))
|| (NULL == (elem_speaker = snd_mixer_find_selem(handle, sid_speaker))))
err = errstr("find selem");
else if ((0 > snd_mixer_selem_get_playback_switch(elem_master, 0, &enabled_master))
|| (0 > snd_mixer_selem_get_playback_switch(elem_headphone, 0, &enabled_headphone))
|| (0 > snd_mixer_selem_get_playback_switch(elem_speaker, 0, &enabled_speaker)))
err = errstr("get playback switch");
else if (0 > snd_mixer_selem_get_playback_volume(elem_master, 0, &volcur))
err = errstr("get playback vol");
else if (0 > snd_mixer_selem_get_playback_volume_range(elem_master, &volmin, &volmax))
err = errstr("get playback vol range");
else if (0 > snd_mixer_detach(handle, card_name))
err = errstr("detach");
else if (0 >= (volrange = (volmax - volmin)))
err = errstr("invalid range");
else if (0 > snprintf(str, n, "%s %d%%%s%s%s",
(enabled_master ? "" : "MUTE"),
(int)(volcur * 100.0 / volrange),
(enabled_headphone || enabled_speaker ? " +" : ""),
(enabled_headphone ? "H" : ""),
(enabled_speaker ? "S" : "")))
err = errstr("snprintf");
if (handle && 0 > snd_mixer_close(handle))
err = errstr("close");
return err ? err : str;
}
int
main(int argc, char **argv)
{
unsigned int seconds = 0;
if (argc > 1)
seconds = (unsigned int)(strtoul(argv[1], NULL, 0));
Display *display;
if (!(display = XOpenDisplay(NULL)))
exit(EXIT_FAILURE);
do {
char status[STRING_MAX + sizeof(" [] [] [] ")];
char b_str[STRING_MAX];
char t_str[STRING_MAX];
char v_str[STRING_MAX];
snprintf(status, sizeof(status), " [%s] [%s] [%s] ",
draw_v(v_str, sizeof(v_str)),
draw_b(b_str, sizeof(b_str)),
draw_t(t_str, sizeof(t_str))
);
XStoreName(display, DefaultRootWindow(display), status);
XSync(display, 0);
sleep(seconds);
} while (seconds);
XCloseDisplay(display);
return 0;
}