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.49 by root, Sun Jun 10 15:29:18 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#:META:X_RESOURCE:%.interval:seconds:minimum time between updates
5 6
6#TODO: once, rootalign 7#TODO: once, rootalign
7 8
8=head1 NAME 9=head1 NAME
9 10
11 12
12=head1 SYNOPSIS 13=head1 SYNOPSIS
13 14
14 urxvt --background-expr 'background expression' 15 urxvt --background-expr 'background expression'
15 --background-border 16 --background-border
17 --background-interval seconds
16 18
17=head1 DESCRIPTION 19=head1 DESCRIPTION
18 20
19This extension manages the terminal background by creating a picture that 21This extension manages the terminal background by creating a picture that
20is behind the text, replacing the normal background colour. 22is behind the text, replacing the normal background colour.
189overwriting borders and any other areas, such as the scrollbar. 191overwriting borders and any other areas, such as the scrollbar.
190 192
191Specifying this flag changes the behaviour, so that the image only 193Specifying this flag changes the behaviour, so that the image only
192replaces the background of the character area. 194replaces the background of the character area.
193 195
196=item --background-interval seconds
197
198Since some operations in the underlying XRender extension can effetively
199freeze your X-server for prolonged time, this extension enforces a minimum
200time between updates, which is normally about 0.1 seconds.
201
202If you want to do updates more often, you can decrease this safety
203interval with this switch.
204
194=back 205=back
195 206
196=cut 207=cut
197 208
209our %_IMGCACHE;
198our $HOME; 210our $HOME;
199our ($self, $old, $new); 211our ($self, $old, $new);
200our ($x, $y, $w, $h); 212our ($x, $y, $w, $h);
201 213
202# enforce at least this interval between updates 214# enforce at least this interval between updates
203our $MIN_INTERVAL = 1/100; 215our $MIN_INTERVAL = 6/59.951;
204 216
205{ 217{
206 package urxvt::bgdsl; # background language 218 package urxvt::bgdsl; # background language
207 219
208 use List::Util qw(min max sum shuffle); 220 use List::Util qw(min max sum shuffle);
263 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); 275 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1);
264 $img->fill ($colour); 276 $img->fill ($colour);
265 $img 277 $img
266 } 278 }
267 279
280=item clone $img
281
282Returns an exact copy of the image. This is useful if you want to have
283multiple copies of the same image to apply different effects to.
284
285=cut
286
287 sub clone($) {
288 $_[0]->clone
289 }
290
268=back 291=back
269 292
293=head2 TILING MODES
294
295The following operators modify the tiling mode of an image, that is, the
296way that pixels outside the image area are painted when the image is used.
297
298=over 4
299
300=item tile $img
301
302Tiles the whole plane with the image and returns this new image - or in
303other words, it returns a copy of the image in plane tiling mode.
304
305Example: load an image and tile it over the background, without
306resizing. The C<tile> call is superfluous because C<load> already defaults
307to tiling mode.
308
309 tile load "mybg.png"
310
311=item mirror $img
312
313Similar to tile, but reflects the image each time it uses a new copy, so
314that top edges always touch top edges, right edges always touch right
315edges and so on (with normal tiling, left edges always touch right edges
316and top always touch bottom edges).
317
318Example: load an image and mirror it over the background, avoiding sharp
319edges at the image borders at the expense of mirroring the image itself
320
321 mirror load "mybg.png"
322
323=item pad $img
324
325Takes an image and modifies it so that all pixels outside the image area
326become transparent. This mode is most useful when you want to place an
327image over another image or the background colour while leaving all
328background pixels outside the image unchanged.
329
330Example: load an image and display it in the upper left corner. The rest
331of the space is left "empty" (transparent or wahtever your compisotr does
332in alpha mode, else background colour).
333
334 pad load "mybg.png"
335
336=item extend $img
337
338Extends the image over the whole plane, using the closest pixel in the
339area outside the image. This mode is mostly useful when you more complex
340filtering operations and want the pixels outside the image to have the
341same values as the pixels near the edge.
342
343Example: just for curiosity, how does this pixel extension stuff work?
344
345 extend move 50, 50, load "mybg.png"
346
347=cut
348
349 sub pad($) {
350 my $img = $_[0]->clone;
351 $img->repeat_mode (urxvt::RepeatNone);
352 $img
353 }
354
355 sub tile($) {
356 my $img = $_[0]->clone;
357 $img->repeat_mode (urxvt::RepeatNormal);
358 $img
359 }
360
361 sub mirror($) {
362 my $img = $_[0]->clone;
363 $img->repeat_mode (urxvt::RepeatReflect);
364 $img
365 }
366
367 sub extend($) {
368 my $img = $_[0]->clone;
369 $img->repeat_mode (urxvt::RepeatPad);
370 $img
371 }
372
373=back
374
270=head2 VARIABLES 375=head2 VARIABLE VALUES
271 376
272The following functions provide variable data such as the terminal window 377The following functions provide variable data such as the terminal window
273dimensions. They are not (Perl-) variables, they jsut return stuff that 378dimensions. They are not (Perl-) variables, they just return stuff that
274varies. Most of them make your expression sensitive to some events, for 379varies. Most of them make your expression sensitive to some events, for
275example using C<TW> (terminal width) means your expression is evaluated 380example using C<TW> (terminal width) means your expression is evaluated
276again when the terminal is resized. 381again when the terminal is resized.
277 382
278=over 4 383=over 4
352 $self->{counter} + 0 457 $self->{counter} + 0
353 } 458 }
354 459
355=back 460=back
356 461
357=head2 TILING MODES 462=head2 SHAPE CHANGING OPERATORS
358 463
359The following operators modify the tiling mode of an image, that is, the 464The 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 465
362=over 4 466=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 467
455=item clip $img 468=item clip $img
456 469
457=item clip $width, $height, $img 470=item clip $width, $height, $img
458 471
552 565
553Example: move the image right by 20 pixels and down by 30. 566Example: move the image right by 20 pixels and down by 30.
554 567
555 move 20, 30, ... 568 move 20, 30, ...
556 569
570=item align $xalign, $yalign, $img
571
572Aligns the image according to a factor - C<0> means the image is moved to
573the left or top edge (for C<$xalign> or C<$yalign>), C<0.5> means it is
574exactly centered and C<1> means it touches the right or bottom edge.
575
576Example: remove any visible border around an image, center it vertically but move
577it to the right hand side.
578
579 align 1, 0.5, pad $img
580
557=item center $img 581=item center $img
558 582
559=item center $width, $height, $img 583=item center $width, $height, $img
560 584
561Centers the image, i.e. the center of the image is moved to the center of 585Centers 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 586the terminal window (or the box specified by C<$width> and C<$height> if
563given). 587given).
588
589Example: load an image and center it.
590
591 center pad load "mybg.png"
564 592
565=item rootalign $img 593=item rootalign $img
566 594
567Moves the image so that it appears glued to the screen as opposed to the 595Moves 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 596window. This gives the illusion of a larger area behind the window. It is
574 rootalign mirror load "mybg.png" 602 rootalign mirror load "mybg.png"
575 603
576Example: take the screen background and align it, giving the illusion of 604Example: take the screen background and align it, giving the illusion of
577transparency as long as the window isn't in front of other windows. 605transparency as long as the window isn't in front of other windows.
578 606
579 rootalign root 607 rootalign root
580 608
581=cut 609=cut
582 610
583 sub move($$;$) { 611 sub move($$;$) {
584 my $img = pop->clone; 612 my $img = pop->clone;
585 $img->move ($_[0], $_[1]); 613 $img->move ($_[0], $_[1]);
586 $img 614 $img
587 } 615 }
588 616
617 sub align($;$$) {
618 my $img = pop;
619
620 move $_[0] * (TW - $img->w),
621 $_[1] * (TH - $img->h),
622 $img
623 }
624
589 sub center($;$$) { 625 sub center($;$$) {
590 my $img = pop; 626 my $img = pop;
591 my $w = $_[0] || TW; 627 my $w = $_[0] || TW;
592 my $h = $_[0] || TH; 628 my $h = $_[1] || TH;
593 629
594 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img 630 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img
595 } 631 }
596 632
597 sub rootalign($) { 633 sub rootalign($) {
598 move -TX, -TY, $_[0] 634 move -TX, -TY, $_[0]
599 } 635 }
600 636
637=back
638
639=head2 COLOUR MODIFICATIONS
640
641The following operators change the pixels of the image.
642
643=over 4
644
601=item contrast $factor, $img 645=item contrast $factor, $img
602 646
603=item contrast $r, $g, $b, $img 647=item contrast $r, $g, $b, $img
604 648
605=item contrast $r, $g, $b, $a, $img 649=item contrast $r, $g, $b, $a, $img
606 650
607Adjusts the I<contrast> of an image. 651Adjusts the I<contrast> of an image.
608 652
609#TODO# 653The first form applies a single C<$factor> to red, green and blue, the
654second form applies separate factors to each colour channel, and the last
655form includes the alpha channel.
610 656
657Values from 0 to 1 lower the contrast, values higher than 1 increase the
658contrast.
659
660Due to limitations in the underlying XRender extension, lowering contrast
661also reduces brightness, while increasing contrast currently also
662increases brightness.
663
611=item brightness $factor, $img 664=item brightness $bias, $img
612 665
613=item brightness $r, $g, $b, $img 666=item brightness $r, $g, $b, $img
614 667
615=item brightness $r, $g, $b, $a, $img 668=item brightness $r, $g, $b, $a, $img
616 669
617Adjusts the brightness of an image. 670Adjusts the brightness of an image.
618 671
672The first form applies a single C<$bias> to red, green and blue, the
673second form applies separate biases to each colour channel, and the last
674form includes the alpha channel.
675
676Values less than 0 reduce brightness, while values larger than 0 increase
677it. Useful range is from -1 to 1 - the former results in a black, the
678latter in a white picture.
679
680Due to idiosynchrasies in the underlying XRender extension, biases less
681than zero can be I<very> slow.
682
619=cut 683=cut
620 684
621 sub contrast($$;$$;$) { 685 sub contrast($$;$$;$) {
622 my $img = pop; 686 my $img = pop;
623 my ($r, $g, $b, $a) = @_; 687 my ($r, $g, $b, $a) = @_;
624 688
625 ($g, $b) = ($r, $r) if @_ < 4; 689 ($g, $b) = ($r, $r) if @_ < 3;
626 $a = 1 if @_ < 5; 690 $a = 1 if @_ < 4;
627 691
628 $img = $img->clone; 692 $img = $img->clone;
629 $img->contrast ($r, $g, $b, $a); 693 $img->contrast ($r, $g, $b, $a);
630 $img 694 $img
631 } 695 }
632 696
633 sub brightness($$;$$;$) { 697 sub brightness($$;$$;$) {
634 my $img = pop; 698 my $img = pop;
635 my ($r, $g, $b, $a) = @_; 699 my ($r, $g, $b, $a) = @_;
636 700
637 ($g, $b) = ($r, $r) if @_ < 4; 701 ($g, $b) = ($r, $r) if @_ < 3;
638 $a = 1 if @_ < 5; 702 $a = 1 if @_ < 4;
639 703
640 $img = $img->clone; 704 $img = $img->clone;
641 $img->brightness ($r, $g, $b, $a); 705 $img->brightness ($r, $g, $b, $a);
642 $img 706 $img
643 } 707 }
733 797
734 # evaluate user expression 798 # evaluate user expression
735 799
736 my $img = eval { $self->{expr}->() }; 800 my $img = eval { $self->{expr}->() };
737 warn $@ if $@;#d# 801 warn $@ if $@;#d#
738 die if !UNIVERSAL::isa $img, "urxvt::img"; 802 die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img";
739 803
740 $state->{size_sensitive} = 1 804 $state->{size_sensitive} = 1
741 if $img->repeat_mode != urxvt::RepeatNormal; 805 if $img->repeat_mode != urxvt::RepeatNormal;
742 806
743 # if the expression is sensitive to external events, prepare reevaluation then 807 # if the expression is sensitive to external events, prepare reevaluation then
793} 857}
794 858
795sub on_start { 859sub on_start {
796 my ($self) = @_; 860 my ($self) = @_;
797 861
798 my $expr = $self->x_resource ("background.expr") 862 my $expr = $self->x_resource ("%.expr")
799 or return; 863 or return;
800 864
865 $self->has_render
866 or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n";
867
801 $self->set_expr (parse_expr $expr); 868 $self->set_expr (parse_expr $expr);
802 $self->{border} = $self->x_resource_boolean ("background.border"); 869 $self->{border} = $self->x_resource_boolean ("%.border");
870
871 $MIN_INTERVAL = $self->x_resource ("%.interval");
803 872
804 () 873 ()
805} 874}
806 875

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines