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 OPERATORS |
62 |
|
63 |
=over 4 |
64 |
|
65 |
=cut |
66 |
|
67 |
# sub clone($) { |
68 |
# $_[0]->clone |
69 |
# } |
70 |
|
71 |
sub clip($$$$$;$) { |
72 |
my $img = pop; |
73 |
$img->sub_rect ($_[0], $_[1], $_[2], $_[3], $_[4]) |
74 |
} |
75 |
|
76 |
sub resize($$$) { |
77 |
my $img = pop; |
78 |
$img->scale ($_[0], $_[1]) |
79 |
} |
80 |
|
81 |
# TODO: ugly |
82 |
sub move($$;$) { |
83 |
my $img = pop; |
84 |
$img->sub_rect ( |
85 |
$_[0], $_[1], |
86 |
$img->w, $img->h, |
87 |
$_[2], |
88 |
) |
89 |
} |
90 |
|
91 |
sub rotate($$$$$$;$) { |
92 |
my $img = pop; |
93 |
$img->rotate ( |
94 |
$_[0], |
95 |
$_[1], |
96 |
$_[2] * $img->w * .01, |
97 |
$_[3] * $img->h * .01, |
98 |
$_[4] * (3.14159265 / 180), |
99 |
$_[5], |
100 |
) |
101 |
} |
102 |
|
103 |
sub blur($$$) { |
104 |
my ($rh, $rv, $img) = @_; |
105 |
|
106 |
$img->blur ($rh, $rv); |
107 |
} |
108 |
|
109 |
sub contrast($$;$$;$) { |
110 |
my $img = pop; |
111 |
my ($r, $g, $b, $a) = @_; |
112 |
|
113 |
($g, $b) = ($r, $r) if @_ < 4; |
114 |
$a = 1 if @_ < 5; |
115 |
|
116 |
$img = $img->clone; |
117 |
$img->contrast ($r, $g, $b, $a); |
118 |
$img |
119 |
} |
120 |
|
121 |
sub brightness($$;$$;$) { |
122 |
my $img = pop; |
123 |
my ($r, $g, $b, $a) = @_; |
124 |
|
125 |
($g, $b) = ($r, $r) if @_ < 4; |
126 |
$a = 1 if @_ < 5; |
127 |
|
128 |
$img = $img->clone; |
129 |
$img->brightness ($r, $g, $b, $a); |
130 |
$img |
131 |
} |
132 |
|
133 |
sub X() { $new->{position_sensitive} = 1; $l } |
134 |
sub Y() { $new->{position_sensitive} = 1; $t } |
135 |
sub W() { $new->{size_sensitive} = 1; $w } |
136 |
sub H() { $new->{size_sensitive} = 1; $h } |
137 |
|
138 |
sub now() { urxvt::NOW } |
139 |
|
140 |
sub again($) { |
141 |
$new->{again} = $_[0]; |
142 |
} |
143 |
|
144 |
sub counter($) { |
145 |
$new->{again} = $_[0]; |
146 |
$bgdsl_self->{counter} + 0 |
147 |
} |
148 |
|
149 |
=back |
150 |
|
151 |
=cut |
152 |
|
153 |
} |
154 |
|
155 |
sub parse_expr { |
156 |
my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}"; |
157 |
die if $@; |
158 |
$expr |
159 |
} |
160 |
|
161 |
# compiles a parsed expression |
162 |
sub set_expr { |
163 |
my ($self, $expr) = @_; |
164 |
|
165 |
$self->{expr} = $expr; |
166 |
$self->recalculate; |
167 |
} |
168 |
|
169 |
# evaluate the current bg expression |
170 |
sub recalculate { |
171 |
my ($self) = @_; |
172 |
|
173 |
# rate limit evaluation |
174 |
|
175 |
if ($self->{next_refresh} > urxvt::NOW) { |
176 |
$self->{next_refresh_timer} = urxvt::timer->new->after ($self->{next_refresh} - urxvt::NOW)->cb (sub { |
177 |
$self->recalculate; |
178 |
}); |
179 |
return; |
180 |
} |
181 |
|
182 |
$self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; |
183 |
|
184 |
# set environment to evaluate user expression |
185 |
|
186 |
local $bgdsl_self = $self; |
187 |
|
188 |
local $old = $self->{state}; |
189 |
local $new = my $state = $self->{state} = {}; |
190 |
|
191 |
($l, $t, $w, $h) = |
192 |
$self->get_geometry; |
193 |
|
194 |
# evaluate user expression |
195 |
|
196 |
my $img = eval { $self->{expr}->() }; |
197 |
warn $@ if $@;#d# |
198 |
die if !UNIVERSAL::isa $img, "urxvt::img"; |
199 |
|
200 |
# if the expression is sensitive to external events, prepare reevaluation then |
201 |
|
202 |
my $repeat; |
203 |
|
204 |
if (my $again = $state->{again}) { |
205 |
$repeat = 1; |
206 |
$state->{timer} = $again == $old->{again} |
207 |
? $old->{timer} |
208 |
: urxvt::timer->new->after ($again)->interval ($again)->cb (sub { |
209 |
++$self->{counter}; |
210 |
$self->recalculate |
211 |
}); |
212 |
} |
213 |
|
214 |
if (delete $state->{position_sensitive}) { |
215 |
$repeat = 1; |
216 |
$self->enable (position_change => sub { $_[0]->recalculate }); |
217 |
} else { |
218 |
$self->disable ("position_change"); |
219 |
} |
220 |
|
221 |
if (delete $state->{size_sensitive}) { |
222 |
$repeat = 1; |
223 |
$self->enable (size_change => sub { $_[0]->recalculate }); |
224 |
} else { |
225 |
$self->disable ("size_change"); |
226 |
} |
227 |
|
228 |
if (delete $state->{rootpmap_sensitive}) { |
229 |
$repeat = 1; |
230 |
$self->enable (rootpmap_change => sub { $_[0]->recalculate }); |
231 |
} else { |
232 |
$self->disable ("rootpmap_change"); |
233 |
} |
234 |
|
235 |
# clear stuff we no longer need |
236 |
|
237 |
%$old = (); |
238 |
|
239 |
unless ($repeat) { |
240 |
delete $self->{state}; |
241 |
delete $self->{expr}; |
242 |
} |
243 |
|
244 |
# prepare and set background pixmap |
245 |
|
246 |
$img = $img->sub_rect (0, 0, $w, $h) |
247 |
if $img->w != $w || $img->h != $h; |
248 |
|
249 |
$self->set_background ($img); |
250 |
$self->scr_recolour (0); |
251 |
$self->want_refresh; |
252 |
} |
253 |
|
254 |
sub on_start { |
255 |
my ($self) = @_; |
256 |
|
257 |
$self->set_expr (parse_expr $EXPR); |
258 |
|
259 |
() |
260 |
} |
261 |
|