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> |
8 | * Copyright (c) 2010-2012 Emanuele Giaquinta <e.giaquinta@glauco.it> |
8 | * Copyright (c) 2010-2012 Emanuele Giaquinta <e.giaquinta@glauco.it> |
9 | * |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
11 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation; either version 2 of the License, or |
12 | * the Free Software Foundation; either version 3 of the License, or |
13 | * (at your option) any later version. |
13 | * (at your option) any later version. |
14 | * |
14 | * |
15 | * This program is distributed in the hope that it will be useful, |
15 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
… | |
… | |
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 | root_img = 0; |
36 | # endif |
36 | # endif |
37 | |
37 | |
38 | # if BG_IMAGE_FROM_FILE |
38 | # if BG_IMAGE_FROM_FILE |
39 | fimage.destroy (); |
39 | fimage.destroy (); |
40 | # endif |
40 | # endif |
41 | |
|
|
42 | delete bg_img; |
|
|
43 | bg_img = 0; |
|
|
44 | } |
|
|
45 | |
|
|
46 | bool |
|
|
47 | rxvt_term::bg_set_position (int x, int y) |
|
|
48 | { |
|
|
49 | |
|
|
50 | if (target_x != x |
|
|
51 | || target_y != y) |
|
|
52 | { |
|
|
53 | target_x = x; |
|
|
54 | target_y = y; |
|
|
55 | return true; |
|
|
56 | } |
|
|
57 | return false; |
|
|
58 | } |
41 | } |
59 | |
42 | |
60 | bool |
43 | bool |
61 | rxvt_term::bg_window_size_sensitive () |
44 | rxvt_term::bg_window_size_sensitive () |
62 | { |
45 | { |
63 | # if ENABLE_TRANSPARENCY |
46 | # if BG_IMAGE_FROM_ROOT |
64 | if (bg_flags & BG_IS_TRANSPARENT) |
47 | if (root_img) |
65 | return true; |
48 | return true; |
66 | # endif |
49 | # endif |
67 | |
50 | |
68 | # if BG_IMAGE_FROM_FILE |
51 | # if BG_IMAGE_FROM_FILE |
69 | if (fimage.img) |
52 | if (fimage.img) |
… | |
… | |
79 | } |
62 | } |
80 | |
63 | |
81 | bool |
64 | bool |
82 | rxvt_term::bg_window_position_sensitive () |
65 | rxvt_term::bg_window_position_sensitive () |
83 | { |
66 | { |
84 | # if ENABLE_TRANSPARENCY |
67 | # if BG_IMAGE_FROM_ROOT |
85 | if (bg_flags & BG_IS_TRANSPARENT) |
68 | if (root_img) |
86 | return true; |
69 | return true; |
87 | # endif |
70 | # endif |
88 | |
71 | |
89 | # if BG_IMAGE_FROM_FILE |
72 | # if BG_IMAGE_FROM_FILE |
90 | if (fimage.img) |
73 | if (fimage.img) |
… | |
… | |
105 | return lerp (0, window_size - image_size, align); |
88 | return lerp (0, window_size - image_size, align); |
106 | else if (align > 100) |
89 | else if (align > 100) |
107 | return lerp (window_size - image_size, window_size, align - 100); |
90 | return lerp (window_size - image_size, window_size, align - 100); |
108 | else |
91 | else |
109 | return lerp (-image_size, 0, align + 100); |
92 | return lerp (-image_size, 0, align + 100); |
110 | } |
|
|
111 | |
|
|
112 | static inline int |
|
|
113 | make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size) |
|
|
114 | { |
|
|
115 | int src_pos = 0; |
|
|
116 | dst_pos = pos; |
|
|
117 | dst_size = size; |
|
|
118 | if (pos < 0) |
|
|
119 | { |
|
|
120 | src_pos = -pos; |
|
|
121 | dst_pos = 0; |
|
|
122 | dst_size += pos; |
|
|
123 | } |
|
|
124 | |
|
|
125 | min_it (dst_size, target_size - dst_pos); |
|
|
126 | return src_pos; |
|
|
127 | } |
93 | } |
128 | |
94 | |
129 | static void |
95 | static void |
130 | 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) |
131 | { |
97 | { |
… | |
… | |
286 | |
252 | |
287 | return changed; |
253 | return changed; |
288 | } |
254 | } |
289 | |
255 | |
290 | void |
256 | void |
291 | rxvt_term::get_image_geometry (rxvt_image &image, int &w, int &h, int &x, int &y) |
257 | rxvt_term::render_image (rxvt_image &image) |
292 | { |
258 | { |
293 | int image_width = image.img->w; |
259 | int image_width = image.img->w; |
294 | int image_height = image.img->h; |
260 | int image_height = image.img->h; |
295 | int target_width = szHint.width; |
261 | int parent_width = szHint.width; |
296 | int target_height = szHint.height; |
262 | int parent_height = szHint.height; |
297 | int h_scale = min (image.h_scale, 32767 * 100 / target_width); |
263 | int h_scale = min (image.h_scale, 32767 * 100 / parent_width); |
298 | int v_scale = min (image.v_scale, 32767 * 100 / target_height); |
264 | int v_scale = min (image.v_scale, 32767 * 100 / parent_height); |
299 | |
265 | |
|
|
266 | int w; |
|
|
267 | int h; |
|
|
268 | int x; |
|
|
269 | int y; |
|
|
270 | |
300 | w = h_scale * target_width / 100; |
271 | w = h_scale * parent_width / 100; |
301 | h = v_scale * target_height / 100; |
272 | h = v_scale * parent_height / 100; |
302 | |
273 | |
303 | if (image.flags & IM_KEEP_ASPECT) |
274 | if (image.flags & IM_KEEP_ASPECT) |
304 | { |
275 | { |
305 | float scale = (float)w / image_width; |
276 | float scale = (float)w / image_width; |
306 | min_it (scale, (float)h / image_height); |
277 | min_it (scale, (float)h / image_height); |
… | |
… | |
311 | if (!w) w = image_width; |
282 | if (!w) w = image_width; |
312 | if (!h) h = image_height; |
283 | if (!h) h = image_height; |
313 | |
284 | |
314 | if (image.flags & IM_ROOT_ALIGN) |
285 | if (image.flags & IM_ROOT_ALIGN) |
315 | { |
286 | { |
316 | x = -target_x; |
287 | x = -parent_x; |
317 | y = -target_y; |
288 | y = -parent_y; |
318 | } |
289 | } |
319 | else |
290 | else |
320 | { |
291 | { |
321 | x = make_align_position (image.h_align, target_width, w); |
292 | x = make_align_position (image.h_align, parent_width, w); |
322 | y = make_align_position (image.v_align, target_height, h); |
293 | y = make_align_position (image.v_align, parent_height, h); |
323 | } |
294 | } |
324 | } |
|
|
325 | |
|
|
326 | bool |
|
|
327 | rxvt_term::render_image (rxvt_image &image) |
|
|
328 | { |
|
|
329 | int target_width = szHint.width; |
|
|
330 | int target_height = szHint.height; |
|
|
331 | |
|
|
332 | int x = 0; |
|
|
333 | int y = 0; |
|
|
334 | int w = 0; |
|
|
335 | int h = 0; |
|
|
336 | |
|
|
337 | get_image_geometry (image, w, h, x, y); |
|
|
338 | |
295 | |
339 | if (!(image.flags & IM_ROOT_ALIGN) |
296 | if (!(image.flags & IM_ROOT_ALIGN) |
340 | && (x >= target_width |
297 | && (x >= parent_width |
341 | || y >= target_height |
298 | || y >= parent_height |
342 | || x + w <= 0 |
299 | || x + w <= 0 |
343 | || y + h <= 0)) |
300 | || y + h <= 0)) |
344 | return false; |
301 | return; |
345 | |
302 | |
346 | rxvt_img *img = image.img->scale (w, h); |
303 | rxvt_img *img = image.img->scale (w, h); |
347 | |
304 | |
348 | if (image.flags & IM_TILE) |
305 | if (image.flags & IM_TILE) |
349 | img->repeat_mode (RepeatNormal); |
306 | img->repeat_mode (RepeatNormal); |
350 | else |
307 | else |
351 | img->repeat_mode (RepeatNone); |
308 | img->repeat_mode (RepeatNone); |
352 | img->sub_rect (-x, -y, target_width, target_height)->replace (img); |
309 | img->sub_rect (-x, -y, parent_width, parent_height)->replace (img); |
353 | |
310 | |
354 | if (bg_flags & BG_IS_VALID) |
311 | if (bg_img) |
355 | { |
312 | img->draw (bg_img, PictOpOver, image.alpha * 1. / 0xffff); |
356 | double factor = image.alpha * 1. / 0xffff; |
|
|
357 | bg_img->blend (img, factor)->replace (img); |
|
|
358 | } |
|
|
359 | |
313 | |
360 | XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); |
314 | XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); |
361 | img->convert_format (format, pix_colors [Color_bg])->replace (img); |
315 | img->convert_format (format, pix_colors [Color_bg])->replace (img); |
362 | |
316 | |
363 | delete bg_img; |
317 | delete bg_img; |
364 | bg_img = img; |
318 | bg_img = img; |
365 | |
|
|
366 | return true; |
|
|
367 | } |
319 | } |
368 | |
320 | |
369 | rxvt_image::rxvt_image () |
321 | rxvt_image::rxvt_image () |
370 | { |
322 | { |
371 | alpha = 0xffff; |
323 | alpha = 0xffff; |
… | |
… | |
376 | v_align = defaultAlign; |
328 | v_align = defaultAlign; |
377 | |
329 | |
378 | img = 0; |
330 | img = 0; |
379 | } |
331 | } |
380 | |
332 | |
381 | bool |
333 | void |
382 | rxvt_image::set_file_geometry (rxvt_screen *s, const char *file) |
334 | rxvt_image::set_file_geometry (rxvt_screen *s, const char *file) |
383 | { |
335 | { |
384 | if (!file || !*file) |
336 | if (!file || !*file) |
385 | return false; |
337 | return; |
386 | |
338 | |
387 | const char *p = strchr (file, ';'); |
339 | const char *p = strchr (file, ';'); |
388 | |
340 | |
389 | if (p) |
341 | if (p) |
390 | { |
342 | { |
… | |
… | |
393 | memcpy (f, file, len); |
345 | memcpy (f, file, len); |
394 | f[len] = '\0'; |
346 | f[len] = '\0'; |
395 | file = f; |
347 | file = f; |
396 | } |
348 | } |
397 | |
349 | |
398 | bool ret = set_file (s, file); |
350 | set_file (s, file); |
399 | alpha = 0x8000; |
351 | alpha = 0x8000; |
400 | if (ret) |
|
|
401 | set_geometry (p ? p + 1 : ""); |
352 | set_geometry (p ? p + 1 : ""); |
402 | return ret; |
|
|
403 | } |
353 | } |
404 | |
354 | |
405 | bool |
355 | void |
406 | rxvt_image::set_file (rxvt_screen *s, const char *file) |
356 | rxvt_image::set_file (rxvt_screen *s, const char *file) |
407 | { |
357 | { |
|
|
358 | rxvt_img *img2 = rxvt_img::new_from_file (s, file); |
408 | delete img; |
359 | delete img; |
409 | img = rxvt_img::new_from_file (s, file); |
360 | img = img2; |
410 | return img != 0; |
|
|
411 | } |
361 | } |
412 | |
362 | |
413 | # endif /* BG_IMAGE_FROM_FILE */ |
363 | # endif /* BG_IMAGE_FROM_FILE */ |
414 | |
364 | |
415 | bool |
365 | bool |
… | |
… | |
473 | } |
423 | } |
474 | |
424 | |
475 | return false; |
425 | return false; |
476 | } |
426 | } |
477 | |
427 | |
478 | # if ENABLE_TRANSPARENCY |
428 | # if BG_IMAGE_FROM_ROOT |
479 | /* |
429 | /* |
480 | * Builds a pixmap of the same size as the terminal window that contains |
430 | * Builds a pixmap of the same size as the terminal window that contains |
481 | * the tiled portion of the root pixmap that is supposed to be covered by |
431 | * the tiled portion of the root pixmap that is supposed to be covered by |
482 | * our window. |
432 | * our window. |
483 | */ |
433 | */ |
484 | bool |
434 | void |
485 | rxvt_term::render_root_image () |
435 | rxvt_term::render_root_image () |
486 | { |
436 | { |
487 | /* root dimensions may change from call to call - but Display structure should |
437 | /* root dimensions may change from call to call - but Display structure should |
488 | * be always up-to-date, so let's use it : |
438 | * be always up-to-date, so let's use it : |
489 | */ |
439 | */ |
490 | int screen = display->screen; |
440 | int screen = display->screen; |
491 | int root_width = DisplayWidth (dpy, screen); |
441 | int root_width = DisplayWidth (dpy, screen); |
492 | int root_height = DisplayHeight (dpy, screen); |
442 | int root_height = DisplayHeight (dpy, screen); |
493 | int window_width = szHint.width; |
443 | int parent_width = szHint.width; |
494 | int window_height = szHint.height; |
444 | int parent_height = szHint.height; |
495 | int sx, sy; |
445 | int sx, sy; |
496 | |
446 | |
497 | sx = target_x; |
447 | sx = parent_x; |
498 | sy = target_y; |
448 | sy = parent_y; |
499 | |
|
|
500 | if (!root_img) |
|
|
501 | return false; |
|
|
502 | |
449 | |
503 | /* check if we are outside of the visible part of the virtual screen : */ |
450 | /* check if we are outside of the visible part of the virtual screen : */ |
504 | if (sx + window_width <= 0 || sy + window_height <= 0 |
451 | if (sx + parent_width <= 0 || sy + parent_height <= 0 |
505 | || sx >= root_width || sy >= root_height) |
452 | || sx >= root_width || sy >= root_height) |
506 | return 0; |
453 | return; |
507 | |
454 | |
508 | while (sx < 0) sx += root_img->w; |
455 | while (sx < 0) sx += root_img->w; |
509 | while (sy < 0) sy += root_img->h; |
456 | while (sy < 0) sy += root_img->h; |
510 | |
457 | |
511 | rxvt_img *img = root_img->sub_rect (sx, sy, window_width, window_height); |
458 | rxvt_img *img = root_img->sub_rect (sx, sy, parent_width, parent_height); |
512 | |
459 | |
513 | if (root_effects.need_blur ()) |
460 | if (root_effects.need_blur ()) |
514 | img->blur (root_effects.h_blurRadius, root_effects.v_blurRadius)->replace (img); |
461 | img->blur (root_effects.h_blurRadius, root_effects.v_blurRadius)->replace (img); |
515 | |
462 | |
516 | if (root_effects.need_tint ()) |
463 | if (root_effects.need_tint ()) |
517 | tint_image (img, root_effects.tint, root_effects.tint_set, root_effects.shade); |
464 | { |
|
|
465 | rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); |
|
|
466 | |
|
|
467 | if (root_effects.tint_set) |
|
|
468 | root_effects.tint.get (c); |
|
|
469 | rxvt_img::nv factor = root_effects.shade / 100. - 1.; |
|
|
470 | img->shade (factor, c)->replace (img); |
|
|
471 | } |
518 | |
472 | |
519 | XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); |
473 | XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual); |
520 | img->convert_format (format, pix_colors [Color_bg])->replace (img); |
474 | img->convert_format (format, pix_colors [Color_bg])->replace (img); |
521 | |
475 | |
522 | delete bg_img; |
476 | delete bg_img; |
523 | bg_img = img; |
477 | bg_img = img; |
524 | |
|
|
525 | return true; |
|
|
526 | } |
478 | } |
527 | |
479 | # endif /* BG_IMAGE_FROM_ROOT */ |
528 | void |
|
|
529 | rxvt_term::bg_set_root_pixmap () |
|
|
530 | { |
|
|
531 | delete root_img; |
|
|
532 | root_img = rxvt_img::new_from_root (this); |
|
|
533 | } |
|
|
534 | # endif /* ENABLE_TRANSPARENCY */ |
|
|
535 | |
480 | |
536 | void |
481 | void |
537 | rxvt_term::bg_render () |
482 | rxvt_term::bg_render () |
538 | { |
483 | { |
539 | if (bg_flags & BG_INHIBIT_RENDER) |
484 | if (bg_flags & BG_INHIBIT_RENDER) |
540 | return; |
485 | return; |
541 | |
486 | |
542 | bg_invalidate (); |
487 | delete bg_img; |
543 | # if ENABLE_TRANSPARENCY |
488 | bg_img = 0; |
544 | if (bg_flags & BG_IS_TRANSPARENT) |
489 | bg_flags = 0; |
|
|
490 | |
|
|
491 | if (!mapped) |
|
|
492 | return; |
|
|
493 | |
|
|
494 | # if BG_IMAGE_FROM_ROOT |
|
|
495 | if (root_img) |
545 | { |
496 | { |
546 | /* we need to re-generate transparency pixmap in that case ! */ |
|
|
547 | if (render_root_image ()) |
497 | render_root_image (); |
548 | bg_flags |= BG_IS_VALID; |
498 | bg_flags |= BG_IS_TRANSPARENT; |
549 | } |
499 | } |
550 | # endif |
500 | # endif |
551 | |
501 | |
552 | # if BG_IMAGE_FROM_FILE |
502 | # if BG_IMAGE_FROM_FILE |
553 | if (fimage.img) |
503 | if (fimage.img) |
554 | { |
|
|
555 | if (render_image (fimage)) |
504 | render_image (fimage); |
556 | bg_flags |= BG_IS_VALID; |
|
|
557 | } |
|
|
558 | # endif |
505 | # endif |
559 | |
|
|
560 | if (!(bg_flags & BG_IS_VALID)) |
|
|
561 | { |
|
|
562 | delete bg_img; |
|
|
563 | bg_img = 0; |
|
|
564 | } |
|
|
565 | |
506 | |
566 | scr_recolour (false); |
507 | scr_recolour (false); |
567 | bg_flags |= BG_NEEDS_REFRESH; |
508 | bg_flags |= BG_NEEDS_REFRESH; |
568 | |
509 | |
569 | bg_valid_since = ev::now (); |
510 | bg_valid_since = ev::now (); |
570 | } |
511 | } |
571 | |
512 | |
572 | void |
513 | void |
573 | rxvt_term::bg_init () |
514 | rxvt_term::bg_init () |
574 | { |
515 | { |
|
|
516 | #if BG_IMAGE_FROM_ROOT |
|
|
517 | if (option (Opt_transparent)) |
|
|
518 | { |
|
|
519 | if (rs [Rs_blurradius]) |
|
|
520 | root_effects.set_blur (rs [Rs_blurradius]); |
|
|
521 | |
|
|
522 | if (ISSET_PIXCOLOR (Color_tint)) |
|
|
523 | root_effects.set_tint (pix_colors_focused [Color_tint]); |
|
|
524 | |
|
|
525 | if (rs [Rs_shade]) |
|
|
526 | root_effects.set_shade (rs [Rs_shade]); |
|
|
527 | |
|
|
528 | rxvt_img::new_from_root (this)->replace (root_img); |
|
|
529 | XSelectInput (dpy, display->root, PropertyChangeMask); |
|
|
530 | rootwin_ev.start (display, display->root); |
|
|
531 | } |
|
|
532 | #endif |
|
|
533 | |
575 | #if BG_IMAGE_FROM_FILE |
534 | #if BG_IMAGE_FROM_FILE |
576 | if (rs[Rs_backgroundPixmap]) |
535 | if (rs[Rs_backgroundPixmap]) |
577 | { |
536 | { |
578 | if (fimage.set_file_geometry (this, rs[Rs_backgroundPixmap]) |
537 | fimage.set_file_geometry (this, rs[Rs_backgroundPixmap]); |
579 | && !bg_window_position_sensitive ()) |
538 | if (!bg_window_position_sensitive ()) |
580 | update_background (); |
539 | update_background (); |
581 | } |
540 | } |
582 | #endif |
541 | #endif |
583 | } |
542 | } |
584 | |
543 | |
585 | void |
|
|
586 | rxvt_term::tint_image (rxvt_img *img, rxvt_color &tint, bool tint_set, int shade) |
|
|
587 | { |
|
|
588 | rgba c (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC); |
|
|
589 | |
|
|
590 | if (tint_set) |
|
|
591 | tint.get (c); |
|
|
592 | |
|
|
593 | if (shade > 100) |
|
|
594 | { |
|
|
595 | c.r = c.r * (200 - shade) / 100; |
|
|
596 | c.g = c.g * (200 - shade) / 100; |
|
|
597 | c.b = c.b * (200 - shade) / 100; |
|
|
598 | } |
|
|
599 | else |
|
|
600 | { |
|
|
601 | c.r = c.r * shade / 100; |
|
|
602 | c.g = c.g * shade / 100; |
|
|
603 | c.b = c.b * shade / 100; |
|
|
604 | } |
|
|
605 | |
|
|
606 | img->contrast (c.r, c.g, c.b, c.a); |
|
|
607 | |
|
|
608 | if (shade > 100) |
|
|
609 | { |
|
|
610 | c.a = 0xffff; |
|
|
611 | c.r = |
|
|
612 | c.g = |
|
|
613 | c.b = 0xffff * (shade - 100) / 100; |
|
|
614 | img->brightness (c.r, c.g, c.b, c.a); |
|
|
615 | } |
|
|
616 | } |
|
|
617 | |
|
|
618 | #endif /* HAVE_BG_PIXMAP */ |
544 | #endif /* HAVE_BG_PIXMAP */ |