summaryrefslogtreecommitdiff
path: root/sb26.c
diff options
context:
space:
mode:
Diffstat (limited to 'sb26.c')
-rw-r--r--sb26.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/sb26.c b/sb26.c
new file mode 100644
index 0000000..e350b11
--- /dev/null
+++ b/sb26.c
@@ -0,0 +1,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);
+ }
+}