1 |
/*----------------------------------------------------------------------* |
2 |
* File: background.C - former xpm.C |
3 |
*----------------------------------------------------------------------* |
4 |
* |
5 |
* All portions of code are copyright by their respective author/s. |
6 |
* Copyright (c) 2005-2008 Marc Lehmann <schmorp@schmorp.de> |
7 |
* Copyright (c) 2007 Sasha Vasko <sasha@aftercode.net> |
8 |
* Copyright (c) 2010-2012 Emanuele Giaquinta <e.giaquinta@glauco.it> |
9 |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
12 |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* (at your option) any later version. |
14 |
* |
15 |
* This program is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* GNU General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU General Public License |
21 |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
23 |
*---------------------------------------------------------------------*/ |
24 |
|
25 |
#include "../config.h" /* NECESSARY */ |
26 |
#include "rxvt.h" /* NECESSARY */ |
27 |
|
28 |
#ifdef HAVE_BG_PIXMAP |
29 |
|
30 |
void |
31 |
rxvt_term::bg_destroy () |
32 |
{ |
33 |
# if ENABLE_TRANSPARENCY |
34 |
delete root_img; |
35 |
root_img = 0; |
36 |
# endif |
37 |
|
38 |
# if BG_IMAGE_FROM_FILE |
39 |
fimage.destroy (); |
40 |
# endif |
41 |
|
42 |
delete bg_img; |
43 |
bg_img = 0; |
44 |
} |
45 |
|
46 |
bool |
47 |
rxvt_term::bg_set_position (int x, int y) |
48 |
{ |
49 |
|
50 |
if (target_x != x |
51 |
|| target_y != y) |
52 |
{ |
53 |
target_x = x; |
54 |
target_y = y; |
55 |
return true; |
56 |
} |
57 |
return false; |
58 |
} |
59 |
|
60 |
bool |
61 |
rxvt_term::bg_window_size_sensitive () |
62 |
{ |
63 |
# if ENABLE_TRANSPARENCY |
64 |
if (bg_flags & BG_IS_TRANSPARENT) |
65 |
return true; |
66 |
# endif |
67 |
|
68 |
# if BG_IMAGE_FROM_FILE |
69 |
if (fimage.img) |
70 |
{ |
71 |
if ((fimage.flags & IM_IS_SIZE_SENSITIVE) |
72 |
|| fimage.img->w > szHint.width |
73 |
|| fimage.img->h > szHint.height) |
74 |
return true; |
75 |
} |
76 |
# endif |
77 |
|
78 |
return false; |
79 |
} |
80 |
|
81 |
bool |
82 |
rxvt_term::bg_window_position_sensitive () |
83 |
{ |
84 |
# if ENABLE_TRANSPARENCY |
85 |
if (bg_flags & BG_IS_TRANSPARENT) |
86 |
return true; |
87 |
# endif |
88 |
|
89 |
# if BG_IMAGE_FROM_FILE |
90 |
if (fimage.img) |
91 |
{ |
92 |
if (fimage.flags & IM_ROOT_ALIGN) |
93 |
return true; |
94 |
} |
95 |
# endif |
96 |
|
97 |
return false; |
98 |
} |
99 |
|
100 |
# if BG_IMAGE_FROM_FILE |
101 |
static inline int |
102 |
make_align_position (int align, int window_size, int image_size) |
103 |
{ |
104 |
if (align >= 0 && align <= 100) |
105 |
return lerp (0, window_size - image_size, align); |
106 |
else if (align > 100) |
107 |
return lerp (window_size - image_size, window_size, align - 100); |
108 |
else |
109 |
return lerp (-image_size, 0, align + 100); |
110 |
} |
111 |
|
112 |
static void |
113 |
parse_style (const char *style, int &x, int &y, unsigned int &w, unsigned int &h, uint8_t &flags) |
114 |
{ |
115 |
if (!strcasecmp (style, "tiled")) |
116 |
{ |
117 |
flags = IM_TILE; |
118 |
w = h = noScale; |
119 |
x = y = 0; |
120 |
} |
121 |
else if (!strcasecmp (style, "aspect-stretched")) |
122 |
{ |
123 |
flags = IM_KEEP_ASPECT; |
124 |
w = h = windowScale; |
125 |
x = y = centerAlign; |
126 |
} |
127 |
else if (!strcasecmp (style, "stretched")) |
128 |
{ |
129 |
flags = 0; |
130 |
w = h = windowScale; |
131 |
x = y = centerAlign; |
132 |
} |
133 |
else if (!strcasecmp (style, "centered")) |
134 |
{ |
135 |
flags = 0; |
136 |
w = h = noScale; |
137 |
x = y = centerAlign; |
138 |
} |
139 |
else if (!strcasecmp (style, "root-tiled")) |
140 |
{ |
141 |
flags = IM_TILE|IM_ROOT_ALIGN; |
142 |
w = h = noScale; |
143 |
x = y = 0; |
144 |
} |
145 |
} |
146 |
|
147 |
bool |
148 |
rxvt_image::set_geometry (const char *geom, bool update) |
149 |
{ |
150 |
bool changed = false; |
151 |
int geom_flags = 0; |
152 |
int x = h_align; |
153 |
int y = v_align; |
154 |
unsigned int w = h_scale; |
155 |
unsigned int h = v_scale; |
156 |
uint8_t new_flags = 0; |
157 |
|
158 |
if (geom == NULL) |
159 |
return false; |
160 |
|
161 |
if (geom[0]) |
162 |
{ |
163 |
char **arr = rxvt_strsplit (':', geom); |
164 |
|
165 |
for (int i = 0; arr[i]; i++) |
166 |
{ |
167 |
if (!strncasecmp (arr[i], "style=", 6)) |
168 |
{ |
169 |
parse_style (arr[i] + 6, x, y, w, h, new_flags); |
170 |
geom_flags = WidthValue|HeightValue|XValue|YValue; |
171 |
} |
172 |
else if (!strcasecmp (arr[i], "op=tile")) |
173 |
new_flags |= IM_TILE; |
174 |
else if (!strcasecmp (arr[i], "op=keep-aspect")) |
175 |
new_flags |= IM_KEEP_ASPECT; |
176 |
else if (!strcasecmp (arr[i], "op=root-align")) |
177 |
new_flags |= IM_ROOT_ALIGN; |
178 |
|
179 |
// deprecated |
180 |
else if (!strcasecmp (arr[i], "tile")) |
181 |
{ |
182 |
new_flags |= IM_TILE; |
183 |
w = h = noScale; |
184 |
geom_flags |= WidthValue|HeightValue; |
185 |
} |
186 |
else if (!strcasecmp (arr[i], "propscale")) |
187 |
{ |
188 |
new_flags |= IM_KEEP_ASPECT; |
189 |
w = h = windowScale; |
190 |
geom_flags |= WidthValue|HeightValue; |
191 |
} |
192 |
else if (!strcasecmp (arr[i], "hscale")) |
193 |
{ |
194 |
new_flags |= IM_TILE; |
195 |
w = windowScale; |
196 |
h = noScale; |
197 |
geom_flags |= WidthValue|HeightValue; |
198 |
} |
199 |
else if (!strcasecmp (arr[i], "vscale")) |
200 |
{ |
201 |
new_flags |= IM_TILE; |
202 |
h = windowScale; |
203 |
w = noScale; |
204 |
geom_flags |= WidthValue|HeightValue; |
205 |
} |
206 |
else if (!strcasecmp (arr[i], "scale")) |
207 |
{ |
208 |
w = h = windowScale; |
209 |
geom_flags |= WidthValue|HeightValue; |
210 |
} |
211 |
else if (!strcasecmp (arr[i], "auto")) |
212 |
{ |
213 |
w = h = windowScale; |
214 |
x = y = centerAlign; |
215 |
geom_flags |= WidthValue|HeightValue|XValue|YValue; |
216 |
} |
217 |
else if (!strcasecmp (arr[i], "root")) |
218 |
{ |
219 |
new_flags |= IM_TILE|IM_ROOT_ALIGN; |
220 |
w = h = noScale; |
221 |
geom_flags |= WidthValue|HeightValue; |
222 |
} |
223 |
|
224 |
else |
225 |
geom_flags |= XParseGeometry (arr[i], &x, &y, &w, &h); |
226 |
} /* done parsing ops */ |
227 |
|
228 |
rxvt_free_strsplit (arr); |
229 |
} |
230 |
|
231 |
new_flags |= flags & ~IM_GEOMETRY_FLAGS; |
232 |
|
233 |
if (!update) |
234 |
{ |
235 |
if (!(geom_flags & XValue)) |
236 |
x = y = defaultAlign; |
237 |
else if (!(geom_flags & YValue)) |
238 |
y = x; |
239 |
|
240 |
if (!(geom_flags & (WidthValue|HeightValue))) |
241 |
w = h = defaultScale; |
242 |
else if (!(geom_flags & HeightValue)) |
243 |
h = w; |
244 |
else if (!(geom_flags & WidthValue)) |
245 |
w = h; |
246 |
} |
247 |
|
248 |
clamp_it (x, -100, 200); |
249 |
clamp_it (y, -100, 200); |
250 |
|
251 |
if (flags != new_flags |
252 |
|| h_scale != w |
253 |
|| v_scale != h |
254 |
|| h_align != x |
255 |
|| v_align != y) |
256 |
{ |
257 |
flags = new_flags; |
258 |
h_scale = w; |
259 |
v_scale = h; |
260 |
h_align = x; |
261 |
v_align = y; |
262 |
changed = true; |
263 |
} |
264 |
|
265 |
if (is_size_sensitive ()) |
266 |
flags |= IM_IS_SIZE_SENSITIVE; |
267 |
else |
268 |
flags &= ~IM_IS_SIZE_SENSITIVE; |
269 |
|
270 |
return changed; |
271 |
} |
272 |
|
273 |
void |
274 |
rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y) |
275 |
{ |
276 |
int image_width = image.img->w; |
277 |
int image_height = image.img->h; |
278 |
int target_width = szHint.width; |
279 |
int target_height = szHint.height; |
280 |
int h_scale = min (image.h_scale, 32767 * 100 / target_width); |
281 |
int v_scale = min (image.v_scale, 32767 * 100 / target_height); |
282 |
|
283 |
w = h_scale * target_width / 100; |
284 |
h = v_scale * target_height / 100; |
285 |
|
286 |
if (image.flags & IM_KEEP_ASPECT) |
287 |
{ |
288 |
float scale = (float)w / image_width; |
289 |
min_it (scale, (float)h / image_height); |
290 |
w = image_width * scale + 0.5; |
291 |
h = image_height * scale + 0.5; |
292 |
} |
293 |
|
294 |
if (!w) w = image_width; |
295 |
if (!h) h = image_height; |
296 |
|
297 |
if (image.flags & IM_ROOT_ALIGN) |
298 |
{ |
299 |
x = -target_x; |
300 |
y = -target_y; |
301 |
} |
302 |
else |
303 |
{ |
304 |
x = make_align_position (image.h_align, target_width, w); |
305 |
y = make_align_position (image.v_align, target_height, h); |
306 |
} |
307 |
} |
308 |
|
309 |
bool |
310 |
rxvt_term::render_image (rxvt_image &image) |
311 |
{ |
312 |
int target_width = szHint.width; |
313 |
int target_height = szHint.height; |
314 |
|
315 |
int x = 0; |
316 |
int y = 0; |
317 |
int w = 0; |
318 |
int h = 0; |
319 |
|
320 |
get_image_geometry (image, w, h, x, y); |
321 |
|
322 |
if (!(image.flags & IM_ROOT_ALIGN) |
323 |
&& (x >= target_width |
324 |
|| y >= target_height |
325 |
|| x + w <= 0 |
326 |
|| y + h <= 0)) |
327 |
return false; |
328 |
|
329 |
rxvt_img *img = image.img->scale (w, h); |
330 |
|
331 |
if (image.flags & IM_TILE) |
332 |
img->repeat_mode (RepeatNormal); |
333 |
else |
334 |
img->repeat_mode (RepeatNone); |
335 |
img->sub_rect (-x, -y, target_width, target_height)->replace (img); |
336 |
|
337 |
if (bg_flags & BG_IS_VALID) |
338 |
{ |
339 |
double factor = image.alpha * 1. / 0xffff; |
340 |
bg_img->blend (img, factor)->replace (img); |
341 |
} |
342 |
|
343 |
XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); |
344 |
img->convert_format (format, pix_colors [Color_bg])->replace (img); |
345 |
|
346 |
delete bg_img; |
347 |
bg_img = img; |
348 |
|
349 |
return true; |
350 |
} |
351 |
|
352 |
rxvt_image::rxvt_image () |
353 |
{ |
354 |
alpha = 0xffff; |
355 |
flags = 0; |
356 |
h_scale = |
357 |
v_scale = defaultScale; |
358 |
h_align = |
359 |
v_align = defaultAlign; |
360 |
|
361 |
img = 0; |
362 |
} |
363 |
|
364 |
bool |
365 |
rxvt_image::set_file_geometry (rxvt_screen *s, const char *file) |
366 |
{ |
367 |
if (!file || !*file) |
368 |
return false; |
369 |
|
370 |
const char *p = strchr (file, ';'); |
371 |
|
372 |
if (p) |
373 |
{ |
374 |
size_t len = p - file; |
375 |
char *f = rxvt_temp_buf<char> (len + 1); |
376 |
memcpy (f, file, len); |
377 |
f[len] = '\0'; |
378 |
file = f; |
379 |
} |
380 |
|
381 |
bool ret = set_file (s, file); |
382 |
alpha = 0x8000; |
383 |
if (ret) |
384 |
set_geometry (p ? p + 1 : ""); |
385 |
return ret; |
386 |
} |
387 |
|
388 |
bool |
389 |
rxvt_image::set_file (rxvt_screen *s, const char *file) |
390 |
{ |
391 |
delete img; |
392 |
img = rxvt_img::new_from_file (s, file); |
393 |
return img != 0; |
394 |
} |
395 |
|
396 |
# endif /* BG_IMAGE_FROM_FILE */ |
397 |
|
398 |
bool |
399 |
image_effects::set_blur (const char *geom) |
400 |
{ |
401 |
bool changed = false; |
402 |
unsigned int hr, vr; |
403 |
int junk; |
404 |
int geom_flags = XParseGeometry (geom, &junk, &junk, &hr, &vr); |
405 |
|
406 |
if (!(geom_flags & WidthValue)) |
407 |
hr = 1; |
408 |
if (!(geom_flags & HeightValue)) |
409 |
vr = hr; |
410 |
|
411 |
min_it (hr, 128); |
412 |
min_it (vr, 128); |
413 |
|
414 |
if (h_blurRadius != hr) |
415 |
{ |
416 |
changed = true; |
417 |
h_blurRadius = hr; |
418 |
} |
419 |
|
420 |
if (v_blurRadius != vr) |
421 |
{ |
422 |
changed = true; |
423 |
v_blurRadius = vr; |
424 |
} |
425 |
|
426 |
return changed; |
427 |
} |
428 |
|
429 |
bool |
430 |
image_effects::set_tint (const rxvt_color &new_tint) |
431 |
{ |
432 |
if (!tint_set || tint != new_tint) |
433 |
{ |
434 |
tint = new_tint; |
435 |
tint_set = true; |
436 |
|
437 |
return true; |
438 |
} |
439 |
|
440 |
return false; |
441 |
} |
442 |
|
443 |
bool |
444 |
image_effects::set_shade (const char *shade_str) |
445 |
{ |
446 |
int new_shade = atoi (shade_str); |
447 |
|
448 |
clamp_it (new_shade, -100, 200); |
449 |
if (new_shade < 0) |
450 |
new_shade = 200 - (100 + new_shade); |
451 |
|
452 |
if (new_shade != shade) |
453 |
{ |
454 |
shade = new_shade; |
455 |
return true; |
456 |
} |
457 |
|
458 |
return false; |
459 |
} |
460 |
|
461 |
# if ENABLE_TRANSPARENCY |
462 |
/* |
463 |
* Builds a pixmap of the same size as the terminal window that contains |
464 |
* the tiled portion of the root pixmap that is supposed to be covered by |
465 |
* our window. |
466 |
*/ |
467 |
bool |
468 |
rxvt_term::render_root_image () |
469 |
{ |
470 |
/* root dimensions may change from call to call - but Display structure should |
471 |
* be always up-to-date, so let's use it : |
472 |
*/ |
473 |
int screen = display->screen; |
474 |
int root_width = DisplayWidth (dpy, screen); |
475 |
int root_height = DisplayHeight (dpy, screen); |
476 |
int window_width = szHint.width; |
477 |
int window_height = szHint.height; |
478 |
int sx, sy; |
479 |
|
480 |
sx = target_x; |
481 |
sy = target_y; |
482 |
|
483 |
if (!root_img) |
484 |
return false; |
485 |
|
486 |
/* check if we are outside of the visible part of the virtual screen : */ |
487 |
if (sx + window_width <= 0 || sy + window_height <= 0 |
488 |
|| sx >= root_width || sy >= root_height) |
489 |
return 0; |
490 |
|
491 |
while (sx < 0) sx += root_img->w; |
492 |
while (sy < 0) sy += root_img->h; |
493 |
|
494 |
rxvt_img *img = root_img->sub_rect (sx, sy, window_width, window_height); |
495 |
|
496 |
if (root_effects.need_blur ()) |
497 |
img->blur (root_effects.h_blurRadius, root_effects.v_blurRadius)->replace (img); |
498 |
|
499 |
if (root_effects.need_tint ()) |
500 |
tint_image (img, root_effects.tint, root_effects.tint_set, root_effects.shade); |
501 |
|
502 |
XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); |
503 |
img->convert_format (format, pix_colors [Color_bg])->replace (img); |
504 |
|
505 |
delete bg_img; |
506 |
bg_img = img; |
507 |
|
508 |
return true; |
509 |
} |
510 |
|
511 |
void |
512 |
rxvt_term::bg_set_root_pixmap () |
513 |
{ |
514 |
delete root_img; |
515 |
root_img = rxvt_img::new_from_root (this); |
516 |
} |
517 |
# endif /* ENABLE_TRANSPARENCY */ |
518 |
|
519 |
void |
520 |
rxvt_term::bg_render () |
521 |
{ |
522 |
if (bg_flags & BG_INHIBIT_RENDER) |
523 |
return; |
524 |
|
525 |
bg_invalidate (); |
526 |
# if ENABLE_TRANSPARENCY |
527 |
if (bg_flags & BG_IS_TRANSPARENT) |
528 |
{ |
529 |
/* we need to re-generate transparency pixmap in that case ! */ |
530 |
if (render_root_image ()) |
531 |
bg_flags |= BG_IS_VALID; |
532 |
} |
533 |
# endif |
534 |
|
535 |
# if BG_IMAGE_FROM_FILE |
536 |
if (fimage.img) |
537 |
{ |
538 |
if (render_image (fimage)) |
539 |
bg_flags |= BG_IS_VALID; |
540 |
} |
541 |
# endif |
542 |
|
543 |
if (!(bg_flags & BG_IS_VALID)) |
544 |
{ |
545 |
delete bg_img; |
546 |
bg_img = 0; |
547 |
} |
548 |
|
549 |
scr_recolour (false); |
550 |
bg_flags |= BG_NEEDS_REFRESH; |
551 |
|
552 |
bg_valid_since = ev::now (); |
553 |
} |
554 |
|
555 |
void |
556 |
rxvt_term::bg_init () |
557 |
{ |
558 |
#if BG_IMAGE_FROM_FILE |
559 |
if (rs[Rs_backgroundPixmap]) |
560 |
{ |
561 |
if (fimage.set_file_geometry (this, rs[Rs_backgroundPixmap]) |
562 |
&& !bg_window_position_sensitive ()) |
563 |
update_background (); |
564 |
} |
565 |
#endif |
566 |
} |
567 |
|
568 |
void |
569 |
rxvt_term::tint_image (rxvt_img *img, rxvt_color &tint, bool tint_set, int shade) |
570 |
{ |
571 |
rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); |
572 |
|
573 |
if (tint_set) |
574 |
tint.get (c); |
575 |
|
576 |
if (shade > 100) |
577 |
{ |
578 |
c.r = c.r * (200 - shade) / 100; |
579 |
c.g = c.g * (200 - shade) / 100; |
580 |
c.b = c.b * (200 - shade) / 100; |
581 |
} |
582 |
else |
583 |
{ |
584 |
c.r = c.r * shade / 100; |
585 |
c.g = c.g * shade / 100; |
586 |
c.b = c.b * shade / 100; |
587 |
} |
588 |
|
589 |
img->contrast (c.r, c.g, c.b, c.a); |
590 |
|
591 |
if (shade > 100) |
592 |
{ |
593 |
c.a = 0xffff; |
594 |
c.r = |
595 |
c.g = |
596 |
c.b = 0xffff * (shade - 100) / 100; |
597 |
img->brightness (c.r, c.g, c.b, c.a); |
598 |
} |
599 |
} |
600 |
|
601 |
#endif /* HAVE_BG_PIXMAP */ |