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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
#define _XOPEN_SOURCE 700
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <json-c/json.h>
#include "swaybar/config.h"
#include "swaybar/status_line.h"
#include "log.h"
#include "util.h"
#define I3JSON_MAXDEPTH 4
#define I3JSON_UNKNOWN 0
#define I3JSON_ARRAY 1
#define I3JSON_STRING 2
struct {
int bufsize;
char *buffer;
char *line_start;
char *parserpos;
bool escape;
int depth;
int bar[I3JSON_MAXDEPTH+1];
} i3json_state = { 0, NULL, NULL, NULL, false, 0, { I3JSON_UNKNOWN } };
static char line[1024];
static char line_rest[1024];
static char event_buff[1024];
static void free_status_block(void *item) {
if (!item) {
return;
}
struct status_block *sb = (struct status_block*)item;
if (sb->full_text) {
free(sb->full_text);
}
if (sb->short_text) {
free(sb->short_text);
}
if (sb->align) {
free(sb->align);
}
if (sb->name) {
free(sb->name);
}
if (sb->instance) {
free(sb->instance);
}
free(sb);
}
static void parse_json(struct bar *bar, const char *text) {
json_object *results = json_tokener_parse(text);
if (!results) {
sway_log(L_DEBUG, "Failed to parse json");
return;
}
if (json_object_array_length(results) < 1) {
return;
}
if (bar->status->block_line) {
list_foreach(bar->status->block_line, free_status_block);
list_free(bar->status->block_line);
}
bar->status->block_line = create_list();
int i;
for (i = 0; i < json_object_array_length(results); ++i) {
json_object *full_text, *short_text, *color, *min_width, *align, *urgent;
json_object *name, *instance, *separator, *separator_block_width;
json_object *background, *border, *border_top, *border_bottom;
json_object *border_left, *border_right, *markup;
json_object *json = json_object_array_get_idx(results, i);
if (!json) {
continue;
}
json_object_object_get_ex(json, "full_text", &full_text);
json_object_object_get_ex(json, "short_text", &short_text);
json_object_object_get_ex(json, "color", &color);
json_object_object_get_ex(json, "min_width", &min_width);
json_object_object_get_ex(json, "align", &align);
json_object_object_get_ex(json, "urgent", &urgent);
json_object_object_get_ex(json, "name", &name);
json_object_object_get_ex(json, "instance", &instance);
json_object_object_get_ex(json, "markup", &markup);
json_object_object_get_ex(json, "separator", &separator);
json_object_object_get_ex(json, "separator_block_width", &separator_block_width);
json_object_object_get_ex(json, "background", &background);
json_object_object_get_ex(json, "border", &border);
json_object_object_get_ex(json, "border_top", &border_top);
json_object_object_get_ex(json, "border_bottom", &border_bottom);
json_object_object_get_ex(json, "border_left", &border_left);
json_object_object_get_ex(json, "border_right", &border_right);
struct status_block *new = calloc(1, sizeof(struct status_block));
if (full_text) {
new->full_text = strdup(json_object_get_string(full_text));
}
if (short_text) {
new->short_text = strdup(json_object_get_string(short_text));
}
if (color) {
new->color = parse_color(json_object_get_string(color));
} else {
new->color = bar->config->colors.statusline;
}
if (min_width) {
json_type type = json_object_get_type(min_width);
if (type == json_type_int) {
new->min_width = json_object_get_int(min_width);
} else if (type == json_type_string) {
/* the width will be calculated when rendering */
new->min_width = 0;
}
}
if (align) {
new->align = strdup(json_object_get_string(align));
} else {
new->align = strdup("left");
}
if (urgent) {
new->urgent = json_object_get_int(urgent);
}
if (name) {
new->name = strdup(json_object_get_string(name));
}
if (instance) {
new->instance = strdup(json_object_get_string(instance));
}
if (markup) {
new->markup = false;
const char *markup_str = json_object_get_string(markup);
if (strcmp(markup_str, "pango") == 0) {
new->markup = true;
}
}
if (separator) {
new->separator = json_object_get_int(separator);
} else {
new->separator = true; // i3bar spec
}
if (separator_block_width) {
new->separator_block_width = json_object_get_int(separator_block_width);
} else {
new->separator_block_width = 9; // i3bar spec
}
// Airblader features
if (background) {
new->background = parse_color(json_object_get_string(background));
} else {
new->background = 0x0; // transparent
}
if (border) {
new->border = parse_color(json_object_get_string(border));
} else {
new->border = 0x0; // transparent
}
if (border_top) {
new->border_top = json_object_get_int(border_top);
} else {
new->border_top = 1;
}
if (border_bottom) {
new->border_bottom = json_object_get_int(border_bottom);
} else {
new->border_bottom = 1;
}
if (border_left) {
new->border_left = json_object_get_int(border_left);
} else {
new->border_left = 1;
}
if (border_right) {
new->border_right = json_object_get_int(border_right);
} else {
new->border_right = 1;
}
list_add(bar->status->block_line, new);
}
json_object_put(results);
}
// continue parsing from last parserpos
static int i3json_parse(struct bar *bar) {
char *c = i3json_state.parserpos;
int handled = 0;
while (*c) {
if (i3json_state.bar[i3json_state.depth] == I3JSON_STRING) {
if (!i3json_state.escape && *c == '"') {
--i3json_state.depth;
}
i3json_state.escape = !i3json_state.escape && *c == '\\';
} else {
switch (*c) {
case '[':
++i3json_state.depth;
if (i3json_state.depth > I3JSON_MAXDEPTH) {
sway_abort("JSON too deep");
}
i3json_state.bar[i3json_state.depth] = I3JSON_ARRAY;
if (i3json_state.depth == 2) {
i3json_state.line_start = c;
}
break;
case ']':
if (i3json_state.bar[i3json_state.depth] != I3JSON_ARRAY) {
sway_abort("JSON malformed");
}
--i3json_state.depth;
if (i3json_state.depth == 1) {
// c[1] is valid since c[0] != '\0'
char p = c[1];
c[1] = '\0';
parse_json(bar, i3json_state.line_start);
c[1] = p;
++handled;
i3json_state.line_start = c+1;
}
break;
case '"':
++i3json_state.depth;
if (i3json_state.depth > I3JSON_MAXDEPTH) {
sway_abort("JSON too deep");
}
i3json_state.bar[i3json_state.depth] = I3JSON_STRING;
break;
}
}
++c;
}
i3json_state.parserpos = c;
return handled;
}
// Read line from file descriptor, only show the line tail if it is too long.
// In non-blocking mode treat "no more data" as a linebreak.
// If data after a line break has been read, return it in rest.
// If rest is non-empty, then use that as the start of the next line.
static int read_line_tail(int fd, char *buf, int nbyte, char *rest) {
if (fd < 0 || !buf || !nbyte) {
return -1;
}
int l;
char *buffer = malloc(nbyte*2+1);
char *readpos = buffer;
char *lf;
// prepend old data to new line if necessary
if (rest) {
l = strlen(rest);
if (l > nbyte) {
strcpy(buffer, rest + l - nbyte);
readpos += nbyte;
} else if (l) {
strcpy(buffer, rest);
readpos += l;
}
}
// read until a linefeed is found or no more data is available
while ((l = read(fd, readpos, nbyte)) > 0) {
readpos[l] = '\0';
lf = strchr(readpos, '\n');
if (lf) {
// linefeed found, replace with \0
*lf = '\0';
// give data from the end of the line, try to fill the buffer
if (lf-buffer > nbyte) {
strcpy(buf, lf - nbyte + 1);
} else {
strcpy(buf, buffer);
}
// we may have read data from the next line, save it to rest
if (rest) {
rest[0] = '\0';
strcpy(rest, lf + 1);
}
free(buffer);
return strlen(buf);
} else {
// no linefeed found, slide data back.
int overflow = readpos - buffer + l - nbyte;
if (overflow > 0) {
memmove(buffer, buffer + overflow , nbyte + 1);
}
}
}
if (l < 0) {
free(buffer);
return l;
}
readpos[l]='\0';
if (rest) {
rest[0] = '\0';
}
if (nbyte < readpos - buffer + l - 1) {
memcpy(buf, readpos - nbyte + l + 1, nbyte);
} else {
strncpy(buf, buffer, nbyte);
}
buf[nbyte-1] = '\0';
free(buffer);
return strlen(buf);
}
// make sure that enough buffer space is available starting from parserpos
static void i3json_ensure_free(int min_free) {
int _step = 10240;
int r = min_free % _step;
if (r) {
min_free += _step - r;
}
if (!i3json_state.buffer) {
i3json_state.buffer = malloc(min_free);
i3json_state.bufsize = min_free;
i3json_state.parserpos = i3json_state.buffer;
} else {
int len = 0;
int pos = 0;
if (i3json_state.line_start) {
len = strlen(i3json_state.line_start);
pos = i3json_state.parserpos - i3json_state.line_start;
if (i3json_state.line_start != i3json_state.buffer) {
memmove(i3json_state.buffer, i3json_state.line_start, len+1);
}
} else {
len = strlen(i3json_state.buffer);
}
if (i3json_state.bufsize < len+min_free) {
i3json_state.bufsize += min_free;
if (i3json_state.bufsize > 1024000) {
sway_abort("Status line json too long or malformed.");
}
i3json_state.buffer = realloc(i3json_state.buffer, i3json_state.bufsize);
if (!i3json_state.buffer) {
sway_abort("Could not allocate json buffer");
}
}
if (i3json_state.line_start) {
i3json_state.line_start = i3json_state.buffer;
i3json_state.parserpos = i3json_state.buffer + pos;
} else {
i3json_state.parserpos = i3json_state.buffer;
}
}
if (!i3json_state.buffer) {
sway_abort("Could not allocate buffer.");
}
}
// append data and parse it.
static int i3json_handle_data(struct bar *bar, char *data) {
int len = strlen(data);
i3json_ensure_free(len);
strcpy(i3json_state.parserpos, data);
return i3json_parse(bar);
}
// read data from fd and parse it.
static int i3json_handle_fd(struct bar *bar) {
i3json_ensure_free(10240);
// get fresh data at the end of the buffer
int readlen = read(bar->status_read_fd, i3json_state.parserpos, 10239);
if (readlen < 0) {
return readlen;
}
i3json_state.parserpos[readlen] = '\0';
return i3json_parse(bar);
}
bool status_line_mouse_event(struct bar *bar, int x, int y, uint32_t button) {
sway_log(L_DEBUG, "status_line_mouse_event.");
if (!bar->status->click_events) {
sway_log(L_DEBUG, "click_events are not enabled.");
return false;
}
if (bar->status->protocol == I3BAR) {
sway_log(L_DEBUG, "Sending click event.");
// find clicked block
struct status_block *clicked_block = NULL;
struct status_block *current_block = NULL;
int num_blocks = bar->status->block_line->length;
if (num_blocks == 0) {
return false;
} else {
current_block = bar->status->block_line->items[0];
if (x < current_block->x) {
return false;
}
}
for (int i = 0; i < num_blocks; i++) {
current_block = bar->status->block_line->items[i];
if (x < (current_block->x + current_block->width)) {
clicked_block = current_block;
break;
}
}
if (!clicked_block || !clicked_block->name) {
return false;
}
// event example {"name":"capture","instance":"label","button":1,"x":3431,"y":18}
struct json_object *event_json = json_object_new_object();
json_object_object_add(event_json, "name", json_object_new_string(clicked_block->name));
if (clicked_block->instance) {
json_object_object_add(event_json, "instance", json_object_new_string(clicked_block->instance));
}
json_object_object_add(event_json, "button", json_object_new_int(button));
json_object_object_add(event_json, "x", json_object_new_int(x));
json_object_object_add(event_json, "y", json_object_new_int(y));
int len = snprintf(event_buff, sizeof(event_buff), "%s\n", json_object_to_json_string(event_json));
json_object_put(event_json);
if (len <= (int)sizeof(event_buff)) { // if not truncated
write(bar->status_write_fd, event_buff, len);
return true;
}
}
return false;
}
bool handle_status_line(struct bar *bar) {
bool dirty = false;
switch (bar->status->protocol) {
case I3BAR:
sway_log(L_DEBUG, "Got i3bar protocol.");
if (i3json_handle_fd(bar) > 0) {
dirty = true;
}
break;
case TEXT:
sway_log(L_DEBUG, "Got text protocol.");
read_line_tail(bar->status_read_fd, line, sizeof(line), line_rest);
dirty = true;
bar->status->text_line = line;
break;
case UNDEF:
sway_log(L_DEBUG, "Detecting protocol...");
if (read_line_tail(bar->status_read_fd, line, sizeof(line), line_rest) < 0) {
break;
}
dirty = true;
bar->status->text_line = line;
bar->status->protocol = TEXT;
if (line[0] == '{') {
// detect i3bar json protocol
json_object *proto = json_tokener_parse(line);
if (proto) {
json_object *version;
if (json_object_object_get_ex(proto, "version", &version)
&& json_object_get_int(version) == 1
) {
sway_log(L_DEBUG, "Switched to i3bar protocol.");
bar->status->protocol = I3BAR;
}
json_object *click_events;
if (json_object_object_get_ex(proto, "click_events", &click_events)
&& json_object_get_boolean(click_events)) {
sway_log(L_DEBUG, "Enabling click events.");
bar->status->click_events = true;
const char *events_array = "[\n";
write(bar->status_write_fd, events_array, strlen(events_array));
}
i3json_handle_data(bar, line_rest);
json_object_put(proto);
}
}
break;
}
return dirty;
}
struct status_line *init_status_line() {
struct status_line *line = malloc(sizeof(struct status_line));
line->block_line = create_list();
line->text_line = NULL;
line->protocol = UNDEF;
line->click_events = false;
return line;
}
void free_status_line(struct status_line *line) {
if (line->block_line) {
list_foreach(line->block_line, free_status_block);
list_free(line->block_line);
}
}
|