]> git.sur5r.net Git - i3/i3status/blob - src/output.c
c153a01126dcf69ce0dc92a56c4f47090f9eeb7f
[i3/i3status] / src / output.c
1 // vim:ts=8:expandtab
2 #include <stdbool.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <dirent.h>
10
11 #include "i3status.h"
12
13 /*
14  * Writes an errormessage to statusbar
15  *
16  */
17 void write_error_to_statusbar(const char *message) {
18         cleanup_rbar_dir();
19         create_file("error");
20         write_to_statusbar("error", message, true);
21 }
22
23 /*
24  * Returns the correct color format for dzen (^fg(color)) or wmii (color <normcolors>)
25  *
26  */
27 char *color(const char *colorstr) {
28         static char colorbuf[32];
29 #ifdef DZEN
30         (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", colorstr);
31 #elif XMOBAR
32         (void)snprintf(colorbuf, sizeof(colorbuf), "<fc=%s>", colorstr);
33 #else
34         (void)snprintf(colorbuf, sizeof(colorbuf), "%s %s ", colorstr, wmii_normcolors);
35 #endif
36         return colorbuf;
37 }
38
39 /*
40  * Some color formats (xmobar) require to terminate colors again
41  *
42  */
43 char *endcolor() {
44 #ifdef XMOBAR
45         return "</fc>";
46 #else
47         return "";
48 #endif
49 }
50
51 /*
52  * Cleans wmii's /rbar directory by deleting all regular files
53  *
54  */
55 void cleanup_rbar_dir() {
56 #if defined(DZEN) || defined(XMOBAR)
57         return;
58 #endif
59         struct dirent *ent;
60         DIR *dir;
61         char pathbuf[strlen(wmii_path)+256+1];
62
63         if ((dir = opendir(wmii_path)) == NULL)
64                 exit(EXIT_FAILURE);
65
66         while ((ent = readdir(dir)) != NULL) {
67                 if (ent->d_type == DT_REG) {
68                         (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, ent->d_name);
69                         if (unlink(pathbuf) == -1)
70                                 exit(EXIT_FAILURE);
71                 }
72         }
73
74         (void)closedir(dir);
75 }
76
77 /*
78  * Creates the specified file in wmii's /rbar directory with
79  * correct modes and initializes colors if colormode is enabled
80  *
81  */
82 void create_file(const char *name) {
83 #if defined(DZEN) || defined(XMOBAR)
84         return;
85 #endif
86         char pathbuf[strlen(wmii_path)+256+1];
87         int fd;
88         int flags = O_CREAT | O_WRONLY;
89         struct stat statbuf;
90
91         (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
92
93         /* Overwrite file's contents if it exists */
94         if (stat(pathbuf, &statbuf) >= 0)
95                 flags |= O_TRUNC;
96
97         if ((fd = open(pathbuf, flags, S_IRUSR | S_IWUSR)) < 0)
98                 exit(EXIT_FAILURE);
99         if (use_colors) {
100                 char *tmp = color("#888888");
101                 if (write(fd, tmp, strlen(tmp)) != (ssize_t)strlen(tmp))
102                         exit(EXIT_FAILURE);
103         }
104         (void)close(fd);
105 }
106
107 /*
108  * Waits until wmii_path/rbar exists (= the filesystem gets mounted),
109  * cleans up all files and creates the needed files
110  *
111  */
112 void setup(void) {
113         unsigned int i;
114         char pathbuf[512];
115
116 #if !defined(DZEN) && !defined(XMOBAR)
117         struct stat statbuf;
118         /* Wait until wmii_path/rbar exists */
119         for (; stat(wmii_path, &statbuf) < 0; sleep(interval));
120 #endif
121 #define cf(orderidx, name) create_file(order_to_str(order[orderidx], name));
122
123         cleanup_rbar_dir();
124         if (wlan_interface)
125                 cf(ORDER_WLAN, "wlan");
126         if (eth_interface)
127                 cf(ORDER_ETH, "eth");
128         if (get_cpu_temperature)
129                 cf(ORDER_CPU_TEMPERATURE, "cpu_temperature");
130         cf(ORDER_LOAD, "load");
131         if (time_format)
132                 cf(ORDER_TIME, "time");
133         for (i = 0; i < num_run_watches; i += 2) {
134                 snprintf(pathbuf, sizeof(pathbuf), "%d%s", order[ORDER_RUN], run_watches[i]);
135                 create_file(pathbuf);
136         }
137 }
138
139 /*
140  * Writes the given message in the corresponding file in wmii's /rbar directory
141  *
142  */
143 void write_to_statusbar(const char *name, const char *message, bool final_entry) {
144 #ifdef DZEN
145         if (final_entry) {
146                 if (printf("%s^p(6)\n", message) < 0) {
147                         perror("printf");
148                         exit(1);
149                 }
150
151                 fflush(stdout);
152                 return;
153         }
154         if (printf("%s" BAR, message) < 0) {
155                 perror("printf");
156                 exit(1);
157         }
158         return;
159 #elif XMOBAR
160         if (final_entry) {
161                 if (printf("%s\n", message) < 0) {
162                         perror("printf");
163                         exit(1);
164                 }
165
166                 fflush(stdout);
167                 return;
168         }
169         if (printf("%s" BAR, message) < 0) {
170                 perror("printf");
171                 exit(1);
172         }
173         return;
174
175 #endif
176
177         char pathbuf[strlen(wmii_path)+256+1];
178         int fd;
179
180         (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
181         if ((fd = open(pathbuf, O_RDWR)) == -1) {
182                 /* Try to re-setup stuff and just continue */
183                 setup();
184                 return;
185         }
186         if (write(fd, message, strlen(message)) != (ssize_t)strlen(message))
187                 exit(EXIT_FAILURE);
188         (void)close(fd);
189 }