ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/background
Revision: 1.42
Committed: Sun Jun 10 10:42:19 2012 UTC (11 years, 11 months ago) by root
Branch: MAIN
Changes since 1.41: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! perl
2
3 #:META:X_RESOURCE:%.expr:string:background expression
4 #:META:X_RESOURCE:%.border.:boolean:respect the terminal border
5
6 #TODO: once, rootalign
7
8 =head1 NAME
9
10 background - manage terminal background
11
12 =head1 SYNOPSIS
13
14 urxvt --background-expr 'background expression'
15 --background-border
16
17 =head1 DESCRIPTION
18
19 This extension manages the terminal background by creating a picture that
20 is behind the text, replacing the normal background colour.
21
22 It does so by evaluating a Perl expression that I<calculates> the image on
23 the fly, for example, by grabbing the root background or loading a file.
24
25 While the full power of Perl is available, the operators have been design
26 to be as simple as possible.
27
28 For example, to load an image and scale it to the window size, you would
29 use:
30
31 urxvt --background-expr 'scale load "/path/to/mybg.png"'
32
33 Or specified as a X resource:
34
35 URxvt.background-expr: scale load "/path/to/mybg.png"
36
37 =head1 THEORY OF OPERATION
38
39 At startup, just before the window is mapped for the first time, the
40 expression is evaluated and must yield an image. The image is then
41 extended as necessary to cover the whole terminal window, and is set as a
42 background pixmap.
43
44 If the image contains an alpha channel, then it will be used as-is in
45 visuals that support alpha channels (for example, for a compositing
46 manager). In other visuals, the terminal background colour will be used to
47 replace any transparency.
48
49 When the expression relies, directly or indirectly, on the window size,
50 position, the root pixmap, or a timer, then it will be remembered. If not,
51 then it will be removed.
52
53 If any of the parameters that the expression relies on changes (when the
54 window is moved or resized, its position or size changes; when the root
55 pixmap is replaced by another one the root background changes; or when the
56 timer elapses), then the expression will be evaluated again.
57
58 For example, an expression such as C<scale load "$HOME/mybg.png"> scales the
59 image to the window size, so it relies on the window size and will
60 be reevaluated each time it is changed, but not when it moves for
61 example. That ensures that the picture always fills the terminal, even
62 after it's size changes.
63
64 =head2 EXPRESSIONS
65
66 Expressions are normal Perl expressions, in fact, they are Perl blocks -
67 which means you could use multiple lines and statements:
68
69 again 3600;
70 if (localtime now)[6]) {
71 return scale load "$HOME/weekday.png";
72 } else {
73 return scale load "$HOME/sunday.png";
74 }
75
76 This expression gets evaluated once per hour. It will set F<sunday.png> as
77 background on Sundays, and F<weekday.png> on all other days.
78
79 Fortunately, we expect that most expressions will be much simpler, with
80 little Perl knowledge needed.
81
82 Basically, you always start with a function that "generates" an image
83 object, such as C<load>, which loads an image from disk, or C<root>, which
84 returns the root window background image:
85
86 load "$HOME/mypic.png"
87
88 The path is usually specified as a quoted string (the exact rules can be
89 found in the L<perlop> manpage). The F<$HOME> at the beginning of the
90 string is expanded to the home directory.
91
92 Then you prepend one or more modifiers or filtering expressions, such as
93 C<scale>:
94
95 scale load "$HOME/mypic.png"
96
97 Just like a mathematical expression with functions, you should read these
98 expressions from right to left, as the C<load> is evaluated first, and
99 its result becomes the argument to the C<scale> function.
100
101 Many operators also allow some parameters preceding the input image
102 that modify its behaviour. For example, C<scale> without any additional
103 arguments scales the image to size of the terminal window. If you specify
104 an additional argument, it uses it as a percentage:
105
106 scale 200, load "$HOME/mypic.png"
107
108 This enlarges the image by a factor of 2 (200%). As you can see, C<scale>
109 has now two arguments, the C<200> and the C<load> expression, while
110 C<load> only has one argument. Arguments are separated from each other by
111 commas.
112
113 Scale also accepts two arguments, which are then separate factors for both
114 horizontal and vertical dimensions. For example, this halves the image
115 width and doubles the image height:
116
117 scale 50, 200, load "$HOME/mypic.png"
118
119 Other effects than scalign are also readily available, for exmaple, you can
120 tile the image to fill the whole window, instead of resizing it:
121
122 tile load "$HOME/mypic.png"
123
124 In fact, images returned by C<load> are in C<tile> mode by default, so the C<tile> operator
125 is kind of superfluous.
126
127 Another common effect is to mirror the image, so that the same edges touch:
128
129 mirror load "$HOME/mypic.png"
130
131 This is also a typical background expression:
132
133 rootalign root
134
135 It first takes a snapshot of the screen background image, and then
136 moves it to the upper left corner of the screen - the result is
137 pseudo-transparency, as the image seems to be static while the window is
138 moved around.
139
140 =head2 CYCLES AND CACHING
141
142 As has been mentioned before, the expression might be evaluated multiple
143 times. Each time the expression is reevaluated, a new cycle is said to
144 have begun. Many operators cache their results till the next cycle.
145
146 For example, the C<load> operator keeps a copy of the image. If it is
147 asked to load the same image on the next cycle it will not load it again,
148 but return the cached copy.
149
150 This only works for one cycle though, so as long as you load the same
151 image every time, it will always be cached, but when you load a different
152 image, it will forget about the first one.
153
154 This allows you to either speed things up by keeping multiple images in
155 memory, or comserve memory by loading images more often.
156
157 For example, you can keep two images in memory and use a random one like
158 this:
159
160 my $img1 = load "img1.png";
161 my $img2 = load "img2.png";
162 (0.5 > rand) ? $img1 : $img2
163
164 Since both images are "loaded" every time the expression is evaluated,
165 they are always kept in memory. Contrast this version:
166
167 my $path1 = "img1.png";
168 my $path2 = "img2.png";
169 load ((0.5 > rand) ? $path1 : $path2)
170
171 Here, a path is selected randomly, and load is only called for one image,
172 so keeps only one image in memory. If, on the next evaluation, luck
173 decides to use the other path, then it will have to load that image again.
174
175 =head1 REFERENCE
176
177 =head2 COMMAND LINE SWITCHES
178
179 =over 4
180
181 =item --background-expr perl-expression
182
183 Specifies the Perl expression to evaluate.
184
185 =item --background-border
186
187 By default, the expression creates an image that fills the full window,
188 overwriting borders and any other areas, such as the scrollbar.
189
190 Specifying this flag changes the behaviour, so that the image only
191 replaces the background of the character area.
192
193 =back
194
195 =cut
196
197 our $HOME;
198 our ($self, $old, $new);
199 our ($x, $y, $w, $h);
200
201 # enforce at least this interval between updates
202 our $MIN_INTERVAL = 1/100;
203
204 {
205 package urxvt::bgdsl; # background language
206
207 =head2 PROVIDERS/GENERATORS
208
209 These functions provide an image, by loading it from disk, grabbing it
210 from the root screen or by simply generating it. They are used as starting
211 points to get an image you can play with.
212
213 =over 4
214
215 =item load $path
216
217 Loads the image at the given C<$path>. The image is set to plane tiling
218 mode.
219
220 Loaded images will be cached for one cycle.
221
222 =cut
223
224 sub load($) {
225 my ($path) = @_;
226
227 $new->{load}{$path} = $old->{load}{$path} || $self->new_img_from_file ($path);
228 }
229
230 =item root
231
232 Returns the root window pixmap, that is, hopefully, the background image
233 of your screen. The image is set to extend mode.
234
235 This function makes your expression root sensitive, that means it will be
236 reevaluated when the bg image changes.
237
238 =cut
239
240 sub root() {
241 $new->{rootpmap_sensitive} = 1;
242 die "root op not supported, exg, we need you";
243 }
244
245 =item solid $colour
246
247 =item solid $width, $height, $colour
248
249 Creates a new image and completely fills it with the given colour. The
250 image is set to tiling mode.
251
252 If C<$width> and C<$height> are omitted, it creates a 1x1 image, which is
253 useful for solid backgrounds or for use in filtering effects.
254
255 =cut
256
257 sub solid($;$$) {
258 my $colour = pop;
259
260 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1);
261 $img->fill ($colour);
262 $img
263 }
264
265 =back
266
267 =head2 VARIABLES
268
269 The following functions provide variable data such as the terminal window
270 dimensions. They are not (Perl-) variables, they jsut return stuff that
271 varies. Most of them make your expression sensitive to some events, for
272 example using C<TW> (terminal width) means your expression is evaluated
273 again when the terminal is resized.
274
275 =over 4
276
277 =item TX
278
279 =item TY
280
281 Return the X and Y coordinates of the terminal window (the terminal
282 window is the full window by default, and the character area only when in
283 border-respect mode).
284
285 Using these functions make your expression sensitive to window moves.
286
287 These functions are mainly useful to align images to the root window.
288
289 Example: load an image and align it so it looks as if anchored to the
290 background.
291
292 move -TX, -TY, load "mybg.png"
293
294 =item TW
295
296 Return the width (C<TW>) and height (C<TH>) of the terminal window (the
297 terminal window is the full window by default, and the character area only
298 when in border-respect mode).
299
300 Using these functions make your expression sensitive to window resizes.
301
302 These functions are mainly useful to scale images, or to clip images to
303 the window size to conserve memory.
304
305 Example: take the screen background, clip it to the window size, blur it a
306 bit, align it to the window position and use it as background.
307
308 clip move -TX, -TY, blur 5, root
309
310 =cut
311
312 sub TX() { $new->{position_sensitive} = 1; $x }
313 sub TY() { $new->{position_sensitive} = 1; $y }
314 sub TW() { $new->{size_sensitive} = 1; $w }
315 sub TH() { $new->{size_sensitive} = 1; $h }
316
317 =item now
318
319 Returns the current time as (fractional) seconds since the epoch.
320
321 Using this expression does I<not> make your expression sensitive to time,
322 but the next two functions do.
323
324 =item again $seconds
325
326 When this function is used the expression will be reevaluated again in
327 C<$seconds> seconds.
328
329 Example: load some image and rotate it according to the time of day (as if it were
330 the hour pointer of a clock). Update this image every minute.
331
332 again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png"
333
334 =item counter $seconds
335
336 Like C<again>, but also returns an increasing counter value, starting at
337 0, which might be useful for some simple animation effects.
338
339 =cut
340
341 sub now() { urxvt::NOW }
342
343 sub again($) {
344 $new->{again} = $_[0];
345 }
346
347 sub counter($) {
348 $new->{again} = $_[0];
349 $self->{counter} + 0
350 }
351
352 =back
353
354 =head2 TILING MODES
355
356 The following operators modify the tiling mode of an image, that is, the
357 way that pixels outside the image area are painted when the image is used.
358
359 =over 4
360
361 =item tile $img
362
363 Tiles the whole plane with the image and returns this new image - or in
364 other words, it returns a copy of the image in plane tiling mode.
365
366 Example: load an image and tile it over the background, without
367 resizing. The C<tile> call is superfluous because C<load> already defaults
368 to tiling mode.
369
370 tile load "mybg.png"
371
372 =item mirror $img
373
374 Similar to tile, but reflects the image each time it uses a new copy, so
375 that top edges always touch top edges, right edges always touch right
376 edges and so on (with normal tiling, left edges always touch right edges
377 and top always touch bottom edges).
378
379 Example: load an image and mirror it over the background, avoiding sharp
380 edges at the image borders at the expense of mirroring the image itself
381
382 mirror load "mybg.png"
383
384 =item pad $img
385
386 Takes an image and modifies it so that all pixels outside the image area
387 become transparent. This mode is most useful when you want to place an
388 image over another image or the background colour while leaving all
389 background pixels outside the image unchanged.
390
391 Example: load an image and display it in the upper left corner. The rest
392 of the space is left "empty" (transparent or wahtever your compisotr does
393 in alpha mode, else background colour).
394
395 pad load "mybg.png"
396
397 =item extend $img
398
399 Extends the image over the whole plane, using the closest pixel in the
400 area outside the image. This mode is mostly useful when you more complex
401 filtering operations and want the pixels outside the image to have the
402 same values as the pixels near the edge.
403
404 Example: just for curiosity, how does this pixel extension stuff work?
405
406 extend move 50, 50, load "mybg.png"
407
408 =cut
409
410 sub pad($) {
411 my $img = $_[0]->clone;
412 $img->repeat_mode (urxvt::RepeatNone);
413 $img
414 }
415
416 sub tile($) {
417 my $img = $_[0]->clone;
418 $img->repeat_mode (urxvt::RepeatNormal);
419 $img
420 }
421
422 sub mirror($) {
423 my $img = $_[0]->clone;
424 $img->repeat_mode (urxvt::RepeatReflect);
425 $img
426 }
427
428 sub extend($) {
429 my $img = $_[0]->clone;
430 $img->repeat_mode (urxvt::RepeatPad);
431 $img
432 }
433
434 =back
435
436 =head2 PIXEL OPERATORS
437
438 The following operators modify the image pixels in various ways.
439
440 =over 4
441
442 =item clone $img
443
444 Returns an exact copy of the image.
445
446 =cut
447
448 sub clone($) {
449 $_[0]->clone
450 }
451
452 =item clip $img
453
454 =item clip $width, $height, $img
455
456 =item clip $x, $y, $width, $height, $img
457
458 Clips an image to the given rectangle. If the rectangle is outside the
459 image area (e.g. when C<$x> or C<$y> are negative) or the rectangle is
460 larger than the image, then the tiling mode defines how the extra pixels
461 will be filled.
462
463 If C<$x> an C<$y> are missing, then C<0> is assumed for both.
464
465 If C<$width> and C<$height> are missing, then the window size will be
466 assumed.
467
468 Example: load an image, blur it, and clip it to the window size to save
469 memory.
470
471 clip blur 10, load "mybg.png"
472
473 =cut
474
475 sub clip($;$$;$$) {
476 my $img = pop;
477 my $h = pop || TH;
478 my $w = pop || TW;
479 $img->sub_rect ($_[0], $_[1], $w, $h)
480 }
481
482 =item scale $img
483
484 =item scale $size_percent, $img
485
486 =item scale $width_percent, $height_percent, $img
487
488 Scales the image by the given percentages in horizontal
489 (C<$width_percent>) and vertical (C<$height_percent>) direction.
490
491 If only one percentage is give, it is used for both directions.
492
493 If no percentages are given, scales the image to the window size without
494 keeping aspect.
495
496 =item resize $width, $height, $img
497
498 Resizes the image to exactly C<$width> times C<$height> pixels.
499
500 =cut
501
502 #TODO: maximise, maximise_fill?
503
504 sub scale($;$;$) {
505 my $img = pop;
506
507 @_ == 2 ? $img->scale ($_[0] * $img->w * 0.01, $_[1] * $img->h * 0.01)
508 : @_ ? $img->scale ($_[0] * $img->w * 0.01, $_[0] * $img->h * 0.01)
509 : $img->scale (TW, TH)
510 }
511
512 sub resize($$$) {
513 my $img = pop;
514 $img->scale ($_[0], $_[1])
515 }
516
517 =item move $dx, $dy, $img
518
519 Moves the image by C<$dx> pixels in the horizontal, and C<$dy> pixels in
520 the vertical.
521
522 Example: move the image right by 20 pixels and down by 30.
523
524 move 20, 30, ...
525
526 =item rootalign $img
527
528 Moves the image so that it appears glued to the screen as opposed to the
529 window. This gives the illusion of a larger area behind the window. It is
530 exactly equivalent to C<move -TX, -TY>, that is, it moves the image to the
531 top left of the screen.
532
533 Example: load a background image, put it in mirror mode and root align it.
534
535 rootalign mirror load "mybg.png"
536
537 Example: take the screen background and align it, giving the illusion of
538 transparency as long as the window isn't in front of other windows.
539
540 rootalign root
541
542 =cut
543
544 sub move($$;$) {
545 my $img = pop->clone;
546 $img->move ($_[0], $_[1]);
547 $img
548 }
549
550 sub rootalign($) {
551 move -TX, -TY, $_[0]
552 }
553
554 =item contrast $factor, $img
555
556 =item contrast $r, $g, $b, $img
557
558 =item contrast $r, $g, $b, $a, $img
559
560 Adjusts the I<contrast> of an image.
561
562 #TODO#
563
564 =item brightness $factor, $img
565
566 =item brightness $r, $g, $b, $img
567
568 =item brightness $r, $g, $b, $a, $img
569
570 Adjusts the brightness of an image.
571
572 =cut
573
574 sub contrast($$;$$;$) {
575 my $img = pop;
576 my ($r, $g, $b, $a) = @_;
577
578 ($g, $b) = ($r, $r) if @_ < 4;
579 $a = 1 if @_ < 5;
580
581 $img = $img->clone;
582 $img->contrast ($r, $g, $b, $a);
583 $img
584 }
585
586 sub brightness($$;$$;$) {
587 my $img = pop;
588 my ($r, $g, $b, $a) = @_;
589
590 ($g, $b) = ($r, $r) if @_ < 4;
591 $a = 1 if @_ < 5;
592
593 $img = $img->clone;
594 $img->brightness ($r, $g, $b, $a);
595 $img
596 }
597
598 =item blur $radius, $img
599
600 =item blur $radius_horz, $radius_vert, $img
601
602 Gaussian-blurs the image with (roughly) C<$radius> pixel radius. The radii
603 can also be specified separately.
604
605 Blurring is often I<very> slow, at least compared or other
606 operators. Larger blur radii are slower than smaller ones, too, so if you
607 don't want to freeze your screen for long times, start experimenting with
608 low values for radius (<5).
609
610 =cut
611
612 sub blur($$;$) {
613 my $img = pop;
614 $img->blur ($_[0], @_ >= 2 ? $_[1] : $_[0])
615 }
616
617 =item rotate $new_width, $new_height, $center_x, $center_y, $degrees
618
619 Rotates the image by C<$degrees> degrees, counter-clockwise, around the
620 pointer at C<$center_x> and C<$center_y> (specified as percentage of image
621 width/height), generating a new image with width C<$new_width> and height
622 C<$new_height>.
623
624 #TODO# new width, height, maybe more operators?
625
626 Example: rotate the image by 90 degrees
627
628 =cut
629
630 sub rotate($$$$$$) {
631 my $img = pop;
632 $img->rotate (
633 $_[0],
634 $_[1],
635 $_[2] * $img->w * .01,
636 $_[3] * $img->h * .01,
637 $_[4] * (3.14159265 / 180),
638 )
639 }
640
641 =back
642
643 =cut
644
645 }
646
647 sub parse_expr {
648 my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}";
649 die if $@;
650 $expr
651 }
652
653 # compiles a parsed expression
654 sub set_expr {
655 my ($self, $expr) = @_;
656
657 $self->{expr} = $expr;
658 $self->recalculate;
659 }
660
661 # evaluate the current bg expression
662 sub recalculate {
663 my ($arg_self) = @_;
664
665 # rate limit evaluation
666
667 if ($arg_self->{next_refresh} > urxvt::NOW) {
668 $arg_self->{next_refresh_timer} = urxvt::timer->new->after ($arg_self->{next_refresh} - urxvt::NOW)->cb (sub {
669 $arg_self->recalculate;
670 });
671 return;
672 }
673
674 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
675
676 # set environment to evaluate user expression
677
678 local $self = $arg_self;
679
680 local $HOME = $ENV{HOME};
681 local $old = $self->{state};
682 local $new = my $state = $self->{state} = {};
683
684 ($x, $y, $w, $h) =
685 $self->background_geometry ($self->{border});
686
687 # evaluate user expression
688
689 my $img = eval { $self->{expr}->() };
690 warn $@ if $@;#d#
691 die if !UNIVERSAL::isa $img, "urxvt::img";
692
693 $state->{size_sensitive} = 1
694 if $img->repeat_mode != urxvt::RepeatNormal;
695
696 # if the expression is sensitive to external events, prepare reevaluation then
697
698 my $repeat;
699
700 if (my $again = $state->{again}) {
701 $repeat = 1;
702 my $self = $self;
703 $state->{timer} = $again == $old->{again}
704 ? $old->{timer}
705 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
706 ++$self->{counter};
707 $self->recalculate
708 });
709 }
710
711 if (delete $state->{position_sensitive}) {
712 $repeat = 1;
713 $self->enable (position_change => sub { $_[0]->recalculate });
714 } else {
715 $self->disable ("position_change");
716 }
717
718 if (delete $state->{size_sensitive}) {
719 $repeat = 1;
720 $self->enable (size_change => sub { $_[0]->recalculate });
721 } else {
722 $self->disable ("size_change");
723 }
724
725 if (delete $state->{rootpmap_sensitive}) {
726 $repeat = 1;
727 $self->enable (rootpmap_change => sub { $_[0]->recalculate });
728 } else {
729 $self->disable ("rootpmap_change");
730 }
731
732 # clear stuff we no longer need
733
734 %$old = ();
735
736 unless ($repeat) {
737 delete $self->{state};
738 delete $self->{expr};
739 }
740
741 # set background pixmap
742
743 $self->set_background ($img, $self->{border});
744 $self->scr_recolour (0);
745 $self->want_refresh;
746 }
747
748 sub on_start {
749 my ($self) = @_;
750
751 my $expr = $self->x_resource ("background.expr")
752 or return;
753
754 $self->set_expr (parse_expr $expr);
755 $self->{border} = $self->x_resource_boolean ("background.border");
756
757 ()
758 }
759