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.54 by root, Thu Jun 14 16:22:20 2012 UTC vs.
Revision 1.99 by sf-exg, Sat May 8 07:45:48 2021 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#: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 19background - manage terminal background
10 20
11=head1 SYNOPSIS 21=head1 SYNOPSIS
12 22
13 urxvt --background-expr 'background expression' 23 urxvt --background-expr 'background expression'
14 --background-border 24 --background-border
15 --background-interval seconds 25 --background-interval seconds
16 26
27=head1 QUICK AND DIRTY CHEAT SHEET
28
29Load a random jpeg image and tile the background with it without scaling
30or anything else:
31
32 load "/path/to/img.jpg"
33
34The same, but use mirroring/reflection instead of tiling:
35
36 mirror load "/path/to/img.jpg"
37
38Load an image and scale it to exactly fill the terminal window:
39
40 scale keep { load "/path/to/img.jpg" }
41
42Implement pseudo-transparency by using a suitably-aligned root pixmap
43as window background:
44
45 rootalign root
46
47Likewise, but keep a blurred copy:
48
49 rootalign keep { blur 10, root }
50
17=head1 DESCRIPTION 51=head1 DESCRIPTION
18 52
19This extension manages the terminal background by creating a picture that 53This extension manages the terminal background by creating a picture that
20is behind the text, replacing the normal background colour. 54is behind the text, replacing the normal background colour.
21 55
26to be as simple as possible. 60to be as simple as possible.
27 61
28For example, to load an image and scale it to the window size, you would 62For example, to load an image and scale it to the window size, you would
29use: 63use:
30 64
31 urxvt --background-expr 'scale load "/path/to/mybg.png"' 65 urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }'
32 66
33Or specified as a X resource: 67Or specified as a X resource:
34 68
35 URxvt.background-expr: scale load "/path/to/mybg.png" 69 URxvt.background.expr: scale keep { load "/path/to/mybg.png" }
36 70
37=head1 THEORY OF OPERATION 71=head1 THEORY OF OPERATION
38 72
39At startup, just before the window is mapped for the first time, the 73At startup, just before the window is mapped for the first time, the
40expression is evaluated and must yield an image. The image is then 74expression is evaluated and must yield an image. The image is then
53If any of the parameters that the expression relies on changes (when the 87If any of the parameters that the expression relies on changes (when the
54window is moved or resized, its position or size changes; when the root 88window is moved or resized, its position or size changes; when the root
55pixmap is replaced by another one the root background changes; or when the 89pixmap is replaced by another one the root background changes; or when the
56timer elapses), then the expression will be evaluated again. 90timer elapses), then the expression will be evaluated again.
57 91
58For example, an expression such as C<scale load "$HOME/mybg.png"> scales the 92For example, an expression such as C<scale keep { load "$HOME/mybg.png"
59image to the window size, so it relies on the window size and will 93}> scales the image to the window size, so it relies on the window size
60be reevaluated each time it is changed, but not when it moves for 94and will be reevaluated each time it is changed, but not when it moves for
61example. That ensures that the picture always fills the terminal, even 95example. That ensures that the picture always fills the terminal, even
62after its size changes. 96after its size changes.
63 97
64=head2 EXPRESSIONS 98=head2 EXPRESSIONS
65 99
66Expressions are normal Perl expressions, in fact, they are Perl blocks - 100Expressions are normal Perl expressions, in fact, they are Perl blocks -
67which means you could use multiple lines and statements: 101which means you could use multiple lines and statements:
68 102
103 scale keep {
69 again 3600; 104 again 3600;
70 if (localtime now)[6]) { 105 if (localtime now)[6]) {
71 return scale load "$HOME/weekday.png"; 106 return load "$HOME/weekday.png";
72 } else { 107 } else {
73 return scale load "$HOME/sunday.png"; 108 return load "$HOME/sunday.png";
109 }
74 } 110 }
75 111
76This expression is evaluated once per hour. It will set F<sunday.png> as 112This inner expression is evaluated once per hour (and whenever the
113terminal window is resized). It sets F<sunday.png> as background on
77background on Sundays, and F<weekday.png> on all other days. 114Sundays, and F<weekday.png> on all other days.
78 115
79Fortunately, we expect that most expressions will be much simpler, with 116Fortunately, we expect that most expressions will be much simpler, with
80little Perl knowledge needed. 117little Perl knowledge needed.
81 118
82Basically, you always start with a function that "generates" an image 119Basically, you always start with a function that "generates" an image
105get a percentage): 142get a percentage):
106 143
107 scale 2, load "$HOME/mypic.png" 144 scale 2, load "$HOME/mypic.png"
108 145
109This enlarges the image by a factor of 2 (200%). As you can see, C<scale> 146This enlarges the image by a factor of 2 (200%). As you can see, C<scale>
110has now two arguments, the C<200> and the C<load> expression, while 147has now two arguments, the C<2> and the C<load> expression, while
111C<load> only has one argument. Arguments are separated from each other by 148C<load> only has one argument. Arguments are separated from each other by
112commas. 149commas.
113 150
114Scale also accepts two arguments, which are then separate factors for both 151Scale also accepts two arguments, which are then separate factors for both
115horizontal and vertical dimensions. For example, this halves the image 152horizontal and vertical dimensions. For example, this halves the image
116width and doubles the image height: 153width and doubles the image height:
117 154
118 scale 0.5, 2, load "$HOME/mypic.png" 155 scale 0.5, 2, load "$HOME/mypic.png"
119 156
120Other effects than scaling are also readily available, for example, you can 157IF you try out these expressions, you might suffer from some sluggishness,
121tile the image to fill the whole window, instead of resizing it: 158because each time the terminal is resized, it loads the PNG image again
159and scales it. Scaling is usually fast (and unavoidable), but loading the
160image can be quite time consuming. This is where C<keep> comes in handy:
122 161
162 scale 0.5, 2, keep { load "$HOME/mypic.png" }
163
164The C<keep> operator executes all the statements inside the braces only
165once, or when it thinks the outcome might change. In other cases it
166returns the last value computed by the brace block.
167
168This means that the C<load> is only executed once, which makes it much
169faster, but also means that more memory is being used, because the loaded
170image must be kept in memory at all times. In this expression, the
171trade-off is likely worth it.
172
173But back to effects: Other effects than scaling are also readily
174available, for example, you can tile the image to fill the whole window,
175instead of resizing it:
176
123 tile load "$HOME/mypic.png" 177 tile keep { load "$HOME/mypic.png" }
124 178
125In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator 179In fact, images returned by C<load> are in C<tile> mode by default, so the
126is kind of superfluous. 180C<tile> operator is kind of superfluous.
127 181
128Another common effect is to mirror the image, so that the same edges touch: 182Another common effect is to mirror the image, so that the same edges
183touch:
129 184
130 mirror load "$HOME/mypic.png" 185 mirror keep { load "$HOME/mypic.png" }
131 186
132This is also a typical background expression: 187Another common background expression is:
133 188
134 rootalign root 189 rootalign root
135 190
136It first takes a snapshot of the screen background image, and then 191This one first takes a snapshot of the screen background image, and then
137moves it to the upper left corner of the screen - the result is 192moves it to the upper left corner of the screen (as opposed to the upper
138pseudo-transparency, as the image seems to be static while the window is 193left corner of the terminal window)- the result is pseudo-transparency:
139moved around. 194the image seems to be static while the window is moved around.
140 195
141=head2 CYCLES AND CACHING 196=head2 COLOUR SPECIFICATIONS
142 197
143As has been mentioned before, the expression might be evaluated multiple 198Whenever an operator expects a "colour", then this can be specified in one
144times. Each time the expression is reevaluated, a new cycle is said to 199of two ways: Either as string with an X11 colour specification, such as:
145have begun. Many operators cache their results till the next cycle.
146 200
147For example, the C<load> operator keeps a copy of the image. If it is 201 "red" # named colour
148asked to load the same image on the next cycle it will not load it again, 202 "#f00" # simple rgb
149but return the cached copy. 203 "[50]red" # red with 50% alpha
204 "TekHVC:300/50/50" # anything goes
150 205
151This only works for one cycle though, so as long as you load the same 206OR as an array reference with one, three or four components:
152image every time, it will always be cached, but when you load a different
153image, it will forget about the first one.
154 207
155This allows you to either speed things up by keeping multiple images in 208 [0.5] # 50% gray, 100% alpha
156memory, or conserve memory by loading images more often. 209 [0.5, 0, 0] # dark red, no green or blur, 100% alpha
210 [0.5, 0, 0, 0.7] # same with explicit 70% alpha
157 211
158For example, you can keep two images in memory and use a random one like 212=head2 CACHING AND SENSITIVITY
159this:
160 213
161 my $img1 = load "img1.png"; 214Since some operations (such as C<load> and C<blur>) can take a long time,
162 my $img2 = load "img2.png"; 215caching results can be very important for a smooth operation. Caching can
163 (0.5 > rand) ? $img1 : $img2 216also be useful to reduce memory usage, though, for example, when an image
217is cached by C<load>, it could be shared by multiple terminal windows
218running inside urxvtd.
164 219
165Since both images are "loaded" every time the expression is evaluated, 220=head3 C<keep { ... }> caching
166they are always kept in memory. Contrast this version:
167 221
168 my $path1 = "img1.png"; 222The most important way to cache expensive operations is to use C<keep {
169 my $path2 = "img2.png"; 223... }>. The C<keep> operator takes a block of multiple statements enclosed
170 load ((0.5 > rand) ? $path1 : $path2) 224by C<{}> and keeps the return value in memory.
171 225
172Here, a path is selected randomly, and load is only called for one image, 226An expression can be "sensitive" to various external events, such as
173so keeps only one image in memory. If, on the next evaluation, luck 227scaling or moving the window, root background changes and timers. Simply
174decides to use the other path, then it will have to load that image again. 228using an expression (such as C<scale> without parameters) that depends on
229certain changing values (called "variables"), or using those variables
230directly, will make an expression sensitive to these events - for example,
231using C<scale> or C<TW> will make the expression sensitive to the terminal
232size, and thus to resizing events.
233
234When such an event happens, C<keep> will automatically trigger a
235reevaluation of the whole expression with the new value of the expression.
236
237C<keep> is most useful for expensive operations, such as C<blur>:
238
239 rootalign keep { blur 20, root }
240
241This makes a blurred copy of the root background once, and on subsequent
242calls, just root-aligns it. Since C<blur> is usually quite slow and
243C<rootalign> is quite fast, this trades extra memory (for the cached
244blurred pixmap) with speed (blur only needs to be redone when root
245changes).
246
247=head3 C<load> caching
248
249The C<load> operator itself does not keep images in memory, but as long as
250the image is still in memory, C<load> will use the in-memory image instead
251of loading it freshly from disk.
252
253That means that this expression:
254
255 keep { load "$HOME/path..." }
256
257Not only caches the image in memory, other terminal instances that try to
258C<load> it can reuse that in-memory copy.
175 259
176=head1 REFERENCE 260=head1 REFERENCE
177 261
178=head2 COMMAND LINE SWITCHES 262=head2 COMMAND LINE SWITCHES
179 263
203=back 287=back
204 288
205=cut 289=cut
206 290
207our %_IMG_CACHE; 291our %_IMG_CACHE;
208our %_ONCE_CACHE;
209our $HOME; 292our $HOME;
210our ($self, $old, $new); 293our ($self, $frame);
211our ($x, $y, $w, $h); 294our ($x, $y, $w, $h, $focus);
212 295
213# enforce at least this interval between updates 296# enforce at least this interval between updates
214our $MIN_INTERVAL = 6/59.951; 297our $MIN_INTERVAL = 6/59.951;
215 298
216{ 299{
217 package urxvt::bgdsl; # background language 300 package urxvt::bgdsl; # background language
301
302 sub FR_PARENT() { 0 } # parent frame, if any - must be #0
303 sub FR_CACHE () { 1 } # cached values
304 sub FR_AGAIN () { 2 } # what this expr is sensitive to
305 sub FR_STATE () { 3 } # watchers etc.
218 306
219 use List::Util qw(min max sum shuffle); 307 use List::Util qw(min max sum shuffle);
220 308
221=head2 PROVIDERS/GENERATORS 309=head2 PROVIDERS/GENERATORS
222 310
229=item load $path 317=item load $path
230 318
231Loads the image at the given C<$path>. The image is set to plane tiling 319Loads the image at the given C<$path>. The image is set to plane tiling
232mode. 320mode.
233 321
234Loaded images will be cached for one cycle, and shared between temrinals 322If the image is already in memory (e.g. because another terminal instance
235running in the same process (e.g. in C<urxvtd>). 323uses it), then the in-memory copy is returned instead.
236 324
237=item load_uc $path 325=item load_uc $path
238 326
239Load uncached - same as load, but does not cache the image. This function 327Load uncached - same as load, but does not cache the image, which means it
240is most useufl if you want to optimise a background expression in some 328is I<always> loaded from the filesystem again, even if another copy of it
241way. 329is in memory at the time.
242 330
243=cut 331=cut
244 332
245 sub load_uc($) { 333 sub load_uc($) {
334 $self->new_img_from_file ($_[0])
335 }
336
337 sub load($) {
246 my ($path) = @_; 338 my ($path) = @_;
247 339
248 $_IMG_CACHE{$path} || do { 340 $_IMG_CACHE{$path} || do {
249 my $img = $self->new_img_from_file ($path); 341 my $img = load_uc $path;
250 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img); 342 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img);
251 $img 343 $img
252 } 344 }
253 } 345 }
254 346
255 sub load($) {
256 my ($path) = @_;
257
258 $new->{load}{$path} = $old->{load}{$path} || load_uc $path;
259 }
260
261=item root 347=item root
262 348
263Returns the root window pixmap, that is, hopefully, the background image 349Returns the root window pixmap, that is, hopefully, the background image
264of your screen. The image is set to extend mode. 350of your screen.
265 351
266This function makes your expression root sensitive, that means it will be 352This function makes your expression root sensitive, that means it will be
267reevaluated when the bg image changes. 353reevaluated when the bg image changes.
268 354
269=cut 355=cut
270 356
271 sub root() { 357 sub root() {
272 $new->{rootpmap_sensitive} = 1; 358 $frame->[FR_AGAIN]{rootpmap} = 1;
273 $self->new_img_from_root 359 $self->new_img_from_root
274 } 360 }
275 361
276=item solid $colour 362=item solid $colour
277 363
286=cut 372=cut
287 373
288 sub solid($;$$) { 374 sub solid($;$$) {
289 my $colour = pop; 375 my $colour = pop;
290 376
291 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); 377 my $img = $self->new_img (urxvt::PictStandardARGB32, 0, 0, $_[0] || 1, $_[1] || 1);
292 $img->fill ($colour); 378 $img->fill ($colour);
293 $img 379 $img
294 } 380 }
295 381
296=item clone $img 382=item clone $img
301=cut 387=cut
302 388
303 sub clone($) { 389 sub clone($) {
304 $_[0]->clone 390 $_[0]->clone
305 } 391 }
392
393=item merge $img ...
394
395Takes any number of images and merges them together, creating a single
396image containing them all. The tiling mode of the first image is used as
397the tiling mode of the resulting image.
398
399This function is called automatically when an expression returns multiple
400images.
401
402=cut
403
404 sub merge(@) {
405 return $_[0] unless $#_;
406
407 # rather annoyingly clumsy, but optimisation is for another time
408
409 my $x0 = +1e9;
410 my $y0 = +1e9;
411 my $x1 = -1e9;
412 my $y1 = -1e9;
413
414 for (@_) {
415 my ($x, $y, $w, $h) = $_->geometry;
416
417 $x0 = $x if $x0 > $x;
418 $y0 = $y if $y0 > $y;
419
420 $x += $w;
421 $y += $h;
422
423 $x1 = $x if $x1 < $x;
424 $y1 = $y if $y1 < $y;
425 }
426
427 my $base = $self->new_img (urxvt::PictStandardARGB32, $x0, $y0, $x1 - $x0, $y1 - $y0);
428 $base->repeat_mode ($_[0]->repeat_mode);
429 $base->fill ([0, 0, 0, 0]);
430
431 $base->draw ($_)
432 for @_;
433
434 $base
435 }
436
437=back
306 438
307=head2 TILING MODES 439=head2 TILING MODES
308 440
309The following operators modify the tiling mode of an image, that is, the 441The following operators modify the tiling mode of an image, that is, the
310way that pixels outside the image area are painted when the image is used. 442way that pixels outside the image area are painted when the image is used.
402 534
403Return the X and Y coordinates of the terminal window (the terminal 535Return the X and Y coordinates of the terminal window (the terminal
404window is the full window by default, and the character area only when in 536window is the full window by default, and the character area only when in
405border-respect mode). 537border-respect mode).
406 538
407Using these functions make your expression sensitive to window moves. 539Using these functions makes your expression sensitive to window moves.
408 540
409These functions are mainly useful to align images to the root window. 541These functions are mainly useful to align images to the root window.
410 542
411Example: load an image and align it so it looks as if anchored to the 543Example: load an image and align it so it looks as if anchored to the
412background. 544background (that's exactly what C<rootalign> does btw.):
413 545
414 move -TX, -TY, load "mybg.png" 546 move -TX, -TY, keep { load "mybg.png" }
415 547
416=item TW 548=item TW
549
550=item TH
417 551
418Return the width (C<TW>) and height (C<TH>) of the terminal window (the 552Return the width (C<TW>) and height (C<TH>) of the terminal window (the
419terminal window is the full window by default, and the character area only 553terminal window is the full window by default, and the character area only
420when in border-respect mode). 554when in border-respect mode).
421 555
422Using these functions make your expression sensitive to window resizes. 556Using these functions makes your expression sensitive to window resizes.
423 557
424These functions are mainly useful to scale images, or to clip images to 558These functions are mainly useful to scale images, or to clip images to
425the window size to conserve memory. 559the window size to conserve memory.
426 560
427Example: take the screen background, clip it to the window size, blur it a 561Example: take the screen background, clip it to the window size, blur it a
428bit, align it to the window position and use it as background. 562bit, align it to the window position and use it as background.
429 563
430 clip move -TX, -TY, blur 5, root 564 clip move -TX, -TY, keep { blur 5, root }
431 565
432=cut 566=item FOCUS
433 567
434 sub TX() { $new->{position_sensitive} = 1; $x } 568Returns a boolean indicating whether the terminal window has keyboard
435 sub TY() { $new->{position_sensitive} = 1; $y } 569focus, in which case it returns true.
436 sub TW() { $new->{size_sensitive} = 1; $w } 570
437 sub TH() { $new->{size_sensitive} = 1; $h } 571Using this function makes your expression sensitive to focus changes.
572
573A common use case is to fade the background image when the terminal loses
574focus, often together with the C<-fade> command line option. In fact,
575there is a special function for just that use case: C<focus_fade>.
576
577Example: use two entirely different background images, depending on
578whether the window has focus.
579
580 FOCUS ? keep { load "has_focus.jpg" } : keep { load "no_focus.jpg" }
581
582=cut
583
584 sub TX () { $frame->[FR_AGAIN]{position} = 1; $x }
585 sub TY () { $frame->[FR_AGAIN]{position} = 1; $y }
586 sub TW () { $frame->[FR_AGAIN]{size} = 1; $w }
587 sub TH () { $frame->[FR_AGAIN]{size} = 1; $h }
588 sub FOCUS() { $frame->[FR_AGAIN]{focus} = 1; $focus }
438 589
439=item now 590=item now
440 591
441Returns the current time as (fractional) seconds since the epoch. 592Returns the current time as (fractional) seconds since the epoch.
442 593
449C<$seconds> seconds. 600C<$seconds> seconds.
450 601
451Example: load some image and rotate it according to the time of day (as if it were 602Example: load some image and rotate it according to the time of day (as if it were
452the hour pointer of a clock). Update this image every minute. 603the hour pointer of a clock). Update this image every minute.
453 604
605 again 60;
454 again 60; rotate 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" 606 rotate 50, 50, (now % 86400) * -72 / 8640, scale keep { load "myclock.png" }
455 607
456=item counter $seconds 608=item counter $seconds
457 609
458Like C<again>, but also returns an increasing counter value, starting at 610Like C<again>, but also returns an increasing counter value, starting at
4590, which might be useful for some simple animation effects. 6110, which might be useful for some simple animation effects.
461=cut 613=cut
462 614
463 sub now() { urxvt::NOW } 615 sub now() { urxvt::NOW }
464 616
465 sub again($) { 617 sub again($) {
466 $new->{again} = $_[0]; 618 $frame->[FR_AGAIN]{time} = $_[0];
467 } 619 }
468 620
469 sub counter($) { 621 sub counter($) {
470 $new->{again} = $_[0]; 622 $frame->[FR_AGAIN]{time} = $_[0];
471 $self->{counter} + 0 623 $frame->[FR_STATE]{counter} + 0
472 } 624 }
473 625
474=back 626=back
475 627
476=head2 SHAPE CHANGING OPERATORS 628=head2 SHAPE CHANGING OPERATORS
488Clips an image to the given rectangle. If the rectangle is outside the 640Clips an image to the given rectangle. If the rectangle is outside the
489image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is 641image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is
490larger than the image, then the tiling mode defines how the extra pixels 642larger than the image, then the tiling mode defines how the extra pixels
491will be filled. 643will be filled.
492 644
493If C<$x> an C<$y> are missing, then C<0> is assumed for both. 645If C<$x> and C<$y> are missing, then C<0> is assumed for both.
494 646
495If C<$width> and C<$height> are missing, then the window size will be 647If C<$width> and C<$height> are missing, then the window size will be
496assumed. 648assumed.
497 649
498Example: load an image, blur it, and clip it to the window size to save 650Example: load an image, blur it, and clip it to the window size to save
499memory. 651memory.
500 652
501 clip blur 10, load "mybg.png" 653 clip keep { blur 10, load "mybg.png" }
502 654
503=cut 655=cut
504 656
505 sub clip($;$$;$$) { 657 sub clip($;$$;$$) {
506 my $img = pop; 658 my $img = pop;
516=item scale $width_factor, $height_factor, $img 668=item scale $width_factor, $height_factor, $img
517 669
518Scales the image by the given factors in horizontal 670Scales the image by the given factors in horizontal
519(C<$width>) and vertical (C<$height>) direction. 671(C<$width>) and vertical (C<$height>) direction.
520 672
521If only one factor is give, it is used for both directions. 673If only one factor is given, it is used for both directions.
522 674
523If no factors are given, scales the image to the window size without 675If no factors are given, scales the image to the window size without
524keeping aspect. 676keeping aspect.
525 677
526=item resize $width, $height, $img 678=item resize $width, $height, $img
600the terminal window (or the box specified by C<$width> and C<$height> if 752the terminal window (or the box specified by C<$width> and C<$height> if
601given). 753given).
602 754
603Example: load an image and center it. 755Example: load an image and center it.
604 756
605 center pad load "mybg.png" 757 center keep { pad load "mybg.png" }
606 758
607=item rootalign $img 759=item rootalign $img
608 760
609Moves the image so that it appears glued to the screen as opposed to the 761Moves the image so that it appears glued to the screen as opposed to the
610window. This gives the illusion of a larger area behind the window. It is 762window. This gives the illusion of a larger area behind the window. It is
611exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the 763exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the
612top left of the screen. 764top left of the screen.
613 765
614Example: load a background image, put it in mirror mode and root align it. 766Example: load a background image, put it in mirror mode and root align it.
615 767
616 rootalign mirror load "mybg.png" 768 rootalign keep { mirror load "mybg.png" }
617 769
618Example: take the screen background and align it, giving the illusion of 770Example: take the screen background and align it, giving the illusion of
619transparency as long as the window isn't in front of other windows. 771transparency as long as the window isn't in front of other windows.
620 772
621 rootalign root 773 rootalign root
646 798
647 sub rootalign($) { 799 sub rootalign($) {
648 move -TX, -TY, $_[0] 800 move -TX, -TY, $_[0]
649 } 801 }
650 802
651=item rotate $center_x, $center_y, $degrees 803=item rotate $center_x, $center_y, $degrees, $img
652 804
653Rotates the image by C<$degrees> degrees, counter-clockwise, around the 805Rotates the image clockwise by C<$degrees> degrees, around the point at
654pointer at C<$center_x> and C<$center_y> (specified as factor of image 806C<$center_x> and C<$center_y> (specified as factor of image width/height).
655width/height).
656 807
657#TODO# new width, height, maybe more operators?
658
659Example: rotate the image by 90 degrees 808Example: rotate the image by 90 degrees around its center.
809
810 rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" }
660 811
661=cut 812=cut
662 813
663 sub rotate($$$$) { 814 sub rotate($$$$) {
664 my $img = pop; 815 my $img = pop;
665 $img->rotate ( 816 $img->rotate (
666 $_[0] * $img->w, 817 $_[0] * ($img->w + $img->x),
667 $_[1] * $img->h, 818 $_[1] * ($img->h + $img->y),
668 $_[2] * (3.14159265 / 180), 819 $_[2] * (3.14159265 / 180),
669 ) 820 )
670 } 821 }
671 822
672=back 823=back
674=head2 COLOUR MODIFICATIONS 825=head2 COLOUR MODIFICATIONS
675 826
676The following operators change the pixels of the image. 827The following operators change the pixels of the image.
677 828
678=over 4 829=over 4
830
831=item tint $color, $img
832
833Tints the image in the given colour.
834
835Example: tint the image red.
836
837 tint "red", load "rgb.png"
838
839Example: the same, but specify the colour by component.
840
841 tint [1, 0, 0], load "rgb.png"
842
843=cut
844
845 sub tint($$) {
846 $_[1]->tint ($_[0])
847 }
848
849=item shade $factor, $img
850
851Shade the image by the given factor.
852
853=cut
854
855 sub shade($$) {
856 $_[1]->shade ($_[0])
857 }
679 858
680=item contrast $factor, $img 859=item contrast $factor, $img
681 860
682=item contrast $r, $g, $b, $img 861=item contrast $r, $g, $b, $img
683 862
713latter in a white picture. 892latter in a white picture.
714 893
715Due to idiosyncrasies in the underlying XRender extension, biases less 894Due to idiosyncrasies in the underlying XRender extension, biases less
716than zero can be I<very> slow. 895than zero can be I<very> slow.
717 896
897You can also try the experimental(!) C<muladd> operator.
898
718=cut 899=cut
719 900
720 sub contrast($$;$$;$) { 901 sub contrast($$;$$;$) {
721 my $img = pop; 902 my $img = pop;
722 my ($r, $g, $b, $a) = @_; 903 my ($r, $g, $b, $a) = @_;
737 $a = 1 if @_ < 4; 918 $a = 1 if @_ < 4;
738 919
739 $img = $img->clone; 920 $img = $img->clone;
740 $img->brightness ($r, $g, $b, $a); 921 $img->brightness ($r, $g, $b, $a);
741 $img 922 $img
923 }
924
925=item muladd $mul, $add, $img # EXPERIMENTAL
926
927First multiplies the pixels by C<$mul>, then adds C<$add>. This can be used
928to implement brightness and contrast at the same time, with a wider value
929range than contrast and brightness operators.
930
931Due to numerous bugs in XRender implementations, it can also introduce a
932number of visual artifacts.
933
934Example: increase contrast by a factor of C<$c> without changing image
935brightness too much.
936
937 muladd $c, (1 - $c) * 0.5, $img
938
939=cut
940
941 sub muladd($$$) {
942 $_[2]->muladd ($_[0], $_[1])
742 } 943 }
743 944
744=item blur $radius, $img 945=item blur $radius, $img
745 946
746=item blur $radius_horz, $radius_vert, $img 947=item blur $radius_horz, $radius_vert, $img
758 sub blur($$;$) { 959 sub blur($$;$) {
759 my $img = pop; 960 my $img = pop;
760 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) 961 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0])
761 } 962 }
762 963
964=item focus_fade $img
965
966=item focus_fade $factor, $img
967
968=item focus_fade $factor, $color, $img
969
970Fades the image by the given factor (and colour) when focus is lost (the
971same as the C<-fade>/C<-fadecolor> command line options, which also supply
972the default values for C<factor> and C<$color>. Unlike with C<-fade>, the
973C<$factor> is a real value, not a percentage value (that is, 0..1, not
9740..100).
975
976Example: 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
763=back 997=back
764 998
765=head2 OTHER STUFF 999=head2 OTHER STUFF
766 1000
767Anything that didn't fit any of the other categories, even after appliyng 1001Anything that didn't fit any of the other categories, even after applying
768force and closing our eyes. 1002force and closing our eyes.
769 1003
770=over 4 1004=over 4
771 1005
772=item once { ... } 1006=item keep { ... }
773 1007
774This function takes a code block as argument, that is, one or more 1008This operator takes a code block as argument, that is, one or more
775statements enclosed by braces. 1009statements enclosed by braces.
776 1010
777The trick is that this code block is only evaluated once - future calls 1011The trick is that this code block is only evaluated when the outcome
778will simply return the original image (yes, it should only be used with 1012changes - on other calls the C<keep> simply returns the image it computed
779images). 1013previously (yes, it should only be used with images). Or in other words,
1014C<keep> I<caches> the result of the code block so it doesn't need to be
1015computed again.
780 1016
781This can be extremely useful to avoid redoign the same slow operations 1017This can be extremely useful to avoid redoing slow operations - for
782again and again- for example, if your background expression takes the root 1018example, if your background expression takes the root background, blurs it
783background, blurs it and then root-aligns it it would have to blur the 1019and then root-aligns it it would have to blur the root background on every
784root background on every window move or resize. 1020window move or resize.
785 1021
1022Another example is C<load>, which can be quite slow.
1023
1024In fact, urxvt itself encloses the whole expression in some kind of
1025C<keep> block so it only is reevaluated as required.
1026
786Putting the blur into a C<once> block will make sure the blur is only done 1027Putting the blur into a C<keep> block will make sure the blur is only done
787once: 1028once, while the C<rootalign> is still done each time the window moves.
788 1029
789 rootlign once { blur 10, root } 1030 rootalign keep { blur 10, root }
790 1031
791This leaves the question of how to force reevaluation of the block, in 1032This leaves the question of how to force reevaluation of the block,
792case the root background changes: Right now, all once blocks forget that 1033in case the root background changes: If expression inside the block
793they ahve been executed before each time the root background changes (if 1034is sensitive to some event (root background changes, window geometry
794the expression is sensitive to that) or when C<once_again> is called. 1035changes), then it will be reevaluated automatically as needed.
795
796=item once_again
797
798Resets all C<once> block as if they had never been called, i.e. on the
799next call they will be reevaluated again.
800
801=cut
802
803 sub once(&) {
804 $_ONCE_CACHE{$_[0]+0} ||= $_[0]()
805 }
806
807 sub once_again() {
808 %_ONCE_CACHE = ();
809 }
810 1036
811=back 1037=back
812 1038
1039=head1 OLD BACKGROUND IMAGE SETTINGS
1040
1041This extension also provides support for the old options/resources and
1042OSC sequences for setting a background image. These settings are
1043B<deprecated> and will be removed in future versions.
1044
1045=head2 OPTIONS AND RESOURCES
1046
1047=over 4
1048
1049=item B<-pixmap> I<file[;oplist]>
1050
1051=item B<backgroundPixmap:> I<file[;oplist]>
1052
1053Use the specified image file as the window's background and also
1054optionally specify a colon separated list of operations to modify it.
1055Note that you may need to quote the C<;> character when using the
1056command line option, as C<;> is usually a metacharacter in shells.
1057Supported operations are:
1058
1059=over 4
1060
1061=item B<WxH+X+Y>
1062
1063sets scale and position. B<"W" / "H"> specify the horizontal/vertical
1064scale (percent), and B<"X" / "Y"> locate the image centre (percent). A
1065scale of 0 disables scaling.
1066
1067=item B<op=tile>
1068
1069enables tiling
1070
1071=item B<op=keep-aspect>
1072
1073maintain the image aspect ratio when scaling
1074
1075=item B<op=root-align>
1076
1077use the position of the terminal window relative to the root window as
1078the image offset, simulating a root window background
1079
1080=back
1081
1082The default scale and position setting is C<100x100+50+50>.
1083Alternatively, a predefined set of templates can be used to achieve
1084the most common setups:
1085
1086=over 4
1087
1088=item B<style=tiled>
1089
1090the image is tiled with no scaling. Equivalent to 0x0+0+0:op=tile
1091
1092=item B<style=aspect-stretched>
1093
1094the image is scaled to fill the whole window maintaining the aspect
1095ratio and centered. Equivalent to 100x100+50+50:op=keep-aspect
1096
1097=item B<style=stretched>
1098
1099the image is scaled to fill the whole window. Equivalent to 100x100
1100
1101=item B<style=centered>
1102
1103the image is centered with no scaling. Equivalent to 0x0+50+50
1104
1105=item B<style=root-tiled>
1106
1107the image is tiled with no scaling and using 'root' positioning.
1108Equivalent to 0x0:op=tile:op=root-align
1109
1110=back
1111
1112If multiple templates are specified the last one wins. Note that a
1113template overrides all the scale, position and operations settings.
1114
1115If used in conjunction with pseudo-transparency, the specified image
1116will be blended over the transparent background using alpha-blending.
1117
1118=item B<-tr>|B<+tr>
1119
1120=item B<transparent:> I<boolean>
1121
1122Turn 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
1128Tint the transparent background with the given colour. Note that a
1129black tint yields a completely black image while a white tint yields
1130the image unchanged.
1131
1132=item B<-sh> I<number>
1133
1134=item B<shading:> I<number>
1135
1136Darken (0 .. 99) or lighten (101 .. 200) the transparent background.
1137A value of 100 means no shading.
1138
1139=item B<-blr> I<HxV>
1140
1141=item B<blurRadius:> I<HxV>
1142
1143Apply gaussian blur with the specified radius to the transparent
1144background. If a single number is specified, the vertical and
1145horizontal radii are considered to be the same. Setting one of the
1146radii to 1 and the other to a large number creates interesting effects
1147on some backgrounds. The maximum radius value is 128. An horizontal or
1148vertical radius of 0 disables blurring.
1149
1150=back
1151
1152=head2 OSC sequences
1153
1154This extension will react to the following OSC sequences. Note that
1155this extension will not be autoloaded when these are used currently,
1156so to make urxvt recognize them, you have to enable the C<background>
1157extension. One way to achieve that is to use the C<--background-expr ''>
1158command line argument or by specifying an empty C<URxvt.background.expr:>>
1159resource.
1160
1161=over 4
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
1166parameters: the value of B<< C<Pt> >> can be one of the following
1167commands:
1168
1169=over 4
1170
1171=item B<< C<?> >>
1172
1173display scale and position in the title
1174
1175=item B<< C<;WxH+X+Y> >>
1176
1177change scale and/or position
1178
1179=item B<< C<FILE;WxH+X+Y> >>
1180
1181change background image
1182
1183=back
1184
1185=cut
1186
1187 sub keep(&) {
1188 my $id = $_[0]+0;
1189
1190 local $frame = $self->{frame_cache}{$id} ||= [$frame];
1191
1192 unless ($frame->[FR_CACHE]) {
1193 $frame->[FR_CACHE] = [ $_[0]() ];
1194
1195 my $self = $self;
1196 my $frame = $frame;
1197 Scalar::Util::weaken $frame;
1198 $self->compile_frame ($frame, sub {
1199 # clear this frame cache, also for all parents
1200 for (my $frame = $frame; $frame; $frame = $frame->[0]) {
1201 undef $frame->[FR_CACHE];
1202 }
1203
1204 $self->recalculate;
1205 });
1206 };
1207
1208 # in scalar context we always return the first original result, which
1209 # is not quite how perl works.
1210 wantarray
1211 ? @{ $frame->[FR_CACHE] }
1212 : $frame->[FR_CACHE][0]
1213 }
1214
1215# sub keep_clear() {
1216# delete $self->{frame_cache};
1217# }
1218
1219=back
1220
813=cut 1221=cut
814 1222
815} 1223}
816 1224
817sub parse_expr { 1225sub parse_expr {
818 my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}"; 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 {
1234 $expr = eval
1235 "sub {\n"
1236 . "package urxvt::bgdsl;\n"
1237 . "#line 0 'background expression'\n"
1238 . "$expr\n"
1239 . "}";
819 die if $@; 1240 die if $@;
1241 }
1242
820 $expr 1243 $expr
821} 1244}
822 1245
823# compiles a parsed expression 1246# compiles a parsed expression
824sub set_expr { 1247sub set_expr {
825 my ($self, $expr) = @_; 1248 my ($self, $expr) = @_;
826 1249
1250 $self->{root} = []; # the outermost frame
827 $self->{expr} = $expr; 1251 $self->{expr} = $expr;
828 $self->recalculate; 1252 $self->recalculate;
1253}
1254
1255# takes a hash of sensitivity indicators and installs watchers
1256sub compile_frame {
1257 my ($self, $frame, $cb) = @_;
1258
1259 my $state = $frame->[urxvt::bgdsl::FR_STATE] ||= {};
1260 my $again = $frame->[urxvt::bgdsl::FR_AGAIN];
1261
1262 # don't keep stuff alive
1263 Scalar::Util::weaken $state;
1264
1265 if ($again->{nested}) {
1266 $state->{nested} = 1;
1267 } else {
1268 delete $state->{nested};
1269 }
1270
1271 if (my $interval = $again->{time}) {
1272 $state->{time} = [$interval, urxvt::timer->new->after ($interval)->interval ($interval)]
1273 if $state->{time}[0] != $interval;
1274
1275 # callback *might* have changed, although we could just rule that out
1276 $state->{time}[1]->cb (sub {
1277 ++$state->{counter};
1278 $cb->();
1279 });
1280 } else {
1281 delete $state->{time};
1282 }
1283
1284 if ($again->{position}) {
1285 $state->{position} = $self->on (position_change => $cb);
1286 } else {
1287 delete $state->{position};
1288 }
1289
1290 if ($again->{size}) {
1291 $state->{size} = $self->on (size_change => $cb);
1292 } else {
1293 delete $state->{size};
1294 }
1295
1296 if ($again->{rootpmap}) {
1297 $state->{rootpmap} = $self->on (rootpmap_change => $cb);
1298 } else {
1299 delete $state->{rootpmap};
1300 }
1301
1302 if ($again->{focus}) {
1303 $state->{focus} = $self->on (focus_in => $cb, focus_out => $cb);
1304 } else {
1305 delete $state->{focus};
1306 }
829} 1307}
830 1308
831# evaluate the current bg expression 1309# evaluate the current bg expression
832sub recalculate { 1310sub recalculate {
833 my ($arg_self) = @_; 1311 my ($arg_self) = @_;
841 return; 1319 return;
842 } 1320 }
843 1321
844 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; 1322 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
845 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
846 # set environment to evaluate user expression 1329 # set environment to evaluate user expression
847 1330
848 local $self = $arg_self; 1331 local $self = $arg_self;
849
850 local $HOME = $ENV{HOME}; 1332 local $HOME = $ENV{HOME};
851 local $old = $self->{state}; 1333 local $frame = $self->{root};
852 local $new = my $state = $self->{state} = {};
853 1334
854 ($x, $y, $w, $h) =
855 $self->background_geometry ($self->{border}); 1335 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
1336 $focus = $self->focus;
856 1337
857 # evaluate user expression 1338 # evaluate user expression
858 1339
859 my $img = eval { $self->{expr}->() }; 1340 my @img = eval { $self->{expr}->() };
860 warn $@ if $@;#d# 1341 die $@ if $@;
861 die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img"; 1342 die "background-expr did not return anything.\n" unless @img;
862 1343
863 $state->{size_sensitive} = 1 1344 if ($img[0]) {
1345 die "background-expr: expected image(s), got something else.\n"
1346 if grep { !UNIVERSAL::isa $_, "urxvt::img" } @img;
1347
1348 my $img = urxvt::bgdsl::merge @img;
1349
1350 $frame->[FR_AGAIN]{size} = 1
864 if $img->repeat_mode != urxvt::RepeatNormal; 1351 if $img->repeat_mode != urxvt::RepeatNormal;
865 1352
866 # if the expression is sensitive to external events, prepare reevaluation then 1353 # if the expression is sensitive to external events, prepare reevaluation then
1354 $self->compile_frame ($frame, sub { $arg_self->recalculate });
867 1355
868 my $repeat; 1356 # clear stuff we no longer need
869 1357
870 if (my $again = $state->{again}) { 1358# unless (%{ $frame->[FR_STATE] }) {
871 $repeat = 1; 1359# delete $self->{state};
872 my $self = $self; 1360# delete $self->{expr};
873 $state->{timer} = $again == $old->{again}
874 ? $old->{timer}
875 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
876 ++$self->{counter};
877 $self->recalculate
878 });
879 } 1361# }
880 1362
881 if (delete $state->{position_sensitive}) { 1363 # set background pixmap
882 $repeat = 1; 1364
883 $self->enable (position_change => sub { $_[0]->recalculate }); 1365 $self->set_background ($img, $self->{border});
884 } else { 1366 } else {
885 $self->disable ("position_change"); 1367 $self->clr_background;
886 } 1368 }
887 1369
888 if (delete $state->{size_sensitive}) {
889 $repeat = 1;
890 $self->enable (size_change => sub { $_[0]->recalculate });
891 } else {
892 $self->disable ("size_change");
893 }
894
895 if (delete $state->{rootpmap_sensitive}) {
896 $repeat = 1;
897 $self->enable (rootpmap_change => sub { $_[0]->recalculate });
898 } else {
899 $self->disable ("rootpmap_change");
900 }
901
902 # clear stuff we no longer need
903
904 %$old = ();
905
906 unless ($repeat) {
907 delete $self->{state};
908 delete $self->{expr};
909 }
910
911 # set background pixmap
912
913 $self->set_background ($img, $self->{border});
914 $self->scr_recolour (0); 1370 $self->scr_recolor (0);
915 $self->want_refresh; 1371 $self->want_refresh;
916} 1372}
917 1373
1374sub 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.
1453sub q0 {
1454 (my $str = shift) =~ s/\x00//g; # make sure there really aren't any embedded NULs
1455 "q\x00$str\x00"
1456}
1457
1458sub 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
1538sub 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
1566sub 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
918sub on_start { 1575sub on_start {
919 my ($self) = @_; 1576 my ($self) = @_;
920 1577
921 my $expr = $self->x_resource ("%.expr") 1578 my $expr = $self->x_resource ("%.expr");
922 or return;
923 1579
924 $self->has_render 1580 if (!$expr) {
925 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 }
926 1592
927 $self->set_expr (parse_expr $expr); 1593 $self->set_expr (parse_expr $expr);
928 $self->{border} = $self->x_resource_boolean ("%.border"); 1594 $self->{border} = $self->x_resource_boolean ("%.border");
929 1595
930 $MIN_INTERVAL = $self->x_resource ("%.interval"); 1596 $MIN_INTERVAL = $self->x_resource ("%.interval");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines