diff options
author | Ian Fan <ianfan0@gmail.com> | 2018-09-17 14:10:57 +0100 |
---|---|---|
committer | Ian Fan <ianfan0@gmail.com> | 2018-09-18 11:36:33 +0100 |
commit | 7882ac66ef4308922045fd100e6a9e12942a240b (patch) | |
tree | 67ce4f14f56372c86eb9992eef354d1d76f3081d | |
parent | swaybar: rewrite protocol determination (diff) | |
download | sway-7882ac66ef4308922045fd100e6a9e12942a240b.tar.gz sway-7882ac66ef4308922045fd100e6a9e12942a240b.tar.zst sway-7882ac66ef4308922045fd100e6a9e12942a240b.zip |
swaybar: rewrite i3bar protocol handling
This now correctly handles an incoming json infinite array by shifting
most of the heavy listing to the json-c parser, as well as sending
multiple statuses at once. It also removes the struct
i3bar_protocol_state and moves its members into the status_line struct,
allowing the same buffer to be used for both protocols.
-rw-r--r-- | include/swaybar/status_line.h | 24 | ||||
-rw-r--r-- | swaybar/i3bar.c | 189 | ||||
-rw-r--r-- | swaybar/render.c | 2 | ||||
-rw-r--r-- | swaybar/status_line.c | 31 |
4 files changed, 128 insertions, 118 deletions
diff --git a/include/swaybar/status_line.h b/include/swaybar/status_line.h index 857948a5..d3eabdf6 100644 --- a/include/swaybar/status_line.h +++ b/include/swaybar/status_line.h | |||
@@ -1,5 +1,6 @@ | |||
1 | #ifndef _SWAYBAR_STATUS_LINE_H | 1 | #ifndef _SWAYBAR_STATUS_LINE_H |
2 | #define _SWAYBAR_STATUS_LINE_H | 2 | #define _SWAYBAR_STATUS_LINE_H |
3 | #include <json-c/json.h> | ||
3 | #include <stdint.h> | 4 | #include <stdint.h> |
4 | #include <stdio.h> | 5 | #include <stdio.h> |
5 | #include <stdbool.h> | 6 | #include <stdbool.h> |
@@ -12,23 +13,6 @@ enum status_protocol { | |||
12 | PROTOCOL_I3BAR, | 13 | PROTOCOL_I3BAR, |
13 | }; | 14 | }; |
14 | 15 | ||
15 | enum json_node_type { | ||
16 | JSON_NODE_UNKNOWN, | ||
17 | JSON_NODE_ARRAY, | ||
18 | JSON_NODE_STRING, | ||
19 | }; | ||
20 | |||
21 | struct i3bar_protocol_state { | ||
22 | bool click_events; | ||
23 | char *buffer; | ||
24 | size_t buffer_size; | ||
25 | size_t buffer_index; | ||
26 | const char *current_node; | ||
27 | bool escape; | ||
28 | size_t depth; | ||
29 | enum json_node_type nodes[16]; | ||
30 | }; | ||
31 | |||
32 | struct i3bar_block { | 16 | struct i3bar_block { |
33 | struct wl_list link; | 17 | struct wl_list link; |
34 | int ref_count; | 18 | int ref_count; |
@@ -58,9 +42,13 @@ struct status_line { | |||
58 | const char *text; | 42 | const char *text; |
59 | struct wl_list blocks; // i3bar_block::link | 43 | struct wl_list blocks; // i3bar_block::link |
60 | 44 | ||
45 | bool click_events; | ||
61 | char *buffer; | 46 | char *buffer; |
62 | size_t buffer_size; | 47 | size_t buffer_size; |
63 | struct i3bar_protocol_state i3bar_state; | 48 | size_t buffer_index; |
49 | bool started; | ||
50 | bool expecting_comma; | ||
51 | json_tokener *tokener; | ||
64 | }; | 52 | }; |
65 | 53 | ||
66 | struct status_line *status_line_init(char *cmd); | 54 | struct status_line *status_line_init(char *cmd); |
diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c index f4ca5504..78cb5bd4 100644 --- a/swaybar/i3bar.c +++ b/swaybar/i3bar.c | |||
@@ -1,6 +1,7 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | 1 | #define _POSIX_C_SOURCE 200809L |
2 | #include <json-c/json.h> | 2 | #include <json-c/json.h> |
3 | #include <linux/input-event-codes.h> | 3 | #include <linux/input-event-codes.h> |
4 | #include <ctype.h> | ||
4 | #include <stdlib.h> | 5 | #include <stdlib.h> |
5 | #include <string.h> | 6 | #include <string.h> |
6 | #include <unistd.h> | 7 | #include <unistd.h> |
@@ -24,24 +25,19 @@ void i3bar_block_unref(struct i3bar_block *block) { | |||
24 | } | 25 | } |
25 | } | 26 | } |
26 | 27 | ||
27 | static bool i3bar_parse_json(struct status_line *status, const char *text) { | 28 | static void i3bar_parse_json(struct status_line *status, |
29 | struct json_object *json_array) { | ||
28 | struct i3bar_block *block, *tmp; | 30 | struct i3bar_block *block, *tmp; |
29 | wl_list_for_each_safe(block, tmp, &status->blocks, link) { | 31 | wl_list_for_each_safe(block, tmp, &status->blocks, link) { |
30 | wl_list_remove(&block->link); | 32 | wl_list_remove(&block->link); |
31 | i3bar_block_unref(block); | 33 | i3bar_block_unref(block); |
32 | } | 34 | } |
33 | json_object *results = json_tokener_parse(text); | 35 | for (size_t i = 0; i < json_object_array_length(json_array); ++i) { |
34 | if (!results) { | ||
35 | status_error(status, "[failed to parse i3bar json]"); | ||
36 | return false; | ||
37 | } | ||
38 | wlr_log(WLR_DEBUG, "Got i3bar json: '%s'", text); | ||
39 | for (size_t i = 0; i < json_object_array_length(results); ++i) { | ||
40 | json_object *full_text, *short_text, *color, *min_width, *align, *urgent; | 36 | json_object *full_text, *short_text, *color, *min_width, *align, *urgent; |
41 | json_object *name, *instance, *separator, *separator_block_width; | 37 | json_object *name, *instance, *separator, *separator_block_width; |
42 | json_object *background, *border, *border_top, *border_bottom; | 38 | json_object *background, *border, *border_top, *border_bottom; |
43 | json_object *border_left, *border_right, *markup; | 39 | json_object *border_left, *border_right, *markup; |
44 | json_object *json = json_object_array_get_idx(results, i); | 40 | json_object *json = json_object_array_get_idx(json_array, i); |
45 | if (!json) { | 41 | if (!json) { |
46 | continue; | 42 | continue; |
47 | } | 43 | } |
@@ -110,96 +106,133 @@ static bool i3bar_parse_json(struct status_line *status, const char *text) { | |||
110 | json_object_get_int(border_right) : 1; | 106 | json_object_get_int(border_right) : 1; |
111 | wl_list_insert(&status->blocks, &block->link); | 107 | wl_list_insert(&status->blocks, &block->link); |
112 | } | 108 | } |
113 | json_object_put(results); | ||
114 | return true; | ||
115 | } | 109 | } |
116 | 110 | ||
117 | bool i3bar_handle_readable(struct status_line *status) { | 111 | bool i3bar_handle_readable(struct status_line *status) { |
118 | struct i3bar_protocol_state *state = &status->i3bar_state; | 112 | while (!status->started) { // look for opening bracket |
119 | 113 | for (size_t c = 0; c < status->buffer_index; ++c) { | |
120 | char *cur = &state->buffer[state->buffer_index]; | 114 | if (status->buffer[c] == '[') { |
121 | ssize_t n = read(status->read_fd, cur, | 115 | status->started = true; |
122 | state->buffer_size - state->buffer_index); | 116 | status->buffer_index -= ++c; |
123 | if (n == -1) { | 117 | memmove(status->buffer, &status->buffer[c], status->buffer_index); |
124 | status_error(status, "[failed to read from status command]"); | 118 | break; |
125 | return false; | 119 | } else if (!isspace(status->buffer[c])) { |
126 | } | 120 | status_error(status, "[invalid json]"); |
121 | return true; | ||
122 | } | ||
123 | } | ||
124 | if (status->started) { | ||
125 | break; | ||
126 | } | ||
127 | 127 | ||
128 | if (n == (ssize_t)(state->buffer_size - state->buffer_index)) { | 128 | errno = 0; |
129 | state->buffer_size = state->buffer_size * 2; | 129 | ssize_t read_bytes = read(status->read_fd, status->buffer, status->buffer_size); |
130 | char *new_buffer = realloc(state->buffer, state->buffer_size); | 130 | if (read_bytes > -1) { |
131 | if (!new_buffer) { | 131 | status->buffer_index = read_bytes; |
132 | free(state->buffer); | 132 | } else if (errno == EAGAIN) { |
133 | status_error(status, "[failed to allocate buffer]"); | 133 | return false; |
134 | } else { | ||
135 | status_error(status, "[error reading from status command]"); | ||
134 | return true; | 136 | return true; |
135 | } | 137 | } |
136 | state->current_node += new_buffer - state->buffer; | ||
137 | cur += new_buffer - state->buffer; | ||
138 | state->buffer = new_buffer; | ||
139 | } | 138 | } |
140 | 139 | ||
141 | cur[n] = '\0'; | 140 | struct json_object *last_object = NULL; |
142 | bool redraw = false; | 141 | struct json_object *test_object; |
143 | while (*cur) { | 142 | size_t buffer_pos = 0; |
144 | if (state->nodes[state->depth] == JSON_NODE_STRING) { | 143 | while (true) { |
145 | if (!state->escape && *cur == '"') { | 144 | // since the incoming stream is an infinite array |
146 | --state->depth; | 145 | // parsing is split into two parts |
146 | // first, attempt to parse the current object, reading more if the | ||
147 | // parser indicates that the current object is incomplete, and failing | ||
148 | // if the parser fails | ||
149 | // second, look for separating comma, ignoring whitespace, failing if | ||
150 | // any other characters are encountered | ||
151 | if (status->expecting_comma) { | ||
152 | for (; buffer_pos < status->buffer_index; ++buffer_pos) { | ||
153 | if (status->buffer[buffer_pos] == ',') { | ||
154 | status->expecting_comma = false; | ||
155 | ++buffer_pos; | ||
156 | break; | ||
157 | } else if (!isspace(status->buffer[buffer_pos])) { | ||
158 | status_error(status, "[invalid i3bar json]"); | ||
159 | return true; | ||
160 | } | ||
161 | } | ||
162 | if (buffer_pos < status->buffer_index) { | ||
163 | continue; // look for new object without reading more input | ||
147 | } | 164 | } |
148 | state->escape = !state->escape && *cur == '\\'; | 165 | buffer_pos = status->buffer_index = 0; |
149 | } else { | 166 | } else { |
150 | switch (*cur) { | 167 | test_object = json_tokener_parse_ex(status->tokener, |
151 | case '[': | 168 | &status->buffer[buffer_pos], status->buffer_index - buffer_pos); |
152 | ++state->depth; | 169 | if (json_tokener_get_error(status->tokener) == json_tokener_success) { |
153 | if (state->depth > | 170 | if (json_object_get_type(test_object) == json_type_array) { |
154 | sizeof(state->nodes) / sizeof(state->nodes[0])) { | 171 | if (last_object) { |
155 | status_error(status, "[i3bar json too deep]"); | 172 | json_object_put(last_object); |
156 | return false; | 173 | } |
157 | } | 174 | last_object = test_object; |
158 | state->nodes[state->depth] = JSON_NODE_ARRAY; | 175 | } else { |
159 | if (state->depth == 1) { | 176 | json_object_put(test_object); |
160 | state->current_node = cur; | ||
161 | } | 177 | } |
162 | break; | 178 | |
163 | case ']': | 179 | buffer_pos += status->tokener->char_offset; |
164 | if (state->nodes[state->depth] != JSON_NODE_ARRAY) { | 180 | status->expecting_comma = true; |
165 | status_error(status, "[failed to parse i3bar json]"); | 181 | |
166 | return false; | 182 | if (buffer_pos < status->buffer_index) { |
183 | continue; // look for comma without reading more input | ||
167 | } | 184 | } |
168 | --state->depth; | 185 | buffer_pos = status->buffer_index = 0; |
169 | if (state->depth == 0) { | 186 | } else if (json_tokener_get_error(status->tokener) == json_tokener_continue) { |
170 | // cur[1] is valid since cur[0] != '\0' | 187 | if (status->buffer_index < status->buffer_size) { |
171 | char p = cur[1]; | 188 | // move the object to the start of the buffer |
172 | cur[1] = '\0'; | 189 | status->buffer_index -= buffer_pos; |
173 | redraw = i3bar_parse_json( | 190 | memmove(status->buffer, &status->buffer[buffer_pos], |
174 | status, state->current_node) || redraw; | 191 | status->buffer_index); |
175 | cur[1] = p; | 192 | } else { |
176 | memmove(state->buffer, cur, | 193 | // expand buffer |
177 | state->buffer_size - (cur - state->buffer)); | 194 | status->buffer_size *= 2; |
178 | cur = state->buffer; | 195 | char *new_buffer = realloc(status->buffer, status->buffer_size); |
179 | state->current_node = cur + 1; | 196 | if (new_buffer) { |
197 | status->buffer = new_buffer; | ||
198 | } else { | ||
199 | free(status->buffer); | ||
200 | status_error(status, "[failed to allocate buffer]"); | ||
201 | return true; | ||
202 | } | ||
180 | } | 203 | } |
181 | break; | 204 | } else { |
182 | case '"': | 205 | status_error(status, "[failed to parse i3bar json]"); |
183 | ++state->depth; | 206 | return true; |
184 | if (state->depth > | ||
185 | sizeof(state->nodes) / sizeof(state->nodes[0])) { | ||
186 | status_error(status, "[i3bar json too deep]"); | ||
187 | return false; | ||
188 | } | ||
189 | state->nodes[state->depth] = JSON_NODE_STRING; | ||
190 | break; | ||
191 | } | 207 | } |
192 | } | 208 | } |
193 | ++cur; | 209 | |
210 | errno = 0; | ||
211 | ssize_t read_bytes = read(status->read_fd, &status->buffer[status->buffer_index], | ||
212 | status->buffer_size - status->buffer_index); | ||
213 | if (read_bytes > -1) { | ||
214 | status->buffer_index += read_bytes; | ||
215 | } else if (errno == EAGAIN) { | ||
216 | break; | ||
217 | } else { | ||
218 | status_error(status, "[error reading from status command]"); | ||
219 | return true; | ||
220 | } | ||
221 | } | ||
222 | |||
223 | if (last_object) { | ||
224 | i3bar_parse_json(status, last_object); | ||
225 | json_object_put(last_object); | ||
226 | return true; | ||
227 | } else { | ||
228 | return false; | ||
194 | } | 229 | } |
195 | state->buffer_index = cur - state->buffer; | ||
196 | return redraw; | ||
197 | } | 230 | } |
198 | 231 | ||
199 | enum hotspot_event_handling i3bar_block_send_click(struct status_line *status, | 232 | enum hotspot_event_handling i3bar_block_send_click(struct status_line *status, |
200 | struct i3bar_block *block, int x, int y, enum x11_button button) { | 233 | struct i3bar_block *block, int x, int y, enum x11_button button) { |
201 | wlr_log(WLR_DEBUG, "block %s clicked", block->name ? block->name : "(nil)"); | 234 | wlr_log(WLR_DEBUG, "block %s clicked", block->name ? block->name : "(nil)"); |
202 | if (!block->name || !status->i3bar_state.click_events) { | 235 | if (!block->name || !status->click_events) { |
203 | return HOTSPOT_PROCESS; | 236 | return HOTSPOT_PROCESS; |
204 | } | 237 | } |
205 | 238 | ||
diff --git a/swaybar/render.c b/swaybar/render.c index b2c1c710..97690338 100644 --- a/swaybar/render.c +++ b/swaybar/render.c | |||
@@ -177,7 +177,7 @@ static uint32_t render_status_block(cairo_t *cairo, | |||
177 | *x -= margin; | 177 | *x -= margin; |
178 | } | 178 | } |
179 | 179 | ||
180 | if (output->bar->status->i3bar_state.click_events) { | 180 | if (output->bar->status->click_events) { |
181 | struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); | 181 | struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); |
182 | hotspot->x = *x; | 182 | hotspot->x = *x; |
183 | hotspot->y = 0; | 183 | hotspot->y = 0; |
diff --git a/swaybar/status_line.c b/swaybar/status_line.c index fcc0cb93..01ed70d9 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c | |||
@@ -32,11 +32,6 @@ void status_error(struct status_line *status, const char *text) { | |||
32 | bool status_handle_readable(struct status_line *status) { | 32 | bool status_handle_readable(struct status_line *status) { |
33 | ssize_t read_bytes = 1; | 33 | ssize_t read_bytes = 1; |
34 | switch (status->protocol) { | 34 | switch (status->protocol) { |
35 | case PROTOCOL_I3BAR: | ||
36 | if (i3bar_handle_readable(status) > 0) { | ||
37 | return true; | ||
38 | } | ||
39 | break; | ||
40 | case PROTOCOL_UNDEF: | 35 | case PROTOCOL_UNDEF: |
41 | errno = 0; | 36 | errno = 0; |
42 | read_bytes = getline(&status->buffer, | 37 | read_bytes = getline(&status->buffer, |
@@ -61,7 +56,7 @@ bool status_handle_readable(struct status_line *status) { | |||
61 | if (json_object_object_get_ex(header, "click_events", &click_events) | 56 | if (json_object_object_get_ex(header, "click_events", &click_events) |
62 | && json_object_get_boolean(click_events)) { | 57 | && json_object_get_boolean(click_events)) { |
63 | wlr_log(WLR_DEBUG, "Enabling click events."); | 58 | wlr_log(WLR_DEBUG, "Enabling click events."); |
64 | status->i3bar_state.click_events = true; | 59 | status->click_events = true; |
65 | if (write(status->write_fd, "[\n", 2) != 2) { | 60 | if (write(status->write_fd, "[\n", 2) != 2) { |
66 | status_error(status, "[failed to write to status command]"); | 61 | status_error(status, "[failed to write to status command]"); |
67 | json_object_put(header); | 62 | json_object_put(header); |
@@ -70,13 +65,11 @@ bool status_handle_readable(struct status_line *status) { | |||
70 | } | 65 | } |
71 | json_object_put(header); | 66 | json_object_put(header); |
72 | 67 | ||
73 | status->protocol = PROTOCOL_I3BAR; | ||
74 | free(status->buffer); | ||
75 | wl_list_init(&status->blocks); | 68 | wl_list_init(&status->blocks); |
76 | status->i3bar_state.buffer_size = 4096; | 69 | status->tokener = json_tokener_new(); |
77 | status->i3bar_state.buffer = | 70 | status->buffer_index = getdelim(&status->buffer, |
78 | malloc(status->i3bar_state.buffer_size); | 71 | &status->buffer_size, EOF, status->read); |
79 | return false; | 72 | return i3bar_handle_readable(status); |
80 | } | 73 | } |
81 | 74 | ||
82 | wlr_log(WLR_DEBUG, "Using text protocol."); | 75 | wlr_log(WLR_DEBUG, "Using text protocol."); |
@@ -99,10 +92,11 @@ bool status_handle_readable(struct status_line *status) { | |||
99 | return true; | 92 | return true; |
100 | } | 93 | } |
101 | } | 94 | } |
95 | case PROTOCOL_I3BAR: | ||
96 | return i3bar_handle_readable(status); | ||
102 | default: | 97 | default: |
103 | return false; | 98 | return false; |
104 | } | 99 | } |
105 | return false; | ||
106 | } | 100 | } |
107 | 101 | ||
108 | struct status_line *status_line_init(char *cmd) { | 102 | struct status_line *status_line_init(char *cmd) { |
@@ -147,19 +141,14 @@ struct status_line *status_line_init(char *cmd) { | |||
147 | void status_line_free(struct status_line *status) { | 141 | void status_line_free(struct status_line *status) { |
148 | status_line_close_fds(status); | 142 | status_line_close_fds(status); |
149 | kill(status->pid, SIGTERM); | 143 | kill(status->pid, SIGTERM); |
150 | switch (status->protocol) { | 144 | if (status->protocol == PROTOCOL_I3BAR) { |
151 | case PROTOCOL_I3BAR: { | ||
152 | struct i3bar_block *block, *tmp; | 145 | struct i3bar_block *block, *tmp; |
153 | wl_list_for_each_safe(block, tmp, &status->blocks, link) { | 146 | wl_list_for_each_safe(block, tmp, &status->blocks, link) { |
154 | wl_list_remove(&block->link); | 147 | wl_list_remove(&block->link); |
155 | i3bar_block_unref(block); | 148 | i3bar_block_unref(block); |
156 | } | 149 | } |
157 | free(status->i3bar_state.buffer); | ||
158 | break; | ||
159 | } | ||
160 | default: | ||
161 | free(status->buffer); | ||
162 | break; | ||
163 | } | 150 | } |
151 | json_tokener_free(status->tokener); | ||
152 | free(status->buffer); | ||
164 | free(status); | 153 | free(status); |
165 | } | 154 | } |