This hardcodes all the JSON parts. Strings are not properly escaped currently.
The best/easiest way to fix this is by actually using libyajl.
output_format = O_DZEN2;
else if (strcasecmp(output_str, "xmobar") == 0)
output_format = O_XMOBAR;
+ else if (strcasecmp(output_str, "i3bar") == 0)
+ output_format = O_I3BAR;
else if (strcasecmp(output_str, "none") == 0)
output_format = O_NONE;
else die("Unknown output format: \"%s\"\n", output_str);
|| !valid_color(cfg_getstr(cfg_general, "color_separator")))
die("Bad color format");
+ if (output_format == O_I3BAR) {
+ /* Initialize the i3bar protocol. See i3/docs/i3bar-protocol
+ * for details. */
+ printf("{\"version\":1}\n[\n");
+ }
+
if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
die("Could not create socket\n");
localtime_r(¤t_time, &tm);
current_tm = &tm;
}
+ if (output_format == O_I3BAR)
+ printf("[");
for (j = 0; j < cfg_size(cfg, "order"); j++) {
if (j > 0)
print_seperator();
CASE_SEC("cpu_usage")
print_cpu_usage(cfg_getstr(sec, "format"));
}
+ if (output_format == O_I3BAR)
+ printf("],");
printf("\n");
fflush(stdout);
#ifndef _I3STATUS_H
#define _I3STATUS_H
-enum { O_DZEN2, O_XMOBAR, O_NONE } output_format;
+enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_NONE } output_format;
#include <stdbool.h>
#include <confuse.h>
* pipe target the parent process of i3status. If we detect that, we set
* the format and we are done. */
if (strcasecmp(parentname, "i3bar") == 0)
- format = "none";
+ format = "i3bar";
else if (strcasecmp(parentname, "dzen2") == 0)
format = "dzen2";
else if (strcasecmp(parentname, "xmobar") == 0)
/* Check for known destination programs and set format */
char *newfmt = NULL;
if (strcasecmp(name, "i3bar") == 0)
- newfmt = "none";
+ newfmt = "i3bar";
else if (strcasecmp(name, "dzen2") == 0)
newfmt = "dzen2";
else if (strcasecmp(name, "xmobar") == 0)
(void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", cfg_getstr(cfg_general, colorstr));
else if (output_format == O_XMOBAR)
(void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", cfg_getstr(cfg_general, colorstr));
+ else if (output_format == O_I3BAR)
+ (void)snprintf(colorbuf, sizeof(colorbuf), "\"color\":\"%s\", ", cfg_getstr(cfg_general, colorstr));
return colorbuf;
}
printf("^fg(%s)^p(5;-2)^ro(2)^p()^fg()^p(5)", cfg_getstr(cfg_general, "color_separator"));
else if (output_format == O_XMOBAR)
printf("<fc=%s> | </fc>", cfg_getstr(cfg_general, "color_separator"));
+ else if (output_format == O_I3BAR)
+ printf(", ");
else if (output_format == O_NONE)
printf(" | ");
}
memset(remainingbuf, '\0', sizeof(remainingbuf));
memset(emptytimebuf, '\0', sizeof(emptytimebuf));
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"battery\", \"instance\": \"%s\", \"full_text\":\"", path);
+
#if defined(LINUX)
static char batpath[512];
sprintf(batpath, path, number);
walk += strlen("emptytime");
}
}
+
+ if (output_format == O_I3BAR)
+ printf("\"}");
}
path = thermal_zone;
}
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"cpu_temperature\", \"instance\": \"%s\", \"full_text\":\"", path);
+
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
putchar(*walk);
walk += strlen("degrees");
}
}
+
+ if (output_format == O_I3BAR)
+ printf("\"}");
+
return;
error:
#endif
int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
int diff_idle, diff_total, diff_usage;
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"cpu_usage\", \"full_text\":\"");
+
#if defined(LINUX)
static char statpath[512];
strcpy(statpath, "/proc/stat");
walk += strlen("usage");
}
}
+
+ if (output_format == O_I3BAR)
+ printf("\"}");
+
return;
error:
(void)fputs("Cannot read usage\n", stderr);
#include <stdlib.h>
#include <string.h>
+#include "i3status.h"
+
/* define fixed output-Strings */
char *season_long[5] = {
"Chaos",
if (form == NULL)
if ((form = malloc(strlen(format) + 1)) == NULL)
return;
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"ddate\", \"full_text\":\"");
strcpy(form, format);
format_output(form, dt);
+ if (output_format == O_I3BAR)
+ printf("\"}");
}
void print_disk_info(const char *path, const char *format) {
const char *walk;
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"disk_info\", \"instance\": \"%s\", \"full_text\":\"", path);
+
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
struct statfs buf;
walk += strlen("avail");
}
}
+
+ if (output_format == O_I3BAR)
+ printf("\"}");
}
const char *walk;
const char *ip_address = get_ip_addr(interface);
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"ethernet\", \"instance\": \"%s\", ", interface);
+
if (ip_address == NULL) {
printf("%s", color("color_bad"));
printf("%s", format_down);
printf("%s", color("color_good"));
}
+ if (output_format == O_I3BAR)
+ printf("\"full_text\":\"");
+
for (walk = format_up; *walk != '\0'; walk++) {
if (*walk != '%') {
putchar(*walk);
}
(void)printf("%s", endcolor());
+
+ if (output_format == O_I3BAR)
+ printf("\"}");
}
#include <string.h>
#include <arpa/inet.h>
+#include "i3status.h"
+
static char *get_sockname(struct addrinfo *addr) {
static char buf[INET6_ADDRSTRLEN+1];
struct sockaddr_storage local;
const char *walk;
char *addr_string = get_ipv6_addr();
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"ipv6\", \"full_text\":\"");
+
if (addr_string == NULL) {
printf("%s", format_down);
return;
walk += strlen("ip");
}
}
+
+ if (output_format == O_I3BAR)
+ printf("\"}");
}
void print_load(const char *format) {
/* Get load */
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"load\", \"full_text\":\"");
+
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun)
double loadavg[3];
const char *walk;
walk += strlen("15min");
}
}
+
+ if (output_format == O_I3BAR)
+ printf("\"}");
+
return;
error:
#endif
bool running = process_runs(pidfile);
const char *walk;
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"run_watch\", \"instance\": \"%s\", ", pidfile);
+
printf("%s", (running ? color("color_good") : color("color_bad")));
+ if (output_format == O_I3BAR)
+ printf("\"full_text\":\"");
+
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
putchar(*walk);
}
printf("%s", endcolor());
+
+ if (output_format == O_I3BAR)
+ printf("\"}");
}
#include <stdio.h>
#include <stdlib.h>
+#include "i3status.h"
+
void print_time(const char *format, struct tm *current_tm) {
static char part[512];
/* Get date & time */
if (current_tm == NULL) {
return;
}
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"time\", \"full_text\":\"");
(void)strftime(part, sizeof(part), format, current_tm);
printf("%s", part);
+ if (output_format == O_I3BAR)
+ printf("\"}");
}
void print_volume(const char *fmt, const char *device, const char *mixer, int mixer_idx) {
/* Printing volume only works with ALSA at the moment */
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"volume\", \"instance\": \"%s.%s.%d\", \"full_text\":\"", device, mixer, mixer_idx);
#ifdef LINUX
/* Check if we already opened the mixer and get the handle
* from cache if so */
}
close(mixfd);
#endif
+ if (output_format == O_I3BAR)
+ printf("\"}");
}
void print_wireless_info(const char *interface, const char *format_up, const char *format_down) {
const char *walk;
wireless_info_t info;
+ if (output_format == O_I3BAR)
+ printf("{\"name\":\"wireless\", \"instance\": \"%s\", ", interface);
+
if (get_wireless_info(interface, &info)) {
walk = format_up;
if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY)
printf("%s", color("color_bad"));
}
+ if (output_format == O_I3BAR)
+ printf("\"full_text\":\"");
+
for (; *walk != '\0'; walk++) {
if (*walk != '%') {
putchar(*walk);
}
(void)printf("%s", endcolor());
+
+ if (output_format == O_I3BAR)
+ printf("\"}");
}