… | |
… | |
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 | |
17 | This extension manages the terminal background by creating a picture that |
19 | This extension manages the terminal background by creating a picture that |
18 | is behind the text, replacing the normal background colour. |
20 | is behind the text, replacing the normal background colour. |
19 | |
21 | |
20 | It does so by evaluating a Perl expression that I<calculates> the image on |
22 | It does so by evaluating a Perl expression that I<calculates> the image on |
… | |
… | |
30 | |
32 | |
31 | Or specified as a X resource: |
33 | Or 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 | |
37 | At startup, just before the window is mapped for the first time, the |
39 | At startup, just before the window is mapped for the first time, the |
38 | expression is evaluated and must yield an image. The image is then |
40 | expression is evaluated and must yield an image. The image is then |
39 | extended as necessary to cover the whole terminal window, and is set as a |
41 | extended as necessary to cover the whole terminal window, and is set as a |
40 | background pixmap. |
42 | background pixmap. |
… | |
… | |
57 | image to the window size, so it relies on the window size and will |
59 | image to the window size, so it relies on the window size and will |
58 | be reevaluated each time it is changed, but not when it moves for |
60 | be reevaluated each time it is changed, but not when it moves for |
59 | example. That ensures that the picture always fills the terminal, even |
61 | example. That ensures that the picture always fills the terminal, even |
60 | after it's size changes. |
62 | after it's size changes. |
61 | |
63 | |
62 | =head3 EXPRESSIONS |
64 | =head2 EXPRESSIONS |
63 | |
65 | |
64 | Expressions are normal Perl expressions, in fact, they are Perl blocks - |
66 | Expressions are normal Perl expressions, in fact, they are Perl blocks - |
65 | which means you could use multiple lines and statements: |
67 | which means you could use multiple lines and statements: |
66 | |
68 | |
67 | again 3600; |
69 | again 3600; |
… | |
… | |
97 | its result becomes the argument to the C<scale> function. |
99 | its result becomes the argument to the C<scale> function. |
98 | |
100 | |
99 | Many operators also allow some parameters preceding the input image |
101 | Many operators also allow some parameters preceding the input image |
100 | that modify its behaviour. For example, C<scale> without any additional |
102 | that modify its behaviour. For example, C<scale> without any additional |
101 | arguments scales the image to size of the terminal window. If you specify |
103 | arguments scales the image to size of the terminal window. If you specify |
102 | an additional argument, it uses it as a percentage: |
104 | an additional argument, it uses it as a scale factor (multiply by 100 to |
|
|
105 | get a percentage): |
103 | |
106 | |
104 | scale 200, load "$HOME/mypic.png" |
107 | scale 2, load "$HOME/mypic.png" |
105 | |
108 | |
106 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
109 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
107 | has now two arguments, the C<200> and the C<load> expression, while |
110 | has now two arguments, the C<200> and the C<load> expression, while |
108 | C<load> only has one argument. Arguments are separated from each other by |
111 | C<load> only has one argument. Arguments are separated from each other by |
109 | commas. |
112 | commas. |
110 | |
113 | |
111 | Scale also accepts two arguments, which are then separate factors for both |
114 | Scale also accepts two arguments, which are then separate factors for both |
112 | horizontal and vertical dimensions. For example, this halves the image |
115 | horizontal and vertical dimensions. For example, this halves the image |
113 | width and doubles the image height: |
116 | width 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 | |
117 | Other effects than scalign are also readily available, for exmaple, you can |
120 | Other effects than scalign are also readily available, for exmaple, you can |
118 | tile the image to fill the whole window, instead of resizing it: |
121 | tile 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" |
… | |
… | |
133 | It first takes a snapshot of the screen background image, and then |
136 | It first takes a snapshot of the screen background image, and then |
134 | moves it to the upper left corner of the screen - the result is |
137 | moves it to the upper left corner of the screen - the result is |
135 | pseudo-transparency, as the image seems to be static while the window is |
138 | pseudo-transparency, as the image seems to be static while the window is |
136 | moved around. |
139 | moved around. |
137 | |
140 | |
138 | =head3 CYCLES AND CACHING |
141 | =head2 CYCLES AND CACHING |
139 | |
142 | |
140 | As has been mentioned before, the expression might be evaluated multiple |
143 | As has been mentioned before, the expression might be evaluated multiple |
141 | times. Each time the expression is reevaluated, a new cycle is said to |
144 | times. Each time the expression is reevaluated, a new cycle is said to |
142 | have begun. Many operators cache their results till the next cycle. |
145 | have begun. Many operators cache their results till the next cycle. |
143 | |
146 | |
… | |
… | |
168 | |
171 | |
169 | Here, a path is selected randomly, and load is only called for one image, |
172 | Here, a path is selected randomly, and load is only called for one image, |
170 | so keeps only one image in memory. If, on the next evaluation, luck |
173 | so keeps only one image in memory. If, on the next evaluation, luck |
171 | decides to use the other path, then it will have to load that image again. |
174 | decides 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 | |
… | |
… | |
200 | our $MIN_INTERVAL = 1/100; |
203 | our $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 | |
207 | These functions provide an image, by loading it from disk, grabbing it |
212 | These functions provide an image, by loading it from disk, grabbing it |
208 | from the root screen or by simply generating it. They are used as starting |
213 | from the root screen or by simply generating it. They are used as starting |
209 | points to get an image you can play with. |
214 | points to get an image you can play with. |
… | |
… | |
250 | If C<$width> and C<$height> are omitted, it creates a 1x1 image, which is |
255 | If C<$width> and C<$height> are omitted, it creates a 1x1 image, which is |
251 | useful for solid backgrounds or for use in filtering effects. |
256 | useful 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 | |
267 | The following functions provide variable data such as the terminal |
272 | The following functions provide variable data such as the terminal window |
|
|
273 | dimensions. They are not (Perl-) variables, they jsut return stuff that |
268 | window dimensions. Most of them make your expression sensitive to some |
274 | varies. Most of them make your expression sensitive to some events, for |
269 | events, for example using C<TW> (terminal width) means your expression is |
275 | example using C<TW> (terminal width) means your expression is evaluated |
270 | evaluated again when the terminal is resized. |
276 | again 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 | |
485 | Scales the image by the given percentages in horizontal |
491 | Scales 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 | |
488 | If only one percentage is give, it is used for both directions. |
494 | If only one factor is give, it is used for both directions. |
489 | |
495 | |
490 | If no percentages are given, scales the image to the window size without |
496 | If no factors are given, scales the image to the window size without |
491 | keeping aspect. |
497 | keeping aspect. |
492 | |
498 | |
493 | =item resize $width, $height, $img |
499 | =item resize $width, $height, $img |
494 | |
500 | |
495 | Resizes the image to exactly C<$width> times C<$height> pixels. |
501 | Resizes 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 | |
|
|
507 | Fits the image into the given C<$width> and C<$height> without changing |
|
|
508 | aspect, or the terminal size. That means it will be shrunk or grown until |
|
|
509 | the whole image fits into the given area, possibly leaving borders. |
|
|
510 | |
|
|
511 | =item cover $img |
|
|
512 | |
|
|
513 | =item cover $width, $height, $img |
|
|
514 | |
|
|
515 | Similar to C<fit>, but shrinks or grows until all of the area is covered |
|
|
516 | by the image, so instead of potentially leaving borders, it will cut off |
|
|
517 | image 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 | |
516 | Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in |
550 | Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in |
517 | the vertical. |
551 | the vertical. |
518 | |
552 | |
519 | Example: move the image right by 20 pixels and down by 30. |
553 | Example: 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 | |
|
|
561 | Centers the image, i.e. the center of the image is moved to the center of |
|
|
562 | the terminal window (or the box specified by C<$width> and C<$height> if |
|
|
563 | given). |
522 | |
564 | |
523 | =item rootalign $img |
565 | =item rootalign $img |
524 | |
566 | |
525 | Moves the image so that it appears glued to the screen as opposed to the |
567 | Moves the image so that it appears glued to the screen as opposed to the |
526 | window. This gives the illusion of a larger area behind the window. It is |
568 | window. 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 | |
616 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
666 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
617 | pointer at C<$center_x> and C<$center_y> (specified as percentage of image |
667 | pointer at C<$center_x> and C<$center_y> (specified as factor of image |
618 | width/height), generating a new image with width C<$new_width> and height |
668 | width/height), generating a new image with width C<$new_width> and height |
619 | C<$new_height>. |
669 | C<$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 |