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.65 by sf-exg, Wed Jun 20 18:18:14 2012 UTC vs.
Revision 1.69 by root, Mon Jul 2 01:32:26 2012 UTC

73 } else { 73 } else {
74 return load "$HOME/sunday.png"; 74 return load "$HOME/sunday.png";
75 } 75 }
76 } 76 }
77 77
78This expression is evaluated once per hour. It will set F<sunday.png> as 78This inner expression is evaluated once per hour (and whenever the
79temrinal window is resized). It sets F<sunday.png> as background on
79background on Sundays, and F<weekday.png> on all other days. 80Sundays, and F<weekday.png> on all other days.
80 81
81Fortunately, we expect that most expressions will be much simpler, with 82Fortunately, we expect that most expressions will be much simpler, with
82little Perl knowledge needed. 83little Perl knowledge needed.
83 84
84Basically, you always start with a function that "generates" an image 85Basically, you always start with a function that "generates" an image
117horizontal and vertical dimensions. For example, this halves the image 118horizontal and vertical dimensions. For example, this halves the image
118width and doubles the image height: 119width and doubles the image height:
119 120
120 scale 0.5, 2, load "$HOME/mypic.png" 121 scale 0.5, 2, load "$HOME/mypic.png"
121 122
122IF you try out these expressions, you might suffer from sluggishness, 123IF you try out these expressions, you might suffer from some sluggishness,
123because each time the terminal is resized, it again loads the PNG image 124because each time the terminal is resized, it loads the PNG image agin
124and scales it. Scaling is usually fast, but loading the image can be quite 125and scales it. Scaling is usually fast (and unavoidable), but loading the
125time consuming. This is where C<keep> comes in handy: 126image can be quite time consuming. This is where C<keep> comes in handy:
126 127
127 scale 0.5, 2, keep { load "$HOME/mypic.png" } 128 scale 0.5, 2, keep { load "$HOME/mypic.png" }
128 129
129The C<keep> operator executes all the statements inside the braces only 130The C<keep> operator executes all the statements inside the braces only
130once, or when it thinks the outcome might change. In other cases it 131once, or when it thinks the outcome might change. In other cases it
172... }>. The C<keep> operator takes a block of multiple statements enclosed 173... }>. The C<keep> operator takes a block of multiple statements enclosed
173by C<{}> and keeps the return value in memory. 174by C<{}> and keeps the return value in memory.
174 175
175An expression can be "sensitive" to various external events, such as 176An expression can be "sensitive" to various external events, such as
176scaling or moving the window, root background changes and timers. Simply 177scaling or moving the window, root background changes and timers. Simply
177using an expression (such as C<scale> without parameters) that depend on 178using an expression (such as C<scale> without parameters) that depends on
178certain changing values (called "variables"), or using those variables 179certain changing values (called "variables"), or using those variables
179directly, will make an expression sensitive to these events - for example, 180directly, will make an expression sensitive to these events - for example,
180using C<scale> or C<TW> will make the expression sensitive to the terminal 181using C<scale> or C<TW> will make the expression sensitive to the terminal
181size, and thus to resizing events. 182size, and thus to resizing events.
182 183
183When such an event happens, C<keep> will automatically trigger a 184When such an event happens, C<keep> will automatically trigger a
184reevaluation of the whole expression with the new value of the expression. 185reevaluation of the whole expression with the new value of the expression.
185 186
186C<keep> is most useful for expensive operations, such as C<blur>: 187C<keep> is most useful for expensive operations, such as C<blur>:
187 188
188 rootalign once { blur 20, root } 189 rootalign keep { blur 20, root }
189 190
190This makes a blurred copy of the root background once, and on subsequent 191This 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 192calls, just root-aligns it. Since C<blur> is usually quite slow and
192C<rootalign> is quite fast, this trades extra memory (for the cached 193C<rootalign> is quite fast, this trades extra memory (for the cached
193blurred pixmap) with speed (blur only needs to be redone when root 194blurred pixmap) with speed (blur only needs to be redone when root
276Load uncached - same as load, but does not cache the image, which means it 277Load uncached - same as load, but does not cache the image, which means it
277is I<always> loaded from the filesystem again. 278is I<always> loaded from the filesystem again.
278 279
279=cut 280=cut
280 281
281 sub load_uc($) {
282 $self->new_img_from_file ($path)
283 }
284
285 sub load($) { 282 sub load($) {
286 my ($path) = @_; 283 my ($path) = @_;
287 284
288 $_IMG_CACHE{$path} || do { 285 $_IMG_CACHE{$path} || do {
289 my $img = load_uc $path; 286 my $img = $self->new_img_from_file ($path);
290 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img); 287 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img);
291 $img 288 $img
292 } 289 }
293 } 290 }
294 291
845Anything that didn't fit any of the other categories, even after applying 842Anything that didn't fit any of the other categories, even after applying
846force and closing our eyes. 843force and closing our eyes.
847 844
848=over 4 845=over 4
849 846
850=item once { ... } 847=item keep { ... }
851 848
852This function takes a code block as argument, that is, one or more 849This operator takes a code block as argument, that is, one or more
853statements enclosed by braces. 850statements enclosed by braces.
854 851
855The trick is that this code block is only evaluated once - future calls 852The 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 853changes - on other calls the C<keep> simply returns the image it computed
857images). 854previously (yes, it should only be used with images). Or in other words,
855C<keep> I<caches> the result of the code block so it doesn't need to be
856computed again.
858 857
859This can be extremely useful to avoid redoing the same slow operations 858This can be extremely useful to avoid redoing slow operations - for
860again and again- for example, if your background expression takes the root 859example, if your background expression takes the root background, blurs it
861background, blurs it and then root-aligns it it would have to blur the 860and then root-aligns it it would have to blur the root background on every
862root background on every window move or resize. 861window move or resize.
862
863Another example is C<load>, which can be quite slow.
863 864
864In fact, urxvt itself encloses the whole expression in some kind of 865In fact, urxvt itself encloses the whole expression in some kind of
865C<once> block so it only is reevaluated as required. 866C<keep> block so it only is reevaluated as required.
866 867
867Putting the blur into a C<once> block will make sure the blur is only done 868Putting the blur into a C<keep> block will make sure the blur is only done
868once: 869once, while the C<rootalign> is still done each time the window moves.
869 870
870 rootlign once { blur 10, root } 871 rootlign keep { blur 10, root }
871 872
872This leaves the question of how to force reevaluation of the block, 873This leaves the question of how to force reevaluation of the block,
873in case the root background changes: If expression inside the block 874in case the root background changes: If expression inside the block
874is sensitive to some event (root background changes, window geometry 875is sensitive to some event (root background changes, window geometry
875changes), then it will be reevaluated automatically as needed. 876changes), then it will be reevaluated automatically as needed.
876 877
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 878=cut
883 879
884 sub once(&) { 880 sub keep(&) {
885 my $id = $_[0]+0; 881 my $id = $_[0]+0;
886 882
887 local $frame = $self->{frame_cache}{$id} ||= [$frame]; 883 local $frame = $self->{frame_cache}{$id} ||= [$frame];
888 884
889 unless ($frame->[FR_CACHE]) { 885 unless ($frame->[FR_CACHE]) {
907 wantarray 903 wantarray
908 ? @{ $frame->[FR_CACHE] } 904 ? @{ $frame->[FR_CACHE] }
909 : $frame->[FR_CACHE][0] 905 : $frame->[FR_CACHE][0]
910 } 906 }
911 907
912 sub once_again() { 908# sub keep_clear() {
913 delete $self->{frame_cache}; 909# delete $self->{frame_cache};
914 } 910# }
915 911
916=back 912=back
917 913
918=cut 914=cut
919 915

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines