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.52 by root, Tue Jun 12 10:45:53 2012 UTC vs.
Revision 1.61 by root, Fri Jun 15 19:50:56 2012 UTC

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
76This expression gets evaluated once per hour. It will set F<sunday.png> as 76This expression is evaluated once per hour. It will set F<sunday.png> as
77background on Sundays, and F<weekday.png> on all other days. 77background on Sundays, and F<weekday.png> on all other days.
78 78
79Fortunately, we expect that most expressions will be much simpler, with 79Fortunately, we expect that most expressions will be much simpler, with
80little Perl knowledge needed. 80little Perl knowledge needed.
81 81
203=back 203=back
204 204
205=cut 205=cut
206 206
207our %_IMG_CACHE; 207our %_IMG_CACHE;
208our %_ONCE_CACHE;
209our $HOME; 208our $HOME;
210our ($self, $old, $new); 209our ($self, $old, $new);
211our ($x, $y, $w, $h); 210our ($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
231Loads the image at the given C<$path>. The image is set to plane tiling 230Loads the image at the given C<$path>. The image is set to plane tiling
232mode. 231mode.
233 232
234Loaded images will be cached for one cycle. 233Loaded images will be cached for one cycle, and shared between temrinals
234running in the same process (e.g. in C<urxvtd>).
235 235
236=item load_uc $path
237
238Load uncached - same as load, but does not cache the image. This function
239is most useufl if you want to optimise a background expression in some
240way.
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
246Returns the root window pixmap, that is, hopefully, the background image 262Returns the root window pixmap, that is, hopefully, the background image
250reevaluated when the bg image changes. 266reevaluated 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
269=cut 285=cut
270 286
271 sub solid($;$$) { 287 sub solid($;$$) {
272 my $colour = pop; 288 my $colour = pop;
273 289
274 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1); 290 my $img = $self->new_img (urxvt::PictStandardARGB32, 0, 0, $_[0] || 1, $_[1] || 1);
275 $img->fill ($colour); 291 $img->fill ($colour);
276 $img 292 $img
277 } 293 }
278 294
279=item clone $img 295=item clone $img
283 299
284=cut 300=cut
285 301
286 sub clone($) { 302 sub clone($) {
287 $_[0]->clone 303 $_[0]->clone
304 }
305
306=item merge $img ...
307
308Takes any number of images and merges them together, creating a single
309image containing them all.
310
311This function is called automatically when an expression returns multiple
312images.
313
314=cut
315
316 sub merge(@) {
317 return $_[0] unless $#_;
318
319 # rather annoyingly clumsy, but optimisation is for another time
320
321 my $x0 = +1e9;
322 my $y0 = +1e9;
323 my $x1 = -1e9;
324 my $y1 = -1e9;
325
326 for (@_) {
327 my ($x, $y, $w, $h) = $_->geometry;
328
329 $x0 = $x if $x0 > $x;
330 $y0 = $y if $y0 > $y;
331
332 $x += $w;
333 $y += $h;
334
335 $x1 = $x if $x1 < $x;
336 $y1 = $y if $y1 < $y;
337 }
338
339 my $base = $self->new_img (urxvt::PictStandardARGB32, $x0, $y0, $x1 - $x0, $y1 - $y0);
340 $base->fill ([0, 0, 0, 0]);
341
342 $base->draw ($_)
343 for @_;
344
345 $base
288 } 346 }
289 347
290=head2 TILING MODES 348=head2 TILING MODES
291 349
292The following operators modify the tiling mode of an image, that is, the 350The following operators modify the tiling mode of an image, that is, the
408the window size to conserve memory. 466the window size to conserve memory.
409 467
410Example: take the screen background, clip it to the window size, blur it a 468Example: take the screen background, clip it to the window size, blur it a
411bit, align it to the window position and use it as background. 469bit, align it to the window position and use it as background.
412 470
413 clip move -TX, -TY, blur 5, root 471 clip move -TX, -TY, once { blur 5, root }
414 472
415=cut 473=cut
416 474
417 sub TX() { $new->{position_sensitive} = 1; $x } 475 sub TX() { $new->{again}{position} = 1; $x }
418 sub TY() { $new->{position_sensitive} = 1; $y } 476 sub TY() { $new->{again}{position} = 1; $y }
419 sub TW() { $new->{size_sensitive} = 1; $w } 477 sub TW() { $new->{again}{size} = 1; $w }
420 sub TH() { $new->{size_sensitive} = 1; $h } 478 sub TH() { $new->{again}{size} = 1; $h }
421 479
422=item now 480=item now
423 481
424Returns the current time as (fractional) seconds since the epoch. 482Returns the current time as (fractional) seconds since the epoch.
425 483
432C<$seconds> seconds. 490C<$seconds> seconds.
433 491
434Example: load some image and rotate it according to the time of day (as if it were 492Example: load some image and rotate it according to the time of day (as if it were
435the hour pointer of a clock). Update this image every minute. 493the hour pointer of a clock). Update this image every minute.
436 494
437 again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png" 495 again 60; rotate 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png"
438 496
439=item counter $seconds 497=item counter $seconds
440 498
441Like C<again>, but also returns an increasing counter value, starting at 499Like C<again>, but also returns an increasing counter value, starting at
4420, which might be useful for some simple animation effects. 5000, which might be useful for some simple animation effects.
444=cut 502=cut
445 503
446 sub now() { urxvt::NOW } 504 sub now() { urxvt::NOW }
447 505
448 sub again($) { 506 sub again($) {
449 $new->{again} = $_[0]; 507 $new->{again}{time} = $_[0];
450 } 508 }
451 509
452 sub counter($) { 510 sub counter($) {
453 $new->{again} = $_[0]; 511 $new->{again}{time} = $_[0];
454 $self->{counter} + 0 512 $self->{counter} + 0
455 } 513 }
456 514
457=back 515=back
458 516
629 687
630 sub rootalign($) { 688 sub rootalign($) {
631 move -TX, -TY, $_[0] 689 move -TX, -TY, $_[0]
632 } 690 }
633 691
634=item rotate $center_x, $center_y, $degrees, $new_width, $new_height 692=item rotate $center_x, $center_y, $degrees
635 693
636Rotates the image by C<$degrees> degrees, counter-clockwise, around the 694Rotates the image by C<$degrees> degrees, counter-clockwise, around the
637pointer at C<$center_x> and C<$center_y> (specified as factor of image 695pointer at C<$center_x> and C<$center_y> (specified as factor of image
638width/height), generating a new image with width C<$new_width> and height 696width/height).
639C<$new_height>.
640 697
641#TODO# new width, height, maybe more operators? 698#TODO# new width, height, maybe more operators?
642 699
643Example: rotate the image by 90 degrees 700Example: rotate the image by 90 degrees
644 701
645=cut 702=cut
646 703
647 sub rotate($$$$$$) { 704 sub rotate($$$$) {
648 my $img = pop; 705 my $img = pop;
649 $img->rotate ( 706 $img->rotate (
650 $_[0] * $img->w, 707 $_[0] * ($img->w + $img->x),
651 $_[1] * $img->h, 708 $_[1] * ($img->h + $img->y),
652 $_[2] * (3.14159265 / 180), 709 $_[2] * (3.14159265 / 180),
653 $_[3],
654 $_[4],
655 ) 710 )
656 } 711 }
657 712
658=back 713=back
659 714
748 803
749=back 804=back
750 805
751=head2 OTHER STUFF 806=head2 OTHER STUFF
752 807
753Anything that didn't fit any of the other categories, even after appliyng 808Anything that didn't fit any of the other categories, even after applying
754force and closing our eyes. 809force and closing our eyes.
755 810
756=over 4 811=over 4
757 812
758=item once { ... } 813=item once { ... }
785next call they will be reevaluated again. 840next call they will be reevaluated again.
786 841
787=cut 842=cut
788 843
789 sub once(&) { 844 sub once(&) {
790 $_ONCE_CACHE{$_[0]+0} ||= $_[0]() 845 my $once = $self->{once_cache}{$_[0]+0} ||= do {
846 local $new->{again};
847 my @res = $_[0]();
848 [$new->{again}, \@res]
849 };
850
851 $new->{again} = {
852 %{ $new->{again} },
853 %{ $once->[0] }
854 };
855
856 # in scalar context we always return the first original result, which
857 # is not quite how perl works.
858 wantarray
859 ? @{ $once->[1] }
860 : $once->[1][0]
791 } 861 }
792 862
793 sub once_again() { 863 sub once_again() {
794 %_ONCE_CACHE = (); 864 delete $self->{once_cache};
795 } 865 }
796 866
797=back 867=back
798 868
799=cut 869=cut
840 ($x, $y, $w, $h) = 910 ($x, $y, $w, $h) =
841 $self->background_geometry ($self->{border}); 911 $self->background_geometry ($self->{border});
842 912
843 # evaluate user expression 913 # evaluate user expression
844 914
845 my $img = eval { $self->{expr}->() }; 915 my $img = eval { urxvt::bgdsl::merge $self->{expr}->() };
846 warn $@ if $@;#d# 916 die $@ if $@;
847 die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img"; 917 die "background-expr did not return an image.\n" if !UNIVERSAL::isa $img, "urxvt::img";
848 918
849 $state->{size_sensitive} = 1 919 # if the expression is sensitive to external events, prepare reevaluation then
920
921 my $again = delete $state->{again};
922
923 $again->{size} = 1
850 if $img->repeat_mode != urxvt::RepeatNormal; 924 if $img->repeat_mode != urxvt::RepeatNormal;
851 925
852 # if the expression is sensitive to external events, prepare reevaluation then
853
854 my $repeat;
855
856 if (my $again = $state->{again}) { 926 if (my $again = $again->{time}) {
857 $repeat = 1;
858 my $self = $self; 927 my $self = $self;
859 $state->{timer} = $again == $old->{again} 928 $state->{timer} = $again == $old->{again}
860 ? $old->{timer} 929 ? $old->{timer}
861 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub { 930 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
862 ++$self->{counter}; 931 ++$self->{counter};
863 $self->recalculate 932 $self->recalculate
864 }); 933 });
865 } 934 }
866 935
867 if (delete $state->{position_sensitive}) { 936 if ($again->{position}) {
868 $repeat = 1;
869 $self->enable (position_change => sub { $_[0]->recalculate }); 937 $self->enable (position_change => sub { $_[0]->recalculate });
870 } else { 938 } else {
871 $self->disable ("position_change"); 939 $self->disable ("position_change");
872 } 940 }
873 941
874 if (delete $state->{size_sensitive}) { 942 if ($again->{size}) {
875 $repeat = 1;
876 $self->enable (size_change => sub { $_[0]->recalculate }); 943 $self->enable (size_change => sub { $_[0]->recalculate });
877 } else { 944 } else {
878 $self->disable ("size_change"); 945 $self->disable ("size_change");
879 } 946 }
880 947
881 if (delete $state->{rootpmap_sensitive}) { 948 if ($again->{rootpmap}) {
882 $repeat = 1;
883 $self->enable (rootpmap_change => sub { $_[0]->recalculate }); 949 $self->enable (rootpmap_change => sub {
950 delete $_[0]{once_cache}; # this will override once-block values from
951 $_[0]->recalculate;
952 });
884 } else { 953 } else {
885 $self->disable ("rootpmap_change"); 954 $self->disable ("rootpmap_change");
886 } 955 }
887 956
888 # clear stuff we no longer need 957 # clear stuff we no longer need
889 958
890 %$old = (); 959 %$old = ();
891 960
892 unless ($repeat) { 961 unless (%$again) {
893 delete $self->{state}; 962 delete $self->{state};
894 delete $self->{expr}; 963 delete $self->{expr};
895 } 964 }
896 965
897 # set background pixmap 966 # set background pixmap

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines