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.44 by root, Sun Jun 10 11:31:22 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
557=item center $img 579=item center $img
558 580
559=item center $width, $height, $img 581=item center $width, $height, $img
560 582
561Centers the image, i.e. the center of the image is moved to the center of 583Centers the image, i.e. the center of the image is moved to the center of
562the terminal window (or the box specified by C<$width> and C<$height> if 584the terminal window (or the box specified by C<$width> and C<$height> if
563given). 585given).
586
587Example: load an image and center it.
588
589 center pad load "mybg.png"
564 590
565=item rootalign $img 591=item rootalign $img
566 592
567Moves 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
568window. 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
574 rootalign mirror load "mybg.png" 600 rootalign mirror load "mybg.png"
575 601
576Example: take the screen background and align it, giving the illusion of 602Example: take the screen background and align it, giving the illusion of
577transparency 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.
578 604
579 rootalign root 605 rootalign root
580 606
581=cut 607=cut
582 608
583 sub move($$;$) { 609 sub move($$;$) {
584 my $img = pop->clone; 610 my $img = pop->clone;
585 $img->move ($_[0], $_[1]); 611 $img->move ($_[0], $_[1]);
586 $img 612 $img
587 } 613 }
588 614
615 sub align($;$$) {
616 my $img = pop;
617
618 move $_[0] * (TW - $img->w),
619 $_[1] * (TH - $img->h),
620 $img
621 }
622
589 sub center($;$$) { 623 sub center($;$$) {
590 my $img = pop; 624 my $img = pop;
591 my $w = $_[0] || TW; 625 my $w = $_[0] || TW;
592 my $h = $_[0] || TH; 626 my $h = $_[1] || TH;
593 627
594 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img 628 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img
595 } 629 }
596 630
597 sub rootalign($) { 631 sub rootalign($) {
598 move -TX, -TY, $_[0] 632 move -TX, -TY, $_[0]
599 } 633 }
600 634
635=back
636
637=head2 COLOUR MODIFICATIONS
638
639The following operators change the pixels of the image.
640
641=over 4
642
601=item contrast $factor, $img 643=item contrast $factor, $img
602 644
603=item contrast $r, $g, $b, $img 645=item contrast $r, $g, $b, $img
604 646
605=item contrast $r, $g, $b, $a, $img 647=item contrast $r, $g, $b, $a, $img
606 648
607Adjusts the I<contrast> of an image. 649Adjusts the I<contrast> of an image.
608 650
609#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.
610 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
611=item brightness $factor, $img 662=item brightness $bias, $img
612 663
613=item brightness $r, $g, $b, $img 664=item brightness $r, $g, $b, $img
614 665
615=item brightness $r, $g, $b, $a, $img 666=item brightness $r, $g, $b, $a, $img
616 667
617Adjusts the brightness of an image. 668Adjusts the brightness of an image.
618 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
619=cut 681=cut
620 682
621 sub contrast($$;$$;$) { 683 sub contrast($$;$$;$) {
622 my $img = pop; 684 my $img = pop;
623 my ($r, $g, $b, $a) = @_; 685 my ($r, $g, $b, $a) = @_;
624 686
625 ($g, $b) = ($r, $r) if @_ < 4; 687 ($g, $b) = ($r, $r) if @_ < 3;
626 $a = 1 if @_ < 5; 688 $a = 1 if @_ < 4;
627 689
628 $img = $img->clone; 690 $img = $img->clone;
629 $img->contrast ($r, $g, $b, $a); 691 $img->contrast ($r, $g, $b, $a);
630 $img 692 $img
631 } 693 }
632 694
633 sub brightness($$;$$;$) { 695 sub brightness($$;$$;$) {
634 my $img = pop; 696 my $img = pop;
635 my ($r, $g, $b, $a) = @_; 697 my ($r, $g, $b, $a) = @_;
636 698
637 ($g, $b) = ($r, $r) if @_ < 4; 699 ($g, $b) = ($r, $r) if @_ < 3;
638 $a = 1 if @_ < 5; 700 $a = 1 if @_ < 4;
639 701
640 $img = $img->clone; 702 $img = $img->clone;
641 $img->brightness ($r, $g, $b, $a); 703 $img->brightness ($r, $g, $b, $a);
642 $img 704 $img
643 } 705 }
733 795
734 # evaluate user expression 796 # evaluate user expression
735 797
736 my $img = eval { $self->{expr}->() }; 798 my $img = eval { $self->{expr}->() };
737 warn $@ if $@;#d# 799 warn $@ if $@;#d#
738 die if !UNIVERSAL::isa $img, "urxvt::img"; 800 die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img";
739 801
740 $state->{size_sensitive} = 1 802 $state->{size_sensitive} = 1
741 if $img->repeat_mode != urxvt::RepeatNormal; 803 if $img->repeat_mode != urxvt::RepeatNormal;
742 804
743 # if the expression is sensitive to external events, prepare reevaluation then 805 # if the expression is sensitive to external events, prepare reevaluation then
793} 855}
794 856
795sub on_start { 857sub on_start {
796 my ($self) = @_; 858 my ($self) = @_;
797 859
798 my $expr = $self->x_resource ("background.expr") 860 my $expr = $self->x_resource ("%.expr")
799 or return; 861 or return;
800 862
863 $self->has_render
864 or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n";
865
801 $self->set_expr (parse_expr $expr); 866 $self->set_expr (parse_expr $expr);
802 $self->{border} = $self->x_resource_boolean ("background.border"); 867 $self->{border} = $self->x_resource_boolean ("%.border");
868
869 $MIN_INTERVAL = $self->x_resource ("%.interval");
803 870
804 () 871 ()
805} 872}
806 873

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines