]> git.sur5r.net Git - i3/i3status/blob - src/output.c
Use own files for each function, add get_ipv6_addr.c
[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
107         cleanup_rbar_dir();
108         if (wlan_interface)
109                 create_file(concat(order[ORDER_WLAN],"wlan"));
110         if (eth_interface)
111                 create_file(concat(order[ORDER_ETH],"eth"));
112         if (get_cpu_temperature)
113                 create_file(concat(order[ORDER_CPU_TEMPERATURE], "cpu_temperature"));
114         create_file(concat(order[ORDER_LOAD],"load"));
115         if (time_format)
116                 create_file(concat(order[ORDER_TIME],"time"));
117         for (i = 0; i < num_run_watches; i += 2) {
118                 snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
119                 create_file(pathbuf);
120         }
121 }
122
123 /*
124  * Writes the given message in the corresponding file in wmii's /rbar directory
125  *
126  */
127 void write_to_statusbar(const char *name, const char *message, bool final_entry) {
128 #ifdef DZEN
129         if (final_entry) {
130                 if (printf("%s^p(6)\n", message) < 0) {
131                         perror("printf");
132                         exit(1);
133                 }
134
135                 fflush(stdout);
136                 return;
137         }
138         if (printf("%s" BAR, message) < 0) {
139                 perror("printf");
140                 exit(1);
141         }
142         return;
143 #endif
144
145         char pathbuf[strlen(wmii_path)+256+1];
146         int fd;
147
148         (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
149         if ((fd = open(pathbuf, O_RDWR)) == -1) {
150                 /* Try to re-setup stuff and just continue */
151                 setup();
152                 return;
153         }
154         if (write(fd, message, strlen(message)) != (ssize_t)strlen(message))
155                 exit(EXIT_FAILURE);
156         (void)close(fd);
157 }