blob: 9fd2e493f78a8ff35cdf8a779b51ecf778c86123 [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 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050024#include "../config.h"
25
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050026#include <stdint.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdio.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050030#include <math.h>
31#include <cairo.h>
32#include "cairo-util.h"
33
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040034#include "../shared/config-parser.h"
Kristian Høgsberg3d5437c2012-02-08 12:46:57 -050035
Kristian Høgsberg87330262008-11-17 22:23:55 -050036#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
37
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050038void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020039surface_flush_device(cairo_surface_t *surface)
40{
41 cairo_device_t *device;
42
43 device = cairo_surface_get_device(surface);
44 if (device)
45 cairo_device_flush(device);
46}
47
48void
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050049blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050050{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050051 int32_t width, height, stride, x, y, z, w;
52 uint8_t *src, *dst;
53 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050054 int i, j, k, size, half;
Kristian Høgsbergec323d22012-03-21 01:07:49 -040055 uint32_t kernel[71];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050056 double f;
57
Kristian Høgsberg87330262008-11-17 22:23:55 -050058 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050059 width = cairo_image_surface_get_width(surface);
60 height = cairo_image_surface_get_height(surface);
61 stride = cairo_image_surface_get_stride(surface);
62 src = cairo_image_surface_get_data(surface);
63
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040064 dst = malloc(height * stride);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050065
66 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050067 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050068 for (i = 0; i < size; i++) {
69 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040070 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050071 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050072 }
73
74 for (i = 0; i < height; i++) {
75 s = (uint32_t *) (src + i * stride);
76 d = (uint32_t *) (dst + i * stride);
77 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050078 if (margin < j && j < width - margin) {
79 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050080 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050081 }
82
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050083 x = 0;
84 y = 0;
85 z = 0;
86 w = 0;
87 for (k = 0; k < size; k++) {
88 if (j - half + k < 0 || j - half + k >= width)
89 continue;
90 p = s[j - half + k];
91
92 x += (p >> 24) * kernel[k];
93 y += ((p >> 16) & 0xff) * kernel[k];
94 z += ((p >> 8) & 0xff) * kernel[k];
95 w += (p & 0xff) * kernel[k];
96 }
97 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
98 }
99 }
100
101 for (i = 0; i < height; i++) {
102 s = (uint32_t *) (dst + i * stride);
103 d = (uint32_t *) (src + i * stride);
104 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500105 if (margin <= i && i < height - margin) {
106 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500107 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500108 }
109
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500110 x = 0;
111 y = 0;
112 z = 0;
113 w = 0;
114 for (k = 0; k < size; k++) {
115 if (i - half + k < 0 || i - half + k >= height)
116 continue;
117 s = (uint32_t *) (dst + (i - half + k) * stride);
118 p = s[j];
119
120 x += (p >> 24) * kernel[k];
121 y += ((p >> 16) & 0xff) * kernel[k];
122 z += ((p >> 8) & 0xff) * kernel[k];
123 w += (p & 0xff) * kernel[k];
124 }
125 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
126 }
127 }
128
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400129 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400130 cairo_surface_mark_dirty(surface);
131}
132
133void
134tile_mask(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400135 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400136{
137 cairo_pattern_t *pattern;
138 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400139 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400140
141 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
142 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400143 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400144
145 for (i = 0; i < 4; i++) {
146 fx = i & 1;
147 fy = i >> 1;
148
149 cairo_matrix_init_translate(&matrix,
150 -x + fx * (128 - width),
151 -y + fy * (128 - height));
152 cairo_pattern_set_matrix(pattern, &matrix);
153
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400154 if (fy)
155 vmargin = margin;
156 else
157 vmargin = top_margin;
158
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400159 cairo_reset_clip(cr);
160 cairo_rectangle(cr,
161 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400162 y + fy * (height - vmargin),
163 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400164 cairo_clip (cr);
165 cairo_mask(cr, pattern);
166 }
167
Tiago Vignatti0a266412012-02-09 19:06:56 +0200168 /* Top stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400169 cairo_matrix_init_translate(&matrix, 60, 0);
170 cairo_matrix_scale(&matrix, 8.0 / width, 1);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400171 cairo_matrix_translate(&matrix, -x - width / 2, -y);
172 cairo_pattern_set_matrix(pattern, &matrix);
173 cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
174
175 cairo_reset_clip(cr);
176 cairo_rectangle(cr,
177 x + margin,
178 y,
179 width - 2 * margin, margin);
180 cairo_clip (cr);
181 cairo_mask(cr, pattern);
182
Tiago Vignatti0a266412012-02-09 19:06:56 +0200183 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400184 cairo_matrix_translate(&matrix, 0, -height + 128);
185 cairo_pattern_set_matrix(pattern, &matrix);
186
187 cairo_reset_clip(cr);
188 cairo_rectangle(cr, x + margin, y + height - margin,
189 width - 2 * margin, margin);
190 cairo_clip (cr);
191 cairo_mask(cr, pattern);
192
Tiago Vignatti0a266412012-02-09 19:06:56 +0200193 /* Left stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400194 cairo_matrix_init_translate(&matrix, 0, 60);
195 cairo_matrix_scale(&matrix, 1, 8.0 / height);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400196 cairo_matrix_translate(&matrix, -x, -y - height / 2);
197 cairo_pattern_set_matrix(pattern, &matrix);
198 cairo_reset_clip(cr);
199 cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
200 cairo_clip (cr);
201 cairo_mask(cr, pattern);
202
Tiago Vignatti0a266412012-02-09 19:06:56 +0200203 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400204 cairo_matrix_translate(&matrix, -width + 128, 0);
205 cairo_pattern_set_matrix(pattern, &matrix);
206 cairo_rectangle(cr, x + width - margin, y + margin,
207 margin, height - 2 * margin);
208 cairo_reset_clip(cr);
209 cairo_clip (cr);
210 cairo_mask(cr, pattern);
211
212 cairo_pattern_destroy(pattern);
213 cairo_reset_clip(cr);
214}
215
216void
217tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400218 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400219{
220 cairo_pattern_t *pattern;
221 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400222 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400223
224 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
225 pattern = cairo_pattern_create_for_surface (surface);
Kristian Høgsberg919fbf02012-04-03 10:53:15 -0400226 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400227 cairo_set_source(cr, pattern);
228 cairo_pattern_destroy(pattern);
229
230 for (i = 0; i < 4; i++) {
231 fx = i & 1;
232 fy = i >> 1;
233
234 cairo_matrix_init_translate(&matrix,
235 -x + fx * (128 - width),
236 -y + fy * (128 - height));
237 cairo_pattern_set_matrix(pattern, &matrix);
238
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400239 if (fy)
240 vmargin = margin;
241 else
242 vmargin = top_margin;
243
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400244 cairo_rectangle(cr,
245 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400246 y + fy * (height - vmargin),
247 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400248 cairo_fill(cr);
249 }
250
Tiago Vignatti0a266412012-02-09 19:06:56 +0200251 /* Top stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400252 cairo_matrix_init_translate(&matrix, 60, 0);
253 cairo_matrix_scale(&matrix, 8.0 / (width - 2 * margin), 1);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400254 cairo_matrix_translate(&matrix, -x - width / 2, -y);
255 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400256 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400257 cairo_fill(cr);
258
Tiago Vignatti0a266412012-02-09 19:06:56 +0200259 /* Bottom stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400260 cairo_matrix_translate(&matrix, 0, -height + 128);
261 cairo_pattern_set_matrix(pattern, &matrix);
262 cairo_rectangle(cr, x + margin, y + height - margin,
263 width - 2 * margin, margin);
264 cairo_fill(cr);
265
Tiago Vignatti0a266412012-02-09 19:06:56 +0200266 /* Left stretch */
Kristian Høgsberg126f8552012-03-21 12:37:04 -0400267 cairo_matrix_init_translate(&matrix, 0, 60);
268 cairo_matrix_scale(&matrix, 1, 8.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400269 cairo_matrix_translate(&matrix, -x, -y - height / 2);
270 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400271 cairo_rectangle(cr, x, y + top_margin,
272 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400273 cairo_fill(cr);
274
Tiago Vignatti0a266412012-02-09 19:06:56 +0200275 /* Right stretch */
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400276 cairo_matrix_translate(&matrix, -width + 128, 0);
277 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400278 cairo_rectangle(cr, x + width - margin, y + top_margin,
279 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400280 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500281}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400282
283void
284rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
285{
286 cairo_move_to(cr, x0, y0 + radius);
287 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
288 cairo_line_to(cr, x1 - radius, y0);
289 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
290 cairo_line_to(cr, x1, y1 - radius);
291 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
292 cairo_line_to(cr, x0 + radius, y1);
293 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
294 cairo_close_path(cr);
295}
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400296
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500297cairo_surface_t *
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400298load_cairo_surface(const char *filename)
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500299{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400300 pixman_image_t *image;
301 int width, height, stride;
302 void *data;
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500303
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400304 image = load_image(filename);
Ustun Ergenoglu6dc0f862012-03-14 22:07:58 +0200305 if (image == NULL) {
306 return NULL;
307 }
308
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400309 data = pixman_image_get_data(image);
310 width = pixman_image_get_width(image);
311 height = pixman_image_get_height(image);
312 stride = pixman_image_get_stride(image);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500313
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400314 return cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
315 width, height, stride);
Kristian Høgsbergd6548762012-01-25 15:43:48 -0500316}
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400317
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400318struct theme *
319theme_create(void)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400320{
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400321 struct theme *t;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400322 cairo_t *cr;
323 cairo_pattern_t *pattern;
324
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400325 t = malloc(sizeof *t);
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400326 t->margin = 32;
327 t->width = 6;
328 t->titlebar_height = 27;
329 t->frame_radius = 3;
330 t->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
331 cr = cairo_create(t->shadow);
332 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
333 cairo_set_source_rgba(cr, 0, 0, 0, 1);
334 rounded_rect(cr, 32, 32, 96, 96, t->frame_radius);
335 cairo_fill(cr);
336 cairo_destroy(cr);
337 blur_surface(t->shadow, 64);
338
339 t->active_frame =
340 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
341 cr = cairo_create(t->active_frame);
342 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
343
344 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
345 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
346 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
347 cairo_set_source(cr, pattern);
348 cairo_pattern_destroy(pattern);
349
350 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
351 cairo_fill(cr);
352 cairo_destroy(cr);
353
354 t->inactive_frame =
355 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
356 cr = cairo_create(t->inactive_frame);
357 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
358 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
359 rounded_rect(cr, 0, 0, 128, 128, t->frame_radius);
360 cairo_fill(cr);
361 cairo_destroy(cr);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400362
363 return t;
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400364}
365
366void
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400367theme_destroy(struct theme *t)
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400368{
369 cairo_surface_destroy(t->active_frame);
370 cairo_surface_destroy(t->inactive_frame);
371 cairo_surface_destroy(t->shadow);
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400372 free(t);
373}
374
375void
376theme_render_frame(struct theme *t,
377 cairo_t *cr, int width, int height,
378 const char *title, uint32_t flags)
379{
380 cairo_text_extents_t extents;
381 cairo_font_extents_t font_extents;
382 cairo_surface_t *source;
383 int x, y;
384
385 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
386 cairo_set_source_rgba(cr, 0, 0, 0, 0);
387 cairo_paint(cr);
388
389 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
390 tile_mask(cr, t->shadow,
391 2, 2, width + 8, height + 8,
392 64, 64);
393
394 if (flags & THEME_FRAME_ACTIVE)
395 source = t->active_frame;
396 else
397 source = t->inactive_frame;
398
399 tile_source(cr, source,
400 t->margin, t->margin,
401 width - t->margin * 2, height - t->margin * 2,
402 t->width, t->titlebar_height);
403
404 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
405 cairo_select_font_face(cr, "sans",
406 CAIRO_FONT_SLANT_NORMAL,
407 CAIRO_FONT_WEIGHT_BOLD);
408 cairo_set_font_size(cr, 14);
409 cairo_text_extents(cr, title, &extents);
410 cairo_font_extents (cr, &font_extents);
411 x = (width - extents.width) / 2;
412 y = t->margin +
413 (t->titlebar_height -
414 font_extents.ascent - font_extents.descent) / 2 +
415 font_extents.ascent;
416
417 if (flags & THEME_FRAME_ACTIVE) {
418 cairo_move_to(cr, x + 1, y + 1);
419 cairo_set_source_rgb(cr, 1, 1, 1);
420 cairo_show_text(cr, title);
421 cairo_move_to(cr, x, y);
422 cairo_set_source_rgb(cr, 0, 0, 0);
423 cairo_show_text(cr, title);
424 } else {
425 cairo_move_to(cr, x, y);
426 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
427 cairo_show_text(cr, title);
428 }
Kristian Høgsberg42abdf52012-05-15 22:14:27 -0400429}