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