]> git.sur5r.net Git - i3/i3status/blob - include/i3status.h
Adding the option "format_down" to path_exists and run_watch.
[i3/i3status] / include / i3status.h
1 #ifndef _I3STATUS_H
2 #define _I3STATUS_H
3
4 enum { O_DZEN2,
5        O_XMOBAR,
6        O_I3BAR,
7        O_TERM,
8        O_NONE } output_format;
9
10 #include <stdbool.h>
11 #include <confuse.h>
12 #include <time.h>
13 #include <yajl/yajl_gen.h>
14 #include <yajl/yajl_version.h>
15 #include <unistd.h>
16 #include <string.h>
17
18 #define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
19 #define max(a, b) ((a) > (b) ? (a) : (b))
20
21 #if defined(LINUX)
22
23 #define THERMAL_ZONE "/sys/class/thermal/thermal_zone%d/temp"
24
25 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
26
27 /* this needs the coretemp module to be loaded */
28 #if defined(__DragonFly__)
29 #define THERMAL_ZONE "hw.sensors.cpu%d.temp0"
30 #else
31 #define THERMAL_ZONE "dev.cpu.%d.temperature"
32 #endif
33 #define BATT_LIFE "hw.acpi.battery.life"
34 #define BATT_TIME "hw.acpi.battery.time"
35 #define BATT_STATE "hw.acpi.battery.state"
36
37 #elif defined(__OpenBSD__) || defined(__NetBSD__)
38 /* Default to acpitz(4) if no path is set. */
39 #define THERMAL_ZONE "acpitz%d"
40 #endif
41
42 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
43
44 #include <sys/stat.h>
45 #include <sys/param.h>
46
47 #endif
48
49 /* Allows for the definition of a variable without opening a new scope, thus
50  * suited for usage in a macro. Idea from wmii. */
51 #define with(type, var, init) \
52     for (type var = (type)-1; (var == (type)-1) && ((var = (init)) || 1);)
53
54 #define CASE_SEC(name)              \
55     if (BEGINS_WITH(current, name)) \
56     with(cfg_t *, sec, cfg_section = cfg_getsec(cfg, name)) 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)) if (sec != NULL)
62
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)                                                                   \
66     do {                                                                                         \
67         /* Terminate the output buffer here in any case, so that it’s                          \
68          * not forgotten in the module */                                                        \
69         *outwalk = '\0';                                                                         \
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));                \
73         } else {                                                                                 \
74             printf("%s", text);                                                                  \
75         }                                                                                        \
76     } while (0)
77
78 #define SEC_OPEN_MAP(name)                                                             \
79     do {                                                                               \
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));      \
84         }                                                                              \
85     } while (0)
86
87 #define SEC_CLOSE_MAP                                                                                    \
88     do {                                                                                                 \
89         if (output_format == O_I3BAR) {                                                                  \
90             char *_align = cfg_getstr(sec, "align");                                                     \
91             if (_align) {                                                                                \
92                 yajl_gen_string(json_gen, (const unsigned char *) "align", strlen("align"));             \
93                 yajl_gen_string(json_gen, (const unsigned char *)_align, strlen(_align));                \
94             }                                                                                            \
95             struct min_width *_width = cfg_getptr(sec, "min_width");                                     \
96             if (_width) {                                                                                \
97                 /* if the value can be parsed as a number, we use the numerical value */                 \
98                 if (_width->num > 0) {                                                                   \
99                     yajl_gen_string(json_gen, (const unsigned char *) "min_width", strlen("min_width")); \
100                     yajl_gen_integer(json_gen, _width->num);                                             \
101                 } else {                                                                                 \
102                     yajl_gen_string(json_gen, (const unsigned char *) "min_width", strlen("min_width")); \
103                     yajl_gen_string(json_gen, (const unsigned char *)_width->str, strlen(_width->str));  \
104                 }                                                                                        \
105             }                                                                                            \
106             const char *_sep = cfg_getstr(cfg_general, "separator");                                     \
107             if (strlen(_sep) == 0) {                                                                     \
108                 yajl_gen_string(json_gen, (const unsigned char *) "separator", strlen("separator"));     \
109                 yajl_gen_bool(json_gen, false);                                                          \
110             }                                                                                            \
111             yajl_gen_map_close(json_gen);                                                                \
112         }                                                                                                \
113     } while (0)
114
115 #define START_COLOR(colorstr)                                                                \
116     do {                                                                                     \
117         if (cfg_getbool(cfg_general, "colors")) {                                            \
118             const char *_val = NULL;                                                         \
119             if (cfg_section)                                                                 \
120                 _val = cfg_getstr(cfg_section, colorstr);                                    \
121             if (!_val)                                                                       \
122                 _val = cfg_getstr(cfg_general, colorstr);                                    \
123             if (output_format == O_I3BAR) {                                                  \
124                 yajl_gen_string(json_gen, (const unsigned char *) "color", strlen("color")); \
125                 yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val));        \
126             } else {                                                                         \
127                 outwalk += sprintf(outwalk, "%s", color(colorstr));                          \
128             }                                                                                \
129         }                                                                                    \
130     } while (0)
131
132 #define END_COLOR                                                             \
133     do {                                                                      \
134         if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
135             outwalk += sprintf(outwalk, "%s", endcolor());                    \
136         }                                                                     \
137     } while (0)
138
139 #define INSTANCE(instance)                                                                     \
140     do {                                                                                       \
141         if (output_format == O_I3BAR) {                                                        \
142             yajl_gen_string(json_gen, (const unsigned char *) "instance", strlen("instance")); \
143             yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance));      \
144         }                                                                                      \
145     } while (0)
146
147 typedef enum { CS_DISCHARGING,
148                CS_CHARGING,
149                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 /* src/first_network_device.c */
177 typedef enum {
178     NET_TYPE_WIRELESS = 0,
179     NET_TYPE_ETHERNET = 1
180 } net_type_t;
181 const char *first_eth_interface(const net_type_t type);
182
183 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
184 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);
185 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);
186 void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, time_t t);
187 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
188 const char *get_ip_addr(const char *interface);
189 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
190 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down);
191 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
192 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
193 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
194 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
195 void print_load(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold);
196 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);
197 bool process_runs(const char *path);
198
199 /* socket file descriptor for general purposes */
200 extern int general_socket;
201
202 extern cfg_t *cfg, *cfg_general, *cfg_section;
203
204 extern void **cur_instance;
205
206 #endif