9 O_NONE } output_format;
12 M_NONE } markup_format;
19 #include <yajl/yajl_gen.h>
20 #include <yajl/yajl_version.h>
26 #define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
27 #define max(a, b) ((a) > (b) ? (a) : (b))
29 #define DEFAULT_SINK_INDEX UINT32_MAX
30 #define COMPOSE_VOLUME_MUTE(vol, mute) ((vol) | ((mute) ? (1 << 30) : 0))
31 #define DECOMPOSE_VOLUME(cvol) ((cvol) & ~(1 << 30))
32 #define DECOMPOSE_MUTED(cvol) (((cvol) & (1 << 30)) != 0)
36 #define THERMAL_ZONE "/sys/class/thermal/thermal_zone%d/temp"
38 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
40 /* this needs the coretemp module to be loaded */
41 #if defined(__DragonFly__)
42 #define THERMAL_ZONE "hw.sensors.cpu%d.temp0"
44 #define THERMAL_ZONE "dev.cpu.%d.temperature"
46 #define BATT_LIFE "hw.acpi.battery.life"
47 #define BATT_TIME "hw.acpi.battery.time"
48 #define BATT_STATE "hw.acpi.battery.state"
50 #elif defined(__OpenBSD__) || defined(__NetBSD__)
51 /* Default to acpitz(4) if no path is set. */
52 #define THERMAL_ZONE "acpitz%d"
55 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
58 #include <sys/param.h>
62 /* Allows for the definition of a variable without opening a new scope, thus
63 * suited for usage in a macro. Idea from wmii. */
64 #define with(type, var, init) \
65 for (type var = (type)-1; (var == (type)-1) && ((var = (init)) || 1); var = (type)1)
67 #define CASE_SEC(name) \
68 if (BEGINS_WITH(current, name)) \
69 with(cfg_t *, sec, cfg_section = cfg_getsec(cfg, name)) if (sec != NULL)
71 #define CASE_SEC_TITLE(name) \
72 if (BEGINS_WITH(current, name)) \
73 with(const char *, title, current + strlen(name) + 1) \
74 with(cfg_t *, sec, cfg_section = cfg_gettsec(cfg, name, title)) if (sec != NULL)
76 /* Macro which any plugin can use to output the full_text part (when the output
77 * format is JSON) or just output to stdout (any other output format). */
78 #define OUTPUT_FULL_TEXT(text) \
80 /* Terminate the output buffer here in any case, so that it’s \
81 * not forgotten in the module */ \
83 if (output_format == O_I3BAR) { \
84 char *_markup = cfg_getstr(cfg_general, "markup"); \
85 yajl_gen_string(json_gen, (const unsigned char *) "markup", strlen("markup")); \
86 yajl_gen_string(json_gen, (const unsigned char *)_markup, strlen(_markup)); \
87 yajl_gen_string(json_gen, (const unsigned char *) "full_text", strlen("full_text")); \
88 yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \
94 #define SEC_OPEN_MAP(name) \
96 if (output_format == O_I3BAR) { \
97 yajl_gen_map_open(json_gen); \
98 yajl_gen_string(json_gen, (const unsigned char *) "name", strlen("name")); \
99 yajl_gen_string(json_gen, (const unsigned char *)name, strlen(name)); \
103 #define SEC_CLOSE_MAP \
105 if (output_format == O_I3BAR) { \
106 char *_align = cfg_getstr(sec, "align"); \
108 yajl_gen_string(json_gen, (const unsigned char *) "align", strlen("align")); \
109 yajl_gen_string(json_gen, (const unsigned char *)_align, strlen(_align)); \
111 struct min_width *_width = cfg_getptr(sec, "min_width"); \
113 /* if the value can be parsed as a number, we use the numerical value */ \
114 if (_width->num > 0) { \
115 yajl_gen_string(json_gen, (const unsigned char *) "min_width", strlen("min_width")); \
116 yajl_gen_integer(json_gen, _width->num); \
118 yajl_gen_string(json_gen, (const unsigned char *) "min_width", strlen("min_width")); \
119 yajl_gen_string(json_gen, (const unsigned char *)_width->str, strlen(_width->str)); \
122 if (cfg_size(sec, "separator") > 0) { \
123 yajl_gen_string(json_gen, (const unsigned char *) "separator", strlen("separator")); \
124 yajl_gen_bool(json_gen, cfg_getbool(sec, "separator")); \
126 if (cfg_size(sec, "separator_block_width") > 0) { \
127 yajl_gen_string(json_gen, (const unsigned char *) "separator_block_width", strlen("separator_block_width")); \
128 yajl_gen_integer(json_gen, cfg_getint(sec, "separator_block_width")); \
130 const char *_sep = cfg_getstr(cfg_general, "separator"); \
131 if (strlen(_sep) == 0) { \
132 yajl_gen_string(json_gen, (const unsigned char *) "separator", strlen("separator")); \
133 yajl_gen_bool(json_gen, false); \
135 yajl_gen_map_close(json_gen); \
139 #define START_COLOR(colorstr) \
141 if (cfg_getbool(cfg_general, "colors")) { \
142 const char *_val = NULL; \
144 _val = cfg_getstr(cfg_section, colorstr); \
146 _val = cfg_getstr(cfg_general, colorstr); \
147 if (output_format == O_I3BAR) { \
148 yajl_gen_string(json_gen, (const unsigned char *) "color", strlen("color")); \
149 yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \
151 outwalk += sprintf(outwalk, "%s", color(colorstr)); \
158 if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
159 outwalk += sprintf(outwalk, "%s", endcolor()); \
163 #define INSTANCE(instance) \
165 if (output_format == O_I3BAR) { \
166 yajl_gen_string(json_gen, (const unsigned char *) "instance", strlen("instance")); \
167 yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \
171 typedef enum { CS_DISCHARGING,
174 CS_FULL } charging_status_t;
177 * The "min_width" module option may either be defined as a string or a number.
185 char *skip_character(char *input, char character, int amount);
186 void die(const char *fmt, ...);
187 bool slurp(const char *filename, char *destination, int size);
190 void print_separator(const char *separator);
191 char *color(const char *colorstr);
192 char *endcolor() __attribute__((pure));
193 void reset_cursor(void);
194 void maybe_escape_markup(char *text, char **buffer);
196 /* src/auto_detect_format.c */
197 char *auto_detect_format();
199 /* src/print_time.c */
200 void set_timezone(const char *tz);
202 /* src/first_network_device.c */
204 NET_TYPE_WIRELESS = 0,
205 NET_TYPE_ETHERNET = 1
207 const char *first_eth_interface(const net_type_t type);
209 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
210 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold);
211 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds);
212 void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *format_time, time_t t);
213 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
214 const char *get_ip_addr(const char *interface);
215 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
216 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down);
217 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
218 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
219 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
220 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
221 void print_load(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold);
222 void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx);
223 bool process_runs(const char *path);
224 int volume_pulseaudio(uint32_t sink_idx);
225 bool pulse_initialize(void);
227 /* socket file descriptor for general purposes */
228 extern int general_socket;
230 extern cfg_t *cfg, *cfg_general, *cfg_section;
232 extern void **cur_instance;
234 extern pthread_t main_thread;