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.37 by root, Fri Jun 8 20:35:43 2012 UTC vs.
Revision 1.63 by root, Tue Jun 19 18:17:56 2012 UTC

1#! perl 1#! perl
2 2
3#:META:X_RESOURCE:%.expr:string:background expression 3#:META:X_RESOURCE:%.expr:string:background expression
4#:META:X_RESOURCE:%.border.:boolean:respect the terminal border 4#:META:X_RESOURCE:%.border:boolean:respect the terminal border
5#:META:X_RESOURCE:%.interval:seconds:minimum time between updates
5 6
6#TODO: once, rootalign 7=head1 NAME
7 8
8=head1 background - manage terminal background 9 background - manage terminal background
9 10
10=head2 SYNOPSIS 11=head1 SYNOPSIS
11 12
12 urxvt --background-expr 'background expression' 13 urxvt --background-expr 'background expression'
13 --background-border 14 --background-border
15 --background-interval seconds
14 16
15=head2 DESCRIPTION 17=head1 DESCRIPTION
16 18
17This extension manages the terminal background by creating a picture that 19This extension manages the terminal background by creating a picture that
18is behind the text, replacing the normal background colour. 20is behind the text, replacing the normal background colour.
19 21
20It does so by evaluating a Perl expression that I<calculates> the image on 22It does so by evaluating a Perl expression that I<calculates> the image on
30 32
31Or specified as a X resource: 33Or specified as a X resource:
32 34
33 URxvt.background-expr: scale load "/path/to/mybg.png" 35 URxvt.background-expr: scale load "/path/to/mybg.png"
34 36
35=head2 THEORY OF OPERATION 37=head1 THEORY OF OPERATION
36 38
37At startup, just before the window is mapped for the first time, the 39At startup, just before the window is mapped for the first time, the
38expression is evaluated and must yield an image. The image is then 40expression is evaluated and must yield an image. The image is then
39extended as necessary to cover the whole terminal window, and is set as a 41extended as necessary to cover the whole terminal window, and is set as a
40background pixmap. 42background pixmap.
55 57
56For example, an expression such as C<scale load "$HOME/mybg.png"> scales the 58For example, an expression such as C<scale load "$HOME/mybg.png"> scales the
57image to the window size, so it relies on the window size and will 59image to the window size, so it relies on the window size and will
58be reevaluated each time it is changed, but not when it moves for 60be reevaluated each time it is changed, but not when it moves for
59example. That ensures that the picture always fills the terminal, even 61example. That ensures that the picture always fills the terminal, even
60after it's size changes. 62after its size changes.
61 63
62=head3 EXPRESSIONS 64=head2 EXPRESSIONS
63 65
64Expressions are normal Perl expressions, in fact, they are Perl blocks - 66Expressions are normal Perl expressions, in fact, they are Perl blocks -
65which means you could use multiple lines and statements: 67which means you could use multiple lines and statements:
66 68
67 again 3600; 69 again 3600;
69 return scale load "$HOME/weekday.png"; 71 return scale load "$HOME/weekday.png";
70 } else { 72 } else {
71 return scale load "$HOME/sunday.png"; 73 return scale load "$HOME/sunday.png";
72 } 74 }
73 75
74This expression gets evaluated once per hour. It will set F<sunday.png> as 76This expression is evaluated once per hour. It will set F<sunday.png> as
75background on sundays, and F<weekday.png> on all other days. 77background on Sundays, and F<weekday.png> on all other days.
76 78
77Fortunately, we expect that most expressions will be much simpler, with 79Fortunately, we expect that most expressions will be much simpler, with
78little Perl knowledge needed. 80little Perl knowledge needed.
79 81
80Basically, you always start with a function that "generates" an image 82Basically, you always start with a function that "generates" an image
97its result becomes the argument to the C<scale> function. 99its result becomes the argument to the C<scale> function.
98 100
99Many operators also allow some parameters preceding the input image 101Many operators also allow some parameters preceding the input image
100that modify its behaviour. For example, C<scale> without any additional 102that modify its behaviour. For example, C<scale> without any additional
101arguments scales the image to size of the terminal window. If you specify 103arguments scales the image to size of the terminal window. If you specify
102an additional argument, it uses it as a percentage: 104an additional argument, it uses it as a scale factor (multiply by 100 to
105get a percentage):
103 106
104 scale 200, load "$HOME/mypic.png" 107 scale 2, load "$HOME/mypic.png"
105 108
106This enlarges the image by a factor of 2 (200%). As you can see, C<scale> 109This enlarges the image by a factor of 2 (200%). As you can see, C<scale>
107has now two arguments, the C<200> and the C<load> expression, while 110has now two arguments, the C<200> and the C<load> expression, while
108C<load> only has one argument. Arguments are separated from each other by 111C<load> only has one argument. Arguments are separated from each other by
109commas. 112commas.
110 113
111Scale also accepts two arguments, which are then separate factors for both 114Scale also accepts two arguments, which are then separate factors for both
112horizontal and vertical dimensions. For example, this halves the image 115horizontal and vertical dimensions. For example, this halves the image
113width and doubles the image height: 116width and doubles the image height:
114 117
115 scale 50, 200, load "$HOME/mypic.png" 118 scale 0.5, 2, load "$HOME/mypic.png"
116 119
117TODO 120Other effects than scaling are also readily available, for example, you can
121tile the image to fill the whole window, instead of resizing it:
118 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
119=head3 CYCLES AND CACHING 141=head2 CYCLES AND CACHING
120 142
121TODO 143=head3 C<load> et al.
122 144
145As has been mentioned before, the expression might be evaluated multiple
123Each time the expression is reevaluated, a new cycle is said to have begun. Many operators 146times. Each time the expression is reevaluated, a new cycle is said to
124cache their results till the next cycle. For example 147have begun. Many operators cache their results till the next cycle.
125 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
126=head2 REFERENCE 195=head1 REFERENCE
127 196
128=head3 COMMAND LINE SWITCHES 197=head2 COMMAND LINE SWITCHES
129 198
130=over 4 199=over 4
131 200
132=item --background-expr perl-expression 201=item --background-expr perl-expression
133 202
139overwriting borders and any other areas, such as the scrollbar. 208overwriting borders and any other areas, such as the scrollbar.
140 209
141Specifying this flag changes the behaviour, so that the image only 210Specifying this flag changes the behaviour, so that the image only
142replaces the background of the character area. 211replaces the background of the character area.
143 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
144=back 222=back
145 223
146=cut 224=cut
147 225
148our $EXPR;#d# 226our %_IMG_CACHE;
149#$EXPR = 'move W * 0.1, -H * 0.1, resize W * 0.5, H * 0.5, repeat_none load "opensource.png"';
150$EXPR = 'move -TX, -TY, load "argb.png"';
151#$EXPR = '
152# rotate W, H, 50, 50, counter 1/59.95, repeat_mirror,
153# clip X, Y, W, H, repeat_mirror,
154# load "/root/pix/das_fette_schwein.jpg"
155#';
156#$EXPR = 'solid "red"';
157#$EXPR = 'blur root, 10, 10'
158#$EXPR = 'blur move (root, -x, -y), 5, 5'
159#resize load "/root/pix/das_fette_schwein.jpg", w, h
160
161our $HOME; 227our $HOME;
162our ($self, $old, $new); 228our ($self, $frame);
163our ($x, $y, $w, $h); 229our ($x, $y, $w, $h);
164 230
165# enforce at least this interval between updates 231# enforce at least this interval between updates
166our $MIN_INTERVAL = 1/100; 232our $MIN_INTERVAL = 6/59.951;
167 233
168{ 234{
169 package urxvt::bgdsl; # background language 235 package urxvt::bgdsl; # background language
236
237 sub FR_PARENT() { 0 } # parent frame, if any - must be #0
238 sub FR_CACHE () { 1 } # cached values
239 sub FR_AGAIN () { 2 } # what this expr is sensitive to
240 sub FR_STATE () { 3 } # watchers etc.
241
242 use List::Util qw(min max sum shuffle);
170 243
171=head2 PROVIDERS/GENERATORS 244=head2 PROVIDERS/GENERATORS
172 245
173These functions provide an image, by loading it from disk, grabbing it 246These functions provide an image, by loading it from disk, grabbing it
174from the root screen or by simply generating it. They are used as starting 247from the root screen or by simply generating it. They are used as starting
179=item load $path 252=item load $path
180 253
181Loads the image at the given C<$path>. The image is set to plane tiling 254Loads the image at the given C<$path>. The image is set to plane tiling
182mode. 255mode.
183 256
184Loaded images will be cached for one cycle. 257Loaded images will be cached for one cycle, and shared between temrinals
258running in the same process (e.g. in C<urxvtd>).
259
260#=item load_uc $path
261#
262#Load uncached - same as load, but does not cache the image. This function
263#is most useufl if you want to optimise a background expression in some
264#way.
185 265
186=cut 266=cut
187 267
188 sub load($) { 268 sub load($) {
189 my ($path) = @_; 269 my ($path) = @_;
190 270
191 $new->{load}{$path} = $old->{load}{$path} || $self->new_img_from_file ($path); 271 $_IMG_CACHE{$path} || do {
272 my $img = $self->new_img_from_file ($path);
273 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img);
274 $img
275 }
192 } 276 }
193 277
194=item root 278=item root
195 279
196Returns the root window pixmap, that is, hopefully, the background image 280Returns the root window pixmap, that is, hopefully, the background image
197of your screen. The image is set to extend mode. 281of your screen.
198 282
199This function makes your expression root sensitive, that means it will be 283This function makes your expression root sensitive, that means it will be
200reevaluated when the bg image changes. 284reevaluated when the bg image changes.
201 285
202=cut 286=cut
203 287
204 sub root() { 288 sub root() {
205 $new->{rootpmap_sensitive} = 1; 289 $frame->[FR_AGAIN]{rootpmap} = 1;
206 die "root op not supported, exg, we need you"; 290 $self->new_img_from_root
207 } 291 }
208 292
209=item solid $colour 293=item solid $colour
210 294
211=item solid $width, $height, $colour 295=item solid $width, $height, $colour
212 296
213Creates a new image and completely fills it with the given colour. The 297Creates a new image and completely fills it with the given colour. The
214image is set to tiling mode. 298image is set to tiling mode.
215 299
216If <$width> and C<$height> are omitted, it creates a 1x1 image, which is 300If C<$width> and C<$height> are omitted, it creates a 1x1 image, which is
217useful for solid backgrounds or for use in filtering effects. 301useful for solid backgrounds or for use in filtering effects.
218 302
219=cut 303=cut
220 304
221 sub solid($$;$) { 305 sub solid($;$$) {
222 my $colour = pop; 306 my $colour = pop;
223 307
224 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); 308 my $img = $self->new_img (urxvt::PictStandardARGB32, 0, 0, $_[0] || 1, $_[1] || 1);
225 $img->fill ($colour); 309 $img->fill ($colour);
226 $img 310 $img
227 } 311 }
228 312
229=back 313=item clone $img
230 314
231=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.
232 317
233The following functions provide variable data such as the terminal
234window dimensions. Most of them make your expression sensitive to some
235events, for example using C<TW> (terminal width) means your expression is
236evaluated again when the terminal is resized.
237
238=over 4
239
240=item TX
241
242=item TY
243
244Return the X and Y coordinates of the terminal window (the terminal
245window is the full window by default, and the character area only when in
246border-respect mode).
247
248Using these functions make your expression sensitive to window moves.
249
250These functions are mainly useful to align images to the root window.
251
252Example: load an image and align it so it looks as if anchored to the
253background.
254
255 move -TX, -TY, load "mybg.png"
256
257=item TW
258
259Return the width (C<TW>) and height (C<TH>) of the terminal window (the
260terminal window is the full window by default, and the character area only
261when in border-respect mode).
262
263Using these functions make your expression sensitive to window resizes.
264
265These functions are mainly useful to scale images, or to clip images to
266the window size to conserve memory.
267
268Example: take the screen background, clip it to the window size, blur it a
269bit, align it to the window position and use it as background.
270
271 clip move -TX, -TY, blur 5, root
272
273=cut 318=cut
274 319
275 sub TX() { $new->{position_sensitive} = 1; $x }
276 sub TY() { $new->{position_sensitive} = 1; $y }
277 sub TW() { $new->{size_sensitive} = 1; $w }
278 sub TH() { $new->{size_sensitive} = 1; $h }
279
280=item now
281
282Returns the current time as (fractional) seconds since the epoch.
283
284Using this expression does I<not> make your expression sensitive to time,
285but the next two functions do.
286
287=item again $seconds
288
289When this function is used the expression will be reevaluated again in
290C<$seconds> seconds.
291
292Example: load some image and rotate it according to the time of day (as if it were
293the hour pointer of a clock). Update this image every minute.
294
295 again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png"
296
297=item counter $seconds
298
299Like C<again>, but also returns an increasing counter value, starting at
3000, which might be useful for some simple animation effects.
301
302=cut
303
304 sub now() { urxvt::NOW }
305
306 sub again($) {
307 $new->{again} = $_[0];
308 }
309
310 sub counter($) { 320 sub clone($) {
311 $new->{again} = $_[0]; 321 $_[0]->clone
312 $self->{counter} + 0
313 } 322 }
314 323
315=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 }
316 367
317=head2 TILING MODES 368=head2 TILING MODES
318 369
319The 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
320way 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.
350become 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
351image over another image or the background colour while leaving all 402image over another image or the background colour while leaving all
352background pixels outside the image unchanged. 403background pixels outside the image unchanged.
353 404
354Example: load an image and display it in the upper left corner. The rest 405Example: load an image and display it in the upper left corner. The rest
355of the space is left "empty" (transparent or wahtever your compisotr does 406of the space is left "empty" (transparent or whatever your compositor does
356in alpha mode, else background colour). 407in alpha mode, else background colour).
357 408
358 pad load "mybg.png" 409 pad load "mybg.png"
359 410
360=item extend $img 411=item extend $img
361 412
362Extends 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
363area 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
364filtering operations and want the pixels outside the image to have the 415filtering operations and want the pixels outside the image to have the
365same values as the pixels near the edge. 416same values as the pixels near the edge.
366 417
367Example: just for curiosity, how does this pixel extension stuff work? 418Example: just for curiosity, how does this pixel extension stuff work?
368 419
394 $img 445 $img
395 } 446 }
396 447
397=back 448=back
398 449
399=head2 PIXEL OPERATORS 450=head2 VARIABLE VALUES
400 451
401The 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.
402 457
403=over 4 458=over 4
404 459
405=item clone $img 460=item TX
406 461
407Returns an exact copy of the image. 462=item TY
408 463
409=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).
410 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
411 sub clone($) { 530 sub counter($) {
412 $_[0]->clone 531 $frame->[FR_AGAIN]{time} = $_[0];
532 $frame->[FR_STATE]{counter} + 0
413 } 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
414 542
415=item clip $img 543=item clip $img
416 544
417=item clip $width, $height, $img 545=item clip $width, $height, $img
418 546
442 $img->sub_rect ($_[0], $_[1], $w, $h) 570 $img->sub_rect ($_[0], $_[1], $w, $h)
443 } 571 }
444 572
445=item scale $img 573=item scale $img
446 574
447=item scale $size_percent, $img 575=item scale $size_factor, $img
448 576
449=item scale $width_percent, $height_percent, $img 577=item scale $width_factor, $height_factor, $img
450 578
451Scales the image by the given percentages in horizontal 579Scales the image by the given factors in horizontal
452(C<$width_percent>) and vertical (C<$height_percent>) direction. 580(C<$width>) and vertical (C<$height>) direction.
453 581
454If only one percentage is give, it is used for both directions. 582If only one factor is give, it is used for both directions.
455 583
456If 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
457keeping aspect. 585keeping aspect.
458 586
459=item resize $width, $height, $img 587=item resize $width, $height, $img
460 588
461Resizes the image to exactly C<$width> times C<$height> pixels. 589Resizes the image to exactly C<$width> times C<$height> pixels.
462 590
463=cut 591=item fit $img
464 592
465#TODO: maximise, maximise_fill? 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
466 608
467 sub scale($;$;$) { 609 sub scale($;$;$) {
468 my $img = pop; 610 my $img = pop;
469 611
470 @_ == 2 ? $img->scale ($_[0] * $img->w * 0.01, $_[1] * $img->h * 0.01) 612 @_ == 2 ? $img->scale ($_[0] * $img->w, $_[1] * $img->h)
471 : @_ ? $img->scale ($_[0] * $img->w * 0.01, $_[0] * $img->h * 0.01) 613 : @_ ? $img->scale ($_[0] * $img->w, $_[0] * $img->h)
472 : $img->scale (TW, TH) 614 : $img->scale (TW, TH)
473 } 615 }
474 616
475 sub resize($$$) { 617 sub resize($$$) {
476 my $img = pop; 618 my $img = pop;
477 $img->scale ($_[0], $_[1]) 619 $img->scale ($_[0], $_[1])
478 } 620 }
479 621
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
480=item move $dx, $dy, $img 636=item move $dx, $dy, $img
481 637
482Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in 638Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in
483the vertical. 639the vertical.
484 640
485Example: move the image right by 20 pixels and down by 30. 641Example: move the image right by 20 pixels and down by 30.
486 642
487 move 20, 30, ... 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"
488 667
489=item rootalign $img 668=item rootalign $img
490 669
491Moves the image so that it appears glued to the screen as opposed to the 670Moves the image so that it appears glued to the screen as opposed to the
492window. This gives the illusion of a larger area behind the window. It is 671window. This gives the illusion of a larger area behind the window. It is
498 rootalign mirror load "mybg.png" 677 rootalign mirror load "mybg.png"
499 678
500Example: take the screen background and align it, giving the illusion of 679Example: take the screen background and align it, giving the illusion of
501transparency as long as the window isn't in front of other windows. 680transparency as long as the window isn't in front of other windows.
502 681
503 rootalign root 682 rootalign root
504 683
505=cut 684=cut
506 685
507 sub move($$;$) { 686 sub move($$;$) {
508 my $img = pop->clone; 687 my $img = pop->clone;
509 $img->move ($_[0], $_[1]); 688 $img->move ($_[0], $_[1]);
510 $img 689 $img
511 } 690 }
512 691
692 sub align($;$$) {
693 my $img = pop;
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
513 sub rootalign($) { 708 sub rootalign($) {
514 move -TX, -TY, $_[0] 709 move -TX, -TY, $_[0]
515 } 710 }
516 711
712=item rotate $center_x, $center_y, $degrees
713
714Rotates the image by C<$degrees> degrees, counter-clockwise, around the
715pointer at C<$center_x> and C<$center_y> (specified as factor of image
716width/height).
717
718#TODO# new width, height, maybe more operators?
719
720Example: rotate the image by 90 degrees
721
722=cut
723
724 sub rotate($$$$) {
725 my $img = pop;
726 $img->rotate (
727 $_[0] * ($img->w + $img->x),
728 $_[1] * ($img->h + $img->y),
729 $_[2] * (3.14159265 / 180),
730 )
731 }
732
733=back
734
735=head2 COLOUR MODIFICATIONS
736
737The following operators change the pixels of the image.
738
739=over 4
740
517=item contrast $factor, $img 741=item contrast $factor, $img
518 742
519=item contrast $r, $g, $b, $img 743=item contrast $r, $g, $b, $img
520 744
521=item contrast $r, $g, $b, $a, $img 745=item contrast $r, $g, $b, $a, $img
522 746
523Adjusts the I<contrast> of an image. 747Adjusts the I<contrast> of an image.
524 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
525=item brightness $factor, $img 760=item brightness $bias, $img
526 761
527=item brightness $r, $g, $b, $img 762=item brightness $r, $g, $b, $img
528 763
529=item brightness $r, $g, $b, $a, $img 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.
530 778
531=cut 779=cut
532 780
533 sub contrast($$;$$;$) { 781 sub contrast($$;$$;$) {
534 my $img = pop; 782 my $img = pop;
535 my ($r, $g, $b, $a) = @_; 783 my ($r, $g, $b, $a) = @_;
536 784
537 ($g, $b) = ($r, $r) if @_ < 4; 785 ($g, $b) = ($r, $r) if @_ < 3;
538 $a = 1 if @_ < 5; 786 $a = 1 if @_ < 4;
539 787
540 $img = $img->clone; 788 $img = $img->clone;
541 $img->contrast ($r, $g, $b, $a); 789 $img->contrast ($r, $g, $b, $a);
542 $img 790 $img
543 } 791 }
544 792
545 sub brightness($$;$$;$) { 793 sub brightness($$;$$;$) {
546 my $img = pop; 794 my $img = pop;
547 my ($r, $g, $b, $a) = @_; 795 my ($r, $g, $b, $a) = @_;
548 796
549 ($g, $b) = ($r, $r) if @_ < 4; 797 ($g, $b) = ($r, $r) if @_ < 3;
550 $a = 1 if @_ < 5; 798 $a = 1 if @_ < 4;
551 799
552 $img = $img->clone; 800 $img = $img->clone;
553 $img->brightness ($r, $g, $b, $a); 801 $img->brightness ($r, $g, $b, $a);
554 $img 802 $img
555 } 803 }
556 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
557 sub blur($$;$) { 819 sub blur($$;$) {
558 my $img = pop; 820 my $img = pop;
559 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) 821 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0])
560 } 822 }
561 823
562 sub rotate($$$$$$) {
563 my $img = pop;
564 $img->rotate (
565 $_[0],
566 $_[1],
567 $_[2] * $img->w * .01,
568 $_[3] * $img->h * .01,
569 $_[4] * (3.14159265 / 180),
570 )
571 }
572
573=back 824=back
574 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
575=cut 907=cut
576 908
577} 909}
578 910
579sub parse_expr { 911sub parse_expr {
580 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 . "}";
581 die if $@; 918 die if $@;
582 $expr 919 $expr
583} 920}
584 921
585# compiles a parsed expression 922# compiles a parsed expression
586sub set_expr { 923sub set_expr {
587 my ($self, $expr) = @_; 924 my ($self, $expr) = @_;
588 925
926 $self->{root} = [];
589 $self->{expr} = $expr; 927 $self->{expr} = $expr;
590 $self->recalculate; 928 $self->recalculate;
929}
930
931# takes a hash of sensitivity indicators and installs watchers
932sub compile_frame {
933 my ($self, $frame, $cb) = @_;
934
935 my $state = $frame->[urxvt::bgdsl::FR_STATE] ||= {};
936 my $again = $frame->[urxvt::bgdsl::FR_AGAIN];
937
938 # don't keep stuff alive
939 Scalar::Util::weaken $state;
940
941 if ($again->{nested}) {
942 $state->{nested} = 1;
943 } else {
944 delete $state->{nested};
945 }
946
947 if (my $interval = $again->{time}) {
948 $state->{time} = [$interval, urxvt::timer->new->after ($interval)->interval ($interval)]
949 if $state->{time}[0] != $interval;
950
951 # callback *might* have changed, although we could just rule that out
952 $state->{time}[1]->cb (sub {
953 ++$state->{counter};
954 $cb->();
955 });
956 } else {
957 delete $state->{time};
958 }
959
960 if ($again->{position}) {
961 $state->{position} = $self->on (position_change => $cb);
962 } else {
963 delete $state->{position};
964 }
965
966 if ($again->{size}) {
967 $state->{size} = $self->on (size_change => $cb);
968 } else {
969 delete $state->{size};
970 }
971
972 if ($again->{rootpmap}) {
973 $state->{rootpmap} = $self->on (rootpmap_change => $cb);
974 } else {
975 delete $state->{rootpmap};
976 }
591} 977}
592 978
593# evaluate the current bg expression 979# evaluate the current bg expression
594sub recalculate { 980sub recalculate {
595 my ($arg_self) = @_; 981 my ($arg_self) = @_;
605 991
606 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; 992 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
607 993
608 # set environment to evaluate user expression 994 # set environment to evaluate user expression
609 995
610 local $self = $arg_self; 996 local $self = $arg_self;
611
612 local $HOME = $ENV{HOME}; 997 local $HOME = $ENV{HOME};
613 local $old = $self->{state}; 998 local $frame = [];
614 local $new = my $state = $self->{state} = {};
615 999
616 ($x, $y, $w, $h) =
617 $self->background_geometry ($self->{border}); 1000 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
618 1001
619 # evaluate user expression 1002 # evaluate user expression
620 1003
621 my $img = eval { $self->{expr}->() }; 1004 my @img = eval { $self->{expr}->() };
622 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"
623 die if !UNIVERSAL::isa $img, "urxvt::img"; 1008 if grep { !UNIVERSAL::isa $_, "urxvt::img" } @img;
624 1009
625 $state->{size_sensitive} = 1 1010 my $img = urxvt::bgdsl::merge @img;
1011
1012 $frame->[FR_AGAIN]{size} = 1
626 if $img->repeat_mode != urxvt::RepeatNormal; 1013 if $img->repeat_mode != urxvt::RepeatNormal;
627 1014
628 # if the expression is sensitive to external events, prepare reevaluation then 1015 # if the expression is sensitive to external events, prepare reevaluation then
629 1016 $self->compile_frame ($frame, sub { $arg_self->recalculate });
630 my $repeat;
631
632 if (my $again = $state->{again}) {
633 $repeat = 1;
634 my $self = $self;
635 $state->{timer} = $again == $old->{again}
636 ? $old->{timer}
637 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
638 ++$self->{counter};
639 $self->recalculate
640 });
641 }
642
643 if (delete $state->{position_sensitive}) {
644 $repeat = 1;
645 $self->enable (position_change => sub { $_[0]->recalculate });
646 } else {
647 $self->disable ("position_change");
648 }
649
650 if (delete $state->{size_sensitive}) {
651 $repeat = 1;
652 $self->enable (size_change => sub { $_[0]->recalculate });
653 } else {
654 $self->disable ("size_change");
655 }
656
657 if (delete $state->{rootpmap_sensitive}) {
658 $repeat = 1;
659 $self->enable (rootpmap_change => sub { $_[0]->recalculate });
660 } else {
661 $self->disable ("rootpmap_change");
662 }
663 1017
664 # clear stuff we no longer need 1018 # clear stuff we no longer need
665 1019
666 %$old = (); 1020# unless (%{ $frame->[FR_STATE] }) {
667
668 unless ($repeat) {
669 delete $self->{state}; 1021# delete $self->{state};
670 delete $self->{expr}; 1022# delete $self->{expr};
671 } 1023# }
672 1024
673 # set background pixmap 1025 # set background pixmap
674 1026
675 $self->set_background ($img, $self->{border}); 1027 $self->set_background ($img, $self->{border});
676 $self->scr_recolour (0); 1028 $self->scr_recolour (0);
678} 1030}
679 1031
680sub on_start { 1032sub on_start {
681 my ($self) = @_; 1033 my ($self) = @_;
682 1034
683 my $expr = $self->x_resource ("background.expr") 1035 my $expr = $self->x_resource ("%.expr")
684 or return; 1036 or return;
685 1037
1038 $self->has_render
1039 or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n";
1040
686 $self->set_expr (parse_expr $expr); 1041 $self->set_expr (parse_expr $expr);
687 $self->{border} = $self->x_resource_boolean ("background.border"); 1042 $self->{border} = $self->x_resource_boolean ("%.border");
1043
1044 $MIN_INTERVAL = $self->x_resource ("%.interval");
688 1045
689 () 1046 ()
690} 1047}
691 1048

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines