-
Notifications
You must be signed in to change notification settings - Fork 182
/
i3bar.c
347 lines (279 loc) · 6.7 KB
/
i3bar.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* i3bar.c - i3bar (plus i3-gaps and sway) protocol support
* Copyright (C) 2014-2019 Vivien Didelot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bar.h"
#include "block.h"
#include "json.h"
#include "line.h"
#include "log.h"
#include "map.h"
#include "term.h"
/* See https://i3wm.org/docs/i3bar-protocol.html for details */
static struct {
const char * const key;
bool string;
} i3bar_keys[] = {
{ "", false }, /* unknown key */
/* Standard keys */
{ "full_text", true },
{ "short_text", true },
{ "color", true },
{ "background", true },
{ "border", true },
{ "min_width", false }, /* can also be a number */
{ "align", true },
{ "name", true },
{ "instance", true },
{ "urgent", false },
{ "separator", false },
{ "separator_block_width", false },
{ "markup", true },
/* i3-gaps features */
{ "border_top", false },
{ "border_bottom", false },
{ "border_left", false },
{ "border_right", false },
};
static unsigned int i3bar_indexof(const char *key)
{
unsigned int i;
for (i = 0; i < sizeof(i3bar_keys) / sizeof(i3bar_keys[0]); i++)
if (strcmp(i3bar_keys[i].key, key) == 0)
return i;
return 0;
}
static int i3bar_line_cb(char *line, size_t num, void *data)
{
unsigned int index = num + 1;
struct map *map = data;
const char *key;
if (index >= sizeof(i3bar_keys) / sizeof(i3bar_keys[0])) {
debug("ignoring excess line %d: %s", num, line);
return 0;
}
key = i3bar_keys[index].key;
return map_set(map, key, line);
}
int i3bar_read(int fd, size_t count, struct map *map)
{
return line_read(fd, count, i3bar_line_cb, map);
}
static void i3bar_print_term(const struct bar *bar)
{
struct block *block = bar->blocks;
const char *full_text;
term_restore_cursor();
while (block) {
full_text = map_get(block->env, "full_text");
if (full_text)
fprintf(stdout, "%s ", full_text);
block = block->next;
}
fflush(stdout);
}
static int i3bar_print_pair(const char *key, const char *value, void *data)
{
unsigned int index = i3bar_indexof(key);
bool string = i3bar_keys[index].string;
unsigned int *pcount = data;
char buf[BUFSIZ];
bool escape;
int err;
/* Skip unknown keys */
if (!index)
return 0;
if (!value)
value = "null";
if (string) {
if (json_is_string(value))
escape = false; /* Expected string already quoted */
else
escape = true; /* Enforce the string type */
} else {
if (json_is_valid(value))
escape = false; /* Already valid JSON */
else
escape = true; /* Unquoted string */
}
if (escape) {
err = json_escape(value, buf, sizeof(buf));
if (err)
return err;
value = buf;
}
if ((*pcount)++)
fprintf(stdout, ",");
fprintf(stdout, "\"%s\":%s", key, value);
return 0;
}
static int i3bar_print_block(struct block *block, void *data)
{
const char *full_text = map_get(block->env, "full_text");
unsigned int *mcount = data;
unsigned int pcount = 0;
int err;
/* "full_text" is the only mandatory key */
if (!full_text) {
block_debug(block, "no text to display, skipping");
return 0;
}
if ((*mcount)++)
fprintf(stdout, ",");
fprintf(stdout, "{");
err = map_for_each(block->env, i3bar_print_pair, &pcount);
fprintf(stdout, "}");
return err;
}
int i3bar_print(const struct bar *bar)
{
struct block *block = bar->blocks;
unsigned int mcount = 0;
int err;
if (bar->term) {
i3bar_print_term(bar);
return 0;
}
fprintf(stdout, ",[");
while (block) {
err = i3bar_print_block(block, &mcount);
if (err)
break;
block = block->next;
}
fprintf(stdout, "]\n");
fflush(stdout);
return err;
}
int i3bar_printf(struct block *block, int lvl, const char *msg)
{
const struct bar *bar = block->bar;
struct map *map = block->env;
int err;
if (bar->term || lvl > LOG_ERROR)
return 0;
block->tainted = true;
err = map_set(map, "full_text", msg);
if (err)
return err;
if (lvl <= LOG_ERROR) {
err = map_set(map, "urgent", "true");
if (err)
return err;
}
return i3bar_print(bar);
}
int i3bar_start(struct bar *bar)
{
if (bar->term) {
term_save_cursor();
term_restore_cursor();
} else {
fprintf(stdout, "{\"version\":1,\"click_events\":true}\n[[]\n");
fflush(stdout);
}
return 0;
}
void i3bar_stop(struct bar *bar)
{
if (bar->term) {
term_reset_cursor();
} else {
fprintf(stdout, "]\n");
fflush(stdout);
}
}
static struct block *i3bar_find(struct bar *bar, const struct map *map)
{
const char *block_name, *block_instance;
const char *map_name, *map_instance;
struct block *block = bar->blocks;
/* "name" and "instance" are the only identifiers provided by i3bar */
map_name = map_get(map, "name") ? : "";
map_instance = map_get(map, "instance") ? : "";
while (block) {
block_name = block_get(block, "name") ? : "";
block_instance = block_get(block, "instance") ? : "";
if (strcmp(block_name, map_name) == 0 &&
strcmp(block_instance, map_instance) == 0)
return block;
block = block->next;
}
return NULL;
}
int i3bar_click(struct bar *bar)
{
struct block *block;
struct map *click;
int err;
click = map_create();
if (!click)
return -ENOMEM;
for (;;) {
/* Each click is one JSON object per line */
err = json_read(STDIN_FILENO, 1, click);
if (err) {
if (err == -EAGAIN)
err = 0;
break;
}
/* Look for the corresponding block */
block = i3bar_find(bar, click);
if (block) {
if (block->tainted) {
err = block_reset(block);
if (err)
break;
block->tainted = false;
err = i3bar_print(bar);
if (err)
break;
} else {
err = map_copy(block->env, click);
if (err)
break;
err = block_click(block);
if (err)
break;
}
}
map_clear(click);
}
map_destroy(click);
return err;
}
int i3bar_setup(struct block *block)
{
const char *instance = map_get(block->config, "instance");
const char *name = map_get(block->config, "name");
char buf[BUFSIZ];
int err;
/* A block needs a name to be clickable */
if (!name) {
name = "foo";
err = map_set(block->config, "name", name);
if (err)
return err;
}
if (instance)
snprintf(buf, sizeof(buf), "%s:%s", name, instance);
else
snprintf(buf, sizeof(buf), "%s", name);
block->name = strdup(buf);
if (!block->name)
return -ENOMEM;
return 0;
}