]> git.sur5r.net Git - i3/i3status/blob - include/i3status.h
559df9e2cdd213c44afdc6ae13d74eecff213fff
[i3/i3status] / include / i3status.h
1 #ifndef _I3STATUS_H
2 #define _I3STATUS_H
3
4 enum { O_DZEN2, O_XMOBAR, O_I3BAR, 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__)
22
23 /* this needs the coretemp module to be loaded */
24 #define THERMAL_ZONE "dev.cpu.%d.temperature"
25 #define BATT_LIFE "hw.acpi.battery.life"
26 #define BATT_TIME "hw.acpi.battery.time"
27 #define BATT_STATE "hw.acpi.battery.state"
28
29 #elif defined(__OpenBSD__)
30 /* Default to acpitz(4) if no path is set. */
31 #define THERMAL_ZONE "acpitz%d"
32 #endif
33
34 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
35
36 #include <sys/stat.h>
37 #include <sys/param.h>
38
39 #endif
40
41 /* Allows for the definition of a variable without opening a new scope, thus
42  * suited for usage in a macro. Idea from wmii. */
43 #define with(type, var, init) \
44         for (type var = (type)-1; (var == (type)-1) && ((var=(init)) || 1); )
45
46 #define CASE_SEC(name) \
47         if (BEGINS_WITH(current, name)) \
48                 with(cfg_t *, sec, cfg_section = cfg_getsec(cfg, name)) \
49                         if (sec != NULL)
50
51 #define CASE_SEC_TITLE(name) \
52         if (BEGINS_WITH(current, name)) \
53                 with(const char *, title, current + strlen(name) + 1) \
54                         with(cfg_t *, sec, cfg_section = cfg_gettsec(cfg, name, title)) \
55                                 if (sec != NULL)
56
57 /* Macro which any plugin can use to output the full_text part (when the output
58  * format is JSON) or just output to stdout (any other output format). */
59 #define OUTPUT_FULL_TEXT(text) \
60         do { \
61                 /* Terminate the output buffer here in any case, so that it’s \
62                  * not forgotten in the module */ \
63                 *outwalk = '\0'; \
64                 if (output_format == O_I3BAR) { \
65                         yajl_gen_string(json_gen, (const unsigned char *)"full_text", strlen("full_text")); \
66                         yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \
67                 } else { \
68                         printf("%s", text); \
69                 } \
70         } while (0)
71
72 #define SEC_OPEN_MAP(name) \
73         do { \
74                 if (output_format == O_I3BAR) { \
75                         yajl_gen_map_open(json_gen); \
76                         yajl_gen_string(json_gen, (const unsigned char *)"name", strlen("name")); \
77                         yajl_gen_string(json_gen, (const unsigned char *)name, strlen(name)); \
78                 } \
79         } while (0)
80
81 #define SEC_CLOSE_MAP \
82         do { \
83                 if (output_format == O_I3BAR) { \
84                         yajl_gen_map_close(json_gen); \
85                 } \
86         } while (0)
87
88 #define START_COLOR(colorstr) \
89         do { \
90                 if (cfg_getbool(cfg_general, "colors")) { \
91                         const char *_val = NULL; \
92                         if (cfg_section) \
93                                 _val = cfg_getstr(cfg_section, colorstr); \
94                         if (!_val) \
95                                 _val = cfg_getstr(cfg_general, colorstr); \
96                         if (output_format == O_I3BAR) { \
97                                 yajl_gen_string(json_gen, (const unsigned char *)"color", strlen("color")); \
98                                 yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \
99                         } else { \
100                                 outwalk += sprintf(outwalk, "%s", color(colorstr)); \
101                         } \
102                 } \
103         } while (0)
104
105 #define END_COLOR \
106         do { \
107                 if (cfg_getbool(cfg_general, "colors") && output_format != O_I3BAR) { \
108                         outwalk += sprintf(outwalk, "%s", endcolor()); \
109                 } \
110         } while (0)
111
112 #define INSTANCE(instance) \
113         do { \
114                 if (output_format == O_I3BAR) { \
115                         yajl_gen_string(json_gen, (const unsigned char *)"instance", strlen("instance")); \
116                         yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \
117                 } \
118         } while (0)
119
120
121 typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t;
122
123 /* src/general.c */
124 char *skip_character(char *input, char character, int amount);
125 void die(const char *fmt, ...);
126 bool slurp(const char *filename, char *destination, int size);
127
128 /* src/output.c */
129 void print_seperator();
130 char *color(const char *colorstr);
131 char *endcolor() __attribute__ ((pure));
132
133 /* src/auto_detect_format.c */
134 char *auto_detect_format();
135
136 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
137 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format);
138 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int low_threshold, char *threshold_type, bool last_full_capacity);
139 void print_time(yajl_gen json_gen, char *buffer, const char *format, struct tm *current_tm);
140 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, struct tm *current_tm);
141 const char *get_ip_addr();
142 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
143 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
144 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
145 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
146 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
147 void print_load(yajl_gen json_gen, char *buffer, const char *format);
148 void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *device, const char *mixer, int mixer_idx);
149 bool process_runs(const char *path);
150
151 /* socket file descriptor for general purposes */
152 extern int general_socket;
153
154 extern cfg_t *cfg, *cfg_general, *cfg_section;
155
156 #endif