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.30 by root, Thu Jun 7 13:22:06 2012 UTC vs.
Revision 1.33 by root, Thu Jun 7 16:30:58 2012 UTC

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:%.enable:boolean:some boolean 4#:META:X_RESOURCE:%.border.:boolean:respect the terminal border
5#:META:X_RESOURCE:%.extra.:value:extra config 5
6=head1 background - manage terminal background
7
8=head2 SYNOPSIS
9
10 rxvt -background-expr 'background expression'
11 -background-border
12
13=head2 DESCRIPTION
14
15=head2 REFERENCE
16
17=cut
6 18
7our $EXPR; 19our $EXPR;
8#$EXPR = 'move W * 0.1, -H * 0.1, resize W * 0.5, H * 0.5, repeat_none load "opensource.png"'; 20#$EXPR = 'move W * 0.1, -H * 0.1, resize W * 0.5, H * 0.5, repeat_none load "opensource.png"';
9$EXPR = 'move -TX, -TY, load "argb.png"'; 21$EXPR = 'move -TX, -TY, load "argb.png"';
10#$EXPR = ' 22#$EXPR = '
15#$EXPR = 'solid "red"'; 27#$EXPR = 'solid "red"';
16#$EXPR = 'blur root, 10, 10' 28#$EXPR = 'blur root, 10, 10'
17#$EXPR = 'blur move (root, -x, -y), 5, 5' 29#$EXPR = 'blur move (root, -x, -y), 5, 5'
18#resize load "/root/pix/das_fette_schwein.jpg", w, h 30#resize load "/root/pix/das_fette_schwein.jpg", w, h
19 31
20use Safe;
21
22our ($bgdsl_self, $old, $new); 32our ($self, $old, $new);
23our ($x, $y, $w, $h); 33our ($x, $y, $w, $h);
24 34
25# enforce at least this interval between updates 35# enforce at least this interval between updates
26our $MIN_INTERVAL = 1/100; 36our $MIN_INTERVAL = 1/100;
27 37
28{ 38{
29 package urxvt::bgdsl; # background language 39 package urxvt::bgdsl; # background language
30 40
31=head2 PROVIDERS/GENERATORS 41=head2 PROVIDERS/GENERATORS
32 42
43These functions provide an image, by loading it from disk, grabbing it
44from the root screen or by simply generating it. They are used as starting
45points to get an image you can play with.
46
33=over 4 47=over 4
34 48
35=item load $path 49=item load $path
36 50
37Loads the image at the given C<$path>. The image is set to plane tiling 51Loads the image at the given C<$path>. The image is set to plane tiling
38mode. 52mode.
39 53
40 54Loaded images will be cached for one cycle.
41 55
42=cut 56=cut
43 57
44 sub load($) { 58 sub load($) {
45 my ($path) = @_; 59 my ($path) = @_;
46 60
47 $new->{load}{$path} = $old->{load}{$path} || $bgdsl_self->new_img_from_file ($path); 61 $new->{load}{$path} = $old->{load}{$path} || $self->new_img_from_file ($path);
48 } 62 }
63
64=item root
65
66Returns the root window pixmap, that is, hopefully, the background image
67of your screen. The image is set to extend mode.
68
69This function makes your expression root sensitive, that means it will be
70reevaluated when the bg image changes.
71
72=cut
49 73
50 sub root() { 74 sub root() {
51 $new->{rootpmap_sensitive} = 1; 75 $new->{rootpmap_sensitive} = 1;
52 die "root op not supported, exg, we need you"; 76 die "root op not supported, exg, we need you";
53 } 77 }
54 78
79=item solid $colour
80
81=item solid $width, $height, $colour
82
83Creates a new image and completely fills it with the given colour. The
84image is set to tiling mode.
85
86If <$width> and C<$height> are omitted, it creates a 1x1 image, which is
87useful for solid backgrounds or for use in filtering effects.
88
89=cut
90
55 sub solid($;$$) { 91 sub solid($$;$) {
92 my $colour = pop;
93
56 my $img = $bgdsl_self->new_img (urxvt::PictStandardARGB32, $_[1] || 1, $_[2] || 1); 94 my $img = $self->new_img (urxvt::PictStandardARGB32, $_[0] || 1, $_[1] || 1);
57 $img->fill ($_[0]); 95 $img->fill ($colour);
58 $img 96 $img
59 } 97 }
60 98
61=back 99=back
62 100
63=head2 VARIABLES 101=head2 VARIABLES
64 102
103The following functions provide variable data such as the terminal
104window dimensions. Most of them make your expression sensitive to some
105events, for example using C<TW> (terminal width) means your expression is
106evaluated again when the terminal is resized.
107
65=over 4 108=over 4
109
110=item TX
111
112=item TY
113
114Return the X and Y coordinates of the terminal window (the terminal
115window is the full window by default, and the character area only when in
116border-respect mode).
117
118Using these functions make your expression sensitive to window moves.
119
120These functions are mainly useful to align images to the root window.
121
122Example: load an image and align it so it looks as if anchored to the
123background.
124
125 move -TX, -TY, load "mybg.png"
126
127=item TW
128
129Return the width (C<TW>) and height (C<TH>) of the terminal window (the
130terminal window is the full window by default, and the character area only
131when in border-respect mode).
132
133Using these functions make your expression sensitive to window resizes.
134
135These functions are mainly useful to scale images, or to clip images to
136the window size to conserve memory.
137
138Example: take the screen background, clip it to the window size, blur it a
139bit, align it to the window position and use it as background.
140
141 clip move -TX, -TY, blur 5, root
66 142
67=cut 143=cut
68 144
69 sub TX() { $new->{position_sensitive} = 1; $x } 145 sub TX() { $new->{position_sensitive} = 1; $x }
70 sub TY() { $new->{position_sensitive} = 1; $y } 146 sub TY() { $new->{position_sensitive} = 1; $y }
71 sub TW() { $new->{size_sensitive} = 1; $w } 147 sub TW() { $new->{size_sensitive} = 1; $w }
72 sub TH() { $new->{size_sensitive} = 1; $h } 148 sub TH() { $new->{size_sensitive} = 1; $h }
73 149
150=item now
151
152Returns the current time as (fractional) seconds since the epoch.
153
154Using this expression does I<not> make your expression sensitive to time,
155but the next two functions do.
156
157=item again $seconds
158
159When this function is used the expression will be reevaluated again in
160C<$seconds> seconds.
161
162Example: load some image and rotate it according to the time of day (as if it were
163the hour pointer of a clock). update this image every minute.
164
165 again 60; rotate TW, TH, 50, 50, (now % 86400) * -720 / 86400, scale load "myclock.png"
166
167=item counter $seconds
168
169Like C<again>, but also returns an increasing counter value, starting at
1700, which might be useful for some simple animation effects.
171
172=cut
173
74 sub now() { urxvt::NOW } 174 sub now() { urxvt::NOW }
75 175
76 sub again($) { 176 sub again($) {
77 $new->{again} = $_[0]; 177 $new->{again} = $_[0];
78 } 178 }
79 179
80 sub counter($) { 180 sub counter($) {
81 $new->{again} = $_[0]; 181 $new->{again} = $_[0];
82 $bgdsl_self->{counter} + 0 182 $self->{counter} + 0
83 } 183 }
84 184
85=back 185=back
86 186
87=head2 TILING MODES 187=head2 TILING MODES
211 311
212=cut 312=cut
213 313
214#TODO: maximise, maximise_fill? 314#TODO: maximise, maximise_fill?
215 315
216 sub scale($$$) { 316 sub scale($;$;$) {
217 my $img = pop; 317 my $img = pop;
218 318
219 @_ == 2 ? $img->scale ($_[0] * $img->w * 0.01, $_[1] * $img->h * 0.01) 319 @_ == 2 ? $img->scale ($_[0] * $img->w * 0.01, $_[1] * $img->h * 0.01)
220 : @_ ? $img->scale ($_[0] * $img->w * 0.01, $_[0] * $img->h * 0.01) 320 : @_ ? $img->scale ($_[0] * $img->w * 0.01, $_[0] * $img->h * 0.01)
221 : $img->scale (TW, TH) 321 : $img->scale (TW, TH)
292 $self->recalculate; 392 $self->recalculate;
293} 393}
294 394
295# evaluate the current bg expression 395# evaluate the current bg expression
296sub recalculate { 396sub recalculate {
297 my ($self) = @_; 397 my ($arg_self) = @_;
298 398
299 # rate limit evaluation 399 # rate limit evaluation
300 400
301 if ($self->{next_refresh} > urxvt::NOW) { 401 if ($arg_self->{next_refresh} > urxvt::NOW) {
302 $self->{next_refresh_timer} = urxvt::timer->new->after ($self->{next_refresh} - urxvt::NOW)->cb (sub { 402 $arg_self->{next_refresh_timer} = urxvt::timer->new->after ($arg_self->{next_refresh} - urxvt::NOW)->cb (sub {
303 $self->recalculate; 403 $arg_self->recalculate;
304 }); 404 });
305 return; 405 return;
306 } 406 }
307 407
308 $self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; 408 $arg_self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
309 409
310 # set environment to evaluate user expression 410 # set environment to evaluate user expression
311 411
312 local $bgdsl_self = $self; 412 local $self = $arg_self;
313 413
314 local $old = $self->{state}; 414 local $old = $self->{state};
315 local $new = my $state = $self->{state} = {}; 415 local $new = my $state = $self->{state} = {};
316 416
317 my $border = 0; #d#
318
319 ($x, $y, $w, $h) = 417 ($x, $y, $w, $h) =
320 $self->background_geometry ($border); 418 $self->background_geometry ($self->{border});
321 419
322 # evaluate user expression 420 # evaluate user expression
323 421
324 my $img = eval { $self->{expr}->() }; 422 my $img = eval { $self->{expr}->() };
325 warn $@ if $@;#d# 423 warn $@ if $@;#d#
372 # prepare and set background pixmap 470 # prepare and set background pixmap
373 471
374 $img = $img->sub_rect (0, 0, $w, $h) 472 $img = $img->sub_rect (0, 0, $w, $h)
375 if $img->w != $w || $img->h != $h; 473 if $img->w != $w || $img->h != $h;
376 474
377 $self->set_background ($img, $border); 475 $self->set_background ($img, $self->{border});
378 $self->scr_recolour (0); 476 $self->scr_recolour (0);
379 $self->want_refresh; 477 $self->want_refresh;
380} 478}
381 479
382sub on_start { 480sub on_start {
383 my ($self) = @_; 481 my ($self) = @_;
384 482
483 my $expr = $self->x_resource ("background.expr")
484 or return;
485
385 $self->set_expr (parse_expr $EXPR); 486 $self->set_expr (parse_expr $expr);
487 $self->{border} = $self->x_resource_boolean ("background.border");
386 488
387 () 489 ()
388} 490}
389 491

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines