forked from dunst-project/dunst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.c
More file actions
649 lines (529 loc) · 20.9 KB
/
draw.c
File metadata and controls
649 lines (529 loc) · 20.9 KB
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include "draw.h"
#include <assert.h>
#include <cairo.h>
#include <math.h>
#include <pango/pango-attributes.h>
#include <pango/pango-font.h>
#include <pango/pango-layout.h>
#include <pango/pango-types.h>
#include <pango/pangocairo.h>
#include <stdlib.h>
#include "dunst.h"
#include "icon.h"
#include "markup.h"
#include "notification.h"
#include "log.h"
#include "queues.h"
#include "x11/x.h"
typedef struct {
PangoLayout *l;
color_t fg;
color_t bg;
color_t frame;
char *text;
PangoAttrList *attr;
cairo_surface_t *icon;
notification *n;
} colored_layout;
window_x11 *win;
PangoFontDescription *pango_fdesc;
void draw_setup(void)
{
x_setup();
win = x_win_create();
pango_fdesc = pango_font_description_from_string(settings.font);
}
static color_t color_hex_to_double(int hexValue)
{
color_t color;
color.r = ((hexValue >> 16) & 0xFF) / 255.0;
color.g = ((hexValue >> 8) & 0xFF) / 255.0;
color.b = ((hexValue) & 0xFF) / 255.0;
return color;
}
static color_t string_to_color(const char *str)
{
char *end;
long int val = strtol(str+1, &end, 16);
if (*end != '\0' && *(end+1) != '\0') {
LOG_W("Invalid color string: '%s'", str);
}
return color_hex_to_double(val);
}
static double color_apply_delta(double base, double delta)
{
base += delta;
if (base > 1)
base = 1;
if (base < 0)
base = 0;
return base;
}
static color_t calculate_foreground_color(color_t bg)
{
double c_delta = 0.1;
color_t color = bg;
/* do we need to darken or brighten the colors? */
bool darken = (bg.r + bg.g + bg.b) / 3 > 0.5;
int signedness = darken ? -1 : 1;
color.r = color_apply_delta(color.r, c_delta * signedness);
color.g = color_apply_delta(color.g, c_delta * signedness);
color.b = color_apply_delta(color.b, c_delta * signedness);
return color;
}
static color_t layout_get_sepcolor(colored_layout *cl, colored_layout *cl_next)
{
switch (settings.sep_color) {
case SEP_FRAME:
if (cl_next->n->urgency > cl->n->urgency)
return cl_next->frame;
else
return cl->frame;
case SEP_CUSTOM:
return string_to_color(settings.sep_custom_color_str);
case SEP_FOREGROUND:
return cl->fg;
case SEP_AUTO:
return calculate_foreground_color(cl->bg);
default:
LOG_E("Invalid %s enum value in %s:%d", "sep_color", __FILE__, __LINE__);
break;
}
}
static void layout_setup_pango(PangoLayout *layout, int width)
{
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
pango_layout_set_width(layout, width * PANGO_SCALE);
pango_layout_set_font_description(layout, pango_fdesc);
pango_layout_set_spacing(layout, settings.line_height * PANGO_SCALE);
PangoAlignment align;
switch (settings.align) {
case left:
default:
align = PANGO_ALIGN_LEFT;
break;
case center:
align = PANGO_ALIGN_CENTER;
break;
case right:
align = PANGO_ALIGN_RIGHT;
break;
}
pango_layout_set_alignment(layout, align);
}
static void free_colored_layout(void *data)
{
colored_layout *cl = data;
g_object_unref(cl->l);
pango_attr_list_unref(cl->attr);
g_free(cl->text);
if (cl->icon) cairo_surface_destroy(cl->icon);
g_free(cl);
}
static bool have_dynamic_width(void)
{
return (settings.geometry.width_set && settings.geometry.w == 0);
}
static struct dimensions calculate_dimensions(GSList *layouts)
{
struct dimensions dim = { 0 };
screen_info *scr = get_active_screen();
if (have_dynamic_width()) {
/* dynamic width */
dim.w = 0;
} else if (settings.geometry.width_set) {
/* fixed width */
if (settings.geometry.negative_width) {
dim.w = scr->w - settings.geometry.w;
} else {
dim.w = settings.geometry.w;
}
} else {
/* across the screen */
dim.w = scr->w;
}
dim.h += 2 * settings.frame_width;
dim.h += (g_slist_length(layouts) - 1) * settings.separator_height;
dim.corner_radius = settings.corner_radius;
int text_width = 0, total_width = 0;
for (GSList *iter = layouts; iter; iter = iter->next) {
colored_layout *cl = iter->data;
int w=0,h=0;
pango_layout_get_pixel_size(cl->l, &w, &h);
if (cl->icon) {
h = MAX(cairo_image_surface_get_height(cl->icon), h);
w += cairo_image_surface_get_width(cl->icon) + settings.h_padding;
}
h = MAX(settings.notification_height, h + settings.padding * 2);
dim.h += h;
text_width = MAX(w, text_width);
if (have_dynamic_width() || settings.shrink) {
/* dynamic width */
total_width = MAX(text_width + 2 * settings.h_padding, total_width);
/* subtract height from the unwrapped text */
dim.h -= h;
if (total_width > scr->w) {
/* set width to screen width */
dim.w = scr->w - settings.geometry.x * 2;
} else if (have_dynamic_width() || (total_width < settings.geometry.w && settings.shrink)) {
/* set width to text width */
dim.w = total_width + 2 * settings.frame_width;
}
/* re-setup the layout */
w = dim.w;
w -= 2 * settings.h_padding;
w -= 2 * settings.frame_width;
if (cl->icon) w -= cairo_image_surface_get_width(cl->icon) + settings.h_padding;
layout_setup_pango(cl->l, w);
/* re-read information */
pango_layout_get_pixel_size(cl->l, &w, &h);
if (cl->icon) {
h = MAX(cairo_image_surface_get_height(cl->icon), h);
w += cairo_image_surface_get_width(cl->icon) + settings.h_padding;
}
h = MAX(settings.notification_height, h + settings.padding * 2);
dim.h += h;
text_width = MAX(w, text_width);
}
dim.corner_radius = MIN(dim.corner_radius, h/2);
}
if (dim.w <= 0) {
dim.w = text_width + 2 * settings.h_padding;
dim.w += 2 * settings.frame_width;
}
return dim;
}
static PangoLayout *layout_create(cairo_t *c)
{
screen_info *screen = get_active_screen();
PangoContext *context = pango_cairo_create_context(c);
pango_cairo_context_set_resolution(context, get_dpi_for_screen(screen));
PangoLayout *layout = pango_layout_new(context);
g_object_unref(context);
return layout;
}
static colored_layout *layout_init_shared(cairo_t *c, notification *n)
{
colored_layout *cl = g_malloc(sizeof(colored_layout));
cl->l = layout_create(c);
if (!settings.word_wrap) {
PangoEllipsizeMode ellipsize;
switch (settings.ellipsize) {
case start:
ellipsize = PANGO_ELLIPSIZE_START;
break;
case middle:
ellipsize = PANGO_ELLIPSIZE_MIDDLE;
break;
case end:
ellipsize = PANGO_ELLIPSIZE_END;
break;
default:
LOG_E("Invalid %s enum value in %s:%d", "ellipsize", __FILE__, __LINE__);
break;
}
pango_layout_set_ellipsize(cl->l, ellipsize);
}
if (settings.icon_position != icons_off) {
cl->icon = icon_get_for_notification(n);
} else {
cl->icon = NULL;
}
if (cl->icon && cairo_surface_status(cl->icon) != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy(cl->icon);
cl->icon = NULL;
}
cl->fg = string_to_color(n->colors[ColFG]);
cl->bg = string_to_color(n->colors[ColBG]);
cl->frame = string_to_color(n->colors[ColFrame]);
cl->n = n;
struct dimensions dim = calculate_dimensions(NULL);
int width = dim.w;
if (have_dynamic_width()) {
layout_setup_pango(cl->l, -1);
} else {
width -= 2 * settings.h_padding;
width -= 2 * settings.frame_width;
if (cl->icon) width -= cairo_image_surface_get_width(cl->icon) + settings.h_padding;
layout_setup_pango(cl->l, width);
}
return cl;
}
static colored_layout *layout_derive_xmore(cairo_t *c, notification *n, int qlen)
{
colored_layout *cl = layout_init_shared(c, n);
cl->text = g_strdup_printf("(%d more)", qlen);
cl->attr = NULL;
pango_layout_set_text(cl->l, cl->text, -1);
return cl;
}
static colored_layout *layout_from_notification(cairo_t *c, notification *n)
{
colored_layout *cl = layout_init_shared(c, n);
/* markup */
GError *err = NULL;
pango_parse_markup(n->text_to_render, -1, 0, &(cl->attr), &(cl->text), NULL, &err);
if (!err) {
pango_layout_set_text(cl->l, cl->text, -1);
pango_layout_set_attributes(cl->l, cl->attr);
} else {
/* remove markup and display plain message instead */
n->text_to_render = markup_strip(n->text_to_render);
cl->text = NULL;
cl->attr = NULL;
pango_layout_set_text(cl->l, n->text_to_render, -1);
if (n->first_render) {
LOG_W("Unable to parse markup: %s", err->message);
}
g_error_free(err);
}
pango_layout_get_pixel_size(cl->l, NULL, &(n->displayed_height));
if (cl->icon) n->displayed_height = MAX(cairo_image_surface_get_height(cl->icon), n->displayed_height);
n->displayed_height = MAX(settings.notification_height, n->displayed_height + settings.padding * 2);
n->first_render = false;
return cl;
}
static GSList *create_layouts(cairo_t *c)
{
GSList *layouts = NULL;
int qlen = queues_length_waiting();
bool xmore_is_needed = qlen > 0 && settings.indicate_hidden;
notification *last = NULL;
for (const GList *iter = queues_get_displayed();
iter; iter = iter->next)
{
notification *n = iter->data;
last = n;
notification_update_text_to_render(n);
if (!iter->next && xmore_is_needed && settings.geometry.h == 1) {
char *new_ttr = g_strdup_printf("%s (%d more)", n->text_to_render, qlen);
g_free(n->text_to_render);
n->text_to_render = new_ttr;
}
layouts = g_slist_append(layouts,
layout_from_notification(c, n));
}
if (xmore_is_needed && settings.geometry.h != 1) {
/* append xmore message as new message */
layouts = g_slist_append(layouts,
layout_derive_xmore(c, last, qlen));
}
return layouts;
}
static void free_layouts(GSList *layouts)
{
g_slist_free_full(layouts, free_colored_layout);
}
static int layout_get_height(colored_layout *cl)
{
int h;
int h_icon = 0;
pango_layout_get_pixel_size(cl->l, NULL, &h);
if (cl->icon)
h_icon = cairo_image_surface_get_height(cl->icon);
return MAX(h, h_icon);
}
/**
* Create a path on the given cairo context to draw the background of a notification.
* The top corners will get rounded by `corner_radius`, if `first` is set.
* Respectably the same for `last` with the bottom corners.
*/
static void draw_rounded_rect(cairo_t *c, int x, int y, int width, int height, int corner_radius, bool first, bool last)
{
const float degrees = M_PI / 180.0;
cairo_new_sub_path(c);
if (last) {
// bottom right
cairo_arc(c,
x + width - corner_radius,
y + height - corner_radius,
corner_radius,
degrees * 0,
degrees * 90);
// bottom left
cairo_arc(c,
x + corner_radius,
y + height - corner_radius,
corner_radius,
degrees * 90,
degrees * 180);
} else {
cairo_line_to(c, x + width, y + height);
cairo_line_to(c, x, y + height);
}
if (first) {
// top left
cairo_arc(c,
x + corner_radius,
y + corner_radius,
corner_radius,
degrees * 180,
degrees * 270);
// top right
cairo_arc(c,
x + width - corner_radius,
y + corner_radius,
corner_radius,
degrees * 270,
degrees * 360);
} else {
cairo_line_to(c, x, y);
cairo_line_to(c, x + width, y);
}
cairo_close_path(c);
}
static cairo_surface_t *render_background(cairo_surface_t *srf,
colored_layout *cl,
colored_layout *cl_next,
int y,
int width,
int height,
int corner_radius,
bool first,
bool last,
int *ret_width)
{
int x = 0;
cairo_t *c = cairo_create(srf);
if (first)
height += settings.frame_width;
if (last)
height += settings.frame_width;
else
height += settings.separator_height;
cairo_set_source_rgb(c, cl->frame.r, cl->frame.g, cl->frame.b);
draw_rounded_rect(c, x, y, width, height, corner_radius, first, last);
cairo_fill(c);
/* adding frame */
x += settings.frame_width;
if (first) {
y += settings.frame_width;
height -= settings.frame_width;
}
width -= 2 * settings.frame_width;
if (last)
height -= settings.frame_width;
else
height -= settings.separator_height;
cairo_set_source_rgb(c, cl->bg.r, cl->bg.g, cl->bg.b);
draw_rounded_rect(c, x, y, width, height, corner_radius, first, last);
cairo_fill(c);
if ( settings.sep_color != SEP_FRAME
&& settings.separator_height > 0
&& !last) {
color_t sep_color = layout_get_sepcolor(cl, cl_next);
cairo_set_source_rgb(c, sep_color.r, sep_color.g, sep_color.b);
cairo_rectangle(c, settings.frame_width, y + height, width, settings.separator_height);
cairo_fill(c);
}
cairo_destroy(c);
if (ret_width)
*ret_width = width;
return cairo_surface_create_for_rectangle(srf, x, y, width, height);
}
static void render_content(cairo_t *c, colored_layout *cl, int width)
{
const int h = layout_get_height(cl);
int h_text;
pango_layout_get_pixel_size(cl->l, NULL, &h_text);
if (cl->icon && settings.icon_position == icons_left) {
cairo_move_to(c, cairo_image_surface_get_width(cl->icon) + 2 * settings.h_padding,
settings.padding + h/2 - h_text/2);
} else if (cl->icon && settings.icon_position == icons_right) {
cairo_move_to(c, settings.h_padding, settings.padding + h/2 - h_text/2);
} else {
cairo_move_to(c, settings.h_padding, settings.padding);
}
cairo_set_source_rgb(c, cl->fg.r, cl->fg.g, cl->fg.b);
pango_cairo_update_layout(c, cl->l);
pango_cairo_show_layout(c, cl->l);
if (cl->icon) {
unsigned int image_width = cairo_image_surface_get_width(cl->icon),
image_height = cairo_image_surface_get_height(cl->icon),
image_x,
image_y = settings.padding + h/2 - image_height/2;
if (settings.icon_position == icons_left) {
image_x = settings.h_padding;
} else if (settings.icon_position == icons_right){
image_x = width - settings.h_padding - image_width;
} else {
LOG_E("Tried to draw icon but icon position is not valid. %s:%d", __FILE__, __LINE__);
}
cairo_set_source_surface(c, cl->icon, image_x, image_y);
cairo_rectangle(c, image_x, image_y, image_width, image_height);
cairo_fill(c);
}
}
static struct dimensions layout_render(cairo_surface_t *srf,
colored_layout *cl,
colored_layout *cl_next,
struct dimensions dim,
bool first,
bool last)
{
const int cl_h = layout_get_height(cl);
int h_text = 0;
pango_layout_get_pixel_size(cl->l, NULL, &h_text);
int bg_width = 0;
int bg_height = MAX(settings.notification_height, (2 * settings.padding) + cl_h);
cairo_surface_t *content = render_background(srf, cl, cl_next, dim.y, dim.w, bg_height, dim.corner_radius, first, last, &bg_width);
cairo_t *c = cairo_create(content);
render_content(c, cl, bg_width);
/* adding frame */
if (first)
dim.y += settings.frame_width;
if (!last)
dim.y += settings.separator_height;
if (settings.notification_height <= (2 * settings.padding) + cl_h)
dim.y += cl_h + 2 * settings.padding;
else
dim.y += settings.notification_height;
cairo_destroy(c);
cairo_surface_destroy(content);
return dim;
}
/**
* Calculates the position the window should be placed at given its width and
* height and stores them in \p ret_x and \p ret_y.
*/
static void calc_window_pos(int width, int height, int *ret_x, int *ret_y)
{
screen_info *scr = get_active_screen();
if (ret_x) {
if (settings.geometry.negative_x) {
*ret_x = (scr->x + (scr->w - width)) + settings.geometry.x;
} else {
*ret_x = scr->x + settings.geometry.x;
}
}
if (ret_y) {
if (settings.geometry.negative_y) {
*ret_y = scr->y + (scr->h + settings.geometry.y) - height;
} else {
*ret_y = scr->y + settings.geometry.y;
}
}
}
void draw(void)
{
GSList *layouts = create_layouts(x_win_get_context(win));
struct dimensions dim = calculate_dimensions(layouts);
cairo_surface_t *image_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, dim.w, dim.h);
bool first = true;
for (GSList *iter = layouts; iter; iter = iter->next) {
colored_layout *cl_this = iter->data;
colored_layout *cl_next = iter->next ? iter->next->data : NULL;
dim = layout_render(image_surface, cl_this, cl_next, dim, first, !cl_next);
first = false;
}
calc_window_pos(dim.w, dim.h, &dim.x, &dim.y);
x_display_surface(image_surface, win, &dim);
cairo_surface_destroy(image_surface);
free_layouts(layouts);
}
void draw_deinit(void)
{
x_win_destroy(win);
x_free();
}
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */