ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/background
Revision: 1.20
Committed: Wed Jun 6 22:01:45 2012 UTC (11 years, 11 months ago) by root
Branch: MAIN
Changes since 1.19: +40 -30 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! perl
2
3 #:META:X_RESOURCE:%.expr:string:background expression
4 #:META:X_RESOURCE:%.enable:boolean:some boolean
5 #:META:X_RESOURCE:%.extra.:value:extra config
6
7 our $EXPR = 'move X, Y, load "MagnoliaAlpha.png"';
8 #$EXPR = '
9 # rotate W, H, 50, 50, counter 1/59.95, repeat_mirror,
10 # clip X, Y, W, H, repeat_mirror,
11 # load "/root/pix/das_fette_schwein.jpg"
12 #';
13 #$EXPR = 'solid "red"';
14 #$EXPR = 'blur root, 10, 10'
15 #$EXPR = 'blur move (root, -x, -y), 5, 5'
16 #resize load "/root/pix/das_fette_schwein.jpg", w, h
17
18 use Safe;
19
20 our ($bgdsl_self, $old, $new);
21 our ($l, $t, $w, $h);
22
23 # enforce at least this interval between updates
24 our $MIN_INTERVAL = 1/100;
25
26 {
27 package urxvt::bgdsl; # background language
28
29 # *repeat_empty = \&urxvt::RepeatNone;
30 # *repeat_tile = \&urxvt::RepeatNormal;
31 # *repeat_pad = \&urxvt::RepeatPad;
32 # *repeat_mirror = \&urxvt::RepeatReflect;
33
34 =head2 PROVIDERS/GENERATORS
35
36 =over 4
37
38 =item load $path
39
40 =cut
41
42 sub load($) {
43 my ($path) = @_;
44
45 $new->{load}{$path} = $old->{load}{$path} || $bgdsl_self->new_img_from_file ($path);
46 }
47
48 sub root() {
49 $new->{rootpmap_sensitive} = 1;
50 die "root op not supported, exg, we need you";
51 }
52
53 sub solid($;$$) {
54 my $img = $bgdsl_self->new_img (urxvt::PictStandardARGB32, $_[1] || 1, $_[2] || 1);
55 $img->fill ($_[0]);
56 $img
57 }
58
59 =back
60
61 =head2 VARIABLES
62
63 =over 4
64
65 =cut
66
67 sub X() { $new->{position_sensitive} = 1; $l }
68 sub Y() { $new->{position_sensitive} = 1; $t }
69 sub W() { $new->{size_sensitive} = 1; $w }
70 sub H() { $new->{size_sensitive} = 1; $h }
71
72 sub now() { urxvt::NOW }
73
74 sub again($) {
75 $new->{again} = $_[0];
76 }
77
78 sub counter($) {
79 $new->{again} = $_[0];
80 $bgdsl_self->{counter} + 0
81 }
82
83 =back
84
85 =head2 OPERATORS
86
87 =over 4
88
89 =cut
90
91 # sub clone($) {
92 # $_[0]->clone
93 # }
94
95 sub clip($;$$;$$) {
96 my $img = pop;
97 $img->sub_rect ($_[0], $_[1], $_[2] || W, $_[3] || H)
98 }
99
100 sub resize($$$) {
101 my $img = pop;
102 $img->scale ($_[0], $_[1])
103 }
104
105 # TODO: ugly
106 sub move($$;$) {
107 my $img = pop->clone;
108 $img->move ($_[0], $_[1]);
109 $img
110 # my $img = pop;
111 # $img->sub_rect (
112 # $_[0], $_[1],
113 # $img->w, $img->h,
114 # $_[2],
115 # )
116 }
117
118 sub rotate($$$$$$) {
119 my $img = pop;
120 $img->rotate (
121 $_[0],
122 $_[1],
123 $_[2] * $img->w * .01,
124 $_[3] * $img->h * .01,
125 $_[4] * (3.14159265 / 180),
126 )
127 }
128
129 sub blur($$$) {
130 my ($rh, $rv, $img) = @_;
131
132 $img->blur ($rh, $rv);
133 }
134
135 sub contrast($$;$$;$) {
136 my $img = pop;
137 my ($r, $g, $b, $a) = @_;
138
139 ($g, $b) = ($r, $r) if @_ < 4;
140 $a = 1 if @_ < 5;
141
142 $img = $img->clone;
143 $img->contrast ($r, $g, $b, $a);
144 $img
145 }
146
147 sub brightness($$;$$;$) {
148 my $img = pop;
149 my ($r, $g, $b, $a) = @_;
150
151 ($g, $b) = ($r, $r) if @_ < 4;
152 $a = 1 if @_ < 5;
153
154 $img = $img->clone;
155 $img->brightness ($r, $g, $b, $a);
156 $img
157 }
158
159 =back
160
161 =cut
162
163 }
164
165 sub parse_expr {
166 my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}";
167 die if $@;
168 $expr
169 }
170
171 # compiles a parsed expression
172 sub set_expr {
173 my ($self, $expr) = @_;
174
175 $self->{expr} = $expr;
176 $self->recalculate;
177 }
178
179 # evaluate the current bg expression
180 sub recalculate {
181 my ($self) = @_;
182
183 # rate limit evaluation
184
185 if ($self->{next_refresh} > urxvt::NOW) {
186 $self->{next_refresh_timer} = urxvt::timer->new->after ($self->{next_refresh} - urxvt::NOW)->cb (sub {
187 $self->recalculate;
188 });
189 return;
190 }
191
192 $self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL;
193
194 # set environment to evaluate user expression
195
196 local $bgdsl_self = $self;
197
198 local $old = $self->{state};
199 local $new = my $state = $self->{state} = {};
200
201 ($l, $t, $w, $h) =
202 $self->get_geometry;
203
204 # evaluate user expression
205
206 my $img = eval { $self->{expr}->() };
207 warn $@ if $@;#d#
208 die if !UNIVERSAL::isa $img, "urxvt::img";
209
210 # if the expression is sensitive to external events, prepare reevaluation then
211
212 my $repeat;
213
214 if (my $again = $state->{again}) {
215 $repeat = 1;
216 $state->{timer} = $again == $old->{again}
217 ? $old->{timer}
218 : urxvt::timer->new->after ($again)->interval ($again)->cb (sub {
219 ++$self->{counter};
220 $self->recalculate
221 });
222 }
223
224 if (delete $state->{position_sensitive}) {
225 $repeat = 1;
226 $self->enable (position_change => sub { $_[0]->recalculate });
227 } else {
228 $self->disable ("position_change");
229 }
230
231 if (delete $state->{size_sensitive}) {
232 $repeat = 1;
233 $self->enable (size_change => sub { $_[0]->recalculate });
234 } else {
235 $self->disable ("size_change");
236 }
237
238 if (delete $state->{rootpmap_sensitive}) {
239 $repeat = 1;
240 $self->enable (rootpmap_change => sub { $_[0]->recalculate });
241 } else {
242 $self->disable ("rootpmap_change");
243 }
244
245 # clear stuff we no longer need
246
247 %$old = ();
248
249 unless ($repeat) {
250 delete $self->{state};
251 delete $self->{expr};
252 }
253
254 # prepare and set background pixmap
255
256 $img = $img->sub_rect (0, 0, $w, $h)
257 if $img->w != $w || $img->h != $h;
258
259 $self->set_background ($img);
260 $self->scr_recolour (0);
261 $self->want_refresh;
262 }
263
264 sub on_start {
265 my ($self) = @_;
266
267 $self->set_expr (parse_expr $EXPR);
268
269 ()
270 }
271