1 /*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include "config.h"
28
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <math.h>
34 #include <wayland-util.h>
35 #include <cairo.h>
36 #include "cairo-util.h"
37
38 #include "shared/helpers.h"
39 #include "image-loader.h"
40 #include <libweston/config-parser.h>
41
42 #ifdef HAVE_PANGO
43 #include <pango/pangocairo.h>
44 #endif
45
46 void
surface_flush_device(cairo_surface_t * surface)47 surface_flush_device(cairo_surface_t *surface)
48 {
49 cairo_device_t *device;
50
51 device = cairo_surface_get_device(surface);
52 if (device)
53 cairo_device_flush(device);
54 }
55
56 static int
blur_surface(cairo_surface_t * surface,int margin)57 blur_surface(cairo_surface_t *surface, int margin)
58 {
59 int32_t width, height, stride, x, y, z, w;
60 uint8_t *src, *dst;
61 uint32_t *s, *d, a, p;
62 int i, j, k, size, half;
63 uint32_t kernel[71];
64 double f;
65
66 size = ARRAY_LENGTH(kernel);
67 width = cairo_image_surface_get_width(surface);
68 height = cairo_image_surface_get_height(surface);
69 stride = cairo_image_surface_get_stride(surface);
70 src = cairo_image_surface_get_data(surface);
71
72 dst = malloc(height * stride);
73 if (dst == NULL)
74 return -1;
75
76 half = size / 2;
77 a = 0;
78 for (i = 0; i < size; i++) {
79 f = (i - half);
80 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
81 a += kernel[i];
82 }
83
84 for (i = 0; i < height; i++) {
85 s = (uint32_t *) (src + i * stride);
86 d = (uint32_t *) (dst + i * stride);
87 for (j = 0; j < width; j++) {
88 if (margin < j && j < width - margin) {
89 d[j] = s[j];
90 continue;
91 }
92
93 x = 0;
94 y = 0;
95 z = 0;
96 w = 0;
97 for (k = 0; k < size; k++) {
98 if (j - half + k < 0 || j - half + k >= width)
99 continue;
100 p = s[j - half + k];
101
102 x += (p >> 24) * kernel[k];
103 y += ((p >> 16) & 0xff) * kernel[k];
104 z += ((p >> 8) & 0xff) * kernel[k];
105 w += (p & 0xff) * kernel[k];
106 }
107 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
108 }
109 }
110
111 for (i = 0; i < height; i++) {
112 s = (uint32_t *) (dst + i * stride);
113 d = (uint32_t *) (src + i * stride);
114 for (j = 0; j < width; j++) {
115 if (margin <= i && i < height - margin) {
116 d[j] = s[j];
117 continue;
118 }
119
120 x = 0;
121 y = 0;
122 z = 0;
123 w = 0;
124 for (k = 0; k < size; k++) {
125 if (i - half + k < 0 || i - half + k >= height)
126 continue;
127 s = (uint32_t *) (dst + (i - half + k) * stride);
128 p = s[j];
129
130 x += (p >> 24) * kernel[k];
131 y += ((p >> 16) & 0xff) * kernel[k];
132 z += ((p >> 8) & 0xff) * kernel[k];
133 w += (p & 0xff) * kernel[k];
134 }
135 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
136 }
137 }
138
139 free(dst);
140 cairo_surface_mark_dirty(surface);
141
142 return 0;
143 }
144
145 void
render_shadow(cairo_t * cr,cairo_surface_t * surface,int x,int y,int width,int height,int margin,int top_margin)146 render_shadow(cairo_t *cr, cairo_surface_t *surface,
147 int x, int y, int width, int height, int margin, int top_margin)
148 {
149 cairo_pattern_t *pattern;
150 cairo_matrix_t matrix;
151 int i, fx, fy, shadow_height, shadow_width;
152
153 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
154 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
155 pattern = cairo_pattern_create_for_surface (surface);
156 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
157
158 for (i = 0; i < 4; i++) {
159 /* when fy is set, then we are working with lower corners,
160 * when fx is set, then we are working with right corners
161 *
162 * 00 ------- 01
163 * | |
164 * | |
165 * 10 ------- 11
166 */
167 fx = i & 1;
168 fy = i >> 1;
169
170 cairo_matrix_init_translate(&matrix,
171 -x + fx * (128 - width),
172 -y + fy * (128 - height));
173 cairo_pattern_set_matrix(pattern, &matrix);
174
175 shadow_width = margin;
176 shadow_height = fy ? margin : top_margin;
177
178 /* if the shadows together are greater than the surface, we need
179 * to fix it - set the shadow size to the half of
180 * the size of surface. Also handle the case when the size is
181 * not divisible by 2. In that case we need one part of the
182 * shadow to be one pixel greater. !fy or !fx, respectively,
183 * will do the work.
184 */
185 if (height < 2 * shadow_height)
186 shadow_height = (height + !fy) / 2;
187
188 if (width < 2 * shadow_width)
189 shadow_width = (width + !fx) / 2;
190
191 cairo_reset_clip(cr);
192 cairo_rectangle(cr,
193 x + fx * (width - shadow_width),
194 y + fy * (height - shadow_height),
195 shadow_width, shadow_height);
196 cairo_clip (cr);
197 cairo_mask(cr, pattern);
198 }
199
200
201 shadow_width = width - 2 * margin;
202 shadow_height = top_margin;
203 if (height < 2 * shadow_height)
204 shadow_height = height / 2;
205
206 if (shadow_width > 0 && shadow_height) {
207 /* Top stretch */
208 cairo_matrix_init_translate(&matrix, 60, 0);
209 cairo_matrix_scale(&matrix, 8.0 / width, 1);
210 cairo_matrix_translate(&matrix, -x - width / 2, -y);
211 cairo_pattern_set_matrix(pattern, &matrix);
212 cairo_rectangle(cr, x + margin, y, shadow_width, shadow_height);
213
214 cairo_reset_clip(cr);
215 cairo_rectangle(cr,
216 x + margin, y,
217 shadow_width, shadow_height);
218 cairo_clip (cr);
219 cairo_mask(cr, pattern);
220
221 /* Bottom stretch */
222 cairo_matrix_translate(&matrix, 0, -height + 128);
223 cairo_pattern_set_matrix(pattern, &matrix);
224
225 cairo_reset_clip(cr);
226 cairo_rectangle(cr, x + margin, y + height - margin,
227 shadow_width, margin);
228 cairo_clip (cr);
229 cairo_mask(cr, pattern);
230 }
231
232 shadow_width = margin;
233 if (width < 2 * shadow_width)
234 shadow_width = width / 2;
235
236 shadow_height = height - margin - top_margin;
237
238 /* if height is smaller than sum of margins,
239 * then the shadow is already done by the corners */
240 if (shadow_height > 0 && shadow_width) {
241 /* Left stretch */
242 cairo_matrix_init_translate(&matrix, 0, 60);
243 cairo_matrix_scale(&matrix, 1, 8.0 / height);
244 cairo_matrix_translate(&matrix, -x, -y - height / 2);
245 cairo_pattern_set_matrix(pattern, &matrix);
246 cairo_reset_clip(cr);
247 cairo_rectangle(cr, x, y + top_margin,
248 shadow_width, shadow_height);
249 cairo_clip (cr);
250 cairo_mask(cr, pattern);
251
252 /* Right stretch */
253 cairo_matrix_translate(&matrix, -width + 128, 0);
254 cairo_pattern_set_matrix(pattern, &matrix);
255 cairo_rectangle(cr, x + width - shadow_width, y + top_margin,
256 shadow_width, shadow_height);
257 cairo_reset_clip(cr);
258 cairo_clip (cr);
259 cairo_mask(cr, pattern);
260 }
261
262 cairo_pattern_destroy(pattern);
263 cairo_reset_clip(cr);
264 }
265
266 void
tile_source(cairo_t * cr,cairo_surface_t * surface,int x,int y,int width,int height,int margin,int top_margin)267 tile_source(cairo_t *cr, cairo_surface_t *surface,
268 int x, int y, int width, int height, int margin, int top_margin)
269 {
270 cairo_pattern_t *pattern;
271 cairo_matrix_t matrix;
272 int i, fx, fy, vmargin;
273
274 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
275 pattern = cairo_pattern_create_for_surface (surface);
276 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
277 cairo_set_source(cr, pattern);
278 cairo_pattern_destroy(pattern);
279
280 for (i = 0; i < 4; i++) {
281 fx = i & 1;
282 fy = i >> 1;
283
284 cairo_matrix_init_translate(&matrix,
285 -x + fx * (128 - width),
286 -y + fy * (128 - height));
287 cairo_pattern_set_matrix(pattern, &matrix);
288
289 if (fy)
290 vmargin = margin;
291 else
292 vmargin = top_margin;
293
294 cairo_rectangle(cr,
295 x + fx * (width - margin),
296 y + fy * (height - vmargin),
297 margin, vmargin);
298 cairo_fill(cr);
299 }
300
301 /* Top stretch */
302 cairo_matrix_init_translate(&matrix, 60, 0);
303 cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1);
304 cairo_matrix_translate(&matrix, -x - width / 2, -y);
305 cairo_pattern_set_matrix(pattern, &matrix);
306 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
307 cairo_fill(cr);
308
309 /* Bottom stretch */
310 cairo_matrix_translate(&matrix, 0, -height + 128);
311 cairo_pattern_set_matrix(pattern, &matrix);
312 cairo_rectangle(cr, x + margin, y + height - margin,
313 width - 2 * margin, margin);
314 cairo_fill(cr);
315
316 /* Left stretch */
317 cairo_matrix_init_translate(&matrix, 0, 60);
318 cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin));
319 cairo_matrix_translate(&matrix, -x, -y - height / 2);
320 cairo_pattern_set_matrix(pattern, &matrix);
321 cairo_rectangle(cr, x, y + top_margin,
322 margin, height - margin - top_margin);
323 cairo_fill(cr);
324
325 /* Right stretch */
326 cairo_matrix_translate(&matrix, -width + 128, 0);
327 cairo_pattern_set_matrix(pattern, &matrix);
328 cairo_rectangle(cr, x + width - margin, y + top_margin,
329 margin, height - margin - top_margin);
330 cairo_fill(cr);
331 }
332
333 void
rounded_rect(cairo_t * cr,int x0,int y0,int x1,int y1,int radius)334 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
335 {
336 cairo_move_to(cr, x0, y0 + radius);
337 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
338 cairo_line_to(cr, x1 - radius, y0);
339 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
340 cairo_line_to(cr, x1, y1 - radius);
341 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
342 cairo_line_to(cr, x0 + radius, y1);
343 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
344 cairo_close_path(cr);
345 }
346
347 cairo_surface_t *
load_cairo_surface(const char * filename)348 load_cairo_surface(const char *filename)
349 {
350 pixman_image_t *image;
351 int width, height, stride;
352 void *data;
353
354 image = load_image(filename);
355 if (image == NULL) {
356 return NULL;
357 }
358
359 data = pixman_image_get_data(image);
360 width = pixman_image_get_width(image);
361 height = pixman_image_get_height(image);
362 stride = pixman_image_get_stride(image);
363
364 return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
365 width, height, stride);
366 }
367
368 void
theme_set_background_source(struct theme * t,cairo_t * cr,uint32_t flags)369 theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags)
370 {
371 cairo_pattern_t *pattern;
372
373 if (flags & THEME_FRAME_ACTIVE) {
374 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
375 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
376 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
377 cairo_set_source(cr, pattern);
378 cairo_pattern_destroy(pattern);
379 } else {
380 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
381 }
382 }
383
384 struct theme *
theme_create(void)385 theme_create(void)
386 {
387 struct theme *t;
388 cairo_t *cr;
389
390 t = malloc(sizeof *t);
391 if (t == NULL)
392 return NULL;
393
394 t->margin = 32;
395 t->width = 6;
396 t->titlebar_height = 27;
397 t->frame_radius = 3;
398 t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
399 cr = cairo_create(t->shadow);
400 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
401 cairo_set_source_rgba(cr, 0, 0, 0, 1);
402 rounded_rect(cr, 32, 32, 96, 96, t->frame_radius);
403 cairo_fill(cr);
404 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
405 goto err_shadow;
406 cairo_destroy(cr);
407 if (blur_surface(t->shadow, 64) == -1)
408 goto err_shadow;
409
410 t->active_frame =
411 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
412 cr = cairo_create(t->active_frame);
413 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
414
415 theme_set_background_source(t, cr, THEME_FRAME_ACTIVE);
416 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
417 cairo_fill(cr);
418
419 if (cairo_status(cr) != CAIRO_STATUS_SUCCESS)
420 goto err_active_frame;
421
422 cairo_destroy(cr);
423
424 t->inactive_frame =
425 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
426 cr = cairo_create(t->inactive_frame);
427 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
428 theme_set_background_source(t, cr, 0);
429 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
430 cairo_fill(cr);
431
432 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
433 goto err_inactive_frame;
434
435 cairo_destroy(cr);
436
437 return t;
438
439 err_inactive_frame:
440 cairo_surface_destroy(t->inactive_frame);
441 err_active_frame:
442 cairo_surface_destroy(t->active_frame);
443 err_shadow:
444 cairo_surface_destroy(t->shadow);
445 free(t);
446 return NULL;
447 }
448
449 void
theme_destroy(struct theme * t)450 theme_destroy(struct theme *t)
451 {
452 cairo_surface_destroy(t->active_frame);
453 cairo_surface_destroy(t->inactive_frame);
454 cairo_surface_destroy(t->shadow);
455 free(t);
456 }
457
458 #ifdef HAVE_PANGO
459 static PangoLayout *
create_layout(cairo_t * cr,const char * title)460 create_layout(cairo_t *cr, const char *title)
461 {
462 PangoLayout *layout;
463 PangoFontDescription *desc;
464
465 layout = pango_cairo_create_layout(cr);
466 if (title) {
467 pango_layout_set_text(layout, title, -1);
468 desc = pango_font_description_from_string("Sans Bold 10");
469 pango_layout_set_font_description(layout, desc);
470 pango_font_description_free(desc);
471 }
472 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
473 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
474 pango_layout_set_auto_dir (layout, FALSE);
475 pango_layout_set_single_paragraph_mode (layout, TRUE);
476 pango_layout_set_width (layout, -1);
477
478 return layout;
479 }
480 #endif
481
482 #ifdef HAVE_PANGO
483 #define SHOW_TEXT(cr) \
484 pango_cairo_show_layout(cr, title_layout)
485 #else
486 #define SHOW_TEXT(cr) \
487 cairo_show_text(cr, title)
488 #endif
489
490 void
theme_render_frame(struct theme * t,cairo_t * cr,int width,int height,const char * title,cairo_rectangle_int_t * title_rect,struct wl_list * buttons,uint32_t flags)491 theme_render_frame(struct theme *t,
492 cairo_t *cr, int width, int height,
493 const char *title, cairo_rectangle_int_t *title_rect,
494 struct wl_list *buttons, uint32_t flags)
495 {
496 cairo_surface_t *source;
497 int x, y, margin, top_margin;
498 int text_width, text_height;
499
500 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
501 cairo_set_source_rgba(cr, 0, 0, 0, 0);
502 cairo_paint(cr);
503
504 if (flags & THEME_FRAME_MAXIMIZED)
505 margin = 0;
506 else {
507 render_shadow(cr, t->shadow,
508 2, 2, width + 8, height + 8,
509 64, 64);
510 margin = t->margin;
511 }
512
513 if (flags & THEME_FRAME_ACTIVE)
514 source = t->active_frame;
515 else
516 source = t->inactive_frame;
517
518 if (title || !wl_list_empty(buttons))
519 top_margin = t->titlebar_height;
520 else
521 top_margin = t->width;
522
523 tile_source(cr, source,
524 margin, margin,
525 width - margin * 2, height - margin * 2,
526 t->width, top_margin);
527
528 if (title || !wl_list_empty(buttons)) {
529
530 cairo_rectangle (cr, title_rect->x, title_rect->y,
531 title_rect->width, title_rect->height);
532 cairo_clip(cr);
533 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
534
535 #ifdef HAVE_PANGO
536 PangoLayout *title_layout;
537 PangoRectangle logical;
538
539 title_layout = create_layout(cr, title);
540
541 pango_layout_get_pixel_extents (title_layout, NULL, &logical);
542 text_width = MIN(title_rect->width, logical.width);
543 text_height = logical.height;
544 if (text_width < logical.width)
545 pango_layout_set_width (title_layout, text_width * PANGO_SCALE);
546
547 #else
548 cairo_text_extents_t extents;
549 cairo_font_extents_t font_extents;
550
551 cairo_select_font_face(cr, "sans",
552 CAIRO_FONT_SLANT_NORMAL,
553 CAIRO_FONT_WEIGHT_BOLD);
554 cairo_set_font_size(cr, 14);
555 cairo_text_extents(cr, title, &extents);
556 cairo_font_extents (cr, &font_extents);
557 text_width = extents.width;
558 text_height = font_extents.descent - font_extents.ascent;
559 #endif
560
561 x = (width - text_width) / 2;
562 y = margin + (t->titlebar_height - text_height) / 2;
563 if (x < title_rect->x)
564 x = title_rect->x;
565 else if (x + text_width > (title_rect->x + title_rect->width))
566 x = (title_rect->x + title_rect->width) - text_width;
567
568 if (flags & THEME_FRAME_ACTIVE) {
569 cairo_move_to(cr, x + 1, y + 1);
570 cairo_set_source_rgb(cr, 1, 1, 1);
571 SHOW_TEXT(cr);
572 cairo_move_to(cr, x, y);
573 cairo_set_source_rgb(cr, 0, 0, 0);
574 SHOW_TEXT(cr);
575 } else {
576 cairo_move_to(cr, x, y);
577 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
578 SHOW_TEXT(cr);
579 }
580 }
581 }
582
583 enum theme_location
theme_get_location(struct theme * t,int x,int y,int width,int height,int flags)584 theme_get_location(struct theme *t, int x, int y,
585 int width, int height, int flags)
586 {
587 int vlocation, hlocation, location;
588 int margin, top_margin, grip_size;
589
590 if (flags & THEME_FRAME_MAXIMIZED) {
591 margin = 0;
592 grip_size = 0;
593 } else {
594 margin = t->margin;
595 grip_size = 8;
596 }
597
598 if (flags & THEME_FRAME_NO_TITLE)
599 top_margin = t->width;
600 else
601 top_margin = t->titlebar_height;
602
603 if (x < margin)
604 hlocation = THEME_LOCATION_EXTERIOR;
605 else if (x < margin + grip_size)
606 hlocation = THEME_LOCATION_RESIZING_LEFT;
607 else if (x < width - margin - grip_size)
608 hlocation = THEME_LOCATION_INTERIOR;
609 else if (x < width - margin)
610 hlocation = THEME_LOCATION_RESIZING_RIGHT;
611 else
612 hlocation = THEME_LOCATION_EXTERIOR;
613
614 if (y < margin)
615 vlocation = THEME_LOCATION_EXTERIOR;
616 else if (y < margin + grip_size)
617 vlocation = THEME_LOCATION_RESIZING_TOP;
618 else if (y < height - margin - grip_size)
619 vlocation = THEME_LOCATION_INTERIOR;
620 else if (y < height - margin)
621 vlocation = THEME_LOCATION_RESIZING_BOTTOM;
622 else
623 vlocation = THEME_LOCATION_EXTERIOR;
624
625 location = vlocation | hlocation;
626 if (location & THEME_LOCATION_EXTERIOR)
627 location = THEME_LOCATION_EXTERIOR;
628 if (location == THEME_LOCATION_INTERIOR &&
629 y < margin + top_margin)
630 location = THEME_LOCATION_TITLEBAR;
631 else if (location == THEME_LOCATION_INTERIOR)
632 location = THEME_LOCATION_CLIENT_AREA;
633
634 return location;
635 }
636