1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | #:META:X_RESOURCE:%.expr:string:background expression |
3 | #:META:RESOURCE:%.expr:string:background expression |
4 | #:META:X_RESOURCE:%.border:boolean:respect the terminal border |
4 | #:META:RESOURCE:%.border:boolean:respect the terminal border |
5 | #:META:X_RESOURCE:%.interval:seconds:minimum time between updates |
5 | #:META:RESOURCE:%.interval:seconds:minimum time between updates |
6 | |
6 | |
7 | =head1 NAME |
7 | =head1 NAME |
8 | |
8 | |
9 | background - manage terminal background |
9 | background - manage terminal background |
10 | |
10 | |
11 | =head1 SYNOPSIS |
11 | =head1 SYNOPSIS |
12 | |
12 | |
13 | urxvt --background-expr 'background expression' |
13 | urxvt --background-expr 'background expression' |
14 | --background-border |
14 | --background-border |
15 | --background-interval seconds |
15 | --background-interval seconds |
|
|
16 | |
|
|
17 | =head1 QUICK AND DIRTY CHEAT SHEET |
|
|
18 | |
|
|
19 | Just load a random jpeg image and tile the background with it without |
|
|
20 | scaling or anything else: |
|
|
21 | |
|
|
22 | load "/path/to/img.jpg" |
|
|
23 | |
|
|
24 | The same, but use mirroring/reflection instead of tiling: |
|
|
25 | |
|
|
26 | mirror load "/path/to/img.jpg" |
|
|
27 | |
|
|
28 | Load an image and scale it to exactly fill the terminal window: |
|
|
29 | |
|
|
30 | scale keep { load "/path/to/img.jpg" } |
|
|
31 | |
|
|
32 | Implement pseudo-transparency by using a suitably-aligned root pixmap |
|
|
33 | as window background: |
|
|
34 | |
|
|
35 | rootalign root |
|
|
36 | |
|
|
37 | Likewise, but keep a blurred copy: |
|
|
38 | |
|
|
39 | rootalign keep { blur 10, root } |
16 | |
40 | |
17 | =head1 DESCRIPTION |
41 | =head1 DESCRIPTION |
18 | |
42 | |
19 | This extension manages the terminal background by creating a picture that |
43 | This extension manages the terminal background by creating a picture that |
20 | is behind the text, replacing the normal background colour. |
44 | is behind the text, replacing the normal background colour. |
… | |
… | |
255 | =cut |
279 | =cut |
256 | |
280 | |
257 | our %_IMG_CACHE; |
281 | our %_IMG_CACHE; |
258 | our $HOME; |
282 | our $HOME; |
259 | our ($self, $frame); |
283 | our ($self, $frame); |
260 | our ($x, $y, $w, $h); |
284 | our ($x, $y, $w, $h, $focus); |
261 | |
285 | |
262 | # enforce at least this interval between updates |
286 | # enforce at least this interval between updates |
263 | our $MIN_INTERVAL = 6/59.951; |
287 | our $MIN_INTERVAL = 6/59.951; |
264 | |
288 | |
265 | { |
289 | { |
… | |
… | |
284 | |
308 | |
285 | Loads the image at the given C<$path>. The image is set to plane tiling |
309 | Loads the image at the given C<$path>. The image is set to plane tiling |
286 | mode. |
310 | mode. |
287 | |
311 | |
288 | If the image is already in memory (e.g. because another terminal instance |
312 | If the image is already in memory (e.g. because another terminal instance |
289 | uses it), then the in-memory copy us returned instead. |
313 | uses it), then the in-memory copy is returned instead. |
290 | |
314 | |
291 | =item load_uc $path |
315 | =item load_uc $path |
292 | |
316 | |
293 | Load uncached - same as load, but does not cache the image, which means it |
317 | Load uncached - same as load, but does not cache the image, which means it |
294 | is I<always> loaded from the filesystem again, even if another copy of it |
318 | is I<always> loaded from the filesystem again, even if another copy of it |
… | |
… | |
398 | for @_; |
422 | for @_; |
399 | |
423 | |
400 | $base |
424 | $base |
401 | } |
425 | } |
402 | |
426 | |
|
|
427 | =back |
|
|
428 | |
403 | =head2 TILING MODES |
429 | =head2 TILING MODES |
404 | |
430 | |
405 | The following operators modify the tiling mode of an image, that is, the |
431 | The following operators modify the tiling mode of an image, that is, the |
406 | way that pixels outside the image area are painted when the image is used. |
432 | way that pixels outside the image area are painted when the image is used. |
407 | |
433 | |
… | |
… | |
498 | |
524 | |
499 | Return the X and Y coordinates of the terminal window (the terminal |
525 | Return the X and Y coordinates of the terminal window (the terminal |
500 | window is the full window by default, and the character area only when in |
526 | window is the full window by default, and the character area only when in |
501 | border-respect mode). |
527 | border-respect mode). |
502 | |
528 | |
503 | Using these functions make your expression sensitive to window moves. |
529 | Using these functions makes your expression sensitive to window moves. |
504 | |
530 | |
505 | These functions are mainly useful to align images to the root window. |
531 | These functions are mainly useful to align images to the root window. |
506 | |
532 | |
507 | Example: load an image and align it so it looks as if anchored to the |
533 | Example: load an image and align it so it looks as if anchored to the |
508 | background (that's exactly what C<rootalign> does btw.): |
534 | background (that's exactly what C<rootalign> does btw.): |
509 | |
535 | |
510 | move -TX, -TY, keep { load "mybg.png" } |
536 | move -TX, -TY, keep { load "mybg.png" } |
511 | |
537 | |
512 | =item TW |
538 | =item TW |
|
|
539 | |
|
|
540 | =item TH |
513 | |
541 | |
514 | Return the width (C<TW>) and height (C<TH>) of the terminal window (the |
542 | Return the width (C<TW>) and height (C<TH>) of the terminal window (the |
515 | terminal window is the full window by default, and the character area only |
543 | terminal window is the full window by default, and the character area only |
516 | when in border-respect mode). |
544 | when in border-respect mode). |
517 | |
545 | |
518 | Using these functions make your expression sensitive to window resizes. |
546 | Using these functions makes your expression sensitive to window resizes. |
519 | |
547 | |
520 | These functions are mainly useful to scale images, or to clip images to |
548 | These functions are mainly useful to scale images, or to clip images to |
521 | the window size to conserve memory. |
549 | the window size to conserve memory. |
522 | |
550 | |
523 | Example: take the screen background, clip it to the window size, blur it a |
551 | Example: take the screen background, clip it to the window size, blur it a |
524 | bit, align it to the window position and use it as background. |
552 | bit, align it to the window position and use it as background. |
525 | |
553 | |
526 | clip move -TX, -TY, keep { blur 5, root } |
554 | clip move -TX, -TY, keep { blur 5, root } |
527 | |
555 | |
528 | =cut |
556 | =item FOCUS |
529 | |
557 | |
|
|
558 | Returns a boolean indicating whether the terminal window has keyboard |
|
|
559 | focus, in which case it returns true. |
|
|
560 | |
|
|
561 | Using this function makes your expression sensitive to focus changes. |
|
|
562 | |
|
|
563 | A common use case is to fade the background image when the terminal loses |
|
|
564 | focus, often together with the C<-fade> command line option. In fact, |
|
|
565 | there is a special function for just that use case: C<focus_fade>. |
|
|
566 | |
|
|
567 | Example: use two entirely different background images, depending on |
|
|
568 | whether the window has focus. |
|
|
569 | |
|
|
570 | FOCUS ? keep { load "has_focus.jpg" } : keep { load "no_focus.jpg" } |
|
|
571 | |
|
|
572 | =cut |
|
|
573 | |
530 | sub TX() { $frame->[FR_AGAIN]{position} = 1; $x } |
574 | sub TX () { $frame->[FR_AGAIN]{position} = 1; $x } |
531 | sub TY() { $frame->[FR_AGAIN]{position} = 1; $y } |
575 | sub TY () { $frame->[FR_AGAIN]{position} = 1; $y } |
532 | sub TW() { $frame->[FR_AGAIN]{size} = 1; $w } |
576 | sub TW () { $frame->[FR_AGAIN]{size} = 1; $w } |
533 | sub TH() { $frame->[FR_AGAIN]{size} = 1; $h } |
577 | sub TH () { $frame->[FR_AGAIN]{size} = 1; $h } |
|
|
578 | sub FOCUS() { $frame->[FR_AGAIN]{focus} = 1; $focus } |
534 | |
579 | |
535 | =item now |
580 | =item now |
536 | |
581 | |
537 | Returns the current time as (fractional) seconds since the epoch. |
582 | Returns the current time as (fractional) seconds since the epoch. |
538 | |
583 | |
… | |
… | |
585 | Clips an image to the given rectangle. If the rectangle is outside the |
630 | Clips an image to the given rectangle. If the rectangle is outside the |
586 | image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is |
631 | image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is |
587 | larger than the image, then the tiling mode defines how the extra pixels |
632 | larger than the image, then the tiling mode defines how the extra pixels |
588 | will be filled. |
633 | will be filled. |
589 | |
634 | |
590 | If C<$x> an C<$y> are missing, then C<0> is assumed for both. |
635 | If C<$x> and C<$y> are missing, then C<0> is assumed for both. |
591 | |
636 | |
592 | If C<$width> and C<$height> are missing, then the window size will be |
637 | If C<$width> and C<$height> are missing, then the window size will be |
593 | assumed. |
638 | assumed. |
594 | |
639 | |
595 | Example: load an image, blur it, and clip it to the window size to save |
640 | Example: load an image, blur it, and clip it to the window size to save |
… | |
… | |
613 | =item scale $width_factor, $height_factor, $img |
658 | =item scale $width_factor, $height_factor, $img |
614 | |
659 | |
615 | Scales the image by the given factors in horizontal |
660 | Scales the image by the given factors in horizontal |
616 | (C<$width>) and vertical (C<$height>) direction. |
661 | (C<$width>) and vertical (C<$height>) direction. |
617 | |
662 | |
618 | If only one factor is give, it is used for both directions. |
663 | If only one factor is given, it is used for both directions. |
619 | |
664 | |
620 | If no factors are given, scales the image to the window size without |
665 | If no factors are given, scales the image to the window size without |
621 | keeping aspect. |
666 | keeping aspect. |
622 | |
667 | |
623 | =item resize $width, $height, $img |
668 | =item resize $width, $height, $img |
… | |
… | |
748 | =item rotate $center_x, $center_y, $degrees, $img |
793 | =item rotate $center_x, $center_y, $degrees, $img |
749 | |
794 | |
750 | Rotates the image clockwise by C<$degrees> degrees, around the point at |
795 | Rotates the image clockwise by C<$degrees> degrees, around the point at |
751 | C<$center_x> and C<$center_y> (specified as factor of image width/height). |
796 | C<$center_x> and C<$center_y> (specified as factor of image width/height). |
752 | |
797 | |
753 | Example: rotate the image by 90 degrees around it's center. |
798 | Example: rotate the image by 90 degrees around its center. |
754 | |
799 | |
755 | rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" } |
800 | rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" } |
756 | |
801 | |
757 | =cut |
802 | =cut |
758 | |
803 | |
… | |
… | |
789 | |
834 | |
790 | sub tint($$) { |
835 | sub tint($$) { |
791 | $_[1]->tint ($_[0]) |
836 | $_[1]->tint ($_[0]) |
792 | } |
837 | } |
793 | |
838 | |
|
|
839 | =item shade $factor, $img |
|
|
840 | |
|
|
841 | Shade the image by the given factor. |
|
|
842 | |
|
|
843 | =cut |
|
|
844 | |
|
|
845 | sub shade($$) { |
|
|
846 | $_[1]->shade ($_[0]) |
|
|
847 | } |
|
|
848 | |
794 | =item contrast $factor, $img |
849 | =item contrast $factor, $img |
795 | |
850 | |
796 | =item contrast $r, $g, $b, $img |
851 | =item contrast $r, $g, $b, $img |
797 | |
852 | |
798 | =item contrast $r, $g, $b, $a, $img |
853 | =item contrast $r, $g, $b, $a, $img |
… | |
… | |
827 | latter in a white picture. |
882 | latter in a white picture. |
828 | |
883 | |
829 | Due to idiosyncrasies in the underlying XRender extension, biases less |
884 | Due to idiosyncrasies in the underlying XRender extension, biases less |
830 | than zero can be I<very> slow. |
885 | than zero can be I<very> slow. |
831 | |
886 | |
|
|
887 | You can also try the experimental(!) C<muladd> operator. |
|
|
888 | |
832 | =cut |
889 | =cut |
833 | |
890 | |
834 | sub contrast($$;$$;$) { |
891 | sub contrast($$;$$;$) { |
835 | my $img = pop; |
892 | my $img = pop; |
836 | my ($r, $g, $b, $a) = @_; |
893 | my ($r, $g, $b, $a) = @_; |
… | |
… | |
851 | $a = 1 if @_ < 4; |
908 | $a = 1 if @_ < 4; |
852 | |
909 | |
853 | $img = $img->clone; |
910 | $img = $img->clone; |
854 | $img->brightness ($r, $g, $b, $a); |
911 | $img->brightness ($r, $g, $b, $a); |
855 | $img |
912 | $img |
|
|
913 | } |
|
|
914 | |
|
|
915 | =item muladd $mul, $add, $img # EXPERIMENTAL |
|
|
916 | |
|
|
917 | First multiplies the pixels by C<$mul>, then adds C<$add>. This can be used |
|
|
918 | to implement brightness and contrast at the same time, with a wider value |
|
|
919 | range than contrast and brightness operators. |
|
|
920 | |
|
|
921 | Due to numerous bugs in XRender implementations, it can also introduce a |
|
|
922 | number of visual artifacts. |
|
|
923 | |
|
|
924 | Example: increase contrast by a factor of C<$c> without changing image |
|
|
925 | brightness too much. |
|
|
926 | |
|
|
927 | muladd $c, (1 - $c) * 0.5, $img |
|
|
928 | |
|
|
929 | =cut |
|
|
930 | |
|
|
931 | sub muladd($$$) { |
|
|
932 | $_[2]->muladd ($_[0], $_[1]) |
856 | } |
933 | } |
857 | |
934 | |
858 | =item blur $radius, $img |
935 | =item blur $radius, $img |
859 | |
936 | |
860 | =item blur $radius_horz, $radius_vert, $img |
937 | =item blur $radius_horz, $radius_vert, $img |
… | |
… | |
870 | =cut |
947 | =cut |
871 | |
948 | |
872 | sub blur($$;$) { |
949 | sub blur($$;$) { |
873 | my $img = pop; |
950 | my $img = pop; |
874 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
951 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
|
|
952 | } |
|
|
953 | |
|
|
954 | =item focus_fade $img |
|
|
955 | |
|
|
956 | =item focus_fade $factor, $img |
|
|
957 | |
|
|
958 | =item focus_fade $factor, $color, $img |
|
|
959 | |
|
|
960 | Fades the image by the given factor (and colour) when focus is lost (the |
|
|
961 | same as the C<-fade>/C<-fadecolor> command line options, which also supply |
|
|
962 | the default values for C<factor> and C<$color>. Unlike with C<-fade>, the |
|
|
963 | C<$factor> is a real value, not a percentage value (that is, 0..1, not |
|
|
964 | 0..100). |
|
|
965 | |
|
|
966 | Example: do the right thing when focus fading is requested. |
|
|
967 | |
|
|
968 | focus_fade load "mybg.jpg"; |
|
|
969 | |
|
|
970 | =cut |
|
|
971 | |
|
|
972 | sub focus_fade($;$$) { |
|
|
973 | my $img = pop; |
|
|
974 | |
|
|
975 | return $img |
|
|
976 | if FOCUS; |
|
|
977 | |
|
|
978 | my $fade = @_ >= 1 ? $_[0] : defined $self->resource ("fade") ? $self->resource ("fade") * 0.01 : 0; |
|
|
979 | my $color = @_ >= 2 ? $_[1] : $self->resource ("color+" . urxvt::Color_fade); |
|
|
980 | |
|
|
981 | $img = $img->tint ($color) if $color ne "rgb:00/00/00"; |
|
|
982 | $img = $img->muladd (1 - $fade, 0) if $fade; |
|
|
983 | |
|
|
984 | $img |
875 | } |
985 | } |
876 | |
986 | |
877 | =back |
987 | =back |
878 | |
988 | |
879 | =head2 OTHER STUFF |
989 | =head2 OTHER STUFF |
… | |
… | |
1018 | if ($again->{rootpmap}) { |
1128 | if ($again->{rootpmap}) { |
1019 | $state->{rootpmap} = $self->on (rootpmap_change => $cb); |
1129 | $state->{rootpmap} = $self->on (rootpmap_change => $cb); |
1020 | } else { |
1130 | } else { |
1021 | delete $state->{rootpmap}; |
1131 | delete $state->{rootpmap}; |
1022 | } |
1132 | } |
|
|
1133 | |
|
|
1134 | if ($again->{focus}) { |
|
|
1135 | $state->{focus} = $self->on (focus_in => $cb, focus_out => $cb); |
|
|
1136 | } else { |
|
|
1137 | delete $state->{focus}; |
|
|
1138 | } |
1023 | } |
1139 | } |
1024 | |
1140 | |
1025 | # evaluate the current bg expression |
1141 | # evaluate the current bg expression |
1026 | sub recalculate { |
1142 | sub recalculate { |
1027 | my ($arg_self) = @_; |
1143 | my ($arg_self) = @_; |
… | |
… | |
1042 | local $self = $arg_self; |
1158 | local $self = $arg_self; |
1043 | local $HOME = $ENV{HOME}; |
1159 | local $HOME = $ENV{HOME}; |
1044 | local $frame = $self->{root}; |
1160 | local $frame = $self->{root}; |
1045 | |
1161 | |
1046 | ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); |
1162 | ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); |
|
|
1163 | $focus = $self->focus; |
1047 | |
1164 | |
1048 | # evaluate user expression |
1165 | # evaluate user expression |
1049 | |
1166 | |
1050 | my @img = eval { $self->{expr}->() }; |
1167 | my @img = eval { $self->{expr}->() }; |
1051 | die $@ if $@; |
1168 | die $@ if $@; |