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.62 by root, Sun Jun 17 21:58:18 2012 UTC vs.
Revision 1.63 by root, Tue Jun 19 18:17:56 2012 UTC

138pseudo-transparency, as the image seems to be static while the window is 138pseudo-transparency, as the image seems to be static while the window is
139moved around. 139moved around.
140 140
141=head2 CYCLES AND CACHING 141=head2 CYCLES AND CACHING
142 142
143=head3 C<load> et al.
144
143As has been mentioned before, the expression might be evaluated multiple 145As has been mentioned before, the expression might be evaluated multiple
144times. Each time the expression is reevaluated, a new cycle is said to 146times. Each time the expression is reevaluated, a new cycle is said to
145have begun. Many operators cache their results till the next cycle. 147have begun. Many operators cache their results till the next cycle.
146 148
147For example, the C<load> operator keeps a copy of the image. If it is 149For example, the C<load> operator keeps a copy of the image. If it is
171 173
172Here, a path is selected randomly, and load is only called for one image, 174Here, a path is selected randomly, and load is only called for one image,
173so keeps only one image in memory. If, on the next evaluation, luck 175so keeps only one image in memory. If, on the next evaluation, luck
174decides to use the other path, then it will have to load that image again. 176decides to use the other path, then it will have to load that image again.
175 177
178=head3 C<once { ... }>
179
180Another way to cache expensive operations is to use C<once { ... }>. The
181C<once> operator takes a block of multiple statements enclosed by C<{}>
182and evaluates it only.. once, returning any images the last statement
183returned. Further calls simply produce the values from the cache.
184
185This is most useful for expensive operations, such as C<blur>:
186
187 rootalign once { blur 20, root }
188
189This makes a blurred copy of the root background once, and on subsequent
190calls, just root-aligns it. Since C<blur> is usually quite slow and
191C<rootalign> is quite fast, this trades extra memory (For the cached
192blurred pixmap) with speed (blur only needs to be redone when root
193changes).
194
176=head1 REFERENCE 195=head1 REFERENCE
177 196
178=head2 COMMAND LINE SWITCHES 197=head2 COMMAND LINE SWITCHES
179 198
180=over 4 199=over 4
204 223
205=cut 224=cut
206 225
207our %_IMG_CACHE; 226our %_IMG_CACHE;
208our $HOME; 227our $HOME;
209our ($self, $old, $new); 228our ($self, $frame);
210our ($x, $y, $w, $h); 229our ($x, $y, $w, $h);
211 230
212# enforce at least this interval between updates 231# enforce at least this interval between updates
213our $MIN_INTERVAL = 6/59.951; 232our $MIN_INTERVAL = 6/59.951;
214 233
215{ 234{
216 package urxvt::bgdsl; # background language 235 package urxvt::bgdsl; # background language
236
237 sub FR_PARENT() { 0 } # parent frame, if any - must be #0
238 sub FR_CACHE () { 1 } # cached values
239 sub FR_AGAIN () { 2 } # what this expr is sensitive to
240 sub FR_STATE () { 3 } # watchers etc.
217 241
218 use List::Util qw(min max sum shuffle); 242 use List::Util qw(min max sum shuffle);
219 243
220=head2 PROVIDERS/GENERATORS 244=head2 PROVIDERS/GENERATORS
221 245
231mode. 255mode.
232 256
233Loaded images will be cached for one cycle, and shared between temrinals 257Loaded images will be cached for one cycle, and shared between temrinals
234running in the same process (e.g. in C<urxvtd>). 258running in the same process (e.g. in C<urxvtd>).
235 259
236=item load_uc $path 260#=item load_uc $path
237 261#
238Load uncached - same as load, but does not cache the image. This function 262#Load uncached - same as load, but does not cache the image. This function
239is most useufl if you want to optimise a background expression in some 263#is most useufl if you want to optimise a background expression in some
240way. 264#way.
241 265
242=cut 266=cut
243 267
244 sub load_uc($) { 268 sub load($) {
245 my ($path) = @_; 269 my ($path) = @_;
246 270
247 $_IMG_CACHE{$path} || do { 271 $_IMG_CACHE{$path} || do {
248 my $img = $self->new_img_from_file ($path); 272 my $img = $self->new_img_from_file ($path);
249 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img); 273 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img);
250 $img 274 $img
251 } 275 }
252 } 276 }
253 277
254 sub load($) {
255 my ($path) = @_;
256
257 $new->{load}{$path} = $old->{load}{$path} || load_uc $path;
258 }
259
260=item root 278=item root
261 279
262Returns the root window pixmap, that is, hopefully, the background image 280Returns the root window pixmap, that is, hopefully, the background image
263of your screen. 281of your screen.
264 282
266reevaluated when the bg image changes. 284reevaluated when the bg image changes.
267 285
268=cut 286=cut
269 287
270 sub root() { 288 sub root() {
271 $new->{again}{rootpmap} = 1; 289 $frame->[FR_AGAIN]{rootpmap} = 1;
272 $self->new_img_from_root 290 $self->new_img_from_root
273 } 291 }
274 292
275=item solid $colour 293=item solid $colour
276 294
472 490
473 clip move -TX, -TY, once { blur 5, root } 491 clip move -TX, -TY, once { blur 5, root }
474 492
475=cut 493=cut
476 494
477 sub TX() { $new->{again}{position} = 1; $x } 495 sub TX() { $frame->[FR_AGAIN]{position} = 1; $x }
478 sub TY() { $new->{again}{position} = 1; $y } 496 sub TY() { $frame->[FR_AGAIN]{position} = 1; $y }
479 sub TW() { $new->{again}{size} = 1; $w } 497 sub TW() { $frame->[FR_AGAIN]{size} = 1; $w }
480 sub TH() { $new->{again}{size} = 1; $h } 498 sub TH() { $frame->[FR_AGAIN]{size} = 1; $h }
481 499
482=item now 500=item now
483 501
484Returns the current time as (fractional) seconds since the epoch. 502Returns the current time as (fractional) seconds since the epoch.
485 503
504=cut 522=cut
505 523
506 sub now() { urxvt::NOW } 524 sub now() { urxvt::NOW }
507 525
508 sub again($) { 526 sub again($) {
509 $new->{again}{time} = $_[0]; 527 $frame->[FR_AGAIN]{time} = $_[0];
510 } 528 }
511 529
512 sub counter($) { 530 sub counter($) {
513 $new->{again}{time} = $_[0]; 531 $frame->[FR_AGAIN]{time} = $_[0];
514 $self->{counter} + 0 532 $frame->[FR_STATE]{counter} + 0
515 } 533 }
516 534
517=back 535=back
518 536
519=head2 SHAPE CHANGING OPERATORS 537=head2 SHAPE CHANGING OPERATORS
819 837
820The trick is that this code block is only evaluated once - future calls 838The trick is that this code block is only evaluated once - future calls
821will simply return the original image (yes, it should only be used with 839will simply return the original image (yes, it should only be used with
822images). 840images).
823 841
824This can be extremely useful to avoid redoign the same slow operations 842This can be extremely useful to avoid redoing the same slow operations
825again and again- for example, if your background expression takes the root 843again and again- for example, if your background expression takes the root
826background, blurs it and then root-aligns it it would have to blur the 844background, blurs it and then root-aligns it it would have to blur the
827root background on every window move or resize. 845root background on every window move or resize.
828 846
847In fact, urxvt itself encloses the whole expression in some kind of
848C<once> block so it only is reevaluated as required.
849
829Putting the blur into a C<once> block will make sure the blur is only done 850Putting the blur into a C<once> block will make sure the blur is only done
830once: 851once:
831 852
832 rootlign once { blur 10, root } 853 rootlign once { blur 10, root }
833 854
834This leaves the question of how to force reevaluation of the block, in 855This leaves the question of how to force reevaluation of the block,
835case the root background changes: Right now, all once blocks forget that 856in case the root background changes: If expression inside the block
836they ahve been executed before each time the root background changes (if 857is sensitive to some event (root background changes, window geometry
837the expression is sensitive to that) or when C<once_again> is called. 858changes), then it will be reevaluated automatically as needed.
838 859
839=item once_again 860=item once_again
840 861
841Resets all C<once> block as if they had never been called, i.e. on the 862Resets all C<once> block as if they had never been called, i.e. on the
842next call they will be reevaluated again. 863next call they will be reevaluated again.
843 864
844=cut 865=cut
845 866
846 sub once(&) { 867 sub once(&) {
847 my $once = $self->{once_cache}{$_[0]+0} ||= do { 868 my $id = $_[0]+0;
848 local $new->{again}; 869
849 my @res = $_[0](); 870 local $frame = $self->{frame_cache}{$id} ||= [$frame];
850 [$new->{again}, \@res] 871
872 unless ($frame->[FR_CACHE]) {
873 $frame->[FR_CACHE] = [ $_[0]() ];
874
875 my $self = $self;
876 my $frame = $frame;
877 Scalar::Util::weaken $frame;
878 $self->compile_frame ($frame, sub {
879 # clear this frame cache, also for all parents
880 for (my $frame = $frame; $frame; $frame = $frame->[0]) {
881 undef $frame->[FR_CACHE];
882 }
883
884 unless ($self->{term}) {
885 use Data::Dump;
886 ddx $frame;
887 exit;
888 }
889
890 $self->recalculate;
851 }; 891 });
852
853 $new->{again} = {
854 %{ $new->{again} },
855 %{ $once->[0] }
856 }; 892 };
857 893
858 # in scalar context we always return the first original result, which 894 # in scalar context we always return the first original result, which
859 # is not quite how perl works. 895 # is not quite how perl works.
860 wantarray 896 wantarray
861 ? @{ $once->[1] } 897 ? @{ $frame->[FR_CACHE] }
862 : $once->[1][0] 898 : $frame->[FR_CACHE][0]
863 } 899 }
864 900
865 sub once_again() { 901 sub once_again() {
866 delete $self->{once_cache}; 902 delete $self->{frame_cache};
867 } 903 }
868 904
869=back 905=back
870 906
871=cut 907=cut
872 908
873} 909}
874 910
875sub parse_expr { 911sub parse_expr {
876 my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}"; 912 my $expr = eval
913 "sub {\n"
914 . "package urxvt::bgdsl;\n"
915 . "#line 0 'background expression'\n"
916 . "$_[0]\n"
917 . "}";
877 die if $@; 918 die if $@;
878 $expr 919 $expr
879} 920}
880 921
881# compiles a parsed expression 922# compiles a parsed expression
882sub set_expr { 923sub set_expr {
883 my ($self, $expr) = @_; 924 my ($self, $expr) = @_;
884 925
926 $self->{root} = [];
885 $self->{expr} = $expr; 927 $self->{expr} = $expr;
886 $self->recalculate; 928 $self->recalculate;
929}
930
931# takes a hash of sensitivity indicators and installs watchers
932sub compile_frame {
933 my ($self, $frame, $cb) = @_;
934
935 my $state = $frame->[urxvt::bgdsl::FR_STATE] ||= {};
936 my $again = $frame->[urxvt::bgdsl::FR_AGAIN];
937
938 # don't keep stuff alive
939 Scalar::Util::weaken $state;
940
941 if ($again->{nested}) {
942 $state->{nested} = 1;
943 } else {
944 delete $state->{nested};
945 }
946
947 if (my $interval = $again->{time}) {
948 $state->{time} = [$interval, urxvt::timer->new->after ($interval)->interval ($interval)]
949 if $state->{time}[0] != $interval;
950
951 # callback *might* have changed, although we could just rule that out
952 $state->{time}[1]->cb (sub {
953 ++$state->{counter};
954 $cb->();
955 });
956 } else {
957 delete $state->{time};
958 }
959
960 if ($again->{position}) {
961 $state->{position} = $self->on (position_change => $cb);
962 } else {
963 delete $state->{position};
964 }
965
966 if ($again->{size}) {
967 $state->{size} = $self->on (size_change => $cb);
968 } else {
969 delete $state->{size};
970 }
971
972 if ($again->{rootpmap}) {
973 $state->{rootpmap} = $self->on (rootpmap_change => $cb);
974 } else {
975 delete $state->{rootpmap};
976 }
887} 977}
888 978
889# evaluate the current bg expression 979# evaluate the current bg expression
890sub recalculate { 980sub recalculate {
891 my ($arg_self) = @_; 981 my ($arg_self) = @_;
901 991
902 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; 992 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
903 993
904 # set environment to evaluate user expression 994 # set environment to evaluate user expression
905 995
906 local $self = $arg_self; 996 local $self = $arg_self;
907
908 local $HOME = $ENV{HOME}; 997 local $HOME = $ENV{HOME};
909 local $old = $self->{state}; 998 local $frame = [];
910 local $new = my $state = $self->{state} = {};
911 999
912 ($x, $y, $w, $h) =
913 $self->background_geometry ($self->{border}); 1000 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
914 1001
915 # evaluate user expression 1002 # evaluate user expression
916 1003
917 my $img = eval { urxvt::bgdsl::merge $self->{expr}->() }; 1004 my @img = eval { $self->{expr}->() };
918 die $@ if $@; 1005 die $@ if $@;
919 die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img"; 1006 die "background-expr did not return anything.\n" unless @img;
1007 die "background-expr: expected image(s), got something else.\n"
1008 if grep { !UNIVERSAL::isa $_, "urxvt::img" } @img;
1009
1010 my $img = urxvt::bgdsl::merge @img;
1011
1012 $frame->[FR_AGAIN]{size} = 1
1013 if $img->repeat_mode != urxvt::RepeatNormal;
920 1014
921 # if the expression is sensitive to external events, prepare reevaluation then 1015 # if the expression is sensitive to external events, prepare reevaluation then
922 1016 $self->compile_frame ($frame, sub { $arg_self->recalculate });
923 my $again = delete $state->{again};
924
925 $again->{size} = 1
926 if $img->repeat_mode != urxvt::RepeatNormal;
927
928 if (my $again = $again->{time}) {
929 my $self = $self;
930 $state->{timer} = $again == $old->{again}
931 ? $old->{timer}
932 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
933 ++$self->{counter};
934 $self->recalculate
935 });
936 }
937
938 if ($again->{position}) {
939 $self->enable (position_change => sub { $_[0]->recalculate });
940 } else {
941 $self->disable ("position_change");
942 }
943
944 if ($again->{size}) {
945 $self->enable (size_change => sub { $_[0]->recalculate });
946 } else {
947 $self->disable ("size_change");
948 }
949
950 if ($again->{rootpmap}) {
951 $self->enable (rootpmap_change => sub {
952 delete $_[0]{once_cache}; # this will override once-block values from
953 $_[0]->recalculate;
954 });
955 } else {
956 $self->disable ("rootpmap_change");
957 }
958 1017
959 # clear stuff we no longer need 1018 # clear stuff we no longer need
960 1019
961 %$old = (); 1020# unless (%{ $frame->[FR_STATE] }) {
962
963 unless (%$again) {
964 delete $self->{state}; 1021# delete $self->{state};
965 delete $self->{expr}; 1022# delete $self->{expr};
966 } 1023# }
967 1024
968 # set background pixmap 1025 # set background pixmap
969 1026
970 $self->set_background ($img, $self->{border}); 1027 $self->set_background ($img, $self->{border});
971 $self->scr_recolour (0); 1028 $self->scr_recolour (0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines