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