diff options
Diffstat (limited to 'swaybar/i3bar.c')
-rw-r--r-- | swaybar/i3bar.c | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c new file mode 100644 index 00000000..923ad755 --- /dev/null +++ b/swaybar/i3bar.c | |||
@@ -0,0 +1,215 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include <json-c/json.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | #include <unistd.h> | ||
6 | #include <wlr/util/log.h> | ||
7 | #include "swaybar/config.h" | ||
8 | #include "swaybar/status_line.h" | ||
9 | |||
10 | static void i3bar_block_free(struct i3bar_block *block) { | ||
11 | if (!block) { | ||
12 | return; | ||
13 | } | ||
14 | wl_list_remove(&block->link); | ||
15 | free(block->full_text); | ||
16 | free(block->short_text); | ||
17 | free(block->align); | ||
18 | free(block->name); | ||
19 | free(block->instance); | ||
20 | free(block->color); | ||
21 | } | ||
22 | |||
23 | static bool i3bar_parse_json(struct status_line *status, const char *text) { | ||
24 | struct i3bar_block *block, *tmp; | ||
25 | wl_list_for_each_safe(block, tmp, &status->blocks, link) { | ||
26 | i3bar_block_free(block); | ||
27 | } | ||
28 | json_object *results = json_tokener_parse(text); | ||
29 | if (!results) { | ||
30 | status_error(status, "[failed to parse i3bar json]"); | ||
31 | return false; | ||
32 | } | ||
33 | wlr_log(L_DEBUG, "Got i3bar json: '%s'", text); | ||
34 | for (size_t i = 0; i < json_object_array_length(results); ++i) { | ||
35 | json_object *full_text, *short_text, *color, *min_width, *align, *urgent; | ||
36 | json_object *name, *instance, *separator, *separator_block_width; | ||
37 | json_object *background, *border, *border_top, *border_bottom; | ||
38 | json_object *border_left, *border_right, *markup; | ||
39 | json_object *json = json_object_array_get_idx(results, i); | ||
40 | if (!json) { | ||
41 | continue; | ||
42 | } | ||
43 | json_object_object_get_ex(json, "full_text", &full_text); | ||
44 | json_object_object_get_ex(json, "short_text", &short_text); | ||
45 | json_object_object_get_ex(json, "color", &color); | ||
46 | json_object_object_get_ex(json, "min_width", &min_width); | ||
47 | json_object_object_get_ex(json, "align", &align); | ||
48 | json_object_object_get_ex(json, "urgent", &urgent); | ||
49 | json_object_object_get_ex(json, "name", &name); | ||
50 | json_object_object_get_ex(json, "instance", &instance); | ||
51 | json_object_object_get_ex(json, "markup", &markup); | ||
52 | json_object_object_get_ex(json, "separator", &separator); | ||
53 | json_object_object_get_ex(json, "separator_block_width", &separator_block_width); | ||
54 | json_object_object_get_ex(json, "background", &background); | ||
55 | json_object_object_get_ex(json, "border", &border); | ||
56 | json_object_object_get_ex(json, "border_top", &border_top); | ||
57 | json_object_object_get_ex(json, "border_bottom", &border_bottom); | ||
58 | json_object_object_get_ex(json, "border_left", &border_left); | ||
59 | json_object_object_get_ex(json, "border_right", &border_right); | ||
60 | |||
61 | struct i3bar_block *block = calloc(1, sizeof(struct i3bar_block)); | ||
62 | block->full_text = full_text ? | ||
63 | strdup(json_object_get_string(full_text)) : NULL; | ||
64 | block->short_text = short_text ? | ||
65 | strdup(json_object_get_string(short_text)) : NULL; | ||
66 | if (color) { | ||
67 | block->color = malloc(sizeof(uint32_t)); | ||
68 | *block->color = parse_color(json_object_get_string(color)); | ||
69 | } | ||
70 | if (min_width) { | ||
71 | json_type type = json_object_get_type(min_width); | ||
72 | if (type == json_type_int) { | ||
73 | block->min_width = json_object_get_int(min_width); | ||
74 | } else if (type == json_type_string) { | ||
75 | /* the width will be calculated when rendering */ | ||
76 | block->min_width = 0; | ||
77 | } | ||
78 | } | ||
79 | block->align = strdup(align ? json_object_get_string(align) : "left"); | ||
80 | block->urgent = urgent ? json_object_get_int(urgent) : false; | ||
81 | block->name = name ? strdup(json_object_get_string(name)) : NULL; | ||
82 | block->instance = instance ? | ||
83 | strdup(json_object_get_string(instance)) : NULL; | ||
84 | if (markup) { | ||
85 | block->markup = false; | ||
86 | const char *markup_str = json_object_get_string(markup); | ||
87 | if (strcmp(markup_str, "pango") == 0) { | ||
88 | block->markup = true; | ||
89 | } | ||
90 | } | ||
91 | block->separator = separator ? json_object_get_int(separator) : true; | ||
92 | block->separator_block_width = separator_block_width ? | ||
93 | json_object_get_int(separator_block_width) : 9; | ||
94 | // Airblader features | ||
95 | block->background = background ? | ||
96 | parse_color(json_object_get_string(background)) : 0; | ||
97 | block->border = border ? | ||
98 | parse_color(json_object_get_string(border)) : 0; | ||
99 | block->border_top = border_top ? json_object_get_int(border_top) : 1; | ||
100 | block->border_bottom = border_bottom ? | ||
101 | json_object_get_int(border_bottom) : 1; | ||
102 | block->border_left = border_left ? json_object_get_int(border_left) : 1; | ||
103 | block->border_right = border_right ? | ||
104 | json_object_get_int(border_right) : 1; | ||
105 | wl_list_insert(&status->blocks, &block->link); | ||
106 | } | ||
107 | return true; | ||
108 | } | ||
109 | |||
110 | bool i3bar_handle_readable(struct status_line *status) { | ||
111 | struct i3bar_protocol_state *state = &status->i3bar_state; | ||
112 | |||
113 | char *cur = &state->buffer[state->buffer_index]; | ||
114 | ssize_t n = read(status->read_fd, cur, | ||
115 | state->buffer_size - state->buffer_index); | ||
116 | if (n == -1) { | ||
117 | status_error(status, "[failed to read from status command]"); | ||
118 | return false; | ||
119 | } | ||
120 | |||
121 | if (n == (ssize_t)(state->buffer_size - state->buffer_index)) { | ||
122 | state->buffer_size = state->buffer_size * 2; | ||
123 | char *new_buffer = realloc(state->buffer, state->buffer_size); | ||
124 | if (!new_buffer) { | ||
125 | free(state->buffer); | ||
126 | status_error(status, "[failed to allocate buffer]"); | ||
127 | return true; | ||
128 | } | ||
129 | state->current_node += new_buffer - state->buffer; | ||
130 | cur += new_buffer - state->buffer; | ||
131 | state->buffer = new_buffer; | ||
132 | } | ||
133 | |||
134 | cur[n] = '\0'; | ||
135 | bool redraw = false; | ||
136 | while (*cur) { | ||
137 | if (state->nodes[state->depth] == JSON_NODE_STRING) { | ||
138 | if (!state->escape && *cur == '"') { | ||
139 | --state->depth; | ||
140 | } | ||
141 | state->escape = !state->escape && *cur == '\\'; | ||
142 | } else { | ||
143 | switch (*cur) { | ||
144 | case '[': | ||
145 | ++state->depth; | ||
146 | if (state->depth > | ||
147 | sizeof(state->nodes) / sizeof(state->nodes[0])) { | ||
148 | status_error(status, "[i3bar json too deep]"); | ||
149 | return false; | ||
150 | } | ||
151 | state->nodes[state->depth] = JSON_NODE_ARRAY; | ||
152 | if (state->depth == 1) { | ||
153 | state->current_node = cur; | ||
154 | } | ||
155 | break; | ||
156 | case ']': | ||
157 | if (state->nodes[state->depth] != JSON_NODE_ARRAY) { | ||
158 | status_error(status, "[failed to parse i3bar json]"); | ||
159 | return false; | ||
160 | } | ||
161 | --state->depth; | ||
162 | if (state->depth == 0) { | ||
163 | // cur[1] is valid since cur[0] != '\0' | ||
164 | char p = cur[1]; | ||
165 | cur[1] = '\0'; | ||
166 | redraw = i3bar_parse_json( | ||
167 | status, state->current_node) || redraw; | ||
168 | cur[1] = p; | ||
169 | memmove(state->buffer, cur, | ||
170 | state->buffer_size - (cur - state->buffer)); | ||
171 | cur = state->buffer; | ||
172 | state->current_node = cur + 1; | ||
173 | } | ||
174 | break; | ||
175 | case '"': | ||
176 | ++state->depth; | ||
177 | if (state->depth > | ||
178 | sizeof(state->nodes) / sizeof(state->nodes[0])) { | ||
179 | status_error(status, "[i3bar json too deep]"); | ||
180 | return false; | ||
181 | } | ||
182 | state->nodes[state->depth] = JSON_NODE_STRING; | ||
183 | break; | ||
184 | } | ||
185 | } | ||
186 | ++cur; | ||
187 | } | ||
188 | state->buffer_index = cur - state->buffer; | ||
189 | return redraw; | ||
190 | } | ||
191 | |||
192 | void i3bar_block_send_click(struct status_line *status, | ||
193 | struct i3bar_block *block, int x, int y, uint32_t button) { | ||
194 | wlr_log(L_DEBUG, "block %s clicked", block->name ? block->name : "(nil)"); | ||
195 | if (!block->name || !status->i3bar_state.click_events) { | ||
196 | return; | ||
197 | } | ||
198 | |||
199 | struct json_object *event_json = json_object_new_object(); | ||
200 | json_object_object_add(event_json, "name", | ||
201 | json_object_new_string(block->name)); | ||
202 | if (block->instance) { | ||
203 | json_object_object_add(event_json, "instance", | ||
204 | json_object_new_string(block->instance)); | ||
205 | } | ||
206 | |||
207 | json_object_object_add(event_json, "button", json_object_new_int(button)); | ||
208 | json_object_object_add(event_json, "x", json_object_new_int(x)); | ||
209 | json_object_object_add(event_json, "y", json_object_new_int(y)); | ||
210 | if (dprintf(status->write_fd, "%s\n", | ||
211 | json_object_to_json_string(event_json)) < 0) { | ||
212 | status_error(status, "[failed to write click event]"); | ||
213 | } | ||
214 | json_object_put(event_json); | ||
215 | } | ||