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