… | |
… | |
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 | rxvt -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 | |
|
|
19 | This extension manages the terminal background by creating a picture that |
|
|
20 | is behind the text, replacing the normal background colour. |
|
|
21 | |
|
|
22 | It does so by evaluating a Perl expression that I<calculates> the image on |
|
|
23 | the fly, for example, by grabbing the root background or loading a file. |
|
|
24 | |
|
|
25 | While the full power of Perl is available, the operators have been design |
|
|
26 | to be as simple as possible. |
|
|
27 | |
|
|
28 | For example, to load an image and scale it to the window size, you would |
|
|
29 | use: |
|
|
30 | |
|
|
31 | urxvt --background-expr 'scale load "/path/to/mybg.png"' |
|
|
32 | |
|
|
33 | Or specified as a X resource: |
|
|
34 | |
|
|
35 | URxvt.background-expr: scale load "/path/to/mybg.png" |
|
|
36 | |
|
|
37 | =head1 THEORY OF OPERATION |
|
|
38 | |
|
|
39 | At startup, just before the window is mapped for the first time, the |
|
|
40 | expression is evaluated and must yield an image. The image is then |
|
|
41 | extended as necessary to cover the whole terminal window, and is set as a |
|
|
42 | background pixmap. |
|
|
43 | |
|
|
44 | If the image contains an alpha channel, then it will be used as-is in |
|
|
45 | visuals that support alpha channels (for example, for a compositing |
|
|
46 | manager). In other visuals, the terminal background colour will be used to |
|
|
47 | replace any transparency. |
|
|
48 | |
|
|
49 | When the expression relies, directly or indirectly, on the window size, |
|
|
50 | position, the root pixmap, or a timer, then it will be remembered. If not, |
|
|
51 | then it will be removed. |
|
|
52 | |
|
|
53 | If any of the parameters that the expression relies on changes (when the |
|
|
54 | window is moved or resized, its position or size changes; when the root |
|
|
55 | pixmap is replaced by another one the root background changes; or when the |
|
|
56 | timer elapses), then the expression will be evaluated again. |
|
|
57 | |
|
|
58 | For example, an expression such as C<scale load "$HOME/mybg.png"> scales the |
|
|
59 | image to the window size, so it relies on the window size and will |
|
|
60 | be reevaluated each time it is changed, but not when it moves for |
|
|
61 | example. That ensures that the picture always fills the terminal, even |
|
|
62 | after it's size changes. |
|
|
63 | |
|
|
64 | =head2 EXPRESSIONS |
|
|
65 | |
|
|
66 | Expressions are normal Perl expressions, in fact, they are Perl blocks - |
|
|
67 | which means you could use multiple lines and statements: |
|
|
68 | |
|
|
69 | again 3600; |
|
|
70 | if (localtime now)[6]) { |
|
|
71 | return scale load "$HOME/weekday.png"; |
|
|
72 | } else { |
|
|
73 | return scale load "$HOME/sunday.png"; |
|
|
74 | } |
|
|
75 | |
|
|
76 | This expression gets evaluated once per hour. It will set F<sunday.png> as |
|
|
77 | background on Sundays, and F<weekday.png> on all other days. |
|
|
78 | |
|
|
79 | Fortunately, we expect that most expressions will be much simpler, with |
|
|
80 | little Perl knowledge needed. |
|
|
81 | |
|
|
82 | Basically, you always start with a function that "generates" an image |
|
|
83 | object, such as C<load>, which loads an image from disk, or C<root>, which |
|
|
84 | returns the root window background image: |
|
|
85 | |
|
|
86 | load "$HOME/mypic.png" |
|
|
87 | |
|
|
88 | The path is usually specified as a quoted string (the exact rules can be |
|
|
89 | found in the L<perlop> manpage). The F<$HOME> at the beginning of the |
|
|
90 | string is expanded to the home directory. |
|
|
91 | |
|
|
92 | Then you prepend one or more modifiers or filtering expressions, such as |
|
|
93 | C<scale>: |
|
|
94 | |
|
|
95 | scale load "$HOME/mypic.png" |
|
|
96 | |
|
|
97 | Just like a mathematical expression with functions, you should read these |
|
|
98 | expressions from right to left, as the C<load> is evaluated first, and |
|
|
99 | its result becomes the argument to the C<scale> function. |
|
|
100 | |
|
|
101 | Many operators also allow some parameters preceding the input image |
|
|
102 | that modify its behaviour. For example, C<scale> without any additional |
|
|
103 | arguments scales the image to size of the terminal window. If you specify |
|
|
104 | an additional argument, it uses it as a percentage: |
|
|
105 | |
|
|
106 | scale 200, load "$HOME/mypic.png" |
|
|
107 | |
|
|
108 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
|
|
109 | has now two arguments, the C<200> and the C<load> expression, while |
|
|
110 | C<load> only has one argument. Arguments are separated from each other by |
|
|
111 | commas. |
|
|
112 | |
|
|
113 | Scale also accepts two arguments, which are then separate factors for both |
|
|
114 | horizontal and vertical dimensions. For example, this halves the image |
|
|
115 | width and doubles the image height: |
|
|
116 | |
|
|
117 | scale 50, 200, load "$HOME/mypic.png" |
|
|
118 | |
|
|
119 | Other effects than scalign are also readily available, for exmaple, you can |
|
|
120 | tile the image to fill the whole window, instead of resizing it: |
|
|
121 | |
|
|
122 | tile load "$HOME/mypic.png" |
|
|
123 | |
|
|
124 | In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator |
|
|
125 | is kind of superfluous. |
|
|
126 | |
|
|
127 | Another common effect is to mirror the image, so that the same edges touch: |
|
|
128 | |
|
|
129 | mirror load "$HOME/mypic.png" |
|
|
130 | |
|
|
131 | This is also a typical background expression: |
|
|
132 | |
|
|
133 | rootalign root |
|
|
134 | |
|
|
135 | It first takes a snapshot of the screen background image, and then |
|
|
136 | moves it to the upper left corner of the screen - the result is |
|
|
137 | pseudo-transparency, as the image seems to be static while the window is |
|
|
138 | moved around. |
|
|
139 | |
|
|
140 | =head2 CYCLES AND CACHING |
|
|
141 | |
|
|
142 | As has been mentioned before, the expression might be evaluated multiple |
|
|
143 | times. Each time the expression is reevaluated, a new cycle is said to |
|
|
144 | have begun. Many operators cache their results till the next cycle. |
|
|
145 | |
|
|
146 | For example, the C<load> operator keeps a copy of the image. If it is |
|
|
147 | asked to load the same image on the next cycle it will not load it again, |
|
|
148 | but return the cached copy. |
|
|
149 | |
|
|
150 | This only works for one cycle though, so as long as you load the same |
|
|
151 | image every time, it will always be cached, but when you load a different |
|
|
152 | image, it will forget about the first one. |
|
|
153 | |
|
|
154 | This allows you to either speed things up by keeping multiple images in |
|
|
155 | memory, or comserve memory by loading images more often. |
|
|
156 | |
|
|
157 | For example, you can keep two images in memory and use a random one like |
|
|
158 | this: |
|
|
159 | |
|
|
160 | my $img1 = load "img1.png"; |
|
|
161 | my $img2 = load "img2.png"; |
|
|
162 | (0.5 > rand) ? $img1 : $img2 |
|
|
163 | |
|
|
164 | Since both images are "loaded" every time the expression is evaluated, |
|
|
165 | they are always kept in memory. Contrast this version: |
|
|
166 | |
|
|
167 | my $path1 = "img1.png"; |
|
|
168 | my $path2 = "img2.png"; |
|
|
169 | load ((0.5 > rand) ? $path1 : $path2) |
|
|
170 | |
|
|
171 | Here, a path is selected randomly, and load is only called for one image, |
|
|
172 | so keeps only one image in memory. If, on the next evaluation, luck |
|
|
173 | decides to use the other path, then it will have to load that image again. |
|
|
174 | |
17 | =head2 REFERENCE |
175 | =head1 REFERENCE |
18 | |
176 | |
19 | =cut |
177 | =head2 COMMAND LINE SWITCHES |
20 | |
178 | |
21 | our $EXPR; |
179 | =over 4 |
22 | #$EXPR = 'move W * 0.1, -H * 0.1, resize W * 0.5, H * 0.5, repeat_none load "opensource.png"'; |
|
|
23 | $EXPR = 'move -TX, -TY, load "argb.png"'; |
|
|
24 | #$EXPR = ' |
|
|
25 | # rotate W, H, 50, 50, counter 1/59.95, repeat_mirror, |
|
|
26 | # clip X, Y, W, H, repeat_mirror, |
|
|
27 | # load "/root/pix/das_fette_schwein.jpg" |
|
|
28 | #'; |
|
|
29 | #$EXPR = 'solid "red"'; |
|
|
30 | #$EXPR = 'blur root, 10, 10' |
|
|
31 | #$EXPR = 'blur move (root, -x, -y), 5, 5' |
|
|
32 | #resize load "/root/pix/das_fette_schwein.jpg", w, h |
|
|
33 | |
180 | |
|
|
181 | =item --background-expr perl-expression |
|
|
182 | |
|
|
183 | Specifies the Perl expression to evaluate. |
|
|
184 | |
|
|
185 | =item --background-border |
|
|
186 | |
|
|
187 | By default, the expression creates an image that fills the full window, |
|
|
188 | overwriting borders and any other areas, such as the scrollbar. |
|
|
189 | |
|
|
190 | Specifying this flag changes the behaviour, so that the image only |
|
|
191 | replaces the background of the character area. |
|
|
192 | |
|
|
193 | =back |
|
|
194 | |
|
|
195 | =cut |
|
|
196 | |
|
|
197 | our $HOME; |
34 | our ($self, $old, $new); |
198 | our ($self, $old, $new); |
35 | our ($x, $y, $w, $h); |
199 | our ($x, $y, $w, $h); |
36 | |
200 | |
37 | # enforce at least this interval between updates |
201 | # enforce at least this interval between updates |
38 | our $MIN_INTERVAL = 1/100; |
202 | our $MIN_INTERVAL = 1/100; |
… | |
… | |
83 | =item solid $width, $height, $colour |
247 | =item solid $width, $height, $colour |
84 | |
248 | |
85 | Creates a new image and completely fills it with the given colour. The |
249 | Creates a new image and completely fills it with the given colour. The |
86 | image is set to tiling mode. |
250 | image is set to tiling mode. |
87 | |
251 | |
88 | If <$width> and C<$height> are omitted, it creates a 1x1 image, which is |
252 | If C<$width> and C<$height> are omitted, it creates a 1x1 image, which is |
89 | useful for solid backgrounds or for use in filtering effects. |
253 | useful for solid backgrounds or for use in filtering effects. |
90 | |
254 | |
91 | =cut |
255 | =cut |
92 | |
256 | |
93 | sub solid($$;$) { |
257 | sub solid($$;$) { |
… | |
… | |
100 | |
264 | |
101 | =back |
265 | =back |
102 | |
266 | |
103 | =head2 VARIABLES |
267 | =head2 VARIABLES |
104 | |
268 | |
105 | The following functions provide variable data such as the terminal |
269 | The following functions provide variable data such as the terminal window |
|
|
270 | dimensions. They are not (Perl-) variables, they jsut return stuff that |
106 | window dimensions. Most of them make your expression sensitive to some |
271 | varies. Most of them make your expression sensitive to some events, for |
107 | events, for example using C<TW> (terminal width) means your expression is |
272 | example using C<TW> (terminal width) means your expression is evaluated |
108 | evaluated again when the terminal is resized. |
273 | again when the terminal is resized. |
109 | |
274 | |
110 | =over 4 |
275 | =over 4 |
111 | |
276 | |
112 | =item TX |
277 | =item TX |
113 | |
278 | |
… | |
… | |
160 | |
325 | |
161 | When this function is used the expression will be reevaluated again in |
326 | When this function is used the expression will be reevaluated again in |
162 | C<$seconds> seconds. |
327 | C<$seconds> seconds. |
163 | |
328 | |
164 | Example: load some image and rotate it according to the time of day (as if it were |
329 | Example: load some image and rotate it according to the time of day (as if it were |
165 | the hour pointer of a clock). update this image every minute. |
330 | the hour pointer of a clock). Update this image every minute. |
166 | |
331 | |
167 | again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
332 | again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
168 | |
333 | |
169 | =item counter $seconds |
334 | =item counter $seconds |
170 | |
335 | |
… | |
… | |
209 | Similar to tile, but reflects the image each time it uses a new copy, so |
374 | Similar to tile, but reflects the image each time it uses a new copy, so |
210 | that top edges always touch top edges, right edges always touch right |
375 | that top edges always touch top edges, right edges always touch right |
211 | edges and so on (with normal tiling, left edges always touch right edges |
376 | edges and so on (with normal tiling, left edges always touch right edges |
212 | and top always touch bottom edges). |
377 | and top always touch bottom edges). |
213 | |
378 | |
214 | Exmaple: load an image and mirror it over the background, avoiding sharp |
379 | Example: load an image and mirror it over the background, avoiding sharp |
215 | edges at the image borders at the expense of mirroring the image itself |
380 | edges at the image borders at the expense of mirroring the image itself |
216 | |
381 | |
217 | mirror load "mybg.png" |
382 | mirror load "mybg.png" |
218 | |
383 | |
219 | =item pad $img |
384 | =item pad $img |
… | |
… | |
221 | Takes an image and modifies it so that all pixels outside the image area |
386 | Takes an image and modifies it so that all pixels outside the image area |
222 | become transparent. This mode is most useful when you want to place an |
387 | become transparent. This mode is most useful when you want to place an |
223 | image over another image or the background colour while leaving all |
388 | image over another image or the background colour while leaving all |
224 | background pixels outside the image unchanged. |
389 | background pixels outside the image unchanged. |
225 | |
390 | |
226 | Example: load an image and display it in the upper left corner. The rets |
391 | Example: load an image and display it in the upper left corner. The rest |
227 | of the space is left "empty" (transparent or wahtever your compisotr does |
392 | of the space is left "empty" (transparent or wahtever your compisotr does |
228 | in alpha mode, else background colour). |
393 | in alpha mode, else background colour). |
229 | |
394 | |
230 | pad load "mybg.png" |
395 | pad load "mybg.png" |
231 | |
396 | |
… | |
… | |
347 | sub resize($$$) { |
512 | sub resize($$$) { |
348 | my $img = pop; |
513 | my $img = pop; |
349 | $img->scale ($_[0], $_[1]) |
514 | $img->scale ($_[0], $_[1]) |
350 | } |
515 | } |
351 | |
516 | |
|
|
517 | =item move $dx, $dy, $img |
|
|
518 | |
|
|
519 | Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in |
|
|
520 | the vertical. |
|
|
521 | |
|
|
522 | Example: move the image right by 20 pixels and down by 30. |
|
|
523 | |
|
|
524 | move 20, 30, ... |
|
|
525 | |
|
|
526 | =item rootalign $img |
|
|
527 | |
|
|
528 | Moves the image so that it appears glued to the screen as opposed to the |
|
|
529 | window. This gives the illusion of a larger area behind the window. It is |
|
|
530 | exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the |
|
|
531 | top left of the screen. |
|
|
532 | |
|
|
533 | Example: load a background image, put it in mirror mode and root align it. |
|
|
534 | |
|
|
535 | rootalign mirror load "mybg.png" |
|
|
536 | |
|
|
537 | Example: take the screen background and align it, giving the illusion of |
|
|
538 | transparency as long as the window isn't in front of other windows. |
|
|
539 | |
|
|
540 | rootalign root |
|
|
541 | |
|
|
542 | =cut |
|
|
543 | |
352 | sub move($$;$) { |
544 | sub move($$;$) { |
353 | my $img = pop->clone; |
545 | my $img = pop->clone; |
354 | $img->move ($_[0], $_[1]); |
546 | $img->move ($_[0], $_[1]); |
355 | $img |
547 | $img |
356 | } |
548 | } |
|
|
549 | |
|
|
550 | sub rootalign($) { |
|
|
551 | move -TX, -TY, $_[0] |
|
|
552 | } |
|
|
553 | |
|
|
554 | =item contrast $factor, $img |
|
|
555 | |
|
|
556 | =item contrast $r, $g, $b, $img |
|
|
557 | |
|
|
558 | =item contrast $r, $g, $b, $a, $img |
|
|
559 | |
|
|
560 | Adjusts the I<contrast> of an image. |
|
|
561 | |
|
|
562 | #TODO# |
|
|
563 | |
|
|
564 | =item brightness $factor, $img |
|
|
565 | |
|
|
566 | =item brightness $r, $g, $b, $img |
|
|
567 | |
|
|
568 | =item brightness $r, $g, $b, $a, $img |
|
|
569 | |
|
|
570 | Adjusts the brightness of an image. |
|
|
571 | |
|
|
572 | =cut |
|
|
573 | |
|
|
574 | sub contrast($$;$$;$) { |
|
|
575 | my $img = pop; |
|
|
576 | my ($r, $g, $b, $a) = @_; |
|
|
577 | |
|
|
578 | ($g, $b) = ($r, $r) if @_ < 4; |
|
|
579 | $a = 1 if @_ < 5; |
|
|
580 | |
|
|
581 | $img = $img->clone; |
|
|
582 | $img->contrast ($r, $g, $b, $a); |
|
|
583 | $img |
|
|
584 | } |
|
|
585 | |
|
|
586 | sub brightness($$;$$;$) { |
|
|
587 | my $img = pop; |
|
|
588 | my ($r, $g, $b, $a) = @_; |
|
|
589 | |
|
|
590 | ($g, $b) = ($r, $r) if @_ < 4; |
|
|
591 | $a = 1 if @_ < 5; |
|
|
592 | |
|
|
593 | $img = $img->clone; |
|
|
594 | $img->brightness ($r, $g, $b, $a); |
|
|
595 | $img |
|
|
596 | } |
|
|
597 | |
|
|
598 | =item blur $radius, $img |
|
|
599 | |
|
|
600 | =item blur $radius_horz, $radius_vert, $img |
|
|
601 | |
|
|
602 | Gaussian-blurs the image with (roughly) C<$radius> pixel radius. The radii |
|
|
603 | can also be specified separately. |
|
|
604 | |
|
|
605 | Blurring is often I<very> slow, at least compared or other |
|
|
606 | operators. Larger blur radii are slower than smaller ones, too, so if you |
|
|
607 | don't want to freeze your screen for long times, start experimenting with |
|
|
608 | low values for radius (<5). |
|
|
609 | |
|
|
610 | =cut |
|
|
611 | |
|
|
612 | sub blur($$;$) { |
|
|
613 | my $img = pop; |
|
|
614 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
|
|
615 | } |
|
|
616 | |
|
|
617 | =item rotate $new_width, $new_height, $center_x, $center_y, $degrees |
|
|
618 | |
|
|
619 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
|
|
620 | pointer at C<$center_x> and C<$center_y> (specified as percentage of image |
|
|
621 | width/height), generating a new image with width C<$new_width> and height |
|
|
622 | C<$new_height>. |
|
|
623 | |
|
|
624 | #TODO# new width, height, maybe more operators? |
|
|
625 | |
|
|
626 | Example: rotate the image by 90 degrees |
|
|
627 | |
|
|
628 | =cut |
357 | |
629 | |
358 | sub rotate($$$$$$) { |
630 | sub rotate($$$$$$) { |
359 | my $img = pop; |
631 | my $img = pop; |
360 | $img->rotate ( |
632 | $img->rotate ( |
361 | $_[0], |
633 | $_[0], |
… | |
… | |
364 | $_[3] * $img->h * .01, |
636 | $_[3] * $img->h * .01, |
365 | $_[4] * (3.14159265 / 180), |
637 | $_[4] * (3.14159265 / 180), |
366 | ) |
638 | ) |
367 | } |
639 | } |
368 | |
640 | |
369 | sub blur($$;$) { |
|
|
370 | my $img = pop; |
|
|
371 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
|
|
372 | } |
|
|
373 | |
|
|
374 | sub contrast($$;$$;$) { |
|
|
375 | my $img = pop; |
|
|
376 | my ($r, $g, $b, $a) = @_; |
|
|
377 | |
|
|
378 | ($g, $b) = ($r, $r) if @_ < 4; |
|
|
379 | $a = 1 if @_ < 5; |
|
|
380 | |
|
|
381 | $img = $img->clone; |
|
|
382 | $img->contrast ($r, $g, $b, $a); |
|
|
383 | $img |
|
|
384 | } |
|
|
385 | |
|
|
386 | sub brightness($$;$$;$) { |
|
|
387 | my $img = pop; |
|
|
388 | my ($r, $g, $b, $a) = @_; |
|
|
389 | |
|
|
390 | ($g, $b) = ($r, $r) if @_ < 4; |
|
|
391 | $a = 1 if @_ < 5; |
|
|
392 | |
|
|
393 | $img = $img->clone; |
|
|
394 | $img->brightness ($r, $g, $b, $a); |
|
|
395 | $img |
|
|
396 | } |
|
|
397 | |
|
|
398 | =back |
641 | =back |
399 | |
642 | |
400 | =cut |
643 | =cut |
401 | |
644 | |
402 | } |
645 | } |
… | |
… | |
432 | |
675 | |
433 | # set environment to evaluate user expression |
676 | # set environment to evaluate user expression |
434 | |
677 | |
435 | local $self = $arg_self; |
678 | local $self = $arg_self; |
436 | |
679 | |
|
|
680 | local $HOME = $ENV{HOME}; |
437 | local $old = $self->{state}; |
681 | local $old = $self->{state}; |
438 | local $new = my $state = $self->{state} = {}; |
682 | local $new = my $state = $self->{state} = {}; |
439 | |
683 | |
440 | ($x, $y, $w, $h) = |
684 | ($x, $y, $w, $h) = |
441 | $self->background_geometry ($self->{border}); |
685 | $self->background_geometry ($self->{border}); |