1 | #! perl |
1 | #! perl |
2 | |
2 | |
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 | #:META:X_RESOURCE:%.interval:seconds:minimum time between updates |
6 | #TODO: once, rootalign |
|
|
7 | |
6 | |
8 | =head1 NAME |
7 | =head1 NAME |
9 | |
8 | |
10 | background - manage terminal background |
9 | background - manage terminal background |
11 | |
10 | |
12 | =head1 SYNOPSIS |
11 | =head1 SYNOPSIS |
13 | |
12 | |
14 | urxvt --background-expr 'background expression' |
13 | urxvt --background-expr 'background expression' |
15 | --background-border |
14 | --background-border |
|
|
15 | --background-interval seconds |
16 | |
16 | |
17 | =head1 DESCRIPTION |
17 | =head1 DESCRIPTION |
18 | |
18 | |
19 | This extension manages the terminal background by creating a picture that |
19 | This extension manages the terminal background by creating a picture that |
20 | is behind the text, replacing the normal background colour. |
20 | is behind the text, replacing the normal background colour. |
… | |
… | |
57 | |
57 | |
58 | For example, an expression such as C<scale load "$HOME/mybg.png"> scales the |
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 |
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 |
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 |
61 | example. That ensures that the picture always fills the terminal, even |
62 | after it's size changes. |
62 | after its size changes. |
63 | |
63 | |
64 | =head2 EXPRESSIONS |
64 | =head2 EXPRESSIONS |
65 | |
65 | |
66 | Expressions are normal Perl expressions, in fact, they are Perl blocks - |
66 | Expressions are normal Perl expressions, in fact, they are Perl blocks - |
67 | which means you could use multiple lines and statements: |
67 | which means you could use multiple lines and statements: |
… | |
… | |
71 | return scale load "$HOME/weekday.png"; |
71 | return scale load "$HOME/weekday.png"; |
72 | } else { |
72 | } else { |
73 | return scale load "$HOME/sunday.png"; |
73 | return scale load "$HOME/sunday.png"; |
74 | } |
74 | } |
75 | |
75 | |
76 | This expression gets evaluated once per hour. It will set F<sunday.png> as |
76 | This expression is evaluated once per hour. It will set F<sunday.png> as |
77 | background on Sundays, and F<weekday.png> on all other days. |
77 | background on Sundays, and F<weekday.png> on all other days. |
78 | |
78 | |
79 | Fortunately, we expect that most expressions will be much simpler, with |
79 | Fortunately, we expect that most expressions will be much simpler, with |
80 | little Perl knowledge needed. |
80 | little Perl knowledge needed. |
81 | |
81 | |
… | |
… | |
115 | horizontal and vertical dimensions. For example, this halves the image |
115 | horizontal and vertical dimensions. For example, this halves the image |
116 | width and doubles the image height: |
116 | width and doubles the image height: |
117 | |
117 | |
118 | scale 0.5, 2, load "$HOME/mypic.png" |
118 | scale 0.5, 2, load "$HOME/mypic.png" |
119 | |
119 | |
120 | Other effects than scalign are also readily available, for exmaple, you can |
120 | Other effects than scaling are also readily available, for example, you can |
121 | 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: |
122 | |
122 | |
123 | tile load "$HOME/mypic.png" |
123 | tile load "$HOME/mypic.png" |
124 | |
124 | |
125 | In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator |
125 | In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator |
… | |
… | |
151 | This only works for one cycle though, so as long as you load the same |
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 |
152 | image every time, it will always be cached, but when you load a different |
153 | image, it will forget about the first one. |
153 | image, it will forget about the first one. |
154 | |
154 | |
155 | This allows you to either speed things up by keeping multiple images in |
155 | This allows you to either speed things up by keeping multiple images in |
156 | memory, or comserve memory by loading images more often. |
156 | memory, or conserve memory by loading images more often. |
157 | |
157 | |
158 | For example, you can keep two images in memory and use a random one like |
158 | For example, you can keep two images in memory and use a random one like |
159 | this: |
159 | this: |
160 | |
160 | |
161 | my $img1 = load "img1.png"; |
161 | my $img1 = load "img1.png"; |
… | |
… | |
189 | overwriting borders and any other areas, such as the scrollbar. |
189 | overwriting borders and any other areas, such as the scrollbar. |
190 | |
190 | |
191 | Specifying this flag changes the behaviour, so that the image only |
191 | Specifying this flag changes the behaviour, so that the image only |
192 | replaces the background of the character area. |
192 | replaces the background of the character area. |
193 | |
193 | |
|
|
194 | =item --background-interval seconds |
|
|
195 | |
|
|
196 | Since some operations in the underlying XRender extension can effectively |
|
|
197 | freeze your X-server for prolonged time, this extension enforces a minimum |
|
|
198 | time between updates, which is normally about 0.1 seconds. |
|
|
199 | |
|
|
200 | If you want to do updates more often, you can decrease this safety |
|
|
201 | interval with this switch. |
|
|
202 | |
194 | =back |
203 | =back |
195 | |
204 | |
196 | =cut |
205 | =cut |
197 | |
206 | |
|
|
207 | our %_IMG_CACHE; |
198 | our $HOME; |
208 | our $HOME; |
199 | our ($self, $old, $new); |
209 | our ($self, $old, $new); |
200 | our ($x, $y, $w, $h); |
210 | our ($x, $y, $w, $h); |
201 | |
211 | |
202 | # enforce at least this interval between updates |
212 | # enforce at least this interval between updates |
203 | our $MIN_INTERVAL = 1/100; |
213 | our $MIN_INTERVAL = 6/59.951; |
204 | |
214 | |
205 | { |
215 | { |
206 | package urxvt::bgdsl; # background language |
216 | package urxvt::bgdsl; # background language |
207 | |
217 | |
208 | use List::Util qw(min max sum shuffle); |
218 | use List::Util qw(min max sum shuffle); |
… | |
… | |
218 | =item load $path |
228 | =item load $path |
219 | |
229 | |
220 | Loads the image at the given C<$path>. The image is set to plane tiling |
230 | Loads the image at the given C<$path>. The image is set to plane tiling |
221 | mode. |
231 | mode. |
222 | |
232 | |
223 | Loaded images will be cached for one cycle. |
233 | Loaded images will be cached for one cycle, and shared between temrinals |
|
|
234 | running in the same process (e.g. in C<urxvtd>). |
224 | |
235 | |
|
|
236 | =item load_uc $path |
|
|
237 | |
|
|
238 | Load uncached - same as load, but does not cache the image. This function |
|
|
239 | is most useufl if you want to optimise a background expression in some |
|
|
240 | way. |
|
|
241 | |
225 | =cut |
242 | =cut |
|
|
243 | |
|
|
244 | sub load_uc($) { |
|
|
245 | my ($path) = @_; |
|
|
246 | |
|
|
247 | $_IMG_CACHE{$path} || do { |
|
|
248 | my $img = $self->new_img_from_file ($path); |
|
|
249 | Scalar::Util::weaken ($_IMG_CACHE{$path} = $img); |
|
|
250 | $img |
|
|
251 | } |
|
|
252 | } |
226 | |
253 | |
227 | sub load($) { |
254 | sub load($) { |
228 | my ($path) = @_; |
255 | my ($path) = @_; |
229 | |
256 | |
230 | $new->{load}{$path} = $old->{load}{$path} || $self->new_img_from_file ($path); |
257 | $new->{load}{$path} = $old->{load}{$path} || load_uc $path; |
231 | } |
258 | } |
232 | |
259 | |
233 | =item root |
260 | =item root |
234 | |
261 | |
235 | Returns the root window pixmap, that is, hopefully, the background image |
262 | Returns the root window pixmap, that is, hopefully, the background image |
… | |
… | |
239 | reevaluated when the bg image changes. |
266 | reevaluated when the bg image changes. |
240 | |
267 | |
241 | =cut |
268 | =cut |
242 | |
269 | |
243 | sub root() { |
270 | sub root() { |
244 | $new->{rootpmap_sensitive} = 1; |
271 | $new->{again}{rootpmap} = 1; |
245 | die "root op not supported, exg, we need you"; |
272 | $self->new_img_from_root |
246 | } |
273 | } |
247 | |
274 | |
248 | =item solid $colour |
275 | =item solid $colour |
249 | |
276 | |
250 | =item solid $width, $height, $colour |
277 | =item solid $width, $height, $colour |
… | |
… | |
258 | =cut |
285 | =cut |
259 | |
286 | |
260 | sub solid($;$$) { |
287 | sub solid($;$$) { |
261 | my $colour = pop; |
288 | my $colour = pop; |
262 | |
289 | |
263 | my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); |
290 | my $img = $self->new_img (urxvt::PictStandardARGB32, 0, 0, $_[0] || 1, $_[1] || 1); |
264 | $img->fill ($colour); |
291 | $img->fill ($colour); |
265 | $img |
292 | $img |
266 | } |
293 | } |
267 | |
294 | |
268 | =item clone $img |
295 | =item clone $img |
… | |
… | |
274 | |
301 | |
275 | sub clone($) { |
302 | sub clone($) { |
276 | $_[0]->clone |
303 | $_[0]->clone |
277 | } |
304 | } |
278 | |
305 | |
279 | =back |
306 | =item merge $img ... |
|
|
307 | |
|
|
308 | Takes any number of images and merges them together, creating a single |
|
|
309 | image containing them all. |
|
|
310 | |
|
|
311 | This function is called automatically when an expression returns multiple |
|
|
312 | images. |
|
|
313 | |
|
|
314 | =cut |
|
|
315 | |
|
|
316 | sub merge(@) { |
|
|
317 | return $_[0] unless $#_; |
|
|
318 | |
|
|
319 | # rather annoyingly clumsy, but optimisation is for another time |
|
|
320 | |
|
|
321 | my $x0 = +1e9; |
|
|
322 | my $y0 = +1e9; |
|
|
323 | my $x1 = -1e9; |
|
|
324 | my $y1 = -1e9; |
|
|
325 | |
|
|
326 | for (@_) { |
|
|
327 | my ($x, $y, $w, $h) = $_->geometry; |
|
|
328 | |
|
|
329 | $x0 = $x if $x0 > $x; |
|
|
330 | $y0 = $y if $y0 > $y; |
|
|
331 | |
|
|
332 | $x += $w; |
|
|
333 | $y += $h; |
|
|
334 | |
|
|
335 | $x1 = $x if $x1 < $x; |
|
|
336 | $y1 = $y if $y1 < $y; |
|
|
337 | } |
|
|
338 | |
|
|
339 | my $base = $self->new_img (urxvt::PictStandardARGB32, $x0, $y0, $x1 - $x0, $y1 - $y0); |
|
|
340 | $base->fill ([0, 0, 0, 0]); |
|
|
341 | |
|
|
342 | $base->draw ($_) |
|
|
343 | for @_; |
|
|
344 | |
|
|
345 | $base |
|
|
346 | } |
280 | |
347 | |
281 | =head2 TILING MODES |
348 | =head2 TILING MODES |
282 | |
349 | |
283 | The following operators modify the tiling mode of an image, that is, the |
350 | The following operators modify the tiling mode of an image, that is, the |
284 | way that pixels outside the image area are painted when the image is used. |
351 | way that pixels outside the image area are painted when the image is used. |
… | |
… | |
314 | become transparent. This mode is most useful when you want to place an |
381 | become transparent. This mode is most useful when you want to place an |
315 | image over another image or the background colour while leaving all |
382 | image over another image or the background colour while leaving all |
316 | background pixels outside the image unchanged. |
383 | background pixels outside the image unchanged. |
317 | |
384 | |
318 | Example: load an image and display it in the upper left corner. The rest |
385 | Example: load an image and display it in the upper left corner. The rest |
319 | of the space is left "empty" (transparent or wahtever your compisotr does |
386 | of the space is left "empty" (transparent or whatever your compositor does |
320 | in alpha mode, else background colour). |
387 | in alpha mode, else background colour). |
321 | |
388 | |
322 | pad load "mybg.png" |
389 | pad load "mybg.png" |
323 | |
390 | |
324 | =item extend $img |
391 | =item extend $img |
325 | |
392 | |
326 | Extends the image over the whole plane, using the closest pixel in the |
393 | Extends the image over the whole plane, using the closest pixel in the |
327 | area outside the image. This mode is mostly useful when you more complex |
394 | area outside the image. This mode is mostly useful when you use more complex |
328 | filtering operations and want the pixels outside the image to have the |
395 | filtering operations and want the pixels outside the image to have the |
329 | same values as the pixels near the edge. |
396 | same values as the pixels near the edge. |
330 | |
397 | |
331 | Example: just for curiosity, how does this pixel extension stuff work? |
398 | Example: just for curiosity, how does this pixel extension stuff work? |
332 | |
399 | |
… | |
… | |
399 | the window size to conserve memory. |
466 | the window size to conserve memory. |
400 | |
467 | |
401 | Example: take the screen background, clip it to the window size, blur it a |
468 | Example: take the screen background, clip it to the window size, blur it a |
402 | bit, align it to the window position and use it as background. |
469 | bit, align it to the window position and use it as background. |
403 | |
470 | |
404 | clip move -TX, -TY, blur 5, root |
471 | clip move -TX, -TY, once { blur 5, root } |
405 | |
472 | |
406 | =cut |
473 | =cut |
407 | |
474 | |
408 | sub TX() { $new->{position_sensitive} = 1; $x } |
475 | sub TX() { $new->{again}{position} = 1; $x } |
409 | sub TY() { $new->{position_sensitive} = 1; $y } |
476 | sub TY() { $new->{again}{position} = 1; $y } |
410 | sub TW() { $new->{size_sensitive} = 1; $w } |
477 | sub TW() { $new->{again}{size} = 1; $w } |
411 | sub TH() { $new->{size_sensitive} = 1; $h } |
478 | sub TH() { $new->{again}{size} = 1; $h } |
412 | |
479 | |
413 | =item now |
480 | =item now |
414 | |
481 | |
415 | Returns the current time as (fractional) seconds since the epoch. |
482 | Returns the current time as (fractional) seconds since the epoch. |
416 | |
483 | |
… | |
… | |
423 | C<$seconds> seconds. |
490 | C<$seconds> seconds. |
424 | |
491 | |
425 | Example: load some image and rotate it according to the time of day (as if it were |
492 | Example: load some image and rotate it according to the time of day (as if it were |
426 | the hour pointer of a clock). Update this image every minute. |
493 | the hour pointer of a clock). Update this image every minute. |
427 | |
494 | |
428 | again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
495 | again 60; rotate 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
429 | |
496 | |
430 | =item counter $seconds |
497 | =item counter $seconds |
431 | |
498 | |
432 | Like C<again>, but also returns an increasing counter value, starting at |
499 | Like C<again>, but also returns an increasing counter value, starting at |
433 | 0, which might be useful for some simple animation effects. |
500 | 0, which might be useful for some simple animation effects. |
… | |
… | |
435 | =cut |
502 | =cut |
436 | |
503 | |
437 | sub now() { urxvt::NOW } |
504 | sub now() { urxvt::NOW } |
438 | |
505 | |
439 | sub again($) { |
506 | sub again($) { |
440 | $new->{again} = $_[0]; |
507 | $new->{again}{time} = $_[0]; |
441 | } |
508 | } |
442 | |
509 | |
443 | sub counter($) { |
510 | sub counter($) { |
444 | $new->{again} = $_[0]; |
511 | $new->{again}{time} = $_[0]; |
445 | $self->{counter} + 0 |
512 | $self->{counter} + 0 |
446 | } |
513 | } |
447 | |
514 | |
448 | =back |
515 | =back |
449 | |
516 | |
… | |
… | |
553 | |
620 | |
554 | Example: move the image right by 20 pixels and down by 30. |
621 | Example: move the image right by 20 pixels and down by 30. |
555 | |
622 | |
556 | move 20, 30, ... |
623 | move 20, 30, ... |
557 | |
624 | |
|
|
625 | =item align $xalign, $yalign, $img |
|
|
626 | |
|
|
627 | Aligns the image according to a factor - C<0> means the image is moved to |
|
|
628 | the left or top edge (for C<$xalign> or C<$yalign>), C<0.5> means it is |
|
|
629 | exactly centered and C<1> means it touches the right or bottom edge. |
|
|
630 | |
|
|
631 | Example: remove any visible border around an image, center it vertically but move |
|
|
632 | it to the right hand side. |
|
|
633 | |
|
|
634 | align 1, 0.5, pad $img |
|
|
635 | |
558 | =item center $img |
636 | =item center $img |
559 | |
637 | |
560 | =item center $width, $height, $img |
638 | =item center $width, $height, $img |
561 | |
639 | |
562 | Centers the image, i.e. the center of the image is moved to the center of |
640 | Centers the image, i.e. the center of the image is moved to the center of |
563 | the terminal window (or the box specified by C<$width> and C<$height> if |
641 | the terminal window (or the box specified by C<$width> and C<$height> if |
564 | given). |
642 | given). |
|
|
643 | |
|
|
644 | Example: load an image and center it. |
|
|
645 | |
|
|
646 | center pad load "mybg.png" |
565 | |
647 | |
566 | =item rootalign $img |
648 | =item rootalign $img |
567 | |
649 | |
568 | Moves the image so that it appears glued to the screen as opposed to the |
650 | Moves the image so that it appears glued to the screen as opposed to the |
569 | window. This gives the illusion of a larger area behind the window. It is |
651 | window. This gives the illusion of a larger area behind the window. It is |
… | |
… | |
575 | rootalign mirror load "mybg.png" |
657 | rootalign mirror load "mybg.png" |
576 | |
658 | |
577 | Example: take the screen background and align it, giving the illusion of |
659 | Example: take the screen background and align it, giving the illusion of |
578 | transparency as long as the window isn't in front of other windows. |
660 | transparency as long as the window isn't in front of other windows. |
579 | |
661 | |
580 | rootalign root |
662 | rootalign root |
581 | |
663 | |
582 | =cut |
664 | =cut |
583 | |
665 | |
584 | sub move($$;$) { |
666 | sub move($$;$) { |
585 | my $img = pop->clone; |
667 | my $img = pop->clone; |
586 | $img->move ($_[0], $_[1]); |
668 | $img->move ($_[0], $_[1]); |
587 | $img |
669 | $img |
588 | } |
670 | } |
589 | |
671 | |
|
|
672 | sub align($;$$) { |
|
|
673 | my $img = pop; |
|
|
674 | |
|
|
675 | move $_[0] * (TW - $img->w), |
|
|
676 | $_[1] * (TH - $img->h), |
|
|
677 | $img |
|
|
678 | } |
|
|
679 | |
590 | sub center($;$$) { |
680 | sub center($;$$) { |
591 | my $img = pop; |
681 | my $img = pop; |
592 | my $w = $_[0] || TW; |
682 | my $w = $_[0] || TW; |
593 | my $h = $_[0] || TH; |
683 | my $h = $_[1] || TH; |
594 | |
684 | |
595 | move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img |
685 | move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img |
596 | } |
686 | } |
597 | |
687 | |
598 | sub rootalign($) { |
688 | sub rootalign($) { |
599 | move -TX, -TY, $_[0] |
689 | move -TX, -TY, $_[0] |
|
|
690 | } |
|
|
691 | |
|
|
692 | =item rotate $center_x, $center_y, $degrees |
|
|
693 | |
|
|
694 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
|
|
695 | pointer at C<$center_x> and C<$center_y> (specified as factor of image |
|
|
696 | width/height). |
|
|
697 | |
|
|
698 | #TODO# new width, height, maybe more operators? |
|
|
699 | |
|
|
700 | Example: rotate the image by 90 degrees |
|
|
701 | |
|
|
702 | =cut |
|
|
703 | |
|
|
704 | sub rotate($$$$) { |
|
|
705 | my $img = pop; |
|
|
706 | $img->rotate ( |
|
|
707 | $_[0] * ($img->w + $img->x), |
|
|
708 | $_[1] * ($img->h + $img->y), |
|
|
709 | $_[2] * (3.14159265 / 180), |
|
|
710 | ) |
600 | } |
711 | } |
601 | |
712 | |
602 | =back |
713 | =back |
603 | |
714 | |
604 | =head2 COLOUR MODIFICATIONS |
715 | =head2 COLOUR MODIFICATIONS |
… | |
… | |
640 | |
751 | |
641 | Values less than 0 reduce brightness, while values larger than 0 increase |
752 | Values less than 0 reduce brightness, while values larger than 0 increase |
642 | it. Useful range is from -1 to 1 - the former results in a black, the |
753 | it. Useful range is from -1 to 1 - the former results in a black, the |
643 | latter in a white picture. |
754 | latter in a white picture. |
644 | |
755 | |
645 | Due to idiosynchrasies in the underlying XRender extension, biases less |
756 | Due to idiosyncrasies in the underlying XRender extension, biases less |
646 | than zero can be I<very> slow. |
757 | than zero can be I<very> slow. |
647 | |
758 | |
648 | =cut |
759 | =cut |
649 | |
760 | |
650 | sub contrast($$;$$;$) { |
761 | sub contrast($$;$$;$) { |
651 | my $img = pop; |
762 | my $img = pop; |
652 | my ($r, $g, $b, $a) = @_; |
763 | my ($r, $g, $b, $a) = @_; |
653 | |
764 | |
654 | ($g, $b) = ($r, $r) if @_ < 4; |
765 | ($g, $b) = ($r, $r) if @_ < 3; |
655 | $a = 1 if @_ < 5; |
766 | $a = 1 if @_ < 4; |
656 | |
767 | |
657 | $img = $img->clone; |
768 | $img = $img->clone; |
658 | $img->contrast ($r, $g, $b, $a); |
769 | $img->contrast ($r, $g, $b, $a); |
659 | $img |
770 | $img |
660 | } |
771 | } |
661 | |
772 | |
662 | sub brightness($$;$$;$) { |
773 | sub brightness($$;$$;$) { |
663 | my $img = pop; |
774 | my $img = pop; |
664 | my ($r, $g, $b, $a) = @_; |
775 | my ($r, $g, $b, $a) = @_; |
665 | |
776 | |
666 | ($g, $b) = ($r, $r) if @_ < 4; |
777 | ($g, $b) = ($r, $r) if @_ < 3; |
667 | $a = 1 if @_ < 5; |
778 | $a = 1 if @_ < 4; |
668 | |
779 | |
669 | $img = $img->clone; |
780 | $img = $img->clone; |
670 | $img->brightness ($r, $g, $b, $a); |
781 | $img->brightness ($r, $g, $b, $a); |
671 | $img |
782 | $img |
672 | } |
783 | } |
… | |
… | |
688 | sub blur($$;$) { |
799 | sub blur($$;$) { |
689 | my $img = pop; |
800 | my $img = pop; |
690 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
801 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
691 | } |
802 | } |
692 | |
803 | |
693 | =item rotate $new_width, $new_height, $center_x, $center_y, $degrees |
804 | =back |
694 | |
805 | |
695 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
806 | =head2 OTHER STUFF |
696 | pointer at C<$center_x> and C<$center_y> (specified as factor of image |
|
|
697 | width/height), generating a new image with width C<$new_width> and height |
|
|
698 | C<$new_height>. |
|
|
699 | |
807 | |
700 | #TODO# new width, height, maybe more operators? |
808 | Anything that didn't fit any of the other categories, even after applying |
|
|
809 | force and closing our eyes. |
701 | |
810 | |
702 | Example: rotate the image by 90 degrees |
811 | =over 4 |
703 | |
812 | |
704 | =cut |
813 | =item once { ... } |
705 | |
814 | |
706 | sub rotate($$$$$$) { |
815 | This function takes a code block as argument, that is, one or more |
707 | my $img = pop; |
816 | statements enclosed by braces. |
708 | $img->rotate ( |
817 | |
709 | $_[0], |
818 | The trick is that this code block is only evaluated once - future calls |
710 | $_[1], |
819 | will simply return the original image (yes, it should only be used with |
711 | $_[2] * $img->w, |
820 | images). |
712 | $_[3] * $img->h, |
821 | |
713 | $_[4] * (3.14159265 / 180), |
822 | This can be extremely useful to avoid redoign the same slow operations |
|
|
823 | again and again- for example, if your background expression takes the root |
|
|
824 | background, blurs it and then root-aligns it it would have to blur the |
|
|
825 | root background on every window move or resize. |
|
|
826 | |
|
|
827 | Putting the blur into a C<once> block will make sure the blur is only done |
|
|
828 | once: |
|
|
829 | |
|
|
830 | rootlign once { blur 10, root } |
|
|
831 | |
|
|
832 | This leaves the question of how to force reevaluation of the block, in |
|
|
833 | case the root background changes: Right now, all once blocks forget that |
|
|
834 | they ahve been executed before each time the root background changes (if |
|
|
835 | the expression is sensitive to that) or when C<once_again> is called. |
|
|
836 | |
|
|
837 | =item once_again |
|
|
838 | |
|
|
839 | Resets all C<once> block as if they had never been called, i.e. on the |
|
|
840 | next call they will be reevaluated again. |
|
|
841 | |
|
|
842 | =cut |
|
|
843 | |
|
|
844 | sub once(&) { |
|
|
845 | my $once = $self->{once_cache}{$_[0]+0} ||= do { |
|
|
846 | local $new->{again}; |
|
|
847 | my @res = $_[0](); |
|
|
848 | [$new->{again}, \@res] |
714 | ) |
849 | }; |
|
|
850 | |
|
|
851 | $new->{again} = { |
|
|
852 | %{ $new->{again} }, |
|
|
853 | %{ $once->[0] } |
|
|
854 | }; |
|
|
855 | |
|
|
856 | # in scalar context we always return the first original result, which |
|
|
857 | # is not quite how perl works. |
|
|
858 | wantarray |
|
|
859 | ? @{ $once->[1] } |
|
|
860 | : $once->[1][0] |
|
|
861 | } |
|
|
862 | |
|
|
863 | sub once_again() { |
|
|
864 | delete $self->{once_cache}; |
715 | } |
865 | } |
716 | |
866 | |
717 | =back |
867 | =back |
718 | |
868 | |
719 | =cut |
869 | =cut |
… | |
… | |
760 | ($x, $y, $w, $h) = |
910 | ($x, $y, $w, $h) = |
761 | $self->background_geometry ($self->{border}); |
911 | $self->background_geometry ($self->{border}); |
762 | |
912 | |
763 | # evaluate user expression |
913 | # evaluate user expression |
764 | |
914 | |
765 | my $img = eval { $self->{expr}->() }; |
915 | my $img = eval { urxvt::bgdsl::merge $self->{expr}->() }; |
766 | warn $@ if $@;#d# |
916 | die $@ if $@; |
767 | die if !UNIVERSAL::isa $img, "urxvt::img"; |
917 | die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img"; |
768 | |
918 | |
769 | $state->{size_sensitive} = 1 |
919 | # if the expression is sensitive to external events, prepare reevaluation then |
|
|
920 | |
|
|
921 | my $again = delete $state->{again}; |
|
|
922 | |
|
|
923 | $again->{size} = 1 |
770 | if $img->repeat_mode != urxvt::RepeatNormal; |
924 | if $img->repeat_mode != urxvt::RepeatNormal; |
771 | |
925 | |
772 | # if the expression is sensitive to external events, prepare reevaluation then |
|
|
773 | |
|
|
774 | my $repeat; |
|
|
775 | |
|
|
776 | if (my $again = $state->{again}) { |
926 | if (my $again = $again->{time}) { |
777 | $repeat = 1; |
|
|
778 | my $self = $self; |
927 | my $self = $self; |
779 | $state->{timer} = $again == $old->{again} |
928 | $state->{timer} = $again == $old->{again} |
780 | ? $old->{timer} |
929 | ? $old->{timer} |
781 | : urxvt::timer->new->after ($again)->interval ($again)->cb (sub { |
930 | : urxvt::timer->new->after ($again)->interval ($again)->cb (sub { |
782 | ++$self->{counter}; |
931 | ++$self->{counter}; |
783 | $self->recalculate |
932 | $self->recalculate |
784 | }); |
933 | }); |
785 | } |
934 | } |
786 | |
935 | |
787 | if (delete $state->{position_sensitive}) { |
936 | if ($again->{position}) { |
788 | $repeat = 1; |
|
|
789 | $self->enable (position_change => sub { $_[0]->recalculate }); |
937 | $self->enable (position_change => sub { $_[0]->recalculate }); |
790 | } else { |
938 | } else { |
791 | $self->disable ("position_change"); |
939 | $self->disable ("position_change"); |
792 | } |
940 | } |
793 | |
941 | |
794 | if (delete $state->{size_sensitive}) { |
942 | if ($again->{size}) { |
795 | $repeat = 1; |
|
|
796 | $self->enable (size_change => sub { $_[0]->recalculate }); |
943 | $self->enable (size_change => sub { $_[0]->recalculate }); |
797 | } else { |
944 | } else { |
798 | $self->disable ("size_change"); |
945 | $self->disable ("size_change"); |
799 | } |
946 | } |
800 | |
947 | |
801 | if (delete $state->{rootpmap_sensitive}) { |
948 | if ($again->{rootpmap}) { |
802 | $repeat = 1; |
|
|
803 | $self->enable (rootpmap_change => sub { $_[0]->recalculate }); |
949 | $self->enable (rootpmap_change => sub { |
|
|
950 | delete $_[0]{once_cache}; # this will override once-block values from |
|
|
951 | $_[0]->recalculate; |
|
|
952 | }); |
804 | } else { |
953 | } else { |
805 | $self->disable ("rootpmap_change"); |
954 | $self->disable ("rootpmap_change"); |
806 | } |
955 | } |
807 | |
956 | |
808 | # clear stuff we no longer need |
957 | # clear stuff we no longer need |
809 | |
958 | |
810 | %$old = (); |
959 | %$old = (); |
811 | |
960 | |
812 | unless ($repeat) { |
961 | unless (%$again) { |
813 | delete $self->{state}; |
962 | delete $self->{state}; |
814 | delete $self->{expr}; |
963 | delete $self->{expr}; |
815 | } |
964 | } |
816 | |
965 | |
817 | # set background pixmap |
966 | # set background pixmap |
… | |
… | |
822 | } |
971 | } |
823 | |
972 | |
824 | sub on_start { |
973 | sub on_start { |
825 | my ($self) = @_; |
974 | my ($self) = @_; |
826 | |
975 | |
827 | my $expr = $self->x_resource ("background.expr") |
976 | my $expr = $self->x_resource ("%.expr") |
828 | or return; |
977 | or return; |
829 | |
978 | |
|
|
979 | $self->has_render |
|
|
980 | or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n"; |
|
|
981 | |
830 | $self->set_expr (parse_expr $expr); |
982 | $self->set_expr (parse_expr $expr); |
831 | $self->{border} = $self->x_resource_boolean ("background.border"); |
983 | $self->{border} = $self->x_resource_boolean ("%.border"); |
|
|
984 | |
|
|
985 | $MIN_INTERVAL = $self->x_resource ("%.interval"); |
832 | |
986 | |
833 | () |
987 | () |
834 | } |
988 | } |
835 | |
989 | |