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