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.44 by root, Sun Jun 10 11:31:22 2012 UTC

3#:META:X_RESOURCE:%.expr:string:background expression 3#:META:X_RESOURCE:%.expr:string:background expression
4#:META:X_RESOURCE:%.border.:boolean:respect the terminal border 4#:META:X_RESOURCE:%.border.:boolean:respect the terminal border
5 5
6#TODO: once, rootalign 6#TODO: once, rootalign
7 7
8=head1 NAME
9
8=head1 background - manage terminal background 10 background - manage terminal background
9 11
10=head2 SYNOPSIS 12=head1 SYNOPSIS
11 13
12 urxvt --background-expr 'background expression' 14 urxvt --background-expr 'background expression'
13 --background-border 15 --background-border
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.
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 it's 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;
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 gets 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 scalign are also readily available, for exmaple, 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 143As has been mentioned before, the expression might be evaluated multiple
122
123Each time the expression is reevaluated, a new cycle is said to have begun. Many operators 144times. Each time the expression is reevaluated, a new cycle is said to
124cache their results till the next cycle. For example 145have begun. Many operators cache their results till the next cycle.
125 146
147For example, the C<load> operator keeps a copy of the image. If it is
148asked to load the same image on the next cycle it will not load it again,
149but return the cached copy.
150
151This only works for one cycle though, so as long as you load the same
152image every time, it will always be cached, but when you load a different
153image, it will forget about the first one.
154
155This allows you to either speed things up by keeping multiple images in
156memory, or comserve memory by loading images more often.
157
158For example, you can keep two images in memory and use a random one like
159this:
160
161 my $img1 = load "img1.png";
162 my $img2 = load "img2.png";
163 (0.5 > rand) ? $img1 : $img2
164
165Since both images are "loaded" every time the expression is evaluated,
166they are always kept in memory. Contrast this version:
167
168 my $path1 = "img1.png";
169 my $path2 = "img2.png";
170 load ((0.5 > rand) ? $path1 : $path2)
171
172Here, a path is selected randomly, and load is only called for one image,
173so keeps only one image in memory. If, on the next evaluation, luck
174decides to use the other path, then it will have to load that image again.
175
126=head2 REFERENCE 176=head1 REFERENCE
127 177
128=head3 COMMAND LINE SWITCHES 178=head2 COMMAND LINE SWITCHES
129 179
130=over 4 180=over 4
131 181
132=item --background-expr perl-expression 182=item --background-expr perl-expression
133 183
142replaces the background of the character area. 192replaces the background of the character area.
143 193
144=back 194=back
145 195
146=cut 196=cut
147
148our $EXPR;#d#
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 197
161our $HOME; 198our $HOME;
162our ($self, $old, $new); 199our ($self, $old, $new);
163our ($x, $y, $w, $h); 200our ($x, $y, $w, $h);
164 201
166our $MIN_INTERVAL = 1/100; 203our $MIN_INTERVAL = 1/100;
167 204
168{ 205{
169 package urxvt::bgdsl; # background language 206 package urxvt::bgdsl; # background language
170 207
208 use List::Util qw(min max sum shuffle);
209
171=head2 PROVIDERS/GENERATORS 210=head2 PROVIDERS/GENERATORS
172 211
173These functions provide an image, by loading it from disk, grabbing it 212These 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 213from the root screen or by simply generating it. They are used as starting
175points to get an image you can play with. 214points to get an image you can play with.
211=item solid $width, $height, $colour 250=item solid $width, $height, $colour
212 251
213Creates a new image and completely fills it with the given colour. The 252Creates a new image and completely fills it with the given colour. The
214image is set to tiling mode. 253image is set to tiling mode.
215 254
216If <$width> and C<$height> are omitted, it creates a 1x1 image, which is 255If C<$width> and C<$height> are omitted, it creates a 1x1 image, which is
217useful for solid backgrounds or for use in filtering effects. 256useful for solid backgrounds or for use in filtering effects.
218 257
219=cut 258=cut
220 259
221 sub solid($$;$) { 260 sub solid($;$$) {
222 my $colour = pop; 261 my $colour = pop;
223 262
224 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); 263 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1);
225 $img->fill ($colour); 264 $img->fill ($colour);
226 $img 265 $img
228 267
229=back 268=back
230 269
231=head2 VARIABLES 270=head2 VARIABLES
232 271
233The following functions provide variable data such as the terminal 272The following functions provide variable data such as the terminal window
273dimensions. They are not (Perl-) variables, they jsut return stuff that
234window dimensions. Most of them make your expression sensitive to some 274varies. Most of them make your expression sensitive to some events, for
235events, for example using C<TW> (terminal width) means your expression is 275example using C<TW> (terminal width) means your expression is evaluated
236evaluated again when the terminal is resized. 276again when the terminal is resized.
237 277
238=over 4 278=over 4
239 279
240=item TX 280=item TX
241 281
442 $img->sub_rect ($_[0], $_[1], $w, $h) 482 $img->sub_rect ($_[0], $_[1], $w, $h)
443 } 483 }
444 484
445=item scale $img 485=item scale $img
446 486
447=item scale $size_percent, $img 487=item scale $size_factor, $img
448 488
449=item scale $width_percent, $height_percent, $img 489=item scale $width_factor, $height_factor, $img
450 490
451Scales the image by the given percentages in horizontal 491Scales the image by the given factors in horizontal
452(C<$width_percent>) and vertical (C<$height_percent>) direction. 492(C<$width>) and vertical (C<$height>) direction.
453 493
454If only one percentage is give, it is used for both directions. 494If only one factor is give, it is used for both directions.
455 495
456If no percentages are given, scales the image to the window size without 496If no factors are given, scales the image to the window size without
457keeping aspect. 497keeping aspect.
458 498
459=item resize $width, $height, $img 499=item resize $width, $height, $img
460 500
461Resizes the image to exactly C<$width> times C<$height> pixels. 501Resizes the image to exactly C<$width> times C<$height> pixels.
462 502
463=cut 503=item fit $img
464 504
465#TODO: maximise, maximise_fill? 505=item fit $width, $height, $img
506
507Fits the image into the given C<$width> and C<$height> without changing
508aspect, or the terminal size. That means it will be shrunk or grown until
509the whole image fits into the given area, possibly leaving borders.
510
511=item cover $img
512
513=item cover $width, $height, $img
514
515Similar to C<fit>, but shrinks or grows until all of the area is covered
516by the image, so instead of potentially leaving borders, it will cut off
517image data that doesn't fit.
518
519=cut
466 520
467 sub scale($;$;$) { 521 sub scale($;$;$) {
468 my $img = pop; 522 my $img = pop;
469 523
470 @_ == 2 ? $img->scale ($_[0] * $img->w * 0.01, $_[1] * $img->h * 0.01) 524 @_ == 2 ? $img->scale ($_[0] * $img->w, $_[1] * $img->h)
471 : @_ ? $img->scale ($_[0] * $img->w * 0.01, $_[0] * $img->h * 0.01) 525 : @_ ? $img->scale ($_[0] * $img->w, $_[0] * $img->h)
472 : $img->scale (TW, TH) 526 : $img->scale (TW, TH)
473 } 527 }
474 528
475 sub resize($$$) { 529 sub resize($$$) {
476 my $img = pop; 530 my $img = pop;
477 $img->scale ($_[0], $_[1]) 531 $img->scale ($_[0], $_[1])
532 }
533
534 sub fit($;$$) {
535 my $img = pop;
536 my $w = ($_[0] || TW) / $img->w;
537 my $h = ($_[1] || TH) / $img->h;
538 scale +(min $w, $h), $img
539 }
540
541 sub cover($;$$) {
542 my $img = pop;
543 my $w = ($_[0] || TW) / $img->w;
544 my $h = ($_[1] || TH) / $img->h;
545 scale +(max $w, $h), $img
478 } 546 }
479 547
480=item move $dx, $dy, $img 548=item move $dx, $dy, $img
481 549
482Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in 550Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in
483the vertical. 551the vertical.
484 552
485Example: move the image right by 20 pixels and down by 30. 553Example: move the image right by 20 pixels and down by 30.
486 554
487 move 20, 30, ... 555 move 20, 30, ...
556
557=item center $img
558
559=item center $width, $height, $img
560
561Centers the image, i.e. the center of the image is moved to the center of
562the terminal window (or the box specified by C<$width> and C<$height> if
563given).
488 564
489=item rootalign $img 565=item rootalign $img
490 566
491Moves the image so that it appears glued to the screen as opposed to the 567Moves 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 568window. This gives the illusion of a larger area behind the window. It is
508 my $img = pop->clone; 584 my $img = pop->clone;
509 $img->move ($_[0], $_[1]); 585 $img->move ($_[0], $_[1]);
510 $img 586 $img
511 } 587 }
512 588
589 sub center($;$$) {
590 my $img = pop;
591 my $w = $_[0] || TW;
592 my $h = $_[0] || TH;
593
594 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img
595 }
596
513 sub rootalign($) { 597 sub rootalign($) {
514 move -TX, -TY, $_[0] 598 move -TX, -TY, $_[0]
515 } 599 }
516 600
517=item contrast $factor, $img 601=item contrast $factor, $img
520 604
521=item contrast $r, $g, $b, $a, $img 605=item contrast $r, $g, $b, $a, $img
522 606
523Adjusts the I<contrast> of an image. 607Adjusts the I<contrast> of an image.
524 608
609#TODO#
610
525=item brightness $factor, $img 611=item brightness $factor, $img
526 612
527=item brightness $r, $g, $b, $img 613=item brightness $r, $g, $b, $img
528 614
529=item brightness $r, $g, $b, $a, $img 615=item brightness $r, $g, $b, $a, $img
616
617Adjusts the brightness of an image.
530 618
531=cut 619=cut
532 620
533 sub contrast($$;$$;$) { 621 sub contrast($$;$$;$) {
534 my $img = pop; 622 my $img = pop;
552 $img = $img->clone; 640 $img = $img->clone;
553 $img->brightness ($r, $g, $b, $a); 641 $img->brightness ($r, $g, $b, $a);
554 $img 642 $img
555 } 643 }
556 644
645=item blur $radius, $img
646
647=item blur $radius_horz, $radius_vert, $img
648
649Gaussian-blurs the image with (roughly) C<$radius> pixel radius. The radii
650can also be specified separately.
651
652Blurring is often I<very> slow, at least compared or other
653operators. Larger blur radii are slower than smaller ones, too, so if you
654don't want to freeze your screen for long times, start experimenting with
655low values for radius (<5).
656
657=cut
658
557 sub blur($$;$) { 659 sub blur($$;$) {
558 my $img = pop; 660 my $img = pop;
559 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) 661 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0])
560 } 662 }
663
664=item rotate $new_width, $new_height, $center_x, $center_y, $degrees
665
666Rotates the image by C<$degrees> degrees, counter-clockwise, around the
667pointer at C<$center_x> and C<$center_y> (specified as factor of image
668width/height), generating a new image with width C<$new_width> and height
669C<$new_height>.
670
671#TODO# new width, height, maybe more operators?
672
673Example: rotate the image by 90 degrees
674
675=cut
561 676
562 sub rotate($$$$$$) { 677 sub rotate($$$$$$) {
563 my $img = pop; 678 my $img = pop;
564 $img->rotate ( 679 $img->rotate (
565 $_[0], 680 $_[0],
566 $_[1], 681 $_[1],
567 $_[2] * $img->w * .01, 682 $_[2] * $img->w,
568 $_[3] * $img->h * .01, 683 $_[3] * $img->h,
569 $_[4] * (3.14159265 / 180), 684 $_[4] * (3.14159265 / 180),
570 ) 685 )
571 } 686 }
572 687
573=back 688=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines