aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2018-03-29 11:58:54 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2018-03-29 22:11:08 -0400
commit1e8faeec0263a7da311a13c56a0de34e47e66fa6 (patch)
tree5d66afa22b48864f5a362971348aa0379ef248c6 /swaybar
parentAdd binding mode indicator (diff)
downloadsway-1e8faeec0263a7da311a13c56a0de34e47e66fa6.tar.gz
sway-1e8faeec0263a7da311a13c56a0de34e47e66fa6.tar.zst
sway-1e8faeec0263a7da311a13c56a0de34e47e66fa6.zip
Pixel-perfect rendering
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/config.c1
-rw-r--r--swaybar/ipc.c62
-rw-r--r--swaybar/render.c48
3 files changed, 74 insertions, 37 deletions
diff --git a/swaybar/config.c b/swaybar/config.c
index 83cf2309..802d0779 100644
--- a/swaybar/config.c
+++ b/swaybar/config.c
@@ -41,6 +41,7 @@ struct swaybar_config *init_config() {
41 41
42 /* colors */ 42 /* colors */
43 config->colors.background = 0x000000FF; 43 config->colors.background = 0x000000FF;
44 config->colors.focused_background = 0x000000FF;
44 config->colors.statusline = 0xFFFFFFFF; 45 config->colors.statusline = 0xFFFFFFFF;
45 config->colors.separator = 0x666666FF; 46 config->colors.separator = 0x666666FF;
46 47
diff --git a/swaybar/ipc.c b/swaybar/ipc.c
index 3c2d6fbc..a260b798 100644
--- a/swaybar/ipc.c
+++ b/swaybar/ipc.c
@@ -48,58 +48,76 @@ static void ipc_parse_colors(
48 json_object_object_get_ex(colors, "binding_mode_bg", &binding_mode_bg); 48 json_object_object_get_ex(colors, "binding_mode_bg", &binding_mode_bg);
49 json_object_object_get_ex(colors, "binding_mode_text", &binding_mode_text); 49 json_object_object_get_ex(colors, "binding_mode_text", &binding_mode_text);
50 if (background) { 50 if (background) {
51 config->colors.background = parse_color(json_object_get_string(background)); 51 config->colors.background = parse_color(
52 json_object_get_string(background));
52 } 53 }
53 if (statusline) { 54 if (statusline) {
54 config->colors.statusline = parse_color(json_object_get_string(statusline)); 55 config->colors.statusline = parse_color(
56 json_object_get_string(statusline));
55 } 57 }
56 if (separator) { 58 if (separator) {
57 config->colors.separator = parse_color(json_object_get_string(separator)); 59 config->colors.separator = parse_color(
60 json_object_get_string(separator));
58 } 61 }
59 if (focused_background) { 62 if (focused_background) {
60 config->colors.focused_background = parse_color(json_object_get_string(focused_background)); 63 config->colors.focused_background = parse_color(
64 json_object_get_string(focused_background));
61 } 65 }
62 if (focused_statusline) { 66 if (focused_statusline) {
63 config->colors.focused_statusline = parse_color(json_object_get_string(focused_statusline)); 67 config->colors.focused_statusline = parse_color(
68 json_object_get_string(focused_statusline));
64 } 69 }
65 if (focused_separator) { 70 if (focused_separator) {
66 config->colors.focused_separator = parse_color(json_object_get_string(focused_separator)); 71 config->colors.focused_separator = parse_color(
72 json_object_get_string(focused_separator));
67 } 73 }
68 if (focused_workspace_border) { 74 if (focused_workspace_border) {
69 config->colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border)); 75 config->colors.focused_workspace.border = parse_color(
76 json_object_get_string(focused_workspace_border));
70 } 77 }
71 if (focused_workspace_bg) { 78 if (focused_workspace_bg) {
72 config->colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg)); 79 config->colors.focused_workspace.background = parse_color(
80 json_object_get_string(focused_workspace_bg));
73 } 81 }
74 if (focused_workspace_text) { 82 if (focused_workspace_text) {
75 config->colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text)); 83 config->colors.focused_workspace.text = parse_color(
84 json_object_get_string(focused_workspace_text));
76 } 85 }
77 if (active_workspace_border) { 86 if (active_workspace_border) {
78 config->colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border)); 87 config->colors.active_workspace.border = parse_color(
88 json_object_get_string(active_workspace_border));
79 } 89 }
80 if (active_workspace_bg) { 90 if (active_workspace_bg) {
81 config->colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg)); 91 config->colors.active_workspace.background = parse_color(
92 json_object_get_string(active_workspace_bg));
82 } 93 }
83 if (active_workspace_text) { 94 if (active_workspace_text) {
84 config->colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text)); 95 config->colors.active_workspace.text = parse_color(
96 json_object_get_string(active_workspace_text));
85 } 97 }
86 if (inactive_workspace_border) { 98 if (inactive_workspace_border) {
87 config->colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border)); 99 config->colors.inactive_workspace.border = parse_color(
100 json_object_get_string(inactive_workspace_border));
88 } 101 }
89 if (inactive_workspace_bg) { 102 if (inactive_workspace_bg) {
90 config->colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg)); 103 config->colors.inactive_workspace.background = parse_color(
104 json_object_get_string(inactive_workspace_bg));
91 } 105 }
92 if (inactive_workspace_text) { 106 if (inactive_workspace_text) {
93 config->colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text)); 107 config->colors.inactive_workspace.text = parse_color(
108 json_object_get_string(inactive_workspace_text));
94 } 109 }
95 if (binding_mode_border) { 110 if (binding_mode_border) {
96 config->colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border)); 111 config->colors.binding_mode.border = parse_color(
112 json_object_get_string(binding_mode_border));
97 } 113 }
98 if (binding_mode_bg) { 114 if (binding_mode_bg) {
99 config->colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg)); 115 config->colors.binding_mode.background = parse_color(
116 json_object_get_string(binding_mode_bg));
100 } 117 }
101 if (binding_mode_text) { 118 if (binding_mode_text) {
102 config->colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text)); 119 config->colors.binding_mode.text = parse_color(
120 json_object_get_string(binding_mode_text));
103 } 121 }
104} 122}
105 123
@@ -306,14 +324,13 @@ bool handle_ipc_event(struct swaybar *bar) {
306 ipc_get_workspaces(bar); 324 ipc_get_workspaces(bar);
307 break; 325 break;
308 case IPC_EVENT_MODE: { 326 case IPC_EVENT_MODE: {
309 // TODO: interpret "pango_markup" field
310 json_object *result = json_tokener_parse(resp->payload); 327 json_object *result = json_tokener_parse(resp->payload);
311 if (!result) { 328 if (!result) {
312 free_ipc_response(resp); 329 free_ipc_response(resp);
313 wlr_log(L_ERROR, "failed to parse payload as json"); 330 wlr_log(L_ERROR, "failed to parse payload as json");
314 return false; 331 return false;
315 } 332 }
316 json_object *json_change; 333 json_object *json_change, *json_pango_markup;
317 if (json_object_object_get_ex(result, "change", &json_change)) { 334 if (json_object_object_get_ex(result, "change", &json_change)) {
318 const char *change = json_object_get_string(json_change); 335 const char *change = json_object_get_string(json_change);
319 free(bar->config->mode); 336 free(bar->config->mode);
@@ -328,6 +345,11 @@ bool handle_ipc_event(struct swaybar *bar) {
328 free_ipc_response(resp); 345 free_ipc_response(resp);
329 return false; 346 return false;
330 } 347 }
348 if (json_object_object_get_ex(result,
349 "pango_markup", &json_pango_markup)) {
350 bar->config->mode_pango_markup = json_object_get_boolean(
351 json_pango_markup);
352 }
331 json_object_put(result); 353 json_object_put(result);
332 break; 354 break;
333 } 355 }
diff --git a/swaybar/render.c b/swaybar/render.c
index beb4de40..ba22e9d4 100644
--- a/swaybar/render.c
+++ b/swaybar/render.c
@@ -13,33 +13,39 @@
13 13
14static const int ws_horizontal_padding = 5; 14static const int ws_horizontal_padding = 5;
15static const double ws_vertical_padding = 1.5; 15static const double ws_vertical_padding = 1.5;
16static const int ws_spacing = 1; 16static const double border_width = 1;
17 17
18static uint32_t render_binding_mode_indicator(cairo_t *cairo, 18static uint32_t render_binding_mode_indicator(cairo_t *cairo,
19 struct swaybar_config *config, const char *mode, double x, 19 struct swaybar_config *config, const char *mode, double x,
20 uint32_t height) { 20 uint32_t height) {
21 int text_width, text_height; 21 int text_width, text_height;
22 get_text_size(cairo, config->font, &text_width, &text_height, 22 get_text_size(cairo, config->font, &text_width, &text_height,
23 1, true, "⚡ %s", mode); 23 1, true, "%s", mode);
24 uint32_t ideal_height = text_height + ws_vertical_padding * 2; 24 uint32_t ideal_height = text_height + ws_vertical_padding * 2
25 + border_width * 2;
25 if (height < ideal_height) { 26 if (height < ideal_height) {
26 height = ideal_height; 27 height = ideal_height;
27 } 28 }
29 uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
28 30
29 cairo_set_source_u32(cairo, config->colors.binding_mode.background); 31 cairo_set_source_u32(cairo, config->colors.binding_mode.background);
30 cairo_rectangle(cairo, x, 0, text_width + ws_horizontal_padding * 2 - 1, 32 cairo_rectangle(cairo, x, 0, width, height);
31 height + ws_vertical_padding * 2);
32 cairo_fill(cairo); 33 cairo_fill(cairo);
33 34
34 cairo_set_source_u32(cairo, config->colors.binding_mode.border); 35 cairo_set_source_u32(cairo, config->colors.binding_mode.border);
35 cairo_rectangle(cairo, x, 0, text_width + ws_horizontal_padding * 2 - 1, 36 cairo_rectangle(cairo, x, 0, width, border_width);
36 height + ws_vertical_padding * 2); 37 cairo_fill(cairo);
37 cairo_stroke(cairo); 38 cairo_rectangle(cairo, x, 0, border_width, height);
39 cairo_fill(cairo);
40 cairo_rectangle(cairo, x + width - border_width, 0, border_width, height);
41 cairo_fill(cairo);
42 cairo_rectangle(cairo, x, height - border_width, width, border_width);
43 cairo_fill(cairo);
38 44
39 double text_y = height / 2.0 - text_height / 2.0; 45 double text_y = height / 2.0 - text_height / 2.0;
40 cairo_set_source_u32(cairo, config->colors.binding_mode.text); 46 cairo_set_source_u32(cairo, config->colors.binding_mode.text);
41 cairo_move_to(cairo, (int)x + ws_horizontal_padding, (int)floor(text_y)); 47 cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y));
42 pango_printf(cairo, config->font, 1, true, "%s", mode); 48 pango_printf(cairo, config->font, 1, true, "%s", mode);
43 return ideal_height; 49 return ideal_height;
44} 50}
45 51
@@ -78,26 +84,33 @@ static uint32_t render_workspace_button(cairo_t *cairo,
78 int text_width, text_height; 84 int text_width, text_height;
79 get_text_size(cairo, config->font, &text_width, &text_height, 85 get_text_size(cairo, config->font, &text_width, &text_height,
80 1, true, "%s", name); 86 1, true, "%s", name);
81 uint32_t ideal_height = ws_vertical_padding * 2 + text_height; 87 uint32_t ideal_height = ws_vertical_padding * 2 + text_height
88 + border_width * 2;
82 if (height < ideal_height) { 89 if (height < ideal_height) {
83 height = ideal_height; 90 height = ideal_height;
84 } 91 }
85 uint32_t width = ws_horizontal_padding * 2 + text_width; 92 uint32_t width = ws_horizontal_padding * 2 + text_width + border_width * 2;
86 93
87 cairo_set_source_u32(cairo, box_colors.background); 94 cairo_set_source_u32(cairo, box_colors.background);
88 cairo_rectangle(cairo, *x, 0, width - 1, height); 95 cairo_rectangle(cairo, *x, 0, width, height);
89 cairo_fill(cairo); 96 cairo_fill(cairo);
90 97
91 cairo_set_source_u32(cairo, box_colors.border); 98 cairo_set_source_u32(cairo, box_colors.border);
92 cairo_rectangle(cairo, *x, 0, width - 1, height); 99 cairo_rectangle(cairo, *x, 0, width, border_width);
93 cairo_stroke(cairo); 100 cairo_fill(cairo);
101 cairo_rectangle(cairo, *x, 0, border_width, height);
102 cairo_fill(cairo);
103 cairo_rectangle(cairo, *x + width - border_width, 0, border_width, height);
104 cairo_fill(cairo);
105 cairo_rectangle(cairo, *x, height - border_width, width, border_width);
106 cairo_fill(cairo);
94 107
95 double text_y = height / 2.0 - text_height / 2.0; 108 double text_y = height / 2.0 - text_height / 2.0;
96 cairo_set_source_u32(cairo, box_colors.text); 109 cairo_set_source_u32(cairo, box_colors.text);
97 cairo_move_to(cairo, (int)*x + ws_horizontal_padding, (int)floor(text_y)); 110 cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y));
98 pango_printf(cairo, config->font, 1, true, "%s", name); 111 pango_printf(cairo, config->font, 1, true, "%s", name);
99 112
100 *x += width + ws_spacing; 113 *x += width;
101 return ideal_height; 114 return ideal_height;
102} 115}
103 116
@@ -167,6 +180,7 @@ void render_frame(struct swaybar *bar,
167 180
168 cairo_set_source_surface(shm, recorder, 0.0, 0.0); 181 cairo_set_source_surface(shm, recorder, 0.0, 0.0);
169 cairo_paint(shm); 182 cairo_paint(shm);
183
170 wl_surface_attach(output->surface, 184 wl_surface_attach(output->surface,
171 output->current_buffer->buffer, 0, 0); 185 output->current_buffer->buffer, 0, 0);
172 wl_surface_damage(output->surface, 0, 0, output->width, output->height); 186 wl_surface_damage(output->surface, 0, 0, output->width, output->height);