aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/i3bar.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-03-31 13:07:22 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2018-04-02 11:09:23 -0400
commitee85c918317ec6a685a999db46f692c7d13cdf2a (patch)
tree34afb98bc7fccb8ea6d215c710579e62a459cb62 /swaybar/i3bar.c
parentMerge pull request #1684 from swaywm/follow-warp (diff)
downloadsway-ee85c918317ec6a685a999db46f692c7d13cdf2a.tar.gz
sway-ee85c918317ec6a685a999db46f692c7d13cdf2a.tar.zst
sway-ee85c918317ec6a685a999db46f692c7d13cdf2a.zip
Demarcate i3bar JSON into individual updates
Diffstat (limited to 'swaybar/i3bar.c')
-rw-r--r--swaybar/i3bar.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c
new file mode 100644
index 00000000..5be348b2
--- /dev/null
+++ b/swaybar/i3bar.c
@@ -0,0 +1,88 @@
1#include <stdlib.h>
2#include <string.h>
3#include <unistd.h>
4#include <wlr/util/log.h>
5#include "swaybar/config.h"
6#include "swaybar/status_line.h"
7
8static void i3bar_parse_json(struct status_line *status, const char *text) {
9 wlr_log(L_DEBUG, "got json: %s", text);
10}
11
12int i3bar_readable(struct status_line *status) {
13 struct i3bar_protocol_state *state = &status->i3bar_state;
14
15 char *cur = &state->buffer[state->buffer_index];
16 ssize_t n = read(status->read_fd, cur,
17 state->buffer_size - state->buffer_index);
18 if (n == 0) {
19 return 0;
20 }
21
22 if (n == (ssize_t)(state->buffer_size - state->buffer_index)) {
23 state->buffer_size = state->buffer_size * 2;
24 char *new_buffer = realloc(state->buffer, state->buffer_size);
25 if (!new_buffer) {
26 free(state->buffer);
27 status_error(status, "[failed to allocate buffer]");
28 return -1;
29 }
30 state->buffer = new_buffer;
31 }
32
33 int handled = 0;
34 while (*cur) {
35 if (state->nodes[state->depth] == JSON_NODE_STRING) {
36 if (!state->escape && *cur == '"') {
37 --state->depth;
38 }
39 state->escape = !state->escape && *cur == '\\';
40 } else {
41 switch (*cur) {
42 case '[':
43 ++state->depth;
44 if (state->depth >
45 sizeof(state->nodes) / sizeof(state->nodes[0])) {
46 status_error(status, "[i3bar json too deep]");
47 return -1;
48 }
49 state->nodes[state->depth] = JSON_NODE_ARRAY;
50 if (state->depth == 1) {
51 state->current_node = cur;
52 }
53 break;
54 case ']':
55 if (state->nodes[state->depth] != JSON_NODE_ARRAY) {
56 status_error(status, "[failed to parse i3bar json]");
57 return -1;
58 }
59 --state->depth;
60 if (state->depth == 0) {
61 // cur[1] is valid since cur[0] != '\0'
62 char p = cur[1];
63 cur[1] = '\0';
64 i3bar_parse_json(status, state->current_node);
65 cur[1] = p;
66 memmove(state->buffer, cur,
67 state->buffer_size - (cur - state->buffer));
68 ++handled;
69 cur = state->buffer;
70 state->current_node = cur + 1;
71 }
72 break;
73 case '"':
74 ++state->depth;
75 if (state->depth >
76 sizeof(state->nodes) / sizeof(state->nodes[0])) {
77 status_error(status, "[i3bar json too deep]");
78 return -1;
79 }
80 state->nodes[state->depth] = JSON_NODE_STRING;
81 break;
82 }
83 }
84 ++cur;
85 }
86 state->buffer_index = cur - state->buffer;
87 return handled;
88}