--- rxvt-unicode/src/xpm.C 2007/08/06 22:42:05 1.60 +++ rxvt-unicode/src/xpm.C 2007/08/08 22:39:49 1.63 @@ -74,23 +74,23 @@ bool bgPixmap_t::window_size_sensitive () { -#ifdef XPM_BACKGROUND -#ifdef HAVE_AFTERIMAGE +# ifdef XPM_BACKGROUND +# ifdef HAVE_AFTERIMAGE if (original_asim != NULL) -#endif +# endif { if (h_scale != 0 || v_scale != 0) return true; } -#endif -#ifdef ENABLE_TRANSPARENCY +# endif +# ifdef ENABLE_TRANSPARENCY if (flags & bgPmap_Transparent) return true; -#endif +# endif return false; } -#ifdef XPM_BACKGROUND +# ifdef XPM_BACKGROUND static inline bool check_set_scale_value (int geom_flags, int flag, unsigned int &scale, unsigned int new_value) { @@ -134,7 +134,7 @@ if (align >= 0 && align <= 50) return diff * align / 100; else if (align > 50 && align <= 100) - return window_size - image_size + diff * align / 100; + return window_size - image_size - diff * (100 - align) / 100; else if (align > 100 && align <= 200 ) return ((align - 100) * smaller / 100) + window_size - smaller; else if (align > -100 && align < 0) @@ -142,26 +142,23 @@ return 0; } -static inline void -make_clip_rectangle (int pos, int size, int target_size, int &clip_pos, int &clip_size) +static inline int +make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size) { - if (size <= 0) - { /* special case - tiling */ - clip_pos = pos; - clip_size = target_size; - } - else if (pos < 0) - { - clip_pos = 0; - clip_size = MIN (target_size, size + pos); - } - else - { - clip_pos = pos; - clip_size = size; - if (pos < target_size && (int)clip_size > target_size - pos) - clip_pos = target_size - pos; - } + int src_pos = 0; + dst_pos = 0; + dst_size = size; + if (pos < 0 && size > target_size) + { + src_pos = -pos; + dst_size += pos; + } + else if (pos > 0) + dst_pos = pos; + + if (dst_pos + dst_size > target_size) + dst_size = target_size - dst_pos; + return src_pos; } bool @@ -173,7 +170,7 @@ unsigned int n; unsigned long new_flags = (flags&(~bgPmap_geometryFlags)); char *p; -#define MAXLEN_GEOM 256 /* could be longer then regular geometry string */ +# define MAXLEN_GEOM 256 /* could be longer then regular geometry string */ if (geom == NULL) return false; @@ -182,12 +179,13 @@ if (!strcmp (geom, "?")) { -#if 0 /* TODO: */ - sprintf (str, "[%dx%d+%d+%d]", /* can't presume snprintf () ! */ - min (h_scale, 32767), min (v_scale, 32767), - min (h_align, 32767), min (v_align, 32767)); - process_xterm_seq (XTerm_title, str, CHAR_ST); -#endif + if (target) + { + sprintf (str, "[%dx%d+%d+%d]", /* can't presume snprintf () ! */ + min (h_scale, 32767), min (v_scale, 32767), + min (h_align, 32767), min (v_align, 32767)); + target->process_xterm_seq (XTerm_title, str, CHAR_ST); + } return false; } while (isspace(*geom)) ++geom; @@ -269,16 +267,16 @@ else if (geom_flags & WidthValue) { if (!(geom_flags & HeightValue)) - h = w; + h = w; } else - w = h; + w = h; } } /* done parsing geometry string */ else if (!(flags & bgPmap_geometrySet)) { /* default geometry - scaled and centered */ - x = y = bgPmap_defaultAlign; - w = h = bgPmap_defaultScale; + x = y = bgPmap_defaultAlign; + w = h = bgPmap_defaultScale; } if (!(flags & bgPmap_geometrySet)) @@ -289,7 +287,7 @@ while (*ops) { while (*ops == ':' || isspace(*ops)) ++ops; -#define CHECK_GEOM_OPS(op_str) (strncasecmp (ops, (op_str), sizeof(op_str)-1) == 0) +# define CHECK_GEOM_OPS(op_str) (strncasecmp (ops, (op_str), sizeof(op_str)-1) == 0) if (CHECK_GEOM_OPS("tile")) { w = h = 0; @@ -332,7 +330,7 @@ x = y = 50; geom_flags |= WidthValue|HeightValue|XValue|YValue; } -#undef CHECK_GEOM_OPS +# undef CHECK_GEOM_OPS while (*ops != ':' && *ops != '\0') ++ops; } /* done parsing ops */ } @@ -357,9 +355,9 @@ return (changed > 0); } -#ifdef HAVE_AFTERIMAGE +# ifdef HAVE_AFTERIMAGE bool -bgPixmap_t::render_asim (rxvt_term *target, ASImage *background, ARGB32 background_tint) +bgPixmap_t::render_asim (ASImage *background, ARGB32 background_tint) { if (target == NULL) return false; @@ -380,25 +378,28 @@ y = make_align_position (v_align, target_height, h > 0 ? h : (int)original_asim->height); } - int dst_x, dst_y; - int clip_width, clip_height; - - make_clip_rectangle (x, w, target_width, dst_x, clip_width); - make_clip_rectangle (y, h, target_height, dst_y, clip_height); - - /* TODO : actuall scaling code :) */ - if (dst_x >= target_width || dst_y >= target_height - || clip_width <= 0 || clip_height <= 0 || original_asim == NULL) + if (original_asim == NULL + || x >= target_width + || y >= target_height + || (w > 0 && x + w <= 0) + || (h > 0 && y + h <= 0)) { - result = background; - dst_x = dst_y = 0; if (background) { - new_pmap_width = clip_width = background->width; - new_pmap_height = clip_height = background->height; + new_pmap_width = background->width; + new_pmap_height = background->height; + result = background; + if (background_tint != TINT_LEAVE_SAME) + { + ASImage* tmp = tile_asimage (target->asv, background, 0, 0, + target_width, target_height, background_tint, + ASA_XImage, 100, ASIMAGE_QUALITY_DEFAULT); + if (tmp) + result = tmp; + } } else - new_pmap_width = new_pmap_height = 0; + new_pmap_width = new_pmap_height = 0; } else { @@ -418,6 +419,22 @@ new_pmap_width = result->width; if (v_scale == 0) new_pmap_height = result->height; + /* we also need to tile our image in one or both directions */ + if (h_scale == 0 || v_scale == 0) + { + ASImage *tmp = tile_asimage (target->asv, result, + (h_scale > 0) ? 0 : (int)result->width - x, + (v_scale > 0) ? 0 : (int)result->height - y, + result->width, result->height, + TINT_LEAVE_SAME, ASA_XImage, + 100, ASIMAGE_QUALITY_DEFAULT); + if (tmp) + { + if (result != original_asim) + destroy_asimage (&result); + result = tmp; + } + } } else {/* if blending background and image - pixmap has to be sized same as target window */ @@ -431,23 +448,25 @@ layers[1].im = result; if (w <= 0) {/* tile horizontally */ - layers[1].dst_x = dst_x - (int)result->width; - layers[1].clip_width = result->width; + while (x > 0) x -= (int)result->width; + layers[1].dst_x = x; + layers[1].clip_width = result->width+target_width; } else {/* clip horizontally */ - layers[1].dst_x = dst_x; - layers[1].clip_width = clip_width; + layers[1].dst_x = x; + layers[1].clip_width = result->width; } if (h <= 0) { - layers[1].dst_y = dst_y - (int)result->height; - layers[1].clip_height = result->height; + while (y > 0) y -= (int)result->height; + layers[1].dst_y = y; + layers[1].clip_height = result->height + target_height; } else { - layers[1].dst_y = dst_y; - layers[1].clip_height = clip_height; + layers[1].dst_y = y; + layers[1].clip_height = result->height; } if (target->rs[Rs_blendtype]) { @@ -462,9 +481,6 @@ if (result != original_asim) destroy_asimage (&result); result = tmp; - dst_x = dst_y = 0; - clip_width = target_width; - clip_height = target_height; } free (layers); } @@ -499,112 +515,57 @@ gcv.foreground = target->pix_colors[Color_bg]; gc = XCreateGC (target->dpy, target->vt, GCForeground, &gcv); - if (dst_x > 0 || dst_y > 0 - || dst_x + clip_width < new_pmap_width - || dst_y + clip_height < new_pmap_height) + int src_x = 0, src_y = 0, dst_x = 0, dst_y = 0; + int dst_width = result->width, dst_height = result->height; + if (background == NULL) { - XFillRectangle (target->dpy, pixmap, gc, 0, 0, new_pmap_width, new_pmap_height); + if (h_scale > 0) + src_x = make_clip_rectangle (x, result->width, new_pmap_width, dst_x, dst_width); + if (v_scale > 0) + src_y = make_clip_rectangle (y, result->height, new_pmap_height, dst_y, dst_height); + + if (dst_x > 0 || dst_y > 0 + || dst_x + dst_width < new_pmap_width + || dst_y + dst_height < new_pmap_height) + { + XFillRectangle (target->dpy, pixmap, gc, 0, 0, new_pmap_width, new_pmap_height); + } } - /* put result on pixmap */ - asimage2drawable (target->asv, pixmap, result, gc, 0, 0, dst_x, dst_y, clip_width, clip_height, True); - /* set target's background to pixmap */ - XSetWindowBackgroundPixmap (target->dpy, target->vt, pixmap); + /* put result on pixmap */ + if (dst_x < new_pmap_width && dst_y < new_pmap_height) + asimage2drawable (target->asv, pixmap, result, gc, src_x, src_y, dst_x, dst_y, dst_width, dst_height, True); + + if (result != background && result != original_asim) + destroy_asimage (&result); XFreeGC (target->dpy, gc); - } - else - { - /* set target background to a pixel */ - XSetWindowBackground (target->dpy, target->vt, target->pix_colors[Color_bg]); + if (background) + flags |= bgPmap_Transparent; + else + flags &= ~bgPmap_Transparent; } return true; } -#endif +# endif /* HAVE_AFTERIMAGE */ void rxvt_term::resize_pixmap () { - XGCValues gcvalue; - GC gc; - unsigned int w = bgPixmap.h_scale*szHint.width/100; - unsigned int h = bgPixmap.v_scale*szHint.height/100; - int x = bgPixmap.h_align*szHint.width/100; - int y = bgPixmap.v_align*szHint.height/100; -#ifdef HAVE_AFTERIMAGE - ASImage *im = bgPixmap.original_asim; -#else - void *im = NULL; -#endif -/* preliminary cleanup - this needs to be integrated with check_our_parents() code */ - if (bgPixmap.pixmap != None) - { - XFreePixmap (dpy, bgPixmap.pixmap); - bgPixmap.pixmap = None ; - } -#ifdef ENABLE_TRANSPARENCY + +# ifdef ENABLE_TRANSPARENCY if (option(Opt_transparent) && am_transparent) { /* we need to re-generate transparency pixmap in that case ! */ check_our_parents (); return; } -#endif - - if (im == NULL) - { /* So be it: I'm not using pixmaps */ - XSetWindowBackground (dpy, vt, pix_colors[Color_bg]); - return; - } - - gcvalue.foreground = pix_colors[Color_bg]; - gc = XCreateGC (dpy, vt, GCForeground, &gcvalue); - - /* don't zoom pixmap too much nor expand really small pixmaps */ - if (w > 16000) - w = 1; - if (h > 16000) - h = 1; - -#ifdef HAVE_AFTERIMAGE - if (w == 0) - w = im->width; - if (h == 0) - h = im->height; - - if (w != im->width || h != im->height) - { - ASImage *tmp = scale_asimage (asv, im, w, h, (x == 0 && y == 0)?ASA_XImage:ASA_ASImage, 0, ASIMAGE_QUALITY_DEFAULT); - if (tmp != NULL) - im = tmp; - } - bgPixmap.pmap_width = MIN(w,szHint.width); - bgPixmap.pmap_height = MIN(h,szHint.height); -#if 0 /* TODO: fix that! */ - if (x != 0 || y != 0) - { - ASImage *tmp = tile_asimage (asv, im, x, y, w, h, TINT_LEAVE_SAME, ASA_XImage, 0, ASIMAGE_QUALITY_DEFAULT); - if (tmp != NULL) - { - if (im != bgPixmap.original_asim) - destroy_asimage (&im); - im = tmp; - } - } -#endif - bgPixmap.pixmap = XCreatePixmap (dpy, vt, bgPixmap.pmap_width, bgPixmap.pmap_height, depth); - bgPixmap.pmap_depth = depth; - - asimage2drawable (asv, bgPixmap.pixmap, im, gc, 0, 0, 0, 0, bgPixmap.pmap_width, bgPixmap.pmap_height, True); - - if (im != bgPixmap.original_asim) - destroy_asimage (&im); -#endif - if( bgPixmap.pixmap ) - XSetWindowBackgroundPixmap (dpy, vt, bgPixmap.pixmap); - - XFreeGC (dpy, gc); +# endif +# ifdef HAVE_AFTERIMAGE + bgPixmap.render_asim(NULL, TINT_LEAVE_SAME); + bgPixmap.apply_background(); +# endif } void @@ -623,7 +584,7 @@ XSetWindowBackground (dpy, vt, pix_colors[Color_bg]); if (*file != '\0') { -#ifdef HAVE_AFTERIMAGE +# ifdef HAVE_AFTERIMAGE if (asimman == NULL) asimman = create_generic_imageman(rs[Rs_path]); if ((f = strchr (file, ';')) == NULL) @@ -637,15 +598,103 @@ bgPixmap.original_asim = get_asimage( asimman, f, 0xFFFFFFFF, 100 ); free( f ); } -#endif +# endif } + resize_pixmap (); /* TODO: temporary fix - should be done by the caller! */ +} + +# endif /* XPM_BACKGROUND */ + +# ifdef ENABLE_TRANSPARENCY +/* make_transparency_pixmap() + * Builds a pixmap sized the same as terminal window, with depth same as the root window + * that pixmap contains tiled portion of the root pixmap that is supposed to be covered by + * our window. + */ +#if 0 +bool +bgPixmap_t::make_transparency_pixmap() +{ + if (target == NULL) + return false; + + int sx, sy; + Window cr; + XTranslateCoordinates (target->dpy, target->parent[0], target->display->root, + 0, 0, &sx, &sy, &cr); + + /* check if we are outside of the visible part of the virtual screen : */ + if( sx + (int)szHint.width <= 0 || sy + (int)szHint.height <= 0 + || sx >= wrootattr.width || sy >= wrootattr.height ) + return false ; + + XWindowAttributes wattr, wrootattr; + + XGetWindowAttributes (dpy, display->root, &wrootattr); + + /* + * Make the frame window set by the window manager have + * the root background. Some window managers put multiple nested frame + * windows for each client, so we have to take care about that. + */ + i = (xa[XA_XROOTPMAP_ID] + && XGetWindowProperty (dpy, display->root, xa[XA_XROOTPMAP_ID], + 0L, 1L, False, XA_PIXMAP, &atype, &aformat, + &nitems, &bytes_after, &prop) == Success); + + if (!i || prop == NULL) + i = (xa[XA_ESETROOT_PMAP_ID] + && XGetWindowProperty (dpy, display->root, xa[XA_ESETROOT_PMAP_ID], + 0L, 1L, False, XA_PIXMAP, &atype, &aformat, + &nitems, &bytes_after, &prop) == Success); + + if (!i || prop == NULL) + rootpixmap = None; - resize_pixmap (); } +#endif /* 0 */ +# endif /* ENABLE_TRANSPARENCY */ -#endif /* XPM_BACKGROUND */ +void +bgPixmap_t::apply_background() +{ + if (target) + { + if (pixmap != None) + { /* set target's background to pixmap */ +# ifdef ENABLE_TRANSPARENCY + if (flags & bgPmap_Transparent) + { + XSetWindowBackgroundPixmap (target->dpy, target->parent[0], pixmap); + XSetWindowBackgroundPixmap (target->dpy, target->vt, ParentRelative); + if (target->scrollBar.win) + XSetWindowBackgroundPixmap (target->dpy, target->scrollBar.win, ParentRelative); + } + else +# endif + { + /* force old pixmap dereference in case it was transparent before :*/ + XSetWindowBackground (target->dpy, target->parent[0], target->pix_colors[Color_bg]); + XSetWindowBackgroundPixmap (target->dpy, target->vt, pixmap); + /* do we also need to set scrollbar's background here ? */ + } + } + else + { /* set target background to a pixel */ + XSetWindowBackground (target->dpy, target->parent[0], target->pix_colors[Color_bg]); + XSetWindowBackground (target->dpy, target->vt, target->pix_colors[Color_bg]); + /* do we also need to set scrollbar's background here ? */ + } + /* don't want Expose on the parent */ + XClearArea (target->dpy, target->parent[0], 0, 0, 0, 0, False); + /* do want Expose on the vt */ + XClearArea (target->dpy, target->parent[0], 0, 0, 0, 0, True); + } +} #endif /* HAVE_BG_PIXMAP */ + + #ifdef ENABLE_TRANSPARENCY #ifndef HAVE_AFTERIMAGE /* taken from aterm-0.4.2 */ @@ -894,7 +943,7 @@ void rxvt_term::check_our_parents_cb (time_watcher &w) { - int i, pchanged, aformat, rootdepth; + int i, aformat, rootdepth; unsigned long nitems, bytes_after; Atom atype; unsigned char *prop = NULL; @@ -905,10 +954,8 @@ Window cr; unsigned int rootpixmap_w = 0, rootpixmap_h = 0; - pchanged = 0; - if (!option (Opt_transparent)) - return /*pchanged*/; /* Don't try any more */ + return; /* Don't try any more */ #if 0 struct timeval stv; @@ -930,12 +977,11 @@ { if (am_transparent) { - pchanged = 1; XSetWindowBackground (dpy, vt, pix_colors_focused[Color_bg]); am_transparent = am_pixmap_trans = 0; } - return /*pchanged*/; /* Don't try any more */ + return; /* Don't try any more */ } /* Get all X ops out of the queue so that our information is up-to-date. */ @@ -963,14 +1009,7 @@ 0L, 1L, False, XA_PIXMAP, &atype, &aformat, &nitems, &bytes_after, &prop) == Success); - /* TODO: the below logic needs to be cleaned up */ - if (!i || prop == NULL - || (!ISSET_PIXCOLOR (Color_tint) && rs[Rs_shade] == NULL -#ifdef HAVE_AFTERIMAGE - && bgPixmap.original_asim == NULL && rs[Rs_blurradius] == NULL -#endif - ) - ) + if (!i || prop == NULL) rootpixmap = None; else { @@ -1084,66 +1123,11 @@ back_im = tmp; } } - - if (bgPixmap.original_asim != NULL) - { - ASImageLayer *layers = create_image_layers (2); - ASImage *merged_im = NULL; - int fore_w, fore_h; - - layers[0].im = back_im; - layers[0].clip_width = szHint.width; - layers[0].clip_height = szHint.height; - layers[0].tint = tint; - layers[1].im = bgPixmap.original_asim; - - fore_w = (bgPixmap.h_scale == 0) ? bgPixmap.original_asim->width : bgPixmap.h_scale*szHint.width/100; - fore_h = (bgPixmap.v_scale == 0) ? bgPixmap.original_asim->height : bgPixmap.v_scale*szHint.height/100; - - if (fore_w != bgPixmap.original_asim->width - || fore_h != bgPixmap.original_asim->height) - { - layers[1].im = scale_asimage (asv, - bgPixmap.original_asim, - fore_w, fore_h, - ASA_ASImage, 100, - ASIMAGE_QUALITY_DEFAULT); - } - layers[1].clip_width = szHint.width; - layers[1].clip_height = szHint.height; - - if (rs[Rs_blendtype]) - { - layers[1].merge_scanlines = blend_scanlines_name2func (rs[Rs_blendtype]); - if (layers[1].merge_scanlines == NULL) - layers[1].merge_scanlines = alphablend_scanlines; - } - PRINT_BACKGROUND_OP_TIME; - merged_im = merge_layers (asv, layers, 2, szHint.width, szHint.height, - ASA_XImage, 0, ASIMAGE_QUALITY_DEFAULT); - if (layers[1].im != bgPixmap.original_asim) - destroy_asimage (&(layers[1].im)); - free (layers); - - if (merged_im != NULL) - { - destroy_asimage (&back_im); - back_im = merged_im; - } - PRINT_BACKGROUND_OP_TIME; - } - else if (tint != TINT_LEAVE_SAME) - { - ASImage* tmp = tile_asimage (asv, back_im, 0, 0, szHint.width, szHint.height, tint, ASA_XImage, 100, ASIMAGE_QUALITY_DEFAULT); - if (tmp) - { - destroy_asimage (&back_im); - back_im = tmp; - } - PRINT_BACKGROUND_OP_TIME; - } - asimage2drawable (asv, bgPixmap.pixmap, back_im, gc, 0, 0, 0, 0, szHint.width, szHint.height, True); + /* TODO: temporary fix - redo the logic, so that same function can do both + transparency and non-transparency */ + bgPixmap.render_asim (back_im, tint); destroy_asimage (&back_im); + } /* back_im != NULL */ else success = False; @@ -1170,110 +1154,30 @@ if (gc != NULL) XFreeGC (dpy, gc); - + + bgPixmap.apply_background(); if (!success) - { - if (am_transparent && am_pixmap_trans) - { - pchanged = 1; - if (bgPixmap.pixmap != None) - { - XFreePixmap (dpy, bgPixmap.pixmap); - bgPixmap.pixmap = None; - } - } - am_pixmap_trans = 0; - } else { - XSetWindowBackgroundPixmap (dpy, parent[0], bgPixmap.pixmap); - XClearWindow (dpy, parent[0]); - - if (!am_transparent || !am_pixmap_trans) - pchanged = 1; - am_transparent = am_pixmap_trans = 1; } } /* rootpixmap != None */ if (am_pixmap_trans) - XSetWindowBackgroundPixmap (dpy, vt, ParentRelative); - else { - unsigned int n; - /* - * InheritPixmap transparency - */ - for (i = 1; i < (int) (sizeof (parent) / sizeof (Window)); i++) + if (scrollBar.win) { - oldp = parent[i]; - XQueryTree (dpy, parent[i - 1], &root, &parent[i], &list, &n); - XFree (list); - - if (parent[i] == display->root) - { - if (oldp != None) - pchanged = 1; - - break; - } - - if (oldp != parent[i]) - pchanged = 1; + scrollBar.setIdle (); + scrollbar_show (0); } - n = 0; - - if (pchanged) - for (; n < (unsigned int)i; n++) - { - XGetWindowAttributes (dpy, parent[n], &wattr); - - if (wattr.depth != rootdepth || wattr.c_class == InputOnly) - { - n = (int) (sizeof (parent) / sizeof (Window)) + 1; - break; - } - } - - if (n > (sizeof (parent) / sizeof (parent[0]))) - { - XSetWindowBackground (dpy, parent[0], pix_colors_focused[Color_border]); - XSetWindowBackground (dpy, vt, pix_colors_focused[Color_bg]); - am_transparent = 0; - /* XXX: also turn off Opt_transparent? */ - } - else + if (am_transparent) { - for (n = 0; n < (unsigned int)i; n++) - { - XSetWindowBackgroundPixmap (dpy, parent[n], ParentRelative); - XClearWindow (dpy, parent[n]); - } - - XSetWindowBackgroundPixmap (dpy, vt, ParentRelative); - am_transparent = 1; + want_refresh = want_full_refresh = 1; + if (am_pixmap_trans) + flush (); } - - for (; i < (int) (sizeof (parent) / sizeof (Window)); i++) - parent[i] = None; } - - if (scrollBar.win) - { - XSetWindowBackgroundPixmap (dpy, scrollBar.win, ParentRelative); - scrollBar.setIdle (); - scrollbar_show (0); - } - - if (am_transparent) - { - want_refresh = want_full_refresh = 1; - if (am_pixmap_trans) - flush (); - } - -// return pchanged; } #endif