]> git.sur5r.net Git - i3/i3status/blob - include/i3status.h
disk: Colorize output when below given threshold
[i3/i3status] / include / i3status.h
1 #ifndef _I3STATUS_H
2 #define _I3STATUS_H
3
4 enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_TERM, O_NONE } output_format;
5
6 #include <stdbool.h>
7 #include <confuse.h>
8 #include <time.h>
9 #include <yajl/yajl_gen.h>
10 #include <yajl/yajl_version.h>
11 #include <unistd.h>
12 #include <string.h>
13
14 #define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
15 #define max(a, b) ((a) > (b) ? (a) : (b))
16
17 #if defined(LINUX)
18
19 #define THERMAL_ZONE "/sys/class/thermal/thermal_zone%d/temp"
20
21 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
22
23 /* this needs the coretemp module to be loaded */
24 #if defined(__DragonFly__)
25 #define THERMAL_ZONE "hw.sensors.cpu%d.temp0"
26 #else
27 #define THERMAL_ZONE "dev.cpu.%d.temperature"
28 #endif
29 #define BATT_LIFE "hw.acpi.battery.life"
30 #define BATT_TIME "hw.acpi.battery.time"
31 #define BATT_STATE "hw.acpi.battery.state"
32
33 #elif defined(__OpenBSD__)
34 /* Default to acpitz(4) if no path is set. */
35 #define THERMAL_ZONE "acpitz%d"
36 #elif defined(__NetBSD__)
37 /* Rely on envsys(4). The key of the sensor is generally cpu%d temperature */
38 #define THERMAL_ZONE "cpu%d temperature"
39 #endif
40
41 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
42
43 #include <sys/stat.h>
44 #include <sys/param.h>
45
46 #endif
47
48 /* Allows for the definition of a variable without opening a new scope, thus
49  * suited for usage in a macro. Idea from wmii. */
50 #define with(type, var, init) \
51         for (type var = (type)-1; (var == (type)-1) && ((var=(init)) || 1); )
52
53 #define CASE_SEC(name) \
54         if (BEGINS_WITH(current, name)) \
55                 with(cfg_t *, sec, cfg_section = cfg_getsec(cfg, name)) \
56                         if (sec != NULL)
57
58 #define CASE_SEC_TITLE(name) \
59         if (BEGINS_WITH(current, name)) \
60                 with(const char *, title, current + strlen(name) + 1) \
61                         with(cfg_t *, sec, cfg_section = cfg_gettsec(cfg, name, title)) \
62                                 if (sec != NULL)
63
64 /* Macro which any plugin can use to output the full_text part (when the output
65  * format is JSON) or just output to stdout (any other output format). */
66 #define OUTPUT_FULL_TEXT(text) \
67         do { \
68                 /* Terminate the output buffer here in any case, so that it’s \
69                  * not forgotten in the module */ \
70                 *outwalk = '\0'; \
71                 if (output_format == O_I3BAR) { \
72                         yajl_gen_string(json_gen, (const unsigned char *)"full_text", strlen("full_text")); \
73                         yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \
74                 } else { \
75                         printf("%s", text); \
76                 } \
77         } while (0)
78
79 #define SEC_OPEN_MAP(name) \
80         do { \
81                 if (output_format == O_I3BAR) { \
82                         yajl_gen_map_open(json_gen); \
83                         yajl_gen_string(json_gen, (const unsigned char *)"name", strlen("name")); \
84                         yajl_gen_string(json_gen, (const unsigned char *)name, strlen(name)); \
85                 } \
86         } while (0)
87
88 #define SEC_CLOSE_MAP \
89         do { \
90                 if (output_format == O_I3BAR) { \
91                         char *_align = cfg_getstr(sec, "align"); \
92                         if (_align) { \
93                                 yajl_gen_string(json_gen, (const unsigned char *)"align", strlen("align")); \
94                                 yajl_gen_string(json_gen, (const unsigned char *)_align, strlen(_align)); \
95                         } \
96                         struct min_width *_width = cfg_getptr(sec, "min_width"); \
97                         if (_width) { \
98                                 /* if the value can be parsed as a number, we use the numerical value */ \
99                                 if (_width->num > 0) { \
100                                         yajl_gen_string(json_gen, (const unsigned char *)"min_width", strlen("min_width")); \
101                                         yajl_gen_integer(json_gen, _width->num); \
102                                 } else { \
103                                         yajl_gen_string(json_gen, (const unsigned char *)"min_width", strlen("min_width")); \
104                                         yajl_gen_string(json_gen, (const unsigned char *)_width->str, strlen(_width->str)); \
105                                 } \
106                         } \
107                         const char *_sep = cfg_getstr(cfg_general, "separator"); \
108                         if (strlen(_sep) == 0) {\
109                                 yajl_gen_string(json_gen, (const unsigned char *)"separator", strlen("separator")); \
110                                 yajl_gen_bool(json_gen, false); \
111                         } \
112                         yajl_gen_map_close(json_gen); \
113                 } \
114         } while (0)
115
116 #define START_COLOR(colorstr) \
117         do { \
118                 if (cfg_getbool(cfg_general, "colors")) { \
119                         const char *_val = NULL; \
120                         if (cfg_section) \
121                                 _val = cfg_getstr(cfg_section, colorstr); \
122                         if (!_val) \
123                                 _val = cfg_getstr(cfg_general, colorstr); \
124                         if (output_format == O_I3BAR) { \
125                                 yajl_gen_string(json_gen, (const unsigned char *)"color", strlen("color")); \
126                                 yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \
127                         } else { \
128                                 outwalk += sprintf(outwalk, "%s", color(colorstr)); \
129                         } \
130                 } \
131         } while (0)
132
133 #define END_COLOR \
134         do { \
135                 if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
136                         outwalk += sprintf(outwalk, "%s", endcolor()); \
137                 } \
138         } while (0)
139
140 #define INSTANCE(instance) \
141         do { \
142                 if (output_format == O_I3BAR) { \
143                         yajl_gen_string(json_gen, (const unsigned char *)"instance", strlen("instance")); \
144                         yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \
145                 } \
146         } while (0)
147
148
149 typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t;
150
151 /*
152  * The "min_width" module option may either be defined as a string or a number.
153  */
154 struct min_width {
155     long num;
156     const char *str;
157 };
158
159 /* src/general.c */
160 char *skip_character(char *input, char character, int amount);
161 void die(const char *fmt, ...);
162 bool slurp(const char *filename, char *destination, int size);
163
164 /* src/output.c */
165 void print_separator(const char *separator);
166 char *color(const char *colorstr);
167 char *endcolor() __attribute__ ((pure));
168 void reset_cursor(void);
169
170 /* src/auto_detect_format.c */
171 char *auto_detect_format();
172
173 /* src/print_time.c */
174 void set_timezone(const char *tz);
175
176 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
177 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type, const char *threshold_type, const double low_threshold);
178 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds);
179 void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t);
180 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
181 const char *get_ip_addr();
182 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
183 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
184 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format);
185 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
186 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
187 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
188 void print_load(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold);
189 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);
190 bool process_runs(const char *path);
191
192 /* socket file descriptor for general purposes */
193 extern int general_socket;
194
195 extern cfg_t *cfg, *cfg_general, *cfg_section;
196
197 #endif