diff options
Diffstat (limited to 'swaybar/status_line.c')
-rw-r--r-- | swaybar/status_line.c | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/swaybar/status_line.c b/swaybar/status_line.c index a072673b..6b630c49 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c | |||
@@ -19,7 +19,7 @@ struct { | |||
19 | char *parserpos; | 19 | char *parserpos; |
20 | bool escape; | 20 | bool escape; |
21 | int depth; | 21 | int depth; |
22 | int state[I3JSON_MAXDEPTH+1]; | 22 | int bar[I3JSON_MAXDEPTH+1]; |
23 | } i3json_state = { 0, NULL, NULL, NULL, false, 0, { I3JSON_UNKNOWN } }; | 23 | } i3json_state = { 0, NULL, NULL, NULL, false, 0, { I3JSON_UNKNOWN } }; |
24 | 24 | ||
25 | static char line[1024]; | 25 | static char line[1024]; |
@@ -48,7 +48,7 @@ static void free_status_block(void *item) { | |||
48 | free(sb); | 48 | free(sb); |
49 | } | 49 | } |
50 | 50 | ||
51 | static void parse_json(struct swaybar_state *state, const char *text) { | 51 | static void parse_json(struct bar *bar, const char *text) { |
52 | json_object *results = json_tokener_parse(text); | 52 | json_object *results = json_tokener_parse(text); |
53 | if (!results) { | 53 | if (!results) { |
54 | sway_log(L_DEBUG, "Failed to parse json"); | 54 | sway_log(L_DEBUG, "Failed to parse json"); |
@@ -59,12 +59,12 @@ static void parse_json(struct swaybar_state *state, const char *text) { | |||
59 | return; | 59 | return; |
60 | } | 60 | } |
61 | 61 | ||
62 | if (state->status->block_line) { | 62 | if (bar->status->block_line) { |
63 | list_foreach(state->status->block_line, free_status_block); | 63 | list_foreach(bar->status->block_line, free_status_block); |
64 | list_free(state->status->block_line); | 64 | list_free(bar->status->block_line); |
65 | } | 65 | } |
66 | 66 | ||
67 | state->status->block_line = create_list(); | 67 | bar->status->block_line = create_list(); |
68 | 68 | ||
69 | int i; | 69 | int i; |
70 | for (i = 0; i < json_object_array_length(results); ++i) { | 70 | for (i = 0; i < json_object_array_length(results); ++i) { |
@@ -108,7 +108,7 @@ static void parse_json(struct swaybar_state *state, const char *text) { | |||
108 | if (color) { | 108 | if (color) { |
109 | new->color = parse_color(json_object_get_string(color)); | 109 | new->color = parse_color(json_object_get_string(color)); |
110 | } else { | 110 | } else { |
111 | new->color = state->config->colors.statusline; | 111 | new->color = bar->config->colors.statusline; |
112 | } | 112 | } |
113 | 113 | ||
114 | if (min_width) { | 114 | if (min_width) { |
@@ -188,18 +188,18 @@ static void parse_json(struct swaybar_state *state, const char *text) { | |||
188 | new->border_right = 1; | 188 | new->border_right = 1; |
189 | } | 189 | } |
190 | 190 | ||
191 | list_add(state->status->block_line, new); | 191 | list_add(bar->status->block_line, new); |
192 | } | 192 | } |
193 | 193 | ||
194 | json_object_put(results); | 194 | json_object_put(results); |
195 | } | 195 | } |
196 | 196 | ||
197 | // continue parsing from last parserpos | 197 | // continue parsing from last parserpos |
198 | static int i3json_parse(struct swaybar_state *st) { | 198 | static int i3json_parse(struct bar *bar) { |
199 | char *c = i3json_state.parserpos; | 199 | char *c = i3json_state.parserpos; |
200 | int handled = 0; | 200 | int handled = 0; |
201 | while (*c) { | 201 | while (*c) { |
202 | if (i3json_state.state[i3json_state.depth] == I3JSON_STRING) { | 202 | if (i3json_state.bar[i3json_state.depth] == I3JSON_STRING) { |
203 | if (!i3json_state.escape && *c == '"') { | 203 | if (!i3json_state.escape && *c == '"') { |
204 | --i3json_state.depth; | 204 | --i3json_state.depth; |
205 | } | 205 | } |
@@ -211,13 +211,13 @@ static int i3json_parse(struct swaybar_state *st) { | |||
211 | if (i3json_state.depth > I3JSON_MAXDEPTH) { | 211 | if (i3json_state.depth > I3JSON_MAXDEPTH) { |
212 | sway_abort("JSON too deep"); | 212 | sway_abort("JSON too deep"); |
213 | } | 213 | } |
214 | i3json_state.state[i3json_state.depth] = I3JSON_ARRAY; | 214 | i3json_state.bar[i3json_state.depth] = I3JSON_ARRAY; |
215 | if (i3json_state.depth == 2) { | 215 | if (i3json_state.depth == 2) { |
216 | i3json_state.line_start = c; | 216 | i3json_state.line_start = c; |
217 | } | 217 | } |
218 | break; | 218 | break; |
219 | case ']': | 219 | case ']': |
220 | if (i3json_state.state[i3json_state.depth] != I3JSON_ARRAY) { | 220 | if (i3json_state.bar[i3json_state.depth] != I3JSON_ARRAY) { |
221 | sway_abort("JSON malformed"); | 221 | sway_abort("JSON malformed"); |
222 | } | 222 | } |
223 | --i3json_state.depth; | 223 | --i3json_state.depth; |
@@ -225,7 +225,7 @@ static int i3json_parse(struct swaybar_state *st) { | |||
225 | // c[1] is valid since c[0] != '\0' | 225 | // c[1] is valid since c[0] != '\0' |
226 | char p = c[1]; | 226 | char p = c[1]; |
227 | c[1] = '\0'; | 227 | c[1] = '\0'; |
228 | parse_json(st, i3json_state.line_start); | 228 | parse_json(bar, i3json_state.line_start); |
229 | c[1] = p; | 229 | c[1] = p; |
230 | ++handled; | 230 | ++handled; |
231 | i3json_state.line_start = c+1; | 231 | i3json_state.line_start = c+1; |
@@ -236,7 +236,7 @@ static int i3json_parse(struct swaybar_state *st) { | |||
236 | if (i3json_state.depth > I3JSON_MAXDEPTH) { | 236 | if (i3json_state.depth > I3JSON_MAXDEPTH) { |
237 | sway_abort("JSON too deep"); | 237 | sway_abort("JSON too deep"); |
238 | } | 238 | } |
239 | i3json_state.state[i3json_state.depth] = I3JSON_STRING; | 239 | i3json_state.bar[i3json_state.depth] = I3JSON_STRING; |
240 | break; | 240 | break; |
241 | } | 241 | } |
242 | } | 242 | } |
@@ -361,49 +361,49 @@ static void i3json_ensure_free(int min_free) { | |||
361 | } | 361 | } |
362 | 362 | ||
363 | // append data and parse it. | 363 | // append data and parse it. |
364 | static int i3json_handle_data(struct swaybar_state *st, char *data) { | 364 | static int i3json_handle_data(struct bar *bar, char *data) { |
365 | int len = strlen(data); | 365 | int len = strlen(data); |
366 | i3json_ensure_free(len); | 366 | i3json_ensure_free(len); |
367 | strcpy(i3json_state.parserpos, data); | 367 | strcpy(i3json_state.parserpos, data); |
368 | return i3json_parse(st); | 368 | return i3json_parse(bar); |
369 | } | 369 | } |
370 | 370 | ||
371 | // read data from fd and parse it. | 371 | // read data from fd and parse it. |
372 | static int i3json_handle_fd(struct swaybar_state *state) { | 372 | static int i3json_handle_fd(struct bar *bar) { |
373 | i3json_ensure_free(10240); | 373 | i3json_ensure_free(10240); |
374 | // get fresh data at the end of the buffer | 374 | // get fresh data at the end of the buffer |
375 | int readlen = read(state->status_read_fd, i3json_state.parserpos, 10239); | 375 | int readlen = read(bar->status_read_fd, i3json_state.parserpos, 10239); |
376 | if (readlen < 0) { | 376 | if (readlen < 0) { |
377 | return readlen; | 377 | return readlen; |
378 | } | 378 | } |
379 | i3json_state.parserpos[readlen] = '\0'; | 379 | i3json_state.parserpos[readlen] = '\0'; |
380 | return i3json_parse(state); | 380 | return i3json_parse(bar); |
381 | } | 381 | } |
382 | 382 | ||
383 | bool handle_status_line(struct swaybar_state *state) { | 383 | bool handle_status_line(struct bar *bar) { |
384 | bool dirty = false; | 384 | bool dirty = false; |
385 | 385 | ||
386 | switch (state->status->protocol) { | 386 | switch (bar->status->protocol) { |
387 | case I3BAR: | 387 | case I3BAR: |
388 | sway_log(L_DEBUG, "Got i3bar protocol."); | 388 | sway_log(L_DEBUG, "Got i3bar protocol."); |
389 | if (i3json_handle_fd(state) > 0) { | 389 | if (i3json_handle_fd(bar) > 0) { |
390 | dirty = true; | 390 | dirty = true; |
391 | } | 391 | } |
392 | break; | 392 | break; |
393 | case TEXT: | 393 | case TEXT: |
394 | sway_log(L_DEBUG, "Got text protocol."); | 394 | sway_log(L_DEBUG, "Got text protocol."); |
395 | read_line_tail(state->status_read_fd, line, sizeof(line), line_rest); | 395 | read_line_tail(bar->status_read_fd, line, sizeof(line), line_rest); |
396 | dirty = true; | 396 | dirty = true; |
397 | state->status->text_line = line; | 397 | bar->status->text_line = line; |
398 | break; | 398 | break; |
399 | case UNDEF: | 399 | case UNDEF: |
400 | sway_log(L_DEBUG, "Detecting protocol..."); | 400 | sway_log(L_DEBUG, "Detecting protocol..."); |
401 | if (read_line_tail(state->status_read_fd, line, sizeof(line), line_rest) < 0) { | 401 | if (read_line_tail(bar->status_read_fd, line, sizeof(line), line_rest) < 0) { |
402 | break; | 402 | break; |
403 | } | 403 | } |
404 | dirty = true; | 404 | dirty = true; |
405 | state->status->text_line = line; | 405 | bar->status->text_line = line; |
406 | state->status->protocol = TEXT; | 406 | bar->status->protocol = TEXT; |
407 | if (line[0] == '{') { | 407 | if (line[0] == '{') { |
408 | // detect i3bar json protocol | 408 | // detect i3bar json protocol |
409 | json_object *proto = json_tokener_parse(line); | 409 | json_object *proto = json_tokener_parse(line); |
@@ -413,8 +413,8 @@ bool handle_status_line(struct swaybar_state *state) { | |||
413 | && json_object_get_int(version) == 1 | 413 | && json_object_get_int(version) == 1 |
414 | ) { | 414 | ) { |
415 | sway_log(L_DEBUG, "Switched to i3bar protocol."); | 415 | sway_log(L_DEBUG, "Switched to i3bar protocol."); |
416 | state->status->protocol = I3BAR; | 416 | bar->status->protocol = I3BAR; |
417 | i3json_handle_data(state, line_rest); | 417 | i3json_handle_data(bar, line_rest); |
418 | } | 418 | } |
419 | json_object_put(proto); | 419 | json_object_put(proto); |
420 | } | 420 | } |