ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/background
(Generate patch)

Comparing rxvt-unicode/src/perl/background (file contents):
Revision 1.66 by root, Thu Jun 28 15:19:15 2012 UTC vs.
Revision 1.76 by root, Tue Aug 14 23:57:07 2012 UTC

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
19Just load a random jpeg image and tile the background with it without
20scaling or anything else:
21
22 load "/path/to/img.jpg"
23
24The same, but use mirroring/reflection instead of tiling:
25
26 mirror load "/path/to/img.jpg"
27
28Load an image and scale it to exactly fill the terminal window:
29
30 scale keep { load "/path/to/img.jpg" }
31
32Implement pseudo-transparency by using a suitably-aligned root pixmap
33as window background:
34
35 rootalign root
36
37Likewise, but keep a blurred copy:
38
39 rootalign keep { blur 10, root }
16 40
17=head1 DESCRIPTION 41=head1 DESCRIPTION
18 42
19This extension manages the terminal background by creating a picture that 43This extension manages the terminal background by creating a picture that
20is behind the text, replacing the normal background colour. 44is behind the text, replacing the normal background colour.
73 } else { 97 } else {
74 return load "$HOME/sunday.png"; 98 return load "$HOME/sunday.png";
75 } 99 }
76 } 100 }
77 101
78This expression is evaluated once per hour. It will set F<sunday.png> as 102This inner expression is evaluated once per hour (and whenever the
103terminal window is resized). It sets F<sunday.png> as background on
79background on Sundays, and F<weekday.png> on all other days. 104Sundays, and F<weekday.png> on all other days.
80 105
81Fortunately, we expect that most expressions will be much simpler, with 106Fortunately, we expect that most expressions will be much simpler, with
82little Perl knowledge needed. 107little Perl knowledge needed.
83 108
84Basically, you always start with a function that "generates" an image 109Basically, you always start with a function that "generates" an image
117horizontal and vertical dimensions. For example, this halves the image 142horizontal and vertical dimensions. For example, this halves the image
118width and doubles the image height: 143width and doubles the image height:
119 144
120 scale 0.5, 2, load "$HOME/mypic.png" 145 scale 0.5, 2, load "$HOME/mypic.png"
121 146
122IF you try out these expressions, you might suffer from sluggishness, 147IF you try out these expressions, you might suffer from some sluggishness,
123because each time the terminal is resized, it again loads the PNG image 148because each time the terminal is resized, it loads the PNG image again
124and scales it. Scaling is usually fast, but loading the image can be quite 149and scales it. Scaling is usually fast (and unavoidable), but loading the
125time consuming. This is where C<keep> comes in handy: 150image can be quite time consuming. This is where C<keep> comes in handy:
126 151
127 scale 0.5, 2, keep { load "$HOME/mypic.png" } 152 scale 0.5, 2, keep { load "$HOME/mypic.png" }
128 153
129The C<keep> operator executes all the statements inside the braces only 154The C<keep> operator executes all the statements inside the braces only
130once, or when it thinks the outcome might change. In other cases it 155once, or when it thinks the outcome might change. In other cases it
155 180
156This one first takes a snapshot of the screen background image, and then 181This one first takes a snapshot of the screen background image, and then
157moves it to the upper left corner of the screen (as opposed to the upper 182moves it to the upper left corner of the screen (as opposed to the upper
158left corner of the terminal window)- the result is pseudo-transparency: 183left corner of the terminal window)- the result is pseudo-transparency:
159the image seems to be static while the window is moved around. 184the image seems to be static while the window is moved around.
185
186=head2 COLOUR SPECIFICATIONS
187
188Whenever an operator expects a "colour", then this can be specified in one
189of two ways: Either as string with an X11 colour specification, such as:
190
191 "red" # named colour
192 "#f00" # simple rgb
193 "[50]red" # red with 50% alpha
194 "TekHVC:300/50/50" # anything goes
195
196OR as an array reference with one, three or four components:
197
198 [0.5] # 50% gray, 100% alpha
199 [0.5, 0, 0] # dark red, no green or blur, 100% alpha
200 [0.5, 0, 0, 0.7] # same with explicit 70% alpha
160 201
161=head2 CACHING AND SENSITIVITY 202=head2 CACHING AND SENSITIVITY
162 203
163Since some operations (such as C<load> and C<blur>) can take a long time, 204Since some operations (such as C<load> and C<blur>) can take a long time,
164caching results can be very important for a smooth operation. Caching can 205caching results can be very important for a smooth operation. Caching can
172... }>. The C<keep> operator takes a block of multiple statements enclosed 213... }>. The C<keep> operator takes a block of multiple statements enclosed
173by C<{}> and keeps the return value in memory. 214by C<{}> and keeps the return value in memory.
174 215
175An expression can be "sensitive" to various external events, such as 216An expression can be "sensitive" to various external events, such as
176scaling or moving the window, root background changes and timers. Simply 217scaling or moving the window, root background changes and timers. Simply
177using an expression (such as C<scale> without parameters) that depend on 218using an expression (such as C<scale> without parameters) that depends on
178certain changing values (called "variables"), or using those variables 219certain changing values (called "variables"), or using those variables
179directly, will make an expression sensitive to these events - for example, 220directly, will make an expression sensitive to these events - for example,
180using C<scale> or C<TW> will make the expression sensitive to the terminal 221using C<scale> or C<TW> will make the expression sensitive to the terminal
181size, and thus to resizing events. 222size, and thus to resizing events.
182 223
183When such an event happens, C<keep> will automatically trigger a 224When such an event happens, C<keep> will automatically trigger a
184reevaluation of the whole expression with the new value of the expression. 225reevaluation of the whole expression with the new value of the expression.
185 226
186C<keep> is most useful for expensive operations, such as C<blur>: 227C<keep> is most useful for expensive operations, such as C<blur>:
187 228
188 rootalign once { blur 20, root } 229 rootalign keep { blur 20, root }
189 230
190This makes a blurred copy of the root background once, and on subsequent 231This makes a blurred copy of the root background once, and on subsequent
191calls, just root-aligns it. Since C<blur> is usually quite slow and 232calls, just root-aligns it. Since C<blur> is usually quite slow and
192C<rootalign> is quite fast, this trades extra memory (for the cached 233C<rootalign> is quite fast, this trades extra memory (for the cached
193blurred pixmap) with speed (blur only needs to be redone when root 234blurred pixmap) with speed (blur only needs to be redone when root
272uses it), then the in-memory copy us returned instead. 313uses it), then the in-memory copy us returned instead.
273 314
274=item load_uc $path 315=item load_uc $path
275 316
276Load uncached - same as load, but does not cache the image, which means it 317Load uncached - same as load, but does not cache the image, which means it
277is I<always> loaded from the filesystem again. 318is I<always> loaded from the filesystem again, even if another copy of it
319is in memory at the time.
278 320
279=cut 321=cut
280 322
281 sub load_uc($) { 323 sub load_uc($) {
282 $self->new_img_from_file ($path) 324 $self->new_img_from_file ($_[0])
283 } 325 }
284 326
285 sub load($) { 327 sub load($) {
286 my ($path) = @_; 328 my ($path) = @_;
287 329
379 $base->draw ($_) 421 $base->draw ($_)
380 for @_; 422 for @_;
381 423
382 $base 424 $base
383 } 425 }
426
427=back
384 428
385=head2 TILING MODES 429=head2 TILING MODES
386 430
387The following operators modify the tiling mode of an image, that is, the 431The following operators modify the tiling mode of an image, that is, the
388way that pixels outside the image area are painted when the image is used. 432way that pixels outside the image area are painted when the image is used.
753 797
754The following operators change the pixels of the image. 798The following operators change the pixels of the image.
755 799
756=over 4 800=over 4
757 801
802=item tint $color, $img
803
804Tints the image in the given colour.
805
806Example: tint the image red.
807
808 tint "red", load "rgb.png"
809
810Example: the same, but specify the colour by component.
811
812 tint [1, 0, 0], load "rgb.png"
813
814=cut
815
816 sub tint($$) {
817 $_[1]->tint ($_[0])
818 }
819
758=item contrast $factor, $img 820=item contrast $factor, $img
759 821
760=item contrast $r, $g, $b, $img 822=item contrast $r, $g, $b, $img
761 823
762=item contrast $r, $g, $b, $a, $img 824=item contrast $r, $g, $b, $a, $img
791latter in a white picture. 853latter in a white picture.
792 854
793Due to idiosyncrasies in the underlying XRender extension, biases less 855Due to idiosyncrasies in the underlying XRender extension, biases less
794than zero can be I<very> slow. 856than zero can be I<very> slow.
795 857
858You can also try the experimental(!) C<muladd> operator.
859
796=cut 860=cut
797 861
798 sub contrast($$;$$;$) { 862 sub contrast($$;$$;$) {
799 my $img = pop; 863 my $img = pop;
800 my ($r, $g, $b, $a) = @_; 864 my ($r, $g, $b, $a) = @_;
815 $a = 1 if @_ < 4; 879 $a = 1 if @_ < 4;
816 880
817 $img = $img->clone; 881 $img = $img->clone;
818 $img->brightness ($r, $g, $b, $a); 882 $img->brightness ($r, $g, $b, $a);
819 $img 883 $img
884 }
885
886=item muladd $mul, $add, $img # EXPERIMENTAL
887
888First multipliesthe pixels by C<$mul>, then adds C<$add>. This cna be used
889to implement brightness and contrast at the same time, with a wider value
890range than contrast and brightness operators.
891
892Due to numerous bugs in XRender implementations, it can also introduce a
893number of visual artifacts.
894
895Example: increase contrast by a factor of C<$c> without changing image
896brightness too much.
897
898 muladd $c, (1 - $c) * 0.5, $img
899
900=cut
901
902 sub muladd($$$) {
903 $_[2]->muladd ($_[0], $_[1])
820 } 904 }
821 905
822=item blur $radius, $img 906=item blur $radius, $img
823 907
824=item blur $radius_horz, $radius_vert, $img 908=item blur $radius_horz, $radius_vert, $img
847 931
848=over 4 932=over 4
849 933
850=item keep { ... } 934=item keep { ... }
851 935
852 #TODO#
853
854This operator takes a code block as argument, that is, one or more 936This operator takes a code block as argument, that is, one or more
855statements enclosed by braces. 937statements enclosed by braces.
856 938
857The trick is that this code block is only evaluated once - future calls 939The trick is that this code block is only evaluated when the outcome
858will simply return the original image (yes, it should only be used with 940changes - on other calls the C<keep> simply returns the image it computed
859images). 941previously (yes, it should only be used with images). Or in other words,
942C<keep> I<caches> the result of the code block so it doesn't need to be
943computed again.
860 944
861This can be extremely useful to avoid redoing the same slow operations 945This can be extremely useful to avoid redoing slow operations - for
862again and again- for example, if your background expression takes the root 946example, if your background expression takes the root background, blurs it
863background, blurs it and then root-aligns it it would have to blur the 947and then root-aligns it it would have to blur the root background on every
864root background on every window move or resize. 948window move or resize.
949
950Another example is C<load>, which can be quite slow.
865 951
866In fact, urxvt itself encloses the whole expression in some kind of 952In fact, urxvt itself encloses the whole expression in some kind of
867C<once> block so it only is reevaluated as required. 953C<keep> block so it only is reevaluated as required.
868 954
869Putting the blur into a C<once> block will make sure the blur is only done 955Putting the blur into a C<keep> block will make sure the blur is only done
870once: 956once, while the C<rootalign> is still done each time the window moves.
871 957
872 rootlign keep { blur 10, root } 958 rootalign keep { blur 10, root }
873 959
874This leaves the question of how to force reevaluation of the block, 960This leaves the question of how to force reevaluation of the block,
875in case the root background changes: If expression inside the block 961in case the root background changes: If expression inside the block
876is sensitive to some event (root background changes, window geometry 962is sensitive to some event (root background changes, window geometry
877changes), then it will be reevaluated automatically as needed. 963changes), then it will be reevaluated automatically as needed.
878 964
879=item once_again
880
881Resets all C<once> block as if they had never been called, i.e. on the
882next call they will be reevaluated again.
883
884=cut 965=cut
885 966
886 sub once(&) { 967 sub keep(&) {
887 my $id = $_[0]+0; 968 my $id = $_[0]+0;
888 969
889 local $frame = $self->{frame_cache}{$id} ||= [$frame]; 970 local $frame = $self->{frame_cache}{$id} ||= [$frame];
890 971
891 unless ($frame->[FR_CACHE]) { 972 unless ($frame->[FR_CACHE]) {
909 wantarray 990 wantarray
910 ? @{ $frame->[FR_CACHE] } 991 ? @{ $frame->[FR_CACHE] }
911 : $frame->[FR_CACHE][0] 992 : $frame->[FR_CACHE][0]
912 } 993 }
913 994
914 sub once_again() { 995# sub keep_clear() {
915 delete $self->{frame_cache}; 996# delete $self->{frame_cache};
916 } 997# }
917 998
918=back 999=back
919 1000
920=cut 1001=cut
921 1002
934 1015
935# compiles a parsed expression 1016# compiles a parsed expression
936sub set_expr { 1017sub set_expr {
937 my ($self, $expr) = @_; 1018 my ($self, $expr) = @_;
938 1019
939 $self->{root} = []; 1020 $self->{root} = []; # the outermost frame
940 $self->{expr} = $expr; 1021 $self->{expr} = $expr;
941 $self->recalculate; 1022 $self->recalculate;
942} 1023}
943 1024
944# takes a hash of sensitivity indicators and installs watchers 1025# takes a hash of sensitivity indicators and installs watchers
1006 1087
1007 # set environment to evaluate user expression 1088 # set environment to evaluate user expression
1008 1089
1009 local $self = $arg_self; 1090 local $self = $arg_self;
1010 local $HOME = $ENV{HOME}; 1091 local $HOME = $ENV{HOME};
1011 local $frame = []; 1092 local $frame = $self->{root};
1012 1093
1013 ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); 1094 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
1014 1095
1015 # evaluate user expression 1096 # evaluate user expression
1016 1097

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines