]> git.sur5r.net Git - i3/i3status/blob - include/i3status.h
Avoid assigning a new value to a var before using the old value
[i3/i3status] / include / i3status.h
1 #ifndef _I3STATUS_H
2 #define _I3STATUS_H
3
4 typedef enum {
5     O_DZEN2,
6     O_XMOBAR,
7     O_I3BAR,
8     O_LEMONBAR,
9     O_TERM,
10     O_NONE
11 } output_format_t;
12 extern output_format_t output_format;
13
14 typedef enum {
15     M_PANGO,
16     M_NONE
17 } markup_format_t;
18 extern markup_format_t markup_format;
19
20 extern char *pct_mark;
21
22 #include <stdbool.h>
23 #include <confuse.h>
24 #include <time.h>
25 #include <yajl/yajl_gen.h>
26 #include <yajl/yajl_version.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <stdint.h>
31
32 #define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
33 #define max(a, b) ((a) > (b) ? (a) : (b))
34
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)
39
40 #if defined(LINUX)
41
42 #define THERMAL_ZONE "/sys/class/thermal/thermal_zone%d/temp"
43
44 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
45
46 /* this needs the coretemp module to be loaded */
47 #if defined(__DragonFly__)
48 #define THERMAL_ZONE "hw.sensors.cpu%d.temp0"
49 #else
50 #define THERMAL_ZONE "dev.cpu.%d.temperature"
51 #endif
52 #define BATT_LIFE "hw.acpi.battery.life"
53 #define BATT_TIME "hw.acpi.battery.time"
54 #define BATT_STATE "hw.acpi.battery.state"
55
56 #elif defined(__OpenBSD__) || defined(__NetBSD__)
57 /* Default to acpitz(4) if no path is set. */
58 #define THERMAL_ZONE "acpitz%d"
59 #endif
60
61 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
62
63 #include <sys/stat.h>
64 #include <sys/param.h>
65
66 #endif
67
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)
72
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)
76
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)
81
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)                                                                   \
85     do {                                                                                         \
86         /* Terminate the output buffer here in any case, so that it’s                          \
87          * not forgotten in the module */                                                        \
88         *outwalk = '\0';                                                                         \
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));                \
95         } else {                                                                                 \
96             printf("%s", text);                                                                  \
97         }                                                                                        \
98     } while (0)
99
100 #define SEC_OPEN_MAP(name)                                                             \
101     do {                                                                               \
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));      \
106         }                                                                              \
107     } while (0)
108
109 #define SEC_CLOSE_MAP                                                                                                        \
110     do {                                                                                                                     \
111         if (output_format == O_I3BAR) {                                                                                      \
112             char *_align = cfg_getstr(sec, "align");                                                                         \
113             if (_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));                                    \
116             }                                                                                                                \
117             struct min_width *_width = cfg_getptr(sec, "min_width");                                                         \
118             if (_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);                                                                 \
123                 } else {                                                                                                     \
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));                      \
126                 }                                                                                                            \
127             }                                                                                                                \
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"));                                                      \
131             }                                                                                                                \
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"));                                        \
135             }                                                                                                                \
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);                                                                              \
140             }                                                                                                                \
141             yajl_gen_map_close(json_gen);                                                                                    \
142         }                                                                                                                    \
143     } while (0)
144
145 #define START_COLOR(colorstr)                                                                \
146     do {                                                                                     \
147         if (cfg_getbool(cfg_general, "colors")) {                                            \
148             const char *_val = NULL;                                                         \
149             if (cfg_section)                                                                 \
150                 _val = cfg_getstr(cfg_section, colorstr);                                    \
151             if (!_val)                                                                       \
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));        \
156             } else {                                                                         \
157                 outwalk += sprintf(outwalk, "%s", color(colorstr));                          \
158             }                                                                                \
159         }                                                                                    \
160     } while (0)
161
162 #define END_COLOR                                                             \
163     do {                                                                      \
164         if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
165             outwalk += sprintf(outwalk, "%s", endcolor());                    \
166         }                                                                     \
167     } while (0)
168
169 #define INSTANCE(instance)                                                                     \
170     do {                                                                                       \
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));      \
174         }                                                                                      \
175     } while (0)
176
177 /*
178  * The "min_width" module option may either be defined as a string or a number.
179  */
180 struct min_width {
181     long num;
182     const char *str;
183 };
184
185 char *sstrdup(const char *str);
186
187 /* src/general.c */
188 char *skip_character(char *input, char character, int amount);
189
190 void die(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn));
191 bool slurp(const char *filename, char *destination, int size);
192
193 /* src/output.c */
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);
199
200 /* src/auto_detect_format.c */
201 char *auto_detect_format();
202
203 /* src/print_time.c */
204 void set_timezone(const char *tz);
205
206 /* src/first_network_device.c */
207 typedef enum {
208     NET_TYPE_WIRELESS = 0,
209     NET_TYPE_ETHERNET = 1,
210     NET_TYPE_OTHER = 2
211 } net_type_t;
212 const char *first_eth_interface(const net_type_t type);
213
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);
232
233 /* socket file descriptor for general purposes */
234 extern int general_socket;
235
236 extern cfg_t *cfg, *cfg_general, *cfg_section;
237
238 extern void **cur_instance;
239
240 extern pthread_t main_thread;
241 #endif