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.38 by root, Fri Jun 8 21:48:07 2012 UTC vs.
Revision 1.108 by root, Tue Nov 29 05:45:43 2022 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: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
16#:META:OSC:20:change/query background image
17#:META:OSC:705:change transparent background tint colour
5 18
6#TODO: once, rootalign 19=head1 NAME
7 20
8=head1 background - manage terminal background 21background - manage terminal background
9 22
10=head2 SYNOPSIS 23=head1 SYNOPSIS
11 24
12 urxvt --background-expr 'background expression' 25 urxvt --background-expr 'background expression'
13 --background-border 26 --background-border
27 --background-interval seconds
14 28
29=head1 QUICK AND DIRTY CHEAT SHEET
30
31Load a random jpeg image and tile the background with it without scaling
32or anything else:
33
34 load "/path/to/img.jpg"
35
36The same, but use mirroring/reflection instead of tiling:
37
38 mirror load "/path/to/img.jpg"
39
40Load an image and scale it to exactly fill the terminal window:
41
42 scale keep { load "/path/to/img.jpg" }
43
44Implement pseudo-transparency by using a suitably-aligned root pixmap
45as window background:
46
47 rootalign root
48
49Likewise, but keep a blurred copy:
50
51 rootalign keep { blur 10, root }
52
15=head2 DESCRIPTION 53=head1 DESCRIPTION
16 54
17This extension manages the terminal background by creating a picture that 55This extension manages the terminal background by creating a picture that
18is behind the text, replacing the normal background colour. 56is behind the text, replacing the normal background colour.
19 57
20It does so by evaluating a Perl expression that I<calculates> the image on 58It does so by evaluating a Perl expression that I<calculates> the image on
21the fly, for example, by grabbing the root background or loading a file. 59the fly, for example, by grabbing the root background or loading a file.
22 60
23While the full power of Perl is available, the operators have been design 61While the full power of Perl is available, the operators have been
24to be as simple as possible. 62designed to be as simple as possible.
25 63
26For example, to load an image and scale it to the window size, you would 64For example, to load an image and scale it to the window size, you would
27use: 65use:
28 66
29 urxvt --background-expr 'scale load "/path/to/mybg.png"' 67 urxvt --background-expr 'scale keep { load "/path/to/mybg.png" }'
30 68
31Or specified as a X resource: 69Or specified as a X resource:
32 70
33 URxvt.background-expr: scale load "/path/to/mybg.png" 71 URxvt.background.expr: scale keep { load "/path/to/mybg.png" }
34 72
35=head2 THEORY OF OPERATION 73=head1 THEORY OF OPERATION
36 74
37At startup, just before the window is mapped for the first time, the 75At startup, just before the window is mapped for the first time, the
38expression is evaluated and must yield an image. The image is then 76expression is evaluated and must yield an image. The image is then
39extended as necessary to cover the whole terminal window, and is set as a 77extended as necessary to cover the whole terminal window, and is set as a
40background pixmap. 78background pixmap.
51If any of the parameters that the expression relies on changes (when the 89If any of the parameters that the expression relies on changes (when the
52window is moved or resized, its position or size changes; when the root 90window is moved or resized, its position or size changes; when the root
53pixmap is replaced by another one the root background changes; or when the 91pixmap is replaced by another one the root background changes; or when the
54timer elapses), then the expression will be evaluated again. 92timer elapses), then the expression will be evaluated again.
55 93
56For example, an expression such as C<scale load "$HOME/mybg.png"> scales the 94For example, an expression such as C<scale keep { load "$HOME/mybg.png"
57image to the window size, so it relies on the window size and will 95}> scales the image to the window size, so it relies on the window size
58be reevaluated each time it is changed, but not when it moves for 96and will be reevaluated each time it is changed, but not when it moves for
59example. That ensures that the picture always fills the terminal, even 97example. That ensures that the picture always fills the terminal, even
60after it's size changes. 98after its size changes.
61 99
62=head3 EXPRESSIONS 100=head2 EXPRESSIONS
63 101
64Expressions are normal Perl expressions, in fact, they are Perl blocks - 102Expressions are normal Perl expressions, in fact, they are Perl blocks -
65which means you could use multiple lines and statements: 103which means you could use multiple lines and statements:
66 104
105 scale keep {
67 again 3600; 106 again 3600;
68 if (localtime now)[6]) { 107 if (localtime now)[6]) {
69 return scale load "$HOME/weekday.png"; 108 return load "$HOME/weekday.png";
70 } else { 109 } else {
71 return scale load "$HOME/sunday.png"; 110 return load "$HOME/sunday.png";
111 }
72 } 112 }
73 113
74This expression gets evaluated once per hour. It will set F<sunday.png> as 114This inner expression is evaluated once per hour (and whenever the
115terminal window is resized). It sets F<sunday.png> as background on
75background on sundays, and F<weekday.png> on all other days. 116Sundays, and F<weekday.png> on all other days.
76 117
77Fortunately, we expect that most expressions will be much simpler, with 118Fortunately, we expect that most expressions will be much simpler, with
78little Perl knowledge needed. 119little Perl knowledge needed.
79 120
80Basically, you always start with a function that "generates" an image 121Basically, you always start with a function that "generates" an image
97its result becomes the argument to the C<scale> function. 138its result becomes the argument to the C<scale> function.
98 139
99Many operators also allow some parameters preceding the input image 140Many operators also allow some parameters preceding the input image
100that modify its behaviour. For example, C<scale> without any additional 141that modify its behaviour. For example, C<scale> without any additional
101arguments scales the image to size of the terminal window. If you specify 142arguments scales the image to size of the terminal window. If you specify
102an additional argument, it uses it as a percentage: 143an additional argument, it uses it as a scale factor (multiply by 100 to
144get a percentage):
103 145
104 scale 200, load "$HOME/mypic.png" 146 scale 2, load "$HOME/mypic.png"
105 147
106This enlarges the image by a factor of 2 (200%). As you can see, C<scale> 148This enlarges the image by a factor of 2 (200%). As you can see, C<scale>
107has now two arguments, the C<200> and the C<load> expression, while 149has now two arguments, the C<2> and the C<load> expression, while
108C<load> only has one argument. Arguments are separated from each other by 150C<load> only has one argument. Arguments are separated from each other by
109commas. 151commas.
110 152
111Scale also accepts two arguments, which are then separate factors for both 153Scale also accepts two arguments, which are then separate factors for both
112horizontal and vertical dimensions. For example, this halves the image 154horizontal and vertical dimensions. For example, this halves the image
113width and doubles the image height: 155width and doubles the image height:
114 156
115 scale 50, 200, load "$HOME/mypic.png" 157 scale 0.5, 2, load "$HOME/mypic.png"
116 158
117TODO 159IF you try out these expressions, you might suffer from some sluggishness,
160because each time the terminal is resized, it loads the PNG image again
161and scales it. Scaling is usually fast (and unavoidable), but loading the
162image can be quite time consuming. This is where C<keep> comes in handy:
118 163
119=head3 CYCLES AND CACHING 164 scale 0.5, 2, keep { load "$HOME/mypic.png" }
120 165
121TODO 166The C<keep> operator executes all the statements inside the braces only
167once, or when it thinks the outcome might change. In other cases it
168returns the last value computed by the brace block.
122 169
123Each time the expression is reevaluated, a new cycle is said to have begun. Many operators 170This means that the C<load> is only executed once, which makes it much
124cache their results till the next cycle. For example 171faster, but also means that more memory is being used, because the loaded
172image must be kept in memory at all times. In this expression, the
173trade-off is likely worth it.
125 174
175But back to effects: Other effects than scaling are also readily
176available, for example, you can tile the image to fill the whole window,
177instead of resizing it:
178
179 tile keep { load "$HOME/mypic.png" }
180
181In fact, images returned by C<load> are in C<tile> mode by default, so the
182C<tile> operator is kind of superfluous.
183
184Another common effect is to mirror the image, so that the same edges
185touch:
186
187 mirror keep { load "$HOME/mypic.png" }
188
189Another common background expression is:
190
191 rootalign root
192
193This one first takes a snapshot of the screen background image, and then
194moves it to the upper left corner of the screen (as opposed to the upper
195left corner of the terminal window)- the result is pseudo-transparency:
196the image seems to be static while the window is moved around.
197
198=head2 COLOUR SPECIFICATIONS
199
200Whenever an operator expects a "colour", then this can be specified in one
201of two ways: Either as string with an X11 colour specification, such as:
202
203 "red" # named colour
204 "#f00" # simple rgb
205 "[50]red" # red with 50% alpha
206 "TekHVC:300/50/50" # anything goes
207
208OR as an array reference with one, three or four components:
209
210 [0.5] # 50% gray, 100% alpha
211 [0.5, 0, 0] # dark red, no green or blur, 100% alpha
212 [0.5, 0, 0, 0.7] # same with explicit 70% alpha
213
214=head2 CACHING AND SENSITIVITY
215
216Since some operations (such as C<load> and C<blur>) can take a long time,
217caching results can be very important for a smooth operation. Caching can
218also be useful to reduce memory usage, though, for example, when an image
219is cached by C<load>, it could be shared by multiple terminal windows
220running inside urxvtd.
221
222=head3 C<keep { ... }> caching
223
224The most important way to cache expensive operations is to use C<keep {
225... }>. The C<keep> operator takes a block of multiple statements enclosed
226by C<{}> and keeps the return value in memory.
227
228An expression can be "sensitive" to various external events, such as
229scaling or moving the window, root background changes and timers. Simply
230using an expression (such as C<scale> without parameters) that depends on
231certain changing values (called "variables"), or using those variables
232directly, will make an expression sensitive to these events - for example,
233using C<scale> or C<TW> will make the expression sensitive to the terminal
234size, and thus to resizing events.
235
236When such an event happens, C<keep> will automatically trigger a
237reevaluation of the whole expression with the new value of the expression.
238
239C<keep> is most useful for expensive operations, such as C<blur>:
240
241 rootalign keep { blur 20, root }
242
243This makes a blurred copy of the root background once, and on subsequent
244calls, just root-aligns it. Since C<blur> is usually quite slow and
245C<rootalign> is quite fast, this trades extra memory (for the cached
246blurred pixmap) with speed (blur only needs to be redone when root
247changes).
248
249=head3 C<load> caching
250
251The C<load> operator itself does not keep images in memory, but as long as
252the image is still in memory, C<load> will use the in-memory image instead
253of loading it freshly from disk.
254
255That means that this expression:
256
257 keep { load "$HOME/path..." }
258
259Not only caches the image in memory, other terminal instances that try to
260C<load> it can reuse that in-memory copy.
261
126=head2 REFERENCE 262=head1 REFERENCE
127 263
128=head3 COMMAND LINE SWITCHES 264=head2 COMMAND LINE SWITCHES
129 265
130=over 4 266=over
131 267
132=item --background-expr perl-expression 268=item --background-expr perl-expression
133 269
134Specifies the Perl expression to evaluate. 270Specifies the Perl expression to evaluate.
135 271
139overwriting borders and any other areas, such as the scrollbar. 275overwriting borders and any other areas, such as the scrollbar.
140 276
141Specifying this flag changes the behaviour, so that the image only 277Specifying this flag changes the behaviour, so that the image only
142replaces the background of the character area. 278replaces the background of the character area.
143 279
280=item --background-interval seconds
281
282Since some operations in the underlying XRender extension can effectively
283freeze your X-server for prolonged time, this extension enforces a minimum
284time between updates, which is normally about 0.1 seconds.
285
286If you want to do updates more often, you can decrease this safety
287interval with this switch.
288
144=back 289=back
145 290
146=cut 291=cut
147 292
148our $EXPR;#d# 293our %_IMG_CACHE;
149#$EXPR = 'move W * 0.1, -H * 0.1, resize W * 0.5, H * 0.5, repeat_none load "opensource.png"';
150$EXPR = 'move -TX, -TY, load "argb.png"';
151#$EXPR = '
152# rotate W, H, 50, 50, counter 1/59.95, repeat_mirror,
153# clip X, Y, W, H, repeat_mirror,
154# load "/root/pix/das_fette_schwein.jpg"
155#';
156#$EXPR = 'solid "red"';
157#$EXPR = 'blur root, 10, 10'
158#$EXPR = 'blur move (root, -x, -y), 5, 5'
159#resize load "/root/pix/das_fette_schwein.jpg", w, h
160
161our $HOME; 294our $HOME;
162our ($self, $old, $new); 295our ($self, $frame);
163our ($x, $y, $w, $h); 296our ($x, $y, $w, $h, $focus);
164 297
165# enforce at least this interval between updates 298# enforce at least this interval between updates
166our $MIN_INTERVAL = 1/100; 299our $MIN_INTERVAL = 6/59.951;
167 300
168{ 301{
169 package urxvt::bgdsl; # background language 302 package urxvt::bgdsl; # background language
303
304 sub FR_PARENT() { 0 } # parent frame, if any - must be #0
305 sub FR_CACHE () { 1 } # cached values
306 sub FR_AGAIN () { 2 } # what this expr is sensitive to
307 sub FR_STATE () { 3 } # watchers etc.
308
309 use List::Util qw(min max sum shuffle);
170 310
171=head2 PROVIDERS/GENERATORS 311=head2 PROVIDERS/GENERATORS
172 312
173These functions provide an image, by loading it from disk, grabbing it 313These functions provide an image, by loading it from disk, grabbing it
174from the root screen or by simply generating it. They are used as starting 314from the root screen or by simply generating it. They are used as starting
175points to get an image you can play with. 315points to get an image you can play with.
176 316
177=over 4 317=over
178 318
179=item load $path 319=item load $path
180 320
181Loads the image at the given C<$path>. The image is set to plane tiling 321Loads the image at the given C<$path>. The image is set to plane tiling
182mode. 322mode.
183 323
184Loaded images will be cached for one cycle. 324If the image is already in memory (e.g. because another terminal instance
325uses it), then the in-memory copy is returned instead.
185 326
327=item load_uc $path
328
329Load uncached - same as load, but does not cache the image, which means it
330is I<always> loaded from the filesystem again, even if another copy of it
331is in memory at the time.
332
186=cut 333=cut
334
335 sub load_uc($) {
336 $self->new_img_from_file ($_[0])
337 }
187 338
188 sub load($) { 339 sub load($) {
189 my ($path) = @_; 340 my ($path) = @_;
190 341
191 $new->{load}{$path} = $old->{load}{$path} || $self->new_img_from_file ($path); 342 $_IMG_CACHE{$path} || do {
343 my $img = load_uc $path;
344 Scalar::Util::weaken ($_IMG_CACHE{$path} = $img);
345 $img
346 }
192 } 347 }
193 348
194=item root 349=item root
195 350
196Returns the root window pixmap, that is, hopefully, the background image 351Returns the root window pixmap, that is, hopefully, the background image
197of your screen. The image is set to extend mode. 352of your screen.
198 353
199This function makes your expression root sensitive, that means it will be 354This function makes your expression root sensitive, that means it will be
200reevaluated when the bg image changes. 355reevaluated when the bg image changes.
201 356
202=cut 357=cut
203 358
204 sub root() { 359 sub root() {
205 $new->{rootpmap_sensitive} = 1; 360 $frame->[FR_AGAIN]{rootpmap} = 1;
206 die "root op not supported, exg, we need you"; 361 $self->new_img_from_root
207 } 362 }
208 363
209=item solid $colour 364=item solid $colour
210 365
211=item solid $width, $height, $colour 366=item solid $width, $height, $colour
212 367
213Creates a new image and completely fills it with the given colour. The 368Creates a new image and completely fills it with the given colour. The
214image is set to tiling mode. 369image is set to tiling mode.
215 370
216If <$width> and C<$height> are omitted, it creates a 1x1 image, which is 371If C<$width> and C<$height> are omitted, it creates a 1x1 image, which is
217useful for solid backgrounds or for use in filtering effects. 372useful for solid backgrounds or for use in filtering effects.
218 373
219=cut 374=cut
220 375
221 sub solid($$;$) { 376 sub solid($;$$) {
222 my $colour = pop; 377 my $colour = pop;
223 378
224 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); 379 my $img = $self->new_img (urxvt::PictStandardARGB32, 0, 0, $_[0] || 1, $_[1] || 1);
225 $img->fill ($colour); 380 $img->fill ($colour);
226 $img 381 $img
227 } 382 }
228 383
229=back 384=item clone $img
230 385
231=head2 VARIABLES 386Returns an exact copy of the image. This is useful if you want to have
387multiple copies of the same image to apply different effects to.
232 388
233The following functions provide variable data such as the terminal
234window dimensions. Most of them make your expression sensitive to some
235events, for example using C<TW> (terminal width) means your expression is
236evaluated again when the terminal is resized.
237
238=over 4
239
240=item TX
241
242=item TY
243
244Return the X and Y coordinates of the terminal window (the terminal
245window is the full window by default, and the character area only when in
246border-respect mode).
247
248Using these functions make your expression sensitive to window moves.
249
250These functions are mainly useful to align images to the root window.
251
252Example: load an image and align it so it looks as if anchored to the
253background.
254
255 move -TX, -TY, load "mybg.png"
256
257=item TW
258
259Return the width (C<TW>) and height (C<TH>) of the terminal window (the
260terminal window is the full window by default, and the character area only
261when in border-respect mode).
262
263Using these functions make your expression sensitive to window resizes.
264
265These functions are mainly useful to scale images, or to clip images to
266the window size to conserve memory.
267
268Example: take the screen background, clip it to the window size, blur it a
269bit, align it to the window position and use it as background.
270
271 clip move -TX, -TY, blur 5, root
272
273=cut 389=cut
274 390
275 sub TX() { $new->{position_sensitive} = 1; $x }
276 sub TY() { $new->{position_sensitive} = 1; $y }
277 sub TW() { $new->{size_sensitive} = 1; $w }
278 sub TH() { $new->{size_sensitive} = 1; $h }
279
280=item now
281
282Returns the current time as (fractional) seconds since the epoch.
283
284Using this expression does I<not> make your expression sensitive to time,
285but the next two functions do.
286
287=item again $seconds
288
289When this function is used the expression will be reevaluated again in
290C<$seconds> seconds.
291
292Example: load some image and rotate it according to the time of day (as if it were
293the hour pointer of a clock). Update this image every minute.
294
295 again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png"
296
297=item counter $seconds
298
299Like C<again>, but also returns an increasing counter value, starting at
3000, which might be useful for some simple animation effects.
301
302=cut
303
304 sub now() { urxvt::NOW }
305
306 sub again($) {
307 $new->{again} = $_[0];
308 }
309
310 sub counter($) { 391 sub clone($) {
311 $new->{again} = $_[0]; 392 $_[0]->clone
312 $self->{counter} + 0 393 }
394
395=item merge $img ...
396
397Takes any number of images and merges them together, creating a single
398image containing them all. The tiling mode of the first image is used as
399the tiling mode of the resulting image.
400
401This function is called automatically when an expression returns multiple
402images.
403
404=cut
405
406 sub merge(@) {
407 return $_[0] unless $#_;
408
409 # rather annoyingly clumsy, but optimisation is for another time
410
411 my $x0 = +1e9;
412 my $y0 = +1e9;
413 my $x1 = -1e9;
414 my $y1 = -1e9;
415
416 for (@_) {
417 my ($x, $y, $w, $h) = $_->geometry;
418
419 $x0 = $x if $x0 > $x;
420 $y0 = $y if $y0 > $y;
421
422 $x += $w;
423 $y += $h;
424
425 $x1 = $x if $x1 < $x;
426 $y1 = $y if $y1 < $y;
427 }
428
429 my $base = $self->new_img (urxvt::PictStandardARGB32, $x0, $y0, $x1 - $x0, $y1 - $y0);
430 $base->repeat_mode ($_[0]->repeat_mode);
431 $base->fill ([0, 0, 0, 0]);
432
433 $base->draw ($_)
434 for @_;
435
436 $base
313 } 437 }
314 438
315=back 439=back
316 440
317=head2 TILING MODES 441=head2 TILING MODES
318 442
319The following operators modify the tiling mode of an image, that is, the 443The following operators modify the tiling mode of an image, that is, the
320way that pixels outside the image area are painted when the image is used. 444way that pixels outside the image area are painted when the image is used.
321 445
322=over 4 446=over
323 447
324=item tile $img 448=item tile $img
325 449
326Tiles the whole plane with the image and returns this new image - or in 450Tiles the whole plane with the image and returns this new image - or in
327other words, it returns a copy of the image in plane tiling mode. 451other words, it returns a copy of the image in plane tiling mode.
350become transparent. This mode is most useful when you want to place an 474become transparent. This mode is most useful when you want to place an
351image over another image or the background colour while leaving all 475image over another image or the background colour while leaving all
352background pixels outside the image unchanged. 476background pixels outside the image unchanged.
353 477
354Example: load an image and display it in the upper left corner. The rest 478Example: load an image and display it in the upper left corner. The rest
355of the space is left "empty" (transparent or wahtever your compisotr does 479of the space is left "empty" (transparent or whatever your compositor does
356in alpha mode, else background colour). 480in alpha mode, else background colour).
357 481
358 pad load "mybg.png" 482 pad load "mybg.png"
359 483
360=item extend $img 484=item extend $img
361 485
362Extends the image over the whole plane, using the closest pixel in the 486Extends the image over the whole plane, using the closest pixel in the
363area outside the image. This mode is mostly useful when you more complex 487area outside the image. This mode is mostly useful when you use more complex
364filtering operations and want the pixels outside the image to have the 488filtering operations and want the pixels outside the image to have the
365same values as the pixels near the edge. 489same values as the pixels near the edge.
366 490
367Example: just for curiosity, how does this pixel extension stuff work? 491Example: just for curiosity, how does this pixel extension stuff work?
368 492
394 $img 518 $img
395 } 519 }
396 520
397=back 521=back
398 522
399=head2 PIXEL OPERATORS 523=head2 VARIABLE VALUES
400 524
401The following operators modify the image pixels in various ways. 525The following functions provide variable data such as the terminal window
526dimensions. They are not (Perl-) variables, they just return stuff that
527varies. Most of them make your expression sensitive to some events, for
528example using C<TW> (terminal width) means your expression is evaluated
529again when the terminal is resized.
402 530
403=over 4 531=over
404 532
405=item clone $img 533=item TX
406 534
407Returns an exact copy of the image. 535=item TY
408 536
409=cut 537Return the X and Y coordinates of the terminal window (the terminal
538window is the full window by default, and the character area only when in
539border-respect mode).
410 540
541Using these functions makes your expression sensitive to window moves.
542
543These functions are mainly useful to align images to the root window.
544
545Example: load an image and align it so it looks as if anchored to the
546background (that's exactly what C<rootalign> does btw.):
547
548 move -TX, -TY, keep { load "mybg.png" }
549
550=item TW
551
552=item TH
553
554Return the width (C<TW>) and height (C<TH>) of the terminal window (the
555terminal window is the full window by default, and the character area only
556when in border-respect mode).
557
558Using these functions makes your expression sensitive to window resizes.
559
560These functions are mainly useful to scale images, or to clip images to
561the window size to conserve memory.
562
563Example: take the screen background, clip it to the window size, blur it a
564bit, align it to the window position and use it as background.
565
566 clip move -TX, -TY, keep { blur 5, root }
567
568=item FOCUS
569
570Returns a boolean indicating whether the terminal window has keyboard
571focus, in which case it returns true.
572
573Using this function makes your expression sensitive to focus changes.
574
575A common use case is to fade the background image when the terminal loses
576focus, often together with the C<-fade> command line option. In fact,
577there is a special function for just that use case: C<focus_fade>.
578
579Example: use two entirely different background images, depending on
580whether the window has focus.
581
582 FOCUS ? keep { load "has_focus.jpg" } : keep { load "no_focus.jpg" }
583
584=cut
585
586 sub TX () { $frame->[FR_AGAIN]{position} = 1; $x }
587 sub TY () { $frame->[FR_AGAIN]{position} = 1; $y }
588 sub TW () { $frame->[FR_AGAIN]{size} = 1; $w }
589 sub TH () { $frame->[FR_AGAIN]{size} = 1; $h }
590 sub FOCUS() { $frame->[FR_AGAIN]{focus} = 1; $focus }
591
592=item now
593
594Returns the current time as (fractional) seconds since the epoch.
595
596Using this expression does I<not> make your expression sensitive to time,
597but the next two functions do.
598
599=item again $seconds
600
601When this function is used the expression will be reevaluated again in
602C<$seconds> seconds.
603
604Example: load some image and rotate it according to the time of day (as if it were
605the hour pointer of a clock). Update this image every minute.
606
607 again 60;
608 rotate 50, 50, (now % 86400) * -72 / 8640, scale keep { load "myclock.png" }
609
610=item counter $seconds
611
612Like C<again>, but also returns an increasing counter value, starting at
6130, which might be useful for some simple animation effects.
614
615=cut
616
617 sub now() { urxvt::NOW }
618
619 sub again($) {
620 $frame->[FR_AGAIN]{time} = $_[0];
621 }
622
411 sub clone($) { 623 sub counter($) {
412 $_[0]->clone 624 $frame->[FR_AGAIN]{time} = $_[0];
625 $frame->[FR_STATE]{counter} + 0
413 } 626 }
627
628=back
629
630=head2 SHAPE CHANGING OPERATORS
631
632The following operators modify the shape, size or position of the image.
633
634=over
414 635
415=item clip $img 636=item clip $img
416 637
417=item clip $width, $height, $img 638=item clip $width, $height, $img
418 639
421Clips an image to the given rectangle. If the rectangle is outside the 642Clips an image to the given rectangle. If the rectangle is outside the
422image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is 643image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is
423larger than the image, then the tiling mode defines how the extra pixels 644larger than the image, then the tiling mode defines how the extra pixels
424will be filled. 645will be filled.
425 646
426If C<$x> an C<$y> are missing, then C<0> is assumed for both. 647If C<$x> and C<$y> are missing, then C<0> is assumed for both.
427 648
428If C<$width> and C<$height> are missing, then the window size will be 649If C<$width> and C<$height> are missing, then the window size will be
429assumed. 650assumed.
430 651
431Example: load an image, blur it, and clip it to the window size to save 652Example: load an image, blur it, and clip it to the window size to save
432memory. 653memory.
433 654
434 clip blur 10, load "mybg.png" 655 clip keep { blur 10, load "mybg.png" }
435 656
436=cut 657=cut
437 658
438 sub clip($;$$;$$) { 659 sub clip($;$$;$$) {
439 my $img = pop; 660 my $img = pop;
442 $img->sub_rect ($_[0], $_[1], $w, $h) 663 $img->sub_rect ($_[0], $_[1], $w, $h)
443 } 664 }
444 665
445=item scale $img 666=item scale $img
446 667
447=item scale $size_percent, $img 668=item scale $size_factor, $img
448 669
449=item scale $width_percent, $height_percent, $img 670=item scale $width_factor, $height_factor, $img
450 671
451Scales the image by the given percentages in horizontal 672Scales the image by the given factors in horizontal
452(C<$width_percent>) and vertical (C<$height_percent>) direction. 673(C<$width>) and vertical (C<$height>) direction.
453 674
454If only one percentage is give, it is used for both directions. 675If only one factor is given, it is used for both directions.
455 676
456If no percentages are given, scales the image to the window size without 677If no factors are given, scales the image to the window size without
457keeping aspect. 678keeping aspect.
458 679
459=item resize $width, $height, $img 680=item resize $width, $height, $img
460 681
461Resizes the image to exactly C<$width> times C<$height> pixels. 682Resizes the image to exactly C<$width> times C<$height> pixels.
462 683
463=cut 684=item fit $img
464 685
465#TODO: maximise, maximise_fill? 686=item fit $width, $height, $img
687
688Fits the image into the given C<$width> and C<$height> without changing
689aspect, or the terminal size. That means it will be shrunk or grown until
690the whole image fits into the given area, possibly leaving borders.
691
692=item cover $img
693
694=item cover $width, $height, $img
695
696Similar to C<fit>, but shrinks or grows until all of the area is covered
697by the image, so instead of potentially leaving borders, it will cut off
698image data that doesn't fit.
699
700=cut
466 701
467 sub scale($;$;$) { 702 sub scale($;$;$) {
468 my $img = pop; 703 my $img = pop;
469 704
470 @_ == 2 ? $img->scale ($_[0] * $img->w * 0.01, $_[1] * $img->h * 0.01) 705 @_ == 2 ? $img->scale ($_[0] * $img->w, $_[1] * $img->h)
471 : @_ ? $img->scale ($_[0] * $img->w * 0.01, $_[0] * $img->h * 0.01) 706 : @_ ? $img->scale ($_[0] * $img->w, $_[0] * $img->h)
472 : $img->scale (TW, TH) 707 : $img->scale (TW, TH)
473 } 708 }
474 709
475 sub resize($$$) { 710 sub resize($$$) {
476 my $img = pop; 711 my $img = pop;
477 $img->scale ($_[0], $_[1]) 712 $img->scale ($_[0], $_[1])
478 } 713 }
479 714
715 sub fit($;$$) {
716 my $img = pop;
717 my $w = ($_[0] || TW) / $img->w;
718 my $h = ($_[1] || TH) / $img->h;
719 scale +(min $w, $h), $img
720 }
721
722 sub cover($;$$) {
723 my $img = pop;
724 my $w = ($_[0] || TW) / $img->w;
725 my $h = ($_[1] || TH) / $img->h;
726 scale +(max $w, $h), $img
727 }
728
480=item move $dx, $dy, $img 729=item move $dx, $dy, $img
481 730
482Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in 731Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in
483the vertical. 732the vertical.
484 733
485Example: move the image right by 20 pixels and down by 30. 734Example: move the image right by 20 pixels and down by 30.
486 735
487 move 20, 30, ... 736 move 20, 30, ...
737
738=item align $xalign, $yalign, $img
739
740Aligns the image according to a factor - C<0> means the image is moved to
741the left or top edge (for C<$xalign> or C<$yalign>), C<0.5> means it is
742exactly centered and C<1> means it touches the right or bottom edge.
743
744Example: remove any visible border around an image, center it vertically but move
745it to the right hand side.
746
747 align 1, 0.5, pad $img
748
749=item center $img
750
751=item center $width, $height, $img
752
753Centers the image, i.e. the center of the image is moved to the center of
754the terminal window (or the box specified by C<$width> and C<$height> if
755given).
756
757Example: load an image and center it.
758
759 center keep { pad load "mybg.png" }
488 760
489=item rootalign $img 761=item rootalign $img
490 762
491Moves the image so that it appears glued to the screen as opposed to the 763Moves the image so that it appears glued to the screen as opposed to the
492window. This gives the illusion of a larger area behind the window. It is 764window. This gives the illusion of a larger area behind the window. It is
493exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the 765exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the
494top left of the screen. 766top left of the screen.
495 767
496Example: load a background image, put it in mirror mode and root align it. 768Example: load a background image, put it in mirror mode and root align it.
497 769
498 rootalign mirror load "mybg.png" 770 rootalign keep { mirror load "mybg.png" }
499 771
500Example: take the screen background and align it, giving the illusion of 772Example: take the screen background and align it, giving the illusion of
501transparency as long as the window isn't in front of other windows. 773transparency as long as the window isn't in front of other windows.
502 774
503 rootalign root 775 rootalign root
504 776
505=cut 777=cut
506 778
507 sub move($$;$) { 779 sub move($$;$) {
508 my $img = pop->clone; 780 my $img = pop->clone;
509 $img->move ($_[0], $_[1]); 781 $img->move ($_[0], $_[1]);
510 $img 782 $img
511 } 783 }
512 784
785 sub align($;$$) {
786 my $img = pop;
787
788 move $_[0] * (TW - $img->w),
789 $_[1] * (TH - $img->h),
790 $img
791 }
792
793 sub center($;$$) {
794 my $img = pop;
795 my $w = $_[0] || TW;
796 my $h = $_[1] || TH;
797
798 move 0.5 * ($w - $img->w), 0.5 * ($h - $img->h), $img
799 }
800
513 sub rootalign($) { 801 sub rootalign($) {
514 move -TX, -TY, $_[0] 802 move -TX, -TY, $_[0]
515 } 803 }
516 804
805=item rotate $center_x, $center_y, $degrees, $img
806
807Rotates the image clockwise by C<$degrees> degrees, around the point at
808C<$center_x> and C<$center_y> (specified as factor of image width/height).
809
810Example: rotate the image by 90 degrees around its center.
811
812 rotate 0.5, 0.5, 90, keep { load "$HOME/mybg.png" }
813
814=cut
815
816 sub rotate($$$$) {
817 my $img = pop;
818 $img->rotate (
819 $_[0] * ($img->w + $img->x),
820 $_[1] * ($img->h + $img->y),
821 $_[2] * (3.14159265 / 180),
822 )
823 }
824
825=back
826
827=head2 COLOUR MODIFICATIONS
828
829The following operators change the pixels of the image.
830
831=over
832
833=item tint $color, $img
834
835Tints the image in the given colour.
836
837Example: tint the image red.
838
839 tint "red", load "rgb.png"
840
841Example: the same, but specify the colour by component.
842
843 tint [1, 0, 0], load "rgb.png"
844
845=cut
846
847 sub tint($$) {
848 $_[1]->tint ($_[0])
849 }
850
851=item shade $factor, $img
852
853Shade the image by the given factor.
854
855=cut
856
857 sub shade($$) {
858 $_[1]->shade ($_[0])
859 }
860
517=item contrast $factor, $img 861=item contrast $factor, $img
518 862
519=item contrast $r, $g, $b, $img 863=item contrast $r, $g, $b, $img
520 864
521=item contrast $r, $g, $b, $a, $img 865=item contrast $r, $g, $b, $a, $img
522 866
523Adjusts the I<contrast> of an image. 867Adjusts the I<contrast> of an image.
524 868
525#TODO# 869The first form applies a single C<$factor> to red, green and blue, the
870second form applies separate factors to each colour channel, and the last
871form includes the alpha channel.
526 872
873Values from 0 to 1 lower the contrast, values higher than 1 increase the
874contrast.
875
876Due to limitations in the underlying XRender extension, lowering contrast
877also reduces brightness, while increasing contrast currently also
878increases brightness.
879
527=item brightness $factor, $img 880=item brightness $bias, $img
528 881
529=item brightness $r, $g, $b, $img 882=item brightness $r, $g, $b, $img
530 883
531=item brightness $r, $g, $b, $a, $img 884=item brightness $r, $g, $b, $a, $img
532 885
533Adjusts the brightness of an image. 886Adjusts the brightness of an image.
887
888The first form applies a single C<$bias> to red, green and blue, the
889second form applies separate biases to each colour channel, and the last
890form includes the alpha channel.
891
892Values less than 0 reduce brightness, while values larger than 0 increase
893it. Useful range is from -1 to 1 - the former results in a black, the
894latter in a white picture.
895
896Due to idiosyncrasies in the underlying XRender extension, biases less
897than zero can be I<very> slow.
898
899You can also try the experimental(!) C<muladd> operator.
534 900
535=cut 901=cut
536 902
537 sub contrast($$;$$;$) { 903 sub contrast($$;$$;$) {
538 my $img = pop; 904 my $img = pop;
539 my ($r, $g, $b, $a) = @_; 905 my ($r, $g, $b, $a) = @_;
540 906
541 ($g, $b) = ($r, $r) if @_ < 4; 907 ($g, $b) = ($r, $r) if @_ < 3;
542 $a = 1 if @_ < 5; 908 $a = 1 if @_ < 4;
543 909
544 $img = $img->clone; 910 $img = $img->clone;
545 $img->contrast ($r, $g, $b, $a); 911 $img->contrast ($r, $g, $b, $a);
546 $img 912 $img
547 } 913 }
548 914
549 sub brightness($$;$$;$) { 915 sub brightness($$;$$;$) {
550 my $img = pop; 916 my $img = pop;
551 my ($r, $g, $b, $a) = @_; 917 my ($r, $g, $b, $a) = @_;
552 918
553 ($g, $b) = ($r, $r) if @_ < 4; 919 ($g, $b) = ($r, $r) if @_ < 3;
554 $a = 1 if @_ < 5; 920 $a = 1 if @_ < 4;
555 921
556 $img = $img->clone; 922 $img = $img->clone;
557 $img->brightness ($r, $g, $b, $a); 923 $img->brightness ($r, $g, $b, $a);
558 $img 924 $img
559 } 925 }
560 926
927=item muladd $mul, $add, $img # EXPERIMENTAL
928
929First multiplies the pixels by C<$mul>, then adds C<$add>. This can be used
930to implement brightness and contrast at the same time, with a wider value
931range than contrast and brightness operators.
932
933Due to numerous bugs in XRender implementations, it can also introduce a
934number of visual artifacts.
935
936Example: increase contrast by a factor of C<$c> without changing image
937brightness too much.
938
939 muladd $c, (1 - $c) * 0.5, $img
940
941=cut
942
943 sub muladd($$$) {
944 $_[2]->muladd ($_[0], $_[1])
945 }
946
561=item blur $radius, $img 947=item blur $radius, $img
562 948
563=item blur $radius_horz, $radius_vert, $img 949=item blur $radius_horz, $radius_vert, $img
564 950
565Gaussian-blurs the image with (roughly) C<$radius> pixel radius. The radii 951Gaussian-blurs the image with (roughly) C<$radius> pixel radius. The radii
566can also be specified separately. 952can also be specified separately.
953
954Blurring is often I<very> slow, at least compared or other
955operators. Larger blur radii are slower than smaller ones, too, so if you
956don't want to freeze your screen for long times, start experimenting with
957low values for radius (<5).
567 958
568=cut 959=cut
569 960
570 sub blur($$;$) { 961 sub blur($$;$) {
571 my $img = pop; 962 my $img = pop;
572 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) 963 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0])
573 } 964 }
574 965
575=item rotate $new_width, $new_height, $center_x, $center_y, $degrees 966=item focus_fade $img
576 967
577Rotates the image by C<$degrees> degrees, counter-clockwise, around the 968=item focus_fade $factor, $img
578pointer at C<$center_x> and C<$center_y> (specified as percentage of image
579width/height), generating a new image with width C<$new_width> and height
580C<$new_height>.
581 969
582#TODO# new width, height, maybe more operators? 970=item focus_fade $factor, $color, $img
583 971
584Example: rotate the image by 90 degrees 972Fades the image by the given factor (and colour) when focus is lost (the
973same as the C<-fade>/C<-fadecolor> command line options, which also supply
974the default values for C<factor> and C<$color>. Unlike with C<-fade>, the
975C<$factor> is a real value, not a percentage value (that is, 0..1, not
9760..100).
585 977
586=cut 978Example: do the right thing when focus fading is requested.
587 979
588 sub rotate($$$$$$) { 980 focus_fade load "mybg.jpg";
981
982=cut
983
984 sub focus_fade($;$$) {
589 my $img = pop; 985 my $img = pop;
590 $img->rotate ( 986
591 $_[0], 987 return $img
592 $_[1], 988 if FOCUS;
593 $_[2] * $img->w * .01, 989
594 $_[3] * $img->h * .01, 990 my $fade = @_ >= 1 ? $_[0] : defined $self->resource ("fade") ? $self->resource ("fade") * 0.01 : 0;
595 $_[4] * (3.14159265 / 180), 991 my $color = @_ >= 2 ? $_[1] : $self->resource ("color+" . &urxvt::Color_fade ()); # Color_fade not always available
596 ) 992
993 $img = $img->tint ($color) if $color ne "rgb:00/00/00";
994 $img = $img->muladd (1 - $fade, 0) if $fade;
995
996 $img
597 } 997 }
598 998
599=back 999=back
600 1000
1001=head2 OTHER STUFF
1002
1003Anything that didn't fit any of the other categories, even after applying
1004force and closing our eyes.
1005
1006=over
1007
1008=item keep { ... }
1009
1010This operator takes a code block as argument, that is, one or more
1011statements enclosed by braces.
1012
1013The trick is that this code block is only evaluated when the outcome
1014changes - on other calls the C<keep> simply returns the image it computed
1015previously (yes, it should only be used with images). Or in other words,
1016C<keep> I<caches> the result of the code block so it doesn't need to be
1017computed again.
1018
1019This can be extremely useful to avoid redoing slow operations - for
1020example, if your background expression takes the root background, blurs it
1021and then root-aligns it it would have to blur the root background on every
1022window move or resize.
1023
1024Another example is C<load>, which can be quite slow.
1025
1026In fact, urxvt itself encloses the whole expression in some kind of
1027C<keep> block so it only is reevaluated as required.
1028
1029Putting the blur into a C<keep> block will make sure the blur is only done
1030once, while the C<rootalign> is still done each time the window moves.
1031
1032 rootalign keep { blur 10, root }
1033
1034This leaves the question of how to force reevaluation of the block,
1035in case the root background changes: If expression inside the block
1036is sensitive to some event (root background changes, window geometry
1037changes), then it will be reevaluated automatically as needed.
1038
1039=back
1040
1041=head1 OLD BACKGROUND IMAGE SETTINGS
1042
1043This extension also provides support for the old options/resources and
1044OSC sequences for setting a background image. These settings are
1045B<deprecated> and will be removed in future versions.
1046
1047=head2 OPTIONS AND RESOURCES
1048
1049=over
1050
1051=item B<-pixmap> I<file[;oplist]>
1052
1053=item B<backgroundPixmap:> I<file[;oplist]>
1054
1055Use the specified image file as the window's background and also
1056optionally specify a colon separated list of operations to modify it.
1057Note that you may need to quote the C<;> character when using the
1058command line option, as C<;> is usually a metacharacter in shells.
1059Supported operations are:
1060
1061=over
1062
1063=item B<WxH+X+Y>
1064
1065sets scale and position. B<"W" / "H"> specify the horizontal/vertical
1066scale (percent), and B<"X" / "Y"> locate the image centre (percent). A
1067scale of 0 disables scaling.
1068
1069=item B<op=tile>
1070
1071enables tiling
1072
1073=item B<op=keep-aspect>
1074
1075maintain the image aspect ratio when scaling
1076
1077=item B<op=root-align>
1078
1079use the position of the terminal window relative to the root window as
1080the image offset, simulating a root window background
1081
1082=back
1083
1084The default scale and position setting is C<100x100+50+50>.
1085Alternatively, a predefined set of templates can be used to achieve
1086the most common setups:
1087
1088=over
1089
1090=item B<style=tiled>
1091
1092the image is tiled with no scaling. Equivalent to 0x0+0+0:op=tile
1093
1094=item B<style=aspect-stretched>
1095
1096the image is scaled to fill the whole window maintaining the aspect
1097ratio and centered. Equivalent to 100x100+50+50:op=keep-aspect
1098
1099=item B<style=stretched>
1100
1101the image is scaled to fill the whole window. Equivalent to 100x100
1102
1103=item B<style=centered>
1104
1105the image is centered with no scaling. Equivalent to 0x0+50+50
1106
1107=item B<style=root-tiled>
1108
1109the image is tiled with no scaling and using 'root' positioning.
1110Equivalent to 0x0:op=tile:op=root-align
1111
1112=back
1113
1114If multiple templates are specified the last one wins. Note that a
1115template overrides all the scale, position and operations settings.
1116
1117If used in conjunction with pseudo-transparency, the specified image
1118will be blended over the transparent background using alpha-blending.
1119
1120=item B<-tr>|B<+tr>
1121
1122=item B<transparent:> I<boolean>
1123
1124Turn on/off pseudo-transparency by using the root pixmap as background.
1125
1126=item B<-tint> I<colour>
1127
1128=item B<tintColor:> I<colour>
1129
1130Tint the transparent background with the given colour. Note that a
1131black tint yields a completely black image while a white tint yields
1132the image unchanged.
1133
1134=item B<-sh> I<number>
1135
1136=item B<shading:> I<number>
1137
1138Darken (0 .. 99) or lighten (101 .. 200) the transparent background.
1139A value of 100 means no shading.
1140
1141=item B<-blr> I<HxV>
1142
1143=item B<blurRadius:> I<HxV>
1144
1145Apply gaussian blur with the specified radius to the transparent
1146background. If a single number is specified, the vertical and
1147horizontal radii are considered to be the same. Setting one of the
1148radii to 1 and the other to a large number creates interesting effects
1149on some backgrounds. The maximum radius value is 128. An horizontal or
1150vertical radius of 0 disables blurring.
1151
1152=back
1153
1154=head2 OSC sequences
1155
1156This extension will react to the following OSC sequences. Note that
1157this extension will not be autoloaded when these are used currently,
1158so to make urxvt recognize them, you have to enable the C<background>
1159extension. One way to achieve that is to use the C<--background-expr ''>
1160command line argument or by specifying an empty C<URxvt.background.expr:>>
1161resource.
1162
1163=over
1164
1165=item B<< C<ESC ] 705 ; Pt ST> >> Change transparent background tint colour to B<< C<Pt> >>.
1166
1167=item B<< C<ESC ] 20 ; Pt ST> >> Change/Query background image
1168parameters: the value of B<< C<Pt> >> can be one of the following
1169commands:
1170
1171=over
1172
1173=item B<< C<?> >>
1174
1175display scale and position in the title
1176
1177=item B<< C<;WxH+X+Y> >>
1178
1179change scale and/or position
1180
1181=item B<< C<FILE;WxH+X+Y> >>
1182
1183change background image
1184
1185=back
1186
1187=cut
1188
1189 sub keep(&) {
1190 my $id = $_[0]+0;
1191
1192 local $frame = $self->{frame_cache}{$id} ||= [$frame];
1193
1194 unless ($frame->[FR_CACHE]) {
1195 $frame->[FR_CACHE] = [ $_[0]() ];
1196
1197 my $self = $self;
1198 my $frame = $frame;
1199 Scalar::Util::weaken $frame;
1200 $self->compile_frame ($frame, sub {
1201 # clear this frame cache, also for all parents
1202 for (my $frame = $frame; $frame; $frame = $frame->[0]) {
1203 undef $frame->[FR_CACHE];
1204 }
1205
1206 $self->recalculate;
1207 });
1208 };
1209
1210 # in scalar context we always return the first original result, which
1211 # is not quite how perl works.
1212 wantarray
1213 ? @{ $frame->[FR_CACHE] }
1214 : $frame->[FR_CACHE][0]
1215 }
1216
1217# sub keep_clear() {
1218# delete $self->{frame_cache};
1219# }
1220
1221=back
1222
601=cut 1223=cut
602 1224
603} 1225}
604 1226
605sub parse_expr { 1227sub parse_expr {
606 my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}"; 1228 my ($expr) = @_;
1229
1230 # an empty expression is valid and represents the default background
1231 if ($expr !~ /\S/) {
1232 $expr = sub {
1233 undef
1234 };
1235 } else {
1236 $expr = eval
1237 "sub {\n"
1238 . "package urxvt::bgdsl;\n"
1239 . "#line 0 'background expression'\n"
1240 . "$expr\n"
1241 . "}";
607 die if $@; 1242 die if $@;
1243 }
1244
608 $expr 1245 $expr
609} 1246}
610 1247
611# compiles a parsed expression 1248# compiles a parsed expression
612sub set_expr { 1249sub set_expr {
613 my ($self, $expr) = @_; 1250 my ($self, $expr) = @_;
614 1251
1252 $self->{root} = []; # the outermost frame
615 $self->{expr} = $expr; 1253 $self->{expr} = $expr;
616 $self->recalculate; 1254 $self->recalculate;
1255}
1256
1257# takes a hash of sensitivity indicators and installs watchers
1258sub compile_frame {
1259 my ($self, $frame, $cb) = @_;
1260
1261 my $state = $frame->[urxvt::bgdsl::FR_STATE] ||= {};
1262 my $again = $frame->[urxvt::bgdsl::FR_AGAIN];
1263
1264 # don't keep stuff alive
1265 Scalar::Util::weaken $state;
1266
1267 if ($again->{nested}) {
1268 $state->{nested} = 1;
1269 } else {
1270 delete $state->{nested};
1271 }
1272
1273 if (my $interval = $again->{time}) {
1274 $state->{time} = [$interval, urxvt::timer->new->after ($interval)->interval ($interval)]
1275 if $state->{time}[0] != $interval;
1276
1277 # callback *might* have changed, although we could just rule that out
1278 $state->{time}[1]->cb (sub {
1279 ++$state->{counter};
1280 $cb->();
1281 });
1282 } else {
1283 delete $state->{time};
1284 }
1285
1286 if ($again->{position}) {
1287 $state->{position} = $self->on (position_change => $cb);
1288 } else {
1289 delete $state->{position};
1290 }
1291
1292 if ($again->{size}) {
1293 $state->{size} = $self->on (size_change => $cb);
1294 } else {
1295 delete $state->{size};
1296 }
1297
1298 if ($again->{rootpmap}) {
1299 $state->{rootpmap} = $self->on (rootpmap_change => $cb);
1300 } else {
1301 delete $state->{rootpmap};
1302 }
1303
1304 if ($again->{focus}) {
1305 $state->{focus} = $self->on (focus_in => $cb, focus_out => $cb);
1306 } else {
1307 delete $state->{focus};
1308 }
617} 1309}
618 1310
619# evaluate the current bg expression 1311# evaluate the current bg expression
620sub recalculate { 1312sub recalculate {
621 my ($arg_self) = @_; 1313 my ($arg_self) = @_;
629 return; 1321 return;
630 } 1322 }
631 1323
632 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; 1324 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
633 1325
1326 unless ($arg_self->has_render) {
1327 warn "background extension needs RENDER extension 0.11 or higher, ignoring background-expr.\n";
1328 return;
1329 }
1330
634 # set environment to evaluate user expression 1331 # set environment to evaluate user expression
635 1332
636 local $self = $arg_self; 1333 local $self = $arg_self;
637
638 local $HOME = $ENV{HOME}; 1334 local $HOME = $ENV{HOME};
639 local $old = $self->{state}; 1335 local $frame = $self->{root};
640 local $new = my $state = $self->{state} = {};
641 1336
642 ($x, $y, $w, $h) =
643 $self->background_geometry ($self->{border}); 1337 ($x, $y, $w, $h) = $self->background_geometry ($self->{border});
1338 $focus = $self->focus;
644 1339
645 # evaluate user expression 1340 # evaluate user expression
646 1341
647 my $img = eval { $self->{expr}->() }; 1342 my @img = eval { $self->{expr}->() };
648 warn $@ if $@;#d# 1343 die $@ if $@;
1344 die "background-expr did not return anything.\n" unless @img;
1345
1346 if ($img[0]) {
1347 die "background-expr: expected image(s), got something else.\n"
649 die if !UNIVERSAL::isa $img, "urxvt::img"; 1348 if grep { !UNIVERSAL::isa $_, "urxvt::img" } @img;
650 1349
651 $state->{size_sensitive} = 1 1350 my $img = urxvt::bgdsl::merge @img;
1351
1352 $frame->[urxvt::bgdsl::FR_AGAIN]{size} = 1
652 if $img->repeat_mode != urxvt::RepeatNormal; 1353 if $img->repeat_mode != urxvt::RepeatNormal;
653 1354
654 # if the expression is sensitive to external events, prepare reevaluation then 1355 # if the expression is sensitive to external events, prepare reevaluation then
1356 $self->compile_frame ($frame, sub { $arg_self->recalculate });
655 1357
656 my $repeat; 1358 # clear stuff we no longer need
657 1359
658 if (my $again = $state->{again}) { 1360# unless (%{ $frame->[FR_STATE] }) {
659 $repeat = 1; 1361# delete $self->{state};
660 my $self = $self; 1362# delete $self->{expr};
661 $state->{timer} = $again == $old->{again}
662 ? $old->{timer}
663 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
664 ++$self->{counter};
665 $self->recalculate
666 });
667 } 1363# }
668 1364
669 if (delete $state->{position_sensitive}) { 1365 # set background pixmap
670 $repeat = 1; 1366
671 $self->enable (position_change => sub { $_[0]->recalculate }); 1367 $self->set_background ($img, $self->{border});
672 } else { 1368 } else {
673 $self->disable ("position_change"); 1369 $self->clr_background;
674 } 1370 }
675 1371
676 if (delete $state->{size_sensitive}) {
677 $repeat = 1;
678 $self->enable (size_change => sub { $_[0]->recalculate });
679 } else {
680 $self->disable ("size_change");
681 }
682
683 if (delete $state->{rootpmap_sensitive}) {
684 $repeat = 1;
685 $self->enable (rootpmap_change => sub { $_[0]->recalculate });
686 } else {
687 $self->disable ("rootpmap_change");
688 }
689
690 # clear stuff we no longer need
691
692 %$old = ();
693
694 unless ($repeat) {
695 delete $self->{state};
696 delete $self->{expr};
697 }
698
699 # set background pixmap
700
701 $self->set_background ($img, $self->{border});
702 $self->scr_recolour (0); 1372 $self->scr_recolor (0);
703 $self->want_refresh; 1373 $self->want_refresh;
704} 1374}
705 1375
1376sub old_bg_opts {
1377 my ($self, $arg) = @_;
1378
1379 $arg or return;
1380
1381 my @str = split /;/, $arg;
1382
1383 return unless $str[0] or $self->{bg_opts}->{path};
1384
1385 my $bg_opts = $self->{bg_opts};
1386
1387 if ($str[0]) {
1388 $bg_opts->{tile} = 0;
1389 $bg_opts->{keep_aspect} = 0;
1390 $bg_opts->{root_align} = 0;
1391 $bg_opts->{h_scale} = $bg_opts->{v_scale} = 100;
1392 $bg_opts->{h_align} = $bg_opts->{v_align} = 50;
1393 $bg_opts->{path} = $str[0];
1394 }
1395
1396 my @oplist = split /:/, $str[1];
1397
1398 for (@oplist) {
1399 if (/style=tiled/i) {
1400 $bg_opts->{tile} = 1;
1401 $bg_opts->{keep_aspect} = 0;
1402 $bg_opts->{root_align} = 0;
1403 $bg_opts->{h_scale} = $bg_opts->{v_scale} = 0;
1404 $bg_opts->{h_align} = $bg_opts->{v_align} = 0;
1405 } elsif (/style=aspect-stretched/i) {
1406 $bg_opts->{tile} = 0;
1407 $bg_opts->{keep_aspect} = 1;
1408 $bg_opts->{root_align} = 0;
1409 $bg_opts->{h_scale} = $bg_opts->{v_scale} = 100;
1410 $bg_opts->{h_align} = $bg_opts->{v_align} = 50;
1411 } elsif (/style=stretched/i) {
1412 $bg_opts->{tile} = 0;
1413 $bg_opts->{keep_aspect} = 0;
1414 $bg_opts->{root_align} = 0;
1415 $bg_opts->{h_scale} = $bg_opts->{v_scale} = 100;
1416 $bg_opts->{h_align} = $bg_opts->{v_align} = 50;
1417 } elsif (/style=centered/i) {
1418 $bg_opts->{tile} = 0;
1419 $bg_opts->{keep_aspect} = 0;
1420 $bg_opts->{root_align} = 0;
1421 $bg_opts->{h_scale} = $bg_opts->{v_scale} = 0;
1422 $bg_opts->{h_align} = $bg_opts->{v_align} = 50;
1423 } elsif (/style=root-tiled/i) {
1424 $bg_opts->{tile} = 1;
1425 $bg_opts->{keep_aspect} = 0;
1426 $bg_opts->{root_align} = 1;
1427 $bg_opts->{h_scale} = $bg_opts->{v_scale} = 0;
1428 $bg_opts->{h_align} = $bg_opts->{v_align} = 0;
1429 } elsif (/op=tile/i) {
1430 $bg_opts->{tile} = 1;
1431 } elsif (/op=keep-aspect/i) {
1432 $bg_opts->{keep_aspect} = 1;
1433 } elsif (/op=root-align/i) {
1434 $bg_opts->{root_align} = 1;
1435 } elsif (/^ =? ([0-9]+)? (?:[xX] ([0-9]+))? ([+-][0-9]+)? ([+-][0-9]+)? $/x) {
1436 my ($w, $h, $x, $y) = ($1, $2, $3, $4);
1437
1438 if ($str[0]) {
1439 $w = $h unless defined $w;
1440 $h = $w unless defined $h;
1441 $y = $x unless defined $y;
1442 }
1443
1444 $bg_opts->{h_scale} = $w if defined $w;
1445 $bg_opts->{v_scale} = $h if defined $h;
1446 $bg_opts->{h_align} = $x if defined $x;
1447 $bg_opts->{v_align} = $y if defined $y;
1448 }
1449 }
1450}
1451
1452# helper function, quote string as perl without allowing
1453# any code execution or other shenanigans.
1454sub q0 {
1455 "qq\x00\Q$_\E\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 find_resource {
1539 my ($self, $res, $opt) = @_;
1540
1541 my $v = $self->x_resource ($opt);
1542 $v = $self->x_resource ($res) unless defined $v;
1543
1544 $v
1545}
1546
1547sub parse_bgopts {
1548 my ($self) = @_;
1549
1550 my $expr = $self->x_resource ("%.expr");
1551
1552 if (!$expr) {
1553 $self->{bg_opts} = { h_scale => 100, v_scale => 100,
1554 h_align => 50, v_align => 50 };
1555
1556 $self->{bg_opts}{shade} = $self->find_resource ("shading", "sh");
1557 $self->{bg_opts}{tint} = $self->find_resource ("tintColor", "tint");
1558 $self->{bg_opts}{blur} = $self->find_resource ("blurRadius", "blr");
1559 $self->{bg_opts}{root} = $self->find_resource ("transparent", "tr");
1560
1561 $self->old_bg_opts ($self->find_resource ("backgroundPixmap", "pixmap"));
1562 $expr = $self->old_bg_expr;
1563 }
1564
1565 $self->set_expr (parse_expr $expr);
1566 $self->{border} = $self->x_resource_boolean ("%.border");
1567
1568 $MIN_INTERVAL = $self->x_resource ("%.interval");
1569}
1570
706sub on_start { 1571sub on_start {
707 my ($self) = @_; 1572 my ($self) = @_;
708 1573
709 my $expr = $self->x_resource ("background.expr") 1574 $self->parse_bgopts;
710 or return;
711
712 $self->set_expr (parse_expr $expr);
713 $self->{border} = $self->x_resource_boolean ("background.border");
714 1575
715 () 1576 ()
716} 1577}
717 1578
1579sub on_osc_seq {
1580 my ($self, $op, $arg) = @_;
1581
1582 $op eq "20" or $op eq "706"
1583 or return;
1584
1585 $self->{bg_opts}
1586 or $self->parse_bgopts;
1587
1588 if ($op eq "20") {
1589 if ($arg eq "?") {
1590 my $h_scale = $self->{bg_opts}{h_scale};
1591 my $v_scale = $self->{bg_opts}{v_scale};
1592 my $h_align = $self->{bg_opts}{h_align};
1593 my $v_align = $self->{bg_opts}{v_align};
1594 $self->cmd_parse ("\033]2;[${h_scale}x${v_scale}+${h_align}+${v_align}]\007");
1595 } else {
1596 $self->old_bg_opts ($arg);
1597 my $expr = $self->old_bg_expr;
1598 $self->set_expr (parse_expr $expr) if $expr;
1599 }
1600 } elsif ($op eq "705") {
1601 $self->{bg_opts}{tint} = $arg;
1602 my $expr = $self->old_bg_expr;
1603 $self->set_expr (parse_expr $expr) if $expr;
1604 }
1605
1606 1
1607}
1608

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines