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