--- rxvt-unicode/src/background.C 2012/05/21 12:29:22 1.218 +++ rxvt-unicode/src/background.C 2012/06/03 15:50:22 1.230 @@ -60,6 +60,11 @@ void rxvt_term::bg_destroy () { +# ifdef BG_IMAGE_FROM_FILE + for (vector::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++) + bg_image->destroy (); +# endif + if (bg_pixmap) XFreePixmap (dpy, bg_pixmap); } @@ -87,11 +92,11 @@ # endif # ifdef BG_IMAGE_FROM_FILE - if (bg_image.flags & IM_IS_SET) + for (vector::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++) { - if ((bg_image.flags & IM_IS_SIZE_SENSITIVE) - || bg_image.width () > szHint.width - || bg_image.height () > szHint.height) + if ((bg_image->flags & IM_IS_SIZE_SENSITIVE) + || bg_image->width () > szHint.width + || bg_image->height () > szHint.height) return true; } # endif @@ -108,9 +113,9 @@ # endif # ifdef BG_IMAGE_FROM_FILE - if (bg_image.flags & IM_IS_SET) + for (vector::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++) { - if (bg_image.flags & IM_ROOT_ALIGN) + if (bg_image->flags & IM_ROOT_ALIGN) return true; } # endif @@ -147,6 +152,41 @@ return src_pos; } +static void +parse_style (const char *style, int &x, int &y, unsigned int &w, unsigned int &h, uint8_t &flags) +{ + if (!strcasecmp (style, "tiled")) + { + flags = IM_TILE; + w = h = noScale; + x = y = 0; + } + else if (!strcasecmp (style, "aspect-stretched")) + { + flags = IM_KEEP_ASPECT; + w = h = windowScale; + x = y = centerAlign; + } + else if (!strcasecmp (style, "stretched")) + { + flags = 0; + w = h = windowScale; + x = y = centerAlign; + } + else if (!strcasecmp (style, "centered")) + { + flags = 0; + w = h = noScale; + x = y = centerAlign; + } + else if (!strcasecmp (style, "root-tiled")) + { + flags = IM_TILE|IM_ROOT_ALIGN; + w = h = noScale; + x = y = 0; + } +} + bool rxvt_image::set_geometry (const char *geom, bool update) { @@ -156,7 +196,7 @@ int y = v_align; unsigned int w = h_scale; unsigned int h = v_scale; - unsigned long new_flags = 0; + uint8_t new_flags = 0; if (geom == NULL) return false; @@ -167,39 +207,11 @@ for (int i = 0; arr[i]; i++) { - if (!strcasecmp (arr[i], "style=tiled")) + if (!strncasecmp (arr[i], "style=", 6)) { - new_flags = IM_TILE; - w = h = noScale; - x = y = 0; + parse_style (arr[i] + 6, x, y, w, h, new_flags); geom_flags = WidthValue|HeightValue|XValue|YValue; } - else if (!strcasecmp (arr[i], "style=aspect-stretched")) - { - new_flags = IM_KEEP_ASPECT; - w = h = windowScale; - x = y = centerAlign; - geom_flags = WidthValue|HeightValue|XValue|YValue; - } - else if (!strcasecmp (arr[i], "style=stretched")) - { - new_flags = 0; - w = h = windowScale; - geom_flags = WidthValue|HeightValue; - } - else if (!strcasecmp (arr[i], "style=centered")) - { - new_flags = 0; - w = h = noScale; - x = y = centerAlign; - geom_flags = WidthValue|HeightValue|XValue|YValue; - } - else if (!strcasecmp (arr[i], "style=root-tiled")) - { - new_flags = IM_TILE|IM_ROOT_ALIGN; - w = h = noScale; - geom_flags = WidthValue|HeightValue; - } else if (!strcasecmp (arr[i], "op=tile")) new_flags |= IM_TILE; else if (!strcasecmp (arr[i], "op=keep-aspect")) @@ -293,9 +305,7 @@ changed = true; } - if (!(flags & IM_TILE) - || h_scale || v_scale - || (!(flags & IM_ROOT_ALIGN) && (h_align || v_align))) + if (is_size_sensitive ()) flags |= IM_IS_SIZE_SENSITIVE; else flags &= ~IM_IS_SIZE_SENSITIVE; @@ -476,7 +486,7 @@ bool need_blend = bg_flags & BG_IS_VALID; if (need_blend - && !(bg_flags & BG_HAS_RENDER)) + && !(display->flags & DISPLAY_HAS_RENDER)) return false; GdkPixbuf *result; @@ -529,7 +539,11 @@ tmp_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, 32); else { - if (image.flags & IM_TILE) + // optimise bg pixmap size when tiling, but only if there are no + // other pixbufs to render. Otherwise, the bg pixmap size must + // be equal to the window size. + if ((image.flags & IM_TILE) + && image_vec.size () == 1) { new_pmap_width = min (image_width, target_width); new_pmap_height = min (image_height, target_height); @@ -591,6 +605,11 @@ dst_width, dst_height, need_blend); } + if (image.need_blur ()) + blur_pixmap (tmp_pixmap, new_pmap_width, new_pmap_height, need_blend, image.h_blurRadius, image.v_blurRadius); + if (image.need_tint ()) + tint_pixmap (tmp_pixmap, new_pmap_width, new_pmap_height, need_blend, image.tint, image.tint_set, image.shade); + #if XRENDER if (need_blend) { @@ -605,7 +624,7 @@ XRenderColor mask_c; - mask_c.alpha = 0x8000; + mask_c.alpha = image.alpha; mask_c.red = mask_c.green = mask_c.blue = 0; @@ -634,13 +653,59 @@ } # endif /* HAVE_PIXBUF */ +# ifndef NO_RESOURCES +static int +rxvt_define_image (XrmDatabase *database ecb_unused, + XrmBindingList bindings ecb_unused, + XrmQuarkList quarks, + XrmRepresentation *type ecb_unused, + XrmValue *value, + XPointer closure ecb_unused) +{ + int size; + + for (size = 0; quarks[size] != NULLQUARK; size++) + ; + + if (size >= 2) + { + int id = strtol (XrmQuarkToString (quarks[size-2]), 0, 0); + if (id >= 1) + GET_R->parse_image (id, XrmQuarkToString (quarks[size-1]), (char *)value->addr); + } + return False; +} +# endif + +void +rxvt_term::parse_image (int id, const char *type, const char *arg) +{ + if (image_vec.size () < id + 1) + image_vec.resize (id + 1); + + rxvt_image *image = &image_vec[id]; +} + +rxvt_image::rxvt_image () +{ + alpha = 0xffff; + flags = 0; + h_scale = + v_scale = defaultScale; + h_align = + v_align = defaultAlign; + +# ifdef HAVE_PIXBUF + pixbuf = 0; +# endif +} + bool -rxvt_image::set_file (const char *file) +rxvt_image::set_file_geometry (const char *file) { if (!file || !*file) return false; - bool ret = false; const char *p = strchr (file, ';'); if (p) @@ -652,6 +717,18 @@ file = f; } + bool ret = set_file (file); + alpha = 0x8000; + if (ret) + set_geometry (p ? p + 1 : ""); + return ret; +} + +bool +rxvt_image::set_file (const char *file) +{ + bool ret = false; + # ifdef HAVE_PIXBUF GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL); if (image) @@ -664,23 +741,15 @@ # endif if (ret) - { - flags = IM_IS_SET | IM_IS_SIZE_SENSITIVE; - h_scale = v_scale = defaultScale; - h_align = v_align = defaultAlign; - - if (p) - set_geometry (p + 1); - } + flags |= IM_IS_SET; return ret; } # endif /* BG_IMAGE_FROM_FILE */ -# ifdef ENABLE_TRANSPARENCY bool -rxvt_term::bg_set_blur (const char *geom) +image_effects::set_blur (const char *geom) { bool changed = false; unsigned int hr, vr; @@ -711,21 +780,12 @@ } bool -rxvt_term::bg_set_tint (rxvt_color &new_tint) +image_effects::set_tint (const rxvt_color &new_tint) { - if (!(bg_flags & BG_TINT_SET) || tint != new_tint) + if (!tint_set || tint != new_tint) { tint = new_tint; - bg_flags |= BG_TINT_SET; - - rgba c; - tint.get (c); - if ((c.r <= 0x00ff || c.r >= 0xff00) - && (c.g <= 0x00ff || c.g >= 0xff00) - && (c.b <= 0x00ff || c.b >= 0xff00)) - bg_flags |= BG_TINT_BITAND; - else - bg_flags &= ~BG_TINT_BITAND; + tint_set = true; return true; } @@ -734,7 +794,7 @@ } bool -rxvt_term::bg_set_shade (const char *shade_str) +image_effects::set_shade (const char *shade_str) { int new_shade = atoi (shade_str); @@ -775,11 +835,11 @@ #endif bool -rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height) +rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height, bool argb, int h_blurRadius, int v_blurRadius) { bool ret = false; #if XRENDER - if (!(bg_flags & BG_HAS_RENDER_CONV)) + if (!(display->flags & DISPLAY_HAS_RENDER_CONV)) return false; int size = max (h_blurRadius, v_blurRadius) * 2 + 1; @@ -787,7 +847,8 @@ XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); XRenderPictureAttributes pa; - XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); + XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32) + : XRenderFindVisualFormat (dpy, visual); pa.repeat = RepeatPad; Picture src = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); @@ -840,11 +901,19 @@ } bool -rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height) +rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height, bool argb, rxvt_color &tint, bool tint_set, int shade) { bool ret = false; - if (shade == 100 && (bg_flags & BG_TINT_BITAND)) + rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); + + if (tint_set) + tint.get (c); + + if (shade == 100 + && (c.r <= 0x00ff || c.r >= 0xff00) + && (c.g <= 0x00ff || c.g >= 0xff00) + && (c.b <= 0x00ff || c.b >= 0xff00)) { XGCValues gcv; GC gc; @@ -864,13 +933,8 @@ } } # if XRENDER - else if (bg_flags & BG_HAS_RENDER) + else if (display->flags & DISPLAY_HAS_RENDER) { - rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); - - if (bg_flags & BG_TINT_SET) - tint.get (c); - if (shade <= 100) { c.r = c.r * shade / 100; @@ -884,7 +948,8 @@ c.b = c.b * (200 - shade) / 100; } - XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); + XRenderPictFormat *format = argb ? XRenderFindStandardFormat (dpy, PictStandardARGB32) + : XRenderFindVisualFormat (dpy, visual); Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, 0); @@ -930,13 +995,14 @@ return ret; } +# ifdef ENABLE_TRANSPARENCY /* * Builds a pixmap of the same size as the terminal window that contains * the tiled portion of the root pixmap that is supposed to be covered by * our window. */ bool -rxvt_term::make_transparency_pixmap () +rxvt_term::render_root_image () { bool ret = false; @@ -982,7 +1048,7 @@ if (root_pixmap != None && root_depth != depth) { #if XRENDER - if (bg_flags & BG_HAS_RENDER) + if (display->flags & DISPLAY_HAS_RENDER) { recoded_root_pmap = XCreatePixmap (dpy, vt, root_pmap_width, root_pmap_height, depth); @@ -1030,34 +1096,33 @@ { XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height); ret = true; - bool need_blur = h_blurRadius && v_blurRadius; - bool need_tint = shade != 100 || (bg_flags & BG_TINT_SET); + bool need_blur = root_effects.need_blur (); + bool need_tint = root_effects.need_tint (); - if (!(bg_flags & BG_CLIENT_RENDER)) + if (need_blur) { - if (need_blur) - { - if (blur_pixmap (bg_pixmap, window_width, window_height)) - need_blur = false; - } - if (need_tint) - { - if (tint_pixmap (bg_pixmap, window_width, window_height)) - need_tint = false; - } - if (need_tint) + if (blur_pixmap (bg_pixmap, window_width, window_height, false, + root_effects.h_blurRadius, root_effects.v_blurRadius)) + need_blur = false; + } + if (need_tint) + { + if (tint_pixmap (bg_pixmap, window_width, window_height, false, + root_effects.tint, root_effects.tint_set, root_effects.shade)) + need_tint = false; + } + if (need_tint) + { + XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap); + if (ximage) { - XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap); - if (ximage) - { - /* our own client-side tinting */ - tint_ximage (ximage); - - XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height); - XDestroyImage (ximage); - } + /* our own client-side tinting */ + tint_ximage (ximage, root_effects.tint, root_effects.tint_set, root_effects.shade); + + XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height); + XDestroyImage (ximage); } - } /* server side rendering completed */ + } XFreeGC (dpy, gc); } @@ -1087,15 +1152,15 @@ if (bg_flags & BG_IS_TRANSPARENT) { /* we need to re-generate transparency pixmap in that case ! */ - if (make_transparency_pixmap ()) + if (render_root_image ()) bg_flags |= BG_IS_VALID; } # endif # ifdef BG_IMAGE_FROM_FILE - if (bg_image.flags & IM_IS_SET) + for (vector::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++) { - if (render_image (bg_image)) + if (render_image (*bg_image)) bg_flags |= BG_IS_VALID; } # endif @@ -1120,30 +1185,38 @@ void rxvt_term::bg_init () { -#ifdef ENABLE_TRANSPARENCY - shade = 100; -#endif +#ifdef BG_IMAGE_FROM_FILE + if (rs[Rs_backgroundPixmap]) + { + rxvt_image *image = new_image (); + if (!image->set_file_geometry (rs[Rs_backgroundPixmap])) + image_vec.pop_back (); + } - bg_flags &= ~(BG_HAS_RENDER | BG_HAS_RENDER_CONV); -#if XRENDER - int major, minor; - if (XRenderQueryVersion (dpy, &major, &minor)) - bg_flags |= BG_HAS_RENDER; - XFilters *filters = XRenderQueryFilters (dpy, vt); - if (filters) - { - for (int i = 0; i < filters->nfilter; i++) - if (!strcmp (filters->filter[i], FilterConvolution)) - bg_flags |= BG_HAS_RENDER_CONV; +# ifndef NO_RESOURCES + find_resources ("image", "Image", XrmEnumAllLevels, rxvt_define_image); +# endif + + vector::iterator bg_image = image_vec.begin (); + while (bg_image != image_vec.end ()) + { + if (!(bg_image->flags & IM_IS_SET)) + bg_image = image_vec.erase (bg_image); + else + { + if (bg_image->is_size_sensitive ()) + bg_image->flags |= IM_IS_SIZE_SENSITIVE; - XFree (filters); + bg_image++; + } } + + if (image_vec.size () > 0 + && !bg_window_position_sensitive ()) + update_background (); #endif } -#endif /* HAVE_BG_PIXMAP */ - -#ifdef ENABLE_TRANSPARENCY /* based on code from aterm-0.4.2 */ static inline void @@ -1159,7 +1232,7 @@ } void -rxvt_term::tint_ximage (XImage *ximage) +rxvt_term::tint_ximage (XImage *ximage, rxvt_color &tint, bool tint_set, int shade) { unsigned int size_r, size_g, size_b; int sh_r, sh_g, sh_b; @@ -1194,7 +1267,7 @@ rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); - if (bg_flags & BG_TINT_SET) + if (tint_set) tint.get (c); /* prepare limits for color transformation (each channel is handled separately) */ @@ -1254,4 +1327,5 @@ free (lookup); } -#endif /* ENABLE_TRANSPARENCY */ + +#endif /* HAVE_BG_PIXMAP */