aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/status_line.c
diff options
context:
space:
mode:
authorLibravatar Ian Fan <ianfan0@gmail.com>2018-09-17 14:10:57 +0100
committerLibravatar Ian Fan <ianfan0@gmail.com>2018-09-18 11:36:33 +0100
commit7882ac66ef4308922045fd100e6a9e12942a240b (patch)
tree67ce4f14f56372c86eb9992eef354d1d76f3081d /swaybar/status_line.c
parentswaybar: rewrite protocol determination (diff)
downloadsway-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.
Diffstat (limited to 'swaybar/status_line.c')
-rw-r--r--swaybar/status_line.c31
1 files changed, 10 insertions, 21 deletions
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) {
32bool status_handle_readable(struct status_line *status) { 32bool 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
108struct status_line *status_line_init(char *cmd) { 102struct status_line *status_line_init(char *cmd) {
@@ -147,19 +141,14 @@ struct status_line *status_line_init(char *cmd) {
147void status_line_free(struct status_line *status) { 141void 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}