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.76 by root, Tue Aug 14 23:57:07 2012 UTC vs.
Revision 1.91 by sf-exg, Thu Jul 7 17:38:34 2016 UTC

1#! perl 1#! perl
2 2
3#:META:X_RESOURCE:%.expr:string:background expression 3#:META:RESOURCE:%.expr:string:background expression
4#:META:X_RESOURCE:%.border:boolean:respect the terminal border 4#:META:RESOURCE:%.border:boolean:respect the terminal border
5#:META:X_RESOURCE:%.interval:seconds:minimum time between updates 5#:META: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
54 54
55 urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }' 55 urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }'
56 56
57Or specified as a X resource: 57Or specified as a X resource:
58 58
59 URxvt.background-expr: scale keep { load "/path/to/mybg.png" } 59 URxvt.background.expr: scale keep { load "/path/to/mybg.png" }
60 60
61=head1 THEORY OF OPERATION 61=head1 THEORY OF OPERATION
62 62
63At startup, just before the window is mapped for the first time, the 63At startup, just before the window is mapped for the first time, the
64expression is evaluated and must yield an image. The image is then 64expression is evaluated and must yield an image. The image is then
132get a percentage): 132get a percentage):
133 133
134 scale 2, load "$HOME/mypic.png" 134 scale 2, load "$HOME/mypic.png"
135 135
136This enlarges the image by a factor of 2 (200%). As you can see, C<scale> 136This enlarges the image by a factor of 2 (200%). As you can see, C<scale>
137has now two arguments, the C<200> and the C<load> expression, while 137has now two arguments, the C<2> and the C<load> expression, while
138C<load> only has one argument. Arguments are separated from each other by 138C<load> only has one argument. Arguments are separated from each other by
139commas. 139commas.
140 140
141Scale also accepts two arguments, which are then separate factors for both 141Scale also accepts two arguments, which are then separate factors for both
142horizontal and vertical dimensions. For example, this halves the image 142horizontal and vertical dimensions. For example, this halves the image
279=cut 279=cut
280 280
281our %_IMG_CACHE; 281our %_IMG_CACHE;
282our $HOME; 282our $HOME;
283our ($self, $frame); 283our ($self, $frame);
284our ($x, $y, $w, $h); 284our ($x, $y, $w, $h, $focus);
285 285
286# enforce at least this interval between updates 286# enforce at least this interval between updates
287our $MIN_INTERVAL = 6/59.951; 287our $MIN_INTERVAL = 6/59.951;
288 288
289{ 289{
308 308
309Loads 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
310mode. 310mode.
311 311
312If 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
313uses it), then the in-memory copy us returned instead. 313uses it), then the in-memory copy is returned instead.
314 314
315=item load_uc $path 315=item load_uc $path
316 316
317Load 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
318is I<always> loaded from the filesystem again, even if another copy of it 318is I<always> loaded from the filesystem again, even if another copy of it
524 524
525Return the X and Y coordinates of the terminal window (the terminal 525Return the X and Y coordinates of the terminal window (the terminal
526window 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
527border-respect mode). 527border-respect mode).
528 528
529Using these functions make your expression sensitive to window moves. 529Using these functions makes your expression sensitive to window moves.
530 530
531These functions are mainly useful to align images to the root window. 531These functions are mainly useful to align images to the root window.
532 532
533Example: 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
534background (that's exactly what C<rootalign> does btw.): 534background (that's exactly what C<rootalign> does btw.):
535 535
536 move -TX, -TY, keep { load "mybg.png" } 536 move -TX, -TY, keep { load "mybg.png" }
537 537
538=item TW 538=item TW
539
540=item TH
539 541
540Return 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
541terminal 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
542when in border-respect mode). 544when in border-respect mode).
543 545
544Using these functions make your expression sensitive to window resizes. 546Using these functions makes your expression sensitive to window resizes.
545 547
546These 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
547the window size to conserve memory. 549the window size to conserve memory.
548 550
549Example: 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
550bit, align it to the window position and use it as background. 552bit, align it to the window position and use it as background.
551 553
552 clip move -TX, -TY, keep { blur 5, root } 554 clip move -TX, -TY, keep { blur 5, root }
553 555
554=cut 556=item FOCUS
555 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 background 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
556 sub TX() { $frame->[FR_AGAIN]{position} = 1; $x } 574 sub TX () { $frame->[FR_AGAIN]{position} = 1; $x }
557 sub TY() { $frame->[FR_AGAIN]{position} = 1; $y } 575 sub TY () { $frame->[FR_AGAIN]{position} = 1; $y }
558 sub TW() { $frame->[FR_AGAIN]{size} = 1; $w } 576 sub TW () { $frame->[FR_AGAIN]{size} = 1; $w }
559 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 }
560 579
561=item now 580=item now
562 581
563Returns the current time as (fractional) seconds since the epoch. 582Returns the current time as (fractional) seconds since the epoch.
564 583
611Clips 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
612image 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
613larger 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
614will be filled. 633will be filled.
615 634
616If 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.
617 636
618If 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
619assumed. 638assumed.
620 639
621Example: 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
639=item scale $width_factor, $height_factor, $img 658=item scale $width_factor, $height_factor, $img
640 659
641Scales the image by the given factors in horizontal 660Scales the image by the given factors in horizontal
642(C<$width>) and vertical (C<$height>) direction. 661(C<$width>) and vertical (C<$height>) direction.
643 662
644If only one factor is give, it is used for both directions. 663If only one factor is given, it is used for both directions.
645 664
646If 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
647keeping aspect. 666keeping aspect.
648 667
649=item resize $width, $height, $img 668=item resize $width, $height, $img
774=item rotate $center_x, $center_y, $degrees, $img 793=item rotate $center_x, $center_y, $degrees, $img
775 794
776Rotates the image clockwise by C<$degrees> degrees, around the point at 795Rotates the image clockwise by C<$degrees> degrees, around the point at
777C<$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).
778 797
779Example: rotate the image by 90 degrees around it's center. 798Example: rotate the image by 90 degrees around its center.
780 799
781 rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" } 800 rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" }
782 801
783=cut 802=cut
784 803
815 834
816 sub tint($$) { 835 sub tint($$) {
817 $_[1]->tint ($_[0]) 836 $_[1]->tint ($_[0])
818 } 837 }
819 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
820=item contrast $factor, $img 849=item contrast $factor, $img
821 850
822=item contrast $r, $g, $b, $img 851=item contrast $r, $g, $b, $img
823 852
824=item contrast $r, $g, $b, $a, $img 853=item contrast $r, $g, $b, $a, $img
883 $img 912 $img
884 } 913 }
885 914
886=item muladd $mul, $add, $img # EXPERIMENTAL 915=item muladd $mul, $add, $img # EXPERIMENTAL
887 916
888First multipliesthe pixels by C<$mul>, then adds C<$add>. This cna be used 917First multiplies the pixels by C<$mul>, then adds C<$add>. This can be used
889to implement brightness and contrast at the same time, with a wider value 918to implement brightness and contrast at the same time, with a wider value
890range than contrast and brightness operators. 919range than contrast and brightness operators.
891 920
892Due to numerous bugs in XRender implementations, it can also introduce a 921Due to numerous bugs in XRender implementations, it can also introduce a
893number of visual artifacts. 922number of visual artifacts.
918=cut 947=cut
919 948
920 sub blur($$;$) { 949 sub blur($$;$) {
921 my $img = pop; 950 my $img = pop;
922 $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 a 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
923 } 985 }
924 986
925=back 987=back
926 988
927=head2 OTHER STUFF 989=head2 OTHER STUFF
1066 if ($again->{rootpmap}) { 1128 if ($again->{rootpmap}) {
1067 $state->{rootpmap} = $self->on (rootpmap_change => $cb); 1129 $state->{rootpmap} = $self->on (rootpmap_change => $cb);
1068 } else { 1130 } else {
1069 delete $state->{rootpmap}; 1131 delete $state->{rootpmap};
1070 } 1132 }
1133
1134 if ($again->{focus}) {
1135 $state->{focus} = $self->on (focus_in => $cb, focus_out => $cb);
1136 } else {
1137 delete $state->{focus};
1138 }
1071} 1139}
1072 1140
1073# evaluate the current bg expression 1141# evaluate the current bg expression
1074sub recalculate { 1142sub recalculate {
1075 my ($arg_self) = @_; 1143 my ($arg_self) = @_;
1090 local $self = $arg_self; 1158 local $self = $arg_self;
1091 local $HOME = $ENV{HOME}; 1159 local $HOME = $ENV{HOME};
1092 local $frame = $self->{root}; 1160 local $frame = $self->{root};
1093 1161
1094 ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); 1162 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
1163 $focus = $self->focus;
1095 1164
1096 # evaluate user expression 1165 # evaluate user expression
1097 1166
1098 my @img = eval { $self->{expr}->() }; 1167 my @img = eval { $self->{expr}->() };
1099 die $@ if $@; 1168 die $@ if $@;
1117# } 1186# }
1118 1187
1119 # set background pixmap 1188 # set background pixmap
1120 1189
1121 $self->set_background ($img, $self->{border}); 1190 $self->set_background ($img, $self->{border});
1122 $self->scr_recolour (0); 1191 $self->scr_recolor (0);
1123 $self->want_refresh; 1192 $self->want_refresh;
1124} 1193}
1125 1194
1126sub on_start { 1195sub on_start {
1127 my ($self) = @_; 1196 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines