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 | #:META:RESOURCE:pixmap:file[;geom]:set image as background |
|
|
7 | #:META:RESOURCE:backgroundPixmap:file[;geom]:set image as background |
|
|
8 | #:META:RESOURCE:tr:boolean:set root pixmap as background |
|
|
9 | #:META:RESOURCE:transparent:boolean:set root pixmap as background |
|
|
10 | #:META:RESOURCE:tint:color:tint background with color |
|
|
11 | #:META:RESOURCE:tintColor:color:tint background with color |
|
|
12 | #:META:RESOURCE:sh:number:shade background by number % |
|
|
13 | #:META:RESOURCE:shading:number:shade background by number % |
|
|
14 | #:META:RESOURCE:blr:HxV:gaussian-blur background with radii |
|
|
15 | #:META:RESOURCE:blurRadius:HxV:gaussian-blur background with radii |
6 | |
16 | |
7 | =head1 NAME |
17 | =head1 NAME |
8 | |
18 | |
9 | background - manage terminal background |
19 | background - manage terminal background |
10 | |
20 | |
… | |
… | |
14 | --background-border |
24 | --background-border |
15 | --background-interval seconds |
25 | --background-interval seconds |
16 | |
26 | |
17 | =head1 QUICK AND DIRTY CHEAT SHEET |
27 | =head1 QUICK AND DIRTY CHEAT SHEET |
18 | |
28 | |
19 | Just load a random jpeg image and tile the background with it without |
29 | Load a random jpeg image and tile the background with it without scaling |
20 | scaling or anything else: |
30 | or anything else: |
21 | |
31 | |
22 | load "/path/to/img.jpg" |
32 | load "/path/to/img.jpg" |
23 | |
33 | |
24 | The same, but use mirroring/reflection instead of tiling: |
34 | The same, but use mirroring/reflection instead of tiling: |
25 | |
35 | |
… | |
… | |
54 | |
64 | |
55 | urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }' |
65 | urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }' |
56 | |
66 | |
57 | Or specified as a X resource: |
67 | Or specified as a X resource: |
58 | |
68 | |
59 | URxvt.background-expr: scale keep { load "/path/to/mybg.png" } |
69 | URxvt.background.expr: scale keep { load "/path/to/mybg.png" } |
60 | |
70 | |
61 | =head1 THEORY OF OPERATION |
71 | =head1 THEORY OF OPERATION |
62 | |
72 | |
63 | At startup, just before the window is mapped for the first time, the |
73 | 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 |
74 | expression is evaluated and must yield an image. The image is then |
… | |
… | |
132 | get a percentage): |
142 | get a percentage): |
133 | |
143 | |
134 | scale 2, load "$HOME/mypic.png" |
144 | scale 2, load "$HOME/mypic.png" |
135 | |
145 | |
136 | This enlarges the image by a factor of 2 (200%). As you can see, C<scale> |
146 | 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 |
147 | 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 |
148 | C<load> only has one argument. Arguments are separated from each other by |
139 | commas. |
149 | commas. |
140 | |
150 | |
141 | Scale also accepts two arguments, which are then separate factors for both |
151 | Scale also accepts two arguments, which are then separate factors for both |
142 | horizontal and vertical dimensions. For example, this halves the image |
152 | horizontal and vertical dimensions. For example, this halves the image |
… | |
… | |
249 | |
259 | |
250 | =head1 REFERENCE |
260 | =head1 REFERENCE |
251 | |
261 | |
252 | =head2 COMMAND LINE SWITCHES |
262 | =head2 COMMAND LINE SWITCHES |
253 | |
263 | |
254 | =over 4 |
264 | =over |
255 | |
265 | |
256 | =item --background-expr perl-expression |
266 | =item --background-expr perl-expression |
257 | |
267 | |
258 | Specifies the Perl expression to evaluate. |
268 | Specifies the Perl expression to evaluate. |
259 | |
269 | |
… | |
… | |
279 | =cut |
289 | =cut |
280 | |
290 | |
281 | our %_IMG_CACHE; |
291 | our %_IMG_CACHE; |
282 | our $HOME; |
292 | our $HOME; |
283 | our ($self, $frame); |
293 | our ($self, $frame); |
284 | our ($x, $y, $w, $h); |
294 | our ($x, $y, $w, $h, $focus); |
285 | |
295 | |
286 | # enforce at least this interval between updates |
296 | # enforce at least this interval between updates |
287 | our $MIN_INTERVAL = 6/59.951; |
297 | our $MIN_INTERVAL = 6/59.951; |
288 | |
298 | |
289 | { |
299 | { |
… | |
… | |
300 | |
310 | |
301 | These functions provide an image, by loading it from disk, grabbing it |
311 | These functions provide an image, by loading it from disk, grabbing it |
302 | from the root screen or by simply generating it. They are used as starting |
312 | from the root screen or by simply generating it. They are used as starting |
303 | points to get an image you can play with. |
313 | points to get an image you can play with. |
304 | |
314 | |
305 | =over 4 |
315 | =over |
306 | |
316 | |
307 | =item load $path |
317 | =item load $path |
308 | |
318 | |
309 | Loads the image at the given C<$path>. The image is set to plane tiling |
319 | Loads the image at the given C<$path>. The image is set to plane tiling |
310 | mode. |
320 | mode. |
311 | |
321 | |
312 | If the image is already in memory (e.g. because another terminal instance |
322 | If the image is already in memory (e.g. because another terminal instance |
313 | uses it), then the in-memory copy us returned instead. |
323 | uses it), then the in-memory copy is returned instead. |
314 | |
324 | |
315 | =item load_uc $path |
325 | =item load_uc $path |
316 | |
326 | |
317 | Load uncached - same as load, but does not cache the image, which means it |
327 | Load uncached - same as load, but does not cache the image, which means it |
318 | is I<always> loaded from the filesystem again, even if another copy of it |
328 | is I<always> loaded from the filesystem again, even if another copy of it |
… | |
… | |
429 | =head2 TILING MODES |
439 | =head2 TILING MODES |
430 | |
440 | |
431 | The following operators modify the tiling mode of an image, that is, the |
441 | The following operators modify the tiling mode of an image, that is, the |
432 | way that pixels outside the image area are painted when the image is used. |
442 | way that pixels outside the image area are painted when the image is used. |
433 | |
443 | |
434 | =over 4 |
444 | =over |
435 | |
445 | |
436 | =item tile $img |
446 | =item tile $img |
437 | |
447 | |
438 | Tiles the whole plane with the image and returns this new image - or in |
448 | Tiles the whole plane with the image and returns this new image - or in |
439 | other words, it returns a copy of the image in plane tiling mode. |
449 | other words, it returns a copy of the image in plane tiling mode. |
… | |
… | |
514 | dimensions. They are not (Perl-) variables, they just return stuff that |
524 | dimensions. They are not (Perl-) variables, they just return stuff that |
515 | varies. Most of them make your expression sensitive to some events, for |
525 | varies. Most of them make your expression sensitive to some events, for |
516 | example using C<TW> (terminal width) means your expression is evaluated |
526 | example using C<TW> (terminal width) means your expression is evaluated |
517 | again when the terminal is resized. |
527 | again when the terminal is resized. |
518 | |
528 | |
519 | =over 4 |
529 | =over |
520 | |
530 | |
521 | =item TX |
531 | =item TX |
522 | |
532 | |
523 | =item TY |
533 | =item TY |
524 | |
534 | |
525 | Return the X and Y coordinates of the terminal window (the terminal |
535 | 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 |
536 | window is the full window by default, and the character area only when in |
527 | border-respect mode). |
537 | border-respect mode). |
528 | |
538 | |
529 | Using these functions make your expression sensitive to window moves. |
539 | Using these functions makes your expression sensitive to window moves. |
530 | |
540 | |
531 | These functions are mainly useful to align images to the root window. |
541 | These functions are mainly useful to align images to the root window. |
532 | |
542 | |
533 | Example: load an image and align it so it looks as if anchored to the |
543 | 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.): |
544 | background (that's exactly what C<rootalign> does btw.): |
… | |
… | |
541 | |
551 | |
542 | Return the width (C<TW>) and height (C<TH>) of the terminal window (the |
552 | 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 |
553 | terminal window is the full window by default, and the character area only |
544 | when in border-respect mode). |
554 | when in border-respect mode). |
545 | |
555 | |
546 | Using these functions make your expression sensitive to window resizes. |
556 | Using these functions makes your expression sensitive to window resizes. |
547 | |
557 | |
548 | These functions are mainly useful to scale images, or to clip images to |
558 | These functions are mainly useful to scale images, or to clip images to |
549 | the window size to conserve memory. |
559 | the window size to conserve memory. |
550 | |
560 | |
551 | Example: take the screen background, clip it to the window size, blur it a |
561 | 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. |
562 | bit, align it to the window position and use it as background. |
553 | |
563 | |
554 | clip move -TX, -TY, keep { blur 5, root } |
564 | clip move -TX, -TY, keep { blur 5, root } |
555 | |
565 | |
556 | =cut |
566 | =item FOCUS |
557 | |
567 | |
|
|
568 | Returns a boolean indicating whether the terminal window has keyboard |
|
|
569 | focus, in which case it returns true. |
|
|
570 | |
|
|
571 | Using this function makes your expression sensitive to focus changes. |
|
|
572 | |
|
|
573 | A common use case is to fade the background image when the terminal loses |
|
|
574 | focus, often together with the C<-fade> command line option. In fact, |
|
|
575 | there is a special function for just that use case: C<focus_fade>. |
|
|
576 | |
|
|
577 | Example: use two entirely different background images, depending on |
|
|
578 | whether the window has focus. |
|
|
579 | |
|
|
580 | FOCUS ? keep { load "has_focus.jpg" } : keep { load "no_focus.jpg" } |
|
|
581 | |
|
|
582 | =cut |
|
|
583 | |
558 | sub TX() { $frame->[FR_AGAIN]{position} = 1; $x } |
584 | sub TX () { $frame->[FR_AGAIN]{position} = 1; $x } |
559 | sub TY() { $frame->[FR_AGAIN]{position} = 1; $y } |
585 | sub TY () { $frame->[FR_AGAIN]{position} = 1; $y } |
560 | sub TW() { $frame->[FR_AGAIN]{size} = 1; $w } |
586 | sub TW () { $frame->[FR_AGAIN]{size} = 1; $w } |
561 | sub TH() { $frame->[FR_AGAIN]{size} = 1; $h } |
587 | sub TH () { $frame->[FR_AGAIN]{size} = 1; $h } |
|
|
588 | sub FOCUS() { $frame->[FR_AGAIN]{focus} = 1; $focus } |
562 | |
589 | |
563 | =item now |
590 | =item now |
564 | |
591 | |
565 | Returns the current time as (fractional) seconds since the epoch. |
592 | Returns the current time as (fractional) seconds since the epoch. |
566 | |
593 | |
… | |
… | |
600 | |
627 | |
601 | =head2 SHAPE CHANGING OPERATORS |
628 | =head2 SHAPE CHANGING OPERATORS |
602 | |
629 | |
603 | The following operators modify the shape, size or position of the image. |
630 | The following operators modify the shape, size or position of the image. |
604 | |
631 | |
605 | =over 4 |
632 | =over |
606 | |
633 | |
607 | =item clip $img |
634 | =item clip $img |
608 | |
635 | |
609 | =item clip $width, $height, $img |
636 | =item clip $width, $height, $img |
610 | |
637 | |
… | |
… | |
641 | =item scale $width_factor, $height_factor, $img |
668 | =item scale $width_factor, $height_factor, $img |
642 | |
669 | |
643 | Scales the image by the given factors in horizontal |
670 | Scales the image by the given factors in horizontal |
644 | (C<$width>) and vertical (C<$height>) direction. |
671 | (C<$width>) and vertical (C<$height>) direction. |
645 | |
672 | |
646 | If only one factor is give, it is used for both directions. |
673 | If only one factor is given, it is used for both directions. |
647 | |
674 | |
648 | If no factors are given, scales the image to the window size without |
675 | If no factors are given, scales the image to the window size without |
649 | keeping aspect. |
676 | keeping aspect. |
650 | |
677 | |
651 | =item resize $width, $height, $img |
678 | =item resize $width, $height, $img |
… | |
… | |
776 | =item rotate $center_x, $center_y, $degrees, $img |
803 | =item rotate $center_x, $center_y, $degrees, $img |
777 | |
804 | |
778 | Rotates the image clockwise by C<$degrees> degrees, around the point at |
805 | Rotates the image clockwise by C<$degrees> degrees, around the point at |
779 | C<$center_x> and C<$center_y> (specified as factor of image width/height). |
806 | C<$center_x> and C<$center_y> (specified as factor of image width/height). |
780 | |
807 | |
781 | Example: rotate the image by 90 degrees around it's center. |
808 | Example: rotate the image by 90 degrees around its center. |
782 | |
809 | |
783 | rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" } |
810 | rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" } |
784 | |
811 | |
785 | =cut |
812 | =cut |
786 | |
813 | |
… | |
… | |
797 | |
824 | |
798 | =head2 COLOUR MODIFICATIONS |
825 | =head2 COLOUR MODIFICATIONS |
799 | |
826 | |
800 | The following operators change the pixels of the image. |
827 | The following operators change the pixels of the image. |
801 | |
828 | |
802 | =over 4 |
829 | =over |
803 | |
830 | |
804 | =item tint $color, $img |
831 | =item tint $color, $img |
805 | |
832 | |
806 | Tints the image in the given colour. |
833 | Tints the image in the given colour. |
807 | |
834 | |
… | |
… | |
815 | |
842 | |
816 | =cut |
843 | =cut |
817 | |
844 | |
818 | sub tint($$) { |
845 | sub tint($$) { |
819 | $_[1]->tint ($_[0]) |
846 | $_[1]->tint ($_[0]) |
|
|
847 | } |
|
|
848 | |
|
|
849 | =item shade $factor, $img |
|
|
850 | |
|
|
851 | Shade the image by the given factor. |
|
|
852 | |
|
|
853 | =cut |
|
|
854 | |
|
|
855 | sub shade($$) { |
|
|
856 | $_[1]->shade ($_[0]) |
820 | } |
857 | } |
821 | |
858 | |
822 | =item contrast $factor, $img |
859 | =item contrast $factor, $img |
823 | |
860 | |
824 | =item contrast $r, $g, $b, $img |
861 | =item contrast $r, $g, $b, $img |
… | |
… | |
922 | sub blur($$;$) { |
959 | sub blur($$;$) { |
923 | my $img = pop; |
960 | my $img = pop; |
924 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
961 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
925 | } |
962 | } |
926 | |
963 | |
|
|
964 | =item focus_fade $img |
|
|
965 | |
|
|
966 | =item focus_fade $factor, $img |
|
|
967 | |
|
|
968 | =item focus_fade $factor, $color, $img |
|
|
969 | |
|
|
970 | Fades the image by the given factor (and colour) when focus is lost (the |
|
|
971 | same as the C<-fade>/C<-fadecolor> command line options, which also supply |
|
|
972 | the default values for C<factor> and C<$color>. Unlike with C<-fade>, the |
|
|
973 | C<$factor> is a real value, not a percentage value (that is, 0..1, not |
|
|
974 | 0..100). |
|
|
975 | |
|
|
976 | Example: do the right thing when focus fading is requested. |
|
|
977 | |
|
|
978 | focus_fade load "mybg.jpg"; |
|
|
979 | |
|
|
980 | =cut |
|
|
981 | |
|
|
982 | sub focus_fade($;$$) { |
|
|
983 | my $img = pop; |
|
|
984 | |
|
|
985 | return $img |
|
|
986 | if FOCUS; |
|
|
987 | |
|
|
988 | my $fade = @_ >= 1 ? $_[0] : defined $self->resource ("fade") ? $self->resource ("fade") * 0.01 : 0; |
|
|
989 | my $color = @_ >= 2 ? $_[1] : $self->resource ("color+" . urxvt::Color_fade); |
|
|
990 | |
|
|
991 | $img = $img->tint ($color) if $color ne "rgb:00/00/00"; |
|
|
992 | $img = $img->muladd (1 - $fade, 0) if $fade; |
|
|
993 | |
|
|
994 | $img |
|
|
995 | } |
|
|
996 | |
927 | =back |
997 | =back |
928 | |
998 | |
929 | =head2 OTHER STUFF |
999 | =head2 OTHER STUFF |
930 | |
1000 | |
931 | Anything that didn't fit any of the other categories, even after applying |
1001 | Anything that didn't fit any of the other categories, even after applying |
932 | force and closing our eyes. |
1002 | force and closing our eyes. |
933 | |
1003 | |
934 | =over 4 |
1004 | =over |
935 | |
1005 | |
936 | =item keep { ... } |
1006 | =item keep { ... } |
937 | |
1007 | |
938 | This operator takes a code block as argument, that is, one or more |
1008 | This operator takes a code block as argument, that is, one or more |
939 | statements enclosed by braces. |
1009 | statements enclosed by braces. |
… | |
… | |
961 | |
1031 | |
962 | This leaves the question of how to force reevaluation of the block, |
1032 | This leaves the question of how to force reevaluation of the block, |
963 | in case the root background changes: If expression inside the block |
1033 | in case the root background changes: If expression inside the block |
964 | is sensitive to some event (root background changes, window geometry |
1034 | is sensitive to some event (root background changes, window geometry |
965 | changes), then it will be reevaluated automatically as needed. |
1035 | changes), then it will be reevaluated automatically as needed. |
|
|
1036 | |
|
|
1037 | =back |
|
|
1038 | |
|
|
1039 | =head1 OLD BACKGROUND IMAGE SETTINGS |
|
|
1040 | |
|
|
1041 | This extension also provides support for the old options/resources and |
|
|
1042 | OSC sequences for setting a background image. These settings are |
|
|
1043 | B<deprecated> and will be removed in future versions. |
|
|
1044 | |
|
|
1045 | =head2 OPTIONS AND RESOURCES |
|
|
1046 | |
|
|
1047 | =over |
|
|
1048 | |
|
|
1049 | =item B<-pixmap> I<file[;oplist]> |
|
|
1050 | |
|
|
1051 | =item B<backgroundPixmap:> I<file[;oplist]> |
|
|
1052 | |
|
|
1053 | Use the specified image file as the window's background and also |
|
|
1054 | optionally specify a colon separated list of operations to modify it. |
|
|
1055 | Note that you may need to quote the C<;> character when using the |
|
|
1056 | command line option, as C<;> is usually a metacharacter in shells. |
|
|
1057 | Supported operations are: |
|
|
1058 | |
|
|
1059 | =over |
|
|
1060 | |
|
|
1061 | =item B<WxH+X+Y> |
|
|
1062 | |
|
|
1063 | sets scale and position. B<"W" / "H"> specify the horizontal/vertical |
|
|
1064 | scale (percent), and B<"X" / "Y"> locate the image centre (percent). A |
|
|
1065 | scale of 0 disables scaling. |
|
|
1066 | |
|
|
1067 | =item B<op=tile> |
|
|
1068 | |
|
|
1069 | enables tiling |
|
|
1070 | |
|
|
1071 | =item B<op=keep-aspect> |
|
|
1072 | |
|
|
1073 | maintain the image aspect ratio when scaling |
|
|
1074 | |
|
|
1075 | =item B<op=root-align> |
|
|
1076 | |
|
|
1077 | use the position of the terminal window relative to the root window as |
|
|
1078 | the image offset, simulating a root window background |
|
|
1079 | |
|
|
1080 | =back |
|
|
1081 | |
|
|
1082 | The default scale and position setting is C<100x100+50+50>. |
|
|
1083 | Alternatively, a predefined set of templates can be used to achieve |
|
|
1084 | the most common setups: |
|
|
1085 | |
|
|
1086 | =over |
|
|
1087 | |
|
|
1088 | =item B<style=tiled> |
|
|
1089 | |
|
|
1090 | the image is tiled with no scaling. Equivalent to 0x0+0+0:op=tile |
|
|
1091 | |
|
|
1092 | =item B<style=aspect-stretched> |
|
|
1093 | |
|
|
1094 | the image is scaled to fill the whole window maintaining the aspect |
|
|
1095 | ratio and centered. Equivalent to 100x100+50+50:op=keep-aspect |
|
|
1096 | |
|
|
1097 | =item B<style=stretched> |
|
|
1098 | |
|
|
1099 | the image is scaled to fill the whole window. Equivalent to 100x100 |
|
|
1100 | |
|
|
1101 | =item B<style=centered> |
|
|
1102 | |
|
|
1103 | the image is centered with no scaling. Equivalent to 0x0+50+50 |
|
|
1104 | |
|
|
1105 | =item B<style=root-tiled> |
|
|
1106 | |
|
|
1107 | the image is tiled with no scaling and using 'root' positioning. |
|
|
1108 | Equivalent to 0x0:op=tile:op=root-align |
|
|
1109 | |
|
|
1110 | =back |
|
|
1111 | |
|
|
1112 | If multiple templates are specified the last one wins. Note that a |
|
|
1113 | template overrides all the scale, position and operations settings. |
|
|
1114 | |
|
|
1115 | If used in conjunction with pseudo-transparency, the specified image |
|
|
1116 | will be blended over the transparent background using alpha-blending. |
|
|
1117 | |
|
|
1118 | =item B<-tr>|B<+tr> |
|
|
1119 | |
|
|
1120 | =item B<transparent:> I<boolean> |
|
|
1121 | |
|
|
1122 | Turn on/off pseudo-transparency by using the root pixmap as background. |
|
|
1123 | |
|
|
1124 | =item B<-tint> I<colour> |
|
|
1125 | |
|
|
1126 | =item B<tintColor:> I<colour> |
|
|
1127 | |
|
|
1128 | Tint the transparent background with the given colour. Note that a |
|
|
1129 | black tint yields a completely black image while a white tint yields |
|
|
1130 | the image unchanged. |
|
|
1131 | |
|
|
1132 | =item B<-sh> I<number> |
|
|
1133 | |
|
|
1134 | =item B<shading:> I<number> |
|
|
1135 | |
|
|
1136 | Darken (0 .. 99) or lighten (101 .. 200) the transparent background. |
|
|
1137 | A value of 100 means no shading. |
|
|
1138 | |
|
|
1139 | =item B<-blr> I<HxV> |
|
|
1140 | |
|
|
1141 | =item B<blurRadius:> I<HxV> |
|
|
1142 | |
|
|
1143 | Apply gaussian blur with the specified radius to the transparent |
|
|
1144 | background. If a single number is specified, the vertical and |
|
|
1145 | horizontal radii are considered to be the same. Setting one of the |
|
|
1146 | radii to 1 and the other to a large number creates interesting effects |
|
|
1147 | on some backgrounds. The maximum radius value is 128. An horizontal or |
|
|
1148 | vertical radius of 0 disables blurring. |
|
|
1149 | |
|
|
1150 | =back |
|
|
1151 | |
|
|
1152 | =head2 OSC sequences |
|
|
1153 | |
|
|
1154 | This extension will react to the following OSC sequences. Note that |
|
|
1155 | this extension will not be autoloaded when these are used currently, |
|
|
1156 | so to make urxvt recognize them, you have to enable the C<background> |
|
|
1157 | extension. One way to achieve that is to use the C<--background-expr ''> |
|
|
1158 | command line argument or by specifying an empty C<URxvt.background.expr:>> |
|
|
1159 | resource. |
|
|
1160 | |
|
|
1161 | =over |
|
|
1162 | |
|
|
1163 | =item B<< C<ESC ] 705 ; Pt ST> >> Change transparent background tint colour to B<< C<Pt> >>. |
|
|
1164 | |
|
|
1165 | =item B<< C<ESC ] 20 ; Pt ST> >> Change/Query background image |
|
|
1166 | parameters: the value of B<< C<Pt> >> can be one of the following |
|
|
1167 | commands: |
|
|
1168 | |
|
|
1169 | =over |
|
|
1170 | |
|
|
1171 | =item B<< C<?> >> |
|
|
1172 | |
|
|
1173 | display scale and position in the title |
|
|
1174 | |
|
|
1175 | =item B<< C<;WxH+X+Y> >> |
|
|
1176 | |
|
|
1177 | change scale and/or position |
|
|
1178 | |
|
|
1179 | =item B<< C<FILE;WxH+X+Y> >> |
|
|
1180 | |
|
|
1181 | change background image |
|
|
1182 | |
|
|
1183 | =back |
966 | |
1184 | |
967 | =cut |
1185 | =cut |
968 | |
1186 | |
969 | sub keep(&) { |
1187 | sub keep(&) { |
970 | my $id = $_[0]+0; |
1188 | my $id = $_[0]+0; |
… | |
… | |
1003 | =cut |
1221 | =cut |
1004 | |
1222 | |
1005 | } |
1223 | } |
1006 | |
1224 | |
1007 | sub parse_expr { |
1225 | sub parse_expr { |
|
|
1226 | my ($expr) = @_; |
|
|
1227 | |
|
|
1228 | # an empty expression is valid and represents the default background |
|
|
1229 | if ($expr !~ /\S/) { |
|
|
1230 | $expr = sub { |
|
|
1231 | undef |
|
|
1232 | }; |
|
|
1233 | } else { |
1008 | my $expr = eval |
1234 | $expr = eval |
1009 | "sub {\n" |
1235 | "sub {\n" |
1010 | . "package urxvt::bgdsl;\n" |
1236 | . "package urxvt::bgdsl;\n" |
1011 | . "#line 0 'background expression'\n" |
1237 | . "#line 0 'background expression'\n" |
1012 | . "$_[0]\n" |
1238 | . "$expr\n" |
1013 | . "}"; |
1239 | . "}"; |
1014 | die if $@; |
1240 | die if $@; |
|
|
1241 | } |
|
|
1242 | |
1015 | $expr |
1243 | $expr |
1016 | } |
1244 | } |
1017 | |
1245 | |
1018 | # compiles a parsed expression |
1246 | # compiles a parsed expression |
1019 | sub set_expr { |
1247 | sub set_expr { |
… | |
… | |
1068 | if ($again->{rootpmap}) { |
1296 | if ($again->{rootpmap}) { |
1069 | $state->{rootpmap} = $self->on (rootpmap_change => $cb); |
1297 | $state->{rootpmap} = $self->on (rootpmap_change => $cb); |
1070 | } else { |
1298 | } else { |
1071 | delete $state->{rootpmap}; |
1299 | delete $state->{rootpmap}; |
1072 | } |
1300 | } |
|
|
1301 | |
|
|
1302 | if ($again->{focus}) { |
|
|
1303 | $state->{focus} = $self->on (focus_in => $cb, focus_out => $cb); |
|
|
1304 | } else { |
|
|
1305 | delete $state->{focus}; |
|
|
1306 | } |
1073 | } |
1307 | } |
1074 | |
1308 | |
1075 | # evaluate the current bg expression |
1309 | # evaluate the current bg expression |
1076 | sub recalculate { |
1310 | sub recalculate { |
1077 | my ($arg_self) = @_; |
1311 | my ($arg_self) = @_; |
… | |
… | |
1085 | return; |
1319 | return; |
1086 | } |
1320 | } |
1087 | |
1321 | |
1088 | $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; |
1322 | $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; |
1089 | |
1323 | |
|
|
1324 | unless ($arg_self->has_render) { |
|
|
1325 | warn "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n"; |
|
|
1326 | return; |
|
|
1327 | } |
|
|
1328 | |
1090 | # set environment to evaluate user expression |
1329 | # set environment to evaluate user expression |
1091 | |
1330 | |
1092 | local $self = $arg_self; |
1331 | local $self = $arg_self; |
1093 | local $HOME = $ENV{HOME}; |
1332 | local $HOME = $ENV{HOME}; |
1094 | local $frame = $self->{root}; |
1333 | local $frame = $self->{root}; |
1095 | |
1334 | |
1096 | ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); |
1335 | ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); |
|
|
1336 | $focus = $self->focus; |
1097 | |
1337 | |
1098 | # evaluate user expression |
1338 | # evaluate user expression |
1099 | |
1339 | |
1100 | my @img = eval { $self->{expr}->() }; |
1340 | my @img = eval { $self->{expr}->() }; |
1101 | die $@ if $@; |
1341 | die $@ if $@; |
1102 | die "background-expr did not return anything.\n" unless @img; |
1342 | die "background-expr did not return anything.\n" unless @img; |
|
|
1343 | |
|
|
1344 | if ($img[0]) { |
1103 | die "background-expr: expected image(s), got something else.\n" |
1345 | die "background-expr: expected image(s), got something else.\n" |
1104 | if grep { !UNIVERSAL::isa $_, "urxvt::img" } @img; |
1346 | if grep { !UNIVERSAL::isa $_, "urxvt::img" } @img; |
1105 | |
1347 | |
1106 | my $img = urxvt::bgdsl::merge @img; |
1348 | my $img = urxvt::bgdsl::merge @img; |
1107 | |
1349 | |
1108 | $frame->[FR_AGAIN]{size} = 1 |
1350 | $frame->[FR_AGAIN]{size} = 1 |
1109 | if $img->repeat_mode != urxvt::RepeatNormal; |
1351 | if $img->repeat_mode != urxvt::RepeatNormal; |
1110 | |
1352 | |
1111 | # if the expression is sensitive to external events, prepare reevaluation then |
1353 | # if the expression is sensitive to external events, prepare reevaluation then |
1112 | $self->compile_frame ($frame, sub { $arg_self->recalculate }); |
1354 | $self->compile_frame ($frame, sub { $arg_self->recalculate }); |
1113 | |
1355 | |
1114 | # clear stuff we no longer need |
1356 | # clear stuff we no longer need |
1115 | |
1357 | |
1116 | # unless (%{ $frame->[FR_STATE] }) { |
1358 | # unless (%{ $frame->[FR_STATE] }) { |
1117 | # delete $self->{state}; |
1359 | # delete $self->{state}; |
1118 | # delete $self->{expr}; |
1360 | # delete $self->{expr}; |
1119 | # } |
1361 | # } |
1120 | |
1362 | |
1121 | # set background pixmap |
1363 | # set background pixmap |
1122 | |
1364 | |
1123 | $self->set_background ($img, $self->{border}); |
1365 | $self->set_background ($img, $self->{border}); |
|
|
1366 | } else { |
|
|
1367 | $self->clr_background; |
|
|
1368 | } |
|
|
1369 | |
1124 | $self->scr_recolour (0); |
1370 | $self->scr_recolor (0); |
1125 | $self->want_refresh; |
1371 | $self->want_refresh; |
1126 | } |
1372 | } |
1127 | |
1373 | |
|
|
1374 | sub old_bg_opts { |
|
|
1375 | my ($self, $arg) = @_; |
|
|
1376 | |
|
|
1377 | $arg or return; |
|
|
1378 | |
|
|
1379 | my @str = split /;/, $arg; |
|
|
1380 | |
|
|
1381 | return unless $str[0] or $self->{bg_opts}->{path}; |
|
|
1382 | |
|
|
1383 | my $bg_opts = $self->{bg_opts}; |
|
|
1384 | |
|
|
1385 | if ($str[0]) { |
|
|
1386 | $bg_opts->{tile} = 0; |
|
|
1387 | $bg_opts->{keep_aspect} = 0; |
|
|
1388 | $bg_opts->{root_align} = 0; |
|
|
1389 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 100; |
|
|
1390 | $bg_opts->{h_align} = $bg_opts->{v_align} = 50; |
|
|
1391 | $bg_opts->{path} = $str[0]; |
|
|
1392 | } |
|
|
1393 | |
|
|
1394 | my @oplist = split /:/, $str[1]; |
|
|
1395 | |
|
|
1396 | for (@oplist) { |
|
|
1397 | if (/style=tiled/i) { |
|
|
1398 | $bg_opts->{tile} = 1; |
|
|
1399 | $bg_opts->{keep_aspect} = 0; |
|
|
1400 | $bg_opts->{root_align} = 0; |
|
|
1401 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 0; |
|
|
1402 | $bg_opts->{h_align} = $bg_opts->{v_align} = 0; |
|
|
1403 | } elsif (/style=aspect-stretched/i) { |
|
|
1404 | $bg_opts->{tile} = 0; |
|
|
1405 | $bg_opts->{keep_aspect} = 1; |
|
|
1406 | $bg_opts->{root_align} = 0; |
|
|
1407 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 100; |
|
|
1408 | $bg_opts->{h_align} = $bg_opts->{v_align} = 50; |
|
|
1409 | } elsif (/style=stretched/i) { |
|
|
1410 | $bg_opts->{tile} = 0; |
|
|
1411 | $bg_opts->{keep_aspect} = 0; |
|
|
1412 | $bg_opts->{root_align} = 0; |
|
|
1413 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 100; |
|
|
1414 | $bg_opts->{h_align} = $bg_opts->{v_align} = 50; |
|
|
1415 | } elsif (/style=centered/i) { |
|
|
1416 | $bg_opts->{tile} = 0; |
|
|
1417 | $bg_opts->{keep_aspect} = 0; |
|
|
1418 | $bg_opts->{root_align} = 0; |
|
|
1419 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 0; |
|
|
1420 | $bg_opts->{h_align} = $bg_opts->{v_align} = 50; |
|
|
1421 | } elsif (/style=root-tiled/i) { |
|
|
1422 | $bg_opts->{tile} = 1; |
|
|
1423 | $bg_opts->{keep_aspect} = 0; |
|
|
1424 | $bg_opts->{root_align} = 1; |
|
|
1425 | $bg_opts->{h_scale} = $bg_opts->{v_scale} = 0; |
|
|
1426 | $bg_opts->{h_align} = $bg_opts->{v_align} = 0; |
|
|
1427 | } elsif (/op=tile/i) { |
|
|
1428 | $bg_opts->{tile} = 1; |
|
|
1429 | } elsif (/op=keep-aspect/i) { |
|
|
1430 | $bg_opts->{keep_aspect} = 1; |
|
|
1431 | } elsif (/op=root-align/i) { |
|
|
1432 | $bg_opts->{root_align} = 1; |
|
|
1433 | } elsif (/^ =? ([0-9]+)? (?:[xX] ([0-9]+))? ([+-][0-9]+)? ([+-][0-9]+)? $/x) { |
|
|
1434 | my ($w, $h, $x, $y) = ($1, $2, $3, $4); |
|
|
1435 | |
|
|
1436 | if ($str[0]) { |
|
|
1437 | $w = $h unless defined $w; |
|
|
1438 | $h = $w unless defined $h; |
|
|
1439 | $y = $x unless defined $y; |
|
|
1440 | } |
|
|
1441 | |
|
|
1442 | $bg_opts->{h_scale} = $w if defined $w; |
|
|
1443 | $bg_opts->{v_scale} = $h if defined $h; |
|
|
1444 | $bg_opts->{h_align} = $x if defined $x; |
|
|
1445 | $bg_opts->{v_align} = $y if defined $y; |
|
|
1446 | } |
|
|
1447 | } |
|
|
1448 | } |
|
|
1449 | |
|
|
1450 | # helper function, quote string as perl without allowing |
|
|
1451 | # any code execution or other shenanigans. does not |
|
|
1452 | # support binary NULs in string. |
|
|
1453 | sub q0 { |
|
|
1454 | (my $str = shift) =~ s/\x00//g; # make sure there really aren't any embedded NULs |
|
|
1455 | "q\x00$str\x00" |
|
|
1456 | } |
|
|
1457 | |
|
|
1458 | sub old_bg_expr { |
|
|
1459 | my ($self) = @_; |
|
|
1460 | |
|
|
1461 | my $expr; |
|
|
1462 | |
|
|
1463 | my $bg_opts = $self->{bg_opts}; |
|
|
1464 | |
|
|
1465 | if ($bg_opts->{root} =~ /^\s*(?:true|yes|on|1)\s*$/i) { |
|
|
1466 | $expr .= "tile ("; |
|
|
1467 | |
|
|
1468 | my $shade = $bg_opts->{shade}; |
|
|
1469 | |
|
|
1470 | if ($shade) { |
|
|
1471 | $shade = List::Util::min $shade, 200; |
|
|
1472 | $shade = List::Util::max $shade, -100; |
|
|
1473 | $shade = 200 - (100 + $shade) if $shade < 0; |
|
|
1474 | |
|
|
1475 | $shade = $shade * 0.01 - 1; |
|
|
1476 | $expr .= "shade $shade, "; |
|
|
1477 | } |
|
|
1478 | |
|
|
1479 | my $tint = $bg_opts->{tint}; |
|
|
1480 | |
|
|
1481 | if ($tint) { |
|
|
1482 | $tint = q0 $tint; |
|
|
1483 | $expr .= "tint $tint,"; |
|
|
1484 | } |
|
|
1485 | |
|
|
1486 | my $blur = $bg_opts->{blur}; |
|
|
1487 | |
|
|
1488 | if ($blur and $blur =~ /^ =? ([0-9]+)? (?:[xX] ([0-9]+))? $/x) { |
|
|
1489 | my $hr = defined $1 ? $1 : 1; |
|
|
1490 | my $vr = defined $2 ? $2 : $hr; |
|
|
1491 | |
|
|
1492 | if ($hr != 0 and $vr != 0) { |
|
|
1493 | $expr .= "blur $hr, $vr, "; |
|
|
1494 | } |
|
|
1495 | } |
|
|
1496 | |
|
|
1497 | $expr .= "rootalign root)"; |
|
|
1498 | } |
|
|
1499 | |
|
|
1500 | if ($bg_opts->{path}) { |
|
|
1501 | my $file_expr; |
|
|
1502 | my $h_scale = $bg_opts->{h_scale} * 0.01; |
|
|
1503 | my $v_scale = $bg_opts->{v_scale} * 0.01; |
|
|
1504 | my $h_align = $bg_opts->{h_align} * 0.01; |
|
|
1505 | my $v_align = $bg_opts->{v_align} * 0.01; |
|
|
1506 | |
|
|
1507 | if (!$bg_opts->{tile}) { |
|
|
1508 | $file_expr .= "pad ("; |
|
|
1509 | } else { |
|
|
1510 | $file_expr .= "tile ("; |
|
|
1511 | } |
|
|
1512 | |
|
|
1513 | if ($bg_opts->{root_align}) { |
|
|
1514 | $file_expr .= "rootalign "; |
|
|
1515 | } else { |
|
|
1516 | $file_expr .= "align $h_align, $v_align, "; |
|
|
1517 | } |
|
|
1518 | |
|
|
1519 | if ($h_scale != 0 and $v_scale != 0) { |
|
|
1520 | my $op = $bg_opts->{keep_aspect} ? "fit" : "resize"; |
|
|
1521 | $file_expr .= "$op TW * $h_scale, TH * $v_scale, "; |
|
|
1522 | } |
|
|
1523 | |
|
|
1524 | my $path = q0 $bg_opts->{path}; |
|
|
1525 | |
|
|
1526 | $file_expr .= "keep { load $path })"; |
|
|
1527 | |
|
|
1528 | if ($expr) { |
|
|
1529 | $expr .= ", tint (\"[50]white\", $file_expr)"; |
|
|
1530 | } else { |
|
|
1531 | $expr = $file_expr; |
|
|
1532 | } |
|
|
1533 | } |
|
|
1534 | |
|
|
1535 | $expr |
|
|
1536 | } |
|
|
1537 | |
|
|
1538 | sub on_osc_seq { |
|
|
1539 | my ($self, $op, $arg) = @_; |
|
|
1540 | |
|
|
1541 | $self->{bg_opts} or return; |
|
|
1542 | |
|
|
1543 | $op =~ /^(?:20|705)$/ or return; |
|
|
1544 | |
|
|
1545 | if ($op eq "20") { |
|
|
1546 | if ($arg eq "?") { |
|
|
1547 | my $h_scale = $self->{bg_opts}->{h_scale}; |
|
|
1548 | my $v_scale = $self->{bg_opts}->{v_scale}; |
|
|
1549 | my $h_align = $self->{bg_opts}->{h_align}; |
|
|
1550 | my $v_align = $self->{bg_opts}->{v_align}; |
|
|
1551 | $self->cmd_parse ("\033]2;[${h_scale}x${v_scale}+${h_align}+${v_align}]\007"); |
|
|
1552 | } else { |
|
|
1553 | $self->old_bg_opts ($arg); |
|
|
1554 | my $expr = $self->old_bg_expr; |
|
|
1555 | $self->set_expr (parse_expr $expr) if $expr; |
|
|
1556 | } |
|
|
1557 | } elsif ($op eq "705") { |
|
|
1558 | $self->{bg_opts}->{tint} = $arg; |
|
|
1559 | my $expr = $self->old_bg_expr; |
|
|
1560 | $self->set_expr (parse_expr $expr) if $expr; |
|
|
1561 | } |
|
|
1562 | |
|
|
1563 | 1 |
|
|
1564 | } |
|
|
1565 | |
|
|
1566 | sub find_resource { |
|
|
1567 | my ($self, $res, $opt) = @_; |
|
|
1568 | |
|
|
1569 | my $v = $self->x_resource ($opt); |
|
|
1570 | $v = $self->x_resource ($res) unless defined $v; |
|
|
1571 | |
|
|
1572 | $v |
|
|
1573 | } |
|
|
1574 | |
1128 | sub on_start { |
1575 | sub on_start { |
1129 | my ($self) = @_; |
1576 | my ($self) = @_; |
1130 | |
1577 | |
1131 | my $expr = $self->x_resource ("%.expr") |
1578 | my $expr = $self->x_resource ("%.expr"); |
1132 | or return; |
|
|
1133 | |
1579 | |
1134 | $self->has_render |
1580 | if (!$expr) { |
1135 | or die "background extension needs RENDER extension 0.10 or higher, ignoring background-expr.\n"; |
1581 | $self->{bg_opts} = { h_scale => 100, v_scale => 100, |
|
|
1582 | h_align => 50, v_align => 50 }; |
|
|
1583 | |
|
|
1584 | $self->{bg_opts}{shade} = $self->find_resource ("shading", "sh"); |
|
|
1585 | $self->{bg_opts}{tint} = $self->find_resource ("tintColor", "tint"); |
|
|
1586 | $self->{bg_opts}{blur} = $self->find_resource ("blurRadius", "blr"); |
|
|
1587 | $self->{bg_opts}{root} = $self->find_resource ("transparent", "tr"); |
|
|
1588 | |
|
|
1589 | $self->old_bg_opts ($self->find_resource ("backgroundPixmap", "pixmap")); |
|
|
1590 | $expr = $self->old_bg_expr; |
|
|
1591 | } |
1136 | |
1592 | |
1137 | $self->set_expr (parse_expr $expr); |
1593 | $self->set_expr (parse_expr $expr); |
1138 | $self->{border} = $self->x_resource_boolean ("%.border"); |
1594 | $self->{border} = $self->x_resource_boolean ("%.border"); |
1139 | |
1595 | |
1140 | $MIN_INTERVAL = $self->x_resource ("%.interval"); |
1596 | $MIN_INTERVAL = $self->x_resource ("%.interval"); |