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