ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/background.C
Revision: 1.236
Committed: Sun Jun 10 13:07:56 2012 UTC (11 years, 11 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.235: +60 -716 lines
Log Message:
Switch background.C to rxvtimg api (xrender is now required).

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