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