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 |
9 | background - manage terminal background |
10 | |
10 | |
… | |
… | |
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 | |
57 | Or specified as a X resource: |
57 | Or 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 | |
63 | At startup, just before the window is mapped for the first time, the |
63 | At startup, just before the window is mapped for the first time, the |
64 | expression is evaluated and must yield an image. The image is then |
64 | expression is evaluated and must yield an image. The image is then |
… | |
… | |
132 | get a percentage): |
132 | get a percentage): |
133 | |
133 | |
134 | scale 2, load "$HOME/mypic.png" |
134 | scale 2, load "$HOME/mypic.png" |
135 | |
135 | |
136 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
136 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
137 | has now two arguments, the C<200> and the C<load> expression, while |
137 | has now two arguments, the C<2> and the C<load> expression, while |
138 | C<load> only has one argument. Arguments are separated from each other by |
138 | C<load> only has one argument. Arguments are separated from each other by |
139 | commas. |
139 | commas. |
140 | |
140 | |
141 | Scale also accepts two arguments, which are then separate factors for both |
141 | Scale also accepts two arguments, which are then separate factors for both |
142 | horizontal and vertical dimensions. For example, this halves the image |
142 | horizontal and vertical dimensions. For example, this halves the image |
… | |
… | |
279 | =cut |
279 | =cut |
280 | |
280 | |
281 | our %_IMG_CACHE; |
281 | our %_IMG_CACHE; |
282 | our $HOME; |
282 | our $HOME; |
283 | our ($self, $frame); |
283 | our ($self, $frame); |
284 | our ($x, $y, $w, $h); |
284 | our ($x, $y, $w, $h, $focus); |
285 | |
285 | |
286 | # enforce at least this interval between updates |
286 | # enforce at least this interval between updates |
287 | our $MIN_INTERVAL = 6/59.951; |
287 | our $MIN_INTERVAL = 6/59.951; |
288 | |
288 | |
289 | { |
289 | { |
… | |
… | |
524 | |
524 | |
525 | Return the X and Y coordinates of the terminal window (the terminal |
525 | Return the X and Y coordinates of the terminal window (the terminal |
526 | window is the full window by default, and the character area only when in |
526 | window is the full window by default, and the character area only when in |
527 | border-respect mode). |
527 | border-respect mode). |
528 | |
528 | |
529 | Using these functions make your expression sensitive to window moves. |
529 | Using these functions makes your expression sensitive to window moves. |
530 | |
530 | |
531 | These functions are mainly useful to align images to the root window. |
531 | These functions are mainly useful to align images to the root window. |
532 | |
532 | |
533 | Example: load an image and align it so it looks as if anchored to the |
533 | Example: load an image and align it so it looks as if anchored to the |
534 | background (that's exactly what C<rootalign> does btw.): |
534 | background (that's exactly what C<rootalign> does btw.): |
… | |
… | |
541 | |
541 | |
542 | Return the width (C<TW>) and height (C<TH>) of the terminal window (the |
542 | Return the width (C<TW>) and height (C<TH>) of the terminal window (the |
543 | terminal window is the full window by default, and the character area only |
543 | terminal window is the full window by default, and the character area only |
544 | when in border-respect mode). |
544 | when in border-respect mode). |
545 | |
545 | |
546 | Using these functions make your expression sensitive to window resizes. |
546 | Using these functions makes your expression sensitive to window resizes. |
547 | |
547 | |
548 | These functions are mainly useful to scale images, or to clip images to |
548 | These functions are mainly useful to scale images, or to clip images to |
549 | the window size to conserve memory. |
549 | the window size to conserve memory. |
550 | |
550 | |
551 | Example: take the screen background, clip it to the window size, blur it a |
551 | Example: take the screen background, clip it to the window size, blur it a |
552 | bit, align it to the window position and use it as background. |
552 | bit, align it to the window position and use it as background. |
553 | |
553 | |
554 | clip move -TX, -TY, keep { blur 5, root } |
554 | clip move -TX, -TY, keep { blur 5, root } |
555 | |
555 | |
556 | =cut |
556 | =item FOCUS |
557 | |
557 | |
|
|
558 | Returns a boolean indicating whether the terminal window has keyboard |
|
|
559 | focus, in which case it returns true. |
|
|
560 | |
|
|
561 | Using this function makes your expression sensitive to focus changes. |
|
|
562 | |
|
|
563 | A common use case is to fade the background image when the terminal loses |
|
|
564 | focus, often together with the C<-fade> command line option. In fact, |
|
|
565 | there is a special function for just that use case: C<focus_fade>. |
|
|
566 | |
|
|
567 | Example: use two entirely different background images, depending on |
|
|
568 | whether the window has focus. |
|
|
569 | |
|
|
570 | FOCUS ? keep { load "has_focus.jpg" } : keep { load "no_focus.jpg" } |
|
|
571 | |
|
|
572 | =cut |
|
|
573 | |
558 | sub TX() { $frame->[FR_AGAIN]{position} = 1; $x } |
574 | sub TX () { $frame->[FR_AGAIN]{position} = 1; $x } |
559 | sub TY() { $frame->[FR_AGAIN]{position} = 1; $y } |
575 | sub TY () { $frame->[FR_AGAIN]{position} = 1; $y } |
560 | sub TW() { $frame->[FR_AGAIN]{size} = 1; $w } |
576 | sub TW () { $frame->[FR_AGAIN]{size} = 1; $w } |
561 | 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 } |
562 | |
579 | |
563 | =item now |
580 | =item now |
564 | |
581 | |
565 | Returns the current time as (fractional) seconds since the epoch. |
582 | Returns the current time as (fractional) seconds since the epoch. |
566 | |
583 | |
… | |
… | |
930 | =cut |
947 | =cut |
931 | |
948 | |
932 | sub blur($$;$) { |
949 | sub blur($$;$) { |
933 | my $img = pop; |
950 | my $img = pop; |
934 | $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 | |
|
|
960 | Fades the image by the given factor (and colour) when focus is lost (the |
|
|
961 | same as the C<-fade>/C<-fadecolor> command line options, which also supply |
|
|
962 | the default values for C<factor> and C<$color>. Unlike with C<-fade>, the |
|
|
963 | C<$factor> is a real value, not a percentage value (that is, 0..1, not |
|
|
964 | 0..100). |
|
|
965 | |
|
|
966 | Example: 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 |
935 | } |
985 | } |
936 | |
986 | |
937 | =back |
987 | =back |
938 | |
988 | |
939 | =head2 OTHER STUFF |
989 | =head2 OTHER STUFF |
… | |
… | |
1078 | if ($again->{rootpmap}) { |
1128 | if ($again->{rootpmap}) { |
1079 | $state->{rootpmap} = $self->on (rootpmap_change => $cb); |
1129 | $state->{rootpmap} = $self->on (rootpmap_change => $cb); |
1080 | } else { |
1130 | } else { |
1081 | delete $state->{rootpmap}; |
1131 | delete $state->{rootpmap}; |
1082 | } |
1132 | } |
|
|
1133 | |
|
|
1134 | if ($again->{focus}) { |
|
|
1135 | $state->{focus} = $self->on (focus_in => $cb, focus_out => $cb); |
|
|
1136 | } else { |
|
|
1137 | delete $state->{focus}; |
|
|
1138 | } |
1083 | } |
1139 | } |
1084 | |
1140 | |
1085 | # evaluate the current bg expression |
1141 | # evaluate the current bg expression |
1086 | sub recalculate { |
1142 | sub recalculate { |
1087 | my ($arg_self) = @_; |
1143 | my ($arg_self) = @_; |
… | |
… | |
1102 | local $self = $arg_self; |
1158 | local $self = $arg_self; |
1103 | local $HOME = $ENV{HOME}; |
1159 | local $HOME = $ENV{HOME}; |
1104 | local $frame = $self->{root}; |
1160 | local $frame = $self->{root}; |
1105 | |
1161 | |
1106 | ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); |
1162 | ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); |
|
|
1163 | $focus = $self->focus; |
1107 | |
1164 | |
1108 | # evaluate user expression |
1165 | # evaluate user expression |
1109 | |
1166 | |
1110 | my @img = eval { $self->{expr}->() }; |
1167 | my @img = eval { $self->{expr}->() }; |
1111 | die $@ if $@; |
1168 | die $@ if $@; |
… | |
… | |
1129 | # } |
1186 | # } |
1130 | |
1187 | |
1131 | # set background pixmap |
1188 | # set background pixmap |
1132 | |
1189 | |
1133 | $self->set_background ($img, $self->{border}); |
1190 | $self->set_background ($img, $self->{border}); |
1134 | $self->scr_recolour (0); |
1191 | $self->scr_recolor (0); |
1135 | $self->want_refresh; |
1192 | $self->want_refresh; |
1136 | } |
1193 | } |
1137 | |
1194 | |
1138 | sub on_start { |
1195 | sub on_start { |
1139 | my ($self) = @_; |
1196 | my ($self) = @_; |