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.209 by sf-exg, Fri May 11 12:21:22 2012 UTC vs.
Revision 1.221 by sf-exg, Fri May 25 09:27:35 2012 UTC

58# endif 58# endif
59 59
60void 60void
61rxvt_term::bg_destroy () 61rxvt_term::bg_destroy ()
62{ 62{
63#ifdef HAVE_PIXBUF 63# ifdef BG_IMAGE_FROM_FILE
64 if (pixbuf) 64 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
65 g_object_unref (pixbuf); 65 bg_image->destroy ();
66#endif 66# endif
67 67
68 if (bg_pixmap) 68 if (bg_pixmap)
69 XFreePixmap (dpy, bg_pixmap); 69 XFreePixmap (dpy, bg_pixmap);
70} 70}
71 71
90 if (bg_flags & BG_IS_TRANSPARENT) 90 if (bg_flags & BG_IS_TRANSPARENT)
91 return true; 91 return true;
92# endif 92# endif
93 93
94# ifdef BG_IMAGE_FROM_FILE 94# ifdef BG_IMAGE_FROM_FILE
95 if (bg_flags & BG_IS_FROM_FILE) 95 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
96 { 96 {
97 if (bg_flags & BG_IS_SIZE_SENSITIVE) 97 if ((bg_image->flags & IM_IS_SIZE_SENSITIVE)
98 || bg_image->width () > szHint.width
99 || bg_image->height () > szHint.height)
98 return true; 100 return true;
99 } 101 }
100# endif 102# endif
101 103
102 return false; 104 return false;
109 if (bg_flags & BG_IS_TRANSPARENT) 111 if (bg_flags & BG_IS_TRANSPARENT)
110 return true; 112 return true;
111# endif 113# endif
112 114
113# ifdef BG_IMAGE_FROM_FILE 115# ifdef BG_IMAGE_FROM_FILE
114 if (bg_flags & BG_IS_FROM_FILE) 116 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
115 { 117 {
116 if (bg_flags & BG_ROOT_ALIGN) 118 if (bg_image->flags & IM_ROOT_ALIGN)
117 return true; 119 return true;
118 } 120 }
119# endif 121# endif
120 122
121 return false; 123 return false;
148 150
149 min_it (dst_size, target_size - dst_pos); 151 min_it (dst_size, target_size - dst_pos);
150 return src_pos; 152 return src_pos;
151} 153}
152 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
153bool 190bool
154rxvt_term::bg_set_geometry (const char *geom, bool update) 191rxvt_image::set_geometry (const char *geom, bool update)
155{ 192{
156 bool changed = false; 193 bool changed = false;
157 int geom_flags = 0; 194 int geom_flags = 0;
158 int x = h_align; 195 int x = h_align;
159 int y = v_align; 196 int y = v_align;
160 unsigned int w = h_scale; 197 unsigned int w = h_scale;
161 unsigned int h = v_scale; 198 unsigned int h = v_scale;
162 unsigned long new_flags = 0; 199 uint8_t new_flags = 0;
163 200
164 if (geom == NULL) 201 if (geom == NULL)
165 return false; 202 return false;
166 203
167 if (geom[0]) 204 if (geom[0])
168 { 205 {
169 char **arr = rxvt_strsplit (':', geom); 206 char **arr = rxvt_strsplit (':', geom);
170 207
171 for (int i = 0; arr[i]; i++) 208 for (int i = 0; arr[i]; i++)
172 { 209 {
173 if (!strcasecmp (arr[i], "style=tiled")) 210 if (!strncasecmp (arr[i], "style=", 6))
174 { 211 {
175 new_flags = BG_TILE; 212 parse_style (arr[i] + 6, x, y, w, h, new_flags);
176 w = h = noScale;
177 x = y = 0;
178 geom_flags = WidthValue|HeightValue|XValue|YValue; 213 geom_flags = WidthValue|HeightValue|XValue|YValue;
179 } 214 }
180 else if (!strcasecmp (arr[i], "style=aspect-stretched"))
181 {
182 new_flags = BG_KEEP_ASPECT;
183 w = h = windowScale;
184 x = y = centerAlign;
185 geom_flags = WidthValue|HeightValue|XValue|YValue;
186 }
187 else if (!strcasecmp (arr[i], "style=stretched"))
188 {
189 new_flags = 0;
190 w = h = windowScale;
191 geom_flags = WidthValue|HeightValue;
192 }
193 else if (!strcasecmp (arr[i], "style=centered"))
194 {
195 new_flags = 0;
196 w = h = noScale;
197 x = y = centerAlign;
198 geom_flags = WidthValue|HeightValue|XValue|YValue;
199 }
200 else if (!strcasecmp (arr[i], "style=root-tiled"))
201 {
202 new_flags = BG_TILE|BG_ROOT_ALIGN;
203 w = h = noScale;
204 geom_flags = WidthValue|HeightValue;
205 }
206 else if (!strcasecmp (arr[i], "op=tile")) 215 else if (!strcasecmp (arr[i], "op=tile"))
207 new_flags |= BG_TILE; 216 new_flags |= IM_TILE;
208 else if (!strcasecmp (arr[i], "op=keep-aspect")) 217 else if (!strcasecmp (arr[i], "op=keep-aspect"))
209 new_flags |= BG_KEEP_ASPECT; 218 new_flags |= IM_KEEP_ASPECT;
210 else if (!strcasecmp (arr[i], "op=root-align")) 219 else if (!strcasecmp (arr[i], "op=root-align"))
211 new_flags |= BG_ROOT_ALIGN; 220 new_flags |= IM_ROOT_ALIGN;
212 221
213 // deprecated 222 // deprecated
214 else if (!strcasecmp (arr[i], "tile")) 223 else if (!strcasecmp (arr[i], "tile"))
215 { 224 {
216 new_flags |= BG_TILE; 225 new_flags |= IM_TILE;
217 w = h = noScale; 226 w = h = noScale;
218 geom_flags |= WidthValue|HeightValue; 227 geom_flags |= WidthValue|HeightValue;
219 } 228 }
220 else if (!strcasecmp (arr[i], "propscale")) 229 else if (!strcasecmp (arr[i], "propscale"))
221 { 230 {
222 new_flags |= BG_KEEP_ASPECT; 231 new_flags |= IM_KEEP_ASPECT;
223 w = h = windowScale; 232 w = h = windowScale;
224 geom_flags |= WidthValue|HeightValue; 233 geom_flags |= WidthValue|HeightValue;
225 } 234 }
226 else if (!strcasecmp (arr[i], "hscale")) 235 else if (!strcasecmp (arr[i], "hscale"))
227 { 236 {
228 new_flags |= BG_TILE; 237 new_flags |= IM_TILE;
229 w = windowScale; 238 w = windowScale;
230 h = noScale; 239 h = noScale;
231 geom_flags |= WidthValue|HeightValue; 240 geom_flags |= WidthValue|HeightValue;
232 } 241 }
233 else if (!strcasecmp (arr[i], "vscale")) 242 else if (!strcasecmp (arr[i], "vscale"))
234 { 243 {
235 new_flags |= BG_TILE; 244 new_flags |= IM_TILE;
236 h = windowScale; 245 h = windowScale;
237 w = noScale; 246 w = noScale;
238 geom_flags |= WidthValue|HeightValue; 247 geom_flags |= WidthValue|HeightValue;
239 } 248 }
240 else if (!strcasecmp (arr[i], "scale")) 249 else if (!strcasecmp (arr[i], "scale"))
248 x = y = centerAlign; 257 x = y = centerAlign;
249 geom_flags |= WidthValue|HeightValue|XValue|YValue; 258 geom_flags |= WidthValue|HeightValue|XValue|YValue;
250 } 259 }
251 else if (!strcasecmp (arr[i], "root")) 260 else if (!strcasecmp (arr[i], "root"))
252 { 261 {
253 new_flags |= BG_TILE|BG_ROOT_ALIGN; 262 new_flags |= IM_TILE|IM_ROOT_ALIGN;
254 w = h = noScale; 263 w = h = noScale;
255 geom_flags |= WidthValue|HeightValue; 264 geom_flags |= WidthValue|HeightValue;
256 } 265 }
257 266
258 else 267 else
260 } /* done parsing ops */ 269 } /* done parsing ops */
261 270
262 rxvt_free_strsplit (arr); 271 rxvt_free_strsplit (arr);
263 } 272 }
264 273
265 new_flags |= bg_flags & ~BG_GEOMETRY_FLAGS; 274 new_flags |= flags & ~IM_GEOMETRY_FLAGS;
266 275
267 if (!update) 276 if (!update)
268 { 277 {
269 if (!(geom_flags & XValue)) 278 if (!(geom_flags & XValue))
270 x = y = defaultAlign; 279 x = y = defaultAlign;
277 h = w; 286 h = w;
278 else if (!(geom_flags & WidthValue)) 287 else if (!(geom_flags & WidthValue))
279 w = h; 288 w = h;
280 } 289 }
281 290
282 min_it (w, 1000);
283 min_it (h, 1000);
284 clamp_it (x, -100, 200); 291 clamp_it (x, -100, 200);
285 clamp_it (y, -100, 200); 292 clamp_it (y, -100, 200);
286 293
287 if (bg_flags != new_flags 294 if (flags != new_flags
288 || h_scale != w 295 || h_scale != w
289 || v_scale != h 296 || v_scale != h
290 || h_align != x 297 || h_align != x
291 || v_align != y) 298 || v_align != y)
292 { 299 {
293 bg_flags = new_flags; 300 flags = new_flags;
294 h_scale = w; 301 h_scale = w;
295 v_scale = h; 302 v_scale = h;
296 h_align = x; 303 h_align = x;
297 v_align = y; 304 v_align = y;
298 changed = true; 305 changed = true;
299 } 306 }
300 307
308 if (!(flags & IM_TILE)
309 || h_scale || v_scale
310 || (!(flags & IM_ROOT_ALIGN) && (h_align || v_align)))
311 flags |= IM_IS_SIZE_SENSITIVE;
312 else
313 flags &= ~IM_IS_SIZE_SENSITIVE;
314
301 return changed; 315 return changed;
302} 316}
303 317
304void 318void
305rxvt_term::get_image_geometry (int image_width, int image_height, int &w, int &h, int &x, int &y) 319rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y)
306{ 320{
321 int image_width = image.width ();
322 int image_height = image.height ();
307 int target_width = szHint.width; 323 int target_width = szHint.width;
308 int target_height = szHint.height; 324 int target_height = szHint.height;
325 int h_scale = min (image.h_scale, 32767 * 100 / target_width);
326 int v_scale = min (image.v_scale, 32767 * 100 / target_height);
309 327
310 w = h_scale * target_width / 100; 328 w = h_scale * target_width / 100;
311 h = v_scale * target_height / 100; 329 h = v_scale * target_height / 100;
312 330
313 if (bg_flags & BG_KEEP_ASPECT) 331 if (image.flags & IM_KEEP_ASPECT)
314 { 332 {
315 float scale = (float)w / image_width; 333 float scale = (float)w / image_width;
316 min_it (scale, (float)h / image_height); 334 min_it (scale, (float)h / image_height);
317 w = image_width * scale + 0.5; 335 w = image_width * scale + 0.5;
318 h = image_height * scale + 0.5; 336 h = image_height * scale + 0.5;
319 } 337 }
320 338
321 if (!w) w = image_width; 339 if (!w) w = image_width;
322 if (!h) h = image_height; 340 if (!h) h = image_height;
323 341
324 if (bg_flags & BG_ROOT_ALIGN) 342 if (image.flags & IM_ROOT_ALIGN)
325 { 343 {
326 x = -target_x; 344 x = -target_x;
327 y = -target_y; 345 y = -target_y;
328 } 346 }
329 else 347 else
330 { 348 {
331 x = make_align_position (h_align, target_width, w); 349 x = make_align_position (image.h_align, target_width, w);
332 y = make_align_position (v_align, target_height, h); 350 y = make_align_position (image.v_align, target_height, h);
333 } 351 }
334
335 if (!(bg_flags & BG_TILE)
336 || h_scale || v_scale
337 || (!(bg_flags & BG_ROOT_ALIGN) && (h_align || v_align))
338 || image_width > target_width || image_height > target_height)
339 bg_flags |= BG_IS_SIZE_SENSITIVE;
340 else
341 bg_flags &= ~BG_IS_SIZE_SENSITIVE;
342} 352}
343 353
344# ifdef HAVE_PIXBUF 354# ifdef HAVE_PIXBUF
345bool 355bool
346rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc, 356rxvt_term::pixbuf_to_pixmap (GdkPixbuf *pixbuf, Pixmap pixmap, GC gc,
347 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,
348 unsigned int width, unsigned int height) 358 unsigned int width, unsigned int height, bool argb)
349{ 359{
350 XImage *ximage; 360 XImage *ximage;
351 char *data, *line; 361 char *line;
352 int bytes_per_pixel;
353 int width_r, width_g, width_b, width_a; 362 int width_r, width_g, width_b, width_a;
354 int sh_r, sh_g, sh_b, sh_a; 363 int sh_r, sh_g, sh_b, sh_a;
355 uint32_t alpha_mask; 364 uint32_t red_mask, green_mask, blue_mask, alpha_mask;
356 int rowstride; 365 int rowstride;
357 int channels; 366 int channels;
358 unsigned char *row; 367 unsigned char *row;
359 368
360 if (visual->c_class != TrueColor) 369 if (visual->c_class != TrueColor)
361 return false; 370 return false;
362 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;
363#if XRENDER 384#if XRENDER
364 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 385 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
365 if (format) 386 if (format)
366 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; 387 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
367 else 388 else
368#endif 389#endif
369 alpha_mask = 0; 390 alpha_mask = 0;
391 }
370 392
371 if (depth == 24 || depth == 32)
372 bytes_per_pixel = 4;
373 else if (depth == 15 || depth == 16)
374 bytes_per_pixel = 2;
375 else
376 return false;
377
378 width_r = ecb_popcount32 (visual->red_mask); 393 width_r = ecb_popcount32 (red_mask);
379 width_g = ecb_popcount32 (visual->green_mask); 394 width_g = ecb_popcount32 (green_mask);
380 width_b = ecb_popcount32 (visual->blue_mask); 395 width_b = ecb_popcount32 (blue_mask);
381 width_a = ecb_popcount32 (alpha_mask); 396 width_a = ecb_popcount32 (alpha_mask);
382 397
383 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)
384 return false; 399 return false;
385 400
386 sh_r = ecb_ctz32 (visual->red_mask); 401 sh_r = ecb_ctz32 (red_mask);
387 sh_g = ecb_ctz32 (visual->green_mask); 402 sh_g = ecb_ctz32 (green_mask);
388 sh_b = ecb_ctz32 (visual->blue_mask); 403 sh_b = ecb_ctz32 (blue_mask);
389 sh_a = ecb_ctz32 (alpha_mask); 404 sh_a = ecb_ctz32 (alpha_mask);
390 405
391 if (width > INT_MAX / height / bytes_per_pixel) 406 if (width > 32767 || height > 32767)
392 return false; 407 return false;
393 408
394 data = (char *)malloc (width * height * bytes_per_pixel); 409 ximage = XCreateImage (dpy, visual, argb ? 32 : depth, ZPixmap, 0, 0,
395 if (!data) 410 width, height, 32, 0);
411 if (!ximage)
396 return false; 412 return false;
397 413
398 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, data, 414 if (height > INT_MAX / ximage->bytes_per_line
399 width, height, bytes_per_pixel * 8, 0); 415 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
400 if (!ximage)
401 { 416 {
402 free (data); 417 XDestroyImage (ximage);
403 return false; 418 return false;
404 } 419 }
405 420
406 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 421 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
407 422
408 rowstride = gdk_pixbuf_get_rowstride (pixbuf); 423 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
409 channels = gdk_pixbuf_get_n_channels (pixbuf); 424 channels = gdk_pixbuf_get_n_channels (pixbuf);
410 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; 425 row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
411 line = data; 426 line = ximage->data;
412 427
413 rgba c (0, 0, 0); 428 rgba c (0, 0, 0);
414 429
415 if (channels == 4 && alpha_mask == 0) 430 if (channels == 4 && alpha_mask == 0)
416 { 431 {
446 value = ((r >> (8 - width_r)) << sh_r) 461 value = ((r >> (8 - width_r)) << sh_r)
447 | ((g >> (8 - width_g)) << sh_g) 462 | ((g >> (8 - width_g)) << sh_g)
448 | ((b >> (8 - width_b)) << sh_b) 463 | ((b >> (8 - width_b)) << sh_b)
449 | ((a >> (8 - width_a)) << sh_a); 464 | ((a >> (8 - width_a)) << sh_a);
450 465
451 if (bytes_per_pixel == 4) 466 if (ximage->bits_per_pixel == 32)
452 ((uint32_t *)line)[x] = value; 467 ((uint32_t *)line)[x] = value;
453 else 468 else
454 ((uint16_t *)line)[x] = value; 469 XPutPixel (ximage, x, y, value);
455 } 470 }
456 471
457 row += rowstride; 472 row += rowstride;
458 line += ximage->bytes_per_line; 473 line += ximage->bytes_per_line;
459 } 474 }
462 XDestroyImage (ximage); 477 XDestroyImage (ximage);
463 return true; 478 return true;
464} 479}
465 480
466bool 481bool
467rxvt_term::render_image (bool transparent) 482rxvt_term::render_image (rxvt_image &image)
468{ 483{
484 GdkPixbuf *pixbuf = image.pixbuf;
469 if (!pixbuf) 485 if (!pixbuf)
470 return false; 486 return false;
471 487
472 if (transparent 488 bool need_blend = bg_flags & BG_IS_VALID;
489
490 if (need_blend
473 && !(bg_flags & BG_HAS_RENDER)) 491 && !(bg_flags & BG_HAS_RENDER))
474 return false; 492 return false;
475 493
476 GdkPixbuf *result; 494 GdkPixbuf *result;
477 495
486 int x = 0; 504 int x = 0;
487 int y = 0; 505 int y = 0;
488 int w = 0; 506 int w = 0;
489 int h = 0; 507 int h = 0;
490 508
491 get_image_geometry (image_width, image_height, w, h, x, y); 509 get_image_geometry (image, w, h, x, y);
492 510
493 if (!(bg_flags & BG_ROOT_ALIGN) 511 if (!(image.flags & IM_ROOT_ALIGN)
494 && (x >= target_width 512 && (x >= target_width
495 || y >= target_height 513 || y >= target_height
496 || x + w <= 0 514 || x + w <= 0
497 || y + h <= 0)) 515 || y + h <= 0))
498 return false; 516 return false;
512 530
513 bool ret = false; 531 bool ret = false;
514 532
515 XGCValues gcv; 533 XGCValues gcv;
516 GC gc; 534 GC gc;
517 Pixmap root_pmap; 535 Pixmap tmp_pixmap;
518 536
519 image_width = gdk_pixbuf_get_width (result); 537 image_width = gdk_pixbuf_get_width (result);
520 image_height = gdk_pixbuf_get_height (result); 538 image_height = gdk_pixbuf_get_height (result);
521 539
522 if (transparent) 540 if (need_blend)
523 { 541 tmp_pixmap = XCreatePixmap (dpy, vt, new_pmap_width, new_pmap_height, 32);
524 root_pmap = bg_pixmap;
525 bg_pixmap = None;
526 }
527 else 542 else
528 { 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.
529 if (bg_flags & BG_TILE) 547 if ((image.flags & IM_TILE)
548 && image_vec.size () == 1)
530 { 549 {
531 new_pmap_width = min (image_width, target_width); 550 new_pmap_width = min (image_width, target_width);
532 new_pmap_height = min (image_height, target_height); 551 new_pmap_height = min (image_height, target_height);
533 } 552 }
534 }
535 553
536 if (bg_pixmap == None 554 if (bg_pixmap == None
537 || bg_pmap_width != new_pmap_width 555 || bg_pmap_width != new_pmap_width
538 || bg_pmap_height != new_pmap_height) 556 || bg_pmap_height != new_pmap_height)
539 { 557 {
540 if (bg_pixmap) 558 if (bg_pixmap)
541 XFreePixmap (dpy, bg_pixmap); 559 XFreePixmap (dpy, bg_pixmap);
542 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);
543 bg_pmap_width = new_pmap_width; 561 bg_pmap_width = new_pmap_width;
544 bg_pmap_height = new_pmap_height; 562 bg_pmap_height = new_pmap_height;
563 }
564
565 tmp_pixmap = bg_pixmap;
545 } 566 }
546 567
547 gcv.foreground = pix_colors[Color_bg]; 568 gcv.foreground = pix_colors[Color_bg];
548 gc = XCreateGC (dpy, vt, GCForeground, &gcv); 569 gc = XCreateGC (dpy, tmp_pixmap, GCForeground, &gcv);
549 570
550 if (gc) 571 if (gc)
551 { 572 {
552 if (bg_flags & BG_TILE) 573 if (image.flags & IM_TILE)
553 { 574 {
554 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, depth); 575 Pixmap tile = XCreatePixmap (dpy, vt, image_width, image_height, need_blend ? 32 : depth);
555 pixbuf_to_pixmap (result, tile, gc, 576 pixbuf_to_pixmap (result, tile, gc,
556 0, 0, 577 0, 0,
557 0, 0, 578 0, 0,
558 image_width, image_height); 579 image_width, image_height, need_blend);
559 580
560 gcv.tile = tile; 581 gcv.tile = tile;
561 gcv.fill_style = FillTiled; 582 gcv.fill_style = FillTiled;
562 gcv.ts_x_origin = x; 583 gcv.ts_x_origin = x;
563 gcv.ts_y_origin = y; 584 gcv.ts_y_origin = y;
564 XChangeGC (dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 585 XChangeGC (dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
565 586
566 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);
567 XFreePixmap (dpy, tile); 588 XFreePixmap (dpy, tile);
568 } 589 }
569 else 590 else
570 { 591 {
571 int src_x, src_y, dst_x, dst_y; 592 int src_x, src_y, dst_x, dst_y;
575 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);
576 597
577 if (dst_x > 0 || dst_y > 0 598 if (dst_x > 0 || dst_y > 0
578 || dst_x + dst_width < new_pmap_width 599 || dst_x + dst_width < new_pmap_width
579 || dst_y + dst_height < new_pmap_height) 600 || dst_y + dst_height < new_pmap_height)
580 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);
581 602
582 if (dst_x < new_pmap_width && dst_y < new_pmap_height) 603 if (dst_x < new_pmap_width && dst_y < new_pmap_height)
583 pixbuf_to_pixmap (result, bg_pixmap, gc, 604 pixbuf_to_pixmap (result, tmp_pixmap, gc,
584 src_x, src_y, 605 src_x, src_y,
585 dst_x, dst_y, 606 dst_x, dst_y,
586 dst_width, dst_height); 607 dst_width, dst_height, need_blend);
587 } 608 }
588 609
589#if XRENDER 610#if XRENDER
590 if (transparent) 611 if (need_blend)
591 { 612 {
613 XRenderPictFormat *argb_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
592 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); 614 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
593 615
594 Picture src = XRenderCreatePicture (dpy, root_pmap, format, 0, 0); 616 Picture src = XRenderCreatePicture (dpy, tmp_pixmap, argb_format, 0, 0);
595 617
596 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0); 618 Picture dst = XRenderCreatePicture (dpy, bg_pixmap, format, 0, 0);
597 619
598 Picture mask = create_xrender_mask (dpy, vt, False, False); 620 Picture mask = create_xrender_mask (dpy, vt, False, False);
599 621
600 XRenderColor mask_c; 622 XRenderColor mask_c;
601 623
602 mask_c.alpha = 0x8000; 624 mask_c.alpha = gdk_pixbuf_get_has_alpha (image.pixbuf) ? 0xffff : image.alpha;
603 mask_c.red = 625 mask_c.red =
604 mask_c.green = 626 mask_c.green =
605 mask_c.blue = 0; 627 mask_c.blue = 0;
606 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); 628 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
607 629
619 } 641 }
620 642
621 if (result != pixbuf) 643 if (result != pixbuf)
622 g_object_unref (result); 644 g_object_unref (result);
623 645
624 if (transparent) 646 if (need_blend)
625 XFreePixmap (dpy, root_pmap); 647 XFreePixmap (dpy, tmp_pixmap);
626 648
627 return ret; 649 return ret;
628} 650}
629# endif /* HAVE_PIXBUF */ 651# endif /* HAVE_PIXBUF */
630 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 rxvt_image *image;
680
681 for (image = image_vec.begin (); image < image_vec.end (); image++)
682 if (image->id == id)
683 break;
684
685 if (image == image_vec.end ())
686 {
687 image = new_image ();
688 image->id = id;
689 }
690}
691# endif
692
693rxvt_image::rxvt_image ()
694{
695 id =
696 alpha =
697 flags =
698 h_scale =
699 v_scale =
700 h_align =
701 v_align = 0;
702
703# ifdef HAVE_PIXBUF
704 pixbuf = 0;
705# endif
706}
707
631bool 708bool
632rxvt_term::bg_set_file (const char *file) 709rxvt_image::set_file_geometry (const char *file)
633{ 710{
634 if (!file || !*file) 711 if (!file || !*file)
635 return false; 712 return false;
636 713
637 bool ret = false;
638 const char *p = strchr (file, ';'); 714 const char *p = strchr (file, ';');
639 715
640 if (p) 716 if (p)
641 { 717 {
642 size_t len = p - file; 718 size_t len = p - file;
644 memcpy (f, file, len); 720 memcpy (f, file, len);
645 f[len] = '\0'; 721 f[len] = '\0';
646 file = f; 722 file = f;
647 } 723 }
648 724
725 bool ret = set_file (file);
726 alpha = 0x8000;
727 if (ret && p)
728 set_geometry (p + 1);
729 return ret;
730}
731
732bool
733rxvt_image::set_file (const char *file)
734{
735 bool ret = false;
736
649# ifdef HAVE_PIXBUF 737# ifdef HAVE_PIXBUF
650 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL); 738 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL);
651 if (image) 739 if (image)
652 { 740 {
653 if (pixbuf) 741 if (pixbuf)
654 g_object_unref (pixbuf); 742 g_object_unref (pixbuf);
655 pixbuf = image; 743 pixbuf = image;
656 bg_flags |= BG_IS_FROM_FILE;
657 ret = true; 744 ret = true;
658 } 745 }
659# endif 746# endif
660 747
661 if (ret) 748 if (ret)
662 { 749 {
663 if (p) 750 alpha = 0xffff;
664 bg_set_geometry (p + 1); 751 flags = IM_IS_SET | IM_IS_SIZE_SENSITIVE;
665 else 752 h_scale = v_scale = defaultScale;
666 bg_set_default_geometry (); 753 h_align = v_align = defaultAlign;
667 } 754 }
668 755
669 return ret; 756 return ret;
670} 757}
671 758
672# endif /* BG_IMAGE_FROM_FILE */ 759# endif /* BG_IMAGE_FROM_FILE */
673 760
674# ifdef ENABLE_TRANSPARENCY 761# ifdef ENABLE_TRANSPARENCY
675bool
676rxvt_term::bg_set_transparent ()
677{
678 if (!(bg_flags & BG_IS_TRANSPARENT))
679 {
680 bg_flags |= BG_IS_TRANSPARENT;
681 return true;
682 }
683
684 return false;
685}
686
687bool 762bool
688rxvt_term::bg_set_blur (const char *geom) 763rxvt_term::bg_set_blur (const char *geom)
689{ 764{
690 bool changed = false; 765 bool changed = false;
691 unsigned int hr, vr; 766 unsigned int hr, vr;
710 { 785 {
711 changed = true; 786 changed = true;
712 v_blurRadius = vr; 787 v_blurRadius = vr;
713 } 788 }
714 789
715 if (h_blurRadius && v_blurRadius)
716 bg_flags |= BG_NEEDS_BLUR;
717 else
718 bg_flags &= ~BG_NEEDS_BLUR;
719
720 return changed; 790 return changed;
721}
722
723void
724rxvt_term::set_tint_shade_flags ()
725{
726 if (shade != 100 || (bg_flags & BG_TINT_SET))
727 bg_flags |= BG_NEEDS_TINT;
728 else
729 bg_flags &= ~BG_NEEDS_TINT;
730} 791}
731 792
732bool 793bool
733rxvt_term::bg_set_tint (rxvt_color &new_tint) 794rxvt_term::bg_set_tint (rxvt_color &new_tint)
734{ 795{
744 && (c.b <= 0x00ff || c.b >= 0xff00)) 805 && (c.b <= 0x00ff || c.b >= 0xff00))
745 bg_flags |= BG_TINT_BITAND; 806 bg_flags |= BG_TINT_BITAND;
746 else 807 else
747 bg_flags &= ~BG_TINT_BITAND; 808 bg_flags &= ~BG_TINT_BITAND;
748 809
749 set_tint_shade_flags ();
750 return true; 810 return true;
751 } 811 }
752 812
753 return false; 813 return false;
754} 814}
763 new_shade = 200 - (100 + new_shade); 823 new_shade = 200 - (100 + new_shade);
764 824
765 if (new_shade != shade) 825 if (new_shade != shade)
766 { 826 {
767 shade = new_shade; 827 shade = new_shade;
768 set_tint_shade_flags ();
769 return true; 828 return true;
770 } 829 }
771 830
772 return false; 831 return false;
773} 832}
794 params[i+2] = XDoubleToFixed (kernel[i] / sum); 853 params[i+2] = XDoubleToFixed (kernel[i] / sum);
795} 854}
796#endif 855#endif
797 856
798bool 857bool
799rxvt_term::blur_pixmap (Pixmap pixmap, Visual *visual, int width, int height, int depth) 858rxvt_term::blur_pixmap (Pixmap pixmap, int width, int height)
800{ 859{
801 bool ret = false; 860 bool ret = false;
802#if XRENDER 861#if XRENDER
803 if (!(bg_flags & BG_HAS_RENDER_CONV)) 862 if (!(bg_flags & BG_HAS_RENDER_CONV))
804 return false; 863 return false;
859#endif 918#endif
860 return ret; 919 return ret;
861} 920}
862 921
863bool 922bool
864rxvt_term::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height) 923rxvt_term::tint_pixmap (Pixmap pixmap, int width, int height)
865{ 924{
866 bool ret = false; 925 bool ret = false;
867 926
868 if (shade == 100 && (bg_flags & BG_TINT_BITAND)) 927 if (shade == 100 && (bg_flags & BG_TINT_BITAND))
869 { 928 {
1049 1108
1050 if (gc) 1109 if (gc)
1051 { 1110 {
1052 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height); 1111 XFillRectangle (dpy, bg_pixmap, gc, 0, 0, window_width, window_height);
1053 ret = true; 1112 ret = true;
1054 unsigned long tr_flags = bg_flags & BG_EFFECTS_FLAGS; 1113 bool need_blur = h_blurRadius && v_blurRadius;
1114 bool need_tint = shade != 100 || (bg_flags & BG_TINT_SET);
1055 1115
1056 if (!(bg_flags & BG_CLIENT_RENDER)) 1116 if (!(bg_flags & BG_CLIENT_RENDER))
1057 { 1117 {
1058 if (bg_flags & BG_NEEDS_BLUR) 1118 if (need_blur)
1059 { 1119 {
1060 if (blur_pixmap (bg_pixmap, visual, window_width, window_height, depth)) 1120 if (blur_pixmap (bg_pixmap, window_width, window_height))
1061 tr_flags &= ~BG_NEEDS_BLUR; 1121 need_blur = false;
1062 } 1122 }
1063 if (bg_flags & BG_NEEDS_TINT) 1123 if (need_tint)
1064 { 1124 {
1065 if (tint_pixmap (bg_pixmap, visual, window_width, window_height)) 1125 if (tint_pixmap (bg_pixmap, window_width, window_height))
1066 tr_flags &= ~BG_NEEDS_TINT; 1126 need_tint = false;
1067 } 1127 }
1068 if (tr_flags & BG_NEEDS_TINT) 1128 if (need_tint)
1069 { 1129 {
1070 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap); 1130 XImage *ximage = XGetImage (dpy, bg_pixmap, 0, 0, bg_pmap_width, bg_pmap_height, AllPlanes, ZPixmap);
1071 if (ximage) 1131 if (ximage)
1072 { 1132 {
1073 /* our own client-side tinting */ 1133 /* our own client-side tinting */
1074 tint_ximage (DefaultVisual (dpy, display->screen), ximage); 1134 tint_ximage (ximage);
1075 1135
1076 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height); 1136 XPutImage (dpy, bg_pixmap, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
1077 XDestroyImage (ximage); 1137 XDestroyImage (ximage);
1078 } 1138 }
1079 } 1139 }
1100# endif /* ENABLE_TRANSPARENCY */ 1160# endif /* ENABLE_TRANSPARENCY */
1101 1161
1102bool 1162bool
1103rxvt_term::bg_render () 1163rxvt_term::bg_render ()
1104{ 1164{
1105 bool transparent = false;
1106
1107 bg_invalidate (); 1165 bg_invalidate ();
1108# ifdef ENABLE_TRANSPARENCY 1166# ifdef ENABLE_TRANSPARENCY
1109 if (bg_flags & BG_IS_TRANSPARENT) 1167 if (bg_flags & BG_IS_TRANSPARENT)
1110 { 1168 {
1111 /* we need to re-generate transparency pixmap in that case ! */ 1169 /* we need to re-generate transparency pixmap in that case ! */
1112 transparent = make_transparency_pixmap (); 1170 if (make_transparency_pixmap ())
1113 if (transparent)
1114 bg_flags |= BG_IS_VALID; 1171 bg_flags |= BG_IS_VALID;
1115 } 1172 }
1116# endif 1173# endif
1117 1174
1118# ifdef BG_IMAGE_FROM_FILE 1175# ifdef BG_IMAGE_FROM_FILE
1119 if (bg_flags & BG_IS_FROM_FILE) 1176 for (vector<rxvt_image>::iterator bg_image = image_vec.begin (); bg_image < image_vec.end (); bg_image++)
1120 { 1177 {
1121 if (render_image (transparent)) 1178 if (render_image (*bg_image))
1122 bg_flags |= BG_IS_VALID; 1179 bg_flags |= BG_IS_VALID;
1123 } 1180 }
1124# endif 1181# endif
1125 1182
1126 if (!(bg_flags & BG_IS_VALID)) 1183 if (!(bg_flags & BG_IS_VALID))
1160 bg_flags |= BG_HAS_RENDER_CONV; 1217 bg_flags |= BG_HAS_RENDER_CONV;
1161 1218
1162 XFree (filters); 1219 XFree (filters);
1163 } 1220 }
1164#endif 1221#endif
1222
1223#ifdef BG_IMAGE_FROM_FILE
1224 if (rs[Rs_backgroundPixmap])
1225 {
1226 rxvt_image *image = new_image ();
1227 if (!image->set_file_geometry (rs[Rs_backgroundPixmap]))
1228 image_vec.pop_back ();
1229 }
1230
1231# ifndef NO_RESOURCES
1232 find_resources ("image", "Image", XrmEnumAllLevels, rxvt_define_image);
1233 vector<rxvt_image>::iterator bg_image = image_vec.begin ();
1234 while (bg_image != image_vec.end ())
1235 {
1236 if (!(bg_image->flags & IM_IS_SET))
1237 bg_image = image_vec.erase (bg_image);
1238 else
1239 bg_image++;
1240 }
1241# endif
1242
1243 if (image_vec.size () > 0
1244 && !bg_window_position_sensitive ())
1245 update_background ();
1246#endif
1165} 1247}
1166 1248
1167#endif /* HAVE_BG_PIXMAP */ 1249#endif /* HAVE_BG_PIXMAP */
1168 1250
1169#ifdef ENABLE_TRANSPARENCY 1251#ifdef ENABLE_TRANSPARENCY
1180 lookup[i] = (tmp / 0xffff) << sh; 1262 lookup[i] = (tmp / 0xffff) << sh;
1181 } 1263 }
1182} 1264}
1183 1265
1184void 1266void
1185rxvt_term::tint_ximage (Visual *visual, XImage *ximage) 1267rxvt_term::tint_ximage (XImage *ximage)
1186{ 1268{
1187 unsigned int size_r, size_g, size_b; 1269 unsigned int size_r, size_g, size_b;
1188 int sh_r, sh_g, sh_b; 1270 int sh_r, sh_g, sh_b;
1189 uint32_t mask_r, mask_g, mask_b; 1271 uint32_t mask_r, mask_g, mask_b;
1190 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b; 1272 uint32_t *lookup, *lookup_r, *lookup_g, *lookup_b;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines