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.72 by root, Mon Jul 2 02:01:41 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 fats, 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
131returns the last value computed by the brace block. 132returns the last value computed by the brace block.
132 133
133This means that the C<load> is only executed once, which makes it much 134This 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 135faster, 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 136image must be kept in memory at all times. In this expression, the
136trade-off is likely worth it. 137trade-off is likely worth it.
137 138
138But back to effects: Other effects than scaling are also readily 139But back to effects: Other effects than scaling are also readily
139available, for example, you can tile the image to fill the whole window, 140available, for example, you can tile the image to fill the whole window,
152Another common background expression is: 153Another common background expression is:
153 154
154 rootalign root 155 rootalign root
155 156
156This one first takes a snapshot of the screen background image, and then 157This 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 158moves 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: 159left corner of the terminal window)- the result is pseudo-transparency:
159the image seems to be static while the window is moved around. 160the image seems to be static while the window is moved around.
161
162=head2 COLOUR SPECIFICATIONS
163
164Whenever an oprator expects a "colour", then this can be specified in one
165of two ways: Either as string with an X11 colour specification, such as:
166
167 "red" # named colour
168 "#f00" # simple rgb
169 "[50]red" # red with 50% alpha
170 "TekHVC:300/50/50" # anything goes
171
172OR as an array reference with one, three or four components:
173
174 [0.5] # 50% gray, 100% alpha
175 [0.5, 0, 0] # dark red, no green or blur, 100% alpha
176 [0.5, 0, 0, 0.7] # same with explicit 70% alpha
160 177
161=head2 CACHING AND SENSITIVITY 178=head2 CACHING AND SENSITIVITY
162 179
163Since some operations (such as C<load> and C<blur>) can take a long time, 180Since 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 181caching results can be very important for a smooth operation. Caching can
171The most important way to cache expensive operations is to use C<keep { 188The most important way to cache expensive operations is to use C<keep {
172... }>. The C<keep> operator takes a block of multiple statements enclosed 189... }>. The C<keep> operator takes a block of multiple statements enclosed
173by C<{}> and keeps the return value in memory. 190by C<{}> and keeps the return value in memory.
174 191
175An expression can be "sensitive" to various external events, such as 192An expression can be "sensitive" to various external events, such as
176scaling or moving the window, root backgorund changes and timers. Simply 193scaling or moving the window, root background changes and timers. Simply
177using an expression (such as C<scale> without parameters) that depend on 194using an expression (such as C<scale> without parameters) that depends on
178certain changing values (called "variables"), or using those variables 195certain changing values (called "variables"), or using those variables
179directly, will make an expression sensitive to these events - for example, 196directly, will make an expression sensitive to these events - for example,
180using C<scale> or C<TW> will make the expression sensitive to the terminal 197using C<scale> or C<TW> will make the expression sensitive to the terminal
181size, and thus to resizing events. 198size, and thus to resizing events.
182 199
183When such an event happens, C<keep> will automatically trigger a 200When such an event happens, C<keep> will automatically trigger a
184reevaluation of the whole expression with the new value of the expression. 201reevaluation of the whole expression with the new value of the expression.
185 202
186C<keep> is most useful for expensive operations, such as C<blur>: 203C<keep> is most useful for expensive operations, such as C<blur>:
187 204
188 rootalign once { blur 20, root } 205 rootalign keep { blur 20, root }
189 206
190This makes a blurred copy of the root background once, and on subsequent 207This 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 208calls, just root-aligns it. Since C<blur> is usually quite slow and
192C<rootalign> is quite fast, this trades extra memory (for the cached 209C<rootalign> is quite fast, this trades extra memory (for the cached
193blurred pixmap) with speed (blur only needs to be redone when root 210blurred pixmap) with speed (blur only needs to be redone when root
266=item load $path 283=item load $path
267 284
268Loads the image at the given C<$path>. The image is set to plane tiling 285Loads the image at the given C<$path>. The image is set to plane tiling
269mode. 286mode.
270 287
271If the image is already in memory (e.g. because another temrinal instance 288If the image is already in memory (e.g. because another terminal instance
272uses it), then the in-memory copy us returned instead. 289uses it), then the in-memory copy us returned instead.
273 290
274=item load_uc $path 291=item load_uc $path
275 292
276Load uncached - same as load, but does not cache the image, which means it 293Load uncached - same as load, but does not cache the image, which means it
277is I<always> loaded from the filesystem again. 294is I<always> loaded from the filesystem again, even if another copy of it
295is in memory at the time.
278 296
279=cut 297=cut
280 298
281 sub load_uc($) { 299 sub load_uc($) {
282 $self->new_img_from_file ($path) 300 $self->new_img_from_file ($_[0])
283 } 301 }
284 302
285 sub load($) { 303 sub load($) {
286 my ($path) = @_; 304 my ($path) = @_;
287 305
340 358
341=item merge $img ... 359=item merge $img ...
342 360
343Takes any number of images and merges them together, creating a single 361Takes 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 362image containing them all. The tiling mode of the first image is used as
345the tiling mdoe of the resulting image. 363the tiling mode of the resulting image.
346 364
347This function is called automatically when an expression returns multiple 365This function is called automatically when an expression returns multiple
348images. 366images.
349 367
350=cut 368=cut
753 771
754The following operators change the pixels of the image. 772The following operators change the pixels of the image.
755 773
756=over 4 774=over 4
757 775
776=item tint $color, $img
777
778Tints the image in the given colour.
779
780Example: tint the image red.
781
782 tint "red", load "rgb.png"
783
784Example: the same, but specify the colour by component.
785
786 tint [1, 0, 0], load "rgb.png"
787
788=cut
789
790 sub tint($$) {
791 $_[1]->tint ($_[0])
792 }
793
758=item contrast $factor, $img 794=item contrast $factor, $img
759 795
760=item contrast $r, $g, $b, $img 796=item contrast $r, $g, $b, $img
761 797
762=item contrast $r, $g, $b, $a, $img 798=item contrast $r, $g, $b, $a, $img
845Anything that didn't fit any of the other categories, even after applying 881Anything that didn't fit any of the other categories, even after applying
846force and closing our eyes. 882force and closing our eyes.
847 883
848=over 4 884=over 4
849 885
850=item once { ... } 886=item keep { ... }
851 887
852This function takes a code block as argument, that is, one or more 888This operator takes a code block as argument, that is, one or more
853statements enclosed by braces. 889statements enclosed by braces.
854 890
855The trick is that this code block is only evaluated once - future calls 891The 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 892changes - on other calls the C<keep> simply returns the image it computed
857images). 893previously (yes, it should only be used with images). Or in other words,
894C<keep> I<caches> the result of the code block so it doesn't need to be
895computed again.
858 896
859This can be extremely useful to avoid redoing the same slow operations 897This can be extremely useful to avoid redoing slow operations - for
860again and again- for example, if your background expression takes the root 898example, if your background expression takes the root background, blurs it
861background, blurs it and then root-aligns it it would have to blur the 899and then root-aligns it it would have to blur the root background on every
862root background on every window move or resize. 900window move or resize.
901
902Another example is C<load>, which can be quite slow.
863 903
864In fact, urxvt itself encloses the whole expression in some kind of 904In fact, urxvt itself encloses the whole expression in some kind of
865C<once> block so it only is reevaluated as required. 905C<keep> block so it only is reevaluated as required.
866 906
867Putting the blur into a C<once> block will make sure the blur is only done 907Putting the blur into a C<keep> block will make sure the blur is only done
868once: 908once, while the C<rootalign> is still done each time the window moves.
869 909
870 rootlign once { blur 10, root } 910 rootlign keep { blur 10, root }
871 911
872This leaves the question of how to force reevaluation of the block, 912This leaves the question of how to force reevaluation of the block,
873in case the root background changes: If expression inside the block 913in case the root background changes: If expression inside the block
874is sensitive to some event (root background changes, window geometry 914is sensitive to some event (root background changes, window geometry
875changes), then it will be reevaluated automatically as needed. 915changes), then it will be reevaluated automatically as needed.
876 916
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 917=cut
883 918
884 sub once(&) { 919 sub keep(&) {
885 my $id = $_[0]+0; 920 my $id = $_[0]+0;
886 921
887 local $frame = $self->{frame_cache}{$id} ||= [$frame]; 922 local $frame = $self->{frame_cache}{$id} ||= [$frame];
888 923
889 unless ($frame->[FR_CACHE]) { 924 unless ($frame->[FR_CACHE]) {
907 wantarray 942 wantarray
908 ? @{ $frame->[FR_CACHE] } 943 ? @{ $frame->[FR_CACHE] }
909 : $frame->[FR_CACHE][0] 944 : $frame->[FR_CACHE][0]
910 } 945 }
911 946
912 sub once_again() { 947# sub keep_clear() {
913 delete $self->{frame_cache}; 948# delete $self->{frame_cache};
914 } 949# }
915 950
916=back 951=back
917 952
918=cut 953=cut
919 954

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines