blob: 3bdf9654915f75d4e8d1ae23358e3355d16ba617 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Kristian Høgsberg902865c2012-02-08 10:11:42 -05003 * Copyright © 2012 Intel Corporation
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -07005 * 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:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050012 *
Bryce Harrington6c6164c2015-06-11 14:20:17 -070013 * 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.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050028
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050029#include <stdint.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdio.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050033#include <math.h>
Boyan Ding850a5142014-08-05 15:22:04 +080034#include <wayland-util.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050035#include <cairo.h>
36#include "cairo-util.h"
37
Jon Cruz35b2eaa2015-06-15 15:37:08 -070038#include "shared/helpers.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050039#include "image-loader.h"
Pekka Paalanen91b10102019-04-04 14:27:31 +030040#include <libweston/config-parser.h>
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050041
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -050042#ifdef HAVE_PANGO
43#include <pango/pangocairo.h>
44#endif
45
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050046void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020047surface_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
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070056static int
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050057blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050058{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050059 int32_t width, height, stride, x, y, z, w;
60 uint8_t *src, *dst;
61 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050062 int i, j, k, size, half;
Kristian Høgsbergec323d22012-03-21 01:07:49 -040063 uint32_t kernel[71];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050064 double f;
65
Kristian Høgsberg87330262008-11-17 22:23:55 -050066 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050067 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
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040072 dst = malloc(height * stride);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -070073 if (dst == NULL)
74 return -1;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050075
76 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050077 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050078 for (i = 0; i < size; i++) {
79 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040080 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050081 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050082 }
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++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050088 if (margin < j && j < width - margin) {
89 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050090 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050091 }
92
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050093 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++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500115 if (margin <= i && i < height - margin) {
116 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500117 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500118 }
119
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500120 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
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400139 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400140 cairo_surface_mark_dirty(surface);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700141
142 return 0;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400143}
144
145void
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100146render_shadow(cairo_t *cr, cairo_surface_t *surface,
147 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400148{
149 cairo_pattern_t *pattern;
150 cairo_matrix_t matrix;
Marek Chalupaeaea4702014-10-29 14:51:23 +0100151 int i, fx, fy, shadow_height, shadow_width;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400152
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100153 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400154 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
155 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400156 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400157
158 for (i = 0; i < 4; i++) {
Marek Chalupaeaea4702014-10-29 14:51:23 +0100159 /* 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 */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400167 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
Marek Chalupaeaea4702014-10-29 14:51:23 +0100175 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;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400190
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400191 cairo_reset_clip(cr);
192 cairo_rectangle(cr,
Marek Chalupaeaea4702014-10-29 14:51:23 +0100193 x + fx * (width - shadow_width),
194 y + fy * (height - shadow_height),
195 shadow_width, shadow_height);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400196 cairo_clip (cr);
197 cairo_mask(cr, pattern);
198 }
199
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400200
Marek Chalupaeaea4702014-10-29 14:51:23 +0100201 shadow_width = width - 2 * margin;
202 shadow_height = top_margin;
203 if (height < 2 * shadow_height)
204 shadow_height = height / 2;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400205
Marek Chalupaeaea4702014-10-29 14:51:23 +0100206 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);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400213
Marek Chalupaeaea4702014-10-29 14:51:23 +0100214 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);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400220
Marek Chalupaeaea4702014-10-29 14:51:23 +0100221 /* Bottom stretch */
222 cairo_matrix_translate(&matrix, 0, -height + 128);
223 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400224
Marek Chalupaeaea4702014-10-29 14:51:23 +0100225 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 }
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400261
262 cairo_pattern_destroy(pattern);
263 cairo_reset_clip(cr);
264}
265
266void
267tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400268 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400269{
270 cairo_pattern_t *pattern;
271 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400272 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400273
274 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
275 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400276 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400277 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
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400289 if (fy)
290 vmargin = margin;
291 else
292 vmargin = top_margin;
293
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400294 cairo_rectangle(cr,
295 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400296 y + fy * (height - vmargin),
297 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400298 cairo_fill(cr);
299 }
300
Tiago Vignatti0a266412012-02-09 19:06:56 +0200301 /* Top stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400302 cairo_matrix_init_translate(&matrix, 60, 0);
303 cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400304 cairo_matrix_translate(&matrix, -x - width / 2, -y);
305 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400306 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400307 cairo_fill(cr);
308
Tiago Vignatti0a266412012-02-09 19:06:56 +0200309 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400310 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
Tiago Vignatti0a266412012-02-09 19:06:56 +0200316 /* Left stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400317 cairo_matrix_init_translate(&matrix, 0, 60);
318 cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400319 cairo_matrix_translate(&matrix, -x, -y - height / 2);
320 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400321 cairo_rectangle(cr, x, y + top_margin,
322 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400323 cairo_fill(cr);
324
Tiago Vignatti0a266412012-02-09 19:06:56 +0200325 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400326 cairo_matrix_translate(&matrix, -width + 128, 0);
327 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400328 cairo_rectangle(cr, x + width - margin, y + top_margin,
329 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400330 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500331}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400332
333void
334rounded_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}
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400346
Pekka Paalanen091b1552021-05-25 11:39:01 +0300347static void
348loaded_cairo_surface_destructor(void *data)
349{
350 pixman_image_t *image = data;
351
352 pixman_image_unref(image);
353}
354
355static const cairo_user_data_key_t weston_cairo_util_load_cairo_surface_key;
356
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500357cairo_surface_t *
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400358load_cairo_surface(const char *filename)
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500359{
Pekka Paalanen091b1552021-05-25 11:39:01 +0300360 cairo_surface_t *surface;
361 cairo_status_t ret;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400362 pixman_image_t *image;
363 int width, height, stride;
364 void *data;
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500365
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400366 image = load_image(filename);
Ustun Ergenoglu6dc0f862012-03-14 22:07:58 +0200367 if (image == NULL) {
368 return NULL;
369 }
370
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400371 data = pixman_image_get_data(image);
372 width = pixman_image_get_width(image);
373 height = pixman_image_get_height(image);
374 stride = pixman_image_get_stride(image);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500375
Pekka Paalanen091b1552021-05-25 11:39:01 +0300376 surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
377 width, height, stride);
378 ret = cairo_surface_status(surface);
379 if (ret != CAIRO_STATUS_SUCCESS)
380 goto fail;
381
382 ret = cairo_surface_set_user_data(surface,
383 &weston_cairo_util_load_cairo_surface_key,
384 image,
385 loaded_cairo_surface_destructor);
386 if (ret != CAIRO_STATUS_SUCCESS)
387 goto fail;
388
389 return surface;
390
391fail:
392 cairo_surface_destroy(surface);
393 pixman_image_unref(image);
394 return NULL;
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500395}
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400396
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700397void
398theme_set_background_source(struct theme *t, cairo_t *cr, uint32_t flags)
399{
400 cairo_pattern_t *pattern;
401
402 if (flags & THEME_FRAME_ACTIVE) {
403 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
404 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
405 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
406 cairo_set_source(cr, pattern);
407 cairo_pattern_destroy(pattern);
408 } else {
409 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
410 }
411}
412
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400413struct theme *
414theme_create(void)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400415{
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400416 struct theme *t;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400417 cairo_t *cr;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400418
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400419 t = malloc(sizeof *t);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700420 if (t == NULL)
421 return NULL;
422
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400423 t->margin = 32;
424 t->width = 6;
425 t->titlebar_height = 27;
426 t->frame_radius = 3;
427 t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
428 cr = cairo_create(t->shadow);
429 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
430 cairo_set_source_rgba(cr, 0, 0, 0, 1);
431 rounded_rect(cr, 32, 32, 96, 96, t->frame_radius);
432 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700433 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
434 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400435 cairo_destroy(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700436 if (blur_surface(t->shadow, 64) == -1)
437 goto err_shadow;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400438
439 t->active_frame =
440 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
441 cr = cairo_create(t->active_frame);
442 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
443
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700444 theme_set_background_source(t, cr, THEME_FRAME_ACTIVE);
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400445 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
446 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700447
448 if (cairo_status(cr) != CAIRO_STATUS_SUCCESS)
449 goto err_active_frame;
450
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400451 cairo_destroy(cr);
452
453 t->inactive_frame =
454 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
455 cr = cairo_create(t->inactive_frame);
456 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergc680e902013-10-23 21:49:30 -0700457 theme_set_background_source(t, cr, 0);
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400458 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
459 cairo_fill(cr);
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700460
461 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
462 goto err_inactive_frame;
463
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400464 cairo_destroy(cr);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400465
466 return t;
Kristian Høgsbergc0bf8172013-07-25 15:05:35 -0700467
468 err_inactive_frame:
469 cairo_surface_destroy(t->inactive_frame);
470 err_active_frame:
471 cairo_surface_destroy(t->active_frame);
472 err_shadow:
473 cairo_surface_destroy(t->shadow);
474 free(t);
475 return NULL;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400476}
477
478void
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400479theme_destroy(struct theme *t)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400480{
481 cairo_surface_destroy(t->active_frame);
482 cairo_surface_destroy(t->inactive_frame);
483 cairo_surface_destroy(t->shadow);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400484 free(t);
485}
486
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500487#ifdef HAVE_PANGO
488static PangoLayout *
489create_layout(cairo_t *cr, const char *title)
490{
491 PangoLayout *layout;
492 PangoFontDescription *desc;
493
494 layout = pango_cairo_create_layout(cr);
Tomohito Esaki6f9db6c2019-04-01 17:51:35 +0900495 if (title) {
496 pango_layout_set_text(layout, title, -1);
Alyssa Ross7c121822021-11-17 17:25:17 +0000497 desc = pango_font_description_from_string("sans-serif Bold 10");
Tomohito Esaki6f9db6c2019-04-01 17:51:35 +0900498 pango_layout_set_font_description(layout, desc);
499 pango_font_description_free(desc);
500 }
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500501 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
502 pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
503 pango_layout_set_auto_dir (layout, FALSE);
504 pango_layout_set_single_paragraph_mode (layout, TRUE);
505 pango_layout_set_width (layout, -1);
506
507 return layout;
508}
509#endif
510
511#ifdef HAVE_PANGO
512#define SHOW_TEXT(cr) \
513 pango_cairo_show_layout(cr, title_layout)
514#else
515#define SHOW_TEXT(cr) \
516 cairo_show_text(cr, title)
517#endif
518
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400519void
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600520theme_render_frame(struct theme *t,
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400521 cairo_t *cr, int width, int height,
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500522 const char *title, cairo_rectangle_int_t *title_rect,
523 struct wl_list *buttons, uint32_t flags)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400524{
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400525 cairo_surface_t *source;
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700526 int x, y, margin, top_margin;
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500527 int text_width, text_height;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400528
529 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
530 cairo_set_source_rgba(cr, 0, 0, 0, 0);
531 cairo_paint(cr);
532
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600533 if (flags & THEME_FRAME_MAXIMIZED)
534 margin = 0;
535 else {
Marek Chalupa0d7fe8d2014-10-29 14:51:22 +0100536 render_shadow(cr, t->shadow,
537 2, 2, width + 8, height + 8,
538 64, 64);
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600539 margin = t->margin;
540 }
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400541
542 if (flags & THEME_FRAME_ACTIVE)
543 source = t->active_frame;
544 else
545 source = t->inactive_frame;
546
Boyan Ding850a5142014-08-05 15:22:04 +0800547 if (title || !wl_list_empty(buttons))
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700548 top_margin = t->titlebar_height;
549 else
550 top_margin = t->width;
551
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400552 tile_source(cr, source,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600553 margin, margin,
554 width - margin * 2, height - margin * 2,
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700555 t->width, top_margin);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400556
Boyan Ding850a5142014-08-05 15:22:04 +0800557 if (title || !wl_list_empty(buttons)) {
Martin Minarik5f3eddc2012-07-02 23:05:50 +0200558
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500559 cairo_rectangle (cr, title_rect->x, title_rect->y,
560 title_rect->width, title_rect->height);
561 cairo_clip(cr);
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700562 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500563
564#ifdef HAVE_PANGO
565 PangoLayout *title_layout;
566 PangoRectangle logical;
567
568 title_layout = create_layout(cr, title);
569
570 pango_layout_get_pixel_extents (title_layout, NULL, &logical);
571 text_width = MIN(title_rect->width, logical.width);
572 text_height = logical.height;
573 if (text_width < logical.width)
574 pango_layout_set_width (title_layout, text_width * PANGO_SCALE);
Pekka Paalanen91b10102019-04-04 14:27:31 +0300575
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500576#else
577 cairo_text_extents_t extents;
578 cairo_font_extents_t font_extents;
579
Alyssa Ross7c121822021-11-17 17:25:17 +0000580 cairo_select_font_face(cr, "sans-serif",
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700581 CAIRO_FONT_SLANT_NORMAL,
582 CAIRO_FONT_WEIGHT_BOLD);
583 cairo_set_font_size(cr, 14);
584 cairo_text_extents(cr, title, &extents);
585 cairo_font_extents (cr, &font_extents);
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500586 text_width = extents.width;
587 text_height = font_extents.descent - font_extents.ascent;
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500588#endif
Louis-Francis Ratté-Boulianne864e39b2017-11-13 16:20:55 -0500589
590 x = (width - text_width) / 2;
591 y = margin + (t->titlebar_height - text_height) / 2;
592 if (x < title_rect->x)
593 x = title_rect->x;
594 else if (x + text_width > (title_rect->x + title_rect->width))
595 x = (title_rect->x + title_rect->width) - text_width;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400596
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700597 if (flags & THEME_FRAME_ACTIVE) {
598 cairo_move_to(cr, x + 1, y + 1);
599 cairo_set_source_rgb(cr, 1, 1, 1);
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500600 SHOW_TEXT(cr);
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700601 cairo_move_to(cr, x, y);
602 cairo_set_source_rgb(cr, 0, 0, 0);
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500603 SHOW_TEXT(cr);
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700604 } else {
605 cairo_move_to(cr, x, y);
606 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
Louis-Francis Ratté-Boulianne037f0562017-11-13 16:20:56 -0500607 SHOW_TEXT(cr);
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700608 }
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400609 }
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400610}
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400611
612enum theme_location
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600613theme_get_location(struct theme *t, int x, int y,
614 int width, int height, int flags)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400615{
616 int vlocation, hlocation, location;
Jasper St. Pierref11ad432014-04-28 11:19:30 -0400617 int margin, top_margin, grip_size;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400618
Jasper St. Pierref11ad432014-04-28 11:19:30 -0400619 if (flags & THEME_FRAME_MAXIMIZED) {
620 margin = 0;
621 grip_size = 0;
622 } else {
623 margin = t->margin;
624 grip_size = 8;
625 }
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600626
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700627 if (flags & THEME_FRAME_NO_TITLE)
628 top_margin = t->width;
629 else
630 top_margin = t->titlebar_height;
631
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600632 if (x < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400633 hlocation = THEME_LOCATION_EXTERIOR;
Jasper St. Pierrea4d97232014-04-28 11:19:29 -0400634 else if (x < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400635 hlocation = THEME_LOCATION_RESIZING_LEFT;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600636 else if (x < width - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400637 hlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600638 else if (x < width - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400639 hlocation = THEME_LOCATION_RESIZING_RIGHT;
640 else
641 hlocation = THEME_LOCATION_EXTERIOR;
642
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600643 if (y < margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400644 vlocation = THEME_LOCATION_EXTERIOR;
Jasper St. Pierrea4d97232014-04-28 11:19:29 -0400645 else if (y < margin + grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400646 vlocation = THEME_LOCATION_RESIZING_TOP;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600647 else if (y < height - margin - grip_size)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400648 vlocation = THEME_LOCATION_INTERIOR;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -0600649 else if (y < height - margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400650 vlocation = THEME_LOCATION_RESIZING_BOTTOM;
651 else
652 vlocation = THEME_LOCATION_EXTERIOR;
653
654 location = vlocation | hlocation;
655 if (location & THEME_LOCATION_EXTERIOR)
656 location = THEME_LOCATION_EXTERIOR;
657 if (location == THEME_LOCATION_INTERIOR &&
Kristian Høgsberg89f4bc42013-10-23 22:12:13 -0700658 y < margin + top_margin)
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -0400659 location = THEME_LOCATION_TITLEBAR;
660 else if (location == THEME_LOCATION_INTERIOR)
661 location = THEME_LOCATION_CLIENT_AREA;
662
663 return location;
664}