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