… | |
… | |
71 | return scale load "$HOME/weekday.png"; |
71 | return scale load "$HOME/weekday.png"; |
72 | } else { |
72 | } else { |
73 | return scale load "$HOME/sunday.png"; |
73 | return scale load "$HOME/sunday.png"; |
74 | } |
74 | } |
75 | |
75 | |
76 | 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 |
77 | background on Sundays, and F<weekday.png> on all other days. |
77 | background on Sundays, and F<weekday.png> on all other days. |
78 | |
78 | |
79 | Fortunately, we expect that most expressions will be much simpler, with |
79 | Fortunately, we expect that most expressions will be much simpler, with |
80 | little Perl knowledge needed. |
80 | little Perl knowledge needed. |
81 | |
81 | |
… | |
… | |
203 | =back |
203 | =back |
204 | |
204 | |
205 | =cut |
205 | =cut |
206 | |
206 | |
207 | our %_IMG_CACHE; |
207 | our %_IMG_CACHE; |
208 | our %_ONCE_CACHE; |
|
|
209 | our $HOME; |
208 | our $HOME; |
210 | our ($self, $old, $new); |
209 | our ($self, $old, $new); |
211 | our ($x, $y, $w, $h); |
210 | our ($x, $y, $w, $h); |
212 | |
211 | |
213 | # enforce at least this interval between updates |
212 | # enforce at least this interval between updates |
… | |
… | |
229 | =item load $path |
228 | =item load $path |
230 | |
229 | |
231 | Loads the image at the given C<$path>. The image is set to plane tiling |
230 | Loads the image at the given C<$path>. The image is set to plane tiling |
232 | mode. |
231 | mode. |
233 | |
232 | |
234 | Loaded images will be cached for one cycle. |
233 | Loaded images will be cached for one cycle, and shared between temrinals |
|
|
234 | running in the same process (e.g. in C<urxvtd>). |
235 | |
235 | |
|
|
236 | =item load_uc $path |
|
|
237 | |
|
|
238 | Load uncached - same as load, but does not cache the image. This function |
|
|
239 | is most useufl if you want to optimise a background expression in some |
|
|
240 | way. |
|
|
241 | |
236 | =cut |
242 | =cut |
|
|
243 | |
|
|
244 | sub load_uc($) { |
|
|
245 | my ($path) = @_; |
|
|
246 | |
|
|
247 | $_IMG_CACHE{$path} || do { |
|
|
248 | my $img = $self->new_img_from_file ($path); |
|
|
249 | Scalar::Util::weaken ($_IMG_CACHE{$path} = $img); |
|
|
250 | $img |
|
|
251 | } |
|
|
252 | } |
237 | |
253 | |
238 | sub load($) { |
254 | sub load($) { |
239 | my ($path) = @_; |
255 | my ($path) = @_; |
240 | |
256 | |
241 | $new->{load}{$path} = $old->{load}{$path} || $self->new_img_from_file ($path); |
257 | $new->{load}{$path} = $old->{load}{$path} || load_uc $path; |
242 | } |
258 | } |
243 | |
259 | |
244 | =item root |
260 | =item root |
245 | |
261 | |
246 | Returns the root window pixmap, that is, hopefully, the background image |
262 | Returns the root window pixmap, that is, hopefully, the background image |
… | |
… | |
250 | reevaluated when the bg image changes. |
266 | reevaluated when the bg image changes. |
251 | |
267 | |
252 | =cut |
268 | =cut |
253 | |
269 | |
254 | sub root() { |
270 | sub root() { |
255 | $new->{rootpmap_sensitive} = 1; |
271 | $new->{again}{rootpmap} = 1; |
256 | $self->new_img_from_root |
272 | $self->new_img_from_root |
257 | } |
273 | } |
258 | |
274 | |
259 | =item solid $colour |
275 | =item solid $colour |
260 | |
276 | |
… | |
… | |
408 | the window size to conserve memory. |
424 | the window size to conserve memory. |
409 | |
425 | |
410 | Example: take the screen background, clip it to the window size, blur it a |
426 | Example: take the screen background, clip it to the window size, blur it a |
411 | bit, align it to the window position and use it as background. |
427 | bit, align it to the window position and use it as background. |
412 | |
428 | |
413 | clip move -TX, -TY, blur 5, root |
429 | clip move -TX, -TY, once { blur 5, root } |
414 | |
430 | |
415 | =cut |
431 | =cut |
416 | |
432 | |
417 | sub TX() { $new->{position_sensitive} = 1; $x } |
433 | sub TX() { $new->{again}{position} = 1; $x } |
418 | sub TY() { $new->{position_sensitive} = 1; $y } |
434 | sub TY() { $new->{again}{position} = 1; $y } |
419 | sub TW() { $new->{size_sensitive} = 1; $w } |
435 | sub TW() { $new->{again}{size} = 1; $w } |
420 | sub TH() { $new->{size_sensitive} = 1; $h } |
436 | sub TH() { $new->{again}{size} = 1; $h } |
421 | |
437 | |
422 | =item now |
438 | =item now |
423 | |
439 | |
424 | Returns the current time as (fractional) seconds since the epoch. |
440 | Returns the current time as (fractional) seconds since the epoch. |
425 | |
441 | |
… | |
… | |
432 | C<$seconds> seconds. |
448 | C<$seconds> seconds. |
433 | |
449 | |
434 | Example: load some image and rotate it according to the time of day (as if it were |
450 | Example: load some image and rotate it according to the time of day (as if it were |
435 | the hour pointer of a clock). Update this image every minute. |
451 | the hour pointer of a clock). Update this image every minute. |
436 | |
452 | |
437 | again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
453 | again 60; rotate 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" |
438 | |
454 | |
439 | =item counter $seconds |
455 | =item counter $seconds |
440 | |
456 | |
441 | Like C<again>, but also returns an increasing counter value, starting at |
457 | Like C<again>, but also returns an increasing counter value, starting at |
442 | 0, which might be useful for some simple animation effects. |
458 | 0, which might be useful for some simple animation effects. |
… | |
… | |
444 | =cut |
460 | =cut |
445 | |
461 | |
446 | sub now() { urxvt::NOW } |
462 | sub now() { urxvt::NOW } |
447 | |
463 | |
448 | sub again($) { |
464 | sub again($) { |
449 | $new->{again} = $_[0]; |
465 | $new->{again}{time} = $_[0]; |
450 | } |
466 | } |
451 | |
467 | |
452 | sub counter($) { |
468 | sub counter($) { |
453 | $new->{again} = $_[0]; |
469 | $new->{again}{time} = $_[0]; |
454 | $self->{counter} + 0 |
470 | $self->{counter} + 0 |
455 | } |
471 | } |
456 | |
472 | |
457 | =back |
473 | =back |
458 | |
474 | |
… | |
… | |
629 | |
645 | |
630 | sub rootalign($) { |
646 | sub rootalign($) { |
631 | move -TX, -TY, $_[0] |
647 | move -TX, -TY, $_[0] |
632 | } |
648 | } |
633 | |
649 | |
634 | =item rotate $center_x, $center_y, $degrees, $new_width, $new_height |
650 | =item rotate $center_x, $center_y, $degrees |
635 | |
651 | |
636 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
652 | Rotates the image by C<$degrees> degrees, counter-clockwise, around the |
637 | pointer at C<$center_x> and C<$center_y> (specified as factor of image |
653 | pointer at C<$center_x> and C<$center_y> (specified as factor of image |
638 | width/height), generating a new image with width C<$new_width> and height |
654 | width/height). |
639 | C<$new_height>. |
|
|
640 | |
655 | |
641 | #TODO# new width, height, maybe more operators? |
656 | #TODO# new width, height, maybe more operators? |
642 | |
657 | |
643 | Example: rotate the image by 90 degrees |
658 | Example: rotate the image by 90 degrees |
644 | |
659 | |
645 | =cut |
660 | =cut |
646 | |
661 | |
647 | sub rotate($$$$$$) { |
662 | sub rotate($$$$) { |
648 | my $img = pop; |
663 | my $img = pop; |
649 | $img->rotate ( |
664 | $img->rotate ( |
650 | $_[0] * $img->w, |
665 | $_[0] * $img->w, |
651 | $_[1] * $img->h, |
666 | $_[1] * $img->h, |
652 | $_[2] * (3.14159265 / 180), |
667 | $_[2] * (3.14159265 / 180), |
653 | $_[3], |
|
|
654 | $_[4], |
|
|
655 | ) |
668 | ) |
656 | } |
669 | } |
657 | |
670 | |
658 | =back |
671 | =back |
659 | |
672 | |
… | |
… | |
785 | next call they will be reevaluated again. |
798 | next call they will be reevaluated again. |
786 | |
799 | |
787 | =cut |
800 | =cut |
788 | |
801 | |
789 | sub once(&) { |
802 | sub once(&) { |
790 | $_ONCE_CACHE{$_[0]+0} ||= $_[0]() |
803 | my $once = $self->{once_cache}{$_[0]+0} ||= do { |
|
|
804 | local $new->{again}; |
|
|
805 | my @res = $_[0](); |
|
|
806 | [$new->{again}, \@res] |
|
|
807 | }; |
|
|
808 | |
|
|
809 | $new->{again} = { |
|
|
810 | %{ $new->{again} }, |
|
|
811 | %{ $once->[0] } |
|
|
812 | }; |
|
|
813 | |
|
|
814 | # in scalar context we always return the first original result, which |
|
|
815 | # is not quite how perl works. |
|
|
816 | wantarray |
|
|
817 | ? @{ $once->[1] } |
|
|
818 | : $once->[1][0] |
791 | } |
819 | } |
792 | |
820 | |
793 | sub once_again() { |
821 | sub once_again() { |
794 | %_ONCE_CACHE = (); |
822 | delete $self->{once_cache}; |
795 | } |
823 | } |
796 | |
824 | |
797 | =back |
825 | =back |
798 | |
826 | |
799 | =cut |
827 | =cut |
… | |
… | |
844 | |
872 | |
845 | my $img = eval { $self->{expr}->() }; |
873 | my $img = eval { $self->{expr}->() }; |
846 | warn $@ if $@;#d# |
874 | warn $@ if $@;#d# |
847 | die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img"; |
875 | die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img"; |
848 | |
876 | |
849 | $state->{size_sensitive} = 1 |
877 | # if the expression is sensitive to external events, prepare reevaluation then |
|
|
878 | |
|
|
879 | my $again = delete $state->{again}; |
|
|
880 | |
|
|
881 | $again->{size} = 1 |
850 | if $img->repeat_mode != urxvt::RepeatNormal; |
882 | if $img->repeat_mode != urxvt::RepeatNormal; |
851 | |
883 | |
852 | # if the expression is sensitive to external events, prepare reevaluation then |
|
|
853 | |
|
|
854 | my $repeat; |
|
|
855 | |
|
|
856 | if (my $again = $state->{again}) { |
884 | if (my $again = $again->{time}) { |
857 | $repeat = 1; |
|
|
858 | my $self = $self; |
885 | my $self = $self; |
859 | $state->{timer} = $again == $old->{again} |
886 | $state->{timer} = $again == $old->{again} |
860 | ? $old->{timer} |
887 | ? $old->{timer} |
861 | : urxvt::timer->new->after ($again)->interval ($again)->cb (sub { |
888 | : urxvt::timer->new->after ($again)->interval ($again)->cb (sub { |
862 | ++$self->{counter}; |
889 | ++$self->{counter}; |
863 | $self->recalculate |
890 | $self->recalculate |
864 | }); |
891 | }); |
865 | } |
892 | } |
866 | |
893 | |
867 | if (delete $state->{position_sensitive}) { |
894 | if ($again->{position}) { |
868 | $repeat = 1; |
|
|
869 | $self->enable (position_change => sub { $_[0]->recalculate }); |
895 | $self->enable (position_change => sub { $_[0]->recalculate }); |
870 | } else { |
896 | } else { |
871 | $self->disable ("position_change"); |
897 | $self->disable ("position_change"); |
872 | } |
898 | } |
873 | |
899 | |
874 | if (delete $state->{size_sensitive}) { |
900 | if ($again->{size}) { |
875 | $repeat = 1; |
|
|
876 | $self->enable (size_change => sub { $_[0]->recalculate }); |
901 | $self->enable (size_change => sub { $_[0]->recalculate }); |
877 | } else { |
902 | } else { |
878 | $self->disable ("size_change"); |
903 | $self->disable ("size_change"); |
879 | } |
904 | } |
880 | |
905 | |
881 | if (delete $state->{rootpmap_sensitive}) { |
906 | if ($again->{rootpmap}) { |
882 | $repeat = 1; |
|
|
883 | $self->enable (rootpmap_change => sub { $_[0]->recalculate }); |
907 | $self->enable (rootpmap_change => sub { |
|
|
908 | delete $_[0]{once_cache}; # this will override once-block values from |
|
|
909 | $_[0]->recalculate; |
|
|
910 | }); |
884 | } else { |
911 | } else { |
885 | $self->disable ("rootpmap_change"); |
912 | $self->disable ("rootpmap_change"); |
886 | } |
913 | } |
887 | |
914 | |
888 | # clear stuff we no longer need |
915 | # clear stuff we no longer need |
889 | |
916 | |
890 | %$old = (); |
917 | %$old = (); |
891 | |
918 | |
892 | unless ($repeat) { |
919 | unless (%$again) { |
893 | delete $self->{state}; |
920 | delete $self->{state}; |
894 | delete $self->{expr}; |
921 | delete $self->{expr}; |
895 | } |
922 | } |
896 | |
923 | |
897 | # set background pixmap |
924 | # set background pixmap |