1 |
#! perl |
2 |
|
3 |
#:META:RESOURCE:$$:string:background expression |
4 |
#:META:RESOURCE:$$-enable:boolean:some boolean |
5 |
|
6 |
our $EXPR = 'move load "/root/pix/das_fette_schwein.jpg", repeat_wrap, X, Y'; |
7 |
$EXPR = ' |
8 |
rotate W, H, 50, 50, counter 1/59.95, repeat_mirror, |
9 |
clip X, Y, W, H, repeat_mirror, |
10 |
load "/root/pix/das_fette_schwein.jpg" |
11 |
'; |
12 |
$EXPR = 'solid "red"'; |
13 |
#$EXPR = 'blur root, 10, 10' |
14 |
#$EXPR = 'blur move (root, -x, -y), 5, 5' |
15 |
#resize load "/root/pix/das_fette_schwein.jpg", w, h |
16 |
|
17 |
use Safe; |
18 |
|
19 |
our ($bgdsl_self, $old, $new); |
20 |
our ($l, $t, $w, $h); |
21 |
|
22 |
# enforce at leats this time between updates |
23 |
our $MIN_INTERVAL = 1/100; |
24 |
|
25 |
{ |
26 |
package urxvt::bgdsl; # background language |
27 |
|
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 |
34 |
|
35 |
=over 4 |
36 |
|
37 |
=item load $path |
38 |
|
39 |
=cut |
40 |
|
41 |
sub load($) { |
42 |
my ($path) = @_; |
43 |
|
44 |
$new->{load}{$path} = $old->{load}{$path} || $bgdsl_self->new_img_from_file ($path); |
45 |
} |
46 |
|
47 |
sub root() { |
48 |
$new->{rootpmap_sensitive} = 1; |
49 |
die "root op not supported, exg, we need you"; |
50 |
} |
51 |
|
52 |
sub solid($;$$) { |
53 |
my $img = $bgdsl_self->new_img (urxvt::PictStandardARGB32, $_[1] || 1, $_[2] || 1); |
54 |
$img->fill ($_[0]); |
55 |
$img |
56 |
} |
57 |
|
58 |
=back |
59 |
|
60 |
=head2 OPERATORS |
61 |
|
62 |
=over 4 |
63 |
|
64 |
=cut |
65 |
|
66 |
# sub clone($) { |
67 |
# $_[0]->clone |
68 |
# } |
69 |
|
70 |
sub clip($$$$$;$) { |
71 |
my $img = pop; |
72 |
$img->sub_rect ($_[0], $_[1], $_[2], $_[3], $_[4]) |
73 |
} |
74 |
|
75 |
sub resize($$$) { |
76 |
my $img = pop; |
77 |
$img->scale ($_[0], $_[1]) |
78 |
} |
79 |
|
80 |
# TODO: ugly |
81 |
sub move($$;$) { |
82 |
my $img = pop; |
83 |
$img->sub_rect ( |
84 |
$_[0], $_[1], |
85 |
$img->w, $img->h, |
86 |
$_[2], |
87 |
) |
88 |
} |
89 |
|
90 |
sub rotate($$$$$$;$) { |
91 |
my $img = pop; |
92 |
$img->rotate ( |
93 |
$_[0], |
94 |
$_[1], |
95 |
$_[2] * $img->w * .01, |
96 |
$_[3] * $img->h * .01, |
97 |
$_[4] * (3.14159265 / 180), |
98 |
$_[5], |
99 |
) |
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 |
} |
147 |
|
148 |
=back |
149 |
|
150 |
=cut |
151 |
|
152 |
} |
153 |
|
154 |
sub parse_expr { |
155 |
my $expr = eval "sub {\npackage urxvt::bgdsl;\n#line 0 'background expression'\n$_[0]\n}"; |
156 |
die if $@; |
157 |
$expr |
158 |
} |
159 |
|
160 |
# compiles a parsed expression |
161 |
sub set_expr { |
162 |
my ($self, $expr) = @_; |
163 |
|
164 |
$self->{expr} = $expr; |
165 |
$self->recalculate; |
166 |
} |
167 |
|
168 |
# evaluate the current bg expression |
169 |
sub recalculate { |
170 |
my ($self) = @_; |
171 |
|
172 |
# rate limit evaluation |
173 |
|
174 |
if ($self->{next_refresh} > urxvt::NOW) { |
175 |
$self->{next_refresh_timer} = urxvt::timer->new->after ($self->{next_refresh} - urxvt::NOW)->cb (sub { |
176 |
$self->recalculate; |
177 |
}); |
178 |
return; |
179 |
} |
180 |
|
181 |
$self->{next_refresh} = urxvt::NOW + $MIN_INTERVAL; |
182 |
|
183 |
# set environment to evaluate user expression |
184 |
|
185 |
local $bgdsl_self = $self; |
186 |
|
187 |
local $old = $self->{state}; |
188 |
local $new = my $state = $self->{state} = {}; |
189 |
|
190 |
($l, $t, $w, $h) = |
191 |
$self->get_geometry; |
192 |
|
193 |
# evaluate user expression |
194 |
|
195 |
my $img = eval { $self->{expr}->() }; |
196 |
warn $@ if $@;#d# |
197 |
die if !UNIVERSAL::isa $img, "urxvt::img"; |
198 |
|
199 |
# if the expression is sensitive to external events, prepare reevaluation then |
200 |
|
201 |
my $repeat; |
202 |
|
203 |
if (my $again = $state->{again}) { |
204 |
$repeat = 1; |
205 |
$state->{timer} = $again == $old->{again} |
206 |
? $old->{timer} |
207 |
: urxvt::timer->new->after ($again)->interval ($again)->cb (sub { |
208 |
++$self->{counter}; |
209 |
$self->recalculate |
210 |
}); |
211 |
} |
212 |
|
213 |
if (delete $state->{position_sensitive}) { |
214 |
$repeat = 1; |
215 |
$self->enable (position_change => sub { $_[0]->recalculate }); |
216 |
} else { |
217 |
$self->disable ("position_change"); |
218 |
} |
219 |
|
220 |
if (delete $state->{size_sensitive}) { |
221 |
$repeat = 1; |
222 |
$self->enable (size_change => sub { $_[0]->recalculate }); |
223 |
} else { |
224 |
$self->disable ("size_change"); |
225 |
} |
226 |
|
227 |
if (delete $state->{rootpmap_sensitive}) { |
228 |
$repeat = 1; |
229 |
$self->enable (rootpmap_change => sub { $_[0]->recalculate }); |
230 |
} else { |
231 |
$self->disable ("rootpmap_change"); |
232 |
} |
233 |
|
234 |
# clear stuff we no longer need |
235 |
|
236 |
%$old = (); |
237 |
|
238 |
unless ($repeat) { |
239 |
delete $self->{state}; |
240 |
delete $self->{expr}; |
241 |
} |
242 |
|
243 |
# prepare and set background pixmap |
244 |
|
245 |
$img = $img->sub_rect (0, 0, $w, $h) |
246 |
if $img->w != $w || $img->h != $h; |
247 |
|
248 |
$self->set_background ($img); |
249 |
$self->scr_recolour (0); |
250 |
$self->want_refresh; |
251 |
} |
252 |
|
253 |
sub on_start { |
254 |
my ($self) = @_; |
255 |
|
256 |
$self->set_expr (parse_expr $EXPR); |
257 |
|
258 |
() |
259 |
} |
260 |
|