1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | #:META:X_RESOURCE:%.expr:string:background expression |
3 | #:META:RESOURCE:%.expr:string:background expression |
4 | #:META:X_RESOURCE:%.border:boolean:respect the terminal border |
4 | #:META:RESOURCE:%.border:boolean:respect the terminal border |
5 | #:META:X_RESOURCE:%.interval:seconds:minimum time between updates |
5 | #:META:RESOURCE:%.interval:seconds:minimum time between updates |
6 | |
6 | |
7 | =head1 NAME |
7 | =head1 NAME |
8 | |
8 | |
9 | background - manage terminal background |
9 | background - 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 | |
|
|
19 | Just load a random jpeg image and tile the background with it without |
|
|
20 | scaling or anything else: |
|
|
21 | |
|
|
22 | load "/path/to/img.jpg" |
|
|
23 | |
|
|
24 | The same, but use mirroring/reflection instead of tiling: |
|
|
25 | |
|
|
26 | mirror load "/path/to/img.jpg" |
|
|
27 | |
|
|
28 | Load an image and scale it to exactly fill the terminal window: |
|
|
29 | |
|
|
30 | scale keep { load "/path/to/img.jpg" } |
|
|
31 | |
|
|
32 | Implement pseudo-transparency by using a suitably-aligned root pixmap |
|
|
33 | as window background: |
|
|
34 | |
|
|
35 | rootalign root |
|
|
36 | |
|
|
37 | Likewise, but keep a blurred copy: |
|
|
38 | |
|
|
39 | rootalign keep { blur 10, root } |
16 | |
40 | |
17 | =head1 DESCRIPTION |
41 | =head1 DESCRIPTION |
18 | |
42 | |
19 | This extension manages the terminal background by creating a picture that |
43 | This extension manages the terminal background by creating a picture that |
20 | is behind the text, replacing the normal background colour. |
44 | is 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 | |
78 | This expression is evaluated once per hour. It will set F<sunday.png> as |
102 | This inner expression is evaluated once per hour (and whenever the |
|
|
103 | terminal window is resized). It sets F<sunday.png> as background on |
79 | background on Sundays, and F<weekday.png> on all other days. |
104 | Sundays, and F<weekday.png> on all other days. |
80 | |
105 | |
81 | Fortunately, we expect that most expressions will be much simpler, with |
106 | Fortunately, we expect that most expressions will be much simpler, with |
82 | little Perl knowledge needed. |
107 | little Perl knowledge needed. |
83 | |
108 | |
84 | Basically, you always start with a function that "generates" an image |
109 | Basically, you always start with a function that "generates" an image |
… | |
… | |
117 | horizontal and vertical dimensions. For example, this halves the image |
142 | horizontal and vertical dimensions. For example, this halves the image |
118 | width and doubles the image height: |
143 | width 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 | |
122 | IF you try out these expressions, you might suffer from sluggishness, |
147 | IF you try out these expressions, you might suffer from some sluggishness, |
123 | because each time the terminal is resized, it again loads the PNG image |
148 | because each time the terminal is resized, it loads the PNG image again |
124 | and scales it. Scaling is usually fast, but loading the image can be quite |
149 | and scales it. Scaling is usually fast (and unavoidable), but loading the |
125 | time consuming. This is where C<keep> comes in handy: |
150 | image 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 | |
129 | The C<keep> operator executes all the statements inside the braces only |
154 | The C<keep> operator executes all the statements inside the braces only |
130 | once, or when it thinks the outcome might change. In other cases it |
155 | once, or when it thinks the outcome might change. In other cases it |
… | |
… | |
155 | |
180 | |
156 | This one first takes a snapshot of the screen background image, and then |
181 | This one first takes a snapshot of the screen background image, and then |
157 | moves it to the upper left corner of the screen (as opposed to the upper |
182 | moves it to the upper left corner of the screen (as opposed to the upper |
158 | left corner of the terminal window)- the result is pseudo-transparency: |
183 | left corner of the terminal window)- the result is pseudo-transparency: |
159 | the image seems to be static while the window is moved around. |
184 | the image seems to be static while the window is moved around. |
|
|
185 | |
|
|
186 | =head2 COLOUR SPECIFICATIONS |
|
|
187 | |
|
|
188 | Whenever an operator expects a "colour", then this can be specified in one |
|
|
189 | of 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 | |
|
|
196 | OR 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 | |
163 | Since some operations (such as C<load> and C<blur>) can take a long time, |
204 | Since some operations (such as C<load> and C<blur>) can take a long time, |
164 | caching results can be very important for a smooth operation. Caching can |
205 | caching 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 |
173 | by C<{}> and keeps the return value in memory. |
214 | by C<{}> and keeps the return value in memory. |
174 | |
215 | |
175 | An expression can be "sensitive" to various external events, such as |
216 | An expression can be "sensitive" to various external events, such as |
176 | scaling or moving the window, root background changes and timers. Simply |
217 | scaling or moving the window, root background changes and timers. Simply |
177 | using an expression (such as C<scale> without parameters) that depend on |
218 | using an expression (such as C<scale> without parameters) that depends on |
178 | certain changing values (called "variables"), or using those variables |
219 | certain changing values (called "variables"), or using those variables |
179 | directly, will make an expression sensitive to these events - for example, |
220 | directly, will make an expression sensitive to these events - for example, |
180 | using C<scale> or C<TW> will make the expression sensitive to the terminal |
221 | using C<scale> or C<TW> will make the expression sensitive to the terminal |
181 | size, and thus to resizing events. |
222 | size, and thus to resizing events. |
182 | |
223 | |
183 | When such an event happens, C<keep> will automatically trigger a |
224 | When such an event happens, C<keep> will automatically trigger a |
184 | reevaluation of the whole expression with the new value of the expression. |
225 | reevaluation of the whole expression with the new value of the expression. |
185 | |
226 | |
186 | C<keep> is most useful for expensive operations, such as C<blur>: |
227 | C<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 | |
190 | This makes a blurred copy of the root background once, and on subsequent |
231 | This makes a blurred copy of the root background once, and on subsequent |
191 | calls, just root-aligns it. Since C<blur> is usually quite slow and |
232 | calls, just root-aligns it. Since C<blur> is usually quite slow and |
192 | C<rootalign> is quite fast, this trades extra memory (for the cached |
233 | C<rootalign> is quite fast, this trades extra memory (for the cached |
193 | blurred pixmap) with speed (blur only needs to be redone when root |
234 | blurred pixmap) with speed (blur only needs to be redone when root |
… | |
… | |
238 | =cut |
279 | =cut |
239 | |
280 | |
240 | our %_IMG_CACHE; |
281 | our %_IMG_CACHE; |
241 | our $HOME; |
282 | our $HOME; |
242 | our ($self, $frame); |
283 | our ($self, $frame); |
243 | our ($x, $y, $w, $h); |
284 | our ($x, $y, $w, $h, $focus); |
244 | |
285 | |
245 | # enforce at least this interval between updates |
286 | # enforce at least this interval between updates |
246 | our $MIN_INTERVAL = 6/59.951; |
287 | our $MIN_INTERVAL = 6/59.951; |
247 | |
288 | |
248 | { |
289 | { |
… | |
… | |
267 | |
308 | |
268 | Loads the image at the given C<$path>. The image is set to plane tiling |
309 | Loads the image at the given C<$path>. The image is set to plane tiling |
269 | mode. |
310 | mode. |
270 | |
311 | |
271 | If the image is already in memory (e.g. because another terminal instance |
312 | If the image is already in memory (e.g. because another terminal instance |
272 | uses it), then the in-memory copy us returned instead. |
313 | uses it), then the in-memory copy is returned instead. |
273 | |
314 | |
274 | =item load_uc $path |
315 | =item load_uc $path |
275 | |
316 | |
276 | Load uncached - same as load, but does not cache the image, which means it |
317 | Load uncached - same as load, but does not cache the image, which means it |
277 | is I<always> loaded from the filesystem again. |
318 | is I<always> loaded from the filesystem again, even if another copy of it |
|
|
319 | is 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 | |
… | |
… | |
380 | for @_; |
422 | for @_; |
381 | |
423 | |
382 | $base |
424 | $base |
383 | } |
425 | } |
384 | |
426 | |
|
|
427 | =back |
|
|
428 | |
385 | =head2 TILING MODES |
429 | =head2 TILING MODES |
386 | |
430 | |
387 | The following operators modify the tiling mode of an image, that is, the |
431 | The following operators modify the tiling mode of an image, that is, the |
388 | way that pixels outside the image area are painted when the image is used. |
432 | way that pixels outside the image area are painted when the image is used. |
389 | |
433 | |
… | |
… | |
480 | |
524 | |
481 | Return the X and Y coordinates of the terminal window (the terminal |
525 | Return the X and Y coordinates of the terminal window (the terminal |
482 | window is the full window by default, and the character area only when in |
526 | window is the full window by default, and the character area only when in |
483 | border-respect mode). |
527 | border-respect mode). |
484 | |
528 | |
485 | Using these functions make your expression sensitive to window moves. |
529 | Using these functions makes your expression sensitive to window moves. |
486 | |
530 | |
487 | These functions are mainly useful to align images to the root window. |
531 | These functions are mainly useful to align images to the root window. |
488 | |
532 | |
489 | Example: load an image and align it so it looks as if anchored to the |
533 | Example: load an image and align it so it looks as if anchored to the |
490 | background (that's exactly what C<rootalign> does btw.): |
534 | background (that's exactly what C<rootalign> does btw.): |
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 |
|
|
539 | |
|
|
540 | =item TH |
495 | |
541 | |
496 | Return the width (C<TW>) and height (C<TH>) of the terminal window (the |
542 | Return the width (C<TW>) and height (C<TH>) of the terminal window (the |
497 | terminal window is the full window by default, and the character area only |
543 | terminal window is the full window by default, and the character area only |
498 | when in border-respect mode). |
544 | when in border-respect mode). |
499 | |
545 | |
500 | Using these functions make your expression sensitive to window resizes. |
546 | Using these functions makes your expression sensitive to window resizes. |
501 | |
547 | |
502 | These functions are mainly useful to scale images, or to clip images to |
548 | These functions are mainly useful to scale images, or to clip images to |
503 | the window size to conserve memory. |
549 | the window size to conserve memory. |
504 | |
550 | |
505 | Example: take the screen background, clip it to the window size, blur it a |
551 | Example: take the screen background, clip it to the window size, blur it a |
506 | bit, align it to the window position and use it as background. |
552 | bit, align it to the window position and use it as background. |
507 | |
553 | |
508 | clip move -TX, -TY, keep { blur 5, root } |
554 | clip move -TX, -TY, keep { blur 5, root } |
509 | |
555 | |
510 | =cut |
556 | =item FOCUS |
511 | |
557 | |
|
|
558 | Returns a boolean indicating whether the terminal window has keyboard |
|
|
559 | focus, in which case it returns true. |
|
|
560 | |
|
|
561 | Using this function makes your expression sensitive to focus changes. |
|
|
562 | |
|
|
563 | A common use case is to fade the background image when the terminal loses |
|
|
564 | focus, often together with the C<-fade> command line option. In fact, |
|
|
565 | there is a special function for just that use case: C<focus_fade>. |
|
|
566 | |
|
|
567 | Example: use two entirely different background images, depending on |
|
|
568 | whether the window has focus. |
|
|
569 | |
|
|
570 | FOCUS ? keep { load "has_focus.jpg" } : keep { load "no_focus.jpg" } |
|
|
571 | |
|
|
572 | =cut |
|
|
573 | |
512 | sub TX() { $frame->[FR_AGAIN]{position} = 1; $x } |
574 | sub TX () { $frame->[FR_AGAIN]{position} = 1; $x } |
513 | sub TY() { $frame->[FR_AGAIN]{position} = 1; $y } |
575 | sub TY () { $frame->[FR_AGAIN]{position} = 1; $y } |
514 | sub TW() { $frame->[FR_AGAIN]{size} = 1; $w } |
576 | sub TW () { $frame->[FR_AGAIN]{size} = 1; $w } |
515 | sub TH() { $frame->[FR_AGAIN]{size} = 1; $h } |
577 | sub TH () { $frame->[FR_AGAIN]{size} = 1; $h } |
|
|
578 | sub FOCUS() { $frame->[FR_AGAIN]{focus} = 1; $focus } |
516 | |
579 | |
517 | =item now |
580 | =item now |
518 | |
581 | |
519 | Returns the current time as (fractional) seconds since the epoch. |
582 | Returns the current time as (fractional) seconds since the epoch. |
520 | |
583 | |
… | |
… | |
567 | Clips an image to the given rectangle. If the rectangle is outside the |
630 | Clips an image to the given rectangle. If the rectangle is outside the |
568 | image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is |
631 | image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is |
569 | larger than the image, then the tiling mode defines how the extra pixels |
632 | larger than the image, then the tiling mode defines how the extra pixels |
570 | will be filled. |
633 | will be filled. |
571 | |
634 | |
572 | If C<$x> an C<$y> are missing, then C<0> is assumed for both. |
635 | If C<$x> and C<$y> are missing, then C<0> is assumed for both. |
573 | |
636 | |
574 | If C<$width> and C<$height> are missing, then the window size will be |
637 | If C<$width> and C<$height> are missing, then the window size will be |
575 | assumed. |
638 | assumed. |
576 | |
639 | |
577 | Example: load an image, blur it, and clip it to the window size to save |
640 | Example: load an image, blur it, and clip it to the window size to save |
… | |
… | |
595 | =item scale $width_factor, $height_factor, $img |
658 | =item scale $width_factor, $height_factor, $img |
596 | |
659 | |
597 | Scales the image by the given factors in horizontal |
660 | Scales the image by the given factors in horizontal |
598 | (C<$width>) and vertical (C<$height>) direction. |
661 | (C<$width>) and vertical (C<$height>) direction. |
599 | |
662 | |
600 | If only one factor is give, it is used for both directions. |
663 | If only one factor is given, it is used for both directions. |
601 | |
664 | |
602 | If no factors are given, scales the image to the window size without |
665 | If no factors are given, scales the image to the window size without |
603 | keeping aspect. |
666 | keeping aspect. |
604 | |
667 | |
605 | =item resize $width, $height, $img |
668 | =item resize $width, $height, $img |
… | |
… | |
730 | =item rotate $center_x, $center_y, $degrees, $img |
793 | =item rotate $center_x, $center_y, $degrees, $img |
731 | |
794 | |
732 | Rotates the image clockwise by C<$degrees> degrees, around the point at |
795 | Rotates the image clockwise by C<$degrees> degrees, around the point at |
733 | C<$center_x> and C<$center_y> (specified as factor of image width/height). |
796 | C<$center_x> and C<$center_y> (specified as factor of image width/height). |
734 | |
797 | |
735 | Example: rotate the image by 90 degrees around it's center. |
798 | Example: rotate the image by 90 degrees around its center. |
736 | |
799 | |
737 | rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" } |
800 | rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" } |
738 | |
801 | |
739 | =cut |
802 | =cut |
740 | |
803 | |
… | |
… | |
753 | |
816 | |
754 | The following operators change the pixels of the image. |
817 | The following operators change the pixels of the image. |
755 | |
818 | |
756 | =over 4 |
819 | =over 4 |
757 | |
820 | |
|
|
821 | =item tint $color, $img |
|
|
822 | |
|
|
823 | Tints the image in the given colour. |
|
|
824 | |
|
|
825 | Example: tint the image red. |
|
|
826 | |
|
|
827 | tint "red", load "rgb.png" |
|
|
828 | |
|
|
829 | Example: the same, but specify the colour by component. |
|
|
830 | |
|
|
831 | tint [1, 0, 0], load "rgb.png" |
|
|
832 | |
|
|
833 | =cut |
|
|
834 | |
|
|
835 | sub tint($$) { |
|
|
836 | $_[1]->tint ($_[0]) |
|
|
837 | } |
|
|
838 | |
|
|
839 | =item shade $factor, $img |
|
|
840 | |
|
|
841 | Shade the image by the given factor. |
|
|
842 | |
|
|
843 | =cut |
|
|
844 | |
|
|
845 | sub shade($$) { |
|
|
846 | $_[1]->shade ($_[0]) |
|
|
847 | } |
|
|
848 | |
758 | =item contrast $factor, $img |
849 | =item contrast $factor, $img |
759 | |
850 | |
760 | =item contrast $r, $g, $b, $img |
851 | =item contrast $r, $g, $b, $img |
761 | |
852 | |
762 | =item contrast $r, $g, $b, $a, $img |
853 | =item contrast $r, $g, $b, $a, $img |
… | |
… | |
791 | latter in a white picture. |
882 | latter in a white picture. |
792 | |
883 | |
793 | Due to idiosyncrasies in the underlying XRender extension, biases less |
884 | Due to idiosyncrasies in the underlying XRender extension, biases less |
794 | than zero can be I<very> slow. |
885 | than zero can be I<very> slow. |
795 | |
886 | |
|
|
887 | You can also try the experimental(!) C<muladd> operator. |
|
|
888 | |
796 | =cut |
889 | =cut |
797 | |
890 | |
798 | sub contrast($$;$$;$) { |
891 | sub contrast($$;$$;$) { |
799 | my $img = pop; |
892 | my $img = pop; |
800 | my ($r, $g, $b, $a) = @_; |
893 | my ($r, $g, $b, $a) = @_; |
… | |
… | |
815 | $a = 1 if @_ < 4; |
908 | $a = 1 if @_ < 4; |
816 | |
909 | |
817 | $img = $img->clone; |
910 | $img = $img->clone; |
818 | $img->brightness ($r, $g, $b, $a); |
911 | $img->brightness ($r, $g, $b, $a); |
819 | $img |
912 | $img |
|
|
913 | } |
|
|
914 | |
|
|
915 | =item muladd $mul, $add, $img # EXPERIMENTAL |
|
|
916 | |
|
|
917 | First multiplies the pixels by C<$mul>, then adds C<$add>. This can be used |
|
|
918 | to implement brightness and contrast at the same time, with a wider value |
|
|
919 | range than contrast and brightness operators. |
|
|
920 | |
|
|
921 | Due to numerous bugs in XRender implementations, it can also introduce a |
|
|
922 | number of visual artifacts. |
|
|
923 | |
|
|
924 | Example: increase contrast by a factor of C<$c> without changing image |
|
|
925 | brightness too much. |
|
|
926 | |
|
|
927 | muladd $c, (1 - $c) * 0.5, $img |
|
|
928 | |
|
|
929 | =cut |
|
|
930 | |
|
|
931 | sub muladd($$$) { |
|
|
932 | $_[2]->muladd ($_[0], $_[1]) |
820 | } |
933 | } |
821 | |
934 | |
822 | =item blur $radius, $img |
935 | =item blur $radius, $img |
823 | |
936 | |
824 | =item blur $radius_horz, $radius_vert, $img |
937 | =item blur $radius_horz, $radius_vert, $img |
… | |
… | |
836 | sub blur($$;$) { |
949 | sub blur($$;$) { |
837 | my $img = pop; |
950 | my $img = pop; |
838 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
951 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
839 | } |
952 | } |
840 | |
953 | |
|
|
954 | =item focus_fade $img |
|
|
955 | |
|
|
956 | =item focus_fade $factor, $img |
|
|
957 | |
|
|
958 | =item focus_fade $factor, $color, $img |
|
|
959 | |
|
|
960 | Fades the image by the given factor (and colour) when focus is lost (the |
|
|
961 | same as the C<-fade>/C<-fadecolor> command line options, which also supply |
|
|
962 | the default values for C<factor> and C<$color>. Unlike with C<-fade>, the |
|
|
963 | C<$factor> is a real value, not a percentage value (that is, 0..1, not |
|
|
964 | 0..100). |
|
|
965 | |
|
|
966 | Example: do the right thing when focus fading is requested. |
|
|
967 | |
|
|
968 | focus_fade load "mybg.jpg"; |
|
|
969 | |
|
|
970 | =cut |
|
|
971 | |
|
|
972 | sub focus_fade($;$$) { |
|
|
973 | my $img = pop; |
|
|
974 | |
|
|
975 | return $img |
|
|
976 | if FOCUS; |
|
|
977 | |
|
|
978 | my $fade = @_ >= 1 ? $_[0] : defined $self->resource ("fade") ? $self->resource ("fade") * 0.01 : 0; |
|
|
979 | my $color = @_ >= 2 ? $_[1] : $self->resource ("color+" . urxvt::Color_fade); |
|
|
980 | |
|
|
981 | $img = $img->tint ($color) if $color ne "rgb:00/00/00"; |
|
|
982 | $img = $img->muladd (1 - $fade, 0) if $fade; |
|
|
983 | |
|
|
984 | $img |
|
|
985 | } |
|
|
986 | |
841 | =back |
987 | =back |
842 | |
988 | |
843 | =head2 OTHER STUFF |
989 | =head2 OTHER STUFF |
844 | |
990 | |
845 | Anything that didn't fit any of the other categories, even after applying |
991 | Anything that didn't fit any of the other categories, even after applying |
846 | force and closing our eyes. |
992 | force and closing our eyes. |
847 | |
993 | |
848 | =over 4 |
994 | =over 4 |
849 | |
995 | |
850 | =item once { ... } |
996 | =item keep { ... } |
851 | |
997 | |
852 | This function takes a code block as argument, that is, one or more |
998 | This operator takes a code block as argument, that is, one or more |
853 | statements enclosed by braces. |
999 | statements enclosed by braces. |
854 | |
1000 | |
855 | The trick is that this code block is only evaluated once - future calls |
1001 | The trick is that this code block is only evaluated when the outcome |
856 | will simply return the original image (yes, it should only be used with |
1002 | changes - on other calls the C<keep> simply returns the image it computed |
857 | images). |
1003 | previously (yes, it should only be used with images). Or in other words, |
|
|
1004 | C<keep> I<caches> the result of the code block so it doesn't need to be |
|
|
1005 | computed again. |
858 | |
1006 | |
859 | This can be extremely useful to avoid redoing the same slow operations |
1007 | This can be extremely useful to avoid redoing slow operations - for |
860 | again and again- for example, if your background expression takes the root |
1008 | example, if your background expression takes the root background, blurs it |
861 | background, blurs it and then root-aligns it it would have to blur the |
1009 | and then root-aligns it it would have to blur the root background on every |
862 | root background on every window move or resize. |
1010 | window move or resize. |
|
|
1011 | |
|
|
1012 | Another example is C<load>, which can be quite slow. |
863 | |
1013 | |
864 | In fact, urxvt itself encloses the whole expression in some kind of |
1014 | In fact, urxvt itself encloses the whole expression in some kind of |
865 | C<once> block so it only is reevaluated as required. |
1015 | C<keep> block so it only is reevaluated as required. |
866 | |
1016 | |
867 | Putting the blur into a C<once> block will make sure the blur is only done |
1017 | Putting the blur into a C<keep> block will make sure the blur is only done |
868 | once: |
1018 | once, while the C<rootalign> is still done each time the window moves. |
869 | |
1019 | |
870 | rootlign once { blur 10, root } |
1020 | rootalign keep { blur 10, root } |
871 | |
1021 | |
872 | This leaves the question of how to force reevaluation of the block, |
1022 | This leaves the question of how to force reevaluation of the block, |
873 | in case the root background changes: If expression inside the block |
1023 | in case the root background changes: If expression inside the block |
874 | is sensitive to some event (root background changes, window geometry |
1024 | is sensitive to some event (root background changes, window geometry |
875 | changes), then it will be reevaluated automatically as needed. |
1025 | changes), then it will be reevaluated automatically as needed. |
876 | |
1026 | |
877 | =item once_again |
|
|
878 | |
|
|
879 | Resets all C<once> block as if they had never been called, i.e. on the |
|
|
880 | next call they will be reevaluated again. |
|
|
881 | |
|
|
882 | =cut |
1027 | =cut |
883 | |
1028 | |
884 | sub once(&) { |
1029 | sub keep(&) { |
885 | my $id = $_[0]+0; |
1030 | my $id = $_[0]+0; |
886 | |
1031 | |
887 | local $frame = $self->{frame_cache}{$id} ||= [$frame]; |
1032 | local $frame = $self->{frame_cache}{$id} ||= [$frame]; |
888 | |
1033 | |
889 | unless ($frame->[FR_CACHE]) { |
1034 | unless ($frame->[FR_CACHE]) { |
… | |
… | |
907 | wantarray |
1052 | wantarray |
908 | ? @{ $frame->[FR_CACHE] } |
1053 | ? @{ $frame->[FR_CACHE] } |
909 | : $frame->[FR_CACHE][0] |
1054 | : $frame->[FR_CACHE][0] |
910 | } |
1055 | } |
911 | |
1056 | |
912 | sub once_again() { |
1057 | # sub keep_clear() { |
913 | delete $self->{frame_cache}; |
1058 | # delete $self->{frame_cache}; |
914 | } |
1059 | # } |
915 | |
1060 | |
916 | =back |
1061 | =back |
917 | |
1062 | |
918 | =cut |
1063 | =cut |
919 | |
1064 | |
… | |
… | |
932 | |
1077 | |
933 | # compiles a parsed expression |
1078 | # compiles a parsed expression |
934 | sub set_expr { |
1079 | sub set_expr { |
935 | my ($self, $expr) = @_; |
1080 | my ($self, $expr) = @_; |
936 | |
1081 | |
937 | $self->{root} = []; |
1082 | $self->{root} = []; # the outermost frame |
938 | $self->{expr} = $expr; |
1083 | $self->{expr} = $expr; |
939 | $self->recalculate; |
1084 | $self->recalculate; |
940 | } |
1085 | } |
941 | |
1086 | |
942 | # takes a hash of sensitivity indicators and installs watchers |
1087 | # takes a hash of sensitivity indicators and installs watchers |
… | |
… | |
983 | if ($again->{rootpmap}) { |
1128 | if ($again->{rootpmap}) { |
984 | $state->{rootpmap} = $self->on (rootpmap_change => $cb); |
1129 | $state->{rootpmap} = $self->on (rootpmap_change => $cb); |
985 | } else { |
1130 | } else { |
986 | delete $state->{rootpmap}; |
1131 | delete $state->{rootpmap}; |
987 | } |
1132 | } |
|
|
1133 | |
|
|
1134 | if ($again->{focus}) { |
|
|
1135 | $state->{focus} = $self->on (focus_in => $cb, focus_out => $cb); |
|
|
1136 | } else { |
|
|
1137 | delete $state->{focus}; |
|
|
1138 | } |
988 | } |
1139 | } |
989 | |
1140 | |
990 | # evaluate the current bg expression |
1141 | # evaluate the current bg expression |
991 | sub recalculate { |
1142 | sub recalculate { |
992 | my ($arg_self) = @_; |
1143 | my ($arg_self) = @_; |
… | |
… | |
1004 | |
1155 | |
1005 | # set environment to evaluate user expression |
1156 | # set environment to evaluate user expression |
1006 | |
1157 | |
1007 | local $self = $arg_self; |
1158 | local $self = $arg_self; |
1008 | local $HOME = $ENV{HOME}; |
1159 | local $HOME = $ENV{HOME}; |
1009 | local $frame = []; |
1160 | local $frame = $self->{root}; |
1010 | |
1161 | |
1011 | ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); |
1162 | ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); |
|
|
1163 | $focus = $self->focus; |
1012 | |
1164 | |
1013 | # evaluate user expression |
1165 | # evaluate user expression |
1014 | |
1166 | |
1015 | my @img = eval { $self->{expr}->() }; |
1167 | my @img = eval { $self->{expr}->() }; |
1016 | die $@ if $@; |
1168 | die $@ if $@; |
… | |
… | |
1034 | # } |
1186 | # } |
1035 | |
1187 | |
1036 | # set background pixmap |
1188 | # set background pixmap |
1037 | |
1189 | |
1038 | $self->set_background ($img, $self->{border}); |
1190 | $self->set_background ($img, $self->{border}); |
1039 | $self->scr_recolour (0); |
1191 | $self->scr_recolor (0); |
1040 | $self->want_refresh; |
1192 | $self->want_refresh; |
1041 | } |
1193 | } |
1042 | |
1194 | |
1043 | sub on_start { |
1195 | sub on_start { |
1044 | my ($self) = @_; |
1196 | my ($self) = @_; |