… | |
… | |
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; |
… | |
… | |
70 | } else { |
72 | } else { |
71 | return scale load "$HOME/sunday.png"; |
73 | return scale load "$HOME/sunday.png"; |
72 | } |
74 | } |
73 | |
75 | |
74 | This expression gets evaluated once per hour. It will set F<sunday.png> as |
76 | This expression gets evaluated once per hour. It will set F<sunday.png> as |
75 | background on sundays, and F<weekday.png> on all other days. |
77 | background on Sundays, and F<weekday.png> on all other days. |
76 | |
78 | |
77 | Fortunately, we expect that most expressions will be much simpler, with |
79 | Fortunately, we expect that most expressions will be much simpler, with |
78 | little Perl knowledge needed. |
80 | little Perl knowledge needed. |
79 | |
81 | |
80 | Basically, you always start with a function that "generates" an image |
82 | Basically, you always start with a function that "generates" an image |
… | |
… | |
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 | TODO |
120 | Other effects than scalign are also readily available, for exmaple, you can |
|
|
121 | tile the image to fill the whole window, instead of resizing it: |
118 | |
122 | |
|
|
123 | tile load "$HOME/mypic.png" |
|
|
124 | |
|
|
125 | In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator |
|
|
126 | is kind of superfluous. |
|
|
127 | |
|
|
128 | Another common effect is to mirror the image, so that the same edges touch: |
|
|
129 | |
|
|
130 | mirror load "$HOME/mypic.png" |
|
|
131 | |
|
|
132 | This is also a typical background expression: |
|
|
133 | |
|
|
134 | rootalign root |
|
|
135 | |
|
|
136 | It first takes a snapshot of the screen background image, and then |
|
|
137 | moves it to the upper left corner of the screen - the result is |
|
|
138 | pseudo-transparency, as the image seems to be static while the window is |
|
|
139 | moved around. |
|
|
140 | |
119 | =head3 CYCLES AND CACHING |
141 | =head2 CYCLES AND CACHING |
120 | |
142 | |
121 | TODO |
143 | As has been mentioned before, the expression might be evaluated multiple |
122 | |
|
|
123 | Each time the expression is reevaluated, a new cycle is said to have begun. Many operators |
144 | times. Each time the expression is reevaluated, a new cycle is said to |
124 | cache their results till the next cycle. For example |
145 | have begun. Many operators cache their results till the next cycle. |
125 | |
146 | |
|
|
147 | For example, the C<load> operator keeps a copy of the image. If it is |
|
|
148 | asked to load the same image on the next cycle it will not load it again, |
|
|
149 | but return the cached copy. |
|
|
150 | |
|
|
151 | This only works for one cycle though, so as long as you load the same |
|
|
152 | image every time, it will always be cached, but when you load a different |
|
|
153 | image, it will forget about the first one. |
|
|
154 | |
|
|
155 | This allows you to either speed things up by keeping multiple images in |
|
|
156 | memory, or comserve memory by loading images more often. |
|
|
157 | |
|
|
158 | For example, you can keep two images in memory and use a random one like |
|
|
159 | this: |
|
|
160 | |
|
|
161 | my $img1 = load "img1.png"; |
|
|
162 | my $img2 = load "img2.png"; |
|
|
163 | (0.5 > rand) ? $img1 : $img2 |
|
|
164 | |
|
|
165 | Since both images are "loaded" every time the expression is evaluated, |
|
|
166 | they 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 | |
|
|
172 | Here, a path is selected randomly, and load is only called for one image, |
|
|
173 | so keeps only one image in memory. If, on the next evaluation, luck |
|
|
174 | decides 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 | |
… | |
… | |
142 | replaces the background of the character area. |
192 | replaces the background of the character area. |
143 | |
193 | |
144 | =back |
194 | =back |
145 | |
195 | |
146 | =cut |
196 | =cut |
147 | |
|
|
148 | our $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 | |
161 | our $HOME; |
198 | our $HOME; |
162 | our ($self, $old, $new); |
199 | our ($self, $old, $new); |
163 | our ($x, $y, $w, $h); |
200 | our ($x, $y, $w, $h); |
164 | |
201 | |
… | |
… | |
166 | our $MIN_INTERVAL = 1/100; |
203 | our $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 | |
173 | 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 |
174 | 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 |
175 | points to get an image you can play with. |
214 | points to get an image you can play with. |
… | |
… | |
211 | =item solid $width, $height, $colour |
250 | =item solid $width, $height, $colour |
212 | |
251 | |
213 | Creates a new image and completely fills it with the given colour. The |
252 | Creates a new image and completely fills it with the given colour. The |
214 | image is set to tiling mode. |
253 | image is set to tiling mode. |
215 | |
254 | |
216 | If <$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 |
217 | useful for solid backgrounds or for use in filtering effects. |
256 | useful 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 | |
233 | 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 |
234 | window dimensions. Most of them make your expression sensitive to some |
274 | varies. Most of them make your expression sensitive to some events, for |
235 | events, for example using C<TW> (terminal width) means your expression is |
275 | example using C<TW> (terminal width) means your expression is evaluated |
236 | evaluated again when the terminal is resized. |
276 | again 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 | |
451 | Scales the image by the given percentages in horizontal |
491 | Scales 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 | |
454 | 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. |
455 | |
495 | |
456 | 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 |
457 | keeping aspect. |
497 | keeping aspect. |
458 | |
498 | |
459 | =item resize $width, $height, $img |
499 | =item resize $width, $height, $img |
460 | |
500 | |
461 | Resizes the image to exactly C<$width> times C<$height> pixels. |
501 | Resizes 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 | |
|
|
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 |
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 | |
482 | 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 |
… | |
… | |
563 | =item blur $radius_horz, $radius_vert, $img |
631 | =item blur $radius_horz, $radius_vert, $img |
564 | |
632 | |
565 | Gaussian-blurs the image with (roughly) C<$radius> pixel radius. The radii |
633 | Gaussian-blurs the image with (roughly) C<$radius> pixel radius. The radii |
566 | can also be specified separately. |
634 | can also be specified separately. |
567 | |
635 | |
|
|
636 | Blurring is often I<very> slow, at least compared or other |
|
|
637 | operators. Larger blur radii are slower than smaller ones, too, so if you |
|
|
638 | don't want to freeze your screen for long times, start experimenting with |
|
|
639 | low values for radius (<5). |
|
|
640 | |
568 | =cut |
641 | =cut |
569 | |
642 | |
570 | sub blur($$;$) { |
643 | sub blur($$;$) { |
571 | my $img = pop; |
644 | my $img = pop; |
572 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
645 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
573 | } |
646 | } |
574 | |
647 | |
575 | =item rotate $new_width, $new_height, $center_x, $center_y, $degrees |
648 | =item rotate $new_width, $new_height, $center_x, $center_y, $degrees |
576 | |
649 | |
577 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
650 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
578 | pointer at C<$center_x> and C<$center_y> (specified as percentage of image |
651 | pointer at C<$center_x> and C<$center_y> (specified as factor of image |
579 | width/height), generating a new image with width C<$new_width> and height |
652 | width/height), generating a new image with width C<$new_width> and height |
580 | C<$new_height>. |
653 | C<$new_height>. |
581 | |
654 | |
582 | #TODO# new width, height, maybe more operators? |
655 | #TODO# new width, height, maybe more operators? |
583 | |
656 | |
… | |
… | |
588 | sub rotate($$$$$$) { |
661 | sub rotate($$$$$$) { |
589 | my $img = pop; |
662 | my $img = pop; |
590 | $img->rotate ( |
663 | $img->rotate ( |
591 | $_[0], |
664 | $_[0], |
592 | $_[1], |
665 | $_[1], |
593 | $_[2] * $img->w * .01, |
666 | $_[2] * $img->w, |
594 | $_[3] * $img->h * .01, |
667 | $_[3] * $img->h, |
595 | $_[4] * (3.14159265 / 180), |
668 | $_[4] * (3.14159265 / 180), |
596 | ) |
669 | ) |
597 | } |
670 | } |
598 | |
671 | |
599 | =back |
672 | =back |