]> git.sur5r.net Git - i3/i3status/blob - src/print_load.c
Implement the i3bar JSON protocol
[i3/i3status] / src / print_load.c
1 // vim:ts=8:expandtab
2 #include "i3status.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 void print_load(const char *format) {
8 /* Get load */
9         if (output_format == O_I3BAR)
10                 printf("{\"name\":\"load\", \"full_text\":\"");
11
12 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun)
13         double loadavg[3];
14         const char *walk;
15
16         if (getloadavg(loadavg, 3) == -1)
17                 goto error;
18
19         for (walk = format; *walk != '\0'; walk++) {
20                 if (*walk != '%') {
21                         putchar(*walk);
22                         continue;
23                 }
24
25                 if (BEGINS_WITH(walk+1, "1min")) {
26                         (void)printf("%1.2f", loadavg[0]);
27                         walk += strlen("1min");
28                 }
29
30                 if (BEGINS_WITH(walk+1, "5min")) {
31                         (void)printf("%1.2f", loadavg[1]);
32                         walk += strlen("5min");
33                 }
34
35                 if (BEGINS_WITH(walk+1, "15min")) {
36                         (void)printf("%1.2f", loadavg[2]);
37                         walk += strlen("15min");
38                 }
39         }
40
41         if (output_format == O_I3BAR)
42                 printf("\"}");
43
44         return;
45 error:
46 #endif
47         (void)fputs("Cannot read load\n", stderr);
48 }