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