forked from emersion/mako
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
456 lines (392 loc) · 14.5 KB
/
render.c
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
455
456
#define _XOPEN_SOURCE 700
#include <stdlib.h>
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include <math.h>
#include "config.h"
#include "criteria.h"
#include "mako.h"
#include "notification.h"
#include "render.h"
#include "wayland.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "icon.h"
// HiDPI conventions: local variables are in surface-local coordinates, unless
// they have a "buffer_" prefix, in which case they are in buffer-local
// coordinates.
static void set_source_u32(cairo_t *cairo, uint32_t color) {
cairo_set_source_rgba(cairo,
(color >> (3*8) & 0xFF) / 255.0,
(color >> (2*8) & 0xFF) / 255.0,
(color >> (1*8) & 0xFF) / 255.0,
(color >> (0*8) & 0xFF) / 255.0);
}
static void set_layout_size(PangoLayout *layout, int width, int height,
int scale) {
pango_layout_set_width(layout, width * scale * PANGO_SCALE);
pango_layout_set_height(layout, height * scale * PANGO_SCALE);
}
static void move_to(cairo_t *cairo, double x, double y, int scale) {
cairo_move_to(cairo, x * scale, y * scale);
}
static void set_rounded_rectangle(cairo_t *cairo, double x, double y, double width, double height,
int scale, int radius) {
if (width == 0 || height == 0) {
return;
}
x *= scale;
y *= scale;
width *= scale;
height *= scale;
radius *= scale;
double degrees = M_PI / 180.0;
cairo_new_sub_path(cairo);
cairo_arc(cairo, x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
cairo_arc(cairo, x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
cairo_arc(cairo, x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
cairo_arc(cairo, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
cairo_close_path(cairo);
}
static cairo_subpixel_order_t get_cairo_subpixel_order(
enum wl_output_subpixel subpixel) {
switch (subpixel) {
case WL_OUTPUT_SUBPIXEL_UNKNOWN:
case WL_OUTPUT_SUBPIXEL_NONE:
return CAIRO_SUBPIXEL_ORDER_DEFAULT;
case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
return CAIRO_SUBPIXEL_ORDER_RGB;
case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
return CAIRO_SUBPIXEL_ORDER_BGR;
case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
return CAIRO_SUBPIXEL_ORDER_VRGB;
case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
return CAIRO_SUBPIXEL_ORDER_VBGR;
}
abort();
}
static void set_font_options(cairo_t *cairo, struct mako_surface *surface) {
if (surface->surface_output == NULL) {
return;
}
cairo_font_options_t *fo = cairo_font_options_create();
if (surface->surface_output->subpixel == WL_OUTPUT_SUBPIXEL_NONE) {
cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_GRAY);
} else {
cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL);
cairo_font_options_set_subpixel_order(fo,
get_cairo_subpixel_order(surface->surface_output->subpixel));
}
cairo_set_font_options(cairo, fo);
cairo_font_options_destroy(fo);
}
static int render_notification(cairo_t *cairo, struct mako_state *state, struct mako_surface *surface,
struct mako_style *style, const char *text, struct mako_icon *icon, int offset_y, int scale,
struct mako_hotspot *hotspot, int progress) {
int border_size = 2 * style->border_size;
int padding_height = style->padding.top + style->padding.bottom;
int padding_width = style->padding.left + style->padding.right;
int radius = style->border_radius;
bool icon_vertical = style->icon_location == MAKO_ICON_LOCATION_TOP ||
style->icon_location == MAKO_ICON_LOCATION_BOTTOM;
// If the compositor has forced us to shrink down, do so.
int notif_width =
(style->width <= surface->width) ? style->width : surface->width;
// offset_x is for the entire draw operation inside the surface
int offset_x;
if (surface->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) {
offset_x = surface->width - notif_width - style->margin.right;
} else if (surface->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) {
offset_x = style->margin.left;
} else { // CENTER has nothing to & with, so it's the else case
offset_x = (surface->width - notif_width) / 2;
}
// text_x is the offset of the text inside our draw operation
double text_x = style->padding.left;
if (icon != NULL && style->icon_location == MAKO_ICON_LOCATION_LEFT) {
text_x = icon->width + 2*style->padding.left;
}
// text_y is the offset of the text inside our draw operation
double text_y = style->padding.top;
if (icon != NULL && style->icon_location == MAKO_ICON_LOCATION_TOP) {
text_y = icon->height + 2*style->padding.top;
}
double text_layout_width = notif_width - border_size - padding_width;
if (icon && ! icon_vertical) {
text_layout_width -= icon->width;
text_layout_width -= style->icon_location == MAKO_ICON_LOCATION_LEFT ?
(style->padding.left * 2) : (style->padding.right * 2);
}
double text_layout_height = style->height - border_size - padding_height;
if (icon && icon_vertical) {
text_layout_height -= icon->height;
text_layout_height -= style->icon_location == MAKO_ICON_LOCATION_TOP ?
(style->padding.top * 2) : (style->padding.bottom * 2);
}
set_font_options(cairo, surface);
PangoLayout *layout = pango_cairo_create_layout(cairo);
set_layout_size(layout, text_layout_width, text_layout_height, scale);
pango_layout_set_alignment(layout, style->text_alignment);
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
PangoFontDescription *desc =
pango_font_description_from_string(style->font);
pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc);
PangoAttrList *attrs = NULL;
GError *error = NULL;
char *buf = NULL;
if (pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, &error)) {
pango_layout_set_text(layout, buf, -1);
free(buf);
} else {
fprintf(stderr, "cannot parse pango markup: %s\n", error->message);
g_error_free(error);
// fallback to plain text
pango_layout_set_text(layout, text, -1);
}
if (attrs == NULL) {
attrs = pango_attr_list_new();
}
pango_attr_list_insert(attrs, pango_attr_scale_new(scale));
pango_layout_set_attributes(layout, attrs);
pango_attr_list_unref(attrs);
int buffer_text_height = 0;
int buffer_text_width = 0;
// If there's no text to be rendered, the notification can shrink down
// smaller than the line height.
if (pango_layout_get_character_count(layout) > 0) {
pango_layout_get_pixel_size(layout, &buffer_text_width, &buffer_text_height);
}
int text_height = buffer_text_height / scale;
int text_width = buffer_text_width / scale;
if (text_height > text_layout_height) {
text_height = text_layout_height;
}
int notif_height = text_height + border_size + padding_height;
if (icon && icon_vertical) {
notif_height += icon->height;
notif_height += style->icon_location == MAKO_ICON_LOCATION_TOP ?
style->padding.top : style->padding.bottom;
}
if (icon != NULL && ! icon_vertical && icon->height > text_height) {
notif_height = icon->height + border_size + padding_height;
}
if (notif_height < radius * 2) {
notif_height = radius * 2 + border_size;
}
int notif_background_width = notif_width - style->border_size;
// Define the shape of the notification. The stroke is drawn centered on
// the edge of the fill, so we need to inset the shape by half the
// border_size.
set_rounded_rectangle(cairo,
offset_x + style->border_size / 2.0,
offset_y + style->border_size / 2.0,
notif_background_width,
notif_height - style->border_size,
scale, radius);
// Render background, keeping the path.
set_source_u32(cairo, style->colors.background);
cairo_fill_preserve(cairo);
// Keep a copy of the path. We need it later to draw the border on top, but
// we have to create a new one for progress in the meantime.
cairo_path_t *border_path = cairo_copy_path(cairo);
// Render progress. We need to render this as a normal rectangle, but clip
// it to the rounded rectangle we drew for the background. We also inset it
// a bit further so that 0 and 100 percent are aligned to the inside edge
// of the border and we can actually see the whole range.
int progress_width =
(notif_background_width - style->border_size) * progress / 100;
if (progress_width < 0) {
progress_width = 0;
} else if (progress_width > notif_background_width) {
progress_width = notif_background_width - style->border_size;
}
cairo_save(cairo);
cairo_clip(cairo);
cairo_set_operator(cairo, style->colors.progress.operator);
set_source_u32(cairo, style->colors.progress.value);
set_rounded_rectangle(cairo,
offset_x + style->border_size,
offset_y + style->border_size,
progress_width,
notif_height - style->border_size,
scale, 0);
cairo_fill(cairo);
cairo_restore(cairo);
// Render border, using the SOURCE operator to clip away the background
// and progress beneath. This is the only way to make the background appear
// to line up with the inside of a rounded border, while not revealing any
// of the background when using a translucent border color.
cairo_save(cairo);
cairo_append_path(cairo, border_path);
set_source_u32(cairo, style->colors.border);
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_set_line_width(cairo, style->border_size * scale);
cairo_stroke(cairo);
cairo_restore(cairo);
cairo_path_destroy(border_path);
if (icon != NULL) {
// Render icon
double xpos = -1;
double ypos = -1;
double ypos_center = offset_y + style->border_size +
(notif_height - icon->height - border_size) / 2;
double xpos_center = offset_x + style->border_size +
(notif_width - icon->width - border_size) / 2;
switch (style->icon_location) {
case MAKO_ICON_LOCATION_LEFT:
xpos = offset_x + style->border_size +
style->padding.left;
ypos = ypos_center;
break;
case MAKO_ICON_LOCATION_RIGHT:
xpos = offset_x + notif_width - style->border_size -
style->padding.right - icon->width;
ypos = ypos_center;
break;
case MAKO_ICON_LOCATION_TOP:
xpos = xpos_center;
ypos = offset_y + style->border_size +
style->padding.top;
break;
case MAKO_ICON_LOCATION_BOTTOM:
xpos = xpos_center;
ypos = offset_y + notif_height - style->border_size -
style->padding.bottom - icon->height;
break;
}
draw_icon(cairo, icon, xpos, ypos, scale);
}
if (icon_vertical) {
text_x = (notif_width - text_width - border_size) / 2;
} else {
text_y = (notif_height - text_height - border_size) / 2;
}
// Render text
set_source_u32(cairo, style->colors.text);
move_to(cairo,
offset_x + style->border_size + text_x,
offset_y + style->border_size + text_y,
scale);
pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout);
// Update hotspot with calculated location
if (hotspot != NULL) {
hotspot->x = offset_x;
hotspot->y = offset_y;
hotspot->width = notif_width;
hotspot->height = notif_height;
}
g_object_unref(layout);
return notif_height;
}
int render(struct mako_surface *surface, struct pool_buffer *buffer, int scale) {
struct mako_state *state = surface->state;
cairo_t *cairo = buffer->cairo;
if (wl_list_empty(&state->notifications)) {
return 0;
}
// Clear
cairo_save(cairo);
cairo_set_source_rgba(cairo, 0, 0, 0, 0);
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_paint(cairo);
cairo_restore(cairo);
size_t visible_count = 0;
size_t hidden_count = 0;
int total_height = 0;
int pending_bottom_margin = 0;
struct mako_notification *notif;
size_t total_notifications = 0;
wl_list_for_each(notif, &state->notifications, link) {
if (notif->surface != surface) {
continue;
}
++total_notifications;
// Immediately before rendering we need to re-match all of the criteria
// so that matches against the anchor and output work even if the
// output was automatically assigned by the compositor.
int rematch_count = apply_each_criteria(&state->config.criteria, notif);
if (rematch_count == -1) {
// We encountered an allocation failure or similar while applying
// criteria. The notification may be partially matched, but the
// worst case is that it has an empty style, so bail.
fprintf(stderr, "Failed to apply criteria\n");
break;
} else if (rematch_count == 0) {
// This should be impossible, since the global criteria is always
// present in a mako_config and matches everything.
fprintf(stderr, "Notification matched zero criteria?!\n");
break;
}
// Note that by this point, everything in the style is guaranteed to
// be specified, so we don't need to check.
struct mako_style *style = ¬if->style;
if (style->max_visible >= 0 &&
visible_count >= (size_t)style->max_visible) {
++hidden_count;
continue;
}
if (style->invisible) {
continue;
}
size_t text_len =
format_text(style->format, NULL, format_notif_text, notif);
char *text = malloc(text_len + 1);
if (text == NULL) {
fprintf(stderr, "Unable to allocate memory to render notification\n");
break;
}
format_text(style->format, text, format_notif_text, notif);
if (style->margin.top > pending_bottom_margin) {
total_height += style->margin.top;
} else {
total_height += pending_bottom_margin;
}
struct mako_icon *icon = (style->icons) ? notif->icon : NULL;
int notif_height = render_notification(
cairo, state, surface, style, text, icon, total_height, scale,
¬if->hotspot, notif->progress);
free(text);
total_height += notif_height;
pending_bottom_margin = style->margin.bottom;
if (notif->group_index < 1) {
// If the notification is ungrouped, or is the first in a group, it
// counts against max_visible. Even if other notifications in the
// group are rendered based on criteria, a group is considered a
// single entity for this purpose.
++visible_count;
}
}
if (hidden_count > 0) {
struct mako_notification *hidden_notif = create_notification(state);
hidden_notif->surface = surface;
hidden_notif->hidden = true;
apply_each_criteria(&state->config.criteria, hidden_notif);
struct mako_style *style = &hidden_notif->style;
if (style->margin.top > pending_bottom_margin) {
total_height += style->margin.top;
} else {
total_height += pending_bottom_margin;
}
struct mako_hidden_format_data data = {
.hidden = hidden_count,
.count = total_notifications,
};
size_t text_ln =
format_text(style->format, NULL, format_hidden_text, &data);
char *text = malloc(text_ln + 1);
if (text == NULL) {
fprintf(stderr, "allocation failed");
return 0;
}
format_text(style->format, text, format_hidden_text, &data);
int hidden_height = render_notification(
cairo, state, surface, style, text, NULL, total_height, scale, NULL, 0);
free(text);
destroy_notification(hidden_notif);
total_height += hidden_height;
pending_bottom_margin = style->margin.bottom;
}
return total_height + pending_bottom_margin;
}