4 enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_NONE } output_format;
9 #include <yajl/yajl_gen.h>
10 #include <yajl/yajl_version.h>
14 #define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
15 #define max(a, b) ((a) > (b) ? (a) : (b))
19 #define THERMAL_ZONE "/sys/class/thermal/thermal_zone%d/temp"
21 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
23 /* this needs the coretemp module to be loaded */
24 #define THERMAL_ZONE "dev.cpu.%d.temperature"
25 #define BATT_LIFE "hw.acpi.battery.life"
26 #define BATT_TIME "hw.acpi.battery.time"
27 #define BATT_STATE "hw.acpi.battery.state"
29 #elif defined(__OpenBSD__)
31 * Due to the fact there are various ways to obtain a temperature reading, THERMAL_ZONE will need
32 * to be adjustable enough for those situations. As it can either be hw.sensors.cpu%d.temp0, or
33 * hw.sensors.acpitz%d.temp0 or even something different entirely within hw.sensors.%s.temp0.
34 * XXX: For now just check cpu%d.temp0. Adjust this later.
36 #define THERMAL_ZONE "cpu%d"
40 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
43 #include <sys/param.h>
47 /* Allows for the definition of a variable without opening a new scope, thus
48 * suited for usage in a macro. Idea from wmii. */
49 #define with(type, var, init) \
50 for (type var = (type)-1; (var == (type)-1) && ((var=(init)) || 1); )
52 #define CASE_SEC(name) \
53 if (BEGINS_WITH(current, name)) \
54 with(cfg_t *, sec, cfg_getsec(cfg, name)) \
57 #define CASE_SEC_TITLE(name) \
58 if (BEGINS_WITH(current, name)) \
59 with(const char *, title, current + strlen(name) + 1) \
60 with(cfg_t *, sec, cfg_gettsec(cfg, name, title)) \
63 /* Macro which any plugin can use to output the full_text part (when the output
64 * format is JSON) or just output to stdout (any other output format). */
65 #define OUTPUT_FULL_TEXT(text) \
67 /* Terminate the output buffer here in any case, so that it’s \
68 * not forgotten in the module */ \
70 if (output_format == O_I3BAR) { \
71 yajl_gen_string(json_gen, (const unsigned char *)"full_text", strlen("full_text")); \
72 yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \
78 #define SEC_OPEN_MAP(name) \
80 if (output_format == O_I3BAR) { \
81 yajl_gen_map_open(json_gen); \
82 yajl_gen_string(json_gen, (const unsigned char *)"name", strlen("name")); \
83 yajl_gen_string(json_gen, (const unsigned char *)name, strlen(name)); \
87 #define SEC_CLOSE_MAP \
89 if (output_format == O_I3BAR) { \
90 yajl_gen_map_close(json_gen); \
94 #define START_COLOR(colorstr) \
96 if (cfg_getbool(cfg_general, "colors")) { \
97 const char *val = cfg_getstr(cfg_general, colorstr); \
98 if (output_format == O_I3BAR) { \
99 yajl_gen_string(json_gen, (const unsigned char *)"color", strlen("color")); \
100 yajl_gen_string(json_gen, (const unsigned char *)val, strlen(val)); \
102 outwalk += sprintf(outwalk, "%s", color(colorstr)); \
109 if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
110 outwalk += sprintf(outwalk, "%s", endcolor()); \
114 #define INSTANCE(instance) \
116 if (output_format == O_I3BAR) { \
117 yajl_gen_string(json_gen, (const unsigned char *)"instance", strlen("instance")); \
118 yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \
123 typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t;
126 char *skip_character(char *input, char character, int amount);
127 void die(const char *fmt, ...);
128 bool slurp(const char *filename, char *destination, int size);
131 void print_seperator();
132 char *color(const char *colorstr);
133 char *endcolor() __attribute__ ((pure));
135 /* src/auto_detect_format.c */
136 char *auto_detect_format();
138 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
139 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format);
140 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int threshold, bool last_full_capacity);
141 void print_time(yajl_gen json_gen, char *buffer, const char *format, struct tm *current_tm);
142 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, struct tm *current_tm);
143 const char *get_ip_addr();
144 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
145 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
146 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format);
147 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
148 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
149 void print_load(yajl_gen json_gen, char *buffer, const char *format);
150 void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *device, const char *mixer, int mixer_idx);
151 bool process_runs(const char *path);
153 /* socket file descriptor for general purposes */
154 extern int general_socket;
156 extern cfg_t *cfg, *cfg_general;