summaryrefslogtreecommitdiffstats
path: root/swaybar/render.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaybar/render.c')
-rw-r--r--swaybar/render.c110
1 files changed, 52 insertions, 58 deletions
diff --git a/swaybar/render.c b/swaybar/render.c
index 9413dc57..90e5bac7 100644
--- a/swaybar/render.c
+++ b/swaybar/render.c
@@ -10,6 +10,7 @@
10#include "pool-buffer.h" 10#include "pool-buffer.h"
11#include "swaybar/bar.h" 11#include "swaybar/bar.h"
12#include "swaybar/config.h" 12#include "swaybar/config.h"
13#include "swaybar/i3bar.h"
13#include "swaybar/ipc.h" 14#include "swaybar/ipc.h"
14#include "swaybar/render.h" 15#include "swaybar/render.h"
15#include "swaybar/status_line.h" 16#include "swaybar/status_line.h"
@@ -20,47 +21,47 @@ static const double WS_VERTICAL_PADDING = 1.5;
20static const double BORDER_WIDTH = 1; 21static const double BORDER_WIDTH = 1;
21 22
22static uint32_t render_status_line_error(cairo_t *cairo, 23static uint32_t render_status_line_error(cairo_t *cairo,
23 struct swaybar_output *output, struct swaybar_config *config, 24 struct swaybar_output *output, double *x) {
24 const char *error, double *x, uint32_t surface_height) { 25 const char *error = output->bar->status->text;
25 if (!error) { 26 if (!error) {
26 return 0; 27 return 0;
27 } 28 }
28 29
29 uint32_t height = surface_height * output->scale; 30 uint32_t height = output->height * output->scale;
30 31
31 cairo_set_source_u32(cairo, 0xFF0000FF); 32 cairo_set_source_u32(cairo, 0xFF0000FF);
32 33
33 int margin = 3 * output->scale; 34 int margin = 3 * output->scale;
34 int ws_vertical_padding = WS_VERTICAL_PADDING * output->scale; 35 int ws_vertical_padding = WS_VERTICAL_PADDING * output->scale;
35 36
37 char *font = output->bar->config->font;
36 int text_width, text_height; 38 int text_width, text_height;
37 get_text_size(cairo, config->font, &text_width, &text_height, NULL, 39 get_text_size(cairo, font, &text_width, &text_height, NULL,
38 output->scale, false, "%s", error); 40 output->scale, false, "%s", error);
39 41
40 uint32_t ideal_height = text_height + ws_vertical_padding * 2; 42 uint32_t ideal_height = text_height + ws_vertical_padding * 2;
41 uint32_t ideal_surface_height = ideal_height / output->scale; 43 uint32_t ideal_surface_height = ideal_height / output->scale;
42 if (surface_height < ideal_surface_height) { 44 if (output->height < ideal_surface_height) {
43 return ideal_surface_height; 45 return ideal_surface_height;
44 } 46 }
45 *x -= text_width + margin; 47 *x -= text_width + margin;
46 48
47 double text_y = height / 2.0 - text_height / 2.0; 49 double text_y = height / 2.0 - text_height / 2.0;
48 cairo_move_to(cairo, *x, (int)floor(text_y)); 50 cairo_move_to(cairo, *x, (int)floor(text_y));
49 pango_printf(cairo, config->font, output->scale, false, "%s", error); 51 pango_printf(cairo, font, output->scale, false, "%s", error);
50 *x -= margin; 52 *x -= margin;
51 return surface_height; 53 return output->height;
52} 54}
53 55
54static uint32_t render_status_line_text(cairo_t *cairo, 56static uint32_t render_status_line_text(cairo_t *cairo,
55 struct swaybar_output *output, struct swaybar_config *config, 57 struct swaybar_output *output, double *x) {
56 const char *text, bool focused, double *x, uint32_t surface_height) { 58 const char *text = output->bar->status->text;
57 if (!text) { 59 if (!text) {
58 return 0; 60 return 0;
59 } 61 }
60 62
61 uint32_t height = surface_height * output->scale; 63 struct swaybar_config *config = output->bar->config;
62 64 cairo_set_source_u32(cairo, output->focused ?
63 cairo_set_source_u32(cairo, focused ?
64 config->colors.focused_statusline : config->colors.statusline); 65 config->colors.focused_statusline : config->colors.statusline);
65 66
66 int text_width, text_height; 67 int text_width, text_height;
@@ -72,17 +73,18 @@ static uint32_t render_status_line_text(cairo_t *cairo,
72 73
73 uint32_t ideal_height = text_height + ws_vertical_padding * 2; 74 uint32_t ideal_height = text_height + ws_vertical_padding * 2;
74 uint32_t ideal_surface_height = ideal_height / output->scale; 75 uint32_t ideal_surface_height = ideal_height / output->scale;
75 if (surface_height < ideal_surface_height) { 76 if (output->height < ideal_surface_height) {
76 return ideal_surface_height; 77 return ideal_surface_height;
77 } 78 }
78 79
79 *x -= text_width + margin; 80 *x -= text_width + margin;
81 uint32_t height = output->height * output->scale;
80 double text_y = height / 2.0 - text_height / 2.0; 82 double text_y = height / 2.0 - text_height / 2.0;
81 cairo_move_to(cairo, *x, (int)floor(text_y)); 83 cairo_move_to(cairo, *x, (int)floor(text_y));
82 pango_printf(cairo, config->font, output->scale, 84 pango_printf(cairo, config->font, output->scale,
83 config->pango_markup, "%s", text); 85 config->pango_markup, "%s", text);
84 *x -= margin; 86 *x -= margin;
85 return surface_height; 87 return output->height;
86} 88}
87 89
88static void render_sharp_line(cairo_t *cairo, uint32_t color, 90static void render_sharp_line(cairo_t *cairo, uint32_t color,
@@ -122,12 +124,11 @@ static void i3bar_block_unref_callback(void *data) {
122 124
123static uint32_t render_status_block(cairo_t *cairo, 125static uint32_t render_status_block(cairo_t *cairo,
124 struct swaybar_output *output, struct i3bar_block *block, double *x, 126 struct swaybar_output *output, struct i3bar_block *block, double *x,
125 uint32_t surface_height, bool focused, bool edge) { 127 bool edge) {
126 if (!block->full_text || !*block->full_text) { 128 if (!block->full_text || !*block->full_text) {
127 return 0; 129 return 0;
128 } 130 }
129 131
130 uint32_t height = surface_height * output->scale;
131 struct swaybar_config *config = output->bar->config; 132 struct swaybar_config *config = output->bar->config;
132 133
133 int text_width, text_height; 134 int text_width, text_height;
@@ -145,7 +146,7 @@ static uint32_t render_status_block(cairo_t *cairo,
145 double block_width = width; 146 double block_width = width;
146 uint32_t ideal_height = text_height + ws_vertical_padding * 2; 147 uint32_t ideal_height = text_height + ws_vertical_padding * 2;
147 uint32_t ideal_surface_height = ideal_height / output->scale; 148 uint32_t ideal_surface_height = ideal_height / output->scale;
148 if (surface_height < ideal_surface_height) { 149 if (output->height < ideal_surface_height) {
149 return ideal_surface_height; 150 return ideal_surface_height;
150 } 151 }
151 152
@@ -166,7 +167,7 @@ static uint32_t render_status_block(cairo_t *cairo,
166 output->scale, false, "%s", config->sep_symbol); 167 output->scale, false, "%s", config->sep_symbol);
167 uint32_t _ideal_height = sep_height + ws_vertical_padding * 2; 168 uint32_t _ideal_height = sep_height + ws_vertical_padding * 2;
168 uint32_t _ideal_surface_height = _ideal_height / output->scale; 169 uint32_t _ideal_surface_height = _ideal_height / output->scale;
169 if (surface_height < _ideal_surface_height) { 170 if (output->height < _ideal_surface_height) {
170 return _ideal_surface_height; 171 return _ideal_surface_height;
171 } 172 }
172 if (sep_width > block->separator_block_width) { 173 if (sep_width > block->separator_block_width) {
@@ -178,6 +179,7 @@ static uint32_t render_status_block(cairo_t *cairo,
178 *x -= margin; 179 *x -= margin;
179 } 180 }
180 181
182 uint32_t height = output->height * output->scale;
181 if (output->bar->status->click_events) { 183 if (output->bar->status->click_events) {
182 struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); 184 struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot));
183 hotspot->x = *x; 185 hotspot->x = *x;
@@ -241,7 +243,7 @@ static uint32_t render_status_block(cairo_t *cairo,
241 } 243 }
242 244
243 if (!edge && block->separator) { 245 if (!edge && block->separator) {
244 if (focused) { 246 if (output->focused) {
245 cairo_set_source_u32(cairo, config->colors.focused_separator); 247 cairo_set_source_u32(cairo, config->colors.focused_separator);
246 } else { 248 } else {
247 cairo_set_source_u32(cairo, config->colors.separator); 249 cairo_set_source_u32(cairo, config->colors.separator);
@@ -260,19 +262,16 @@ static uint32_t render_status_block(cairo_t *cairo,
260 cairo_stroke(cairo); 262 cairo_stroke(cairo);
261 } 263 }
262 } 264 }
263 return surface_height; 265 return output->height;
264} 266}
265 267
266static uint32_t render_status_line_i3bar(cairo_t *cairo, 268static uint32_t render_status_line_i3bar(cairo_t *cairo,
267 struct swaybar_config *config, struct swaybar_output *output, 269 struct swaybar_output *output, double *x) {
268 struct status_line *status, bool focused,
269 double *x, uint32_t surface_height) {
270 uint32_t max_height = 0; 270 uint32_t max_height = 0;
271 bool edge = true; 271 bool edge = true;
272 struct i3bar_block *block; 272 struct i3bar_block *block;
273 wl_list_for_each(block, &status->blocks, link) { 273 wl_list_for_each(block, &output->bar->status->blocks, link) {
274 uint32_t h = render_status_block(cairo, output, 274 uint32_t h = render_status_block(cairo, output, block, x, edge);
275 block, x, surface_height, focused, edge);
276 max_height = h > max_height ? h : max_height; 275 max_height = h > max_height ? h : max_height;
277 edge = false; 276 edge = false;
278 } 277 }
@@ -280,19 +279,15 @@ static uint32_t render_status_line_i3bar(cairo_t *cairo,
280} 279}
281 280
282static uint32_t render_status_line(cairo_t *cairo, 281static uint32_t render_status_line(cairo_t *cairo,
283 struct swaybar_config *config, struct swaybar_output *output, 282 struct swaybar_output *output, double *x) {
284 struct status_line *status, bool focused, 283 struct status_line *status = output->bar->status;
285 double *x, uint32_t surface_height) {
286 switch (status->protocol) { 284 switch (status->protocol) {
287 case PROTOCOL_ERROR: 285 case PROTOCOL_ERROR:
288 return render_status_line_error(cairo, output, config, 286 return render_status_line_error(cairo, output, x);
289 status->text, x, surface_height);
290 case PROTOCOL_TEXT: 287 case PROTOCOL_TEXT:
291 return render_status_line_text(cairo, output, config, 288 return render_status_line_text(cairo, output, x);
292 status->text, focused, x, surface_height);
293 case PROTOCOL_I3BAR: 289 case PROTOCOL_I3BAR:
294 return render_status_line_i3bar(cairo, config, output, 290 return render_status_line_i3bar(cairo, output, x);
295 status, focused, x, surface_height);
296 case PROTOCOL_UNDEF: 291 case PROTOCOL_UNDEF:
297 return 0; 292 return 0;
298 } 293 }
@@ -300,10 +295,9 @@ static uint32_t render_status_line(cairo_t *cairo,
300} 295}
301 296
302static uint32_t render_binding_mode_indicator(cairo_t *cairo, 297static uint32_t render_binding_mode_indicator(cairo_t *cairo,
303 struct swaybar_output *output, struct swaybar_config *config, 298 struct swaybar_output *output, double x) {
304 const char *mode, double x, uint32_t surface_height) { 299 struct swaybar_config *config = output->bar->config;
305 uint32_t height = surface_height * output->scale; 300 const char *mode = config->mode;
306
307 int text_width, text_height; 301 int text_width, text_height;
308 get_text_size(cairo, config->font, &text_width, &text_height, NULL, 302 get_text_size(cairo, config->font, &text_width, &text_height, NULL,
309 output->scale, config->mode_pango_markup, 303 output->scale, config->mode_pango_markup,
@@ -316,11 +310,12 @@ static uint32_t render_binding_mode_indicator(cairo_t *cairo,
316 uint32_t ideal_height = text_height + ws_vertical_padding * 2 310 uint32_t ideal_height = text_height + ws_vertical_padding * 2
317 + border_width * 2; 311 + border_width * 2;
318 uint32_t ideal_surface_height = ideal_height / output->scale; 312 uint32_t ideal_surface_height = ideal_height / output->scale;
319 if (surface_height < ideal_surface_height) { 313 if (output->height < ideal_surface_height) {
320 return ideal_surface_height; 314 return ideal_surface_height;
321 } 315 }
322 uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2; 316 uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
323 317
318 uint32_t height = output->height * output->scale;
324 cairo_set_source_u32(cairo, config->colors.binding_mode.background); 319 cairo_set_source_u32(cairo, config->colors.binding_mode.background);
325 cairo_rectangle(cairo, x, 0, width, height); 320 cairo_rectangle(cairo, x, 0, width, height);
326 cairo_fill(cairo); 321 cairo_fill(cairo);
@@ -340,7 +335,7 @@ static uint32_t render_binding_mode_indicator(cairo_t *cairo,
340 cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y)); 335 cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y));
341 pango_printf(cairo, config->font, output->scale, config->mode_pango_markup, 336 pango_printf(cairo, config->font, output->scale, config->mode_pango_markup,
342 "%s", mode); 337 "%s", mode);
343 return surface_height; 338 return output->height;
344} 339}
345 340
346static const char *strip_workspace_number(const char *ws_name) { 341static const char *strip_workspace_number(const char *ws_name) {
@@ -366,8 +361,9 @@ static enum hotspot_event_handling workspace_hotspot_callback(struct swaybar_out
366} 361}
367 362
368static uint32_t render_workspace_button(cairo_t *cairo, 363static uint32_t render_workspace_button(cairo_t *cairo,
369 struct swaybar_output *output, struct swaybar_config *config, 364 struct swaybar_output *output,
370 struct swaybar_workspace *ws, double *x, uint32_t surface_height) { 365 struct swaybar_workspace *ws, double *x) {
366 struct swaybar_config *config = output->bar->config;
371 const char *name = ws->name; 367 const char *name = ws->name;
372 if (config->strip_workspace_numbers) { 368 if (config->strip_workspace_numbers) {
373 name = strip_workspace_number(ws->name); 369 name = strip_workspace_number(ws->name);
@@ -384,7 +380,7 @@ static uint32_t render_workspace_button(cairo_t *cairo,
384 box_colors = config->colors.inactive_workspace; 380 box_colors = config->colors.inactive_workspace;
385 } 381 }
386 382
387 uint32_t height = surface_height * output->scale; 383 uint32_t height = output->height * output->scale;
388 384
389 int text_width, text_height; 385 int text_width, text_height;
390 get_text_size(cairo, config->font, &text_width, &text_height, NULL, 386 get_text_size(cairo, config->font, &text_width, &text_height, NULL,
@@ -397,7 +393,7 @@ static uint32_t render_workspace_button(cairo_t *cairo,
397 uint32_t ideal_height = ws_vertical_padding * 2 + text_height 393 uint32_t ideal_height = ws_vertical_padding * 2 + text_height
398 + border_width * 2; 394 + border_width * 2;
399 uint32_t ideal_surface_height = ideal_height / output->scale; 395 uint32_t ideal_surface_height = ideal_height / output->scale;
400 if (surface_height < ideal_surface_height) { 396 if (output->height < ideal_surface_height) {
401 return ideal_surface_height; 397 return ideal_surface_height;
402 } 398 }
403 399
@@ -434,11 +430,11 @@ static uint32_t render_workspace_button(cairo_t *cairo,
434 wl_list_insert(&output->hotspots, &hotspot->link); 430 wl_list_insert(&output->hotspots, &hotspot->link);
435 431
436 *x += width; 432 *x += width;
437 return surface_height; 433 return output->height;
438} 434}
439 435
440static uint32_t render_to_cairo(cairo_t *cairo, 436static uint32_t render_to_cairo(cairo_t *cairo, struct swaybar_output *output) {
441 struct swaybar *bar, struct swaybar_output *output) { 437 struct swaybar *bar = output->bar;
442 struct swaybar_config *config = bar->config; 438 struct swaybar_config *config = bar->config;
443 cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); 439 cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
444 if (output->focused) { 440 if (output->focused) {
@@ -458,22 +454,19 @@ static uint32_t render_to_cairo(cairo_t *cairo,
458 */ 454 */
459 double x = output->width * output->scale; 455 double x = output->width * output->scale;
460 if (bar->status) { 456 if (bar->status) {
461 uint32_t h = render_status_line(cairo, config, output, 457 uint32_t h = render_status_line(cairo, output, &x);
462 bar->status, output->focused, &x, output->height);
463 max_height = h > max_height ? h : max_height; 458 max_height = h > max_height ? h : max_height;
464 } 459 }
465 x = 0; 460 x = 0;
466 if (config->workspace_buttons) { 461 if (config->workspace_buttons) {
467 struct swaybar_workspace *ws; 462 struct swaybar_workspace *ws;
468 wl_list_for_each_reverse(ws, &output->workspaces, link) { 463 wl_list_for_each_reverse(ws, &output->workspaces, link) {
469 uint32_t h = render_workspace_button(cairo, 464 uint32_t h = render_workspace_button(cairo, output, ws, &x);
470 output, config, ws, &x, output->height);
471 max_height = h > max_height ? h : max_height; 465 max_height = h > max_height ? h : max_height;
472 } 466 }
473 } 467 }
474 if (config->binding_mode_indicator && config->mode) { 468 if (config->binding_mode_indicator && config->mode) {
475 uint32_t h = render_binding_mode_indicator(cairo, 469 uint32_t h = render_binding_mode_indicator(cairo, output, x);
476 output, config, config->mode, x, output->height);
477 max_height = h > max_height ? h : max_height; 470 max_height = h > max_height ? h : max_height;
478 } 471 }
479 472
@@ -506,9 +499,10 @@ void render_frame(struct swaybar *bar, struct swaybar_output *output) {
506 cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); 499 cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
507 cairo_paint(cairo); 500 cairo_paint(cairo);
508 cairo_restore(cairo); 501 cairo_restore(cairo);
509 uint32_t height = render_to_cairo(cairo, bar, output); 502 uint32_t height = render_to_cairo(cairo, output);
510 if (bar->config->height >= 0 && height < (uint32_t)bar->config->height) { 503 int config_height = output->bar->config->height;
511 height = bar->config->height; 504 if (config_height >= 0 && height < (uint32_t)config_height) {
505 height = config_height;
512 } 506 }
513 if (height != output->height) { 507 if (height != output->height) {
514 // Reconfigure surface 508 // Reconfigure surface
@@ -519,7 +513,7 @@ void render_frame(struct swaybar *bar, struct swaybar_output *output) {
519 wl_surface_commit(output->surface); 513 wl_surface_commit(output->surface);
520 } else if (height > 0) { 514 } else if (height > 0) {
521 // Replay recording into shm and send it off 515 // Replay recording into shm and send it off
522 output->current_buffer = get_next_buffer(bar->shm, 516 output->current_buffer = get_next_buffer(output->bar->shm,
523 output->buffers, 517 output->buffers,
524 output->width * output->scale, 518 output->width * output->scale,
525 output->height * output->scale); 519 output->height * output->scale);