Skip to content

Commit 4134407

Browse files
authored
Merge pull request #1690 from swaywm/i3bar-json
I3bar json
2 parents 2a8985a + ef50d84 commit 4134407

File tree

10 files changed

+546
-40
lines changed

10 files changed

+546
-40
lines changed

common/pango.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
#include <stdio.h>
77
#include <stdlib.h>
88
#include <string.h>
9+
#include "log.h"
910

1011
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
1112
const char *text, int32_t scale, bool markup) {
1213
PangoLayout *layout = pango_cairo_create_layout(cairo);
1314
PangoAttrList *attrs;
1415
if (markup) {
1516
char *buf;
16-
pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, NULL);
17+
GError *error = NULL;
18+
if (!sway_assert(pango_parse_markup(
19+
text, -1, 0, &attrs, &buf, NULL, &error),
20+
"pango_parse_markup '%s' -> error %s", text,
21+
error ? error->message : NULL)) {
22+
return NULL;
23+
}
1724
pango_layout_set_markup(layout, buf, -1);
1825
free(buf);
1926
} else {

include/swaybar/ipc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "swaybar/bar.h"
55

66
void ipc_initialize(struct swaybar *bar, const char *bar_id);
7-
bool handle_ipc_event(struct swaybar *bar);
7+
bool handle_ipc_readable(struct swaybar *bar);
88
void ipc_get_workspaces(struct swaybar *bar);
99
void ipc_send_workspace_command(struct swaybar *bar, const char *ws);
1010

include/swaybar/status_line.h

+50-3
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,71 @@
77

88
enum status_protocol {
99
PROTOCOL_UNDEF,
10+
PROTOCOL_ERROR,
1011
PROTOCOL_TEXT,
1112
PROTOCOL_I3BAR,
1213
};
1314

15+
struct text_protocol_state {
16+
char *buffer;
17+
size_t buffer_size;
18+
};
19+
20+
enum json_node_type {
21+
JSON_NODE_UNKNOWN,
22+
JSON_NODE_ARRAY,
23+
JSON_NODE_STRING,
24+
};
25+
26+
struct i3bar_protocol_state {
27+
bool click_events;
28+
char *buffer;
29+
size_t buffer_size;
30+
size_t buffer_index;
31+
const char *current_node;
32+
bool escape;
33+
size_t depth;
34+
enum json_node_type nodes[16];
35+
};
36+
37+
struct i3bar_block {
38+
struct wl_list link;
39+
char *full_text, *short_text, *align;
40+
bool urgent;
41+
uint32_t *color;
42+
int min_width;
43+
char *name, *instance;
44+
bool separator;
45+
int separator_block_width;
46+
bool markup;
47+
// Airblader features
48+
uint32_t background;
49+
uint32_t border;
50+
int border_top;
51+
int border_bottom;
52+
int border_left;
53+
int border_right;
54+
};
55+
1456
struct status_line {
1557
pid_t pid;
1658
int read_fd, write_fd;
1759
FILE *read, *write;
1860

1961
enum status_protocol protocol;
2062
const char *text;
63+
struct wl_list blocks; // i3bar_block::link
2164

22-
char *buffer;
23-
size_t buffer_size;
65+
struct text_protocol_state text_state;
66+
struct i3bar_protocol_state i3bar_state;
2467
};
2568

2669
struct status_line *status_line_init(char *cmd);
70+
void status_error(struct status_line *status, const char *text);
71+
bool status_handle_readable(struct status_line *status);
2772
void status_line_free(struct status_line *status);
28-
bool handle_status_readable(struct status_line *status);
73+
bool i3bar_handle_readable(struct status_line *status);
74+
void i3bar_block_send_click(struct status_line *status,
75+
struct i3bar_block *block, int x, int y, uint32_t button);
2976

3077
#endif

sway/tree/layout.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ void arrange_windows(struct sway_container *container,
274274
struct wlr_box *area = &output->sway_output->usable_area;
275275
wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d",
276276
area->width, area->height, area->x, area->y);
277-
container->width = area->width;
278-
container->height = area->height;
277+
container->width = width = area->width;
278+
container->height = height = area->height;
279279
container->x = x = area->x;
280280
container->y = y = area->y;
281281
wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f",

swaybar/bar.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,14 @@ static void display_in(int fd, short mask, void *_bar) {
308308

309309
static void ipc_in(int fd, short mask, void *_bar) {
310310
struct swaybar *bar = (struct swaybar *)_bar;
311-
if (handle_ipc_event(bar)) {
311+
if (handle_ipc_readable(bar)) {
312312
render_all_frames(bar);
313313
}
314314
}
315315

316316
static void status_in(int fd, short mask, void *_bar) {
317317
struct swaybar *bar = (struct swaybar *)_bar;
318-
if (handle_status_readable(bar->status)) {
318+
if (status_handle_readable(bar->status)) {
319319
render_all_frames(bar);
320320
}
321321
}

swaybar/i3bar.c

+208
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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 == 0) {
117+
return 0;
118+
}
119+
120+
if (n == (ssize_t)(state->buffer_size - state->buffer_index)) {
121+
state->buffer_size = state->buffer_size * 2;
122+
char *new_buffer = realloc(state->buffer, state->buffer_size);
123+
if (!new_buffer) {
124+
free(state->buffer);
125+
status_error(status, "[failed to allocate buffer]");
126+
return -1;
127+
}
128+
state->buffer = new_buffer;
129+
}
130+
131+
bool redraw = false;
132+
while (*cur) {
133+
if (state->nodes[state->depth] == JSON_NODE_STRING) {
134+
if (!state->escape && *cur == '"') {
135+
--state->depth;
136+
}
137+
state->escape = !state->escape && *cur == '\\';
138+
} else {
139+
switch (*cur) {
140+
case '[':
141+
++state->depth;
142+
if (state->depth >
143+
sizeof(state->nodes) / sizeof(state->nodes[0])) {
144+
status_error(status, "[i3bar json too deep]");
145+
return false;
146+
}
147+
state->nodes[state->depth] = JSON_NODE_ARRAY;
148+
if (state->depth == 1) {
149+
state->current_node = cur;
150+
}
151+
break;
152+
case ']':
153+
if (state->nodes[state->depth] != JSON_NODE_ARRAY) {
154+
status_error(status, "[failed to parse i3bar json]");
155+
return false;
156+
}
157+
--state->depth;
158+
if (state->depth == 0) {
159+
// cur[1] is valid since cur[0] != '\0'
160+
char p = cur[1];
161+
cur[1] = '\0';
162+
redraw = i3bar_parse_json(
163+
status, state->current_node) || redraw;
164+
cur[1] = p;
165+
memmove(state->buffer, cur,
166+
state->buffer_size - (cur - state->buffer));
167+
cur = state->buffer;
168+
state->current_node = cur + 1;
169+
}
170+
break;
171+
case '"':
172+
++state->depth;
173+
if (state->depth >
174+
sizeof(state->nodes) / sizeof(state->nodes[0])) {
175+
status_error(status, "[i3bar json too deep]");
176+
return false;
177+
}
178+
state->nodes[state->depth] = JSON_NODE_STRING;
179+
break;
180+
}
181+
}
182+
++cur;
183+
}
184+
state->buffer_index = cur - state->buffer;
185+
return redraw;
186+
}
187+
188+
void i3bar_block_send_click(struct status_line *status,
189+
struct i3bar_block *block, int x, int y, uint32_t button) {
190+
wlr_log(L_DEBUG, "block %s clicked", block->name ? block->name : "(nil)");
191+
if (!block->name || !status->i3bar_state.click_events) {
192+
return;
193+
}
194+
195+
struct json_object *event_json = json_object_new_object();
196+
json_object_object_add(event_json, "name",
197+
json_object_new_string(block->name));
198+
if (block->instance) {
199+
json_object_object_add(event_json, "instance",
200+
json_object_new_string(block->instance));
201+
}
202+
203+
json_object_object_add(event_json, "button", json_object_new_int(button));
204+
json_object_object_add(event_json, "x", json_object_new_int(x));
205+
json_object_object_add(event_json, "y", json_object_new_int(y));
206+
dprintf(status->write_fd, "%s\n", json_object_to_json_string(event_json));
207+
json_object_put(event_json);
208+
}

swaybar/ipc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ void ipc_initialize(struct swaybar *bar, const char *bar_id) {
323323
IPC_SUBSCRIBE, subscribe, &len));
324324
}
325325

326-
bool handle_ipc_event(struct swaybar *bar) {
326+
bool handle_ipc_readable(struct swaybar *bar) {
327327
struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd);
328328
if (!resp) {
329329
return false;

swaybar/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ executable(
33
'bar.c',
44
'config.c',
55
'event_loop.c',
6+
'i3bar.c',
67
'ipc.c',
78
'main.c',
89
'render.c',

0 commit comments

Comments
 (0)