ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/background
(Generate patch)

Comparing rxvt-unicode/src/perl/background (file contents):
Revision 1.43 by root, Sun Jun 10 11:23:20 2012 UTC vs.
Revision 1.50 by root, Sun Jun 10 17:31:53 2012 UTC

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
19This extension manages the terminal background by creating a picture that 19This extension manages the terminal background by creating a picture that
20is behind the text, replacing the normal background colour. 20is behind the text, replacing the normal background colour.
189overwriting borders and any other areas, such as the scrollbar. 189overwriting borders and any other areas, such as the scrollbar.
190 190
191Specifying this flag changes the behaviour, so that the image only 191Specifying this flag changes the behaviour, so that the image only
192replaces the background of the character area. 192replaces the background of the character area.
193 193
194=item --background-interval seconds
195
196Since some operations in the underlying XRender extension can effetively
197freeze your X-server for prolonged time, this extension enforces a minimum
198time between updates, which is normally about 0.1 seconds.
199
200If you want to do updates more often, you can decrease this safety
201interval with this switch.
202
194=back 203=back
195 204
196=cut 205=cut
197 206
207our %_IMGCACHE;
198our $HOME; 208our $HOME;
199our ($self, $old, $new); 209our ($self, $old, $new);
200our ($x, $y, $w, $h); 210our ($x, $y, $w, $h);
201 211
202# enforce at least this interval between updates 212# enforce at least this interval between updates
203our $MIN_INTERVAL = 1/100; 213our $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
280Returns an exact copy of the image. This is useful if you want to have
281multiple 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
293The following operators modify the tiling mode of an image, that is, the
294way that pixels outside the image area are painted when the image is used.
295
296=over 4
297
298=item tile $img
299
300Tiles the whole plane with the image and returns this new image - or in
301other words, it returns a copy of the image in plane tiling mode.
302
303Example: load an image and tile it over the background, without
304resizing. The C<tile> call is superfluous because C<load> already defaults
305to tiling mode.
306
307 tile load "mybg.png"
308
309=item mirror $img
310
311Similar to tile, but reflects the image each time it uses a new copy, so
312that top edges always touch top edges, right edges always touch right
313edges and so on (with normal tiling, left edges always touch right edges
314and top always touch bottom edges).
315
316Example: load an image and mirror it over the background, avoiding sharp
317edges at the image borders at the expense of mirroring the image itself
318
319 mirror load "mybg.png"
320
321=item pad $img
322
323Takes an image and modifies it so that all pixels outside the image area
324become transparent. This mode is most useful when you want to place an
325image over another image or the background colour while leaving all
326background pixels outside the image unchanged.
327
328Example: load an image and display it in the upper left corner. The rest
329of the space is left "empty" (transparent or wahtever your compisotr does
330in alpha mode, else background colour).
331
332 pad load "mybg.png"
333
334=item extend $img
335
336Extends the image over the whole plane, using the closest pixel in the
337area outside the image. This mode is mostly useful when you more complex
338filtering operations and want the pixels outside the image to have the
339same values as the pixels near the edge.
340
341Example: 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
272The following functions provide variable data such as the terminal window 375The following functions provide variable data such as the terminal window
273dimensions. They are not (Perl-) variables, they jsut return stuff that 376dimensions. They are not (Perl-) variables, they just return stuff that
274varies. Most of them make your expression sensitive to some events, for 377varies. Most of them make your expression sensitive to some events, for
275example using C<TW> (terminal width) means your expression is evaluated 378example using C<TW> (terminal width) means your expression is evaluated
276again when the terminal is resized. 379again 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
359The following operators modify the tiling mode of an image, that is, the 462The following operators modify the shape, size or position of the image.
360way 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
366Tiles the whole plane with the image and returns this new image - or in
367other words, it returns a copy of the image in plane tiling mode.
368
369Example: load an image and tile it over the background, without
370resizing. The C<tile> call is superfluous because C<load> already defaults
371to tiling mode.
372
373 tile load "mybg.png"
374
375=item mirror $img
376
377Similar to tile, but reflects the image each time it uses a new copy, so
378that top edges always touch top edges, right edges always touch right
379edges and so on (with normal tiling, left edges always touch right edges
380and top always touch bottom edges).
381
382Example: load an image and mirror it over the background, avoiding sharp
383edges at the image borders at the expense of mirroring the image itself
384
385 mirror load "mybg.png"
386
387=item pad $img
388
389Takes an image and modifies it so that all pixels outside the image area
390become transparent. This mode is most useful when you want to place an
391image over another image or the background colour while leaving all
392background pixels outside the image unchanged.
393
394Example: load an image and display it in the upper left corner. The rest
395of the space is left "empty" (transparent or wahtever your compisotr does
396in alpha mode, else background colour).
397
398 pad load "mybg.png"
399
400=item extend $img
401
402Extends the image over the whole plane, using the closest pixel in the
403area outside the image. This mode is mostly useful when you more complex
404filtering operations and want the pixels outside the image to have the
405same values as the pixels near the edge.
406
407Example: 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
441The following operators modify the image pixels in various ways.
442
443=over 4
444
445=item clone $img
446
447Returns 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
553Example: move the image right by 20 pixels and down by 30. 564Example: 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
570Aligns the image according to a factor - C<0> means the image is moved to
571the left or top edge (for C<$xalign> or C<$yalign>), C<0.5> means it is
572exactly centered and C<1> means it touches the right or bottom edge.
573
574Example: remove any visible border around an image, center it vertically but move
575it 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
583Centers the image, i.e. the center of the image is moved to the center of
584the terminal window (or the box specified by C<$width> and C<$height> if
585given).
586
587Example: load an image and center it.
588
589 center pad load "mybg.png"
590
557=item rootalign $img 591=item rootalign $img
558 592
559Moves the image so that it appears glued to the screen as opposed to the 593Moves the image so that it appears glued to the screen as opposed to the
560window. This gives the illusion of a larger area behind the window. It is 594window. This gives the illusion of a larger area behind the window. It is
561exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the 595exactly 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
568Example: take the screen background and align it, giving the illusion of 602Example: take the screen background and align it, giving the illusion of
569transparency as long as the window isn't in front of other windows. 603transparency 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
639The 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
591Adjusts the I<contrast> of an image. 649Adjusts the I<contrast> of an image.
592 650
593#TODO# 651The first form applies a single C<$factor> to red, green and blue, the
652second form applies separate factors to each colour channel, and the last
653form includes the alpha channel.
594 654
655Values from 0 to 1 lower the contrast, values higher than 1 increase the
656contrast.
657
658Due to limitations in the underlying XRender extension, lowering contrast
659also reduces brightness, while increasing contrast currently also
660increases 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
601Adjusts the brightness of an image. 668Adjusts the brightness of an image.
602 669
670The first form applies a single C<$bias> to red, green and blue, the
671second form applies separate biases to each colour channel, and the last
672form includes the alpha channel.
673
674Values less than 0 reduce brightness, while values larger than 0 increase
675it. Useful range is from -1 to 1 - the former results in a black, the
676latter in a white picture.
677
678Due to idiosynchrasies in the underlying XRender extension, biases less
679than 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
779sub on_start { 857sub 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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines