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