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