1 | /*----------------------------------------------------------------------* |
1 | /*----------------------------------------------------------------------* |
2 | * File: background.C - former xpm.C |
2 | * File: background.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 2005-2008 Marc Lehmann <schmorp@schmorp.de> |
6 | * Copyright (c) 2005-2008 Marc Lehmann <schmorp@schmorp.de> |
7 | * Copyright (c) 2007 Sasha Vasko <sasha@aftercode.net> |
7 | * Copyright (c) 2007 Sasha Vasko <sasha@aftercode.net> |
… | |
… | |
28 | #ifdef HAVE_BG_PIXMAP |
28 | #ifdef HAVE_BG_PIXMAP |
29 | |
29 | |
30 | void |
30 | void |
31 | rxvt_term::bg_destroy () |
31 | rxvt_term::bg_destroy () |
32 | { |
32 | { |
33 | # if ENABLE_TRANSPARENCY |
33 | # if BG_IMAGE_FROM_ROOT |
34 | delete root_img; |
34 | delete root_img; |
|
|
35 | root_img = 0; |
35 | # endif |
36 | # endif |
36 | |
37 | |
37 | # if BG_IMAGE_FROM_FILE |
38 | # if BG_IMAGE_FROM_FILE |
38 | fimage.destroy (); |
39 | fimage.destroy (); |
39 | # endif |
40 | # endif |
40 | |
|
|
41 | delete bg_img; |
|
|
42 | } |
|
|
43 | |
|
|
44 | bool |
|
|
45 | rxvt_term::bg_set_position (int x, int y) |
|
|
46 | { |
|
|
47 | |
|
|
48 | if (target_x != x |
|
|
49 | || target_y != y) |
|
|
50 | { |
|
|
51 | target_x = x; |
|
|
52 | target_y = y; |
|
|
53 | return true; |
|
|
54 | } |
|
|
55 | return false; |
|
|
56 | } |
41 | } |
57 | |
42 | |
58 | bool |
43 | bool |
59 | rxvt_term::bg_window_size_sensitive () |
44 | rxvt_term::bg_window_size_sensitive () |
60 | { |
45 | { |
61 | # if ENABLE_TRANSPARENCY |
46 | # if BG_IMAGE_FROM_ROOT |
62 | if (bg_flags & BG_IS_TRANSPARENT) |
47 | if (option (Opt_transparent)) |
63 | return true; |
48 | return true; |
64 | # endif |
49 | # endif |
65 | |
50 | |
66 | # if BG_IMAGE_FROM_FILE |
51 | # if BG_IMAGE_FROM_FILE |
67 | if (fimage.img) |
52 | if (fimage.img) |
… | |
… | |
77 | } |
62 | } |
78 | |
63 | |
79 | bool |
64 | bool |
80 | rxvt_term::bg_window_position_sensitive () |
65 | rxvt_term::bg_window_position_sensitive () |
81 | { |
66 | { |
82 | # if ENABLE_TRANSPARENCY |
67 | # if BG_IMAGE_FROM_ROOT |
83 | if (bg_flags & BG_IS_TRANSPARENT) |
68 | if (option (Opt_transparent)) |
84 | return true; |
69 | return true; |
85 | # endif |
70 | # endif |
86 | |
71 | |
87 | # if BG_IMAGE_FROM_FILE |
72 | # if BG_IMAGE_FROM_FILE |
88 | if (fimage.img) |
73 | if (fimage.img) |
… | |
… | |
103 | return lerp (0, window_size - image_size, align); |
88 | return lerp (0, window_size - image_size, align); |
104 | else if (align > 100) |
89 | else if (align > 100) |
105 | return lerp (window_size - image_size, window_size, align - 100); |
90 | return lerp (window_size - image_size, window_size, align - 100); |
106 | else |
91 | else |
107 | return lerp (-image_size, 0, align + 100); |
92 | return lerp (-image_size, 0, align + 100); |
108 | } |
|
|
109 | |
|
|
110 | static inline int |
|
|
111 | make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size) |
|
|
112 | { |
|
|
113 | int src_pos = 0; |
|
|
114 | dst_pos = pos; |
|
|
115 | dst_size = size; |
|
|
116 | if (pos < 0) |
|
|
117 | { |
|
|
118 | src_pos = -pos; |
|
|
119 | dst_pos = 0; |
|
|
120 | dst_size += pos; |
|
|
121 | } |
|
|
122 | |
|
|
123 | min_it (dst_size, target_size - dst_pos); |
|
|
124 | return src_pos; |
|
|
125 | } |
93 | } |
126 | |
94 | |
127 | static void |
95 | static void |
128 | parse_style (const char *style, int &x, int &y, unsigned int &w, unsigned int &h, uint8_t &flags) |
96 | parse_style (const char *style, int &x, int &y, unsigned int &w, unsigned int &h, uint8_t &flags) |
129 | { |
97 | { |
… | |
… | |
288 | void |
256 | void |
289 | rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y) |
257 | rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y) |
290 | { |
258 | { |
291 | int image_width = image.img->w; |
259 | int image_width = image.img->w; |
292 | int image_height = image.img->h; |
260 | int image_height = image.img->h; |
293 | int target_width = szHint.width; |
261 | int parent_width = szHint.width; |
294 | int target_height = szHint.height; |
262 | int parent_height = szHint.height; |
295 | int h_scale = min (image.h_scale, 32767 * 100 / target_width); |
263 | int h_scale = min (image.h_scale, 32767 * 100 / parent_width); |
296 | int v_scale = min (image.v_scale, 32767 * 100 / target_height); |
264 | int v_scale = min (image.v_scale, 32767 * 100 / parent_height); |
297 | |
265 | |
298 | w = h_scale * target_width / 100; |
266 | w = h_scale * parent_width / 100; |
299 | h = v_scale * target_height / 100; |
267 | h = v_scale * parent_height / 100; |
300 | |
268 | |
301 | if (image.flags & IM_KEEP_ASPECT) |
269 | if (image.flags & IM_KEEP_ASPECT) |
302 | { |
270 | { |
303 | float scale = (float)w / image_width; |
271 | float scale = (float)w / image_width; |
304 | min_it (scale, (float)h / image_height); |
272 | min_it (scale, (float)h / image_height); |
… | |
… | |
309 | if (!w) w = image_width; |
277 | if (!w) w = image_width; |
310 | if (!h) h = image_height; |
278 | if (!h) h = image_height; |
311 | |
279 | |
312 | if (image.flags & IM_ROOT_ALIGN) |
280 | if (image.flags & IM_ROOT_ALIGN) |
313 | { |
281 | { |
314 | x = -target_x; |
282 | x = -parent_x; |
315 | y = -target_y; |
283 | y = -parent_y; |
316 | } |
284 | } |
317 | else |
285 | else |
318 | { |
286 | { |
319 | x = make_align_position (image.h_align, target_width, w); |
287 | x = make_align_position (image.h_align, parent_width, w); |
320 | y = make_align_position (image.v_align, target_height, h); |
288 | y = make_align_position (image.v_align, parent_height, h); |
321 | } |
289 | } |
322 | } |
290 | } |
323 | |
291 | |
324 | bool |
292 | bool |
325 | rxvt_term::render_image (rxvt_image &image) |
293 | rxvt_term::render_image (rxvt_image &image) |
326 | { |
294 | { |
327 | int target_width = szHint.width; |
295 | int parent_width = szHint.width; |
328 | int target_height = szHint.height; |
296 | int parent_height = szHint.height; |
329 | |
297 | |
330 | int x = 0; |
298 | int x = 0; |
331 | int y = 0; |
299 | int y = 0; |
332 | int w = 0; |
300 | int w = 0; |
333 | int h = 0; |
301 | int h = 0; |
334 | |
302 | |
335 | get_image_geometry (image, w, h, x, y); |
303 | get_image_geometry (image, w, h, x, y); |
336 | |
304 | |
337 | if (!(image.flags & IM_ROOT_ALIGN) |
305 | if (!(image.flags & IM_ROOT_ALIGN) |
338 | && (x >= target_width |
306 | && (x >= parent_width |
339 | || y >= target_height |
307 | || y >= parent_height |
340 | || x + w <= 0 |
308 | || x + w <= 0 |
341 | || y + h <= 0)) |
309 | || y + h <= 0)) |
342 | return false; |
310 | return false; |
343 | |
311 | |
344 | rxvt_img *img = image.img->scale (w, h); |
312 | rxvt_img *img = image.img->scale (w, h); |
345 | |
313 | |
346 | if (image.flags & IM_TILE) |
314 | if (image.flags & IM_TILE) |
347 | img->repeat_mode (RepeatNormal); |
315 | img->repeat_mode (RepeatNormal); |
348 | else |
316 | else |
349 | img->repeat_mode (RepeatNone); |
317 | img->repeat_mode (RepeatNone); |
350 | img->sub_rect (-x, -y, target_width, target_height)->replace (img); |
318 | img->sub_rect (-x, -y, parent_width, parent_height)->replace (img); |
351 | |
319 | |
352 | if (bg_flags & BG_IS_VALID) |
320 | if (bg_flags & BG_IS_VALID) |
353 | { |
321 | img->draw (bg_img, PictOpOver, image.alpha * 1. / 0xffff); |
354 | double factor = image.alpha * 1. / 0xffff; |
|
|
355 | bg_img->blend (img, factor)->replace (img); |
|
|
356 | } |
|
|
357 | |
322 | |
358 | XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); |
323 | XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); |
359 | img->convert_format (format, pix_colors [Color_bg])->replace (img); |
324 | img->convert_format (format, pix_colors [Color_bg])->replace (img); |
360 | |
325 | |
361 | delete bg_img; |
326 | delete bg_img; |
… | |
… | |
374 | v_align = defaultAlign; |
339 | v_align = defaultAlign; |
375 | |
340 | |
376 | img = 0; |
341 | img = 0; |
377 | } |
342 | } |
378 | |
343 | |
379 | bool |
344 | void |
380 | rxvt_image::set_file_geometry (rxvt_screen *s, const char *file) |
345 | rxvt_image::set_file_geometry (rxvt_screen *s, const char *file) |
381 | { |
346 | { |
382 | if (!file || !*file) |
347 | if (!file || !*file) |
383 | return false; |
348 | return; |
384 | |
349 | |
385 | const char *p = strchr (file, ';'); |
350 | const char *p = strchr (file, ';'); |
386 | |
351 | |
387 | if (p) |
352 | if (p) |
388 | { |
353 | { |
… | |
… | |
391 | memcpy (f, file, len); |
356 | memcpy (f, file, len); |
392 | f[len] = '\0'; |
357 | f[len] = '\0'; |
393 | file = f; |
358 | file = f; |
394 | } |
359 | } |
395 | |
360 | |
396 | bool ret = set_file (s, file); |
361 | set_file (s, file); |
397 | alpha = 0x8000; |
362 | alpha = 0x8000; |
398 | if (ret) |
|
|
399 | set_geometry (p ? p + 1 : ""); |
363 | set_geometry (p ? p + 1 : ""); |
400 | return ret; |
|
|
401 | } |
364 | } |
402 | |
365 | |
403 | bool |
366 | void |
404 | rxvt_image::set_file (rxvt_screen *s, const char *file) |
367 | rxvt_image::set_file (rxvt_screen *s, const char *file) |
405 | { |
368 | { |
|
|
369 | rxvt_img *img2 = rxvt_img::new_from_file (s, file); |
406 | delete img; |
370 | delete img; |
407 | img = rxvt_img::new_from_file (s, file); |
371 | img = img2; |
408 | return img != 0; |
|
|
409 | } |
372 | } |
410 | |
373 | |
411 | # endif /* BG_IMAGE_FROM_FILE */ |
374 | # endif /* BG_IMAGE_FROM_FILE */ |
412 | |
375 | |
413 | bool |
376 | bool |
… | |
… | |
471 | } |
434 | } |
472 | |
435 | |
473 | return false; |
436 | return false; |
474 | } |
437 | } |
475 | |
438 | |
476 | # if ENABLE_TRANSPARENCY |
439 | # if BG_IMAGE_FROM_ROOT |
477 | /* |
440 | /* |
478 | * Builds a pixmap of the same size as the terminal window that contains |
441 | * Builds a pixmap of the same size as the terminal window that contains |
479 | * the tiled portion of the root pixmap that is supposed to be covered by |
442 | * the tiled portion of the root pixmap that is supposed to be covered by |
480 | * our window. |
443 | * our window. |
481 | */ |
444 | */ |
… | |
… | |
486 | * be always up-to-date, so let's use it : |
449 | * be always up-to-date, so let's use it : |
487 | */ |
450 | */ |
488 | int screen = display->screen; |
451 | int screen = display->screen; |
489 | int root_width = DisplayWidth (dpy, screen); |
452 | int root_width = DisplayWidth (dpy, screen); |
490 | int root_height = DisplayHeight (dpy, screen); |
453 | int root_height = DisplayHeight (dpy, screen); |
491 | int window_width = szHint.width; |
454 | int parent_width = szHint.width; |
492 | int window_height = szHint.height; |
455 | int parent_height = szHint.height; |
493 | int sx, sy; |
456 | int sx, sy; |
494 | |
457 | |
495 | sx = target_x; |
458 | sx = parent_x; |
496 | sy = target_y; |
459 | sy = parent_y; |
497 | |
460 | |
498 | if (!root_img) |
461 | if (!root_img) |
499 | return false; |
462 | return false; |
500 | |
463 | |
501 | /* check if we are outside of the visible part of the virtual screen : */ |
464 | /* check if we are outside of the visible part of the virtual screen : */ |
502 | if (sx + window_width <= 0 || sy + window_height <= 0 |
465 | if (sx + parent_width <= 0 || sy + parent_height <= 0 |
503 | || sx >= root_width || sy >= root_height) |
466 | || sx >= root_width || sy >= root_height) |
504 | return 0; |
467 | return 0; |
505 | |
468 | |
506 | while (sx < 0) sx += root_img->w; |
469 | while (sx < 0) sx += root_img->w; |
507 | while (sy < 0) sy += root_img->h; |
470 | while (sy < 0) sy += root_img->h; |
508 | |
471 | |
509 | rxvt_img *img = root_img->sub_rect (sx, sy, window_width, window_height); |
472 | rxvt_img *img = root_img->sub_rect (sx, sy, parent_width, parent_height); |
510 | |
473 | |
511 | if (root_effects.need_blur ()) |
474 | if (root_effects.need_blur ()) |
512 | img->blur (root_effects.h_blurRadius, root_effects.v_blurRadius)->replace (img); |
475 | img->blur (root_effects.h_blurRadius, root_effects.v_blurRadius)->replace (img); |
513 | |
476 | |
514 | if (root_effects.need_tint ()) |
477 | if (root_effects.need_tint ()) |
… | |
… | |
527 | rxvt_term::bg_set_root_pixmap () |
490 | rxvt_term::bg_set_root_pixmap () |
528 | { |
491 | { |
529 | delete root_img; |
492 | delete root_img; |
530 | root_img = rxvt_img::new_from_root (this); |
493 | root_img = rxvt_img::new_from_root (this); |
531 | } |
494 | } |
532 | # endif /* ENABLE_TRANSPARENCY */ |
495 | # endif /* BG_IMAGE_FROM_ROOT */ |
533 | |
496 | |
534 | void |
497 | void |
535 | rxvt_term::bg_render () |
498 | rxvt_term::bg_render () |
536 | { |
499 | { |
537 | if (bg_flags & BG_INHIBIT_RENDER) |
500 | if (bg_flags & BG_INHIBIT_RENDER) |
538 | return; |
501 | return; |
539 | |
502 | |
540 | bg_invalidate (); |
503 | bg_flags &= BG_INHIBIT_RENDER; |
541 | # if ENABLE_TRANSPARENCY |
504 | |
542 | if (bg_flags & BG_IS_TRANSPARENT) |
505 | # if BG_IMAGE_FROM_ROOT |
|
|
506 | if (option (Opt_transparent)) |
543 | { |
507 | { |
544 | /* we need to re-generate transparency pixmap in that case ! */ |
508 | /* we need to re-generate transparency pixmap in that case ! */ |
545 | if (render_root_image ()) |
509 | if (render_root_image ()) |
546 | bg_flags |= BG_IS_VALID; |
510 | bg_flags |= BG_IS_VALID | BG_IS_TRANSPARENT; |
547 | } |
511 | } |
548 | # endif |
512 | # endif |
549 | |
513 | |
550 | # if BG_IMAGE_FROM_FILE |
514 | # if BG_IMAGE_FROM_FILE |
551 | if (fimage.img) |
515 | if (fimage.img) |
… | |
… | |
568 | } |
532 | } |
569 | |
533 | |
570 | void |
534 | void |
571 | rxvt_term::bg_init () |
535 | rxvt_term::bg_init () |
572 | { |
536 | { |
|
|
537 | #if BG_IMAGE_FROM_ROOT |
|
|
538 | if (option (Opt_transparent)) |
|
|
539 | { |
|
|
540 | if (rs [Rs_blurradius]) |
|
|
541 | root_effects.set_blur (rs [Rs_blurradius]); |
|
|
542 | |
|
|
543 | if (ISSET_PIXCOLOR (Color_tint)) |
|
|
544 | root_effects.set_tint (pix_colors_focused [Color_tint]); |
|
|
545 | |
|
|
546 | if (rs [Rs_shade]) |
|
|
547 | root_effects.set_shade (rs [Rs_shade]); |
|
|
548 | |
|
|
549 | bg_set_root_pixmap (); |
|
|
550 | XSelectInput (dpy, display->root, PropertyChangeMask); |
|
|
551 | rootwin_ev.start (display, display->root); |
|
|
552 | } |
|
|
553 | #endif |
|
|
554 | |
573 | #if BG_IMAGE_FROM_FILE |
555 | #if BG_IMAGE_FROM_FILE |
574 | if (rs[Rs_backgroundPixmap]) |
556 | if (rs[Rs_backgroundPixmap]) |
575 | { |
557 | { |
576 | if (fimage.set_file_geometry (this, rs[Rs_backgroundPixmap]) |
558 | fimage.set_file_geometry (this, rs[Rs_backgroundPixmap]); |
577 | && !bg_window_position_sensitive ()) |
559 | if (!bg_window_position_sensitive ()) |
578 | update_background (); |
560 | update_background (); |
579 | } |
561 | } |
580 | #endif |
562 | #endif |
581 | } |
563 | } |
582 | |
564 | |