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.71 by root, Mon Jul 2 01:40:41 2012 UTC vs.
Revision 1.85 by root, Thu Oct 3 01:11:48 2013 UTC

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#:META:X_RESOURCE:%.interval:seconds:minimum time between updates
6 6
7=head1 NAME 7=head1 NAME
8 8
9 background - manage terminal background 9background - manage terminal background
10 10
11=head1 SYNOPSIS 11=head1 SYNOPSIS
12 12
13 urxvt --background-expr 'background expression' 13 urxvt --background-expr 'background expression'
14 --background-border 14 --background-border
15 --background-interval seconds 15 --background-interval seconds
16
17=head1 QUICK AND DIRTY CHEAT SHEET
18
19Just load a random jpeg image and tile the background with it without
20scaling or anything else:
21
22 load "/path/to/img.jpg"
23
24The same, but use mirroring/reflection instead of tiling:
25
26 mirror load "/path/to/img.jpg"
27
28Load an image and scale it to exactly fill the terminal window:
29
30 scale keep { load "/path/to/img.jpg" }
31
32Implement pseudo-transparency by using a suitably-aligned root pixmap
33as window background:
34
35 rootalign root
36
37Likewise, but keep a blurred copy:
38
39 rootalign keep { blur 10, root }
16 40
17=head1 DESCRIPTION 41=head1 DESCRIPTION
18 42
19This extension manages the terminal background by creating a picture that 43This extension manages the terminal background by creating a picture that
20is behind the text, replacing the normal background colour. 44is behind the text, replacing the normal background colour.
74 return load "$HOME/sunday.png"; 98 return load "$HOME/sunday.png";
75 } 99 }
76 } 100 }
77 101
78This inner expression is evaluated once per hour (and whenever the 102This inner expression is evaluated once per hour (and whenever the
79temrinal window is resized). It sets F<sunday.png> as background on 103terminal window is resized). It sets F<sunday.png> as background on
80Sundays, and F<weekday.png> on all other days. 104Sundays, and F<weekday.png> on all other days.
81 105
82Fortunately, we expect that most expressions will be much simpler, with 106Fortunately, we expect that most expressions will be much simpler, with
83little Perl knowledge needed. 107little Perl knowledge needed.
84 108
119width and doubles the image height: 143width and doubles the image height:
120 144
121 scale 0.5, 2, load "$HOME/mypic.png" 145 scale 0.5, 2, load "$HOME/mypic.png"
122 146
123IF you try out these expressions, you might suffer from some sluggishness, 147IF you try out these expressions, you might suffer from some sluggishness,
124because each time the terminal is resized, it loads the PNG image agin 148because each time the terminal is resized, it loads the PNG image again
125and scales it. Scaling is usually fast (and unavoidable), but loading the 149and scales it. Scaling is usually fast (and unavoidable), but loading the
126image can be quite time consuming. This is where C<keep> comes in handy: 150image can be quite time consuming. This is where C<keep> comes in handy:
127 151
128 scale 0.5, 2, keep { load "$HOME/mypic.png" } 152 scale 0.5, 2, keep { load "$HOME/mypic.png" }
129 153
159left corner of the terminal window)- the result is pseudo-transparency: 183left corner of the terminal window)- the result is pseudo-transparency:
160the image seems to be static while the window is moved around. 184the image seems to be static while the window is moved around.
161 185
162=head2 COLOUR SPECIFICATIONS 186=head2 COLOUR SPECIFICATIONS
163 187
164Whenever an oprator expects a "colour", then this can be specified in one 188Whenever an operator expects a "colour", then this can be specified in one
165of two ways: Either as string with an X11 colour specification, such as: 189of two ways: Either as string with an X11 colour specification, such as:
166 190
167 "red" # named colour 191 "red" # named colour
168 "#f00" # simple rgb 192 "#f00" # simple rgb
169 "[50]red" # red with 50% alpha 193 "[50]red" # red with 50% alpha
255=cut 279=cut
256 280
257our %_IMG_CACHE; 281our %_IMG_CACHE;
258our $HOME; 282our $HOME;
259our ($self, $frame); 283our ($self, $frame);
260our ($x, $y, $w, $h); 284our ($x, $y, $w, $h, $focus);
261 285
262# enforce at least this interval between updates 286# enforce at least this interval between updates
263our $MIN_INTERVAL = 6/59.951; 287our $MIN_INTERVAL = 6/59.951;
264 288
265{ 289{
284 308
285Loads the image at the given C<$path>. The image is set to plane tiling 309Loads the image at the given C<$path>. The image is set to plane tiling
286mode. 310mode.
287 311
288If the image is already in memory (e.g. because another terminal instance 312If the image is already in memory (e.g. because another terminal instance
289uses it), then the in-memory copy us returned instead. 313uses it), then the in-memory copy is returned instead.
290 314
291=item load_uc $path 315=item load_uc $path
292 316
293Load uncached - same as load, but does not cache the image, which means it 317Load uncached - same as load, but does not cache the image, which means it
294is I<always> loaded from the filesystem again. 318is I<always> loaded from the filesystem again, even if another copy of it
319is in memory at the time.
295 320
296=cut 321=cut
322
323 sub load_uc($) {
324 $self->new_img_from_file ($_[0])
325 }
297 326
298 sub load($) { 327 sub load($) {
299 my ($path) = @_; 328 my ($path) = @_;
300 329
301 $_IMG_CACHE{$path} || do { 330 $_IMG_CACHE{$path} || do {
302 my $img = $self->new_img_from_file ($path); 331 my $img = load_uc $path;
303 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img); 332 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img);
304 $img 333 $img
305 } 334 }
306 } 335 }
307 336
392 $base->draw ($_) 421 $base->draw ($_)
393 for @_; 422 for @_;
394 423
395 $base 424 $base
396 } 425 }
426
427=back
397 428
398=head2 TILING MODES 429=head2 TILING MODES
399 430
400The following operators modify the tiling mode of an image, that is, the 431The following operators modify the tiling mode of an image, that is, the
401way that pixels outside the image area are painted when the image is used. 432way that pixels outside the image area are painted when the image is used.
493 524
494Return the X and Y coordinates of the terminal window (the terminal 525Return the X and Y coordinates of the terminal window (the terminal
495window is the full window by default, and the character area only when in 526window is the full window by default, and the character area only when in
496border-respect mode). 527border-respect mode).
497 528
498Using these functions make your expression sensitive to window moves. 529Using these functions makes your expression sensitive to window moves.
499 530
500These functions are mainly useful to align images to the root window. 531These functions are mainly useful to align images to the root window.
501 532
502Example: load an image and align it so it looks as if anchored to the 533Example: load an image and align it so it looks as if anchored to the
503background (that's exactly what C<rootalign> does btw.): 534background (that's exactly what C<rootalign> does btw.):
504 535
505 move -TX, -TY, keep { load "mybg.png" } 536 move -TX, -TY, keep { load "mybg.png" }
506 537
507=item TW 538=item TW
539
540=item TH
508 541
509Return the width (C<TW>) and height (C<TH>) of the terminal window (the 542Return the width (C<TW>) and height (C<TH>) of the terminal window (the
510terminal window is the full window by default, and the character area only 543terminal window is the full window by default, and the character area only
511when in border-respect mode). 544when in border-respect mode).
512 545
513Using these functions make your expression sensitive to window resizes. 546Using these functions makes your expression sensitive to window resizes.
514 547
515These functions are mainly useful to scale images, or to clip images to 548These functions are mainly useful to scale images, or to clip images to
516the window size to conserve memory. 549the window size to conserve memory.
517 550
518Example: take the screen background, clip it to the window size, blur it a 551Example: take the screen background, clip it to the window size, blur it a
519bit, align it to the window position and use it as background. 552bit, align it to the window position and use it as background.
520 553
521 clip move -TX, -TY, keep { blur 5, root } 554 clip move -TX, -TY, keep { blur 5, root }
522 555
523=cut 556=item FOCUS
524 557
558Returns a boolean indicating whether the terminal window has keyboard
559focus, in which case it returns true.
560
561Using this function makes your expression sensitive to focus changes.
562
563A common use case is to fade the background image when the terminal loses
564focus, often together with the C<-fade> command line option. In fact,
565there is a special function for just that use case: C<focus_fade>.
566
567Example: use two entirely different bacckground images, depending on
568whether the window has focus.
569
570 FOCUS ? keep { load "has_focus.jpg" } : keep { load "no_focus.jpg" }
571
572=cut
573
525 sub TX() { $frame->[FR_AGAIN]{position} = 1; $x } 574 sub TX () { $frame->[FR_AGAIN]{position} = 1; $x }
526 sub TY() { $frame->[FR_AGAIN]{position} = 1; $y } 575 sub TY () { $frame->[FR_AGAIN]{position} = 1; $y }
527 sub TW() { $frame->[FR_AGAIN]{size} = 1; $w } 576 sub TW () { $frame->[FR_AGAIN]{size} = 1; $w }
528 sub TH() { $frame->[FR_AGAIN]{size} = 1; $h } 577 sub TH () { $frame->[FR_AGAIN]{size} = 1; $h }
578 sub FOCUS() { $frame->[FR_AGAIN]{focus} = 1; $focus }
529 579
530=item now 580=item now
531 581
532Returns the current time as (fractional) seconds since the epoch. 582Returns the current time as (fractional) seconds since the epoch.
533 583
580Clips an image to the given rectangle. If the rectangle is outside the 630Clips an image to the given rectangle. If the rectangle is outside the
581image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is 631image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is
582larger than the image, then the tiling mode defines how the extra pixels 632larger than the image, then the tiling mode defines how the extra pixels
583will be filled. 633will be filled.
584 634
585If C<$x> an C<$y> are missing, then C<0> is assumed for both. 635If C<$x> and C<$y> are missing, then C<0> is assumed for both.
586 636
587If C<$width> and C<$height> are missing, then the window size will be 637If C<$width> and C<$height> are missing, then the window size will be
588assumed. 638assumed.
589 639
590Example: load an image, blur it, and clip it to the window size to save 640Example: load an image, blur it, and clip it to the window size to save
608=item scale $width_factor, $height_factor, $img 658=item scale $width_factor, $height_factor, $img
609 659
610Scales the image by the given factors in horizontal 660Scales the image by the given factors in horizontal
611(C<$width>) and vertical (C<$height>) direction. 661(C<$width>) and vertical (C<$height>) direction.
612 662
613If only one factor is give, it is used for both directions. 663If only one factor is given, it is used for both directions.
614 664
615If no factors are given, scales the image to the window size without 665If no factors are given, scales the image to the window size without
616keeping aspect. 666keeping aspect.
617 667
618=item resize $width, $height, $img 668=item resize $width, $height, $img
743=item rotate $center_x, $center_y, $degrees, $img 793=item rotate $center_x, $center_y, $degrees, $img
744 794
745Rotates the image clockwise by C<$degrees> degrees, around the point at 795Rotates the image clockwise by C<$degrees> degrees, around the point at
746C<$center_x> and C<$center_y> (specified as factor of image width/height). 796C<$center_x> and C<$center_y> (specified as factor of image width/height).
747 797
748Example: rotate the image by 90 degrees around it's center. 798Example: rotate the image by 90 degrees around its center.
749 799
750 rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" } 800 rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" }
751 801
752=cut 802=cut
753 803
784 834
785 sub tint($$) { 835 sub tint($$) {
786 $_[1]->tint ($_[0]) 836 $_[1]->tint ($_[0])
787 } 837 }
788 838
839=item shade $factor, $img
840
841Shade the image by the given factor.
842
843=cut
844
845 sub shade($$) {
846 $_[1]->shade ($_[0])
847 }
848
789=item contrast $factor, $img 849=item contrast $factor, $img
790 850
791=item contrast $r, $g, $b, $img 851=item contrast $r, $g, $b, $img
792 852
793=item contrast $r, $g, $b, $a, $img 853=item contrast $r, $g, $b, $a, $img
822latter in a white picture. 882latter in a white picture.
823 883
824Due to idiosyncrasies in the underlying XRender extension, biases less 884Due to idiosyncrasies in the underlying XRender extension, biases less
825than zero can be I<very> slow. 885than zero can be I<very> slow.
826 886
887You can also try the experimental(!) C<muladd> operator.
888
827=cut 889=cut
828 890
829 sub contrast($$;$$;$) { 891 sub contrast($$;$$;$) {
830 my $img = pop; 892 my $img = pop;
831 my ($r, $g, $b, $a) = @_; 893 my ($r, $g, $b, $a) = @_;
846 $a = 1 if @_ < 4; 908 $a = 1 if @_ < 4;
847 909
848 $img = $img->clone; 910 $img = $img->clone;
849 $img->brightness ($r, $g, $b, $a); 911 $img->brightness ($r, $g, $b, $a);
850 $img 912 $img
913 }
914
915=item muladd $mul, $add, $img # EXPERIMENTAL
916
917First multiplies the pixels by C<$mul>, then adds C<$add>. This can be used
918to implement brightness and contrast at the same time, with a wider value
919range than contrast and brightness operators.
920
921Due to numerous bugs in XRender implementations, it can also introduce a
922number of visual artifacts.
923
924Example: increase contrast by a factor of C<$c> without changing image
925brightness too much.
926
927 muladd $c, (1 - $c) * 0.5, $img
928
929=cut
930
931 sub muladd($$$) {
932 $_[2]->muladd ($_[0], $_[1])
851 } 933 }
852 934
853=item blur $radius, $img 935=item blur $radius, $img
854 936
855=item blur $radius_horz, $radius_vert, $img 937=item blur $radius_horz, $radius_vert, $img
865=cut 947=cut
866 948
867 sub blur($$;$) { 949 sub blur($$;$) {
868 my $img = pop; 950 my $img = pop;
869 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) 951 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0])
952 }
953
954=item focus_fade $img
955
956=item focus_fade $factor, $img
957
958=item focus_fade $factor, $color, $img
959
960Fades the image by the given factor (and colour) when focus is lost (the
961same as the C<-fade>/C<-fadecolor> command line options, which also supply
962the default values for C<factor> and C<$color>. Unlike with C<-fade>, the
963C<$factor> is the real value, not a percentage value (that is, 0..1, not
9640..100).
965
966Example: do the right thing when focus fading is requested.
967
968 focus_fade load "mybg.jpg";
969
970=cut
971
972 sub focus_fade($;$$) {
973 my $img = pop;
974
975 return $img
976 if FOCUS;
977
978 my $fade = @_ >= 1 ? $_[0] : defined $self->resource ("fade") ? $self->resource ("fade") * 0.01 : 0;
979 my $color = @_ >= 2 ? $_[1] : $self->resource ("color+" . urxvt::Color_fade);
980
981 $img = $img->tint ($color) if $color ne "rgb:00/00/00";
982 $img = $img->muladd (1 - $fade, 0) if $fade;
983
984 $img
870 } 985 }
871 986
872=back 987=back
873 988
874=head2 OTHER STUFF 989=head2 OTHER STUFF
900C<keep> block so it only is reevaluated as required. 1015C<keep> block so it only is reevaluated as required.
901 1016
902Putting the blur into a C<keep> block will make sure the blur is only done 1017Putting the blur into a C<keep> block will make sure the blur is only done
903once, while the C<rootalign> is still done each time the window moves. 1018once, while the C<rootalign> is still done each time the window moves.
904 1019
905 rootlign keep { blur 10, root } 1020 rootalign keep { blur 10, root }
906 1021
907This leaves the question of how to force reevaluation of the block, 1022This leaves the question of how to force reevaluation of the block,
908in case the root background changes: If expression inside the block 1023in case the root background changes: If expression inside the block
909is sensitive to some event (root background changes, window geometry 1024is sensitive to some event (root background changes, window geometry
910changes), then it will be reevaluated automatically as needed. 1025changes), then it will be reevaluated automatically as needed.
962 1077
963# compiles a parsed expression 1078# compiles a parsed expression
964sub set_expr { 1079sub set_expr {
965 my ($self, $expr) = @_; 1080 my ($self, $expr) = @_;
966 1081
967 $self->{root} = []; 1082 $self->{root} = []; # the outermost frame
968 $self->{expr} = $expr; 1083 $self->{expr} = $expr;
969 $self->recalculate; 1084 $self->recalculate;
970} 1085}
971 1086
972# takes a hash of sensitivity indicators and installs watchers 1087# takes a hash of sensitivity indicators and installs watchers
1013 if ($again->{rootpmap}) { 1128 if ($again->{rootpmap}) {
1014 $state->{rootpmap} = $self->on (rootpmap_change => $cb); 1129 $state->{rootpmap} = $self->on (rootpmap_change => $cb);
1015 } else { 1130 } else {
1016 delete $state->{rootpmap}; 1131 delete $state->{rootpmap};
1017 } 1132 }
1133
1134 if ($again->{focus}) {
1135 $state->{focus} = $self->on (focus_in => $cb, focus_out => $cb);
1136 } else {
1137 delete $state->{focus};
1138 }
1018} 1139}
1019 1140
1020# evaluate the current bg expression 1141# evaluate the current bg expression
1021sub recalculate { 1142sub recalculate {
1022 my ($arg_self) = @_; 1143 my ($arg_self) = @_;
1034 1155
1035 # set environment to evaluate user expression 1156 # set environment to evaluate user expression
1036 1157
1037 local $self = $arg_self; 1158 local $self = $arg_self;
1038 local $HOME = $ENV{HOME}; 1159 local $HOME = $ENV{HOME};
1039 local $frame = []; 1160 local $frame = $self->{root};
1040 1161
1041 ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); 1162 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
1163 $focus = $self->focus;
1042 1164
1043 # evaluate user expression 1165 # evaluate user expression
1044 1166
1045 my @img = eval { $self->{expr}->() }; 1167 my @img = eval { $self->{expr}->() };
1046 die $@ if $@; 1168 die $@ if $@;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines