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.45 by root, Sun Jun 10 11:53:32 2012 UTC vs.
Revision 1.63 by root, Tue Jun 19 18:17:56 2012 UTC

1#! perl 1#! perl
2 2
3#:META:X_RESOURCE:%.expr:string:background expression 3#:META:X_RESOURCE:%.expr:string:background expression
4#:META:X_RESOURCE:%.border.:boolean:respect the terminal border 4#:META:X_RESOURCE:%.border:boolean:respect the terminal border
5 5#:META:X_RESOURCE:%.interval:seconds:minimum time between updates
6#TODO: once, rootalign
7 6
8=head1 NAME 7=head1 NAME
9 8
10 background - manage terminal background 9 background - manage terminal background
11 10
12=head1 SYNOPSIS 11=head1 SYNOPSIS
13 12
14 urxvt --background-expr 'background expression' 13 urxvt --background-expr 'background expression'
15 --background-border 14 --background-border
15 --background-interval seconds
16 16
17=head1 DESCRIPTION 17=head1 DESCRIPTION
18 18
19This extension manages the terminal background by creating a picture that 19This extension manages the terminal background by creating a picture that
20is behind the text, replacing the normal background colour. 20is behind the text, replacing the normal background colour.
57 57
58For example, an expression such as C<scale load "$HOME/mybg.png"> scales the 58For example, an expression such as C<scale load "$HOME/mybg.png"> scales the
59image to the window size, so it relies on the window size and will 59image to the window size, so it relies on the window size and will
60be reevaluated each time it is changed, but not when it moves for 60be reevaluated each time it is changed, but not when it moves for
61example. That ensures that the picture always fills the terminal, even 61example. That ensures that the picture always fills the terminal, even
62after it's size changes. 62after its size changes.
63 63
64=head2 EXPRESSIONS 64=head2 EXPRESSIONS
65 65
66Expressions are normal Perl expressions, in fact, they are Perl blocks - 66Expressions are normal Perl expressions, in fact, they are Perl blocks -
67which means you could use multiple lines and statements: 67which means you could use multiple lines and statements:
71 return scale load "$HOME/weekday.png"; 71 return scale load "$HOME/weekday.png";
72 } else { 72 } else {
73 return scale load "$HOME/sunday.png"; 73 return scale load "$HOME/sunday.png";
74 } 74 }
75 75
76This expression gets evaluated once per hour. It will set F<sunday.png> as 76This expression is evaluated once per hour. It will set F<sunday.png> as
77background on Sundays, and F<weekday.png> on all other days. 77background on Sundays, and F<weekday.png> on all other days.
78 78
79Fortunately, we expect that most expressions will be much simpler, with 79Fortunately, we expect that most expressions will be much simpler, with
80little Perl knowledge needed. 80little Perl knowledge needed.
81 81
115horizontal and vertical dimensions. For example, this halves the image 115horizontal and vertical dimensions. For example, this halves the image
116width and doubles the image height: 116width and doubles the image height:
117 117
118 scale 0.5, 2, load "$HOME/mypic.png" 118 scale 0.5, 2, load "$HOME/mypic.png"
119 119
120Other effects than scalign are also readily available, for exmaple, you can 120Other effects than scaling are also readily available, for example, you can
121tile the image to fill the whole window, instead of resizing it: 121tile the image to fill the whole window, instead of resizing it:
122 122
123 tile load "$HOME/mypic.png" 123 tile load "$HOME/mypic.png"
124 124
125In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator 125In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator
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
151This only works for one cycle though, so as long as you load the same 153This only works for one cycle though, so as long as you load the same
152image every time, it will always be cached, but when you load a different 154image every time, it will always be cached, but when you load a different
153image, it will forget about the first one. 155image, it will forget about the first one.
154 156
155This allows you to either speed things up by keeping multiple images in 157This allows you to either speed things up by keeping multiple images in
156memory, or comserve memory by loading images more often. 158memory, or conserve memory by loading images more often.
157 159
158For example, you can keep two images in memory and use a random one like 160For example, you can keep two images in memory and use a random one like
159this: 161this:
160 162
161 my $img1 = load "img1.png"; 163 my $img1 = load "img1.png";
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
189overwriting borders and any other areas, such as the scrollbar. 208overwriting borders and any other areas, such as the scrollbar.
190 209
191Specifying this flag changes the behaviour, so that the image only 210Specifying this flag changes the behaviour, so that the image only
192replaces the background of the character area. 211replaces the background of the character area.
193 212
213=item --background-interval seconds
214
215Since some operations in the underlying XRender extension can effectively
216freeze your X-server for prolonged time, this extension enforces a minimum
217time between updates, which is normally about 0.1 seconds.
218
219If you want to do updates more often, you can decrease this safety
220interval with this switch.
221
194=back 222=back
195 223
196=cut 224=cut
197 225
226our %_IMG_CACHE;
198our $HOME; 227our $HOME;
199our ($self, $old, $new); 228our ($self, $frame);
200our ($x, $y, $w, $h); 229our ($x, $y, $w, $h);
201 230
202# enforce at least this interval between updates 231# enforce at least this interval between updates
203our $MIN_INTERVAL = 1/100; 232our $MIN_INTERVAL = 6/59.951;
204 233
205{ 234{
206 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.
207 241
208 use List::Util qw(min max sum shuffle); 242 use List::Util qw(min max sum shuffle);
209 243
210=head2 PROVIDERS/GENERATORS 244=head2 PROVIDERS/GENERATORS
211 245
218=item load $path 252=item load $path
219 253
220Loads the image at the given C<$path>. The image is set to plane tiling 254Loads the image at the given C<$path>. The image is set to plane tiling
221mode. 255mode.
222 256
223Loaded images will be cached for one cycle. 257Loaded images will be cached for one cycle, and shared between temrinals
258running in the same process (e.g. in C<urxvtd>).
259
260#=item load_uc $path
261#
262#Load uncached - same as load, but does not cache the image. This function
263#is most useufl if you want to optimise a background expression in some
264#way.
224 265
225=cut 266=cut
226 267
227 sub load($) { 268 sub load($) {
228 my ($path) = @_; 269 my ($path) = @_;
229 270
230 $new->{load}{$path} = $old->{load}{$path} || $self->new_img_from_file ($path); 271 $_IMG_CACHE{$path} || do {
272 my $img = $self->new_img_from_file ($path);
273 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img);
274 $img
275 }
231 } 276 }
232 277
233=item root 278=item root
234 279
235Returns the root window pixmap, that is, hopefully, the background image 280Returns the root window pixmap, that is, hopefully, the background image
236of your screen. The image is set to extend mode. 281of your screen.
237 282
238This function makes your expression root sensitive, that means it will be 283This function makes your expression root sensitive, that means it will be
239reevaluated when the bg image changes. 284reevaluated when the bg image changes.
240 285
241=cut 286=cut
242 287
243 sub root() { 288 sub root() {
244 $new->{rootpmap_sensitive} = 1; 289 $frame->[FR_AGAIN]{rootpmap} = 1;
245 die "root op not supported, exg, we need you"; 290 $self->new_img_from_root
246 } 291 }
247 292
248=item solid $colour 293=item solid $colour
249 294
250=item solid $width, $height, $colour 295=item solid $width, $height, $colour
258=cut 303=cut
259 304
260 sub solid($;$$) { 305 sub solid($;$$) {
261 my $colour = pop; 306 my $colour = pop;
262 307
263 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); 308 my $img = $self->new_img (urxvt::PictStandardARGB32, 0, 0, $_[0] || 1, $_[1] || 1);
264 $img->fill ($colour); 309 $img->fill ($colour);
265 $img 310 $img
266 } 311 }
267 312
268=item clone $img 313=item clone $img
274 319
275 sub clone($) { 320 sub clone($) {
276 $_[0]->clone 321 $_[0]->clone
277 } 322 }
278 323
279=back 324=item merge $img ...
325
326Takes any number of images and merges them together, creating a single
327image containing them all. The tiling mode of the first image is used as
328the tiling mdoe of the resulting image.
329
330This function is called automatically when an expression returns multiple
331images.
332
333=cut
334
335 sub merge(@) {
336 return $_[0] unless $#_;
337
338 # rather annoyingly clumsy, but optimisation is for another time
339
340 my $x0 = +1e9;
341 my $y0 = +1e9;
342 my $x1 = -1e9;
343 my $y1 = -1e9;
344
345 for (@_) {
346 my ($x, $y, $w, $h) = $_->geometry;
347
348 $x0 = $x if $x0 > $x;
349 $y0 = $y if $y0 > $y;
350
351 $x += $w;
352 $y += $h;
353
354 $x1 = $x if $x1 < $x;
355 $y1 = $y if $y1 < $y;
356 }
357
358 my $base = $self->new_img (urxvt::PictStandardARGB32, $x0, $y0, $x1 - $x0, $y1 - $y0);
359 $base->repeat_mode ($_[0]->repeat_mode);
360 $base->fill ([0, 0, 0, 0]);
361
362 $base->draw ($_)
363 for @_;
364
365 $base
366 }
280 367
281=head2 TILING MODES 368=head2 TILING MODES
282 369
283The following operators modify the tiling mode of an image, that is, the 370The following operators modify the tiling mode of an image, that is, the
284way that pixels outside the image area are painted when the image is used. 371way that pixels outside the image area are painted when the image is used.
314become transparent. This mode is most useful when you want to place an 401become transparent. This mode is most useful when you want to place an
315image over another image or the background colour while leaving all 402image over another image or the background colour while leaving all
316background pixels outside the image unchanged. 403background pixels outside the image unchanged.
317 404
318Example: load an image and display it in the upper left corner. The rest 405Example: load an image and display it in the upper left corner. The rest
319of the space is left "empty" (transparent or wahtever your compisotr does 406of the space is left "empty" (transparent or whatever your compositor does
320in alpha mode, else background colour). 407in alpha mode, else background colour).
321 408
322 pad load "mybg.png" 409 pad load "mybg.png"
323 410
324=item extend $img 411=item extend $img
325 412
326Extends the image over the whole plane, using the closest pixel in the 413Extends the image over the whole plane, using the closest pixel in the
327area outside the image. This mode is mostly useful when you more complex 414area outside the image. This mode is mostly useful when you use more complex
328filtering operations and want the pixels outside the image to have the 415filtering operations and want the pixels outside the image to have the
329same values as the pixels near the edge. 416same values as the pixels near the edge.
330 417
331Example: just for curiosity, how does this pixel extension stuff work? 418Example: just for curiosity, how does this pixel extension stuff work?
332 419
399the window size to conserve memory. 486the window size to conserve memory.
400 487
401Example: take the screen background, clip it to the window size, blur it a 488Example: take the screen background, clip it to the window size, blur it a
402bit, align it to the window position and use it as background. 489bit, align it to the window position and use it as background.
403 490
404 clip move -TX, -TY, blur 5, root 491 clip move -TX, -TY, once { blur 5, root }
405 492
406=cut 493=cut
407 494
408 sub TX() { $new->{position_sensitive} = 1; $x } 495 sub TX() { $frame->[FR_AGAIN]{position} = 1; $x }
409 sub TY() { $new->{position_sensitive} = 1; $y } 496 sub TY() { $frame->[FR_AGAIN]{position} = 1; $y }
410 sub TW() { $new->{size_sensitive} = 1; $w } 497 sub TW() { $frame->[FR_AGAIN]{size} = 1; $w }
411 sub TH() { $new->{size_sensitive} = 1; $h } 498 sub TH() { $frame->[FR_AGAIN]{size} = 1; $h }
412 499
413=item now 500=item now
414 501
415Returns the current time as (fractional) seconds since the epoch. 502Returns the current time as (fractional) seconds since the epoch.
416 503
423C<$seconds> seconds. 510C<$seconds> seconds.
424 511
425Example: load some image and rotate it according to the time of day (as if it were 512Example: load some image and rotate it according to the time of day (as if it were
426the hour pointer of a clock). Update this image every minute. 513the hour pointer of a clock). Update this image every minute.
427 514
428 again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" 515 again 60; rotate 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png"
429 516
430=item counter $seconds 517=item counter $seconds
431 518
432Like C<again>, but also returns an increasing counter value, starting at 519Like C<again>, but also returns an increasing counter value, starting at
4330, which might be useful for some simple animation effects. 5200, which might be useful for some simple animation effects.
435=cut 522=cut
436 523
437 sub now() { urxvt::NOW } 524 sub now() { urxvt::NOW }
438 525
439 sub again($) { 526 sub again($) {
440 $new->{again} = $_[0]; 527 $frame->[FR_AGAIN]{time} = $_[0];
441 } 528 }
442 529
443 sub counter($) { 530 sub counter($) {
444 $new->{again} = $_[0]; 531 $frame->[FR_AGAIN]{time} = $_[0];
445 $self->{counter} + 0 532 $frame->[FR_STATE]{counter} + 0
446 } 533 }
447 534
448=back 535=back
449 536
450=head2 SHAPE CHANGING OPERATORS 537=head2 SHAPE CHANGING OPERATORS
553 640
554Example: move the image right by 20 pixels and down by 30. 641Example: move the image right by 20 pixels and down by 30.
555 642
556 move 20, 30, ... 643 move 20, 30, ...
557 644
645=item align $xalign, $yalign, $img
646
647Aligns the image according to a factor - C<0> means the image is moved to
648the left or top edge (for C<$xalign> or C<$yalign>), C<0.5> means it is
649exactly centered and C<1> means it touches the right or bottom edge.
650
651Example: remove any visible border around an image, center it vertically but move
652it to the right hand side.
653
654 align 1, 0.5, pad $img
655
558=item center $img 656=item center $img
559 657
560=item center $width, $height, $img 658=item center $width, $height, $img
561 659
562Centers the image, i.e. the center of the image is moved to the center of 660Centers the image, i.e. the center of the image is moved to the center of
563the terminal window (or the box specified by C<$width> and C<$height> if 661the terminal window (or the box specified by C<$width> and C<$height> if
564given). 662given).
663
664Example: load an image and center it.
665
666 center pad load "mybg.png"
565 667
566=item rootalign $img 668=item rootalign $img
567 669
568Moves the image so that it appears glued to the screen as opposed to the 670Moves the image so that it appears glued to the screen as opposed to the
569window. This gives the illusion of a larger area behind the window. It is 671window. This gives the illusion of a larger area behind the window. It is
575 rootalign mirror load "mybg.png" 677 rootalign mirror load "mybg.png"
576 678
577Example: take the screen background and align it, giving the illusion of 679Example: take the screen background and align it, giving the illusion of
578transparency as long as the window isn't in front of other windows. 680transparency as long as the window isn't in front of other windows.
579 681
580 rootalign root 682 rootalign root
581 683
582=cut 684=cut
583 685
584 sub move($$;$) { 686 sub move($$;$) {
585 my $img = pop->clone; 687 my $img = pop->clone;
586 $img->move ($_[0], $_[1]); 688 $img->move ($_[0], $_[1]);
587 $img 689 $img
588 } 690 }
589 691
692 sub align($;$$) {
693 my $img = pop;
694
695 move $_[0] * (TW - $img->w),
696 $_[1] * (TH - $img->h),
697 $img
698 }
699
590 sub center($;$$) { 700 sub center($;$$) {
591 my $img = pop; 701 my $img = pop;
592 my $w = $_[0] || TW; 702 my $w = $_[0] || TW;
593 my $h = $_[0] || TH; 703 my $h = $_[1] || TH;
594 704
595 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img 705 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img
596 } 706 }
597 707
598 sub rootalign($) { 708 sub rootalign($) {
599 move -TX, -TY, $_[0] 709 move -TX, -TY, $_[0]
710 }
711
712=item rotate $center_x, $center_y, $degrees
713
714Rotates the image by C<$degrees> degrees, counter-clockwise, around the
715pointer at C<$center_x> and C<$center_y> (specified as factor of image
716width/height).
717
718#TODO# new width, height, maybe more operators?
719
720Example: rotate the image by 90 degrees
721
722=cut
723
724 sub rotate($$$$) {
725 my $img = pop;
726 $img->rotate (
727 $_[0] * ($img->w + $img->x),
728 $_[1] * ($img->h + $img->y),
729 $_[2] * (3.14159265 / 180),
730 )
600 } 731 }
601 732
602=back 733=back
603 734
604=head2 COLOUR MODIFICATIONS 735=head2 COLOUR MODIFICATIONS
640 771
641Values less than 0 reduce brightness, while values larger than 0 increase 772Values less than 0 reduce brightness, while values larger than 0 increase
642it. Useful range is from -1 to 1 - the former results in a black, the 773it. Useful range is from -1 to 1 - the former results in a black, the
643latter in a white picture. 774latter in a white picture.
644 775
645Due to idiosynchrasies in the underlying XRender extension, biases less 776Due to idiosyncrasies in the underlying XRender extension, biases less
646than zero can be I<very> slow. 777than zero can be I<very> slow.
647 778
648=cut 779=cut
649 780
650 sub contrast($$;$$;$) { 781 sub contrast($$;$$;$) {
651 my $img = pop; 782 my $img = pop;
652 my ($r, $g, $b, $a) = @_; 783 my ($r, $g, $b, $a) = @_;
653 784
654 ($g, $b) = ($r, $r) if @_ < 4; 785 ($g, $b) = ($r, $r) if @_ < 3;
655 $a = 1 if @_ < 5; 786 $a = 1 if @_ < 4;
656 787
657 $img = $img->clone; 788 $img = $img->clone;
658 $img->contrast ($r, $g, $b, $a); 789 $img->contrast ($r, $g, $b, $a);
659 $img 790 $img
660 } 791 }
661 792
662 sub brightness($$;$$;$) { 793 sub brightness($$;$$;$) {
663 my $img = pop; 794 my $img = pop;
664 my ($r, $g, $b, $a) = @_; 795 my ($r, $g, $b, $a) = @_;
665 796
666 ($g, $b) = ($r, $r) if @_ < 4; 797 ($g, $b) = ($r, $r) if @_ < 3;
667 $a = 1 if @_ < 5; 798 $a = 1 if @_ < 4;
668 799
669 $img = $img->clone; 800 $img = $img->clone;
670 $img->brightness ($r, $g, $b, $a); 801 $img->brightness ($r, $g, $b, $a);
671 $img 802 $img
672 } 803 }
688 sub blur($$;$) { 819 sub blur($$;$) {
689 my $img = pop; 820 my $img = pop;
690 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) 821 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0])
691 } 822 }
692 823
693=item rotate $new_width, $new_height, $center_x, $center_y, $degrees
694
695Rotates the image by C<$degrees> degrees, counter-clockwise, around the
696pointer at C<$center_x> and C<$center_y> (specified as factor of image
697width/height), generating a new image with width C<$new_width> and height
698C<$new_height>.
699
700#TODO# new width, height, maybe more operators?
701
702Example: rotate the image by 90 degrees
703
704=cut
705
706 sub rotate($$$$$$) {
707 my $img = pop;
708 $img->rotate (
709 $_[0],
710 $_[1],
711 $_[2] * $img->w,
712 $_[3] * $img->h,
713 $_[4] * (3.14159265 / 180),
714 )
715 }
716
717=back 824=back
718 825
826=head2 OTHER STUFF
827
828Anything that didn't fit any of the other categories, even after applying
829force and closing our eyes.
830
831=over 4
832
833=item once { ... }
834
835This function takes a code block as argument, that is, one or more
836statements enclosed by braces.
837
838The trick is that this code block is only evaluated once - future calls
839will simply return the original image (yes, it should only be used with
840images).
841
842This can be extremely useful to avoid redoing the same slow operations
843again and again- for example, if your background expression takes the root
844background, blurs it and then root-aligns it it would have to blur the
845root background on every window move or resize.
846
847In fact, urxvt itself encloses the whole expression in some kind of
848C<once> block so it only is reevaluated as required.
849
850Putting the blur into a C<once> block will make sure the blur is only done
851once:
852
853 rootlign once { blur 10, root }
854
855This leaves the question of how to force reevaluation of the block,
856in case the root background changes: If expression inside the block
857is sensitive to some event (root background changes, window geometry
858changes), then it will be reevaluated automatically as needed.
859
860=item once_again
861
862Resets all C<once> block as if they had never been called, i.e. on the
863next call they will be reevaluated again.
864
865=cut
866
867 sub once(&) {
868 my $id = $_[0]+0;
869
870 local $frame = $self->{frame_cache}{$id} ||= [$frame];
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;
891 });
892 };
893
894 # in scalar context we always return the first original result, which
895 # is not quite how perl works.
896 wantarray
897 ? @{ $frame->[FR_CACHE] }
898 : $frame->[FR_CACHE][0]
899 }
900
901 sub once_again() {
902 delete $self->{frame_cache};
903 }
904
905=back
906
719=cut 907=cut
720 908
721} 909}
722 910
723sub parse_expr { 911sub parse_expr {
724 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 . "}";
725 die if $@; 918 die if $@;
726 $expr 919 $expr
727} 920}
728 921
729# compiles a parsed expression 922# compiles a parsed expression
730sub set_expr { 923sub set_expr {
731 my ($self, $expr) = @_; 924 my ($self, $expr) = @_;
732 925
926 $self->{root} = [];
733 $self->{expr} = $expr; 927 $self->{expr} = $expr;
734 $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 }
735} 977}
736 978
737# evaluate the current bg expression 979# evaluate the current bg expression
738sub recalculate { 980sub recalculate {
739 my ($arg_self) = @_; 981 my ($arg_self) = @_;
749 991
750 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; 992 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
751 993
752 # set environment to evaluate user expression 994 # set environment to evaluate user expression
753 995
754 local $self = $arg_self; 996 local $self = $arg_self;
755
756 local $HOME = $ENV{HOME}; 997 local $HOME = $ENV{HOME};
757 local $old = $self->{state}; 998 local $frame = [];
758 local $new = my $state = $self->{state} = {};
759 999
760 ($x, $y, $w, $h) =
761 $self->background_geometry ($self->{border}); 1000 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
762 1001
763 # evaluate user expression 1002 # evaluate user expression
764 1003
765 my $img = eval { $self->{expr}->() }; 1004 my @img = eval { $self->{expr}->() };
766 warn $@ if $@;#d# 1005 die $@ if $@;
1006 die "background-expr did not return anything.\n" unless @img;
1007 die "background-expr: expected image(s), got something else.\n"
767 die if !UNIVERSAL::isa $img, "urxvt::img"; 1008 if grep { !UNIVERSAL::isa $_, "urxvt::img" } @img;
768 1009
769 $state->{size_sensitive} = 1 1010 my $img = urxvt::bgdsl::merge @img;
1011
1012 $frame->[FR_AGAIN]{size} = 1
770 if $img->repeat_mode != urxvt::RepeatNormal; 1013 if $img->repeat_mode != urxvt::RepeatNormal;
771 1014
772 # if the expression is sensitive to external events, prepare reevaluation then 1015 # if the expression is sensitive to external events, prepare reevaluation then
773 1016 $self->compile_frame ($frame, sub { $arg_self->recalculate });
774 my $repeat;
775
776 if (my $again = $state->{again}) {
777 $repeat = 1;
778 my $self = $self;
779 $state->{timer} = $again == $old->{again}
780 ? $old->{timer}
781 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
782 ++$self->{counter};
783 $self->recalculate
784 });
785 }
786
787 if (delete $state->{position_sensitive}) {
788 $repeat = 1;
789 $self->enable (position_change => sub { $_[0]->recalculate });
790 } else {
791 $self->disable ("position_change");
792 }
793
794 if (delete $state->{size_sensitive}) {
795 $repeat = 1;
796 $self->enable (size_change => sub { $_[0]->recalculate });
797 } else {
798 $self->disable ("size_change");
799 }
800
801 if (delete $state->{rootpmap_sensitive}) {
802 $repeat = 1;
803 $self->enable (rootpmap_change => sub { $_[0]->recalculate });
804 } else {
805 $self->disable ("rootpmap_change");
806 }
807 1017
808 # clear stuff we no longer need 1018 # clear stuff we no longer need
809 1019
810 %$old = (); 1020# unless (%{ $frame->[FR_STATE] }) {
811
812 unless ($repeat) {
813 delete $self->{state}; 1021# delete $self->{state};
814 delete $self->{expr}; 1022# delete $self->{expr};
815 } 1023# }
816 1024
817 # set background pixmap 1025 # set background pixmap
818 1026
819 $self->set_background ($img, $self->{border}); 1027 $self->set_background ($img, $self->{border});
820 $self->scr_recolour (0); 1028 $self->scr_recolour (0);
822} 1030}
823 1031
824sub on_start { 1032sub on_start {
825 my ($self) = @_; 1033 my ($self) = @_;
826 1034
827 my $expr = $self->x_resource ("background.expr") 1035 my $expr = $self->x_resource ("%.expr")
828 or return; 1036 or return;
829 1037
1038 $self->has_render
1039 or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n";
1040
830 $self->set_expr (parse_expr $expr); 1041 $self->set_expr (parse_expr $expr);
831 $self->{border} = $self->x_resource_boolean ("background.border"); 1042 $self->{border} = $self->x_resource_boolean ("%.border");
1043
1044 $MIN_INTERVAL = $self->x_resource ("%.interval");
832 1045
833 () 1046 ()
834} 1047}
835 1048

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines