… | |
… | |
7 | |
7 | |
8 | =head1 background - manage terminal background |
8 | =head1 background - manage terminal background |
9 | |
9 | |
10 | =head2 SYNOPSIS |
10 | =head2 SYNOPSIS |
11 | |
11 | |
12 | rxvt -background-expr 'background expression' |
12 | urxvt --background-expr 'background expression' |
13 | -background-border |
13 | --background-border |
14 | |
14 | |
15 | =head2 DESCRIPTION |
15 | =head2 DESCRIPTION |
16 | |
16 | |
|
|
17 | This extension manages the terminal background by creating a picture that |
|
|
18 | is behind the text, replacing the normal background colour. |
|
|
19 | |
|
|
20 | It does so by evaluating a Perl expression that I<calculates> the image on |
|
|
21 | the fly, for example, by grabbing the root background or loading a file. |
|
|
22 | |
|
|
23 | While the full power of Perl is available, the operators have been design |
|
|
24 | to be as simple as possible. |
|
|
25 | |
|
|
26 | For example, to load an image and scale it to the window size, you would |
|
|
27 | use: |
|
|
28 | |
|
|
29 | urxvt --background-expr 'scale load "/path/to/mybg.png"' |
|
|
30 | |
|
|
31 | Or specified as a X resource: |
|
|
32 | |
|
|
33 | URxvt.background-expr: scale load "/path/to/mybg.png" |
|
|
34 | |
|
|
35 | =head2 THEORY OF OPERATION |
|
|
36 | |
|
|
37 | 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 |
|
|
39 | extended as necessary to cover the whole terminal window, and is set as a |
|
|
40 | background pixmap. |
|
|
41 | |
|
|
42 | If the image contains an alpha channel, then it will be used as-is in |
|
|
43 | visuals that support alpha channels (for example, for a compositing |
|
|
44 | manager). In other visuals, the terminal background colour will be used to |
|
|
45 | replace any transparency. |
|
|
46 | |
|
|
47 | When the expression relies, directly or indirectly, on the window size, |
|
|
48 | position, the root pixmap, or a timer, then it will be remembered. If not, |
|
|
49 | then it will be removed. |
|
|
50 | |
|
|
51 | If any of the parameters that the expression relies on changes (when the |
|
|
52 | window is moved or resized, its position or size changes; when the root |
|
|
53 | pixmap is replaced by another one the root background changes; or when the |
|
|
54 | timer elapses), then the expression will be evaluated again. |
|
|
55 | |
|
|
56 | For example, an expression such as C<scale load "$HOME/mybg.png"> scales the |
|
|
57 | 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 |
|
|
59 | example. That ensures that the picture always fills the terminal, even |
|
|
60 | after it's size changes. |
|
|
61 | |
|
|
62 | =head3 EXPRESSIONS |
|
|
63 | |
|
|
64 | Expressions are normal Perl expressions, in fact, they are Perl blocks - |
|
|
65 | which means you could use multiple lines and statements: |
|
|
66 | |
|
|
67 | again 3600; |
|
|
68 | if (localtime now)[6]) { |
|
|
69 | return scale load "$HOME/weekday.png"; |
|
|
70 | } else { |
|
|
71 | return scale load "$HOME/sunday.png"; |
|
|
72 | } |
|
|
73 | |
|
|
74 | 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. |
|
|
76 | |
|
|
77 | Fortunately, we expect that most expressions will be much simpler, with |
|
|
78 | little Perl knowledge needed. |
|
|
79 | |
|
|
80 | Basically, you always start with a function that "generates" an image |
|
|
81 | object, such as C<load>, which loads an image from disk, or C<root>, which |
|
|
82 | returns the root window background image: |
|
|
83 | |
|
|
84 | load "$HOME/mypic.png" |
|
|
85 | |
|
|
86 | The path is usually specified as a quoted string (the exact rules can be |
|
|
87 | found in the L<perlop> manpage). The F<$HOME> at the beginning of the |
|
|
88 | string is expanded to the home directory. |
|
|
89 | |
|
|
90 | Then you prepend one or more modifiers or filtering expressions, such as |
|
|
91 | C<scale>: |
|
|
92 | |
|
|
93 | scale load "$HOME/mypic.png" |
|
|
94 | |
|
|
95 | Just like a mathematical expression with functions, you should read these |
|
|
96 | expressions from right to left, as the C<load> is evaluated first, and |
|
|
97 | its result becomes the argument to the C<scale> function. |
|
|
98 | |
|
|
99 | Many operators also allow some parameters preceding the input image |
|
|
100 | 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 |
|
|
102 | an additional argument, it uses it as a percentage: |
|
|
103 | |
|
|
104 | scale 200, load "$HOME/mypic.png" |
|
|
105 | |
|
|
106 | 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 |
|
|
108 | C<load> only has one argument. Arguments are separated from each other by |
|
|
109 | commas. |
|
|
110 | |
|
|
111 | Scale also accepts two arguments, which are then separate factors for both |
|
|
112 | horizontal and vertical dimensions. For example, this halves the image |
|
|
113 | width and doubles the image height: |
|
|
114 | |
|
|
115 | scale 50, 200, load "$HOME/mypic.png" |
|
|
116 | |
|
|
117 | TODO |
|
|
118 | |
|
|
119 | =head3 CYCLES AND CACHING |
|
|
120 | |
|
|
121 | TODO |
|
|
122 | |
|
|
123 | Each time the expression is reevaluated, a new cycle is said to have begun. Many operators |
|
|
124 | cache their results till the next cycle. For example |
|
|
125 | |
17 | =head2 REFERENCE |
126 | =head2 REFERENCE |
18 | |
127 | |
19 | =cut |
128 | =head3 COMMAND LINE SWITCHES |
20 | |
129 | |
|
|
130 | =over 4 |
|
|
131 | |
|
|
132 | =item --background-expr perl-expression |
|
|
133 | |
|
|
134 | Specifies the Perl expression to evaluate. |
|
|
135 | |
|
|
136 | =item --background-border |
|
|
137 | |
|
|
138 | By default, the expression creates an image that fills the full window, |
|
|
139 | overwriting borders and any other areas, such as the scrollbar. |
|
|
140 | |
|
|
141 | Specifying this flag changes the behaviour, so that the image only |
|
|
142 | replaces the background of the character area. |
|
|
143 | |
|
|
144 | =back |
|
|
145 | |
|
|
146 | =cut |
|
|
147 | |
21 | our $EXPR; |
148 | our $EXPR;#d# |
22 | #$EXPR = 'move W * 0.1, -H * 0.1, resize W * 0.5, H * 0.5, repeat_none load "opensource.png"'; |
149 | #$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"'; |
150 | $EXPR = 'move -TX, -TY, load "argb.png"'; |
24 | #$EXPR = ' |
151 | #$EXPR = ' |
25 | # rotate W, H, 50, 50, counter 1/59.95, repeat_mirror, |
152 | # rotate W, H, 50, 50, counter 1/59.95, repeat_mirror, |
26 | # clip X, Y, W, H, repeat_mirror, |
153 | # clip X, Y, W, H, repeat_mirror, |
… | |
… | |
29 | #$EXPR = 'solid "red"'; |
156 | #$EXPR = 'solid "red"'; |
30 | #$EXPR = 'blur root, 10, 10' |
157 | #$EXPR = 'blur root, 10, 10' |
31 | #$EXPR = 'blur move (root, -x, -y), 5, 5' |
158 | #$EXPR = 'blur move (root, -x, -y), 5, 5' |
32 | #resize load "/root/pix/das_fette_schwein.jpg", w, h |
159 | #resize load "/root/pix/das_fette_schwein.jpg", w, h |
33 | |
160 | |
|
|
161 | our $HOME; |
34 | our ($self, $old, $new); |
162 | our ($self, $old, $new); |
35 | our ($x, $y, $w, $h); |
163 | our ($x, $y, $w, $h); |
36 | |
164 | |
37 | # enforce at least this interval between updates |
165 | # enforce at least this interval between updates |
38 | our $MIN_INTERVAL = 1/100; |
166 | our $MIN_INTERVAL = 1/100; |
… | |
… | |
160 | |
288 | |
161 | When this function is used the expression will be reevaluated again in |
289 | When this function is used the expression will be reevaluated again in |
162 | C<$seconds> seconds. |
290 | C<$seconds> seconds. |
163 | |
291 | |
164 | Example: load some image and rotate it according to the time of day (as if it were |
292 | 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. |
293 | the hour pointer of a clock). Update this image every minute. |
166 | |
294 | |
167 | again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
295 | again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
168 | |
296 | |
169 | =item counter $seconds |
297 | =item counter $seconds |
170 | |
298 | |
… | |
… | |
209 | Similar to tile, but reflects the image each time it uses a new copy, so |
337 | 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 |
338 | 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 |
339 | edges and so on (with normal tiling, left edges always touch right edges |
212 | and top always touch bottom edges). |
340 | and top always touch bottom edges). |
213 | |
341 | |
214 | Exmaple: load an image and mirror it over the background, avoiding sharp |
342 | 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 |
343 | edges at the image borders at the expense of mirroring the image itself |
216 | |
344 | |
217 | mirror load "mybg.png" |
345 | mirror load "mybg.png" |
218 | |
346 | |
219 | =item pad $img |
347 | =item pad $img |
… | |
… | |
221 | Takes an image and modifies it so that all pixels outside the image area |
349 | 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 |
350 | 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 |
351 | image over another image or the background colour while leaving all |
224 | background pixels outside the image unchanged. |
352 | background pixels outside the image unchanged. |
225 | |
353 | |
226 | Example: load an image and display it in the upper left corner. The rets |
354 | 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 |
355 | of the space is left "empty" (transparent or wahtever your compisotr does |
228 | in alpha mode, else background colour). |
356 | in alpha mode, else background colour). |
229 | |
357 | |
230 | pad load "mybg.png" |
358 | pad load "mybg.png" |
231 | |
359 | |
… | |
… | |
347 | sub resize($$$) { |
475 | sub resize($$$) { |
348 | my $img = pop; |
476 | my $img = pop; |
349 | $img->scale ($_[0], $_[1]) |
477 | $img->scale ($_[0], $_[1]) |
350 | } |
478 | } |
351 | |
479 | |
|
|
480 | =item move $dx, $dy, $img |
|
|
481 | |
|
|
482 | Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in |
|
|
483 | the vertical. |
|
|
484 | |
|
|
485 | Example: move the image right by 20 pixels and down by 30. |
|
|
486 | |
|
|
487 | move 20, 30, ... |
|
|
488 | |
|
|
489 | =item rootalign $img |
|
|
490 | |
|
|
491 | Moves the image so that it appears glued to the screen as opposed to the |
|
|
492 | window. This gives the illusion of a larger area behind the window. It is |
|
|
493 | exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the |
|
|
494 | top left of the screen. |
|
|
495 | |
|
|
496 | Example: load a background image, put it in mirror mode and root align it. |
|
|
497 | |
|
|
498 | rootalign mirror load "mybg.png" |
|
|
499 | |
|
|
500 | Example: take the screen background and align it, giving the illusion of |
|
|
501 | transparency as long as the window isn't in front of other windows. |
|
|
502 | |
|
|
503 | rootalign root |
|
|
504 | |
|
|
505 | =cut |
|
|
506 | |
352 | sub move($$;$) { |
507 | sub move($$;$) { |
353 | my $img = pop->clone; |
508 | my $img = pop->clone; |
354 | $img->move ($_[0], $_[1]); |
509 | $img->move ($_[0], $_[1]); |
355 | $img |
510 | $img |
|
|
511 | } |
|
|
512 | |
|
|
513 | sub rootalign($) { |
|
|
514 | move -TX, -TY, $_[0] |
|
|
515 | } |
|
|
516 | |
|
|
517 | =item contrast $factor, $img |
|
|
518 | |
|
|
519 | =item contrast $r, $g, $b, $img |
|
|
520 | |
|
|
521 | =item contrast $r, $g, $b, $a, $img |
|
|
522 | |
|
|
523 | Adjusts the I<contrast> of an image. |
|
|
524 | |
|
|
525 | =item brightness $factor, $img |
|
|
526 | |
|
|
527 | =item brightness $r, $g, $b, $img |
|
|
528 | |
|
|
529 | =item brightness $r, $g, $b, $a, $img |
|
|
530 | |
|
|
531 | =cut |
|
|
532 | |
|
|
533 | sub contrast($$;$$;$) { |
|
|
534 | my $img = pop; |
|
|
535 | my ($r, $g, $b, $a) = @_; |
|
|
536 | |
|
|
537 | ($g, $b) = ($r, $r) if @_ < 4; |
|
|
538 | $a = 1 if @_ < 5; |
|
|
539 | |
|
|
540 | $img = $img->clone; |
|
|
541 | # $img->contrast ($r, $g, $b, $a); |
|
|
542 | $img |
|
|
543 | } |
|
|
544 | |
|
|
545 | sub brightness($$;$$;$) { |
|
|
546 | my $img = pop; |
|
|
547 | my ($r, $g, $b, $a) = @_; |
|
|
548 | |
|
|
549 | ($g, $b) = ($r, $r) if @_ < 4; |
|
|
550 | $a = 1 if @_ < 5; |
|
|
551 | |
|
|
552 | $img = $img->clone; |
|
|
553 | $img->brightness ($r, $g, $b, $a); |
|
|
554 | $img |
|
|
555 | } |
|
|
556 | |
|
|
557 | sub blur($$;$) { |
|
|
558 | my $img = pop; |
|
|
559 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
356 | } |
560 | } |
357 | |
561 | |
358 | sub rotate($$$$$$) { |
562 | sub rotate($$$$$$) { |
359 | my $img = pop; |
563 | my $img = pop; |
360 | $img->rotate ( |
564 | $img->rotate ( |
… | |
… | |
364 | $_[3] * $img->h * .01, |
568 | $_[3] * $img->h * .01, |
365 | $_[4] * (3.14159265 / 180), |
569 | $_[4] * (3.14159265 / 180), |
366 | ) |
570 | ) |
367 | } |
571 | } |
368 | |
572 | |
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 |
573 | =back |
399 | |
574 | |
400 | =cut |
575 | =cut |
401 | |
576 | |
402 | } |
577 | } |
… | |
… | |
432 | |
607 | |
433 | # set environment to evaluate user expression |
608 | # set environment to evaluate user expression |
434 | |
609 | |
435 | local $self = $arg_self; |
610 | local $self = $arg_self; |
436 | |
611 | |
|
|
612 | local $HOME = $ENV{HOME}; |
437 | local $old = $self->{state}; |
613 | local $old = $self->{state}; |
438 | local $new = my $state = $self->{state} = {}; |
614 | local $new = my $state = $self->{state} = {}; |
439 | |
615 | |
440 | ($x, $y, $w, $h) = |
616 | ($x, $y, $w, $h) = |
441 | $self->background_geometry ($self->{border}); |
617 | $self->background_geometry ($self->{border}); |