]> git.sur5r.net Git - i3/i3status/blob - include/i3status.h
f3a894126c649cd73f2bc137f05f7cdf59f71531
[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 #define MAX_SINK_DESCRIPTION_LEN (128) /* arbitrary */
40
41 #if defined(__linux__)
42
43 #define THERMAL_ZONE "/sys/class/thermal/thermal_zone%d/temp"
44
45 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
46
47 /* this needs the coretemp module to be loaded */
48 #if defined(__DragonFly__)
49 #define THERMAL_ZONE "hw.sensors.cpu%d.temp0"
50 #else
51 #define THERMAL_ZONE "dev.cpu.%d.temperature"
52 #endif
53 #define BATT_LIFE "hw.acpi.battery.life"
54 #define BATT_TIME "hw.acpi.battery.time"
55 #define BATT_STATE "hw.acpi.battery.state"
56
57 #elif defined(__OpenBSD__) || defined(__NetBSD__)
58 /* Default to acpitz(4) if no path is set. */
59 #define THERMAL_ZONE "acpitz%d"
60 #endif
61
62 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
63
64 #include <sys/stat.h>
65 #include <sys/param.h>
66
67 #endif
68
69 /* Allows for the definition of a variable without opening a new scope, thus
70  * suited for usage in a macro. Idea from wmii. */
71 #define with(type, var, init) \
72     for (type var = (type)-1; (var == (type)-1) && ((var = (init)) || 1); var = (type)1)
73
74 #define CASE_SEC(name)              \
75     if (BEGINS_WITH(current, name)) \
76     with(cfg_t *, sec, cfg_section = cfg_getsec(cfg, name)) if (sec != NULL)
77
78 #define CASE_SEC_TITLE(name)                              \
79     if (BEGINS_WITH(current, name))                       \
80     with(const char *, title, current + strlen(name) + 1) \
81         with(cfg_t *, sec, cfg_section = cfg_gettsec(cfg, name, title)) if (sec != NULL)
82
83 /* Macro which any plugin can use to output the full_text part (when the output
84  * format is JSON) or just output to stdout (any other output format). */
85 #define OUTPUT_FULL_TEXT(text)                                                                  \
86     do {                                                                                        \
87         /* Terminate the output buffer here in any case, so that it’s                         \
88          * not forgotten in the module */                                                       \
89         *outwalk = '\0';                                                                        \
90         if (output_format == O_I3BAR) {                                                         \
91             char *_markup = cfg_getstr(cfg_general, "markup");                                  \
92             yajl_gen_string(json_gen, (const unsigned char *)"markup", strlen("markup"));       \
93             yajl_gen_string(json_gen, (const unsigned char *)_markup, strlen(_markup));         \
94             yajl_gen_string(json_gen, (const unsigned char *)"full_text", strlen("full_text")); \
95             yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text));               \
96         } else {                                                                                \
97             printf("%s", text);                                                                 \
98         }                                                                                       \
99     } while (0)
100
101 #define SEC_OPEN_MAP(name)                                                            \
102     do {                                                                              \
103         if (output_format == O_I3BAR) {                                               \
104             yajl_gen_map_open(json_gen);                                              \
105             yajl_gen_string(json_gen, (const unsigned char *)"name", strlen("name")); \
106             yajl_gen_string(json_gen, (const unsigned char *)name, strlen(name));     \
107         }                                                                             \
108     } while (0)
109
110 #define SEC_CLOSE_MAP                                                                                                       \
111     do {                                                                                                                    \
112         if (output_format == O_I3BAR) {                                                                                     \
113             char *_align = cfg_getstr(sec, "align");                                                                        \
114             if (_align) {                                                                                                   \
115                 yajl_gen_string(json_gen, (const unsigned char *)"align", strlen("align"));                                 \
116                 yajl_gen_string(json_gen, (const unsigned char *)_align, strlen(_align));                                   \
117             }                                                                                                               \
118             struct min_width *_width = cfg_getptr(sec, "min_width");                                                        \
119             if (_width) {                                                                                                   \
120                 /* if the value can be parsed as a number, we use the numerical value */                                    \
121                 if (_width->num > 0) {                                                                                      \
122                     yajl_gen_string(json_gen, (const unsigned char *)"min_width", strlen("min_width"));                     \
123                     yajl_gen_integer(json_gen, _width->num);                                                                \
124                 } else {                                                                                                    \
125                     yajl_gen_string(json_gen, (const unsigned char *)"min_width", strlen("min_width"));                     \
126                     yajl_gen_string(json_gen, (const unsigned char *)_width->str, strlen(_width->str));                     \
127                 }                                                                                                           \
128             }                                                                                                               \
129             if (cfg_size(sec, "separator") > 0) {                                                                           \
130                 yajl_gen_string(json_gen, (const unsigned char *)"separator", strlen("separator"));                         \
131                 yajl_gen_bool(json_gen, cfg_getbool(sec, "separator"));                                                     \
132             }                                                                                                               \
133             if (cfg_size(sec, "separator_block_width") > 0) {                                                               \
134                 yajl_gen_string(json_gen, (const unsigned char *)"separator_block_width", strlen("separator_block_width")); \
135                 yajl_gen_integer(json_gen, cfg_getint(sec, "separator_block_width"));                                       \
136             }                                                                                                               \
137             const char *_sep = cfg_getstr(cfg_general, "separator");                                                        \
138             if (strlen(_sep) == 0) {                                                                                        \
139                 yajl_gen_string(json_gen, (const unsigned char *)"separator", strlen("separator"));                         \
140                 yajl_gen_bool(json_gen, false);                                                                             \
141             }                                                                                                               \
142             yajl_gen_map_close(json_gen);                                                                                   \
143         }                                                                                                                   \
144     } while (0)
145
146 #define START_COLOR(colorstr)                                                               \
147     do {                                                                                    \
148         if (cfg_getbool(cfg_general, "colors")) {                                           \
149             const char *_val = NULL;                                                        \
150             if (cfg_section)                                                                \
151                 _val = cfg_getstr(cfg_section, colorstr);                                   \
152             if (!_val)                                                                      \
153                 _val = cfg_getstr(cfg_general, colorstr);                                   \
154             if (output_format == O_I3BAR) {                                                 \
155                 yajl_gen_string(json_gen, (const unsigned char *)"color", strlen("color")); \
156                 yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val));       \
157             } else {                                                                        \
158                 outwalk += sprintf(outwalk, "%s", color(colorstr));                         \
159             }                                                                               \
160         }                                                                                   \
161     } while (0)
162
163 #define END_COLOR                                                             \
164     do {                                                                      \
165         if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
166             outwalk += sprintf(outwalk, "%s", endcolor());                    \
167         }                                                                     \
168     } while (0)
169
170 #define INSTANCE(instance)                                                                    \
171     do {                                                                                      \
172         if (output_format == O_I3BAR) {                                                       \
173             yajl_gen_string(json_gen, (const unsigned char *)"instance", strlen("instance")); \
174             yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance));     \
175         }                                                                                     \
176     } while (0)
177
178 /*
179  * The "min_width" module option may either be defined as a string or a number.
180  */
181 struct min_width {
182     long num;
183     const char *str;
184 };
185
186 char *sstrdup(const char *str);
187
188 /* src/general.c */
189 char *skip_character(char *input, char character, int amount);
190
191 void die(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn));
192 bool slurp(const char *filename, char *destination, int size);
193
194 /* src/output.c */
195 void print_separator(const char *separator);
196 char *color(const char *colorstr);
197 char *endcolor() __attribute__((pure));
198 void reset_cursor(void);
199 void maybe_escape_markup(char *text, char **buffer);
200
201 /* src/auto_detect_format.c */
202 char *auto_detect_format();
203
204 /* src/print_time.c */
205 void set_timezone(const char *tz);
206
207 /* src/first_network_device.c */
208 typedef enum {
209     NET_TYPE_WIRELESS = 0,
210     NET_TYPE_ETHERNET = 1,
211     NET_TYPE_OTHER = 2
212 } net_type_t;
213 const char *first_eth_interface(const net_type_t type);
214
215 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
216 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);
217 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);
218 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, bool hide_if_equals_localtime, time_t t);
219 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
220 const char *get_ip_addr(const char *interface, int family);
221 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *quality_min_lenght);
222 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down);
223 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
224 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);
225 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);
226 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
227 void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold);
228 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);
229 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);
230 bool process_runs(const char *path);
231 int volume_pulseaudio(uint32_t sink_idx, const char *sink_name);
232 bool description_pulseaudio(uint32_t sink_idx, const char *sink_name, char buffer[MAX_SINK_DESCRIPTION_LEN]);
233 bool pulse_initialize(void);
234
235 /* socket file descriptor for general purposes */
236 extern int general_socket;
237
238 extern cfg_t *cfg, *cfg_general, *cfg_section;
239
240 extern void **cur_instance;
241
242 extern pthread_t main_thread;
243 #endif