]> git.sur5r.net Git - i3/i3status/blob - include/i3status.h
Merge pull request #140 from tommie/multibatt
[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_LEMONBAR,
8        O_TERM,
9        O_NONE } output_format;
10
11 enum { M_PANGO,
12        M_NONE } markup_format;
13
14 char *pct_mark;
15
16 #include <stdbool.h>
17 #include <confuse.h>
18 #include <time.h>
19 #include <yajl/yajl_gen.h>
20 #include <yajl/yajl_version.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <stdint.h>
25
26 #define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
27 #define max(a, b) ((a) > (b) ? (a) : (b))
28
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)
33
34 #if defined(LINUX)
35
36 #define THERMAL_ZONE "/sys/class/thermal/thermal_zone%d/temp"
37
38 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
39
40 /* this needs the coretemp module to be loaded */
41 #if defined(__DragonFly__)
42 #define THERMAL_ZONE "hw.sensors.cpu%d.temp0"
43 #else
44 #define THERMAL_ZONE "dev.cpu.%d.temperature"
45 #endif
46 #define BATT_LIFE "hw.acpi.battery.life"
47 #define BATT_TIME "hw.acpi.battery.time"
48 #define BATT_STATE "hw.acpi.battery.state"
49
50 #elif defined(__OpenBSD__) || defined(__NetBSD__)
51 /* Default to acpitz(4) if no path is set. */
52 #define THERMAL_ZONE "acpitz%d"
53 #endif
54
55 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
56
57 #include <sys/stat.h>
58 #include <sys/param.h>
59
60 #endif
61
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)
66
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)
70
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)
75
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)                                                                   \
79     do {                                                                                         \
80         /* Terminate the output buffer here in any case, so that it’s                          \
81          * not forgotten in the module */                                                        \
82         *outwalk = '\0';                                                                         \
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));                \
89         } else {                                                                                 \
90             printf("%s", text);                                                                  \
91         }                                                                                        \
92     } while (0)
93
94 #define SEC_OPEN_MAP(name)                                                             \
95     do {                                                                               \
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));      \
100         }                                                                              \
101     } while (0)
102
103 #define SEC_CLOSE_MAP                                                                                                        \
104     do {                                                                                                                     \
105         if (output_format == O_I3BAR) {                                                                                      \
106             char *_align = cfg_getstr(sec, "align");                                                                         \
107             if (_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));                                    \
110             }                                                                                                                \
111             struct min_width *_width = cfg_getptr(sec, "min_width");                                                         \
112             if (_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);                                                                 \
117                 } else {                                                                                                     \
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));                      \
120                 }                                                                                                            \
121             }                                                                                                                \
122             if (cfg_size(sec, "separator") > 0) {                                                                            \
123                 yajl_gen_string(json_gen, (const unsigned char *) "separator", strlen("separator"));                         \
124                 yajl_gen_bool(json_gen, cfg_getbool(sec, "separator"));                                                      \
125             }                                                                                                                \
126             if (cfg_size(sec, "separator_block_width") > 0) {                                                                \
127                 yajl_gen_string(json_gen, (const unsigned char *) "separator_block_width", strlen("separator_block_width")); \
128                 yajl_gen_integer(json_gen, cfg_getint(sec, "separator_block_width"));                                        \
129             }                                                                                                                \
130             const char *_sep = cfg_getstr(cfg_general, "separator");                                                         \
131             if (strlen(_sep) == 0) {                                                                                         \
132                 yajl_gen_string(json_gen, (const unsigned char *) "separator", strlen("separator"));                         \
133                 yajl_gen_bool(json_gen, false);                                                                              \
134             }                                                                                                                \
135             yajl_gen_map_close(json_gen);                                                                                    \
136         }                                                                                                                    \
137     } while (0)
138
139 #define START_COLOR(colorstr)                                                                \
140     do {                                                                                     \
141         if (cfg_getbool(cfg_general, "colors")) {                                            \
142             const char *_val = NULL;                                                         \
143             if (cfg_section)                                                                 \
144                 _val = cfg_getstr(cfg_section, colorstr);                                    \
145             if (!_val)                                                                       \
146                 _val = cfg_getstr(cfg_general, colorstr);                                    \
147             if (output_format == O_I3BAR) {                                                  \
148                 yajl_gen_string(json_gen, (const unsigned char *) "color", strlen("color")); \
149                 yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val));        \
150             } else {                                                                         \
151                 outwalk += sprintf(outwalk, "%s", color(colorstr));                          \
152             }                                                                                \
153         }                                                                                    \
154     } while (0)
155
156 #define END_COLOR                                                             \
157     do {                                                                      \
158         if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
159             outwalk += sprintf(outwalk, "%s", endcolor());                    \
160         }                                                                     \
161     } while (0)
162
163 #define INSTANCE(instance)                                                                     \
164     do {                                                                                       \
165         if (output_format == O_I3BAR) {                                                        \
166             yajl_gen_string(json_gen, (const unsigned char *) "instance", strlen("instance")); \
167             yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance));      \
168         }                                                                                      \
169     } while (0)
170
171 /*
172  * The "min_width" module option may either be defined as a string or a number.
173  */
174 struct min_width {
175     long num;
176     const char *str;
177 };
178
179 /* src/general.c */
180 char *skip_character(char *input, char character, int amount);
181 void die(const char *fmt, ...);
182 bool slurp(const char *filename, char *destination, int size);
183
184 /* src/output.c */
185 void print_separator(const char *separator);
186 char *color(const char *colorstr);
187 char *endcolor() __attribute__((pure));
188 void reset_cursor(void);
189 void maybe_escape_markup(char *text, char **buffer);
190
191 /* src/auto_detect_format.c */
192 char *auto_detect_format();
193
194 /* src/print_time.c */
195 void set_timezone(const char *tz);
196
197 /* src/first_network_device.c */
198 typedef enum {
199     NET_TYPE_WIRELESS = 0,
200     NET_TYPE_ETHERNET = 1,
201     NET_TYPE_OTHER = 2
202 } net_type_t;
203 const char *first_eth_interface(const net_type_t type);
204
205 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
206 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);
207 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);
208 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);
209 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
210 const char *get_ip_addr(const char *interface);
211 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
212 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down);
213 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down);
214 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
215 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold, const float degraded_threshold);
216 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
217 void print_load(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold);
218 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);
219 bool process_runs(const char *path);
220 int volume_pulseaudio(uint32_t sink_idx);
221 bool pulse_initialize(void);
222
223 /* socket file descriptor for general purposes */
224 extern int general_socket;
225
226 extern cfg_t *cfg, *cfg_general, *cfg_section;
227
228 extern void **cur_instance;
229
230 extern pthread_t main_thread;
231 #endif