summaryrefslogtreecommitdiffstats
path: root/swaybar/status_line.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaybar/status_line.c')
-rw-r--r--swaybar/status_line.c144
1 files changed, 78 insertions, 66 deletions
diff --git a/swaybar/status_line.c b/swaybar/status_line.c
index 3ba990bd..401bf6f6 100644
--- a/swaybar/status_line.c
+++ b/swaybar/status_line.c
@@ -7,86 +7,102 @@
7#include <unistd.h> 7#include <unistd.h>
8#include <wlr/util/log.h> 8#include <wlr/util/log.h>
9#include "swaybar/config.h" 9#include "swaybar/config.h"
10#include "swaybar/event_loop.h"
10#include "swaybar/status_line.h" 11#include "swaybar/status_line.h"
11#include "readline.h" 12#include "readline.h"
12 13
14static void status_line_close_fds(struct status_line *status) {
15 if (status->read_fd != -1) {
16 remove_event(status->read_fd);
17 close(status->read_fd);
18 status->read_fd = -1;
19 }
20 if (status->write_fd != -1) {
21 close(status->write_fd);
22 status->write_fd = -1;
23 }
24}
25
13void status_error(struct status_line *status, const char *text) { 26void status_error(struct status_line *status, const char *text) {
14 close(status->read_fd); 27 status_line_close_fds(status);
15 close(status->write_fd);
16 status->protocol = PROTOCOL_ERROR; 28 status->protocol = PROTOCOL_ERROR;
17 status->text = text; 29 status->text = text;
18} 30}
19 31
20bool status_handle_readable(struct status_line *status) { 32bool status_handle_readable(struct status_line *status) {
21 char *line; 33 ssize_t read_bytes = 1;
22 switch (status->protocol) { 34 switch (status->protocol) {
23 case PROTOCOL_ERROR:
24 return false;
25 case PROTOCOL_I3BAR:
26 if (i3bar_handle_readable(status) > 0) {
27 return true;
28 }
29 break;
30 case PROTOCOL_TEXT:
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 }
38 return true;
39 case PROTOCOL_UNDEF: 35 case PROTOCOL_UNDEF:
40 line = read_line_buffer(status->read, 36 errno = 0;
41 status->text_state.buffer, status->text_state.buffer_size); 37 read_bytes = getline(&status->buffer,
42 if (!line) { 38 &status->buffer_size, status->read);
39 if (errno == EAGAIN) {
40 clearerr(status->read);
41 } else if (errno) {
43 status_error(status, "[error reading from status command]"); 42 status_error(status, "[error reading from status command]");
44 return false; 43 return true;
45 } 44 }
46 if (line[0] == '{') { 45
47 json_object *proto = json_tokener_parse(line); 46 // the header must be sent completely the first time round
48 if (proto) { 47 json_object *header, *version;
49 json_object *version; 48 if (status->buffer[read_bytes - 1] == '\n'
50 if (json_object_object_get_ex(proto, "version", &version) 49 && (header = json_tokener_parse(status->buffer))
51 && json_object_get_int(version) == 1) { 50 && json_object_object_get_ex(header, "version", &version)
52 wlr_log(WLR_DEBUG, "Switched to i3bar protocol."); 51 && json_object_get_int(version) == 1) {
53 status->protocol = PROTOCOL_I3BAR; 52 wlr_log(WLR_DEBUG, "Using i3bar protocol.");
54 } 53 status->protocol = PROTOCOL_I3BAR;
55 json_object *click_events; 54
56 if (json_object_object_get_ex( 55 json_object *click_events;
57 proto, "click_events", &click_events) 56 if (json_object_object_get_ex(header, "click_events", &click_events)
58 && json_object_get_boolean(click_events)) { 57 && json_object_get_boolean(click_events)) {
59 wlr_log(WLR_DEBUG, "Enabled click events."); 58 wlr_log(WLR_DEBUG, "Enabling click events.");
60 status->i3bar_state.click_events = true; 59 status->click_events = true;
61 const char *events_array = "[\n"; 60 if (write(status->write_fd, "[\n", 2) != 2) {
62 ssize_t len = strlen(events_array); 61 status_error(status, "[failed to write to status command]");
63 if (write(status->write_fd, events_array, len) != len) { 62 json_object_put(header);
64 status_error(status, 63 return true;
65 "[failed to write to status command]");
66 }
67 } 64 }
68 json_object_put(proto);
69 } 65 }
66 json_object_put(header);
70 67
71 status->protocol = PROTOCOL_I3BAR;
72 free(status->text_state.buffer);
73 wl_list_init(&status->blocks); 68 wl_list_init(&status->blocks);
74 status->i3bar_state.buffer_size = 4096; 69 status->tokener = json_tokener_new();
75 status->i3bar_state.buffer = 70 status->buffer_index = getdelim(&status->buffer,
76 malloc(status->i3bar_state.buffer_size); 71 &status->buffer_size, EOF, status->read);
77 } else { 72 return i3bar_handle_readable(status);
78 status->protocol = PROTOCOL_TEXT;
79 status->text = line;
80 } 73 }
81 return true; 74
75 wlr_log(WLR_DEBUG, "Using text protocol.");
76 status->protocol = PROTOCOL_TEXT;
77 status->text = status->buffer;
78 // intentional fall-through
79 case PROTOCOL_TEXT:
80 errno = 0;
81 while (true) {
82 if (status->buffer[read_bytes - 1] == '\n') {
83 status->buffer[read_bytes - 1] = '\0';
84 }
85 read_bytes = getline(&status->buffer,
86 &status->buffer_size, status->read);
87 if (errno == EAGAIN) {
88 clearerr(status->read);
89 return true;
90 } else if (errno) {
91 status_error(status, "[error reading from status command]");
92 return true;
93 }
94 }
95 case PROTOCOL_I3BAR:
96 return i3bar_handle_readable(status);
97 default:
98 return false;
82 } 99 }
83 return false;
84} 100}
85 101
86struct status_line *status_line_init(char *cmd) { 102struct status_line *status_line_init(char *cmd) {
87 struct status_line *status = calloc(1, sizeof(struct status_line)); 103 struct status_line *status = calloc(1, sizeof(struct status_line));
88 status->text_state.buffer_size = 8192; 104 status->buffer_size = 8192;
89 status->text_state.buffer = malloc(status->text_state.buffer_size); 105 status->buffer = malloc(status->buffer_size);
90 106
91 int pipe_read_fd[2]; 107 int pipe_read_fd[2];
92 int pipe_write_fd[2]; 108 int pipe_write_fd[2];
@@ -123,20 +139,16 @@ struct status_line *status_line_init(char *cmd) {
123} 139}
124 140
125void status_line_free(struct status_line *status) { 141void status_line_free(struct status_line *status) {
126 close(status->read_fd); 142 status_line_close_fds(status);
127 close(status->write_fd);
128 kill(status->pid, SIGTERM); 143 kill(status->pid, SIGTERM);
129 switch (status->protocol) { 144 if (status->protocol == PROTOCOL_I3BAR) {
130 case PROTOCOL_I3BAR:;
131 struct i3bar_block *block, *tmp; 145 struct i3bar_block *block, *tmp;
132 wl_list_for_each_safe(block, tmp, &status->blocks, link) { 146 wl_list_for_each_safe(block, tmp, &status->blocks, link) {
147 wl_list_remove(&block->link);
133 i3bar_block_unref(block); 148 i3bar_block_unref(block);
134 } 149 }
135 free(status->i3bar_state.buffer); 150 json_tokener_free(status->tokener);
136 break;
137 default:
138 free(status->text_state.buffer);
139 break;
140 } 151 }
152 free(status->buffer);
141 free(status); 153 free(status);
142} 154}