ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/background.C
Revision: 1.241
Committed: Mon Jun 11 14:45:53 2012 UTC (11 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.240: +9 -11 lines
Log Message:
Do not abort if loading an image with OSC 20 fails.

File Contents

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