diff options
author | 2015-12-25 08:06:50 -0700 | |
---|---|---|
committer | 2015-12-25 08:06:50 -0700 | |
commit | 3bcb3b332dd38bba15cf6c1f388f8aad94df077f (patch) | |
tree | f60d3559229b0523ca3215cbd604dc2db19faaf9 /swaybar/main.c | |
parent | Merge pull request #403 from crondog/master (diff) | |
parent | swaybar: remove random whitespaces (diff) | |
download | sway-3bcb3b332dd38bba15cf6c1f388f8aad94df077f.tar.gz sway-3bcb3b332dd38bba15cf6c1f388f8aad94df077f.tar.zst sway-3bcb3b332dd38bba15cf6c1f388f8aad94df077f.zip |
Merge pull request #406 from mikkeloscar/fix-swaybar-json
swaybar: Fix json related crash.
Diffstat (limited to 'swaybar/main.c')
-rw-r--r-- | swaybar/main.c | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/swaybar/main.c b/swaybar/main.c index 9f735f36..434d7363 100644 --- a/swaybar/main.c +++ b/swaybar/main.c | |||
@@ -59,7 +59,7 @@ struct status_block { | |||
59 | list_t *status_line = NULL; | 59 | list_t *status_line = NULL; |
60 | 60 | ||
61 | list_t *workspaces = NULL; | 61 | list_t *workspaces = NULL; |
62 | int socketfd; | 62 | int ipc_socketfd, ipc_listen_socketfd; |
63 | pid_t pid; | 63 | pid_t pid; |
64 | int status_read_fd; | 64 | int status_read_fd; |
65 | char line[1024]; | 65 | char line[1024]; |
@@ -142,6 +142,14 @@ void swaybar_teardown() { | |||
142 | if (status_read_fd) { | 142 | if (status_read_fd) { |
143 | close(status_read_fd); | 143 | close(status_read_fd); |
144 | } | 144 | } |
145 | |||
146 | if (ipc_socketfd) { | ||
147 | close(ipc_socketfd); | ||
148 | } | ||
149 | |||
150 | if (ipc_listen_socketfd) { | ||
151 | close(ipc_listen_socketfd); | ||
152 | } | ||
145 | } | 153 | } |
146 | 154 | ||
147 | void sway_terminate(void) { | 155 | void sway_terminate(void) { |
@@ -181,7 +189,7 @@ void ipc_update_workspaces() { | |||
181 | workspaces = create_list(); | 189 | workspaces = create_list(); |
182 | 190 | ||
183 | uint32_t len = 0; | 191 | uint32_t len = 0; |
184 | char *res = ipc_single_command(socketfd, IPC_GET_WORKSPACES, NULL, &len); | 192 | char *res = ipc_single_command(ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len); |
185 | json_object *results = json_tokener_parse(res); | 193 | json_object *results = json_tokener_parse(res); |
186 | if (!results) { | 194 | if (!results) { |
187 | free(res); | 195 | free(res); |
@@ -189,12 +197,12 @@ void ipc_update_workspaces() { | |||
189 | } | 197 | } |
190 | 198 | ||
191 | int i; | 199 | int i; |
192 | for (i = 0; i < json_object_array_length(results); ++i) { | 200 | int length = json_object_array_length(results); |
193 | json_object *ws_json = json_object_array_get_idx(results, i); | 201 | json_object *ws_json; |
194 | json_object *num, *name, *visible, *focused, *out, *urgent; | 202 | json_object *num, *name, *visible, *focused, *out, *urgent; |
195 | if (!ws_json) { | 203 | for (i = 0; i < length; ++i) { |
196 | return; | 204 | ws_json = json_object_array_get_idx(results, i); |
197 | } | 205 | |
198 | json_object_object_get_ex(ws_json, "num", &num); | 206 | json_object_object_get_ex(ws_json, "num", &num); |
199 | json_object_object_get_ex(ws_json, "name", &name); | 207 | json_object_object_get_ex(ws_json, "name", &name); |
200 | json_object_object_get_ex(ws_json, "visible", &visible); | 208 | json_object_object_get_ex(ws_json, "visible", &visible); |
@@ -259,7 +267,7 @@ static const int ws_spacing = 1; | |||
259 | 267 | ||
260 | void bar_ipc_init(int outputi, const char *bar_id) { | 268 | void bar_ipc_init(int outputi, const char *bar_id) { |
261 | uint32_t len = 0; | 269 | uint32_t len = 0; |
262 | char *res = ipc_single_command(socketfd, IPC_GET_OUTPUTS, NULL, &len); | 270 | char *res = ipc_single_command(ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len); |
263 | json_object *outputs = json_tokener_parse(res); | 271 | json_object *outputs = json_tokener_parse(res); |
264 | json_object *info = json_object_array_get_idx(outputs, outputi); | 272 | json_object *info = json_object_array_get_idx(outputs, outputi); |
265 | json_object *name; | 273 | json_object *name; |
@@ -269,7 +277,7 @@ void bar_ipc_init(int outputi, const char *bar_id) { | |||
269 | json_object_put(outputs); | 277 | json_object_put(outputs); |
270 | 278 | ||
271 | len = strlen(bar_id); | 279 | len = strlen(bar_id); |
272 | res = ipc_single_command(socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); | 280 | res = ipc_single_command(ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); |
273 | 281 | ||
274 | json_object *bar_config = json_tokener_parse(res); | 282 | json_object *bar_config = json_tokener_parse(res); |
275 | json_object *tray_output, *mode, *hidden_state, *position, *_status_command; | 283 | json_object *tray_output, *mode, *hidden_state, *position, *_status_command; |
@@ -384,7 +392,7 @@ void bar_ipc_init(int outputi, const char *bar_id) { | |||
384 | 392 | ||
385 | const char *subscribe_json = "[ \"workspace\" ]"; | 393 | const char *subscribe_json = "[ \"workspace\" ]"; |
386 | len = strlen(subscribe_json); | 394 | len = strlen(subscribe_json); |
387 | res = ipc_single_command(socketfd, IPC_SUBSCRIBE, subscribe_json, &len); | 395 | res = ipc_single_command(ipc_listen_socketfd, IPC_SUBSCRIBE, subscribe_json, &len); |
388 | 396 | ||
389 | ipc_update_workspaces(); | 397 | ipc_update_workspaces(); |
390 | } | 398 | } |
@@ -418,14 +426,14 @@ void render() { | |||
418 | get_text_size(window, &width, &height, "%s", block->full_text); | 426 | get_text_size(window, &width, &height, "%s", block->full_text); |
419 | 427 | ||
420 | int textwidth = width; | 428 | int textwidth = width; |
421 | 429 | ||
422 | if (width < block->min_width) { | 430 | if (width < block->min_width) { |
423 | width = block->min_width; | 431 | width = block->min_width; |
424 | } | 432 | } |
425 | 433 | ||
426 | moved += width + block->separator_block_width; | 434 | moved += width + block->separator_block_width; |
427 | blockpos = window->width - margin - moved; | 435 | blockpos = window->width - margin - moved; |
428 | 436 | ||
429 | int offset = 0; | 437 | int offset = 0; |
430 | 438 | ||
431 | if (strncmp(block->align, "left", 5) == 0) { | 439 | if (strncmp(block->align, "left", 5) == 0) { |
@@ -495,7 +503,7 @@ void free_status_block(void *item) { | |||
495 | return; | 503 | return; |
496 | } | 504 | } |
497 | struct status_block *sb = (struct status_block*)item; | 505 | struct status_block *sb = (struct status_block*)item; |
498 | if (sb->full_text) { | 506 | if (sb->full_text) { |
499 | free(sb->full_text); | 507 | free(sb->full_text); |
500 | } | 508 | } |
501 | if (sb->short_text) { | 509 | if (sb->short_text) { |
@@ -714,7 +722,7 @@ void i3json_ensure_free(int min_free) { | |||
714 | len = strlen(i3json_state.buffer); | 722 | len = strlen(i3json_state.buffer); |
715 | } | 723 | } |
716 | if (i3json_state.bufsize < len+min_free) { | 724 | if (i3json_state.bufsize < len+min_free) { |
717 | i3json_state.bufsize += min_free; | 725 | i3json_state.bufsize += min_free; |
718 | if (i3json_state.bufsize > 1024000) { | 726 | if (i3json_state.bufsize > 1024000) { |
719 | sway_abort("Status line json too long or malformed."); | 727 | sway_abort("Status line json too long or malformed."); |
720 | } | 728 | } |
@@ -822,7 +830,7 @@ void poll_for_update() { | |||
822 | 830 | ||
823 | dirty = false; | 831 | dirty = false; |
824 | FD_ZERO(&readfds); | 832 | FD_ZERO(&readfds); |
825 | FD_SET(socketfd, &readfds); | 833 | FD_SET(ipc_listen_socketfd, &readfds); |
826 | FD_SET(status_read_fd, &readfds); | 834 | FD_SET(status_read_fd, &readfds); |
827 | 835 | ||
828 | activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); | 836 | activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); |
@@ -830,10 +838,10 @@ void poll_for_update() { | |||
830 | sway_log(L_ERROR, "polling failed: %d", errno); | 838 | sway_log(L_ERROR, "polling failed: %d", errno); |
831 | } | 839 | } |
832 | 840 | ||
833 | if (FD_ISSET(socketfd, &readfds)) { | 841 | if (FD_ISSET(ipc_listen_socketfd, &readfds)) { |
834 | sway_log(L_DEBUG, "Got workspace update."); | 842 | sway_log(L_DEBUG, "Got workspace update."); |
835 | uint32_t len; | 843 | uint32_t len; |
836 | char *buf = ipc_recv_response(socketfd, &len); | 844 | char *buf = ipc_recv_response(ipc_listen_socketfd, &len); |
837 | free(buf); | 845 | free(buf); |
838 | ipc_update_workspaces(); | 846 | ipc_update_workspaces(); |
839 | dirty = true; | 847 | dirty = true; |
@@ -937,7 +945,9 @@ int main(int argc, char **argv) { | |||
937 | sway_abort("Unable to retrieve socket path"); | 945 | sway_abort("Unable to retrieve socket path"); |
938 | } | 946 | } |
939 | } | 947 | } |
940 | socketfd = ipc_open_socket(socket_path); | 948 | ipc_socketfd = ipc_open_socket(socket_path); |
949 | ipc_listen_socketfd = ipc_open_socket(socket_path); | ||
950 | |||
941 | 951 | ||
942 | if (argc == optind) { | 952 | if (argc == optind) { |
943 | sway_abort("No output index provided"); | 953 | sway_abort("No output index provided"); |