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