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.64 by root, Tue Jun 19 20:49:53 2012 UTC vs.
Revision 1.82 by sf-exg, Sat Jan 19 10:04:34 2013 UTC

4#:META:X_RESOURCE:%.border:boolean:respect the terminal border 4#:META:X_RESOURCE:%.border:boolean:respect the terminal border
5#:META:X_RESOURCE:%.interval:seconds:minimum time between updates 5#:META:X_RESOURCE:%.interval:seconds:minimum time between updates
6 6
7=head1 NAME 7=head1 NAME
8 8
9 background - manage terminal background 9background - 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
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 fats, 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
131returns the last value computed by the brace block. 156returns the last value computed by the brace block.
132 157
133This means that the C<load> is only executed once, which makes it much 158This means that the C<load> is only executed once, which makes it much
134faster, but alos means that more memory is being used, because the loaded 159faster, but also means that more memory is being used, because the loaded
135image must be kept in memory at all times. In this expression, the 160image must be kept in memory at all times. In this expression, the
136trade-off is likely worth it. 161trade-off is likely worth it.
137 162
138But back to effects: Other effects than scaling are also readily 163But back to effects: Other effects than scaling are also readily
139available, for example, you can tile the image to fill the whole window, 164available, for example, you can tile the image to fill the whole window,
152Another common background expression is: 177Another common background expression is:
153 178
154 rootalign root 179 rootalign root
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 upepr 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
171The most important way to cache expensive operations is to use C<keep { 212The most important way to cache expensive operations is to use C<keep {
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 backgorund 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
266=item load $path 307=item load $path
267 308
268Loads the image at the given C<$path>. The image is set to plane tiling 309Loads the image at the given C<$path>. The image is set to plane tiling
269mode. 310mode.
270 311
271If the image is already in memory (e.g. because another temrinal instance 312If the image is already in memory (e.g. because another terminal instance
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
340 382
341=item merge $img ... 383=item merge $img ...
342 384
343Takes any number of images and merges them together, creating a single 385Takes any number of images and merges them together, creating a single
344image containing them all. The tiling mode of the first image is used as 386image containing them all. The tiling mode of the first image is used as
345the tiling mdoe of the resulting image. 387the tiling mode of the resulting image.
346 388
347This function is called automatically when an expression returns multiple 389This function is called automatically when an expression returns multiple
348images. 390images.
349 391
350=cut 392=cut
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.
491 535
492 move -TX, -TY, keep { load "mybg.png" } 536 move -TX, -TY, keep { load "mybg.png" }
493 537
494=item TW 538=item TW
495 539
540=item TH
541
496Return the width (C<TW>) and height (C<TH>) of the terminal window (the 542Return the width (C<TW>) and height (C<TH>) of the terminal window (the
497terminal window is the full window by default, and the character area only 543terminal window is the full window by default, and the character area only
498when in border-respect mode). 544when in border-respect mode).
499 545
500Using these functions make your expression sensitive to window resizes. 546Using these functions make your expression sensitive to window resizes.
567Clips an image to the given rectangle. If the rectangle is outside the 613Clips an image to the given rectangle. If the rectangle is outside the
568image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is 614image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is
569larger than the image, then the tiling mode defines how the extra pixels 615larger than the image, then the tiling mode defines how the extra pixels
570will be filled. 616will be filled.
571 617
572If C<$x> an C<$y> are missing, then C<0> is assumed for both. 618If C<$x> and C<$y> are missing, then C<0> is assumed for both.
573 619
574If C<$width> and C<$height> are missing, then the window size will be 620If C<$width> and C<$height> are missing, then the window size will be
575assumed. 621assumed.
576 622
577Example: load an image, blur it, and clip it to the window size to save 623Example: load an image, blur it, and clip it to the window size to save
595=item scale $width_factor, $height_factor, $img 641=item scale $width_factor, $height_factor, $img
596 642
597Scales the image by the given factors in horizontal 643Scales the image by the given factors in horizontal
598(C<$width>) and vertical (C<$height>) direction. 644(C<$width>) and vertical (C<$height>) direction.
599 645
600If only one factor is give, it is used for both directions. 646If only one factor is given, it is used for both directions.
601 647
602If no factors are given, scales the image to the window size without 648If no factors are given, scales the image to the window size without
603keeping aspect. 649keeping aspect.
604 650
605=item resize $width, $height, $img 651=item resize $width, $height, $img
730=item rotate $center_x, $center_y, $degrees, $img 776=item rotate $center_x, $center_y, $degrees, $img
731 777
732Rotates the image clockwise by C<$degrees> degrees, around the point at 778Rotates the image clockwise by C<$degrees> degrees, around the point at
733C<$center_x> and C<$center_y> (specified as factor of image width/height). 779C<$center_x> and C<$center_y> (specified as factor of image width/height).
734 780
735Example: rotate the image by 90 degrees around it's center. 781Example: rotate the image by 90 degrees around its center.
736 782
737 rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" } 783 rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" }
738 784
739=cut 785=cut
740 786
753 799
754The following operators change the pixels of the image. 800The following operators change the pixels of the image.
755 801
756=over 4 802=over 4
757 803
804=item tint $color, $img
805
806Tints the image in the given colour.
807
808Example: tint the image red.
809
810 tint "red", load "rgb.png"
811
812Example: the same, but specify the colour by component.
813
814 tint [1, 0, 0], load "rgb.png"
815
816=cut
817
818 sub tint($$) {
819 $_[1]->tint ($_[0])
820 }
821
822=item shade $factor, $img
823
824Shade the image by the given factor.
825
826=cut
827
828 sub shade($$) {
829 $_[1]->shade ($_[0])
830 }
831
758=item contrast $factor, $img 832=item contrast $factor, $img
759 833
760=item contrast $r, $g, $b, $img 834=item contrast $r, $g, $b, $img
761 835
762=item contrast $r, $g, $b, $a, $img 836=item contrast $r, $g, $b, $a, $img
791latter in a white picture. 865latter in a white picture.
792 866
793Due to idiosyncrasies in the underlying XRender extension, biases less 867Due to idiosyncrasies in the underlying XRender extension, biases less
794than zero can be I<very> slow. 868than zero can be I<very> slow.
795 869
870You can also try the experimental(!) C<muladd> operator.
871
796=cut 872=cut
797 873
798 sub contrast($$;$$;$) { 874 sub contrast($$;$$;$) {
799 my $img = pop; 875 my $img = pop;
800 my ($r, $g, $b, $a) = @_; 876 my ($r, $g, $b, $a) = @_;
815 $a = 1 if @_ < 4; 891 $a = 1 if @_ < 4;
816 892
817 $img = $img->clone; 893 $img = $img->clone;
818 $img->brightness ($r, $g, $b, $a); 894 $img->brightness ($r, $g, $b, $a);
819 $img 895 $img
896 }
897
898=item muladd $mul, $add, $img # EXPERIMENTAL
899
900First multiplies the pixels by C<$mul>, then adds C<$add>. This can be used
901to implement brightness and contrast at the same time, with a wider value
902range than contrast and brightness operators.
903
904Due to numerous bugs in XRender implementations, it can also introduce a
905number of visual artifacts.
906
907Example: increase contrast by a factor of C<$c> without changing image
908brightness too much.
909
910 muladd $c, (1 - $c) * 0.5, $img
911
912=cut
913
914 sub muladd($$$) {
915 $_[2]->muladd ($_[0], $_[1])
820 } 916 }
821 917
822=item blur $radius, $img 918=item blur $radius, $img
823 919
824=item blur $radius_horz, $radius_vert, $img 920=item blur $radius_horz, $radius_vert, $img
845Anything that didn't fit any of the other categories, even after applying 941Anything that didn't fit any of the other categories, even after applying
846force and closing our eyes. 942force and closing our eyes.
847 943
848=over 4 944=over 4
849 945
850=item once { ... } 946=item keep { ... }
851 947
852This function takes a code block as argument, that is, one or more 948This operator takes a code block as argument, that is, one or more
853statements enclosed by braces. 949statements enclosed by braces.
854 950
855The trick is that this code block is only evaluated once - future calls 951The trick is that this code block is only evaluated when the outcome
856will simply return the original image (yes, it should only be used with 952changes - on other calls the C<keep> simply returns the image it computed
857images). 953previously (yes, it should only be used with images). Or in other words,
954C<keep> I<caches> the result of the code block so it doesn't need to be
955computed again.
858 956
859This can be extremely useful to avoid redoing the same slow operations 957This can be extremely useful to avoid redoing slow operations - for
860again and again- for example, if your background expression takes the root 958example, if your background expression takes the root background, blurs it
861background, blurs it and then root-aligns it it would have to blur the 959and then root-aligns it it would have to blur the root background on every
862root background on every window move or resize. 960window move or resize.
961
962Another example is C<load>, which can be quite slow.
863 963
864In fact, urxvt itself encloses the whole expression in some kind of 964In fact, urxvt itself encloses the whole expression in some kind of
865C<once> block so it only is reevaluated as required. 965C<keep> block so it only is reevaluated as required.
866 966
867Putting the blur into a C<once> block will make sure the blur is only done 967Putting the blur into a C<keep> block will make sure the blur is only done
868once: 968once, while the C<rootalign> is still done each time the window moves.
869 969
870 rootlign once { blur 10, root } 970 rootalign keep { blur 10, root }
871 971
872This leaves the question of how to force reevaluation of the block, 972This leaves the question of how to force reevaluation of the block,
873in case the root background changes: If expression inside the block 973in case the root background changes: If expression inside the block
874is sensitive to some event (root background changes, window geometry 974is sensitive to some event (root background changes, window geometry
875changes), then it will be reevaluated automatically as needed. 975changes), then it will be reevaluated automatically as needed.
876 976
877=item once_again
878
879Resets all C<once> block as if they had never been called, i.e. on the
880next call they will be reevaluated again.
881
882=cut 977=cut
883 978
884 sub once(&) { 979 sub keep(&) {
885 my $id = $_[0]+0; 980 my $id = $_[0]+0;
886 981
887 local $frame = $self->{frame_cache}{$id} ||= [$frame]; 982 local $frame = $self->{frame_cache}{$id} ||= [$frame];
888 983
889 unless ($frame->[FR_CACHE]) { 984 unless ($frame->[FR_CACHE]) {
907 wantarray 1002 wantarray
908 ? @{ $frame->[FR_CACHE] } 1003 ? @{ $frame->[FR_CACHE] }
909 : $frame->[FR_CACHE][0] 1004 : $frame->[FR_CACHE][0]
910 } 1005 }
911 1006
912 sub once_again() { 1007# sub keep_clear() {
913 delete $self->{frame_cache}; 1008# delete $self->{frame_cache};
914 } 1009# }
915 1010
916=back 1011=back
917 1012
918=cut 1013=cut
919 1014
932 1027
933# compiles a parsed expression 1028# compiles a parsed expression
934sub set_expr { 1029sub set_expr {
935 my ($self, $expr) = @_; 1030 my ($self, $expr) = @_;
936 1031
937 $self->{root} = []; 1032 $self->{root} = []; # the outermost frame
938 $self->{expr} = $expr; 1033 $self->{expr} = $expr;
939 $self->recalculate; 1034 $self->recalculate;
940} 1035}
941 1036
942# takes a hash of sensitivity indicators and installs watchers 1037# takes a hash of sensitivity indicators and installs watchers
1004 1099
1005 # set environment to evaluate user expression 1100 # set environment to evaluate user expression
1006 1101
1007 local $self = $arg_self; 1102 local $self = $arg_self;
1008 local $HOME = $ENV{HOME}; 1103 local $HOME = $ENV{HOME};
1009 local $frame = []; 1104 local $frame = $self->{root};
1010 1105
1011 ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); 1106 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
1012 1107
1013 # evaluate user expression 1108 # evaluate user expression
1014 1109

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines