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. |
… | |
… | |
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 effetively |
|
|
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 %_IMGCACHE; |
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); |
… | |
… | |
263 | my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); |
273 | my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); |
264 | $img->fill ($colour); |
274 | $img->fill ($colour); |
265 | $img |
275 | $img |
266 | } |
276 | } |
267 | |
277 | |
|
|
278 | =item clone $img |
|
|
279 | |
|
|
280 | Returns an exact copy of the image. This is useful if you want to have |
|
|
281 | multiple copies of the same image to apply different effects to. |
|
|
282 | |
|
|
283 | =cut |
|
|
284 | |
|
|
285 | sub clone($) { |
|
|
286 | $_[0]->clone |
|
|
287 | } |
|
|
288 | |
268 | =back |
289 | =back |
269 | |
290 | |
|
|
291 | =head2 TILING MODES |
|
|
292 | |
|
|
293 | The following operators modify the tiling mode of an image, that is, the |
|
|
294 | way that pixels outside the image area are painted when the image is used. |
|
|
295 | |
|
|
296 | =over 4 |
|
|
297 | |
|
|
298 | =item tile $img |
|
|
299 | |
|
|
300 | Tiles the whole plane with the image and returns this new image - or in |
|
|
301 | other words, it returns a copy of the image in plane tiling mode. |
|
|
302 | |
|
|
303 | Example: load an image and tile it over the background, without |
|
|
304 | resizing. The C<tile> call is superfluous because C<load> already defaults |
|
|
305 | to tiling mode. |
|
|
306 | |
|
|
307 | tile load "mybg.png" |
|
|
308 | |
|
|
309 | =item mirror $img |
|
|
310 | |
|
|
311 | Similar to tile, but reflects the image each time it uses a new copy, so |
|
|
312 | that top edges always touch top edges, right edges always touch right |
|
|
313 | edges and so on (with normal tiling, left edges always touch right edges |
|
|
314 | and top always touch bottom edges). |
|
|
315 | |
|
|
316 | Example: load an image and mirror it over the background, avoiding sharp |
|
|
317 | edges at the image borders at the expense of mirroring the image itself |
|
|
318 | |
|
|
319 | mirror load "mybg.png" |
|
|
320 | |
|
|
321 | =item pad $img |
|
|
322 | |
|
|
323 | Takes an image and modifies it so that all pixels outside the image area |
|
|
324 | become transparent. This mode is most useful when you want to place an |
|
|
325 | image over another image or the background colour while leaving all |
|
|
326 | background pixels outside the image unchanged. |
|
|
327 | |
|
|
328 | Example: load an image and display it in the upper left corner. The rest |
|
|
329 | of the space is left "empty" (transparent or wahtever your compisotr does |
|
|
330 | in alpha mode, else background colour). |
|
|
331 | |
|
|
332 | pad load "mybg.png" |
|
|
333 | |
|
|
334 | =item extend $img |
|
|
335 | |
|
|
336 | Extends the image over the whole plane, using the closest pixel in the |
|
|
337 | area outside the image. This mode is mostly useful when you more complex |
|
|
338 | filtering operations and want the pixels outside the image to have the |
|
|
339 | same values as the pixels near the edge. |
|
|
340 | |
|
|
341 | Example: just for curiosity, how does this pixel extension stuff work? |
|
|
342 | |
|
|
343 | extend move 50, 50, load "mybg.png" |
|
|
344 | |
|
|
345 | =cut |
|
|
346 | |
|
|
347 | sub pad($) { |
|
|
348 | my $img = $_[0]->clone; |
|
|
349 | $img->repeat_mode (urxvt::RepeatNone); |
|
|
350 | $img |
|
|
351 | } |
|
|
352 | |
|
|
353 | sub tile($) { |
|
|
354 | my $img = $_[0]->clone; |
|
|
355 | $img->repeat_mode (urxvt::RepeatNormal); |
|
|
356 | $img |
|
|
357 | } |
|
|
358 | |
|
|
359 | sub mirror($) { |
|
|
360 | my $img = $_[0]->clone; |
|
|
361 | $img->repeat_mode (urxvt::RepeatReflect); |
|
|
362 | $img |
|
|
363 | } |
|
|
364 | |
|
|
365 | sub extend($) { |
|
|
366 | my $img = $_[0]->clone; |
|
|
367 | $img->repeat_mode (urxvt::RepeatPad); |
|
|
368 | $img |
|
|
369 | } |
|
|
370 | |
|
|
371 | =back |
|
|
372 | |
270 | =head2 VARIABLES |
373 | =head2 VARIABLE VALUES |
271 | |
374 | |
272 | The following functions provide variable data such as the terminal window |
375 | The following functions provide variable data such as the terminal window |
273 | dimensions. They are not (Perl-) variables, they jsut return stuff that |
376 | dimensions. They are not (Perl-) variables, they just return stuff that |
274 | varies. Most of them make your expression sensitive to some events, for |
377 | varies. Most of them make your expression sensitive to some events, for |
275 | example using C<TW> (terminal width) means your expression is evaluated |
378 | example using C<TW> (terminal width) means your expression is evaluated |
276 | again when the terminal is resized. |
379 | again when the terminal is resized. |
277 | |
380 | |
278 | =over 4 |
381 | =over 4 |
… | |
… | |
352 | $self->{counter} + 0 |
455 | $self->{counter} + 0 |
353 | } |
456 | } |
354 | |
457 | |
355 | =back |
458 | =back |
356 | |
459 | |
357 | =head2 TILING MODES |
460 | =head2 SHAPE CHANGING OPERATORS |
358 | |
461 | |
359 | The following operators modify the tiling mode of an image, that is, the |
462 | The following operators modify the shape, size or position of the image. |
360 | way that pixels outside the image area are painted when the image is used. |
|
|
361 | |
463 | |
362 | =over 4 |
464 | =over 4 |
363 | |
|
|
364 | =item tile $img |
|
|
365 | |
|
|
366 | Tiles the whole plane with the image and returns this new image - or in |
|
|
367 | other words, it returns a copy of the image in plane tiling mode. |
|
|
368 | |
|
|
369 | Example: load an image and tile it over the background, without |
|
|
370 | resizing. The C<tile> call is superfluous because C<load> already defaults |
|
|
371 | to tiling mode. |
|
|
372 | |
|
|
373 | tile load "mybg.png" |
|
|
374 | |
|
|
375 | =item mirror $img |
|
|
376 | |
|
|
377 | Similar to tile, but reflects the image each time it uses a new copy, so |
|
|
378 | that top edges always touch top edges, right edges always touch right |
|
|
379 | edges and so on (with normal tiling, left edges always touch right edges |
|
|
380 | and top always touch bottom edges). |
|
|
381 | |
|
|
382 | Example: load an image and mirror it over the background, avoiding sharp |
|
|
383 | edges at the image borders at the expense of mirroring the image itself |
|
|
384 | |
|
|
385 | mirror load "mybg.png" |
|
|
386 | |
|
|
387 | =item pad $img |
|
|
388 | |
|
|
389 | Takes an image and modifies it so that all pixels outside the image area |
|
|
390 | become transparent. This mode is most useful when you want to place an |
|
|
391 | image over another image or the background colour while leaving all |
|
|
392 | background pixels outside the image unchanged. |
|
|
393 | |
|
|
394 | Example: load an image and display it in the upper left corner. The rest |
|
|
395 | of the space is left "empty" (transparent or wahtever your compisotr does |
|
|
396 | in alpha mode, else background colour). |
|
|
397 | |
|
|
398 | pad load "mybg.png" |
|
|
399 | |
|
|
400 | =item extend $img |
|
|
401 | |
|
|
402 | Extends the image over the whole plane, using the closest pixel in the |
|
|
403 | area outside the image. This mode is mostly useful when you more complex |
|
|
404 | filtering operations and want the pixels outside the image to have the |
|
|
405 | same values as the pixels near the edge. |
|
|
406 | |
|
|
407 | Example: just for curiosity, how does this pixel extension stuff work? |
|
|
408 | |
|
|
409 | extend move 50, 50, load "mybg.png" |
|
|
410 | |
|
|
411 | =cut |
|
|
412 | |
|
|
413 | sub pad($) { |
|
|
414 | my $img = $_[0]->clone; |
|
|
415 | $img->repeat_mode (urxvt::RepeatNone); |
|
|
416 | $img |
|
|
417 | } |
|
|
418 | |
|
|
419 | sub tile($) { |
|
|
420 | my $img = $_[0]->clone; |
|
|
421 | $img->repeat_mode (urxvt::RepeatNormal); |
|
|
422 | $img |
|
|
423 | } |
|
|
424 | |
|
|
425 | sub mirror($) { |
|
|
426 | my $img = $_[0]->clone; |
|
|
427 | $img->repeat_mode (urxvt::RepeatReflect); |
|
|
428 | $img |
|
|
429 | } |
|
|
430 | |
|
|
431 | sub extend($) { |
|
|
432 | my $img = $_[0]->clone; |
|
|
433 | $img->repeat_mode (urxvt::RepeatPad); |
|
|
434 | $img |
|
|
435 | } |
|
|
436 | |
|
|
437 | =back |
|
|
438 | |
|
|
439 | =head2 PIXEL OPERATORS |
|
|
440 | |
|
|
441 | The following operators modify the image pixels in various ways. |
|
|
442 | |
|
|
443 | =over 4 |
|
|
444 | |
|
|
445 | =item clone $img |
|
|
446 | |
|
|
447 | Returns an exact copy of the image. |
|
|
448 | |
|
|
449 | =cut |
|
|
450 | |
|
|
451 | sub clone($) { |
|
|
452 | $_[0]->clone |
|
|
453 | } |
|
|
454 | |
465 | |
455 | =item clip $img |
466 | =item clip $img |
456 | |
467 | |
457 | =item clip $width, $height, $img |
468 | =item clip $width, $height, $img |
458 | |
469 | |
… | |
… | |
552 | |
563 | |
553 | Example: move the image right by 20 pixels and down by 30. |
564 | Example: move the image right by 20 pixels and down by 30. |
554 | |
565 | |
555 | move 20, 30, ... |
566 | move 20, 30, ... |
556 | |
567 | |
|
|
568 | =item align $xalign, $yalign, $img |
|
|
569 | |
|
|
570 | Aligns the image according to a factor - C<0> means the image is moved to |
|
|
571 | the left or top edge (for C<$xalign> or C<$yalign>), C<0.5> means it is |
|
|
572 | exactly centered and C<1> means it touches the right or bottom edge. |
|
|
573 | |
|
|
574 | Example: remove any visible border around an image, center it vertically but move |
|
|
575 | it to the right hand side. |
|
|
576 | |
|
|
577 | align 1, 0.5, pad $img |
|
|
578 | |
|
|
579 | =item center $img |
|
|
580 | |
|
|
581 | =item center $width, $height, $img |
|
|
582 | |
|
|
583 | Centers the image, i.e. the center of the image is moved to the center of |
|
|
584 | the terminal window (or the box specified by C<$width> and C<$height> if |
|
|
585 | given). |
|
|
586 | |
|
|
587 | Example: load an image and center it. |
|
|
588 | |
|
|
589 | center pad load "mybg.png" |
|
|
590 | |
557 | =item rootalign $img |
591 | =item rootalign $img |
558 | |
592 | |
559 | Moves the image so that it appears glued to the screen as opposed to the |
593 | Moves the image so that it appears glued to the screen as opposed to the |
560 | window. This gives the illusion of a larger area behind the window. It is |
594 | window. This gives the illusion of a larger area behind the window. It is |
561 | exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the |
595 | exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the |
… | |
… | |
566 | rootalign mirror load "mybg.png" |
600 | rootalign mirror load "mybg.png" |
567 | |
601 | |
568 | Example: take the screen background and align it, giving the illusion of |
602 | Example: take the screen background and align it, giving the illusion of |
569 | transparency as long as the window isn't in front of other windows. |
603 | transparency as long as the window isn't in front of other windows. |
570 | |
604 | |
571 | rootalign root |
605 | rootalign root |
572 | |
606 | |
573 | =cut |
607 | =cut |
574 | |
608 | |
575 | sub move($$;$) { |
609 | sub move($$;$) { |
576 | my $img = pop->clone; |
610 | my $img = pop->clone; |
577 | $img->move ($_[0], $_[1]); |
611 | $img->move ($_[0], $_[1]); |
578 | $img |
612 | $img |
579 | } |
613 | } |
580 | |
614 | |
|
|
615 | sub align($;$$) { |
|
|
616 | my $img = pop; |
|
|
617 | |
|
|
618 | move $_[0] * (TW - $img->w), |
|
|
619 | $_[1] * (TH - $img->h), |
|
|
620 | $img |
|
|
621 | } |
|
|
622 | |
|
|
623 | sub center($;$$) { |
|
|
624 | my $img = pop; |
|
|
625 | my $w = $_[0] || TW; |
|
|
626 | my $h = $_[1] || TH; |
|
|
627 | |
|
|
628 | move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img |
|
|
629 | } |
|
|
630 | |
581 | sub rootalign($) { |
631 | sub rootalign($) { |
582 | move -TX, -TY, $_[0] |
632 | move -TX, -TY, $_[0] |
583 | } |
633 | } |
584 | |
634 | |
|
|
635 | =back |
|
|
636 | |
|
|
637 | =head2 COLOUR MODIFICATIONS |
|
|
638 | |
|
|
639 | The following operators change the pixels of the image. |
|
|
640 | |
|
|
641 | =over 4 |
|
|
642 | |
585 | =item contrast $factor, $img |
643 | =item contrast $factor, $img |
586 | |
644 | |
587 | =item contrast $r, $g, $b, $img |
645 | =item contrast $r, $g, $b, $img |
588 | |
646 | |
589 | =item contrast $r, $g, $b, $a, $img |
647 | =item contrast $r, $g, $b, $a, $img |
590 | |
648 | |
591 | Adjusts the I<contrast> of an image. |
649 | Adjusts the I<contrast> of an image. |
592 | |
650 | |
593 | #TODO# |
651 | The first form applies a single C<$factor> to red, green and blue, the |
|
|
652 | second form applies separate factors to each colour channel, and the last |
|
|
653 | form includes the alpha channel. |
594 | |
654 | |
|
|
655 | Values from 0 to 1 lower the contrast, values higher than 1 increase the |
|
|
656 | contrast. |
|
|
657 | |
|
|
658 | Due to limitations in the underlying XRender extension, lowering contrast |
|
|
659 | also reduces brightness, while increasing contrast currently also |
|
|
660 | increases brightness. |
|
|
661 | |
595 | =item brightness $factor, $img |
662 | =item brightness $bias, $img |
596 | |
663 | |
597 | =item brightness $r, $g, $b, $img |
664 | =item brightness $r, $g, $b, $img |
598 | |
665 | |
599 | =item brightness $r, $g, $b, $a, $img |
666 | =item brightness $r, $g, $b, $a, $img |
600 | |
667 | |
601 | Adjusts the brightness of an image. |
668 | Adjusts the brightness of an image. |
602 | |
669 | |
|
|
670 | The first form applies a single C<$bias> to red, green and blue, the |
|
|
671 | second form applies separate biases to each colour channel, and the last |
|
|
672 | form includes the alpha channel. |
|
|
673 | |
|
|
674 | Values less than 0 reduce brightness, while values larger than 0 increase |
|
|
675 | it. Useful range is from -1 to 1 - the former results in a black, the |
|
|
676 | latter in a white picture. |
|
|
677 | |
|
|
678 | Due to idiosynchrasies in the underlying XRender extension, biases less |
|
|
679 | than zero can be I<very> slow. |
|
|
680 | |
603 | =cut |
681 | =cut |
604 | |
682 | |
605 | sub contrast($$;$$;$) { |
683 | sub contrast($$;$$;$) { |
606 | my $img = pop; |
684 | my $img = pop; |
607 | my ($r, $g, $b, $a) = @_; |
685 | my ($r, $g, $b, $a) = @_; |
608 | |
686 | |
609 | ($g, $b) = ($r, $r) if @_ < 4; |
687 | ($g, $b) = ($r, $r) if @_ < 3; |
610 | $a = 1 if @_ < 5; |
688 | $a = 1 if @_ < 4; |
611 | |
689 | |
612 | $img = $img->clone; |
690 | $img = $img->clone; |
613 | $img->contrast ($r, $g, $b, $a); |
691 | $img->contrast ($r, $g, $b, $a); |
614 | $img |
692 | $img |
615 | } |
693 | } |
616 | |
694 | |
617 | sub brightness($$;$$;$) { |
695 | sub brightness($$;$$;$) { |
618 | my $img = pop; |
696 | my $img = pop; |
619 | my ($r, $g, $b, $a) = @_; |
697 | my ($r, $g, $b, $a) = @_; |
620 | |
698 | |
621 | ($g, $b) = ($r, $r) if @_ < 4; |
699 | ($g, $b) = ($r, $r) if @_ < 3; |
622 | $a = 1 if @_ < 5; |
700 | $a = 1 if @_ < 4; |
623 | |
701 | |
624 | $img = $img->clone; |
702 | $img = $img->clone; |
625 | $img->brightness ($r, $g, $b, $a); |
703 | $img->brightness ($r, $g, $b, $a); |
626 | $img |
704 | $img |
627 | } |
705 | } |
… | |
… | |
717 | |
795 | |
718 | # evaluate user expression |
796 | # evaluate user expression |
719 | |
797 | |
720 | my $img = eval { $self->{expr}->() }; |
798 | my $img = eval { $self->{expr}->() }; |
721 | warn $@ if $@;#d# |
799 | warn $@ if $@;#d# |
722 | die if !UNIVERSAL::isa $img, "urxvt::img"; |
800 | die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img"; |
723 | |
801 | |
724 | $state->{size_sensitive} = 1 |
802 | $state->{size_sensitive} = 1 |
725 | if $img->repeat_mode != urxvt::RepeatNormal; |
803 | if $img->repeat_mode != urxvt::RepeatNormal; |
726 | |
804 | |
727 | # if the expression is sensitive to external events, prepare reevaluation then |
805 | # if the expression is sensitive to external events, prepare reevaluation then |
… | |
… | |
777 | } |
855 | } |
778 | |
856 | |
779 | sub on_start { |
857 | sub on_start { |
780 | my ($self) = @_; |
858 | my ($self) = @_; |
781 | |
859 | |
782 | my $expr = $self->x_resource ("background.expr") |
860 | my $expr = $self->x_resource ("%.expr") |
783 | or return; |
861 | or return; |
784 | |
862 | |
|
|
863 | $self->has_render |
|
|
864 | or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n"; |
|
|
865 | |
785 | $self->set_expr (parse_expr $expr); |
866 | $self->set_expr (parse_expr $expr); |
786 | $self->{border} = $self->x_resource_boolean ("background.border"); |
867 | $self->{border} = $self->x_resource_boolean ("%.border"); |
|
|
868 | |
|
|
869 | $MIN_INTERVAL = $self->x_resource ("%.interval"); |
787 | |
870 | |
788 | () |
871 | () |
789 | } |
872 | } |
790 | |
873 | |