aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/status_line.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaybar/status_line.c')
-rw-r--r--swaybar/status_line.c148
1 files changed, 82 insertions, 66 deletions
diff --git a/swaybar/status_line.c b/swaybar/status_line.c
index 3ba990bd..48b43248 100644
--- a/swaybar/status_line.c
+++ b/swaybar/status_line.c
@@ -7,86 +7,106 @@
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 read_bytes = getdelim(&status->buffer, &status->buffer_size, EOF, status->read);
76 malloc(status->i3bar_state.buffer_size); 71 if (read_bytes > 0) {
77 } else { 72 status->buffer_index = read_bytes;
78 status->protocol = PROTOCOL_TEXT; 73 return i3bar_handle_readable(status);
79 status->text = line; 74 } else {
75 return false;
76 }
77 }
78
79 wlr_log(WLR_DEBUG, "Using text protocol.");
80 status->protocol = PROTOCOL_TEXT;
81 status->text = status->buffer;
82 // intentional fall-through
83 case PROTOCOL_TEXT:
84 errno = 0;
85 while (true) {
86 if (status->buffer[read_bytes - 1] == '\n') {
87 status->buffer[read_bytes - 1] = '\0';
88 }
89 read_bytes = getline(&status->buffer,
90 &status->buffer_size, status->read);
91 if (errno == EAGAIN) {
92 clearerr(status->read);
93 return true;
94 } else if (errno) {
95 status_error(status, "[error reading from status command]");
96 return true;
97 }
80 } 98 }
81 return true; 99 case PROTOCOL_I3BAR:
100 return i3bar_handle_readable(status);
101 default:
102 return false;
82 } 103 }
83 return false;
84} 104}
85 105
86struct status_line *status_line_init(char *cmd) { 106struct status_line *status_line_init(char *cmd) {
87 struct status_line *status = calloc(1, sizeof(struct status_line)); 107 struct status_line *status = calloc(1, sizeof(struct status_line));
88 status->text_state.buffer_size = 8192; 108 status->buffer_size = 8192;
89 status->text_state.buffer = malloc(status->text_state.buffer_size); 109 status->buffer = malloc(status->buffer_size);
90 110
91 int pipe_read_fd[2]; 111 int pipe_read_fd[2];
92 int pipe_write_fd[2]; 112 int pipe_write_fd[2];
@@ -123,20 +143,16 @@ struct status_line *status_line_init(char *cmd) {
123} 143}
124 144
125void status_line_free(struct status_line *status) { 145void status_line_free(struct status_line *status) {
126 close(status->read_fd); 146 status_line_close_fds(status);
127 close(status->write_fd);
128 kill(status->pid, SIGTERM); 147 kill(status->pid, SIGTERM);
129 switch (status->protocol) { 148 if (status->protocol == PROTOCOL_I3BAR) {
130 case PROTOCOL_I3BAR:;
131 struct i3bar_block *block, *tmp; 149 struct i3bar_block *block, *tmp;
132 wl_list_for_each_safe(block, tmp, &status->blocks, link) { 150 wl_list_for_each_safe(block, tmp, &status->blocks, link) {
151 wl_list_remove(&block->link);
133 i3bar_block_unref(block); 152 i3bar_block_unref(block);
134 } 153 }
135 free(status->i3bar_state.buffer); 154 json_tokener_free(status->tokener);
136 break;
137 default:
138 free(status->text_state.buffer);
139 break;
140 } 155 }
156 free(status->buffer);
141 free(status); 157 free(status);
142} 158}