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.28 by root, Thu Jun 7 12:56:27 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; 7=head1 NAME
8#$EXPR = 'move W * 0.1, -H * 0.1, resize W * 0.5, H * 0.5, repeat_none load "opensource.png"';
9$EXPR = 'border; move -X, -Y, load "argb.png"';
10#$EXPR = '
11# rotate W, H, 50, 50, counter 1/59.95, repeat_mirror,
12# clip X, Y, W, H, repeat_mirror,
13# load "/root/pix/das_fette_schwein.jpg"
14#';
15#$EXPR = 'solid "red"';
16#$EXPR = 'blur root, 10, 10'
17#$EXPR = 'blur move (root, -x, -y), 5, 5'
18#resize load "/root/pix/das_fette_schwein.jpg", w, h
19 8
20use Safe; 9 background - manage terminal background
21 10
22our $border; 11=head1 SYNOPSIS
23our ($bgdsl_self, $old, $new); 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);
24our ($l, $t, $w, $h); 229our ($x, $y, $w, $h);
25 230
26# enforce at least this interval between updates 231# enforce at least this interval between updates
27our $MIN_INTERVAL = 1/100; 232our $MIN_INTERVAL = 6/59.951;
28 233
29{ 234{
30 package urxvt::bgdsl; # background language 235 package urxvt::bgdsl; # background language
31 236
32# *repeat_empty = \&urxvt::RepeatNone; 237 sub FR_PARENT() { 0 } # parent frame, if any - must be #0
33# *repeat_tile = \&urxvt::RepeatNormal; 238 sub FR_CACHE () { 1 } # cached values
34# *repeat_pad = \&urxvt::RepeatPad; 239 sub FR_AGAIN () { 2 } # what this expr is sensitive to
35# *repeat_mirror = \&urxvt::RepeatReflect; 240 sub FR_STATE () { 3 } # watchers etc.
241
242 use List::Util qw(min max sum shuffle);
36 243
37=head2 PROVIDERS/GENERATORS 244=head2 PROVIDERS/GENERATORS
38 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
39=over 4 250=over 4
40 251
41=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.
42 265
43=cut 266=cut
44 267
45 sub load($) { 268 sub load($) {
46 my ($path) = @_; 269 my ($path) = @_;
47 270
48 $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 }
49 } 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
50 287
51 sub root() { 288 sub root() {
52 $new->{rootpmap_sensitive} = 1; 289 $frame->[FR_AGAIN]{rootpmap} = 1;
53 die "root op not supported, exg, we need you"; 290 $self->new_img_from_root
54 } 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
55 304
56 sub solid($;$$) { 305 sub solid($;$$) {
306 my $colour = pop;
307
57 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);
58 $img->fill ($_[0]); 309 $img->fill ($colour);
59 $img 310 $img
60 } 311 }
61 312
62=back 313=item clone $img
63 314
64=head2 VARIABLES 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.
65 317
66=over 4
67
68=cut 318=cut
69 319
70 sub X() { $new->{position_sensitive} = 1; $l }
71 sub Y() { $new->{position_sensitive} = 1; $t }
72 sub W() { $new->{size_sensitive} = 1; $w }
73 sub H() { $new->{size_sensitive} = 1; $h }
74
75 sub now() { urxvt::NOW }
76
77 sub again($) {
78 $new->{again} = $_[0];
79 }
80
81 sub counter($) { 320 sub clone($) {
82 $new->{again} = $_[0]; 321 $_[0]->clone
83 $bgdsl_self->{counter} + 0
84 } 322 }
85 323
86=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 }
87 367
88=head2 TILING MODES 368=head2 TILING MODES
89 369
90The 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
91way 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.
94 374
95=item tile $img 375=item tile $img
96 376
97Tiles the whole plane with the image and returns this new image - or in 377Tiles the whole plane with the image and returns this new image - or in
98other words, it returns a copy of the image in plane tiling mode. 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"
99 385
100=item mirror $img 386=item mirror $img
101 387
102Similar to tile, but reflects the image each time it uses a new copy, so 388Similar to tile, but reflects the image each time it uses a new copy, so
103that top edges always touch top edges, right edges always touch right 389that top edges always touch top edges, right edges always touch right
104edges and so on (with normal tiling, left edges always touch right edges 390edges and so on (with normal tiling, left edges always touch right edges
105and top always touch bottom edges). 391and top always touch bottom edges).
106 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
107=item pad $img 398=item pad $img
108 399
109Takes an image and modifies it so that all pixels outside the image area 400Takes an image and modifies it so that all pixels outside the image area
110become 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
111image over another image or the background colour while leaving all 402image over another image or the background colour while leaving all
112background pixels outside the image unchanged. 403background pixels outside the image unchanged.
113 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
114=item extend $img 411=item extend $img
115 412
116Extends 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
117area 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
118filtering operations and want the pixels outside the image to have the 415filtering operations and want the pixels outside the image to have the
119same values as the pixels near the edge. 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"
120 421
121=cut 422=cut
122 423
123 sub pad($) { 424 sub pad($) {
124 my $img = $_[0]->clone; 425 my $img = $_[0]->clone;
144 $img 445 $img
145 } 446 }
146 447
147=back 448=back
148 449
149=head2 PIXEL OPERATORS 450=head2 VARIABLE VALUES
150 451
151The following operators modify the image pixels in various ways. 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.
152 457
153=over 4 458=over 4
154 459
155=item clone $img 460=item TX
156 461
157Returns an exact copy of the image. 462=item TY
158 463
159=cut 464Return the X and Y coordinates of the terminal window (the terminal
465window is the full window by default, and the character area only when in
466border-respect mode).
160 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
523
524 sub now() { urxvt::NOW }
525
526 sub again($) {
527 $frame->[FR_AGAIN]{time} = $_[0];
528 }
529
161 sub clone($) { 530 sub counter($) {
162 $_[0]->clone 531 $frame->[FR_AGAIN]{time} = $_[0];
532 $frame->[FR_STATE]{counter} + 0
163 } 533 }
534
535=back
536
537=head2 SHAPE CHANGING OPERATORS
538
539The following operators modify the shape, size or position of the image.
540
541=over 4
164 542
165=item clip $img 543=item clip $img
166 544
167=item clip $width, $height, $img 545=item clip $width, $height, $img
168 546
185 563
186=cut 564=cut
187 565
188 sub clip($;$$;$$) { 566 sub clip($;$$;$$) {
189 my $img = pop; 567 my $img = pop;
190 my $h = pop || H; 568 my $h = pop || TH;
191 my $w = pop || W; 569 my $w = pop || TW;
192 $img->sub_rect ($_[0], $_[1], $w, $h) 570 $img->sub_rect ($_[0], $_[1], $w, $h)
193 } 571 }
194 572
195=item scale $img 573=item scale $img
196 574
197=item scale $size_percent, $img 575=item scale $size_factor, $img
198 576
199=item scale $width_percent, $height_percent, $img 577=item scale $width_factor, $height_factor, $img
200 578
201Scales the image by the given percentages in horizontal 579Scales the image by the given factors in horizontal
202(C<$width_percent>) and vertical (C<$height_percent>) direction. 580(C<$width>) and vertical (C<$height>) direction.
203 581
204If only one percentage is give, it is used for both directions. 582If only one factor is give, it is used for both directions.
205 583
206If no percentages are given, scales the image to the window size without 584If no factors are given, scales the image to the window size without
207keeping aspect. 585keeping aspect.
208 586
209=item resize $width, $height, $img 587=item resize $width, $height, $img
210 588
211Resizes the image to exactly C<$width> times C<$height> pixels. 589Resizes the image to exactly C<$width> times C<$height> pixels.
212 590
213=cut 591=item fit $img
214 592
215#TODO: maximise, maximise_fill? 593=item fit $width, $height, $img
216 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
217 sub scale($$$) { 609 sub scale($;$;$) {
218 my $img = pop; 610 my $img = pop;
219 611
220 @_ == 2 ? $img->scale ($_[0] * $img->w * 0.01, $_[1] * $img->h * 0.01) 612 @_ == 2 ? $img->scale ($_[0] * $img->w, $_[1] * $img->h)
221 : @_ ? $img->scale ($_[0] * $img->w * 0.01, $_[0] * $img->h * 0.01) 613 : @_ ? $img->scale ($_[0] * $img->w, $_[0] * $img->h)
222 : $img->scale (W, H) 614 : $img->scale (TW, TH)
223 } 615 }
224 616
225 sub resize($$$) { 617 sub resize($$$) {
226 my $img = pop; 618 my $img = pop;
227 $img->scale ($_[0], $_[1]) 619 $img->scale ($_[0], $_[1])
228 } 620 }
229 621
230 # 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
231 sub move($$;$) { 686 sub move($$;$) {
232 my $img = pop->clone; 687 my $img = pop->clone;
233 $img->move ($_[0], $_[1]); 688 $img->move ($_[0], $_[1]);
234 $img 689 $img
690 }
691
692 sub align($;$$) {
235# my $img = pop; 693 my $img = pop;
236# $img->sub_rect (
237# $_[0], $_[1],
238# $img->w, $img->h,
239# $_[2],
240# )
241 }
242 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
243 sub rotate($$$$$$) { 724 sub rotate($$$$) {
244 my $img = pop; 725 my $img = pop;
245 $img->rotate ( 726 $img->rotate (
246 $_[0], 727 $_[0] * ($img->w + $img->x),
247 $_[1], 728 $_[1] * ($img->h + $img->y),
248 $_[2] * $img->w * .01,
249 $_[3] * $img->h * .01,
250 $_[4] * (3.14159265 / 180), 729 $_[2] * (3.14159265 / 180),
251 ) 730 )
252 } 731 }
253 732
254 sub blur($$;$) { 733=back
255 my $img = pop;
256 734
257 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]); 735=head2 COLOUR MODIFICATIONS
258 } 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
259 780
260 sub contrast($$;$$;$) { 781 sub contrast($$;$$;$) {
261 my $img = pop; 782 my $img = pop;
262 my ($r, $g, $b, $a) = @_; 783 my ($r, $g, $b, $a) = @_;
263 784
264 ($g, $b) = ($r, $r) if @_ < 4; 785 ($g, $b) = ($r, $r) if @_ < 3;
265 $a = 1 if @_ < 5; 786 $a = 1 if @_ < 4;
266 787
267 $img = $img->clone; 788 $img = $img->clone;
268 $img->contrast ($r, $g, $b, $a); 789 $img->contrast ($r, $g, $b, $a);
269 $img 790 $img
270 } 791 }
271 792
272 sub brightness($$;$$;$) { 793 sub brightness($$;$$;$) {
273 my $img = pop; 794 my $img = pop;
274 my ($r, $g, $b, $a) = @_; 795 my ($r, $g, $b, $a) = @_;
275 796
276 ($g, $b) = ($r, $r) if @_ < 4; 797 ($g, $b) = ($r, $r) if @_ < 3;
277 $a = 1 if @_ < 5; 798 $a = 1 if @_ < 4;
278 799
279 $img = $img->clone; 800 $img = $img->clone;
280 $img->brightness ($r, $g, $b, $a); 801 $img->brightness ($r, $g, $b, $a);
281 $img 802 $img
282 } 803 }
283 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
284=back 824=back
285 825
286=head2 SETTINGS 826=head2 OTHER STUFF
827
828Anything that didn't fit any of the other categories, even after applying
829force and closing our eyes.
287 830
288=over 4 831=over 4
289 832
290=item border $respect_border=1 833=item once { ... }
291 834
292Sets whether the image should respect the terminal border (argument true 835This function takes a code block as argument, that is, one or more
293or missing), or whether it should fill the whole window (the default). 836statements enclosed by braces.
294 837
295By default, the image will cover the whole toplevel window. If C<border> 838The trick is that this code block is only evaluated once - future calls
296is enabled, then it will only fill the character area and leave a normal 839will simply return the original image (yes, it should only be used with
297border in the background colour around it and behind the scrollbar. 840images).
298 841
299=cut 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.
300 846
301 sub border { 847In fact, urxvt itself encloses the whole expression in some kind of
302 $border = @_ ? $_[0] : 1; 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};
303 } 903 }
304 904
305=back 905=back
306 906
307=cut 907=cut
308 908
309} 909}
310 910
311sub parse_expr { 911sub parse_expr {
312 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 . "}";
313 die if $@; 918 die if $@;
314 $expr 919 $expr
315} 920}
316 921
317# compiles a parsed expression 922# compiles a parsed expression
318sub set_expr { 923sub set_expr {
319 my ($self, $expr) = @_; 924 my ($self, $expr) = @_;
320 925
926 $self->{root} = [];
321 $self->{expr} = $expr; 927 $self->{expr} = $expr;
322 $self->recalculate; 928 $self->recalculate;
323} 929}
324 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
325# evaluate the current bg expression 979# evaluate the current bg expression
326sub recalculate { 980sub recalculate {
327 my ($self) = @_; 981 my ($arg_self) = @_;
328 982
329 # rate limit evaluation 983 # rate limit evaluation
330 984
331 if ($self->{next_refresh} > urxvt::NOW) { 985 if ($arg_self->{next_refresh} > urxvt::NOW) {
332 $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 {
333 $self->recalculate; 987 $arg_self->recalculate;
334 }); 988 });
335 return; 989 return;
336 } 990 }
337 991
338 $self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; 992 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
339 993
340 # set environment to evaluate user expression 994 # set environment to evaluate user expression
341 995
342 local $bgdsl_self = $self; 996 local $self = $arg_self;
343 local $border; 997 local $HOME = $ENV{HOME};
998 local $frame = [];
344 999
345 local $old = $self->{state}; 1000 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
346 local $new = my $state = $self->{state} = {};
347
348 ($l, $t, $w, $h) =
349 $self->get_geometry;
350
351 warn "$l,$t,$w,$h\n";#d#
352 1001
353 # evaluate user expression 1002 # evaluate user expression
354 1003
355 my $img = eval { $self->{expr}->() }; 1004 my @img = eval { $self->{expr}->() };
356 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"
357 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;
358 1014
359 # if the expression is sensitive to external events, prepare reevaluation then 1015 # if the expression is sensitive to external events, prepare reevaluation then
360 1016 $self->compile_frame ($frame, sub { $arg_self->recalculate });
361 my $repeat;
362
363 if (my $again = $state->{again}) {
364 $repeat = 1;
365 $state->{timer} = $again == $old->{again}
366 ? $old->{timer}
367 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
368 ++$self->{counter};
369 $self->recalculate
370 });
371 }
372
373 if (delete $state->{position_sensitive}) {
374 $repeat = 1;
375 $self->enable (position_change => sub { $_[0]->recalculate });
376 } else {
377 $self->disable ("position_change");
378 }
379
380 if (delete $state->{size_sensitive}) {
381 $repeat = 1;
382 $self->enable (size_change => sub { $_[0]->recalculate });
383 } else {
384 $self->disable ("size_change");
385 }
386
387 if (delete $state->{rootpmap_sensitive}) {
388 $repeat = 1;
389 $self->enable (rootpmap_change => sub { $_[0]->recalculate });
390 } else {
391 $self->disable ("rootpmap_change");
392 }
393 1017
394 # clear stuff we no longer need 1018 # clear stuff we no longer need
395 1019
396 %$old = (); 1020# unless (%{ $frame->[FR_STATE] }) {
397
398 unless ($repeat) {
399 delete $self->{state}; 1021# delete $self->{state};
400 delete $self->{expr}; 1022# delete $self->{expr};
401 } 1023# }
402 1024
403 # prepare and set background pixmap 1025 # set background pixmap
404 1026
405 $img = $img->sub_rect (0, 0, $w, $h)
406 if $img->w != $w || $img->h != $h;
407
408 $self->set_background ($img, $border); 1027 $self->set_background ($img, $self->{border});
409 $self->scr_recolour (0); 1028 $self->scr_recolour (0);
410 $self->want_refresh; 1029 $self->want_refresh;
411} 1030}
412 1031
413sub on_start { 1032sub on_start {
414 my ($self) = @_; 1033 my ($self) = @_;
415 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
416 $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");
417 1045
418 () 1046 ()
419} 1047}
420 1048

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines