12 extern output_format_t output_format;
18 extern markup_format_t markup_format;
20 extern char *pct_mark;
25 #include <yajl/yajl_gen.h>
26 #include <yajl/yajl_version.h>
32 #define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
33 #define max(a, b) ((a) > (b) ? (a) : (b))
35 #define DEFAULT_SINK_INDEX UINT32_MAX
36 #define COMPOSE_VOLUME_MUTE(vol, mute) ((vol) | ((mute) ? (1 << 30) : 0))
37 #define DECOMPOSE_VOLUME(cvol) ((cvol) & ~(1 << 30))
38 #define DECOMPOSE_MUTED(cvol) (((cvol) & (1 << 30)) != 0)
42 #define THERMAL_ZONE "/sys/class/thermal/thermal_zone%d/temp"
44 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
46 /* this needs the coretemp module to be loaded */
47 #if defined(__DragonFly__)
48 #define THERMAL_ZONE "hw.sensors.cpu%d.temp0"
50 #define THERMAL_ZONE "dev.cpu.%d.temperature"
52 #define BATT_LIFE "hw.acpi.battery.life"
53 #define BATT_TIME "hw.acpi.battery.time"
54 #define BATT_STATE "hw.acpi.battery.state"
56 #elif defined(__OpenBSD__) || defined(__NetBSD__)
57 /* Default to acpitz(4) if no path is set. */
58 #define THERMAL_ZONE "acpitz%d"
61 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
64 #include <sys/param.h>
68 /* Allows for the definition of a variable without opening a new scope, thus
69 * suited for usage in a macro. Idea from wmii. */
70 #define with(type, var, init) \
71 for (type var = (type)-1; (var == (type)-1) && ((var = (init)) || 1); var = (type)1)
73 #define CASE_SEC(name) \
74 if (BEGINS_WITH(current, name)) \
75 with(cfg_t *, sec, cfg_section = cfg_getsec(cfg, name)) if (sec != NULL)
77 #define CASE_SEC_TITLE(name) \
78 if (BEGINS_WITH(current, name)) \
79 with(const char *, title, current + strlen(name) + 1) \
80 with(cfg_t *, sec, cfg_section = cfg_gettsec(cfg, name, title)) if (sec != NULL)
82 /* Macro which any plugin can use to output the full_text part (when the output
83 * format is JSON) or just output to stdout (any other output format). */
84 #define OUTPUT_FULL_TEXT(text) \
86 /* Terminate the output buffer here in any case, so that it’s \
87 * not forgotten in the module */ \
89 if (output_format == O_I3BAR) { \
90 char *_markup = cfg_getstr(cfg_general, "markup"); \
91 yajl_gen_string(json_gen, (const unsigned char *) "markup", strlen("markup")); \
92 yajl_gen_string(json_gen, (const unsigned char *)_markup, strlen(_markup)); \
93 yajl_gen_string(json_gen, (const unsigned char *) "full_text", strlen("full_text")); \
94 yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \
100 #define SEC_OPEN_MAP(name) \
102 if (output_format == O_I3BAR) { \
103 yajl_gen_map_open(json_gen); \
104 yajl_gen_string(json_gen, (const unsigned char *) "name", strlen("name")); \
105 yajl_gen_string(json_gen, (const unsigned char *)name, strlen(name)); \
109 #define SEC_CLOSE_MAP \
111 if (output_format == O_I3BAR) { \
112 char *_align = cfg_getstr(sec, "align"); \
114 yajl_gen_string(json_gen, (const unsigned char *) "align", strlen("align")); \
115 yajl_gen_string(json_gen, (const unsigned char *)_align, strlen(_align)); \
117 struct min_width *_width = cfg_getptr(sec, "min_width"); \
119 /* if the value can be parsed as a number, we use the numerical value */ \
120 if (_width->num > 0) { \
121 yajl_gen_string(json_gen, (const unsigned char *) "min_width", strlen("min_width")); \
122 yajl_gen_integer(json_gen, _width->num); \
124 yajl_gen_string(json_gen, (const unsigned char *) "min_width", strlen("min_width")); \
125 yajl_gen_string(json_gen, (const unsigned char *)_width->str, strlen(_width->str)); \
128 if (cfg_size(sec, "separator") > 0) { \
129 yajl_gen_string(json_gen, (const unsigned char *) "separator", strlen("separator")); \
130 yajl_gen_bool(json_gen, cfg_getbool(sec, "separator")); \
132 if (cfg_size(sec, "separator_block_width") > 0) { \
133 yajl_gen_string(json_gen, (const unsigned char *) "separator_block_width", strlen("separator_block_width")); \
134 yajl_gen_integer(json_gen, cfg_getint(sec, "separator_block_width")); \
136 const char *_sep = cfg_getstr(cfg_general, "separator"); \
137 if (strlen(_sep) == 0) { \
138 yajl_gen_string(json_gen, (const unsigned char *) "separator", strlen("separator")); \
139 yajl_gen_bool(json_gen, false); \
141 yajl_gen_map_close(json_gen); \
145 #define START_COLOR(colorstr) \
147 if (cfg_getbool(cfg_general, "colors")) { \
148 const char *_val = NULL; \
150 _val = cfg_getstr(cfg_section, colorstr); \
152 _val = cfg_getstr(cfg_general, colorstr); \
153 if (output_format == O_I3BAR) { \
154 yajl_gen_string(json_gen, (const unsigned char *) "color", strlen("color")); \
155 yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \
157 outwalk += sprintf(outwalk, "%s", color(colorstr)); \
164 if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
165 outwalk += sprintf(outwalk, "%s", endcolor()); \
169 #define INSTANCE(instance) \
171 if (output_format == O_I3BAR) { \
172 yajl_gen_string(json_gen, (const unsigned char *) "instance", strlen("instance")); \
173 yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \
178 * The "min_width" module option may either be defined as a string or a number.
185 char *sstrdup(const char *str);
188 char *skip_character(char *input, char character, int amount);
190 void die(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn));
191 bool slurp(const char *filename, char *destination, int size);
194 void print_separator(const char *separator);
195 char *color(const char *colorstr);
196 char *endcolor() __attribute__((pure));
197 void reset_cursor(void);
198 void maybe_escape_markup(char *text, char **buffer);
200 /* src/auto_detect_format.c */
201 char *auto_detect_format();
203 /* src/print_time.c */
204 void set_timezone(const char *tz);
206 /* src/first_network_device.c */
208 NET_TYPE_WIRELESS = 0,
209 NET_TYPE_ETHERNET = 1,
212 const char *first_eth_interface(const net_type_t type);
214 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
215 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold);
216 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);
217 void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, time_t t);
218 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
219 const char *get_ip_addr(const char *interface, int family);
220 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
221 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down);
222 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
223 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, const char *format_above_threshold, int);
224 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const char *format_above_degraded_threshold, const char *path, const float max_threshold, const float degraded_threshold);
225 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
226 void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold);
227 void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method);
228 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);
229 bool process_runs(const char *path);
230 int volume_pulseaudio(uint32_t sink_idx, const char *sink_name);
231 bool pulse_initialize(void);
233 /* socket file descriptor for general purposes */
234 extern int general_socket;
236 extern cfg_t *cfg, *cfg_general, *cfg_section;
238 extern void **cur_instance;
240 extern pthread_t main_thread;