ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/background.C
Revision: 1.237
Committed: Sun Jun 10 15:01:14 2012 UTC (11 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.236: +2 -0 lines
Log Message:
must have been in a hurry

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