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.23 by root, Thu Jun 7 10:22:55 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:%.enable:boolean:some boolean 4#:META:X_RESOURCE:%.border:boolean:respect the terminal border
5#:META:X_RESOURCE:%.extra.:value:extra config 5#:META:X_RESOURCE:%.interval:seconds:minimum time between updates
6 6
7our $EXPR = 'clip move -X, -Y, load "MagnoliaAlpha.png"'; 7=head1 NAME
8#$EXPR = '
9# rotate W, H, 50, 50, counter 1/59.95, repeat_mirror,
10# clip X, Y, W, H, repeat_mirror,
11# load "/root/pix/das_fette_schwein.jpg"
12#';
13#$EXPR = 'solid "red"';
14#$EXPR = 'blur root, 10, 10'
15#$EXPR = 'blur move (root, -x, -y), 5, 5'
16#resize load "/root/pix/das_fette_schwein.jpg", w, h
17 8
18use Safe; 9 background - manage terminal background
19 10
20our ($bgdsl_self, $old, $new); 11=head1 SYNOPSIS
12
13 urxvt --background-expr 'background expression'
14 --background-border
15 --background-interval seconds
16
17=head1 DESCRIPTION
18
19This extension manages the terminal background by creating a picture that
20is behind the text, replacing the normal background colour.
21
22It does so by evaluating a Perl expression that I<calculates> the image on
23the fly, for example, by grabbing the root background or loading a file.
24
25While the full power of Perl is available, the operators have been design
26to be as simple as possible.
27
28For example, to load an image and scale it to the window size, you would
29use:
30
31 urxvt --background-expr 'scale load "/path/to/mybg.png"'
32
33Or specified as a X resource:
34
35 URxvt.background-expr: scale load "/path/to/mybg.png"
36
37=head1 THEORY OF OPERATION
38
39At startup, just before the window is mapped for the first time, the
40expression is evaluated and must yield an image. The image is then
41extended as necessary to cover the whole terminal window, and is set as a
42background pixmap.
43
44If the image contains an alpha channel, then it will be used as-is in
45visuals that support alpha channels (for example, for a compositing
46manager). In other visuals, the terminal background colour will be used to
47replace any transparency.
48
49When the expression relies, directly or indirectly, on the window size,
50position, the root pixmap, or a timer, then it will be remembered. If not,
51then it will be removed.
52
53If any of the parameters that the expression relies on changes (when the
54window is moved or resized, its position or size changes; when the root
55pixmap is replaced by another one the root background changes; or when the
56timer elapses), then the expression will be evaluated again.
57
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
60be reevaluated each time it is changed, but not when it moves for
61example. That ensures that the picture always fills the terminal, even
62after its size changes.
63
64=head2 EXPRESSIONS
65
66Expressions are normal Perl expressions, in fact, they are Perl blocks -
67which 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
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.
78
79Fortunately, we expect that most expressions will be much simpler, with
80little Perl knowledge needed.
81
82Basically, you always start with a function that "generates" an image
83object, such as C<load>, which loads an image from disk, or C<root>, which
84returns the root window background image:
85
86 load "$HOME/mypic.png"
87
88The path is usually specified as a quoted string (the exact rules can be
89found in the L<perlop> manpage). The F<$HOME> at the beginning of the
90string is expanded to the home directory.
91
92Then you prepend one or more modifiers or filtering expressions, such as
93C<scale>:
94
95 scale load "$HOME/mypic.png"
96
97Just like a mathematical expression with functions, you should read these
98expressions from right to left, as the C<load> is evaluated first, and
99its result becomes the argument to the C<scale> function.
100
101Many operators also allow some parameters preceding the input image
102that modify its behaviour. For example, C<scale> without any additional
103arguments scales the image to size of the terminal window. If you specify
104an additional argument, it uses it as a scale factor (multiply by 100 to
105get a percentage):
106
107 scale 2, load "$HOME/mypic.png"
108
109This enlarges the image by a factor of 2 (200%). As you can see, C<scale>
110has now two arguments, the C<200> and the C<load> expression, while
111C<load> only has one argument. Arguments are separated from each other by
112commas.
113
114Scale also accepts two arguments, which are then separate factors for both
115horizontal and vertical dimensions. For example, this halves the image
116width and doubles the image height:
117
118 scale 0.5, 2, load "$HOME/mypic.png"
119
120Other effects than scaling are also readily available, for example, you can
121tile the image to fill the whole window, instead of resizing it:
122
123 tile load "$HOME/mypic.png"
124
125In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator
126is kind of superfluous.
127
128Another common effect is to mirror the image, so that the same edges touch:
129
130 mirror load "$HOME/mypic.png"
131
132This is also a typical background expression:
133
134 rootalign root
135
136It first takes a snapshot of the screen background image, and then
137moves it to the upper left corner of the screen - the result is
138pseudo-transparency, as the image seems to be static while the window is
139moved around.
140
141=head2 CYCLES AND CACHING
142
143=head3 C<load> et al.
144
145As has been mentioned before, the expression might be evaluated multiple
146times. Each time the expression is reevaluated, a new cycle is said to
147have begun. Many operators cache their results till the next cycle.
148
149For example, the C<load> operator keeps a copy of the image. If it is
150asked to load the same image on the next cycle it will not load it again,
151but return the cached copy.
152
153This only works for one cycle though, so as long as you load the same
154image every time, it will always be cached, but when you load a different
155image, it will forget about the first one.
156
157This allows you to either speed things up by keeping multiple images in
158memory, or conserve memory by loading images more often.
159
160For example, you can keep two images in memory and use a random one like
161this:
162
163 my $img1 = load "img1.png";
164 my $img2 = load "img2.png";
165 (0.5 > rand) ? $img1 : $img2
166
167Since both images are "loaded" every time the expression is evaluated,
168they are always kept in memory. Contrast this version:
169
170 my $path1 = "img1.png";
171 my $path2 = "img2.png";
172 load ((0.5 > rand) ? $path1 : $path2)
173
174Here, a path is selected randomly, and load is only called for one image,
175so keeps only one image in memory. If, on the next evaluation, luck
176decides to use the other path, then it will have to load that image again.
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
195=head1 REFERENCE
196
197=head2 COMMAND LINE SWITCHES
198
199=over 4
200
201=item --background-expr perl-expression
202
203Specifies the Perl expression to evaluate.
204
205=item --background-border
206
207By default, the expression creates an image that fills the full window,
208overwriting borders and any other areas, such as the scrollbar.
209
210Specifying this flag changes the behaviour, so that the image only
211replaces the background of the character area.
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
222=back
223
224=cut
225
226our %_IMG_CACHE;
227our $HOME;
228our ($self, $frame);
21our ($l, $t, $w, $h); 229our ($x, $y, $w, $h);
22 230
23# enforce at least this interval between updates 231# enforce at least this interval between updates
24our $MIN_INTERVAL = 1/100; 232our $MIN_INTERVAL = 6/59.951;
25 233
26{ 234{
27 package urxvt::bgdsl; # background language 235 package urxvt::bgdsl; # background language
28 236
29# *repeat_empty = \&urxvt::RepeatNone; 237 sub FR_PARENT() { 0 } # parent frame, if any - must be #0
30# *repeat_tile = \&urxvt::RepeatNormal; 238 sub FR_CACHE () { 1 } # cached values
31# *repeat_pad = \&urxvt::RepeatPad; 239 sub FR_AGAIN () { 2 } # what this expr is sensitive to
32# *repeat_mirror = \&urxvt::RepeatReflect; 240 sub FR_STATE () { 3 } # watchers etc.
241
242 use List::Util qw(min max sum shuffle);
33 243
34=head2 PROVIDERS/GENERATORS 244=head2 PROVIDERS/GENERATORS
35 245
246These functions provide an image, by loading it from disk, grabbing it
247from the root screen or by simply generating it. They are used as starting
248points to get an image you can play with.
249
36=over 4 250=over 4
37 251
38=item load $path 252=item load $path
253
254Loads the image at the given C<$path>. The image is set to plane tiling
255mode.
256
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.
39 265
40=cut 266=cut
41 267
42 sub load($) { 268 sub load($) {
43 my ($path) = @_; 269 my ($path) = @_;
44 270
45 $new->{load}{$path} = $old->{load}{$path} || $bgdsl_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 }
46 } 276 }
277
278=item root
279
280Returns the root window pixmap, that is, hopefully, the background image
281of your screen.
282
283This function makes your expression root sensitive, that means it will be
284reevaluated when the bg image changes.
285
286=cut
47 287
48 sub root() { 288 sub root() {
49 $new->{rootpmap_sensitive} = 1; 289 $frame->[FR_AGAIN]{rootpmap} = 1;
50 die "root op not supported, exg, we need you"; 290 $self->new_img_from_root
51 } 291 }
292
293=item solid $colour
294
295=item solid $width, $height, $colour
296
297Creates a new image and completely fills it with the given colour. The
298image is set to tiling mode.
299
300If C<$width> and C<$height> are omitted, it creates a 1x1 image, which is
301useful for solid backgrounds or for use in filtering effects.
302
303=cut
52 304
53 sub solid($;$$) { 305 sub solid($;$$) {
306 my $colour = pop;
307
54 my $img = $bgdsl_self->new_img (urxvt::PictStandardARGB32, $_[1] || 1, $_[2] || 1); 308 my $img = $self->new_img (urxvt::PictStandardARGB32, 0, 0, $_[0] || 1, $_[1] || 1);
55 $img->fill ($_[0]); 309 $img->fill ($colour);
56 $img 310 $img
57 } 311 }
58 312
313=item clone $img
314
315Returns an exact copy of the image. This is useful if you want to have
316multiple copies of the same image to apply different effects to.
317
318=cut
319
320 sub clone($) {
321 $_[0]->clone
322 }
323
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 }
367
368=head2 TILING MODES
369
370The following operators modify the tiling mode of an image, that is, the
371way that pixels outside the image area are painted when the image is used.
372
373=over 4
374
375=item tile $img
376
377Tiles the whole plane with the image and returns this new image - or in
378other words, it returns a copy of the image in plane tiling mode.
379
380Example: load an image and tile it over the background, without
381resizing. The C<tile> call is superfluous because C<load> already defaults
382to tiling mode.
383
384 tile load "mybg.png"
385
386=item mirror $img
387
388Similar to tile, but reflects the image each time it uses a new copy, so
389that top edges always touch top edges, right edges always touch right
390edges and so on (with normal tiling, left edges always touch right edges
391and top always touch bottom edges).
392
393Example: load an image and mirror it over the background, avoiding sharp
394edges at the image borders at the expense of mirroring the image itself
395
396 mirror load "mybg.png"
397
398=item pad $img
399
400Takes an image and modifies it so that all pixels outside the image area
401become transparent. This mode is most useful when you want to place an
402image over another image or the background colour while leaving all
403background pixels outside the image unchanged.
404
405Example: load an image and display it in the upper left corner. The rest
406of the space is left "empty" (transparent or whatever your compositor does
407in alpha mode, else background colour).
408
409 pad load "mybg.png"
410
411=item extend $img
412
413Extends the image over the whole plane, using the closest pixel in the
414area outside the image. This mode is mostly useful when you use more complex
415filtering operations and want the pixels outside the image to have the
416same values as the pixels near the edge.
417
418Example: just for curiosity, how does this pixel extension stuff work?
419
420 extend move 50, 50, load "mybg.png"
421
422=cut
423
424 sub pad($) {
425 my $img = $_[0]->clone;
426 $img->repeat_mode (urxvt::RepeatNone);
427 $img
428 }
429
430 sub tile($) {
431 my $img = $_[0]->clone;
432 $img->repeat_mode (urxvt::RepeatNormal);
433 $img
434 }
435
436 sub mirror($) {
437 my $img = $_[0]->clone;
438 $img->repeat_mode (urxvt::RepeatReflect);
439 $img
440 }
441
442 sub extend($) {
443 my $img = $_[0]->clone;
444 $img->repeat_mode (urxvt::RepeatPad);
445 $img
446 }
447
59=back 448=back
60 449
61=head2 VARIABLES 450=head2 VARIABLE VALUES
451
452The following functions provide variable data such as the terminal window
453dimensions. They are not (Perl-) variables, they just return stuff that
454varies. Most of them make your expression sensitive to some events, for
455example using C<TW> (terminal width) means your expression is evaluated
456again when the terminal is resized.
62 457
63=over 4 458=over 4
64 459
65=cut 460=item TX
66 461
67 sub X() { $new->{position_sensitive} = 1; $l } 462=item TY
68 sub Y() { $new->{position_sensitive} = 1; $t } 463
69 sub W() { $new->{size_sensitive} = 1; $w } 464Return the X and Y coordinates of the terminal window (the terminal
70 sub H() { $new->{size_sensitive} = 1; $h } 465window is the full window by default, and the character area only when in
466border-respect mode).
467
468Using these functions make your expression sensitive to window moves.
469
470These functions are mainly useful to align images to the root window.
471
472Example: load an image and align it so it looks as if anchored to the
473background.
474
475 move -TX, -TY, load "mybg.png"
476
477=item TW
478
479Return the width (C<TW>) and height (C<TH>) of the terminal window (the
480terminal window is the full window by default, and the character area only
481when in border-respect mode).
482
483Using these functions make your expression sensitive to window resizes.
484
485These functions are mainly useful to scale images, or to clip images to
486the window size to conserve memory.
487
488Example: take the screen background, clip it to the window size, blur it a
489bit, align it to the window position and use it as background.
490
491 clip move -TX, -TY, once { blur 5, root }
492
493=cut
494
495 sub TX() { $frame->[FR_AGAIN]{position} = 1; $x }
496 sub TY() { $frame->[FR_AGAIN]{position} = 1; $y }
497 sub TW() { $frame->[FR_AGAIN]{size} = 1; $w }
498 sub TH() { $frame->[FR_AGAIN]{size} = 1; $h }
499
500=item now
501
502Returns the current time as (fractional) seconds since the epoch.
503
504Using this expression does I<not> make your expression sensitive to time,
505but the next two functions do.
506
507=item again $seconds
508
509When this function is used the expression will be reevaluated again in
510C<$seconds> seconds.
511
512Example: load some image and rotate it according to the time of day (as if it were
513the hour pointer of a clock). Update this image every minute.
514
515 again 60; rotate 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png"
516
517=item counter $seconds
518
519Like C<again>, but also returns an increasing counter value, starting at
5200, which might be useful for some simple animation effects.
521
522=cut
71 523
72 sub now() { urxvt::NOW } 524 sub now() { urxvt::NOW }
73 525
74 sub again($) { 526 sub again($) {
75 $new->{again} = $_[0]; 527 $frame->[FR_AGAIN]{time} = $_[0];
76 } 528 }
77 529
78 sub counter($) { 530 sub counter($) {
79 $new->{again} = $_[0]; 531 $frame->[FR_AGAIN]{time} = $_[0];
80 $bgdsl_self->{counter} + 0 532 $frame->[FR_STATE]{counter} + 0
81 } 533 }
82 534
83=back 535=back
84 536
85=head2 OPERATORS 537=head2 SHAPE CHANGING OPERATORS
538
539The following operators modify the shape, size or position of the image.
86 540
87=over 4 541=over 4
88 542
89=cut 543=item clip $img
90 544
91# sub clone($) { 545=item clip $width, $height, $img
92# $_[0]->clone 546
93# } 547=item clip $x, $y, $width, $height, $img
548
549Clips an image to the given rectangle. If the rectangle is outside the
550image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is
551larger than the image, then the tiling mode defines how the extra pixels
552will be filled.
553
554If C<$x> an C<$y> are missing, then C<0> is assumed for both.
555
556If C<$width> and C<$height> are missing, then the window size will be
557assumed.
558
559Example: load an image, blur it, and clip it to the window size to save
560memory.
561
562 clip blur 10, load "mybg.png"
563
564=cut
94 565
95 sub clip($;$$;$$) { 566 sub clip($;$$;$$) {
96 my $img = pop; 567 my $img = pop;
97 my $h = pop || H; 568 my $h = pop || TH;
98 my $w = pop || W; 569 my $w = pop || TW;
99 $img->sub_rect ($_[0], $_[1], $w, $h) 570 $img->sub_rect ($_[0], $_[1], $w, $h)
571 }
572
573=item scale $img
574
575=item scale $size_factor, $img
576
577=item scale $width_factor, $height_factor, $img
578
579Scales the image by the given factors in horizontal
580(C<$width>) and vertical (C<$height>) direction.
581
582If only one factor is give, it is used for both directions.
583
584If no factors are given, scales the image to the window size without
585keeping aspect.
586
587=item resize $width, $height, $img
588
589Resizes the image to exactly C<$width> times C<$height> pixels.
590
591=item fit $img
592
593=item fit $width, $height, $img
594
595Fits the image into the given C<$width> and C<$height> without changing
596aspect, or the terminal size. That means it will be shrunk or grown until
597the whole image fits into the given area, possibly leaving borders.
598
599=item cover $img
600
601=item cover $width, $height, $img
602
603Similar to C<fit>, but shrinks or grows until all of the area is covered
604by the image, so instead of potentially leaving borders, it will cut off
605image data that doesn't fit.
606
607=cut
608
609 sub scale($;$;$) {
610 my $img = pop;
611
612 @_ == 2 ? $img->scale ($_[0] * $img->w, $_[1] * $img->h)
613 : @_ ? $img->scale ($_[0] * $img->w, $_[0] * $img->h)
614 : $img->scale (TW, TH)
100 } 615 }
101 616
102 sub resize($$$) { 617 sub resize($$$) {
103 my $img = pop; 618 my $img = pop;
104 $img->scale ($_[0], $_[1]) 619 $img->scale ($_[0], $_[1])
105 } 620 }
106 621
107 # TODO: ugly 622 sub fit($;$$) {
623 my $img = pop;
624 my $w = ($_[0] || TW) / $img->w;
625 my $h = ($_[1] || TH) / $img->h;
626 scale +(min $w, $h), $img
627 }
628
629 sub cover($;$$) {
630 my $img = pop;
631 my $w = ($_[0] || TW) / $img->w;
632 my $h = ($_[1] || TH) / $img->h;
633 scale +(max $w, $h), $img
634 }
635
636=item move $dx, $dy, $img
637
638Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in
639the vertical.
640
641Example: move the image right by 20 pixels and down by 30.
642
643 move 20, 30, ...
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
656=item center $img
657
658=item center $width, $height, $img
659
660Centers the image, i.e. the center of the image is moved to the center of
661the terminal window (or the box specified by C<$width> and C<$height> if
662given).
663
664Example: load an image and center it.
665
666 center pad load "mybg.png"
667
668=item rootalign $img
669
670Moves the image so that it appears glued to the screen as opposed to the
671window. This gives the illusion of a larger area behind the window. It is
672exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the
673top left of the screen.
674
675Example: load a background image, put it in mirror mode and root align it.
676
677 rootalign mirror load "mybg.png"
678
679Example: take the screen background and align it, giving the illusion of
680transparency as long as the window isn't in front of other windows.
681
682 rootalign root
683
684=cut
685
108 sub move($$;$) { 686 sub move($$;$) {
109 my $img = pop->clone; 687 my $img = pop->clone;
110 $img->move ($_[0], $_[1]); 688 $img->move ($_[0], $_[1]);
111 $img 689 $img
690 }
691
692 sub align($;$$) {
112# my $img = pop; 693 my $img = pop;
113# $img->sub_rect (
114# $_[0], $_[1],
115# $img->w, $img->h,
116# $_[2],
117# )
118 }
119 694
695 move $_[0] * (TW - $img->w),
696 $_[1] * (TH - $img->h),
697 $img
698 }
699
700 sub center($;$$) {
701 my $img = pop;
702 my $w = $_[0] || TW;
703 my $h = $_[1] || TH;
704
705 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img
706 }
707
708 sub rootalign($) {
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
120 sub rotate($$$$$$) { 724 sub rotate($$$$) {
121 my $img = pop; 725 my $img = pop;
122 $img->rotate ( 726 $img->rotate (
123 $_[0], 727 $_[0] * ($img->w + $img->x),
124 $_[1], 728 $_[1] * ($img->h + $img->y),
125 $_[2] * $img->w * .01,
126 $_[3] * $img->h * .01,
127 $_[4] * (3.14159265 / 180), 729 $_[2] * (3.14159265 / 180),
128 ) 730 )
129 } 731 }
130 732
131 sub blur($$$) { 733=back
132 my ($rh, $rv, $img) = @_;
133 734
134 $img->blur ($rh, $rv); 735=head2 COLOUR MODIFICATIONS
135 } 736
737The following operators change the pixels of the image.
738
739=over 4
740
741=item contrast $factor, $img
742
743=item contrast $r, $g, $b, $img
744
745=item contrast $r, $g, $b, $a, $img
746
747Adjusts the I<contrast> of an image.
748
749The first form applies a single C<$factor> to red, green and blue, the
750second form applies separate factors to each colour channel, and the last
751form includes the alpha channel.
752
753Values from 0 to 1 lower the contrast, values higher than 1 increase the
754contrast.
755
756Due to limitations in the underlying XRender extension, lowering contrast
757also reduces brightness, while increasing contrast currently also
758increases brightness.
759
760=item brightness $bias, $img
761
762=item brightness $r, $g, $b, $img
763
764=item brightness $r, $g, $b, $a, $img
765
766Adjusts the brightness of an image.
767
768The first form applies a single C<$bias> to red, green and blue, the
769second form applies separate biases to each colour channel, and the last
770form includes the alpha channel.
771
772Values less than 0 reduce brightness, while values larger than 0 increase
773it. Useful range is from -1 to 1 - the former results in a black, the
774latter in a white picture.
775
776Due to idiosyncrasies in the underlying XRender extension, biases less
777than zero can be I<very> slow.
778
779=cut
136 780
137 sub contrast($$;$$;$) { 781 sub contrast($$;$$;$) {
138 my $img = pop; 782 my $img = pop;
139 my ($r, $g, $b, $a) = @_; 783 my ($r, $g, $b, $a) = @_;
140 784
141 ($g, $b) = ($r, $r) if @_ < 4; 785 ($g, $b) = ($r, $r) if @_ < 3;
142 $a = 1 if @_ < 5; 786 $a = 1 if @_ < 4;
143 787
144 $img = $img->clone; 788 $img = $img->clone;
145 $img->contrast ($r, $g, $b, $a); 789 $img->contrast ($r, $g, $b, $a);
146 $img 790 $img
147 } 791 }
148 792
149 sub brightness($$;$$;$) { 793 sub brightness($$;$$;$) {
150 my $img = pop; 794 my $img = pop;
151 my ($r, $g, $b, $a) = @_; 795 my ($r, $g, $b, $a) = @_;
152 796
153 ($g, $b) = ($r, $r) if @_ < 4; 797 ($g, $b) = ($r, $r) if @_ < 3;
154 $a = 1 if @_ < 5; 798 $a = 1 if @_ < 4;
155 799
156 $img = $img->clone; 800 $img = $img->clone;
157 $img->brightness ($r, $g, $b, $a); 801 $img->brightness ($r, $g, $b, $a);
158 $img 802 $img
159 } 803 }
160 804
805=item blur $radius, $img
806
807=item blur $radius_horz, $radius_vert, $img
808
809Gaussian-blurs the image with (roughly) C<$radius> pixel radius. The radii
810can also be specified separately.
811
812Blurring is often I<very> slow, at least compared or other
813operators. Larger blur radii are slower than smaller ones, too, so if you
814don't want to freeze your screen for long times, start experimenting with
815low values for radius (<5).
816
817=cut
818
819 sub blur($$;$) {
820 my $img = pop;
821 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0])
822 }
823
161=back 824=back
162 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
163=cut 907=cut
164 908
165} 909}
166 910
167sub parse_expr { 911sub parse_expr {
168 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 . "}";
169 die if $@; 918 die if $@;
170 $expr 919 $expr
171} 920}
172 921
173# compiles a parsed expression 922# compiles a parsed expression
174sub set_expr { 923sub set_expr {
175 my ($self, $expr) = @_; 924 my ($self, $expr) = @_;
176 925
926 $self->{root} = [];
177 $self->{expr} = $expr; 927 $self->{expr} = $expr;
178 $self->recalculate; 928 $self->recalculate;
179} 929}
180 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 }
977}
978
181# evaluate the current bg expression 979# evaluate the current bg expression
182sub recalculate { 980sub recalculate {
183 my ($self) = @_; 981 my ($arg_self) = @_;
184 982
185 # rate limit evaluation 983 # rate limit evaluation
186 984
187 if ($self->{next_refresh} > urxvt::NOW) { 985 if ($arg_self->{next_refresh} > urxvt::NOW) {
188 $self->{next_refresh_timer} = urxvt::timer->new->after ($self->{next_refresh} - urxvt::NOW)->cb (sub { 986 $arg_self->{next_refresh_timer} = urxvt::timer->new->after ($arg_self->{next_refresh} - urxvt::NOW)->cb (sub {
189 $self->recalculate; 987 $arg_self->recalculate;
190 }); 988 });
191 return; 989 return;
192 } 990 }
193 991
194 $self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; 992 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
195 993
196 # set environment to evaluate user expression 994 # set environment to evaluate user expression
197 995
198 local $bgdsl_self = $self; 996 local $self = $arg_self;
997 local $HOME = $ENV{HOME};
998 local $frame = [];
199 999
200 local $old = $self->{state}; 1000 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
201 local $new = my $state = $self->{state} = {};
202
203 ($l, $t, $w, $h) =
204 $self->get_geometry;
205
206 warn "$l,$t,$w,$h\n";#d#
207 1001
208 # evaluate user expression 1002 # evaluate user expression
209 1003
210 my $img = eval { $self->{expr}->() }; 1004 my @img = eval { $self->{expr}->() };
211 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"
212 die if !UNIVERSAL::isa $img, "urxvt::img"; 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;
213 1014
214 # if the expression is sensitive to external events, prepare reevaluation then 1015 # if the expression is sensitive to external events, prepare reevaluation then
215 1016 $self->compile_frame ($frame, sub { $arg_self->recalculate });
216 my $repeat;
217
218 if (my $again = $state->{again}) {
219 $repeat = 1;
220 $state->{timer} = $again == $old->{again}
221 ? $old->{timer}
222 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
223 ++$self->{counter};
224 $self->recalculate
225 });
226 }
227
228 if (delete $state->{position_sensitive}) {
229 $repeat = 1;
230 $self->enable (position_change => sub { $_[0]->recalculate });
231 } else {
232 $self->disable ("position_change");
233 }
234
235 if (delete $state->{size_sensitive}) {
236 $repeat = 1;
237 $self->enable (size_change => sub { $_[0]->recalculate });
238 } else {
239 $self->disable ("size_change");
240 }
241
242 if (delete $state->{rootpmap_sensitive}) {
243 $repeat = 1;
244 $self->enable (rootpmap_change => sub { $_[0]->recalculate });
245 } else {
246 $self->disable ("rootpmap_change");
247 }
248 1017
249 # clear stuff we no longer need 1018 # clear stuff we no longer need
250 1019
251 %$old = (); 1020# unless (%{ $frame->[FR_STATE] }) {
252
253 unless ($repeat) {
254 delete $self->{state}; 1021# delete $self->{state};
255 delete $self->{expr}; 1022# delete $self->{expr};
256 } 1023# }
257 1024
258 # prepare and set background pixmap 1025 # set background pixmap
259 1026
260 $img = $img->sub_rect (0, 0, $w, $h)
261 if $img->w != $w || $img->h != $h;
262
263 $self->set_background ($img); 1027 $self->set_background ($img, $self->{border});
264 $self->scr_recolour (0); 1028 $self->scr_recolour (0);
265 $self->want_refresh; 1029 $self->want_refresh;
266} 1030}
267 1031
268sub on_start { 1032sub on_start {
269 my ($self) = @_; 1033 my ($self) = @_;
270 1034
1035 my $expr = $self->x_resource ("%.expr")
1036 or return;
1037
1038 $self->has_render
1039 or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n";
1040
271 $self->set_expr (parse_expr $EXPR); 1041 $self->set_expr (parse_expr $expr);
1042 $self->{border} = $self->x_resource_boolean ("%.border");
1043
1044 $MIN_INTERVAL = $self->x_resource ("%.interval");
272 1045
273 () 1046 ()
274} 1047}
275 1048

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines