ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/background
Revision: 1.61
Committed: Fri Jun 15 19:50:56 2012 UTC (11 years, 11 months ago) by root
Branch: MAIN
Changes since 1.60: +7 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! perl
2
3 #:META:X_RESOURCE:%.expr:string:background expression
4 #:META:X_RESOURCE:%.border:boolean:respect the terminal border
5 #:META:X_RESOURCE:%.interval:seconds:minimum time between updates
6
7 =head1 NAME
8
9 background - manage terminal background
10
11 =head1 SYNOPSIS
12
13 urxvt --background-expr 'background expression'
14 --background-border
15 --background-interval seconds
16
17 =head1 DESCRIPTION
18
19 This extension manages the terminal background by creating a picture that
20 is behind the text, replacing the normal background colour.
21
22 It does so by evaluating a Perl expression that I<calculates> the image on
23 the fly, for example, by grabbing the root background or loading a file.
24
25 While the full power of Perl is available, the operators have been design
26 to be as simple as possible.
27
28 For example, to load an image and scale it to the window size, you would
29 use:
30
31 urxvt --background-expr 'scale load "/path/to/mybg.png"'
32
33 Or specified as a X resource:
34
35 URxvt.background-expr: scale load "/path/to/mybg.png"
36
37 =head1 THEORY OF OPERATION
38
39 At startup, just before the window is mapped for the first time, the
40 expression is evaluated and must yield an image. The image is then
41 extended as necessary to cover the whole terminal window, and is set as a
42 background pixmap.
43
44 If the image contains an alpha channel, then it will be used as-is in
45 visuals that support alpha channels (for example, for a compositing
46 manager). In other visuals, the terminal background colour will be used to
47 replace any transparency.
48
49 When the expression relies, directly or indirectly, on the window size,
50 position, the root pixmap, or a timer, then it will be remembered. If not,
51 then it will be removed.
52
53 If any of the parameters that the expression relies on changes (when the
54 window is moved or resized, its position or size changes; when the root
55 pixmap is replaced by another one the root background changes; or when the
56 timer elapses), then the expression will be evaluated again.
57
58 For example, an expression such as C<scale load "$HOME/mybg.png"> scales the
59 image to the window size, so it relies on the window size and will
60 be reevaluated each time it is changed, but not when it moves for
61 example. That ensures that the picture always fills the terminal, even
62 after its size changes.
63
64 =head2 EXPRESSIONS
65
66 Expressions are normal Perl expressions, in fact, they are Perl blocks -
67 which means you could use multiple lines and statements:
68
69 again 3600;
70 if (localtime now)[6]) {
71 return scale load "$HOME/weekday.png";
72 } else {
73 return scale load "$HOME/sunday.png";
74 }
75
76 This expression is evaluated once per hour. It will set F<sunday.png> as
77 background on Sundays, and F<weekday.png> on all other days.
78
79 Fortunately, we expect that most expressions will be much simpler, with
80 little Perl knowledge needed.
81
82 Basically, you always start with a function that "generates" an image
83 object, such as C<load>, which loads an image from disk, or C<root>, which
84 returns the root window background image:
85
86 load "$HOME/mypic.png"
87
88 The path is usually specified as a quoted string (the exact rules can be
89 found in the L<perlop> manpage). The F<$HOME> at the beginning of the
90 string is expanded to the home directory.
91
92 Then you prepend one or more modifiers or filtering expressions, such as
93 C<scale>:
94
95 scale load "$HOME/mypic.png"
96
97 Just like a mathematical expression with functions, you should read these
98 expressions from right to left, as the C<load> is evaluated first, and
99 its result becomes the argument to the C<scale> function.
100
101 Many operators also allow some parameters preceding the input image
102 that modify its behaviour. For example, C<scale> without any additional
103 arguments scales the image to size of the terminal window. If you specify
104 an additional argument, it uses it as a scale factor (multiply by 100 to
105 get a percentage):
106
107 scale 2, load "$HOME/mypic.png"
108
109 This enlarges the image by a factor of 2 (200%). As you can see, C<scale>
110 has now two arguments, the C<200> and the C<load> expression, while
111 C<load> only has one argument. Arguments are separated from each other by
112 commas.
113
114 Scale also accepts two arguments, which are then separate factors for both
115 horizontal and vertical dimensions. For example, this halves the image
116 width and doubles the image height:
117
118 scale 0.5, 2, load "$HOME/mypic.png"
119
120 Other effects than scaling are also readily available, for example, you can
121 tile the image to fill the whole window, instead of resizing it:
122
123 tile load "$HOME/mypic.png"
124
125 In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator
126 is kind of superfluous.
127
128 Another common effect is to mirror the image, so that the same edges touch:
129
130 mirror load "$HOME/mypic.png"
131
132 This is also a typical background expression:
133
134 rootalign root
135
136 It first takes a snapshot of the screen background image, and then
137 moves it to the upper left corner of the screen - the result is
138 pseudo-transparency, as the image seems to be static while the window is
139 moved around.
140
141 =head2 CYCLES AND CACHING
142
143 As has been mentioned before, the expression might be evaluated multiple
144 times. Each time the expression is reevaluated, a new cycle is said to
145 have begun. Many operators cache their results till the next cycle.
146
147 For example, the C<load> operator keeps a copy of the image. If it is
148 asked to load the same image on the next cycle it will not load it again,
149 but return the cached copy.
150
151 This only works for one cycle though, so as long as you load the same
152 image every time, it will always be cached, but when you load a different
153 image, it will forget about the first one.
154
155 This allows you to either speed things up by keeping multiple images in
156 memory, or conserve memory by loading images more often.
157
158 For example, you can keep two images in memory and use a random one like
159 this:
160
161 my $img1 = load "img1.png";
162 my $img2 = load "img2.png";
163 (0.5 > rand) ? $img1 : $img2
164
165 Since both images are "loaded" every time the expression is evaluated,
166 they are always kept in memory. Contrast this version:
167
168 my $path1 = "img1.png";
169 my $path2 = "img2.png";
170 load ((0.5 > rand) ? $path1 : $path2)
171
172 Here, a path is selected randomly, and load is only called for one image,
173 so keeps only one image in memory. If, on the next evaluation, luck
174 decides to use the other path, then it will have to load that image again.
175
176 =head1 REFERENCE
177
178 =head2 COMMAND LINE SWITCHES
179
180 =over 4
181
182 =item --background-expr perl-expression
183
184 Specifies the Perl expression to evaluate.
185
186 =item --background-border
187
188 By default, the expression creates an image that fills the full window,
189 overwriting borders and any other areas, such as the scrollbar.
190
191 Specifying this flag changes the behaviour, so that the image only
192 replaces the background of the character area.
193
194 =item --background-interval seconds
195
196 Since some operations in the underlying XRender extension can effectively
197 freeze your X-server for prolonged time, this extension enforces a minimum
198 time between updates, which is normally about 0.1 seconds.
199
200 If you want to do updates more often, you can decrease this safety
201 interval with this switch.
202
203 =back
204
205 =cut
206
207 our %_IMG_CACHE;
208 our $HOME;
209 our ($self, $old, $new);
210 our ($x, $y, $w, $h);
211
212 # enforce at least this interval between updates
213 our $MIN_INTERVAL = 6/59.951;
214
215 {
216 package urxvt::bgdsl; # background language
217
218 use List::Util qw(min max sum shuffle);
219
220 =head2 PROVIDERS/GENERATORS
221
222 These functions provide an image, by loading it from disk, grabbing it
223 from the root screen or by simply generating it. They are used as starting
224 points to get an image you can play with.
225
226 =over 4
227
228 =item load $path
229
230 Loads the image at the given C<$path>. The image is set to plane tiling
231 mode.
232
233 Loaded images will be cached for one cycle, and shared between temrinals
234 running in the same process (e.g. in C<urxvtd>).
235
236 =item load_uc $path
237
238 Load uncached - same as load, but does not cache the image. This function
239 is most useufl if you want to optimise a background expression in some
240 way.
241
242 =cut
243
244 sub load_uc($) {
245 my ($path) = @_;
246
247 $_IMG_CACHE{$path} || do {
248 my $img = $self->new_img_from_file ($path);
249 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img);
250 $img
251 }
252 }
253
254 sub load($) {
255 my ($path) = @_;
256
257 $new->{load}{$path} = $old->{load}{$path} || load_uc $path;
258 }
259
260 =item root
261
262 Returns the root window pixmap, that is, hopefully, the background image
263 of your screen. The image is set to extend mode.
264
265 This function makes your expression root sensitive, that means it will be
266 reevaluated when the bg image changes.
267
268 =cut
269
270 sub root() {
271 $new->{again}{rootpmap} = 1;
272 $self->new_img_from_root
273 }
274
275 =item solid $colour
276
277 =item solid $width, $height, $colour
278
279 Creates a new image and completely fills it with the given colour. The
280 image is set to tiling mode.
281
282 If C<$width> and C<$height> are omitted, it creates a 1x1 image, which is
283 useful for solid backgrounds or for use in filtering effects.
284
285 =cut
286
287 sub solid($;$$) {
288 my $colour = pop;
289
290 my $img = $self->new_img (urxvt::PictStandardARGB32, 0, 0, $_[0] || 1, $_[1] || 1);
291 $img->fill ($colour);
292 $img
293 }
294
295 =item clone $img
296
297 Returns an exact copy of the image. This is useful if you want to have
298 multiple copies of the same image to apply different effects to.
299
300 =cut
301
302 sub clone($) {
303 $_[0]->clone
304 }
305
306 =item merge $img ...
307
308 Takes any number of images and merges them together, creating a single
309 image containing them all.
310
311 This function is called automatically when an expression returns multiple
312 images.
313
314 =cut
315
316 sub merge(@) {
317 return $_[0] unless $#_;
318
319 # rather annoyingly clumsy, but optimisation is for another time
320
321 my $x0 = +1e9;
322 my $y0 = +1e9;
323 my $x1 = -1e9;
324 my $y1 = -1e9;
325
326 for (@_) {
327 my ($x, $y, $w, $h) = $_->geometry;
328
329 $x0 = $x if $x0 > $x;
330 $y0 = $y if $y0 > $y;
331
332 $x += $w;
333 $y += $h;
334
335 $x1 = $x if $x1 < $x;
336 $y1 = $y if $y1 < $y;
337 }
338
339 my $base = $self->new_img (urxvt::PictStandardARGB32, $x0, $y0, $x1 - $x0, $y1 - $y0);
340 $base->fill ([0, 0, 0, 0]);
341
342 $base->draw ($_)
343 for @_;
344
345 $base
346 }
347
348 =head2 TILING MODES
349
350 The following operators modify the tiling mode of an image, that is, the
351 way that pixels outside the image area are painted when the image is used.
352
353 =over 4
354
355 =item tile $img
356
357 Tiles the whole plane with the image and returns this new image - or in
358 other words, it returns a copy of the image in plane tiling mode.
359
360 Example: load an image and tile it over the background, without
361 resizing. The C<tile> call is superfluous because C<load> already defaults
362 to tiling mode.
363
364 tile load "mybg.png"
365
366 =item mirror $img
367
368 Similar to tile, but reflects the image each time it uses a new copy, so
369 that top edges always touch top edges, right edges always touch right
370 edges and so on (with normal tiling, left edges always touch right edges
371 and top always touch bottom edges).
372
373 Example: load an image and mirror it over the background, avoiding sharp
374 edges at the image borders at the expense of mirroring the image itself
375
376 mirror load "mybg.png"
377
378 =item pad $img
379
380 Takes an image and modifies it so that all pixels outside the image area
381 become transparent. This mode is most useful when you want to place an
382 image over another image or the background colour while leaving all
383 background pixels outside the image unchanged.
384
385 Example: load an image and display it in the upper left corner. The rest
386 of the space is left "empty" (transparent or whatever your compositor does
387 in alpha mode, else background colour).
388
389 pad load "mybg.png"
390
391 =item extend $img
392
393 Extends the image over the whole plane, using the closest pixel in the
394 area outside the image. This mode is mostly useful when you use more complex
395 filtering operations and want the pixels outside the image to have the
396 same values as the pixels near the edge.
397
398 Example: just for curiosity, how does this pixel extension stuff work?
399
400 extend move 50, 50, load "mybg.png"
401
402 =cut
403
404 sub pad($) {
405 my $img = $_[0]->clone;
406 $img->repeat_mode (urxvt::RepeatNone);
407 $img
408 }
409
410 sub tile($) {
411 my $img = $_[0]->clone;
412 $img->repeat_mode (urxvt::RepeatNormal);
413 $img
414 }
415
416 sub mirror($) {
417 my $img = $_[0]->clone;
418 $img->repeat_mode (urxvt::RepeatReflect);
419 $img
420 }
421
422 sub extend($) {
423 my $img = $_[0]->clone;
424 $img->repeat_mode (urxvt::RepeatPad);
425 $img
426 }
427
428 =back
429
430 =head2 VARIABLE VALUES
431
432 The following functions provide variable data such as the terminal window
433 dimensions. They are not (Perl-) variables, they just return stuff that
434 varies. Most of them make your expression sensitive to some events, for
435 example using C<TW> (terminal width) means your expression is evaluated
436 again when the terminal is resized.
437
438 =over 4
439
440 =item TX
441
442 =item TY
443
444 Return the X and Y coordinates of the terminal window (the terminal
445 window is the full window by default, and the character area only when in
446 border-respect mode).
447
448 Using these functions make your expression sensitive to window moves.
449
450 These functions are mainly useful to align images to the root window.
451
452 Example: load an image and align it so it looks as if anchored to the
453 background.
454
455 move -TX, -TY, load "mybg.png"
456
457 =item TW
458
459 Return the width (C<TW>) and height (C<TH>) of the terminal window (the
460 terminal window is the full window by default, and the character area only
461 when in border-respect mode).
462
463 Using these functions make your expression sensitive to window resizes.
464
465 These functions are mainly useful to scale images, or to clip images to
466 the window size to conserve memory.
467
468 Example: take the screen background, clip it to the window size, blur it a
469 bit, align it to the window position and use it as background.
470
471 clip move -TX, -TY, once { blur 5, root }
472
473 =cut
474
475 sub TX() { $new->{again}{position} = 1; $x }
476 sub TY() { $new->{again}{position} = 1; $y }
477 sub TW() { $new->{again}{size} = 1; $w }
478 sub TH() { $new->{again}{size} = 1; $h }
479
480 =item now
481
482 Returns the current time as (fractional) seconds since the epoch.
483
484 Using this expression does I<not> make your expression sensitive to time,
485 but the next two functions do.
486
487 =item again $seconds
488
489 When this function is used the expression will be reevaluated again in
490 C<$seconds> seconds.
491
492 Example: load some image and rotate it according to the time of day (as if it were
493 the hour pointer of a clock). Update this image every minute.
494
495 again 60; rotate 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png"
496
497 =item counter $seconds
498
499 Like C<again>, but also returns an increasing counter value, starting at
500 0, which might be useful for some simple animation effects.
501
502 =cut
503
504 sub now() { urxvt::NOW }
505
506 sub again($) {
507 $new->{again}{time} = $_[0];
508 }
509
510 sub counter($) {
511 $new->{again}{time} = $_[0];
512 $self->{counter} + 0
513 }
514
515 =back
516
517 =head2 SHAPE CHANGING OPERATORS
518
519 The following operators modify the shape, size or position of the image.
520
521 =over 4
522
523 =item clip $img
524
525 =item clip $width, $height, $img
526
527 =item clip $x, $y, $width, $height, $img
528
529 Clips an image to the given rectangle. If the rectangle is outside the
530 image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is
531 larger than the image, then the tiling mode defines how the extra pixels
532 will be filled.
533
534 If C<$x> an C<$y> are missing, then C<0> is assumed for both.
535
536 If C<$width> and C<$height> are missing, then the window size will be
537 assumed.
538
539 Example: load an image, blur it, and clip it to the window size to save
540 memory.
541
542 clip blur 10, load "mybg.png"
543
544 =cut
545
546 sub clip($;$$;$$) {
547 my $img = pop;
548 my $h = pop || TH;
549 my $w = pop || TW;
550 $img->sub_rect ($_[0], $_[1], $w, $h)
551 }
552
553 =item scale $img
554
555 =item scale $size_factor, $img
556
557 =item scale $width_factor, $height_factor, $img
558
559 Scales the image by the given factors in horizontal
560 (C<$width>) and vertical (C<$height>) direction.
561
562 If only one factor is give, it is used for both directions.
563
564 If no factors are given, scales the image to the window size without
565 keeping aspect.
566
567 =item resize $width, $height, $img
568
569 Resizes the image to exactly C<$width> times C<$height> pixels.
570
571 =item fit $img
572
573 =item fit $width, $height, $img
574
575 Fits the image into the given C<$width> and C<$height> without changing
576 aspect, or the terminal size. That means it will be shrunk or grown until
577 the whole image fits into the given area, possibly leaving borders.
578
579 =item cover $img
580
581 =item cover $width, $height, $img
582
583 Similar to C<fit>, but shrinks or grows until all of the area is covered
584 by the image, so instead of potentially leaving borders, it will cut off
585 image data that doesn't fit.
586
587 =cut
588
589 sub scale($;$;$) {
590 my $img = pop;
591
592 @_ == 2 ? $img->scale ($_[0] * $img->w, $_[1] * $img->h)
593 : @_ ? $img->scale ($_[0] * $img->w, $_[0] * $img->h)
594 : $img->scale (TW, TH)
595 }
596
597 sub resize($$$) {
598 my $img = pop;
599 $img->scale ($_[0], $_[1])
600 }
601
602 sub fit($;$$) {
603 my $img = pop;
604 my $w = ($_[0] || TW) / $img->w;
605 my $h = ($_[1] || TH) / $img->h;
606 scale +(min $w, $h), $img
607 }
608
609 sub cover($;$$) {
610 my $img = pop;
611 my $w = ($_[0] || TW) / $img->w;
612 my $h = ($_[1] || TH) / $img->h;
613 scale +(max $w, $h), $img
614 }
615
616 =item move $dx, $dy, $img
617
618 Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in
619 the vertical.
620
621 Example: move the image right by 20 pixels and down by 30.
622
623 move 20, 30, ...
624
625 =item align $xalign, $yalign, $img
626
627 Aligns the image according to a factor - C<0> means the image is moved to
628 the left or top edge (for C<$xalign> or C<$yalign>), C<0.5> means it is
629 exactly centered and C<1> means it touches the right or bottom edge.
630
631 Example: remove any visible border around an image, center it vertically but move
632 it to the right hand side.
633
634 align 1, 0.5, pad $img
635
636 =item center $img
637
638 =item center $width, $height, $img
639
640 Centers the image, i.e. the center of the image is moved to the center of
641 the terminal window (or the box specified by C<$width> and C<$height> if
642 given).
643
644 Example: load an image and center it.
645
646 center pad load "mybg.png"
647
648 =item rootalign $img
649
650 Moves the image so that it appears glued to the screen as opposed to the
651 window. This gives the illusion of a larger area behind the window. It is
652 exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the
653 top left of the screen.
654
655 Example: load a background image, put it in mirror mode and root align it.
656
657 rootalign mirror load "mybg.png"
658
659 Example: take the screen background and align it, giving the illusion of
660 transparency as long as the window isn't in front of other windows.
661
662 rootalign root
663
664 =cut
665
666 sub move($$;$) {
667 my $img = pop->clone;
668 $img->move ($_[0], $_[1]);
669 $img
670 }
671
672 sub align($;$$) {
673 my $img = pop;
674
675 move $_[0] * (TW - $img->w),
676 $_[1] * (TH - $img->h),
677 $img
678 }
679
680 sub center($;$$) {
681 my $img = pop;
682 my $w = $_[0] || TW;
683 my $h = $_[1] || TH;
684
685 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img
686 }
687
688 sub rootalign($) {
689 move -TX, -TY, $_[0]
690 }
691
692 =item rotate $center_x, $center_y, $degrees
693
694 Rotates the image by C<$degrees> degrees, counter-clockwise, around the
695 pointer at C<$center_x> and C<$center_y> (specified as factor of image
696 width/height).
697
698 #TODO# new width, height, maybe more operators?
699
700 Example: rotate the image by 90 degrees
701
702 =cut
703
704 sub rotate($$$$) {
705 my $img = pop;
706 $img->rotate (
707 $_[0] * ($img->w + $img->x),
708 $_[1] * ($img->h + $img->y),
709 $_[2] * (3.14159265 / 180),
710 )
711 }
712
713 =back
714
715 =head2 COLOUR MODIFICATIONS
716
717 The following operators change the pixels of the image.
718
719 =over 4
720
721 =item contrast $factor, $img
722
723 =item contrast $r, $g, $b, $img
724
725 =item contrast $r, $g, $b, $a, $img
726
727 Adjusts the I<contrast> of an image.
728
729 The first form applies a single C<$factor> to red, green and blue, the
730 second form applies separate factors to each colour channel, and the last
731 form includes the alpha channel.
732
733 Values from 0 to 1 lower the contrast, values higher than 1 increase the
734 contrast.
735
736 Due to limitations in the underlying XRender extension, lowering contrast
737 also reduces brightness, while increasing contrast currently also
738 increases brightness.
739
740 =item brightness $bias, $img
741
742 =item brightness $r, $g, $b, $img
743
744 =item brightness $r, $g, $b, $a, $img
745
746 Adjusts the brightness of an image.
747
748 The first form applies a single C<$bias> to red, green and blue, the
749 second form applies separate biases to each colour channel, and the last
750 form includes the alpha channel.
751
752 Values less than 0 reduce brightness, while values larger than 0 increase
753 it. Useful range is from -1 to 1 - the former results in a black, the
754 latter in a white picture.
755
756 Due to idiosyncrasies in the underlying XRender extension, biases less
757 than zero can be I<very> slow.
758
759 =cut
760
761 sub contrast($$;$$;$) {
762 my $img = pop;
763 my ($r, $g, $b, $a) = @_;
764
765 ($g, $b) = ($r, $r) if @_ < 3;
766 $a = 1 if @_ < 4;
767
768 $img = $img->clone;
769 $img->contrast ($r, $g, $b, $a);
770 $img
771 }
772
773 sub brightness($$;$$;$) {
774 my $img = pop;
775 my ($r, $g, $b, $a) = @_;
776
777 ($g, $b) = ($r, $r) if @_ < 3;
778 $a = 1 if @_ < 4;
779
780 $img = $img->clone;
781 $img->brightness ($r, $g, $b, $a);
782 $img
783 }
784
785 =item blur $radius, $img
786
787 =item blur $radius_horz, $radius_vert, $img
788
789 Gaussian-blurs the image with (roughly) C<$radius> pixel radius. The radii
790 can also be specified separately.
791
792 Blurring is often I<very> slow, at least compared or other
793 operators. Larger blur radii are slower than smaller ones, too, so if you
794 don't want to freeze your screen for long times, start experimenting with
795 low values for radius (<5).
796
797 =cut
798
799 sub blur($$;$) {
800 my $img = pop;
801 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0])
802 }
803
804 =back
805
806 =head2 OTHER STUFF
807
808 Anything that didn't fit any of the other categories, even after applying
809 force and closing our eyes.
810
811 =over 4
812
813 =item once { ... }
814
815 This function takes a code block as argument, that is, one or more
816 statements enclosed by braces.
817
818 The trick is that this code block is only evaluated once - future calls
819 will simply return the original image (yes, it should only be used with
820 images).
821
822 This can be extremely useful to avoid redoign the same slow operations
823 again and again- for example, if your background expression takes the root
824 background, blurs it and then root-aligns it it would have to blur the
825 root background on every window move or resize.
826
827 Putting the blur into a C<once> block will make sure the blur is only done
828 once:
829
830 rootlign once { blur 10, root }
831
832 This leaves the question of how to force reevaluation of the block, in
833 case the root background changes: Right now, all once blocks forget that
834 they ahve been executed before each time the root background changes (if
835 the expression is sensitive to that) or when C<once_again> is called.
836
837 =item once_again
838
839 Resets all C<once> block as if they had never been called, i.e. on the
840 next call they will be reevaluated again.
841
842 =cut
843
844 sub once(&) {
845 my $once = $self->{once_cache}{$_[0]+0} ||= do {
846 local $new->{again};
847 my @res = $_[0]();
848 [$new->{again}, \@res]
849 };
850
851 $new->{again} = {
852 %{ $new->{again} },
853 %{ $once->[0] }
854 };
855
856 # in scalar context we always return the first original result, which
857 # is not quite how perl works.
858 wantarray
859 ? @{ $once->[1] }
860 : $once->[1][0]
861 }
862
863 sub once_again() {
864 delete $self->{once_cache};
865 }
866
867 =back
868
869 =cut
870
871 }
872
873 sub parse_expr {
874 my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}";
875 die if $@;
876 $expr
877 }
878
879 # compiles a parsed expression
880 sub set_expr {
881 my ($self, $expr) = @_;
882
883 $self->{expr} = $expr;
884 $self->recalculate;
885 }
886
887 # evaluate the current bg expression
888 sub recalculate {
889 my ($arg_self) = @_;
890
891 # rate limit evaluation
892
893 if ($arg_self->{next_refresh} > urxvt::NOW) {
894 $arg_self->{next_refresh_timer} = urxvt::timer->new->after ($arg_self->{next_refresh} - urxvt::NOW)->cb (sub {
895 $arg_self->recalculate;
896 });
897 return;
898 }
899
900 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
901
902 # set environment to evaluate user expression
903
904 local $self = $arg_self;
905
906 local $HOME = $ENV{HOME};
907 local $old = $self->{state};
908 local $new = my $state = $self->{state} = {};
909
910 ($x, $y, $w, $h) =
911 $self->background_geometry ($self->{border});
912
913 # evaluate user expression
914
915 my $img = eval { urxvt::bgdsl::merge $self->{expr}->() };
916 die $@ if $@;
917 die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img";
918
919 # if the expression is sensitive to external events, prepare reevaluation then
920
921 my $again = delete $state->{again};
922
923 $again->{size} = 1
924 if $img->repeat_mode != urxvt::RepeatNormal;
925
926 if (my $again = $again->{time}) {
927 my $self = $self;
928 $state->{timer} = $again == $old->{again}
929 ? $old->{timer}
930 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
931 ++$self->{counter};
932 $self->recalculate
933 });
934 }
935
936 if ($again->{position}) {
937 $self->enable (position_change => sub { $_[0]->recalculate });
938 } else {
939 $self->disable ("position_change");
940 }
941
942 if ($again->{size}) {
943 $self->enable (size_change => sub { $_[0]->recalculate });
944 } else {
945 $self->disable ("size_change");
946 }
947
948 if ($again->{rootpmap}) {
949 $self->enable (rootpmap_change => sub {
950 delete $_[0]{once_cache}; # this will override once-block values from
951 $_[0]->recalculate;
952 });
953 } else {
954 $self->disable ("rootpmap_change");
955 }
956
957 # clear stuff we no longer need
958
959 %$old = ();
960
961 unless (%$again) {
962 delete $self->{state};
963 delete $self->{expr};
964 }
965
966 # set background pixmap
967
968 $self->set_background ($img, $self->{border});
969 $self->scr_recolour (0);
970 $self->want_refresh;
971 }
972
973 sub on_start {
974 my ($self) = @_;
975
976 my $expr = $self->x_resource ("%.expr")
977 or return;
978
979 $self->has_render
980 or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n";
981
982 $self->set_expr (parse_expr $expr);
983 $self->{border} = $self->x_resource_boolean ("%.border");
984
985 $MIN_INTERVAL = $self->x_resource ("%.interval");
986
987 ()
988 }
989