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