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.40 by root, Fri Jun 8 22:19:21 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;
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
117Other effects than scalign are also readily available, for exmaple, you can 120Other effects than scalign are also readily available, for exmaple, you can
118tile the image to fill the whole window, instead of resizing it: 121tile the image to fill the whole window, instead of resizing it:
119 122
120 tile load "$HOME/mypic.png" 123 tile load "$HOME/mypic.png"
133It first takes a snapshot of the screen background image, and then 136It first takes a snapshot of the screen background image, and then
134moves it to the upper left corner of the screen - the result is 137moves it to the upper left corner of the screen - the result is
135pseudo-transparency, as the image seems to be static while the window is 138pseudo-transparency, as the image seems to be static while the window is
136moved around. 139moved around.
137 140
138=head3 CYCLES AND CACHING 141=head2 CYCLES AND CACHING
139 142
140As has been mentioned before, the expression might be evaluated multiple 143As has been mentioned before, the expression might be evaluated multiple
141times. Each time the expression is reevaluated, a new cycle is said to 144times. Each time the expression is reevaluated, a new cycle is said to
142have begun. Many operators cache their results till the next cycle. 145have begun. Many operators cache their results till the next cycle.
143 146
168 171
169Here, a path is selected randomly, and load is only called for one image, 172Here, a path is selected randomly, and load is only called for one image,
170so keeps only one image in memory. If, on the next evaluation, luck 173so keeps only one image in memory. If, on the next evaluation, luck
171decides to use the other path, then it will have to load that image again. 174decides to use the other path, then it will have to load that image again.
172 175
173=head2 REFERENCE 176=head1 REFERENCE
174 177
175=head3 COMMAND LINE SWITCHES 178=head2 COMMAND LINE SWITCHES
176 179
177=over 4 180=over 4
178 181
179=item --background-expr perl-expression 182=item --background-expr perl-expression
180 183
200our $MIN_INTERVAL = 1/100; 203our $MIN_INTERVAL = 1/100;
201 204
202{ 205{
203 package urxvt::bgdsl; # background language 206 package urxvt::bgdsl; # background language
204 207
208 use List::Util qw(min max sum shuffle);
209
205=head2 PROVIDERS/GENERATORS 210=head2 PROVIDERS/GENERATORS
206 211
207These functions provide an image, by loading it from disk, grabbing it 212These functions provide an image, by loading it from disk, grabbing it
208from 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
209points to get an image you can play with. 214points to get an image you can play with.
250If C<$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
251useful for solid backgrounds or for use in filtering effects. 256useful for solid backgrounds or for use in filtering effects.
252 257
253=cut 258=cut
254 259
255 sub solid($$;$) { 260 sub solid($;$$) {
256 my $colour = pop; 261 my $colour = pop;
257 262
258 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); 263 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1);
259 $img->fill ($colour); 264 $img->fill ($colour);
260 $img 265 $img
262 267
263=back 268=back
264 269
265=head2 VARIABLES 270=head2 VARIABLES
266 271
267The 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
268window dimensions. Most of them make your expression sensitive to some 274varies. Most of them make your expression sensitive to some events, for
269events, for example using C<TW> (terminal width) means your expression is 275example using C<TW> (terminal width) means your expression is evaluated
270evaluated again when the terminal is resized. 276again when the terminal is resized.
271 277
272=over 4 278=over 4
273 279
274=item TX 280=item TX
275 281
476 $img->sub_rect ($_[0], $_[1], $w, $h) 482 $img->sub_rect ($_[0], $_[1], $w, $h)
477 } 483 }
478 484
479=item scale $img 485=item scale $img
480 486
481=item scale $size_percent, $img 487=item scale $size_factor, $img
482 488
483=item scale $width_percent, $height_percent, $img 489=item scale $width_factor, $height_factor, $img
484 490
485Scales the image by the given percentages in horizontal 491Scales the image by the given factors in horizontal
486(C<$width_percent>) and vertical (C<$height_percent>) direction. 492(C<$width>) and vertical (C<$height>) direction.
487 493
488If only one percentage is give, it is used for both directions. 494If only one factor is give, it is used for both directions.
489 495
490If 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
491keeping aspect. 497keeping aspect.
492 498
493=item resize $width, $height, $img 499=item resize $width, $height, $img
494 500
495Resizes the image to exactly C<$width> times C<$height> pixels. 501Resizes the image to exactly C<$width> times C<$height> pixels.
496 502
497=cut 503=item fit $img
498 504
499#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
500 520
501 sub scale($;$;$) { 521 sub scale($;$;$) {
502 my $img = pop; 522 my $img = pop;
503 523
504 @_ == 2 ? $img->scale ($_[0] * $img->w * 0.01, $_[1] * $img->h * 0.01) 524 @_ == 2 ? $img->scale ($_[0] * $img->w, $_[1] * $img->h)
505 : @_ ? $img->scale ($_[0] * $img->w * 0.01, $_[0] * $img->h * 0.01) 525 : @_ ? $img->scale ($_[0] * $img->w, $_[0] * $img->h)
506 : $img->scale (TW, TH) 526 : $img->scale (TW, TH)
507 } 527 }
508 528
509 sub resize($$$) { 529 sub resize($$$) {
510 my $img = pop; 530 my $img = pop;
511 $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
512 } 546 }
513 547
514=item move $dx, $dy, $img 548=item move $dx, $dy, $img
515 549
516Moves 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
517the vertical. 551the vertical.
518 552
519Example: move the image right by 20 pixels and down by 30. 553Example: move the image right by 20 pixels and down by 30.
520 554
521 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).
522 564
523=item rootalign $img 565=item rootalign $img
524 566
525Moves 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
526window. 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
542 my $img = pop->clone; 584 my $img = pop->clone;
543 $img->move ($_[0], $_[1]); 585 $img->move ($_[0], $_[1]);
544 $img 586 $img
545 } 587 }
546 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
547 sub rootalign($) { 597 sub rootalign($) {
548 move -TX, -TY, $_[0] 598 move -TX, -TY, $_[0]
549 } 599 }
550 600
551=item contrast $factor, $img 601=item contrast $factor, $img
612 } 662 }
613 663
614=item rotate $new_width, $new_height, $center_x, $center_y, $degrees 664=item rotate $new_width, $new_height, $center_x, $center_y, $degrees
615 665
616Rotates the image by C<$degrees> degrees, counter-clockwise, around the 666Rotates the image by C<$degrees> degrees, counter-clockwise, around the
617pointer at C<$center_x> and C<$center_y> (specified as percentage of image 667pointer at C<$center_x> and C<$center_y> (specified as factor of image
618width/height), generating a new image with width C<$new_width> and height 668width/height), generating a new image with width C<$new_width> and height
619C<$new_height>. 669C<$new_height>.
620 670
621#TODO# new width, height, maybe more operators? 671#TODO# new width, height, maybe more operators?
622 672
627 sub rotate($$$$$$) { 677 sub rotate($$$$$$) {
628 my $img = pop; 678 my $img = pop;
629 $img->rotate ( 679 $img->rotate (
630 $_[0], 680 $_[0],
631 $_[1], 681 $_[1],
632 $_[2] * $img->w * .01, 682 $_[2] * $img->w,
633 $_[3] * $img->h * .01, 683 $_[3] * $img->h,
634 $_[4] * (3.14159265 / 180), 684 $_[4] * (3.14159265 / 180),
635 ) 685 )
636 } 686 }
637 687
638=back 688=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines