-
Notifications
You must be signed in to change notification settings - Fork 0
/
dstat.c
410 lines (306 loc) · 8.38 KB
/
dstat.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* dstat - a simple status bar
* Copyright © 2018 Vasili Karaev
*
* This file is part of dstat.
*
* dstat is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dstat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHENTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with dstat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#ifdef ALSA
#include <alsa/asoundlib.h>
#endif
#ifdef MPD
#include <libgen.h>
#include <mpd/client.h>
#endif
#include <xcb/xcb.h>
#include <xcb/xcb_icccm.h>
#define LENGTH(X) (sizeof X / sizeof X[0])
/* writes module info to the status buffer */
static void write_modules(char *statbuf, size_t n);
/* module functions */
/**
* writes current date and time to the buffer
*/
static int date(char *modbuf, size_t n);
#ifdef ALSA
/**
* writes current alsa volume level to the buffer
*/
static int alsa(char *modbuf, size_t n);
#endif
#ifdef MPD
/**
* writes current mpd status to the buffer
*/
static int mpd(char *modbuf, size_t n);
#endif
/* config */
#include "config.h"
/* module helper functions */
#ifdef ALSA
/**
* initializes internal alsa stuff
*/
int alsa_init(snd_mixer_t **mixer, snd_mixer_elem_t **elem);
#endif
#ifdef MPD
/**
* writes info on currently playing song to the buffer
*/
static void mpd_cur_song_info(struct mpd_connection *c,
struct mpd_status *s,
char *songbuf,
size_t n);
#endif
/* xcb helper functions */
/**
* returns the screen of display
*/
static xcb_screen_t *x_get_screen(xcb_connection_t *c, int nscreen);
/**
* sets WM_NAME of a window
*/
static void x_set_wm_name(xcb_connection_t *c, xcb_window_t wid, const char *str);
int main()
{
int nscreen;
xcb_connection_t *c = xcb_connect(NULL, &nscreen);
if (xcb_connection_has_error(c)) {
return EXIT_FAILURE;
}
xcb_screen_t *screen = x_get_screen(c, nscreen);
if (!screen) {
return EXIT_FAILURE;
}
xcb_window_t root = screen->root;
char statbuf[DSTAT_MAX_STATBUFSIZE];
while (1) {
memset(statbuf, 0, DSTAT_MAX_STATBUFSIZE);
write_modules(statbuf, DSTAT_MAX_STATBUFSIZE);
x_set_wm_name(c, root, statbuf);
nanosleep(&interval, NULL);
}
xcb_disconnect(c);
return EXIT_SUCCESS;
}
void write_modules(char *statbuf, size_t n)
{
char modbuf[DSTAT_MAX_MODBUFSIZE];
for (int i = 0; i < LENGTH(modules); i++) {
memset(modbuf, 0, DSTAT_MAX_MODBUFSIZE);
int err = modules[i](modbuf, DSTAT_MAX_MODBUFSIZE);
if (err) {
continue;
}
strncat(statbuf, modbuf, DSTAT_MAX_MODBUFSIZE - 1);
if (i != LENGTH(modules) - 1 && strlen(modbuf)) {
strcat(statbuf, separator);
}
}
}
int date(char *modbuf, size_t n)
{
time_t now = time(NULL);
const char *fmt = "[ %a %b %e ] ( %I:%M:%S )";
return !strftime(modbuf, n, fmt, localtime(&now));
}
#ifdef ALSA
int alsa(char *modbuf, size_t n)
{
static snd_mixer_t *mixer = NULL;
static snd_mixer_elem_t *elem = NULL;
int err = 0;
if (!mixer && !elem) {
err = alsa_init(&mixer, &elem);
if (err) {
goto out;
}
}
if (snd_mixer_wait(mixer, 25)) {
err = 1;
goto out;
}
if (snd_mixer_handle_events(mixer) < 0) {
err = 1;
goto out;
}
long vol_min;
long vol_max;
if (snd_mixer_selem_get_playback_volume_range(elem, &vol_min, &vol_max)) {
err = 2;
goto free_mixer;
}
int nchan = 0;
long vol_total = 0;
for (snd_mixer_selem_channel_id_t chan_id = SND_MIXER_SCHN_FRONT_LEFT;
chan_id <= SND_MIXER_SCHN_LAST;
chan_id++) {
if (snd_mixer_selem_has_playback_channel(elem, chan_id)) {
long vol_current;
if (snd_mixer_selem_get_playback_volume(elem,
chan_id,
&vol_current)) {
err = 3;
goto free_mixer;
}
nchan++;
vol_total += vol_current;
}
}
snprintf(modbuf, n, "< %ld >", vol_total / nchan);
goto out;
free_mixer:
snd_mixer_close(mixer);
mixer = NULL;
elem = NULL;
out:
return err;
}
#endif
#ifdef MPD
int mpd(char *modbuf, size_t n)
{
static struct mpd_connection *c = NULL;
if (!c) {
c = mpd_connection_new(mpd_host, mpd_port, mpd_timeout);
}
if (mpd_connection_get_error(c) != MPD_ERROR_SUCCESS) {
if (c) {
mpd_connection_free(c);
c = NULL;
}
return 1;
}
struct mpd_status *status = mpd_run_status(c);
if (!status) {
return 1;
}
enum mpd_state state = mpd_status_get_state(status);
if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
char songbuf[n - 20];
mpd_cur_song_info(c, status, songbuf, n - 20);
snprintf(modbuf,
n,
"%u:%02u / %u:%02u . %s",
mpd_status_get_elapsed_time(status) / 60,
mpd_status_get_elapsed_time(status) % 60,
mpd_status_get_total_time(status) / 60,
mpd_status_get_total_time(status) % 60,
songbuf);
}
mpd_status_free(status);
return 0;
}
#endif
#ifdef ALSA
int alsa_init(snd_mixer_t **mixer, snd_mixer_elem_t **elem)
{
int err = 0;
snd_mixer_t *m;
if (snd_mixer_open(&m, 1)) {
err = 1;
goto out;
}
snd_config_update_free_global();
if (snd_mixer_attach(m, alsa_soundcard)) {
err = 2;
goto free_mixer;
}
if (snd_mixer_selem_register(m, NULL, NULL)) {
err = 3;
goto free_mixer;
}
if (snd_mixer_load(m)) {
err = 4;
goto free_mixer;
}
snd_mixer_selem_id_t *selem_id = NULL;
if (snd_mixer_selem_id_malloc(&selem_id)) {
err = 5;
goto free_mixer;
}
snd_mixer_selem_id_set_index(selem_id, 0);
snd_mixer_selem_id_set_name(selem_id, alsa_mixer);
snd_mixer_elem_t *e;
if ((e = snd_mixer_find_selem(m, selem_id)) == NULL) {
err = 6;
goto free_selem_id;
}
*mixer = m;
*elem = e;
snd_mixer_selem_id_free(selem_id);
goto out;
free_selem_id:
snd_mixer_selem_id_free(selem_id);
free_mixer:
snd_mixer_close(m);
out:
return err;
}
#endif
#ifdef MPD
void mpd_cur_song_info(struct mpd_connection *c,
struct mpd_status *s,
char *songbuf,
size_t n)
{
struct mpd_song *song = mpd_run_current_song(c);
if (!song) {
return;
}
int pos = mpd_status_get_song_pos(s);
int que = mpd_status_get_queue_length(s);
const char *artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
const char *title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
if (artist && title) {
snprintf(songbuf, n, "%i / %i : %s - %s", pos, que, artist, title);
} else {
const char *uri = mpd_song_get_uri(song);
char *basebuf = malloc(strlen(uri) + 1);
memcpy(basebuf, uri, strlen(uri) + 1);
const char *base = basename(basebuf);
snprintf(songbuf, n, "%s", base);
free(basebuf);
}
mpd_song_free(song);
}
#endif
xcb_screen_t *x_get_screen(xcb_connection_t *c, int nscreen)
{
xcb_screen_t *screen = NULL;
xcb_screen_iterator_t it = xcb_setup_roots_iterator(xcb_get_setup(c));
while (it.rem) {
if (nscreen == 0) {
screen = it.data;
}
nscreen--;
xcb_screen_next(&it);
}
return screen;
}
void x_set_wm_name(xcb_connection_t *c, xcb_window_t wid, const char *str)
{
xcb_icccm_set_wm_name(c,
wid,
XCB_ATOM_STRING,
8,
strlen(str),
str);
xcb_flush(c);
}