summaryrefslogtreecommitdiff
path: root/src/output.c
blob: 901a940be04bc40648e81c33550ee027c33562bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// vim:ts=8:expandtab
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>

#include "i3status.h"

/*
 * Writes an errormessage to statusbar
 *
 */
void write_error_to_statusbar(const char *message) {
        cleanup_rbar_dir();
        create_file("error");
        write_to_statusbar("error", message, true);
}

/*
 * Returns the correct color format for dzen (^fg(color)) or wmii (color <normcolors>)
 *
 */
char *color(const char *colorstr) {
        static char colorbuf[32];
#ifdef DZEN
        (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", colorstr);
#else
        (void)snprintf(colorbuf, sizeof(colorbuf), "%s %s ", colorstr, wmii_normcolors);
#endif
        return colorbuf;
}

/*
 * Cleans wmii's /rbar directory by deleting all regular files
 *
 */
void cleanup_rbar_dir() {
#ifdef DZEN
        return;
#endif
        struct dirent *ent;
        DIR *dir;
        char pathbuf[strlen(wmii_path)+256+1];

        if ((dir = opendir(wmii_path)) == NULL)
                exit(EXIT_FAILURE);

        while ((ent = readdir(dir)) != NULL) {
                if (ent->d_type == DT_REG) {
                        (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, ent->d_name);
                        if (unlink(pathbuf) == -1)
                                exit(EXIT_FAILURE);
                }
        }

        (void)closedir(dir);
}

/*
 * Creates the specified file in wmii's /rbar directory with
 * correct modes and initializes colors if colormode is enabled
 *
 */
void create_file(const char *name) {
#ifdef DZEN
        return;
#endif
        char pathbuf[strlen(wmii_path)+256+1];
        int fd;
        int flags = O_CREAT | O_WRONLY;
        struct stat statbuf;

        (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);

        /* Overwrite file's contents if it exists */
        if (stat(pathbuf, &statbuf) >= 0)
                flags |= O_TRUNC;

        if ((fd = open(pathbuf, flags, S_IRUSR | S_IWUSR)) < 0)
                exit(EXIT_FAILURE);
        if (use_colors) {
                char *tmp = color("#888888");
                if (write(fd, tmp, strlen(tmp)) != (ssize_t)strlen(tmp))
                        exit(EXIT_FAILURE);
        }
        (void)close(fd);
}

/*
 * Waits until wmii_path/rbar exists (= the filesystem gets mounted),
 * cleans up all files and creates the needed files
 *
 */
void setup(void) {
        unsigned int i;
        char pathbuf[512];

#ifndef DZEN
        struct stat statbuf;
        /* Wait until wmii_path/rbar exists */
        for (; stat(wmii_path, &statbuf) < 0; sleep(interval));
#endif
#define cf(orderidx, name) create_file(order_to_str(order[orderidx], name));

        cleanup_rbar_dir();
        if (wlan_interface)
                cf(ORDER_WLAN, "wlan");
        if (eth_interface)
                cf(ORDER_ETH, "eth");
        if (get_cpu_temperature)
                cf(ORDER_CPU_TEMPERATURE, "cpu_temperature");
        cf(ORDER_LOAD, "load");
        if (time_format)
                cf(ORDER_TIME, "time");
        for (i = 0; i < num_run_watches; i += 2) {
                snprintf(pathbuf, sizeof(pathbuf), "%d%s", order[ORDER_RUN], run_watches[i]);
                create_file(pathbuf);
        }
}

/*
 * Writes the given message in the corresponding file in wmii's /rbar directory
 *
 */
void write_to_statusbar(const char *name, const char *message, bool final_entry) {
#ifdef DZEN
        if (final_entry) {
                if (printf("%s^p(6)\n", message) < 0) {
                        perror("printf");
                        exit(1);
                }

                fflush(stdout);
                return;
        }
        if (printf("%s" BAR, message) < 0) {
                perror("printf");
                exit(1);
        }
        return;
#endif

        char pathbuf[strlen(wmii_path)+256+1];
        int fd;

        (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
        if ((fd = open(pathbuf, O_RDWR)) == -1) {
                /* Try to re-setup stuff and just continue */
                setup();
                return;
        }
        if (write(fd, message, strlen(message)) != (ssize_t)strlen(message))
                exit(EXIT_FAILURE);
        (void)close(fd);
}