ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/background.C
(Generate patch)

Comparing rxvt-unicode/src/background.C (file contents):
Revision 1.217 by sf-exg, Sun May 20 16:34:42 2012 UTC vs.
Revision 1.222 by sf-exg, Sat May 26 08:10:25 2012 UTC

58# endif 58# endif
59 59
60void 60void
61rxvt_term::bg_destroy () 61rxvt_term::bg_destroy ()
62{ 62{
63# ifdef BG_IMAGE_FROM_FILE
64 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
65 bg_image->destroy ();
66# endif
67
63 if (bg_pixmap) 68 if (bg_pixmap)
64 XFreePixmap (dpy, bg_pixmap); 69 XFreePixmap (dpy, bg_pixmap);
65} 70}
66 71
67bool 72bool
85 if (bg_flags & BG_IS_TRANSPARENT) 90 if (bg_flags & BG_IS_TRANSPARENT)
86 return true; 91 return true;
87# endif 92# endif
88 93
89# ifdef BG_IMAGE_FROM_FILE 94# ifdef BG_IMAGE_FROM_FILE
90 if (bg_image.flags & IM_IS_SET) 95 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
91 { 96 {
92 if ((bg_image.flags & IM_IS_SIZE_SENSITIVE) 97 if ((bg_image->flags & IM_IS_SIZE_SENSITIVE)
93 || bg_image.width () > szHint.width 98 || bg_image->width () > szHint.width
94 || bg_image.height () > szHint.height) 99 || bg_image->height () > szHint.height)
95 return true; 100 return true;
96 } 101 }
97# endif 102# endif
98 103
99 return false; 104 return false;
106 if (bg_flags & BG_IS_TRANSPARENT) 111 if (bg_flags & BG_IS_TRANSPARENT)
107 return true; 112 return true;
108# endif 113# endif
109 114
110# ifdef BG_IMAGE_FROM_FILE 115# ifdef BG_IMAGE_FROM_FILE
111 if (bg_image.flags & IM_IS_SET) 116 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
112 { 117 {
113 if (bg_image.flags & IM_ROOT_ALIGN) 118 if (bg_image->flags & IM_ROOT_ALIGN)
114 return true; 119 return true;
115 } 120 }
116# endif 121# endif
117 122
118 return false; 123 return false;
145 150
146 min_it (dst_size, target_size - dst_pos); 151 min_it (dst_size, target_size - dst_pos);
147 return src_pos; 152 return src_pos;
148} 153}
149 154
155static void
156parse_style (const char *style, int &x, int &y, unsigned int &w, unsigned int &h, uint8_t &flags)
157{
158 if (!strcasecmp (style, "tiled"))
159 {
160 flags = IM_TILE;
161 w = h = noScale;
162 x = y = 0;
163 }
164 else if (!strcasecmp (style, "aspect-stretched"))
165 {
166 flags = IM_KEEP_ASPECT;
167 w = h = windowScale;
168 x = y = centerAlign;
169 }
170 else if (!strcasecmp (style, "stretched"))
171 {
172 flags = 0;
173 w = h = windowScale;
174 x = y = centerAlign;
175 }
176 else if (!strcasecmp (style, "centered"))
177 {
178 flags = 0;
179 w = h = noScale;
180 x = y = centerAlign;
181 }
182 else if (!strcasecmp (style, "root-tiled"))
183 {
184 flags = IM_TILE|IM_ROOT_ALIGN;
185 w = h = noScale;
186 x = y = 0;
187 }
188}
189
150bool 190bool
151rxvt_image::set_geometry (const char *geom, bool update) 191rxvt_image::set_geometry (const char *geom, bool update)
152{ 192{
153 bool changed = false; 193 bool changed = false;
154 int geom_flags = 0; 194 int geom_flags = 0;
155 int x = h_align; 195 int x = h_align;
156 int y = v_align; 196 int y = v_align;
157 unsigned int w = h_scale; 197 unsigned int w = h_scale;
158 unsigned int h = v_scale; 198 unsigned int h = v_scale;
159 unsigned long new_flags = 0; 199 uint8_t new_flags = 0;
160 200
161 if (geom == NULL) 201 if (geom == NULL)
162 return false; 202 return false;
163 203
164 if (geom[0]) 204 if (geom[0])
165 { 205 {
166 char **arr = rxvt_strsplit (':', geom); 206 char **arr = rxvt_strsplit (':', geom);
167 207
168 for (int i = 0; arr[i]; i++) 208 for (int i = 0; arr[i]; i++)
169 { 209 {
170 if (!strcasecmp (arr[i], "style=tiled")) 210 if (!strncasecmp (arr[i], "style=", 6))
171 { 211 {
172 new_flags = IM_TILE; 212 parse_style (arr[i] + 6, x, y, w, h, new_flags);
173 w = h = noScale;
174 x = y = 0;
175 geom_flags = WidthValue|HeightValue|XValue|YValue; 213 geom_flags = WidthValue|HeightValue|XValue|YValue;
176 }
177 else if (!strcasecmp (arr[i], "style=aspect-stretched"))
178 {
179 new_flags = IM_KEEP_ASPECT;
180 w = h = windowScale;
181 x = y = centerAlign;
182 geom_flags = WidthValue|HeightValue|XValue|YValue;
183 }
184 else if (!strcasecmp (arr[i], "style=stretched"))
185 {
186 new_flags = 0;
187 w = h = windowScale;
188 geom_flags = WidthValue|HeightValue;
189 }
190 else if (!strcasecmp (arr[i], "style=centered"))
191 {
192 new_flags = 0;
193 w = h = noScale;
194 x = y = centerAlign;
195 geom_flags = WidthValue|HeightValue|XValue|YValue;
196 }
197 else if (!strcasecmp (arr[i], "style=root-tiled"))
198 {
199 new_flags = IM_TILE|IM_ROOT_ALIGN;
200 w = h = noScale;
201 geom_flags = WidthValue|HeightValue;
202 } 214 }
203 else if (!strcasecmp (arr[i], "op=tile")) 215 else if (!strcasecmp (arr[i], "op=tile"))
204 new_flags |= IM_TILE; 216 new_flags |= IM_TILE;
205 else if (!strcasecmp (arr[i], "op=keep-aspect")) 217 else if (!strcasecmp (arr[i], "op=keep-aspect"))
206 new_flags |= IM_KEEP_ASPECT; 218 new_flags |= IM_KEEP_ASPECT;
341 353
342# ifdef HAVE_PIXBUF 354# ifdef HAVE_PIXBUF
343bool 355bool
344rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc, 356rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc,
345 int src_x, int src_y, int dst_x, int dst_y, 357 int src_x, int src_y, int dst_x, int dst_y,
346 unsigned int width, unsigned int height) 358 unsigned int width, unsigned int height, bool argb)
347{ 359{
348 XImage *ximage; 360 XImage *ximage;
349 char *line; 361 char *line;
350 int width_r, width_g, width_b, width_a; 362 int width_r, width_g, width_b, width_a;
351 int sh_r, sh_g, sh_b, sh_a; 363 int sh_r, sh_g, sh_b, sh_a;
352 uint32_t alpha_mask; 364 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
353 int rowstride; 365 int rowstride;
354 int channels; 366 int channels;
355 unsigned char *row; 367 unsigned char *row;
356 368
357 if (visual->c_class != TrueColor) 369 if (visual->c_class != TrueColor)
358 return false; 370 return false;
359 371
372 if (argb)
373 {
374 red_mask = 0xff << 16;
375 green_mask = 0xff << 8;
376 blue_mask = 0xff;
377 alpha_mask = 0xff << 24;
378 }
379 else
380 {
381 red_mask = visual->red_mask;
382 green_mask = visual->green_mask;
383 blue_mask = visual->blue_mask;
360#if XRENDER 384#if XRENDER
361 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 385 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
362 if (format) 386 if (format)
363 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; 387 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
364 else 388 else
365#endif 389#endif
366 alpha_mask = 0; 390 alpha_mask = 0;
391 }
367 392
368 width_r = ecb_popcount32 (visual->red_mask); 393 width_r = ecb_popcount32 (red_mask);
369 width_g = ecb_popcount32 (visual->green_mask); 394 width_g = ecb_popcount32 (green_mask);
370 width_b = ecb_popcount32 (visual->blue_mask); 395 width_b = ecb_popcount32 (blue_mask);
371 width_a = ecb_popcount32 (alpha_mask); 396 width_a = ecb_popcount32 (alpha_mask);
372 397
373 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) 398 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8)
374 return false; 399 return false;
375 400
376 sh_r = ecb_ctz32 (visual->red_mask); 401 sh_r = ecb_ctz32 (red_mask);
377 sh_g = ecb_ctz32 (visual->green_mask); 402 sh_g = ecb_ctz32 (green_mask);
378 sh_b = ecb_ctz32 (visual->blue_mask); 403 sh_b = ecb_ctz32 (blue_mask);
379 sh_a = ecb_ctz32 (alpha_mask); 404 sh_a = ecb_ctz32 (alpha_mask);
380 405
381 if (width > 32767 || height > 32767) 406 if (width > 32767 || height > 32767)
382 return false; 407 return false;
383 408
384 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, 0, 409 ximage = XCreateImage (dpy, visual, argb ? 32 : depth, ZPixmap, 0, 0,
385 width, height, 32, 0); 410 width, height, 32, 0);
386 if (!ximage) 411 if (!ximage)
387 return false; 412 return false;
388 413
389 if (height > INT_MAX / ximage->bytes_per_line 414 if (height > INT_MAX / ximage->bytes_per_line
511 536
512 image_width = gdk_pixbuf_get_width (result); 537 image_width = gdk_pixbuf_get_width (result);
513 image_height = gdk_pixbuf_get_height (result); 538 image_height = gdk_pixbuf_get_height (result);
514 539
515 if (need_blend) 540 if (need_blend)
516 { 541 tmp_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, 32);
517 tmp_pixmap = bg_pixmap;
518 bg_pixmap = None;
519 }
520 else 542 else
521 { 543 {
544 // optimise bg pixmap size when tiling, but only if there are no
545 // other pixbufs to render. Otherwise, the bg pixmap size must
546 // be equal to the window size.
522 if (image.flags & IM_TILE) 547 if ((image.flags & IM_TILE)
548 && image_vec.size () == 1)
523 { 549 {
524 new_pmap_width = min (image_width, target_width); 550 new_pmap_width = min (image_width, target_width);
525 new_pmap_height = min (image_height, target_height); 551 new_pmap_height = min (image_height, target_height);
526 } 552 }
527 }
528 553
529 if (bg_pixmap == None 554 if (bg_pixmap == None
530 || bg_pmap_width != new_pmap_width 555 || bg_pmap_width != new_pmap_width
531 || bg_pmap_height != new_pmap_height) 556 || bg_pmap_height != new_pmap_height)
532 { 557 {
533 if (bg_pixmap) 558 if (bg_pixmap)
534 XFreePixmap (dpy, bg_pixmap); 559 XFreePixmap (dpy, bg_pixmap);
535 bg_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, depth); 560 bg_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, depth);
536 bg_pmap_width = new_pmap_width; 561 bg_pmap_width = new_pmap_width;
537 bg_pmap_height = new_pmap_height; 562 bg_pmap_height = new_pmap_height;
563 }
564
565 tmp_pixmap = bg_pixmap;
538 } 566 }
539 567
540 gcv.foreground = pix_colors[Color_bg]; 568 gcv.foreground = pix_colors[Color_bg];
541 gc = XCreateGC (dpy, vt, GCForeground, &gcv); 569 gc = XCreateGC (dpy, tmp_pixmap, GCForeground, &gcv);
542 570
543 if (gc) 571 if (gc)
544 { 572 {
545 if (image.flags & IM_TILE) 573 if (image.flags & IM_TILE)
546 { 574 {
547 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, depth); 575 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, need_blend ? 32 : depth);
548 pixbuf_to_pixmap (result, tile, gc, 576 pixbuf_to_pixmap (result, tile, gc,
549 0, 0, 577 0, 0,
550 0, 0, 578 0, 0,
551 image_width, image_height); 579 image_width, image_height, need_blend);
552 580
553 gcv.tile = tile; 581 gcv.tile = tile;
554 gcv.fill_style = FillTiled; 582 gcv.fill_style = FillTiled;
555 gcv.ts_x_origin = x; 583 gcv.ts_x_origin = x;
556 gcv.ts_y_origin = y; 584 gcv.ts_y_origin = y;
557 XChangeGC (dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 585 XChangeGC (dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
558 586
559 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height); 587 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
560 XFreePixmap (dpy, tile); 588 XFreePixmap (dpy, tile);
561 } 589 }
562 else 590 else
563 { 591 {
564 int src_x, src_y, dst_x, dst_y; 592 int src_x, src_y, dst_x, dst_y;
568 src_y = make_clip_rectangle (y, image_height, new_pmap_height, dst_y, dst_height); 596 src_y = make_clip_rectangle (y, image_height, new_pmap_height, dst_y, dst_height);
569 597
570 if (dst_x > 0 || dst_y > 0 598 if (dst_x > 0 || dst_y > 0
571 || dst_x + dst_width < new_pmap_width 599 || dst_x + dst_width < new_pmap_width
572 || dst_y + dst_height < new_pmap_height) 600 || dst_y + dst_height < new_pmap_height)
573 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height); 601 XFillRectangle (dpy, tmp_pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
574 602
575 if (dst_x < new_pmap_width && dst_y < new_pmap_height) 603 if (dst_x < new_pmap_width && dst_y < new_pmap_height)
576 pixbuf_to_pixmap (result, bg_pixmap, gc, 604 pixbuf_to_pixmap (result, tmp_pixmap, gc,
577 src_x, src_y, 605 src_x, src_y,
578 dst_x, dst_y, 606 dst_x, dst_y,
579 dst_width, dst_height); 607 dst_width, dst_height, need_blend);
580 } 608 }
581 609
582#if XRENDER 610#if XRENDER
583 if (need_blend) 611 if (need_blend)
584 { 612 {
613 XRenderPictFormat *argb_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
585 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 614 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
586 615
587 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, format, 0, 0); 616 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, argb_format, 0, 0);
588 617
589 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0); 618 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0);
590 619
591 Picture mask = create_xrender_mask (dpy, vt, False, False); 620 Picture mask = create_xrender_mask (dpy, vt, False, False);
592 621
593 XRenderColor mask_c; 622 XRenderColor mask_c;
594 623
595 mask_c.alpha = 0x8000; 624 mask_c.alpha = gdk_pixbuf_get_has_alpha (image.pixbuf) ? 0xffff : image.alpha;
596 mask_c.red = 625 mask_c.red =
597 mask_c.green = 626 mask_c.green =
598 mask_c.blue = 0; 627 mask_c.blue = 0;
599 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); 628 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
600 629
619 648
620 return ret; 649 return ret;
621} 650}
622# endif /* HAVE_PIXBUF */ 651# endif /* HAVE_PIXBUF */
623 652
653# ifndef NO_RESOURCES
654static int
655rxvt_define_image (XrmDatabase *database ecb_unused,
656 XrmBindingList bindings ecb_unused,
657 XrmQuarkList quarks,
658 XrmRepresentation *type ecb_unused,
659 XrmValue *value,
660 XPointer closure ecb_unused)
661{
662 int size;
663
664 for (size = 0; quarks[size] != NULLQUARK; size++)
665 ;
666
667 if (size >= 2)
668 {
669 int id = strtol (XrmQuarkToString (quarks[size-2]), 0, 0);
670 if (id >= 1)
671 GET_R->parse_image (id, XrmQuarkToString (quarks[size-1]), (char *)value->addr);
672 }
673 return False;
674}
675
676void
677rxvt_term::parse_image (int id, const char *type, const char *arg)
678{
679 if (image_vec.size () < id + 1)
680 image_vec.resize (id + 1);
681
682 rxvt_image *image = &image_vec[id];
683}
684# endif
685
686rxvt_image::rxvt_image ()
687{
688 alpha =
689 flags =
690 h_scale =
691 v_scale =
692 h_align =
693 v_align = 0;
694
695# ifdef HAVE_PIXBUF
696 pixbuf = 0;
697# endif
698}
699
624bool 700bool
625rxvt_image::set_file (const char *file) 701rxvt_image::set_file_geometry (const char *file)
626{ 702{
627 if (!file || !*file) 703 if (!file || !*file)
628 return false; 704 return false;
629 705
630 bool ret = false;
631 const char *p = strchr (file, ';'); 706 const char *p = strchr (file, ';');
632 707
633 if (p) 708 if (p)
634 { 709 {
635 size_t len = p - file; 710 size_t len = p - file;
637 memcpy (f, file, len); 712 memcpy (f, file, len);
638 f[len] = '\0'; 713 f[len] = '\0';
639 file = f; 714 file = f;
640 } 715 }
641 716
717 bool ret = set_file (file);
718 alpha = 0x8000;
719 if (ret && p)
720 set_geometry (p + 1);
721 return ret;
722}
723
724bool
725rxvt_image::set_file (const char *file)
726{
727 bool ret = false;
728
642# ifdef HAVE_PIXBUF 729# ifdef HAVE_PIXBUF
643 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL); 730 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL);
644 if (image) 731 if (image)
645 { 732 {
646 if (pixbuf) 733 if (pixbuf)
650 } 737 }
651# endif 738# endif
652 739
653 if (ret) 740 if (ret)
654 { 741 {
742 alpha = 0xffff;
655 flags = IM_IS_SET | IM_IS_SIZE_SENSITIVE; 743 flags = IM_IS_SET | IM_IS_SIZE_SENSITIVE;
656 h_scale = v_scale = defaultScale; 744 h_scale = v_scale = defaultScale;
657 h_align = v_align = defaultAlign; 745 h_align = v_align = defaultAlign;
658
659 if (p)
660 set_geometry (p + 1);
661 } 746 }
662 747
663 return ret; 748 return ret;
664} 749}
665 750
1078 bg_flags |= BG_IS_VALID; 1163 bg_flags |= BG_IS_VALID;
1079 } 1164 }
1080# endif 1165# endif
1081 1166
1082# ifdef BG_IMAGE_FROM_FILE 1167# ifdef BG_IMAGE_FROM_FILE
1083 if (bg_image.flags & IM_IS_SET) 1168 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
1084 { 1169 {
1085 if (render_image (bg_image)) 1170 if (render_image (*bg_image))
1086 bg_flags |= BG_IS_VALID; 1171 bg_flags |= BG_IS_VALID;
1087 } 1172 }
1088# endif 1173# endif
1089 1174
1090 if (!(bg_flags & BG_IS_VALID)) 1175 if (!(bg_flags & BG_IS_VALID))
1123 if (!strcmp (filters->filter[i], FilterConvolution)) 1208 if (!strcmp (filters->filter[i], FilterConvolution))
1124 bg_flags |= BG_HAS_RENDER_CONV; 1209 bg_flags |= BG_HAS_RENDER_CONV;
1125 1210
1126 XFree (filters); 1211 XFree (filters);
1127 } 1212 }
1213#endif
1214
1215#ifdef BG_IMAGE_FROM_FILE
1216 if (rs[Rs_backgroundPixmap])
1217 {
1218 rxvt_image *image = new_image ();
1219 if (!image->set_file_geometry (rs[Rs_backgroundPixmap]))
1220 image_vec.pop_back ();
1221 }
1222
1223# ifndef NO_RESOURCES
1224 find_resources ("image", "Image", XrmEnumAllLevels, rxvt_define_image);
1225 vector<rxvt_image>::iterator bg_image = image_vec.begin ();
1226 while (bg_image != image_vec.end ())
1227 {
1228 if (!(bg_image->flags & IM_IS_SET))
1229 bg_image = image_vec.erase (bg_image);
1230 else
1231 bg_image++;
1232 }
1233# endif
1234
1235 if (image_vec.size () > 0
1236 && !bg_window_position_sensitive ())
1237 update_background ();
1128#endif 1238#endif
1129} 1239}
1130 1240
1131#endif /* HAVE_BG_PIXMAP */ 1241#endif /* HAVE_BG_PIXMAP */
1132 1242

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines