1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | #:META:X_RESOURCE:%.expr:string:background expression |
3 | #:META:X_RESOURCE:%.expr:string:background expression |
4 | #:META:X_RESOURCE:%.border:boolean:respect the terminal border |
4 | #:META:X_RESOURCE:%.border:boolean:respect the terminal border |
5 | #:META:X_RESOURCE:%.interval:seconds:minimum time between updates |
5 | #:META:X_RESOURCE:%.interval:seconds:minimum time between updates |
6 | |
|
|
7 | #TODO: once, rootalign |
|
|
8 | |
6 | |
9 | =head1 NAME |
7 | =head1 NAME |
10 | |
8 | |
11 | background - manage terminal background |
9 | background - manage terminal background |
12 | |
10 | |
… | |
… | |
59 | |
57 | |
60 | For example, an expression such as C<scale load "$HOME/mybg.png"> scales the |
58 | For example, an expression such as C<scale load "$HOME/mybg.png"> scales the |
61 | image to the window size, so it relies on the window size and will |
59 | image to the window size, so it relies on the window size and will |
62 | be reevaluated each time it is changed, but not when it moves for |
60 | be reevaluated each time it is changed, but not when it moves for |
63 | example. That ensures that the picture always fills the terminal, even |
61 | example. That ensures that the picture always fills the terminal, even |
64 | after it's size changes. |
62 | after its size changes. |
65 | |
63 | |
66 | =head2 EXPRESSIONS |
64 | =head2 EXPRESSIONS |
67 | |
65 | |
68 | Expressions are normal Perl expressions, in fact, they are Perl blocks - |
66 | Expressions are normal Perl expressions, in fact, they are Perl blocks - |
69 | which means you could use multiple lines and statements: |
67 | which means you could use multiple lines and statements: |
… | |
… | |
73 | return scale load "$HOME/weekday.png"; |
71 | return scale load "$HOME/weekday.png"; |
74 | } else { |
72 | } else { |
75 | return scale load "$HOME/sunday.png"; |
73 | return scale load "$HOME/sunday.png"; |
76 | } |
74 | } |
77 | |
75 | |
78 | This expression gets evaluated once per hour. It will set F<sunday.png> as |
76 | This expression is evaluated once per hour. It will set F<sunday.png> as |
79 | background on Sundays, and F<weekday.png> on all other days. |
77 | background on Sundays, and F<weekday.png> on all other days. |
80 | |
78 | |
81 | Fortunately, we expect that most expressions will be much simpler, with |
79 | Fortunately, we expect that most expressions will be much simpler, with |
82 | little Perl knowledge needed. |
80 | little Perl knowledge needed. |
83 | |
81 | |
… | |
… | |
117 | horizontal and vertical dimensions. For example, this halves the image |
115 | horizontal and vertical dimensions. For example, this halves the image |
118 | width and doubles the image height: |
116 | width and doubles the image height: |
119 | |
117 | |
120 | scale 0.5, 2, load "$HOME/mypic.png" |
118 | scale 0.5, 2, load "$HOME/mypic.png" |
121 | |
119 | |
122 | Other effects than scalign are also readily available, for exmaple, you can |
120 | Other effects than scaling are also readily available, for example, you can |
123 | tile the image to fill the whole window, instead of resizing it: |
121 | tile the image to fill the whole window, instead of resizing it: |
124 | |
122 | |
125 | tile load "$HOME/mypic.png" |
123 | tile load "$HOME/mypic.png" |
126 | |
124 | |
127 | In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator |
125 | In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator |
… | |
… | |
140 | pseudo-transparency, as the image seems to be static while the window is |
138 | pseudo-transparency, as the image seems to be static while the window is |
141 | moved around. |
139 | moved around. |
142 | |
140 | |
143 | =head2 CYCLES AND CACHING |
141 | =head2 CYCLES AND CACHING |
144 | |
142 | |
|
|
143 | =head3 C<load> et al. |
|
|
144 | |
145 | As has been mentioned before, the expression might be evaluated multiple |
145 | As has been mentioned before, the expression might be evaluated multiple |
146 | times. Each time the expression is reevaluated, a new cycle is said to |
146 | times. Each time the expression is reevaluated, a new cycle is said to |
147 | have begun. Many operators cache their results till the next cycle. |
147 | have begun. Many operators cache their results till the next cycle. |
148 | |
148 | |
149 | For example, the C<load> operator keeps a copy of the image. If it is |
149 | For example, the C<load> operator keeps a copy of the image. If it is |
… | |
… | |
153 | This only works for one cycle though, so as long as you load the same |
153 | This only works for one cycle though, so as long as you load the same |
154 | image every time, it will always be cached, but when you load a different |
154 | image every time, it will always be cached, but when you load a different |
155 | image, it will forget about the first one. |
155 | image, it will forget about the first one. |
156 | |
156 | |
157 | This allows you to either speed things up by keeping multiple images in |
157 | This allows you to either speed things up by keeping multiple images in |
158 | memory, or comserve memory by loading images more often. |
158 | memory, or conserve memory by loading images more often. |
159 | |
159 | |
160 | For example, you can keep two images in memory and use a random one like |
160 | For example, you can keep two images in memory and use a random one like |
161 | this: |
161 | this: |
162 | |
162 | |
163 | my $img1 = load "img1.png"; |
163 | my $img1 = load "img1.png"; |
… | |
… | |
173 | |
173 | |
174 | Here, a path is selected randomly, and load is only called for one image, |
174 | Here, a path is selected randomly, and load is only called for one image, |
175 | so keeps only one image in memory. If, on the next evaluation, luck |
175 | so keeps only one image in memory. If, on the next evaluation, luck |
176 | decides to use the other path, then it will have to load that image again. |
176 | decides to use the other path, then it will have to load that image again. |
177 | |
177 | |
|
|
178 | =head3 C<once { ... }> |
|
|
179 | |
|
|
180 | Another way to cache expensive operations is to use C<once { ... }>. The |
|
|
181 | C<once> operator takes a block of multiple statements enclosed by C<{}> |
|
|
182 | and evaluates it only.. once, returning any images the last statement |
|
|
183 | returned. Further calls simply produce the values from the cache. |
|
|
184 | |
|
|
185 | This is most useful for expensive operations, such as C<blur>: |
|
|
186 | |
|
|
187 | rootalign once { blur 20, root } |
|
|
188 | |
|
|
189 | This makes a blurred copy of the root background once, and on subsequent |
|
|
190 | calls, just root-aligns it. Since C<blur> is usually quite slow and |
|
|
191 | C<rootalign> is quite fast, this trades extra memory (For the cached |
|
|
192 | blurred pixmap) with speed (blur only needs to be redone when root |
|
|
193 | changes). |
|
|
194 | |
178 | =head1 REFERENCE |
195 | =head1 REFERENCE |
179 | |
196 | |
180 | =head2 COMMAND LINE SWITCHES |
197 | =head2 COMMAND LINE SWITCHES |
181 | |
198 | |
182 | =over 4 |
199 | =over 4 |
… | |
… | |
193 | Specifying this flag changes the behaviour, so that the image only |
210 | Specifying this flag changes the behaviour, so that the image only |
194 | replaces the background of the character area. |
211 | replaces the background of the character area. |
195 | |
212 | |
196 | =item --background-interval seconds |
213 | =item --background-interval seconds |
197 | |
214 | |
198 | Since some operations in the underlying XRender extension can effetively |
215 | Since some operations in the underlying XRender extension can effectively |
199 | freeze your X-server for prolonged time, this extension enforces a minimum |
216 | freeze your X-server for prolonged time, this extension enforces a minimum |
200 | time between updates, which is normally about 0.1 seconds. |
217 | time between updates, which is normally about 0.1 seconds. |
201 | |
218 | |
202 | If you want to do updates more often, you can decrease this safety |
219 | If you want to do updates more often, you can decrease this safety |
203 | interval with this switch. |
220 | interval with this switch. |
204 | |
221 | |
205 | =back |
222 | =back |
206 | |
223 | |
207 | =cut |
224 | =cut |
208 | |
225 | |
209 | our %_IMGCACHE; |
226 | our %_IMG_CACHE; |
210 | our $HOME; |
227 | our $HOME; |
211 | our ($self, $old, $new); |
228 | our ($self, $frame); |
212 | our ($x, $y, $w, $h); |
229 | our ($x, $y, $w, $h); |
213 | |
230 | |
214 | # enforce at least this interval between updates |
231 | # enforce at least this interval between updates |
215 | our $MIN_INTERVAL = 6/59.951; |
232 | our $MIN_INTERVAL = 6/59.951; |
216 | |
233 | |
217 | { |
234 | { |
218 | package urxvt::bgdsl; # background language |
235 | package urxvt::bgdsl; # background language |
|
|
236 | |
|
|
237 | sub FR_PARENT() { 0 } # parent frame, if any - must be #0 |
|
|
238 | sub FR_CACHE () { 1 } # cached values |
|
|
239 | sub FR_AGAIN () { 2 } # what this expr is sensitive to |
|
|
240 | sub FR_STATE () { 3 } # watchers etc. |
219 | |
241 | |
220 | use List::Util qw(min max sum shuffle); |
242 | use List::Util qw(min max sum shuffle); |
221 | |
243 | |
222 | =head2 PROVIDERS/GENERATORS |
244 | =head2 PROVIDERS/GENERATORS |
223 | |
245 | |
… | |
… | |
230 | =item load $path |
252 | =item load $path |
231 | |
253 | |
232 | Loads the image at the given C<$path>. The image is set to plane tiling |
254 | Loads the image at the given C<$path>. The image is set to plane tiling |
233 | mode. |
255 | mode. |
234 | |
256 | |
235 | Loaded images will be cached for one cycle. |
257 | Loaded images will be cached for one cycle, and shared between temrinals |
|
|
258 | running in the same process (e.g. in C<urxvtd>). |
|
|
259 | |
|
|
260 | #=item load_uc $path |
|
|
261 | # |
|
|
262 | #Load uncached - same as load, but does not cache the image. This function |
|
|
263 | #is most useufl if you want to optimise a background expression in some |
|
|
264 | #way. |
236 | |
265 | |
237 | =cut |
266 | =cut |
238 | |
267 | |
239 | sub load($) { |
268 | sub load($) { |
240 | my ($path) = @_; |
269 | my ($path) = @_; |
241 | |
270 | |
242 | $new->{load}{$path} = $old->{load}{$path} || $self->new_img_from_file ($path); |
271 | $_IMG_CACHE{$path} || do { |
|
|
272 | my $img = $self->new_img_from_file ($path); |
|
|
273 | Scalar::Util::weaken ($_IMG_CACHE{$path} = $img); |
|
|
274 | $img |
|
|
275 | } |
243 | } |
276 | } |
244 | |
277 | |
245 | =item root |
278 | =item root |
246 | |
279 | |
247 | Returns the root window pixmap, that is, hopefully, the background image |
280 | Returns the root window pixmap, that is, hopefully, the background image |
248 | of your screen. The image is set to extend mode. |
281 | of your screen. |
249 | |
282 | |
250 | This function makes your expression root sensitive, that means it will be |
283 | This function makes your expression root sensitive, that means it will be |
251 | reevaluated when the bg image changes. |
284 | reevaluated when the bg image changes. |
252 | |
285 | |
253 | =cut |
286 | =cut |
254 | |
287 | |
255 | sub root() { |
288 | sub root() { |
256 | $new->{rootpmap_sensitive} = 1; |
289 | $frame->[FR_AGAIN]{rootpmap} = 1; |
257 | die "root op not supported, exg, we need you"; |
290 | $self->new_img_from_root |
258 | } |
291 | } |
259 | |
292 | |
260 | =item solid $colour |
293 | =item solid $colour |
261 | |
294 | |
262 | =item solid $width, $height, $colour |
295 | =item solid $width, $height, $colour |
… | |
… | |
270 | =cut |
303 | =cut |
271 | |
304 | |
272 | sub solid($;$$) { |
305 | sub solid($;$$) { |
273 | my $colour = pop; |
306 | my $colour = pop; |
274 | |
307 | |
275 | my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); |
308 | my $img = $self->new_img (urxvt::PictStandardARGB32, 0, 0, $_[0] || 1, $_[1] || 1); |
276 | $img->fill ($colour); |
309 | $img->fill ($colour); |
277 | $img |
310 | $img |
278 | } |
311 | } |
279 | |
312 | |
280 | =item clone $img |
313 | =item clone $img |
… | |
… | |
286 | |
319 | |
287 | sub clone($) { |
320 | sub clone($) { |
288 | $_[0]->clone |
321 | $_[0]->clone |
289 | } |
322 | } |
290 | |
323 | |
291 | =back |
324 | =item merge $img ... |
|
|
325 | |
|
|
326 | Takes any number of images and merges them together, creating a single |
|
|
327 | image containing them all. The tiling mode of the first image is used as |
|
|
328 | the tiling mdoe of the resulting image. |
|
|
329 | |
|
|
330 | This function is called automatically when an expression returns multiple |
|
|
331 | images. |
|
|
332 | |
|
|
333 | =cut |
|
|
334 | |
|
|
335 | sub merge(@) { |
|
|
336 | return $_[0] unless $#_; |
|
|
337 | |
|
|
338 | # rather annoyingly clumsy, but optimisation is for another time |
|
|
339 | |
|
|
340 | my $x0 = +1e9; |
|
|
341 | my $y0 = +1e9; |
|
|
342 | my $x1 = -1e9; |
|
|
343 | my $y1 = -1e9; |
|
|
344 | |
|
|
345 | for (@_) { |
|
|
346 | my ($x, $y, $w, $h) = $_->geometry; |
|
|
347 | |
|
|
348 | $x0 = $x if $x0 > $x; |
|
|
349 | $y0 = $y if $y0 > $y; |
|
|
350 | |
|
|
351 | $x += $w; |
|
|
352 | $y += $h; |
|
|
353 | |
|
|
354 | $x1 = $x if $x1 < $x; |
|
|
355 | $y1 = $y if $y1 < $y; |
|
|
356 | } |
|
|
357 | |
|
|
358 | my $base = $self->new_img (urxvt::PictStandardARGB32, $x0, $y0, $x1 - $x0, $y1 - $y0); |
|
|
359 | $base->repeat_mode ($_[0]->repeat_mode); |
|
|
360 | $base->fill ([0, 0, 0, 0]); |
|
|
361 | |
|
|
362 | $base->draw ($_) |
|
|
363 | for @_; |
|
|
364 | |
|
|
365 | $base |
|
|
366 | } |
292 | |
367 | |
293 | =head2 TILING MODES |
368 | =head2 TILING MODES |
294 | |
369 | |
295 | The following operators modify the tiling mode of an image, that is, the |
370 | The following operators modify the tiling mode of an image, that is, the |
296 | way that pixels outside the image area are painted when the image is used. |
371 | way that pixels outside the image area are painted when the image is used. |
… | |
… | |
326 | become transparent. This mode is most useful when you want to place an |
401 | become transparent. This mode is most useful when you want to place an |
327 | image over another image or the background colour while leaving all |
402 | image over another image or the background colour while leaving all |
328 | background pixels outside the image unchanged. |
403 | background pixels outside the image unchanged. |
329 | |
404 | |
330 | Example: load an image and display it in the upper left corner. The rest |
405 | Example: load an image and display it in the upper left corner. The rest |
331 | of the space is left "empty" (transparent or wahtever your compisotr does |
406 | of the space is left "empty" (transparent or whatever your compositor does |
332 | in alpha mode, else background colour). |
407 | in alpha mode, else background colour). |
333 | |
408 | |
334 | pad load "mybg.png" |
409 | pad load "mybg.png" |
335 | |
410 | |
336 | =item extend $img |
411 | =item extend $img |
337 | |
412 | |
338 | Extends the image over the whole plane, using the closest pixel in the |
413 | Extends the image over the whole plane, using the closest pixel in the |
339 | area outside the image. This mode is mostly useful when you more complex |
414 | area outside the image. This mode is mostly useful when you use more complex |
340 | filtering operations and want the pixels outside the image to have the |
415 | filtering operations and want the pixels outside the image to have the |
341 | same values as the pixels near the edge. |
416 | same values as the pixels near the edge. |
342 | |
417 | |
343 | Example: just for curiosity, how does this pixel extension stuff work? |
418 | Example: just for curiosity, how does this pixel extension stuff work? |
344 | |
419 | |
… | |
… | |
411 | the window size to conserve memory. |
486 | the window size to conserve memory. |
412 | |
487 | |
413 | Example: take the screen background, clip it to the window size, blur it a |
488 | Example: take the screen background, clip it to the window size, blur it a |
414 | bit, align it to the window position and use it as background. |
489 | bit, align it to the window position and use it as background. |
415 | |
490 | |
416 | clip move -TX, -TY, blur 5, root |
491 | clip move -TX, -TY, once { blur 5, root } |
417 | |
492 | |
418 | =cut |
493 | =cut |
419 | |
494 | |
420 | sub TX() { $new->{position_sensitive} = 1; $x } |
495 | sub TX() { $frame->[FR_AGAIN]{position} = 1; $x } |
421 | sub TY() { $new->{position_sensitive} = 1; $y } |
496 | sub TY() { $frame->[FR_AGAIN]{position} = 1; $y } |
422 | sub TW() { $new->{size_sensitive} = 1; $w } |
497 | sub TW() { $frame->[FR_AGAIN]{size} = 1; $w } |
423 | sub TH() { $new->{size_sensitive} = 1; $h } |
498 | sub TH() { $frame->[FR_AGAIN]{size} = 1; $h } |
424 | |
499 | |
425 | =item now |
500 | =item now |
426 | |
501 | |
427 | Returns the current time as (fractional) seconds since the epoch. |
502 | Returns the current time as (fractional) seconds since the epoch. |
428 | |
503 | |
… | |
… | |
435 | C<$seconds> seconds. |
510 | C<$seconds> seconds. |
436 | |
511 | |
437 | Example: load some image and rotate it according to the time of day (as if it were |
512 | Example: load some image and rotate it according to the time of day (as if it were |
438 | the hour pointer of a clock). Update this image every minute. |
513 | the hour pointer of a clock). Update this image every minute. |
439 | |
514 | |
440 | again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
515 | again 60; rotate 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
441 | |
516 | |
442 | =item counter $seconds |
517 | =item counter $seconds |
443 | |
518 | |
444 | Like C<again>, but also returns an increasing counter value, starting at |
519 | Like C<again>, but also returns an increasing counter value, starting at |
445 | 0, which might be useful for some simple animation effects. |
520 | 0, which might be useful for some simple animation effects. |
… | |
… | |
447 | =cut |
522 | =cut |
448 | |
523 | |
449 | sub now() { urxvt::NOW } |
524 | sub now() { urxvt::NOW } |
450 | |
525 | |
451 | sub again($) { |
526 | sub again($) { |
452 | $new->{again} = $_[0]; |
527 | $frame->[FR_AGAIN]{time} = $_[0]; |
453 | } |
528 | } |
454 | |
529 | |
455 | sub counter($) { |
530 | sub counter($) { |
456 | $new->{again} = $_[0]; |
531 | $frame->[FR_AGAIN]{time} = $_[0]; |
457 | $self->{counter} + 0 |
532 | $frame->[FR_STATE]{counter} + 0 |
458 | } |
533 | } |
459 | |
534 | |
460 | =back |
535 | =back |
461 | |
536 | |
462 | =head2 SHAPE CHANGING OPERATORS |
537 | =head2 SHAPE CHANGING OPERATORS |
… | |
… | |
632 | |
707 | |
633 | sub rootalign($) { |
708 | sub rootalign($) { |
634 | move -TX, -TY, $_[0] |
709 | move -TX, -TY, $_[0] |
635 | } |
710 | } |
636 | |
711 | |
|
|
712 | =item rotate $center_x, $center_y, $degrees |
|
|
713 | |
|
|
714 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
|
|
715 | pointer at C<$center_x> and C<$center_y> (specified as factor of image |
|
|
716 | width/height). |
|
|
717 | |
|
|
718 | #TODO# new width, height, maybe more operators? |
|
|
719 | |
|
|
720 | Example: rotate the image by 90 degrees |
|
|
721 | |
|
|
722 | =cut |
|
|
723 | |
|
|
724 | sub rotate($$$$) { |
|
|
725 | my $img = pop; |
|
|
726 | $img->rotate ( |
|
|
727 | $_[0] * ($img->w + $img->x), |
|
|
728 | $_[1] * ($img->h + $img->y), |
|
|
729 | $_[2] * (3.14159265 / 180), |
|
|
730 | ) |
|
|
731 | } |
|
|
732 | |
637 | =back |
733 | =back |
638 | |
734 | |
639 | =head2 COLOUR MODIFICATIONS |
735 | =head2 COLOUR MODIFICATIONS |
640 | |
736 | |
641 | The following operators change the pixels of the image. |
737 | The following operators change the pixels of the image. |
… | |
… | |
675 | |
771 | |
676 | Values less than 0 reduce brightness, while values larger than 0 increase |
772 | Values less than 0 reduce brightness, while values larger than 0 increase |
677 | it. Useful range is from -1 to 1 - the former results in a black, the |
773 | it. Useful range is from -1 to 1 - the former results in a black, the |
678 | latter in a white picture. |
774 | latter in a white picture. |
679 | |
775 | |
680 | Due to idiosynchrasies in the underlying XRender extension, biases less |
776 | Due to idiosyncrasies in the underlying XRender extension, biases less |
681 | than zero can be I<very> slow. |
777 | than zero can be I<very> slow. |
682 | |
778 | |
683 | =cut |
779 | =cut |
684 | |
780 | |
685 | sub contrast($$;$$;$) { |
781 | sub contrast($$;$$;$) { |
… | |
… | |
723 | sub blur($$;$) { |
819 | sub blur($$;$) { |
724 | my $img = pop; |
820 | my $img = pop; |
725 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
821 | $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0]) |
726 | } |
822 | } |
727 | |
823 | |
728 | =item rotate $new_width, $new_height, $center_x, $center_y, $degrees |
|
|
729 | |
|
|
730 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
|
|
731 | pointer at C<$center_x> and C<$center_y> (specified as factor of image |
|
|
732 | width/height), generating a new image with width C<$new_width> and height |
|
|
733 | C<$new_height>. |
|
|
734 | |
|
|
735 | #TODO# new width, height, maybe more operators? |
|
|
736 | |
|
|
737 | Example: rotate the image by 90 degrees |
|
|
738 | |
|
|
739 | =cut |
|
|
740 | |
|
|
741 | sub rotate($$$$$$) { |
|
|
742 | my $img = pop; |
|
|
743 | $img->rotate ( |
|
|
744 | $_[0], |
|
|
745 | $_[1], |
|
|
746 | $_[2] * $img->w, |
|
|
747 | $_[3] * $img->h, |
|
|
748 | $_[4] * (3.14159265 / 180), |
|
|
749 | ) |
|
|
750 | } |
|
|
751 | |
|
|
752 | =back |
824 | =back |
753 | |
825 | |
|
|
826 | =head2 OTHER STUFF |
|
|
827 | |
|
|
828 | Anything that didn't fit any of the other categories, even after applying |
|
|
829 | force and closing our eyes. |
|
|
830 | |
|
|
831 | =over 4 |
|
|
832 | |
|
|
833 | =item once { ... } |
|
|
834 | |
|
|
835 | This function takes a code block as argument, that is, one or more |
|
|
836 | statements enclosed by braces. |
|
|
837 | |
|
|
838 | The trick is that this code block is only evaluated once - future calls |
|
|
839 | will simply return the original image (yes, it should only be used with |
|
|
840 | images). |
|
|
841 | |
|
|
842 | This can be extremely useful to avoid redoing the same slow operations |
|
|
843 | again and again- for example, if your background expression takes the root |
|
|
844 | background, blurs it and then root-aligns it it would have to blur the |
|
|
845 | root background on every window move or resize. |
|
|
846 | |
|
|
847 | In fact, urxvt itself encloses the whole expression in some kind of |
|
|
848 | C<once> block so it only is reevaluated as required. |
|
|
849 | |
|
|
850 | Putting the blur into a C<once> block will make sure the blur is only done |
|
|
851 | once: |
|
|
852 | |
|
|
853 | rootlign once { blur 10, root } |
|
|
854 | |
|
|
855 | This leaves the question of how to force reevaluation of the block, |
|
|
856 | in case the root background changes: If expression inside the block |
|
|
857 | is sensitive to some event (root background changes, window geometry |
|
|
858 | changes), then it will be reevaluated automatically as needed. |
|
|
859 | |
|
|
860 | =item once_again |
|
|
861 | |
|
|
862 | Resets all C<once> block as if they had never been called, i.e. on the |
|
|
863 | next call they will be reevaluated again. |
|
|
864 | |
|
|
865 | =cut |
|
|
866 | |
|
|
867 | sub once(&) { |
|
|
868 | my $id = $_[0]+0; |
|
|
869 | |
|
|
870 | local $frame = $self->{frame_cache}{$id} ||= [$frame]; |
|
|
871 | |
|
|
872 | unless ($frame->[FR_CACHE]) { |
|
|
873 | $frame->[FR_CACHE] = [ $_[0]() ]; |
|
|
874 | |
|
|
875 | my $self = $self; |
|
|
876 | my $frame = $frame; |
|
|
877 | Scalar::Util::weaken $frame; |
|
|
878 | $self->compile_frame ($frame, sub { |
|
|
879 | # clear this frame cache, also for all parents |
|
|
880 | for (my $frame = $frame; $frame; $frame = $frame->[0]) { |
|
|
881 | undef $frame->[FR_CACHE]; |
|
|
882 | } |
|
|
883 | |
|
|
884 | unless ($self->{term}) { |
|
|
885 | use Data::Dump; |
|
|
886 | ddx $frame; |
|
|
887 | exit; |
|
|
888 | } |
|
|
889 | |
|
|
890 | $self->recalculate; |
|
|
891 | }); |
|
|
892 | }; |
|
|
893 | |
|
|
894 | # in scalar context we always return the first original result, which |
|
|
895 | # is not quite how perl works. |
|
|
896 | wantarray |
|
|
897 | ? @{ $frame->[FR_CACHE] } |
|
|
898 | : $frame->[FR_CACHE][0] |
|
|
899 | } |
|
|
900 | |
|
|
901 | sub once_again() { |
|
|
902 | delete $self->{frame_cache}; |
|
|
903 | } |
|
|
904 | |
|
|
905 | =back |
|
|
906 | |
754 | =cut |
907 | =cut |
755 | |
908 | |
756 | } |
909 | } |
757 | |
910 | |
758 | sub parse_expr { |
911 | sub parse_expr { |
759 | my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}"; |
912 | my $expr = eval |
|
|
913 | "sub {\n" |
|
|
914 | . "package urxvt::bgdsl;\n" |
|
|
915 | . "#line 0 'background expression'\n" |
|
|
916 | . "$_[0]\n" |
|
|
917 | . "}"; |
760 | die if $@; |
918 | die if $@; |
761 | $expr |
919 | $expr |
762 | } |
920 | } |
763 | |
921 | |
764 | # compiles a parsed expression |
922 | # compiles a parsed expression |
765 | sub set_expr { |
923 | sub set_expr { |
766 | my ($self, $expr) = @_; |
924 | my ($self, $expr) = @_; |
767 | |
925 | |
|
|
926 | $self->{root} = []; |
768 | $self->{expr} = $expr; |
927 | $self->{expr} = $expr; |
769 | $self->recalculate; |
928 | $self->recalculate; |
|
|
929 | } |
|
|
930 | |
|
|
931 | # takes a hash of sensitivity indicators and installs watchers |
|
|
932 | sub compile_frame { |
|
|
933 | my ($self, $frame, $cb) = @_; |
|
|
934 | |
|
|
935 | my $state = $frame->[urxvt::bgdsl::FR_STATE] ||= {}; |
|
|
936 | my $again = $frame->[urxvt::bgdsl::FR_AGAIN]; |
|
|
937 | |
|
|
938 | # don't keep stuff alive |
|
|
939 | Scalar::Util::weaken $state; |
|
|
940 | |
|
|
941 | if ($again->{nested}) { |
|
|
942 | $state->{nested} = 1; |
|
|
943 | } else { |
|
|
944 | delete $state->{nested}; |
|
|
945 | } |
|
|
946 | |
|
|
947 | if (my $interval = $again->{time}) { |
|
|
948 | $state->{time} = [$interval, urxvt::timer->new->after ($interval)->interval ($interval)] |
|
|
949 | if $state->{time}[0] != $interval; |
|
|
950 | |
|
|
951 | # callback *might* have changed, although we could just rule that out |
|
|
952 | $state->{time}[1]->cb (sub { |
|
|
953 | ++$state->{counter}; |
|
|
954 | $cb->(); |
|
|
955 | }); |
|
|
956 | } else { |
|
|
957 | delete $state->{time}; |
|
|
958 | } |
|
|
959 | |
|
|
960 | if ($again->{position}) { |
|
|
961 | $state->{position} = $self->on (position_change => $cb); |
|
|
962 | } else { |
|
|
963 | delete $state->{position}; |
|
|
964 | } |
|
|
965 | |
|
|
966 | if ($again->{size}) { |
|
|
967 | $state->{size} = $self->on (size_change => $cb); |
|
|
968 | } else { |
|
|
969 | delete $state->{size}; |
|
|
970 | } |
|
|
971 | |
|
|
972 | if ($again->{rootpmap}) { |
|
|
973 | $state->{rootpmap} = $self->on (rootpmap_change => $cb); |
|
|
974 | } else { |
|
|
975 | delete $state->{rootpmap}; |
|
|
976 | } |
770 | } |
977 | } |
771 | |
978 | |
772 | # evaluate the current bg expression |
979 | # evaluate the current bg expression |
773 | sub recalculate { |
980 | sub recalculate { |
774 | my ($arg_self) = @_; |
981 | my ($arg_self) = @_; |
… | |
… | |
784 | |
991 | |
785 | $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; |
992 | $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; |
786 | |
993 | |
787 | # set environment to evaluate user expression |
994 | # set environment to evaluate user expression |
788 | |
995 | |
789 | local $self = $arg_self; |
996 | local $self = $arg_self; |
790 | |
|
|
791 | local $HOME = $ENV{HOME}; |
997 | local $HOME = $ENV{HOME}; |
792 | local $old = $self->{state}; |
998 | local $frame = []; |
793 | local $new = my $state = $self->{state} = {}; |
|
|
794 | |
999 | |
795 | ($x, $y, $w, $h) = |
|
|
796 | $self->background_geometry ($self->{border}); |
1000 | ($x, $y, $w, $h) = $self->background_geometry ($self->{border}); |
797 | |
1001 | |
798 | # evaluate user expression |
1002 | # evaluate user expression |
799 | |
1003 | |
800 | my $img = eval { $self->{expr}->() }; |
1004 | my @img = eval { $self->{expr}->() }; |
801 | warn $@ if $@;#d# |
1005 | die $@ if $@; |
802 | die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img"; |
1006 | die "background-expr did not return anything.\n" unless @img; |
|
|
1007 | die "background-expr: expected image(s), got something else.\n" |
|
|
1008 | if grep { !UNIVERSAL::isa $_, "urxvt::img" } @img; |
803 | |
1009 | |
804 | $state->{size_sensitive} = 1 |
1010 | my $img = urxvt::bgdsl::merge @img; |
|
|
1011 | |
|
|
1012 | $frame->[FR_AGAIN]{size} = 1 |
805 | if $img->repeat_mode != urxvt::RepeatNormal; |
1013 | if $img->repeat_mode != urxvt::RepeatNormal; |
806 | |
1014 | |
807 | # if the expression is sensitive to external events, prepare reevaluation then |
1015 | # if the expression is sensitive to external events, prepare reevaluation then |
808 | |
1016 | $self->compile_frame ($frame, sub { $arg_self->recalculate }); |
809 | my $repeat; |
|
|
810 | |
|
|
811 | if (my $again = $state->{again}) { |
|
|
812 | $repeat = 1; |
|
|
813 | my $self = $self; |
|
|
814 | $state->{timer} = $again == $old->{again} |
|
|
815 | ? $old->{timer} |
|
|
816 | : urxvt::timer->new->after ($again)->interval ($again)->cb (sub { |
|
|
817 | ++$self->{counter}; |
|
|
818 | $self->recalculate |
|
|
819 | }); |
|
|
820 | } |
|
|
821 | |
|
|
822 | if (delete $state->{position_sensitive}) { |
|
|
823 | $repeat = 1; |
|
|
824 | $self->enable (position_change => sub { $_[0]->recalculate }); |
|
|
825 | } else { |
|
|
826 | $self->disable ("position_change"); |
|
|
827 | } |
|
|
828 | |
|
|
829 | if (delete $state->{size_sensitive}) { |
|
|
830 | $repeat = 1; |
|
|
831 | $self->enable (size_change => sub { $_[0]->recalculate }); |
|
|
832 | } else { |
|
|
833 | $self->disable ("size_change"); |
|
|
834 | } |
|
|
835 | |
|
|
836 | if (delete $state->{rootpmap_sensitive}) { |
|
|
837 | $repeat = 1; |
|
|
838 | $self->enable (rootpmap_change => sub { $_[0]->recalculate }); |
|
|
839 | } else { |
|
|
840 | $self->disable ("rootpmap_change"); |
|
|
841 | } |
|
|
842 | |
1017 | |
843 | # clear stuff we no longer need |
1018 | # clear stuff we no longer need |
844 | |
1019 | |
845 | %$old = (); |
1020 | # unless (%{ $frame->[FR_STATE] }) { |
846 | |
|
|
847 | unless ($repeat) { |
|
|
848 | delete $self->{state}; |
1021 | # delete $self->{state}; |
849 | delete $self->{expr}; |
1022 | # delete $self->{expr}; |
850 | } |
1023 | # } |
851 | |
1024 | |
852 | # set background pixmap |
1025 | # set background pixmap |
853 | |
1026 | |
854 | $self->set_background ($img, $self->{border}); |
1027 | $self->set_background ($img, $self->{border}); |
855 | $self->scr_recolour (0); |
1028 | $self->scr_recolour (0); |