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