1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | #:META:X_RESOURCE:%.expr:string:background expression |
3 | #:META:X_RESOURCE:%.expr:string:background expression |
4 | #:META:X_RESOURCE:%.border.:boolean:respect the terminal border |
4 | #:META:X_RESOURCE:%.border:boolean:respect the terminal border |
5 | |
5 | #:META:X_RESOURCE:%.interval:seconds:minimum time between updates |
6 | #TODO: once, rootalign |
|
|
7 | |
6 | |
8 | =head1 NAME |
7 | =head1 NAME |
9 | |
8 | |
10 | background - manage terminal background |
9 | background - manage terminal background |
11 | |
10 | |
12 | =head1 SYNOPSIS |
11 | =head1 SYNOPSIS |
13 | |
12 | |
14 | urxvt --background-expr 'background expression' |
13 | urxvt --background-expr 'background expression' |
15 | --background-border |
14 | --background-border |
|
|
15 | --background-interval seconds |
16 | |
16 | |
17 | =head1 DESCRIPTION |
17 | =head1 DESCRIPTION |
18 | |
18 | |
19 | This extension manages the terminal background by creating a picture that |
19 | This extension manages the terminal background by creating a picture that |
20 | is behind the text, replacing the normal background colour. |
20 | is behind the text, replacing the normal background colour. |
… | |
… | |
189 | overwriting borders and any other areas, such as the scrollbar. |
189 | overwriting borders and any other areas, such as the scrollbar. |
190 | |
190 | |
191 | Specifying this flag changes the behaviour, so that the image only |
191 | Specifying this flag changes the behaviour, so that the image only |
192 | replaces the background of the character area. |
192 | replaces the background of the character area. |
193 | |
193 | |
|
|
194 | =item --background-interval seconds |
|
|
195 | |
|
|
196 | Since some operations in the underlying XRender extension can effetively |
|
|
197 | freeze your X-server for prolonged time, this extension enforces a minimum |
|
|
198 | time between updates, which is normally about 0.1 seconds. |
|
|
199 | |
|
|
200 | If you want to do updates more often, you can decrease this safety |
|
|
201 | interval with this switch. |
|
|
202 | |
194 | =back |
203 | =back |
195 | |
204 | |
196 | =cut |
205 | =cut |
197 | |
206 | |
|
|
207 | our %_IMGCACHE; |
198 | our $HOME; |
208 | our $HOME; |
199 | our ($self, $old, $new); |
209 | our ($self, $old, $new); |
200 | our ($x, $y, $w, $h); |
210 | our ($x, $y, $w, $h); |
201 | |
211 | |
202 | # enforce at least this interval between updates |
212 | # enforce at least this interval between updates |
203 | our $MIN_INTERVAL = 1/100; |
213 | our $MIN_INTERVAL = 6/59.951; |
204 | |
214 | |
205 | { |
215 | { |
206 | package urxvt::bgdsl; # background language |
216 | package urxvt::bgdsl; # background language |
207 | |
217 | |
208 | use List::Util qw(min max sum shuffle); |
218 | use List::Util qw(min max sum shuffle); |
… | |
… | |
553 | |
563 | |
554 | Example: move the image right by 20 pixels and down by 30. |
564 | Example: move the image right by 20 pixels and down by 30. |
555 | |
565 | |
556 | move 20, 30, ... |
566 | move 20, 30, ... |
557 | |
567 | |
|
|
568 | =item align $xalign, $yalign, $img |
|
|
569 | |
|
|
570 | Aligns the image according to a factor - C<0> means the image is moved to |
|
|
571 | the left or top edge (for C<$xalign> or C<$yalign>), C<0.5> means it is |
|
|
572 | exactly centered and C<1> means it touches the right or bottom edge. |
|
|
573 | |
|
|
574 | Example: remove any visible border around an image, center it vertically but move |
|
|
575 | it to the right hand side. |
|
|
576 | |
|
|
577 | align 1, 0.5, pad $img |
|
|
578 | |
558 | =item center $img |
579 | =item center $img |
559 | |
580 | |
560 | =item center $width, $height, $img |
581 | =item center $width, $height, $img |
561 | |
582 | |
562 | Centers the image, i.e. the center of the image is moved to the center of |
583 | Centers the image, i.e. the center of the image is moved to the center of |
563 | the terminal window (or the box specified by C<$width> and C<$height> if |
584 | the terminal window (or the box specified by C<$width> and C<$height> if |
564 | given). |
585 | given). |
|
|
586 | |
|
|
587 | Example: load an image and center it. |
|
|
588 | |
|
|
589 | center pad load "mybg.png" |
565 | |
590 | |
566 | =item rootalign $img |
591 | =item rootalign $img |
567 | |
592 | |
568 | Moves the image so that it appears glued to the screen as opposed to the |
593 | Moves the image so that it appears glued to the screen as opposed to the |
569 | window. This gives the illusion of a larger area behind the window. It is |
594 | window. This gives the illusion of a larger area behind the window. It is |
… | |
… | |
575 | rootalign mirror load "mybg.png" |
600 | rootalign mirror load "mybg.png" |
576 | |
601 | |
577 | Example: take the screen background and align it, giving the illusion of |
602 | Example: take the screen background and align it, giving the illusion of |
578 | transparency as long as the window isn't in front of other windows. |
603 | transparency as long as the window isn't in front of other windows. |
579 | |
604 | |
580 | rootalign root |
605 | rootalign root |
581 | |
606 | |
582 | =cut |
607 | =cut |
583 | |
608 | |
584 | sub move($$;$) { |
609 | sub move($$;$) { |
585 | my $img = pop->clone; |
610 | my $img = pop->clone; |
586 | $img->move ($_[0], $_[1]); |
611 | $img->move ($_[0], $_[1]); |
587 | $img |
612 | $img |
588 | } |
613 | } |
589 | |
614 | |
|
|
615 | sub align($;$$) { |
|
|
616 | my $img = pop; |
|
|
617 | |
|
|
618 | move $_[0] * (TW - $img->w), |
|
|
619 | $_[1] * (TH - $img->h), |
|
|
620 | $img |
|
|
621 | } |
|
|
622 | |
590 | sub center($;$$) { |
623 | sub center($;$$) { |
591 | my $img = pop; |
624 | my $img = pop; |
592 | my $w = $_[0] || TW; |
625 | my $w = $_[0] || TW; |
593 | my $h = $_[0] || TH; |
626 | my $h = $_[1] || TH; |
594 | |
627 | |
595 | move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img |
628 | move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img |
596 | } |
629 | } |
597 | |
630 | |
598 | sub rootalign($) { |
631 | sub rootalign($) { |
… | |
… | |
649 | |
682 | |
650 | sub contrast($$;$$;$) { |
683 | sub contrast($$;$$;$) { |
651 | my $img = pop; |
684 | my $img = pop; |
652 | my ($r, $g, $b, $a) = @_; |
685 | my ($r, $g, $b, $a) = @_; |
653 | |
686 | |
654 | ($g, $b) = ($r, $r) if @_ < 4; |
687 | ($g, $b) = ($r, $r) if @_ < 3; |
655 | $a = 1 if @_ < 5; |
688 | $a = 1 if @_ < 4; |
656 | |
689 | |
657 | $img = $img->clone; |
690 | $img = $img->clone; |
658 | $img->contrast ($r, $g, $b, $a); |
691 | $img->contrast ($r, $g, $b, $a); |
659 | $img |
692 | $img |
660 | } |
693 | } |
661 | |
694 | |
662 | sub brightness($$;$$;$) { |
695 | sub brightness($$;$$;$) { |
663 | my $img = pop; |
696 | my $img = pop; |
664 | my ($r, $g, $b, $a) = @_; |
697 | my ($r, $g, $b, $a) = @_; |
665 | |
698 | |
666 | ($g, $b) = ($r, $r) if @_ < 4; |
699 | ($g, $b) = ($r, $r) if @_ < 3; |
667 | $a = 1 if @_ < 5; |
700 | $a = 1 if @_ < 4; |
668 | |
701 | |
669 | $img = $img->clone; |
702 | $img = $img->clone; |
670 | $img->brightness ($r, $g, $b, $a); |
703 | $img->brightness ($r, $g, $b, $a); |
671 | $img |
704 | $img |
672 | } |
705 | } |
… | |
… | |
762 | |
795 | |
763 | # evaluate user expression |
796 | # evaluate user expression |
764 | |
797 | |
765 | my $img = eval { $self->{expr}->() }; |
798 | my $img = eval { $self->{expr}->() }; |
766 | warn $@ if $@;#d# |
799 | warn $@ if $@;#d# |
767 | die if !UNIVERSAL::isa $img, "urxvt::img"; |
800 | die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img"; |
768 | |
801 | |
769 | $state->{size_sensitive} = 1 |
802 | $state->{size_sensitive} = 1 |
770 | if $img->repeat_mode != urxvt::RepeatNormal; |
803 | if $img->repeat_mode != urxvt::RepeatNormal; |
771 | |
804 | |
772 | # if the expression is sensitive to external events, prepare reevaluation then |
805 | # if the expression is sensitive to external events, prepare reevaluation then |
… | |
… | |
822 | } |
855 | } |
823 | |
856 | |
824 | sub on_start { |
857 | sub on_start { |
825 | my ($self) = @_; |
858 | my ($self) = @_; |
826 | |
859 | |
827 | my $expr = $self->x_resource ("background.expr") |
860 | my $expr = $self->x_resource ("%.expr") |
828 | or return; |
861 | or return; |
829 | |
862 | |
|
|
863 | $self->has_render |
|
|
864 | or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n"; |
|
|
865 | |
830 | $self->set_expr (parse_expr $expr); |
866 | $self->set_expr (parse_expr $expr); |
831 | $self->{border} = $self->x_resource_boolean ("background.border"); |
867 | $self->{border} = $self->x_resource_boolean ("%.border"); |
|
|
868 | |
|
|
869 | $MIN_INTERVAL = $self->x_resource ("%.interval"); |
832 | |
870 | |
833 | () |
871 | () |
834 | } |
872 | } |
835 | |
873 | |