summaryrefslogtreecommitdiff
path: root/sb26.c
blob: e350b11cdbd87e690ba3db39ce4db4a3fb7b3a71 (plain)
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
#include <stdio.h>
#include <string.h>
#include <time.h>

static const char MONTHS[12][4] = {"Jan\0", "Feb\0", "Mar\0",
	"Apr\0", "May\0", "Jun\0",
	"Jul\0", "Aug\0", "Sep\0",
	"Oct\0", "Nov\0", "Dec\0"
};
static const char DAYS[7][4] = {"Sun\0", "Mon\0", "Tue\0", "Wed\0", "Thu\0", "Fri\0", "Sat\0"};
static const char PM[2][3] = {"AM\0", "PM\0"};

static inline void
cpu_get(char *b)
{
	FILE *p = fopen("/proc/loadavg", "r");
	char c = fgetc(p);
	int i;
	for (i = 0; i < 7 && c != ' '; i++) {
    		b[i] = c;
    		c = fgetc(p);
	}
	b[i] = '\0';
	fclose(p);
}

static inline void
bat_get(char *b)
{
	FILE *p = fopen("/sys/class/power_supply/BAT1/capacity", "r");
	fgets(b, 3, p);
	fclose(p);
}

static inline void
mem_get(char *b)
{
	/* Returns "Volume: <v> [[Muted]]" */
	FILE *p = popen("free -m | awk '/^Mem/{print $3}'", "r");
	fgets(b, 16, p);
	/* Truncate trailing newline */
	b[strlen(b)-1] = '\0';
	fclose(p);
}

static inline void
snd_get(char *b)
{
	/* Returns "Volume: <v> [[Muted]]" */
	FILE *p = popen("wpctl get-volume @DEFAULT_SINK@", "r");
	while (fgetc(p) != ' ');
	fgets(b, 16, p);
	/* Truncate trailing newline */
	b[strlen(b)-1] = '\0';
	fclose(p);
}

static inline void
date_get(struct tm *tm)
{
	time_t now = time(NULL);
	localtime_r(&now, tm);

	// unsigned char hr = tm.tm_hour % 12;
	// if (hr == 0) hr = 12;
	// snprintf(tmf, 21, "%s %s %02d %02d:%02d %s", DAYS[tm.tm_wday], MONTHS[tm.tm_mon], tm.tm_mday, hr, tm.tm_min, PM[tm.tm_hour/12]);
}

int
main(void)
{
	static const struct timespec WAIT = { 2, 0 };
	struct tm tm;
	tzset();
	char snd[16];
	char mem[16];
	char bat[3];
	char cpu[7];
	unsigned char hr = 0;
	for (;;) {
    		date_get(&tm);
		hr = tm.tm_hour % 12;
		if (hr == 0) hr = 12;

		snd_get(snd);
		mem_get(mem);
		bat_get(bat);
		cpu_get(cpu);
		printf("%s | %s | %s MiB | %s%% | %s %s %02d %02d:%02d %s\n", snd, cpu, mem, bat, DAYS[tm.tm_wday], MONTHS[tm.tm_mon], tm.tm_mday, hr, tm.tm_min, PM[tm.tm_hour/12]);
		nanosleep(&WAIT, NULL);
	}
}