diff options
Diffstat (limited to 'swaybar/status_line.c')
-rw-r--r-- | swaybar/status_line.c | 69 |
1 files changed, 59 insertions, 10 deletions
diff --git a/swaybar/status_line.c b/swaybar/status_line.c index 3454f207..8afe4707 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c | |||
@@ -1,5 +1,6 @@ | |||
1 | #define _POSIX_C_SOURCE | 1 | #define _POSIX_C_SOURCE |
2 | #include <fcntl.h> | 2 | #include <fcntl.h> |
3 | #include <json-c/json.h> | ||
3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
4 | #include <string.h> | 5 | #include <string.h> |
5 | #include <stdio.h> | 6 | #include <stdio.h> |
@@ -9,35 +10,83 @@ | |||
9 | #include "swaybar/status_line.h" | 10 | #include "swaybar/status_line.h" |
10 | #include "readline.h" | 11 | #include "readline.h" |
11 | 12 | ||
12 | bool handle_status_readable(struct status_line *status) { | 13 | void status_error(struct status_line *status, const char *text) { |
13 | char *line = read_line_buffer(status->read, | 14 | close(status->read_fd); |
14 | status->buffer, status->buffer_size); | 15 | close(status->write_fd); |
16 | status->protocol = PROTOCOL_ERROR; | ||
17 | status->text = text; | ||
18 | } | ||
19 | |||
20 | bool status_handle_readable(struct status_line *status) { | ||
21 | char *line; | ||
15 | switch (status->protocol) { | 22 | switch (status->protocol) { |
23 | case PROTOCOL_ERROR: | ||
24 | return false; | ||
16 | case PROTOCOL_I3BAR: | 25 | case PROTOCOL_I3BAR: |
17 | // TODO | 26 | if (i3bar_handle_readable(status) > 0) { |
27 | return true; | ||
28 | } | ||
18 | break; | 29 | break; |
19 | case PROTOCOL_TEXT: | 30 | case PROTOCOL_TEXT: |
20 | status->text = line; | 31 | line = read_line_buffer(status->read, |
32 | status->text_state.buffer, status->text_state.buffer_size); | ||
33 | if (!line) { | ||
34 | status_error(status, "[error reading from status command]"); | ||
35 | } else { | ||
36 | status->text = line; | ||
37 | } | ||
21 | return true; | 38 | return true; |
22 | case PROTOCOL_UNDEF: | 39 | case PROTOCOL_UNDEF: |
40 | line = read_line_buffer(status->read, | ||
41 | status->text_state.buffer, status->text_state.buffer_size); | ||
23 | if (!line) { | 42 | if (!line) { |
43 | status_error(status, "[error reading from status command]"); | ||
24 | return false; | 44 | return false; |
25 | } | 45 | } |
26 | if (line[0] == '{') { | 46 | if (line[0] == '{') { |
27 | // TODO: JSON | 47 | json_object *proto = json_tokener_parse(line); |
48 | if (proto) { | ||
49 | json_object *version; | ||
50 | if (json_object_object_get_ex(proto, "version", &version) | ||
51 | && json_object_get_int(version) == 1) { | ||
52 | wlr_log(L_DEBUG, "Switched to i3bar protocol."); | ||
53 | status->protocol = PROTOCOL_I3BAR; | ||
54 | } | ||
55 | json_object *click_events; | ||
56 | if (json_object_object_get_ex( | ||
57 | proto, "click_events", &click_events) | ||
58 | && json_object_get_boolean(click_events)) { | ||
59 | wlr_log(L_DEBUG, "Enabled click events."); | ||
60 | status->i3bar_state.click_events = true; | ||
61 | const char *events_array = "[\n"; | ||
62 | ssize_t len = strlen(events_array); | ||
63 | if (write(status->write_fd, events_array, len) != len) { | ||
64 | status_error(status, | ||
65 | "[failed to write to status command]"); | ||
66 | } | ||
67 | } | ||
68 | json_object_put(proto); | ||
69 | } | ||
70 | |||
71 | status->protocol = PROTOCOL_I3BAR; | ||
72 | free(status->text_state.buffer); | ||
73 | wl_list_init(&status->blocks); | ||
74 | status->i3bar_state.buffer_size = 4096; | ||
75 | status->i3bar_state.buffer = | ||
76 | malloc(status->i3bar_state.buffer_size); | ||
28 | } else { | 77 | } else { |
29 | status->text = line; | ||
30 | status->protocol = PROTOCOL_TEXT; | 78 | status->protocol = PROTOCOL_TEXT; |
79 | status->text = line; | ||
31 | } | 80 | } |
32 | return false; | 81 | return true; |
33 | } | 82 | } |
34 | return false; | 83 | return false; |
35 | } | 84 | } |
36 | 85 | ||
37 | struct status_line *status_line_init(char *cmd) { | 86 | struct status_line *status_line_init(char *cmd) { |
38 | struct status_line *status = calloc(1, sizeof(struct status_line)); | 87 | struct status_line *status = calloc(1, sizeof(struct status_line)); |
39 | status->buffer_size = 4096; | 88 | status->text_state.buffer_size = 8192; |
40 | status->buffer = malloc(status->buffer_size); | 89 | status->text_state.buffer = malloc(status->text_state.buffer_size); |
41 | 90 | ||
42 | int pipe_read_fd[2]; | 91 | int pipe_read_fd[2]; |
43 | int pipe_write_fd[2]; | 92 | int pipe_write_fd[2]; |