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 const char *_sep = cfg_getstr(cfg_general, "separator"); \
123 if (strlen(_sep) == 0) { \
124 yajl_gen_string(json_gen, (const unsigned char *) "separator", strlen("separator")); \
125 yajl_gen_bool(json_gen, false); \
127 yajl_gen_map_close(json_gen); \
131 #define START_COLOR(colorstr) \
133 if (cfg_getbool(cfg_general, "colors")) { \
134 const char *_val = NULL; \
136 _val = cfg_getstr(cfg_section, colorstr); \
138 _val = cfg_getstr(cfg_general, colorstr); \
139 if (output_format == O_I3BAR) { \
140 yajl_gen_string(json_gen, (const unsigned char *) "color", strlen("color")); \
141 yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \
143 outwalk += sprintf(outwalk, "%s", color(colorstr)); \
150 if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
151 outwalk += sprintf(outwalk, "%s", endcolor()); \
155 #define INSTANCE(instance) \
157 if (output_format == O_I3BAR) { \
158 yajl_gen_string(json_gen, (const unsigned char *) "instance", strlen("instance")); \
159 yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \
163 typedef enum { CS_DISCHARGING,
165 CS_FULL } charging_status_t;
168 * The "min_width" module option may either be defined as a string or a number.
176 char *skip_character(char *input, char character, int amount);
177 void die(const char *fmt, ...);
178 bool slurp(const char *filename, char *destination, int size);
181 void print_separator(const char *separator);
182 char *color(const char *colorstr);
183 char *endcolor() __attribute__((pure));
184 void reset_cursor(void);
185 void maybe_escape_markup(char *text, char **buffer);
187 /* src/auto_detect_format.c */
188 char *auto_detect_format();
190 /* src/print_time.c */
191 void set_timezone(const char *tz);
193 /* src/first_network_device.c */
195 NET_TYPE_WIRELESS = 0,
196 NET_TYPE_ETHERNET = 1
198 const char *first_eth_interface(const net_type_t type);
200 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
201 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);
202 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_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds);
203 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);
204 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
205 const char *get_ip_addr(const char *interface);
206 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
207 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down);
208 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
209 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
210 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
211 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
212 void print_load(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold);
213 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);
214 bool process_runs(const char *path);
215 int volume_pulseaudio(uint32_t sink_idx);
216 bool pulse_initialize(void);
218 /* socket file descriptor for general purposes */
219 extern int general_socket;
221 extern cfg_t *cfg, *cfg_general, *cfg_section;
223 extern void **cur_instance;
225 extern pthread_t main_thread;