aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/render.c
blob: 3ad6d5d712932ee8965669942791c13edc5722c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#define _POSIX_C_SOURCE 200809L
#include <limits.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <wlr/util/log.h>
#include "cairo.h"
#include "pango.h"
#include "pool-buffer.h"
#include "swaybar/bar.h"
#include "swaybar/config.h"
#include "swaybar/ipc.h"
#include "swaybar/render.h"
#include "swaybar/status_line.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"

static const int ws_horizontal_padding = 5;
static const double ws_vertical_padding = 1.5;
static const double border_width = 1;

static uint32_t render_status_line_error(cairo_t *cairo,
		struct swaybar_config *config, const char *error,
		double *x, uint32_t width, uint32_t height) {
	if (!error) {
		return 0;
	}
	cairo_set_source_u32(cairo, 0xFF0000FF);
	static const int margin = 3;
	int text_width, text_height;
	get_text_size(cairo, config->font,
			&text_width, &text_height, 1, false, "%s", error);
	uint32_t ideal_height = text_height + ws_vertical_padding * 2;
	if (height < ideal_height) {
		return ideal_height;
	}
	*x -= text_width + margin;
	double text_y = height / 2.0 - text_height / 2.0;
	cairo_move_to(cairo, *x, (int)floor(text_y));
	pango_printf(cairo, config->font, 1, false, "%s", error);
	*x -= margin;
	return ideal_height;
}

static uint32_t render_status_line_text(cairo_t *cairo,
		struct swaybar_config *config, const char *text,
		bool focused, double *x, uint32_t width, uint32_t height) {
	if (!text) {
		return 0;
	}
	cairo_set_source_u32(cairo, focused ?
			config->colors.focused_statusline : config->colors.statusline);
	static const int margin = 3;
	int text_width, text_height;
	get_text_size(cairo, config->font, &text_width, &text_height,
			1, config->pango_markup, "%s", text);
	uint32_t ideal_height = text_height + ws_vertical_padding * 2;
	if (height < ideal_height) {
		return ideal_height;
	}
	*x -= text_width + margin;
	double text_y = height / 2.0 - text_height / 2.0;
	cairo_move_to(cairo, *x, (int)floor(text_y));
	pango_printf(cairo, config->font, 1, config->pango_markup, "%s", text);
	*x -= margin;
	return ideal_height;
}

static void render_sharp_line(cairo_t *cairo, uint32_t color,
		double x, double y, double width, double height) {
	cairo_set_source_u32(cairo, color);
	if (width > 1 && height > 1) {
		cairo_rectangle(cairo, x, y, width, height);
		cairo_fill(cairo);
	} else {
		if (width == 1) {
			x += 0.5;
			height += y;
			width = x;
		}
		if (height == 1) {
			y += 0.5;
			width += x;
			height = y;
		}
		cairo_move_to(cairo, x, y);
		cairo_set_line_width(cairo, 1.0);
		cairo_line_to(cairo, width, height);
		cairo_stroke(cairo);
	}
}

static uint32_t render_status_block(cairo_t *cairo,
		struct swaybar_config *config, struct i3bar_block *block,
		double *x, uint32_t height, bool focused, bool edge) {
	static const int margin = 3;
	if (!block->full_text || !*block->full_text) {
		return 0;
	}

	int text_width, text_height;
	get_text_size(cairo, config->font, &text_width, &text_height,
			1, block->markup, "%s", block->full_text);
	int width = text_width;
	if (width < block->min_width) {
		width = block->min_width;
	}
	double block_width = width;
	uint32_t ideal_height = text_height + ws_vertical_padding * 2;
	if (height < ideal_height) {
		return ideal_height;
	}

	*x -= width;
	if (block->border && block->border_left > 0) {
		*x -= (block->border_left + margin);
		block_width += block->border_left + margin;
	}
	if (block->border && block->border_right > 0) {
		*x -= (block->border_right + margin);
		block_width += block->border_right + margin;
	}

	int sep_width;
	if (!edge) {
		if (config->sep_symbol) {
			int _height;
			get_text_size(cairo, config->font, &sep_width, &_height,
					1, false, "%s", config->sep_symbol);
			uint32_t _ideal_height = _height + ws_vertical_padding * 2;
			if (height < _ideal_height) {
				return _height;
			}
			if (sep_width > block->separator_block_width) {
				block->separator_block_width = sep_width + margin * 2;
			}
		}
		*x -= block->separator_block_width;
	} else {
		*x -= margin;
	}

	// TODO: Create hotspot here

	double pos = *x;
	if (block->background) {
		cairo_set_source_u32(cairo, block->background);
		cairo_rectangle(cairo, pos - 0.5, 1, block_width, height);
		cairo_fill(cairo);
	}

	if (block->border && block->border_top > 0) {
		render_sharp_line(cairo, block->border,
				pos - 0.5, 1, block_width, block->border_top);
	}
	if (block->border && block->border_bottom > 0) {
		render_sharp_line(cairo, block->border,
				pos - 0.5, height - 1 - block->border_bottom,
				block_width, block->border_bottom);
	}
	if (block->border != 0 && block->border_left > 0) {
		render_sharp_line(cairo, block->border,
				pos - 0.5, 1, block->border_left, height);
		pos += block->border_left + margin;
	}

	double offset = 0;
	if (strncmp(block->align, "left", 5) == 0) {
		offset = pos;
	} else if (strncmp(block->align, "right", 5) == 0) {
		offset = pos + width - text_width;
	} else if (strncmp(block->align, "center", 6) == 0) {
		offset = pos + (width - text_width) / 2;
	}
	cairo_move_to(cairo, offset, height / 2.0 - text_height / 2.0);
	uint32_t color = block->color ?  *block->color : config->colors.statusline;
	cairo_set_source_u32(cairo, color);
	pango_printf(cairo, config->font, 1, block->markup, "%s", block->full_text);
	pos += width;

	if (block->border && block->border_right > 0) {
		pos += margin;
		render_sharp_line(cairo, block->border,
				pos - 0.5, 1, block->border_right, height);
		pos += block->border_right;
	}

	if (!edge && block->separator) {
		if (focused) {
			cairo_set_source_u32(cairo, config->colors.focused_separator);
		} else {
			cairo_set_source_u32(cairo, config->colors.separator);
		}
		if (config->sep_symbol) {
			offset = pos + (block->separator_block_width - sep_width) / 2;
			cairo_move_to(cairo, offset, margin);
			pango_printf(cairo, config->font, 1, false,
					"%s", config->sep_symbol);
		} else {
			cairo_set_line_width(cairo, 1);
			cairo_move_to(cairo,
					pos + block->separator_block_width / 2, margin);
			cairo_line_to(cairo,
					pos + block->separator_block_width / 2, height - margin);
			cairo_stroke(cairo);
		}
	}
	return ideal_height;
}

static uint32_t render_status_line_i3bar(cairo_t *cairo,
		struct swaybar_config *config, struct status_line *status,
		bool focused, double *x, uint32_t width, uint32_t height) {
	struct i3bar_block *block;
	uint32_t max_height = 0;
	bool edge = true;
	wl_list_for_each_reverse(block, &status->blocks, link) {
		uint32_t h = render_status_block(cairo, config,
				block, x, height, focused, edge);
		max_height = h > max_height ? h : max_height;
		edge = false;
	}
	return max_height;
}

static uint32_t render_status_line(cairo_t *cairo,
		struct swaybar_config *config, struct status_line *status,
		bool focused, double *x, uint32_t width, uint32_t height) {
	switch (status->protocol) {
	case PROTOCOL_ERROR:
		return render_status_line_error(cairo,
				config, status->text, x, width, height);
	case PROTOCOL_TEXT:
		return render_status_line_text(cairo,
				config, status->text, focused, x, width, height);
	case PROTOCOL_I3BAR:
		return render_status_line_i3bar(cairo,
				config, status, focused, x, width, height);
	case PROTOCOL_UNDEF:
		return 0;
	}
	return 0;
}

static uint32_t render_binding_mode_indicator(cairo_t *cairo,
		struct swaybar_config *config, const char *mode, double x,
		uint32_t height) {
	int text_width, text_height;
	get_text_size(cairo, config->font, &text_width, &text_height,
			1, true, "%s", mode);
	uint32_t ideal_height = text_height + ws_vertical_padding * 2
		+ border_width * 2;
	if (height < ideal_height) {
		return ideal_height;
	}
	uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;

	cairo_set_source_u32(cairo, config->colors.binding_mode.background);
	cairo_rectangle(cairo, x, 0, width, height);
	cairo_fill(cairo);

	cairo_set_source_u32(cairo, config->colors.binding_mode.border);
	cairo_rectangle(cairo, x, 0, width, border_width);
	cairo_fill(cairo);
	cairo_rectangle(cairo, x, 0, border_width, height);
	cairo_fill(cairo);
	cairo_rectangle(cairo, x + width - border_width, 0, border_width, height);
	cairo_fill(cairo);
	cairo_rectangle(cairo, x, height - border_width, width, border_width);
	cairo_fill(cairo);

	double text_y = height / 2.0 - text_height / 2.0;
	cairo_set_source_u32(cairo, config->colors.binding_mode.text);
	cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y));
	pango_printf(cairo, config->font, 1, true, "%s", mode);
	return ideal_height;
}

static const char *strip_workspace_number(const char *ws_name) {
	size_t len = strlen(ws_name);
	for (size_t i = 0; i < len; ++i) {
		if (ws_name[i] < '0' || ws_name[i] > '9') {
			if (':' == ws_name[i] && i < len - 1 && i > 0) {
				return ws_name + i + 1;
			}
			return ws_name;
		}
	}
	return ws_name;
}

static void workspace_hotspot_callback(struct swaybar_output *output,
			int x, int y, uint32_t button, void *data) {
	ipc_send_workspace_command(output->bar, (const char *)data);
}

static uint32_t render_workspace_button(cairo_t *cairo,
		struct swaybar_output *output, struct swaybar_config *config,
		struct swaybar_workspace *ws, double *x, uint32_t height) {
	const char *name = ws->name;
	if (config->strip_workspace_numbers) {
		name = strip_workspace_number(ws->name);
	}

	struct box_colors box_colors;
	if (ws->urgent) {
		box_colors = config->colors.urgent_workspace;
	} else if (ws->focused) {
		box_colors = config->colors.focused_workspace;
	} else if (ws->visible) {
		box_colors = config->colors.active_workspace;
	} else {
		box_colors = config->colors.inactive_workspace;
	}

	int text_width, text_height;
	get_text_size(cairo, config->font, &text_width, &text_height,
			1, true, "%s", name);
	uint32_t ideal_height = ws_vertical_padding * 2 + text_height
		+ border_width * 2;
	if (height < ideal_height) {
		return ideal_height;
	}
	uint32_t width = ws_horizontal_padding * 2 + text_width + border_width * 2;

	cairo_set_source_u32(cairo, box_colors.background);
	cairo_rectangle(cairo, *x, 0, width, height);
	cairo_fill(cairo);

	cairo_set_source_u32(cairo, box_colors.border);
	cairo_rectangle(cairo, *x, 0, width, border_width);
	cairo_fill(cairo);
	cairo_rectangle(cairo, *x, 0, border_width, height);
	cairo_fill(cairo);
	cairo_rectangle(cairo, *x + width - border_width, 0, border_width, height);
	cairo_fill(cairo);
	cairo_rectangle(cairo, *x, height - border_width, width, border_width);
	cairo_fill(cairo);

	double text_y = height / 2.0 - text_height / 2.0;
	cairo_set_source_u32(cairo, box_colors.text);
	cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y));
	pango_printf(cairo, config->font, 1, true, "%s", name);

	struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot));
	hotspot->x = *x;
	hotspot->y = 0;
	hotspot->height = height;
	hotspot->width = width;
	hotspot->callback = workspace_hotspot_callback;
	hotspot->destroy = free;
	hotspot->data = strdup(name);
	wl_list_insert(&output->hotspots, &hotspot->link);

	*x += width;
	return height;
}

static uint32_t render_to_cairo(cairo_t *cairo,
		struct swaybar *bar, struct swaybar_output *output) {
	struct swaybar_config *config = bar->config;

	cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
	if (output->focused) {
		cairo_set_source_u32(cairo, config->colors.focused_background);
	} else {
		cairo_set_source_u32(cairo, config->colors.background);
	}
	cairo_paint(cairo);

	uint32_t max_height = 0;
	/*
	 * Each render_* function takes the actual height of the bar, and returns
	 * the ideal height. If the actual height is too short, the render function
	 * can do whatever it wants - the buffer won't be committed. If the actual
	 * height is too tall, the render function should adapt its drawing to
	 * utilize the available space.
	 */
	double x = 0;
	if (config->workspace_buttons) {
		struct swaybar_workspace *ws;
		wl_list_for_each_reverse(ws, &output->workspaces, link) {
			uint32_t h = render_workspace_button(cairo,
					output, config, ws, &x, output->height);
			max_height = h > max_height ? h : max_height;
		}
	}
	if (config->binding_mode_indicator && config->mode) {
		uint32_t h = render_binding_mode_indicator(
				cairo, config, config->mode, x, output->height);
		max_height = h > max_height ? h : max_height;
	}
	x = output->width;
	if (bar->status) {
		uint32_t h = render_status_line(cairo, config, bar->status,
				output->focused, &x, output->width, output->height);
		max_height = h > max_height ? h : max_height;
	}

	return max_height > output->height ? max_height : output->height;
}

void render_frame(struct swaybar *bar, struct swaybar_output *output) {
	struct swaybar_hotspot *hotspot, *tmp;
	wl_list_for_each_safe(hotspot, tmp, &output->hotspots, link) {
		if (hotspot->destroy) {
			hotspot->destroy(hotspot->data);
		}
		wl_list_remove(&hotspot->link);
		free(hotspot);
	}

	cairo_surface_t *recorder = cairo_recording_surface_create(
			CAIRO_CONTENT_COLOR_ALPHA, NULL);
	cairo_t *cairo = cairo_create(recorder);
	cairo_save(cairo);
	cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
	cairo_paint(cairo);
	cairo_restore(cairo);
	uint32_t height = render_to_cairo(cairo, bar, output);
	if (bar->config->height >= 0 && height < (uint32_t)bar->config->height) {
		height = bar->config->height;
	}
	if (height != output->height) {
		// Reconfigure surface
		zwlr_layer_surface_v1_set_size(
				output->layer_surface, 0, height);
		zwlr_layer_surface_v1_set_exclusive_zone(output->layer_surface, height);
		// TODO: this could infinite loop if the compositor assigns us a
		// different height than what we asked for
		wl_surface_commit(output->surface);
		wl_display_roundtrip(bar->display);
	} else {
		// Replay recording into shm and send it off
		output->current_buffer = get_next_buffer(bar->shm,
				output->buffers, output->width, output->height);
		cairo_t *shm = output->current_buffer->cairo;

		cairo_save(shm);
		cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR);
		cairo_paint(shm);
		cairo_restore(shm);

		cairo_set_source_surface(shm, recorder, 0.0, 0.0);
		cairo_paint(shm);

		wl_surface_attach(output->surface,
				output->current_buffer->buffer, 0, 0);
		wl_surface_damage(output->surface, 0, 0, output->width, output->height);
		wl_surface_commit(output->surface);
		wl_display_roundtrip(bar->display);
	}
	cairo_surface_destroy(recorder);
	cairo_destroy(cairo);
}